I'm practicing doing buffer overflows and, weirdly, I can do some but not others and it is not clear to me why. Here is the example code:
Code that I need help with::
#include <stdio.h>
int main(int argc, char * argv[]) {
char buf[256];
if(argc == 1) {
printf("Usage: %s input\n", argv[0]);
exit(0);
}
strcpy(buf,argv[1]);
printf("%s", buf);
}
I compile this code with gcc -z exestack -fno-no-stack-protector
Questions:
I understand that the general technique is to write to a buffer such that I overwrite where the next instruction register looks in memory. So my question is, where do I insert a breakpoint in this code to figure out where to overwrite? If I do disas main I get:
0x080497b1 <+60>: add $0x10,%esp
0x080497b4 <+63>: sub $0xc,%esp
0x080497b7 <+66>: push $0x0
0x080497b9 <+68>: call 0x804fd50
0x080497be <+73>: mov 0x4(%eax),%eax
0x080497c1 <+76>: add $0x4,%eax
0x080497c4 <+79>: mov (%eax),%eax
0x080497c6 <+81>: sub $0x8,%esp
0x080497c9 <+84>: push %eax
0x080497ca <+85>: lea -0x108(%ebp),%eax
0x080497d0 <+91>: push %eax
0x080497d1 <+92>: call 0x8049028
0x080497d6 <+97>: add $0x10,%esp
0x080497d9 <+100>: sub $0x8,%esp 0x080497dc <+103>: lea -0x108(%ebp),%eax
If I add a breakpoint at *main+97, and then run AAAAAA I get the following:
Breakpoint 1, 0x080497d6 in main ()
(gdb) info registers
eax 0xbffff1f0 -1073745424
ecx 0xbffff575 -1073744523
edx 0xbffff1f0 -1073745424
ebx 0x80da000 135110656
esp 0xbffff1e0 0xbffff1e0
ebp 0xbffff2f8 0xbffff2f8
esi 0x80da000 135110656
edi 0x80481e8 134513128
eip 0x80497d6 0x80497d6 <main+97>
eflags 0x246 [ PF ZF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
Am I putting the breakpoint in the right place to help with this overflow? Right after the call to strcpy? Also, I see that ebp is at 0xbffff2f8. Does this mean that I want to overwrite the address right next to this at 0xbffff2fc to point back inside the buffer and I will be done? Because that does not appear to work: my buffer is a NOP sled and shell code.
Thanks for any help!