I am working through Erickson's "The Art of Exploitation" and failing to reproduce an exploit.
Shellcode is stored in an environmental variable "SHELLCODE", which is then executed by a program with root access. The Book suggests, that the address stays roughly the same for each call of the program (depending on the length of the program name) and uses the following code to find it:
getenv.c
#include ...
int main(int argc, char *argv[]) {
printf("SHELLCODE is at %p\n", getenv(SHELLCODE));
}
However, on my linux virtual machine (gcc (Debian 10.2.1-6) 10.2.1) the address changes wildly with each call of the program:
cream@debian$ ./getenv
SHELLCODE is at 0xbf955414
cream@debian$ ./getenv
SHELLCODE is at 0xbfa4b414
cream@debian$ ./getenv
SHELLCODE is at 0xbfe94414
Why is this the case and how can I avoid it?
Or alternatively, how would one modify the exploit to get it to work despite an unpredictable SHELLCODE address before program call.
Some detail on the exlpoit:
"SHELLCODE" contains a NOP sled of reasonable size ending with the shellcode. To get root access, one now runs a program (notesearch
) with root access and, using a buffer overflow, overwrites the return address of the stack pointer with an address on the NOP sled.
This, in turn, executes the shellcode as root:
cream@debian$ ./notesearch $(perl -e 'print "\x47\xf9\xff\xbf"x40')
It is necessary to know the address of the SHELLCODE
variable (\x47\xf9\xff\xbf
in the above) to work the exploit.
This question is on the same exploit with a similar problem. There, the only answer assumes that the address changes between gdb and shell (it is never clarified if this solves the problem). I see a changing address in the shell alone.