-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetenvaddr.c
More file actions
30 lines (26 loc) · 902 Bytes
/
Copy pathgetenvaddr.c
File metadata and controls
30 lines (26 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Imprime la dirección de una variable de entorno *cuando la lee otro programa*.
//
// Compensa la diferencia de longitud de argv[0] entre este helper y el target,
// para que la dirección impresa coincida con la que verá el programa objetivo.
//
// Uso:
// ./getenvaddr SHELLCODE ./binario_objetivo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *ptr;
if (argc < 3) {
printf("Uso: %s <ENV_VAR> <programa_objetivo>\n", argv[0]);
return 1;
}
ptr = getenv(argv[1]);
if (ptr == NULL) {
fprintf(stderr, "[-] La variable %s no está exportada.\n", argv[1]);
return 1;
}
// ajuste: cada char de diferencia en argv[0] mueve el entorno 2 bytes
ptr += (strlen(argv[0]) - strlen(argv[2])) * 2;
printf("%s estará en %p (cuando lo lea %s)\n", argv[1], ptr, argv[2]);
return 0;
}