你的问题
It pushes ebp, then copies esp to ebt
ebt 是错误的,没有调用 ebt 的寄存器。应该是ebp。
push ebp
该命令用于在调用 test_function 函数之前保留堆栈底部。
mov ebp,esp
sub esp,0x28
该命令用于在 test_function 函数执行期间增加堆栈以分配新的内存块来存储值。并且栈增加的大小不是固定的,不会一直是0x28。该值由编译器计算,以确保大小合适。
mov DWORD PTR [ebp-12],0x7a69
对应flag=31337,flag地址等于ebp-12的值。地址由编译器分配。 DWORD对应于int。十六进制数0x7a69相当于十进制数31337。
And
mov BYTE PTR [ebp-40],0x41
与相同。十六进制数0x41相当于十进制数65。字母A的ASCII码的十进制值为十进制数65。BYTE对应于char。
下面的代码
(gdb) x/16xw $esp
0xbffff7c0: (1)0x00000000 0x08049548 0xbffff7d8 0x08048249
0xbffff7d0: 0xb7f9f729 0xb7fd6ff4 0xbffff808 0x080483b9
0xbffff7e0: 0xb7fd6ff4 (2)0xbffff89c (3)0xbffff808 (4)0x0804838b
0xbffff7f0: (5)0x00000001 0x00000002 0x00000003 0x00000004
如0xbffff7c0:左侧代码为地址,其值为0x00000000。所以 0xbffff7c4(0xbffff7c4 = 0xbffff7c0 + 4) 地址的值为 0x08049548,因为 0x00000000 占用 4 个字节。 0xbffff7c8地址的值为0xbffff7d8。 ....
我认为下面的汇编代码
Dump of assembler code for function test_function:
0x08048344 <test_function+0>: push ebp
0x08048345 <test_function+1>: mov ebp,esp
0x08048347 <test_function+3>: sub esp,0x28
0x0804834a <test_function+6>: mov DWORD PTR [ebp-12],0x7a69
0x08048351 <test_function+13>: mov BYTE PTR [ebp-40],0x41
0x08048355 <test_function+17>: leave
0x08048356 <test_function+18>: ret
可以转换下面的代码(@Peter Cordes 指出“leave”等于“mov esp,ebp”+“pop ebp”,请参见https://www.felixcloutier.com/x86/leave)
Dump of assembler code for function test_function:
0x08048344 <test_function+0>: push ebp
0x08048345 <test_function+1>: mov ebp,esp
0x08048347 <test_function+3>: sub esp,0x28
0x0804834a <test_function+6>: mov DWORD PTR [ebp-12],0x7a69
0x08048351 <test_function+13>: mov BYTE PTR [ebp-40],0x41
mov esp,ebp // here changed
pop ebp // here changed
0x08048356 <test_function+18>: ret
局部变量存储在堆栈中。
这样我们就可以得到一张图,如下图所示: