user@protostar:/opt/protostar/bin$ gdb -q stack5 Reading symbols from /opt/protostar/bin/stack5...done. (gdb) disass main Dump of assembler code forfunction main: 0x080483c4 <main+0>: push %ebp 0x080483c5 <main+1>: mov %esp,%ebp 0x080483c7 <main+3>: and $0xfffffff0,%esp 0x080483ca <main+6>: sub $0x50,%esp 0x080483cd <main+9>: lea 0x10(%esp),%eax 0x080483d1 <main+13>: mov %eax,(%esp) 0x080483d4 <main+16>: call 0x80482e8 <gets@plt> 0x080483d9 <main+21>: leave 0x080483da <main+22>: ret End of assembler dump. (gdb) break main Breakpoint 1 at 0x80483cd: file stack5/stack5.c, line 10. (gdb) break *main+21 Breakpoint 2 at 0x80483d9: file stack5/stack5.c, line 11. (gdb) r Starting program: /opt/protostar/bin/stack5
Breakpoint 1, main (argc=1, argv=0xbffff874) at stack5/stack5.c:10 10 stack5/stack5.c: No such file or directory. in stack5/stack5.c (gdb) i r eax 0xbffff874 -1073743756 ecx 0x66e21e45 1726094917 edx 0x1 1 ebx 0xb7fd7ff4 -1208123404 esp 0xbffff770 0xbffff770 ebp 0xbffff7c8 0xbffff7c8 esi 0x0 0 edi 0x0 0 eip 0x80483cd 0x80483cd <main+9> eflags 0x200282 [ SF IF ID ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) x/x $esp 0xbffff770: 0xb7fd7ff4 (gdb) c Continuing. AAAAAAAAAAAAAAAAAAAA
Breakpoint 2, main (argc=1, argv=0xbffff874) at stack5/stack5.c:11 11 in stack5/stack5.c (gdb) x/x $esp 0xbffff770: 0xbffff780 (gdb) i r $ebp ebp 0xbffff7c8 0xbffff7c8 (gdb) p $ebp +0x4 - 0xbffff780 $1 = (void *) 0x4c (gdb) p 0x4c $2 = 76 (gdb)
I’m placing two breakpoints here but only one is necessary : the one after the call to gets. After the program hits the breakpoint, I need to check both %esp and %ebp registers in order to get the offset.
The offset of the %eip register is of 76b.
We also need to know where the buffer is being written.