gpt4 book ai didi

c - 如何在 GCC、windows XP、x86 中编写缓冲区溢出漏洞?

转载 作者:太空狗 更新时间:2023-10-29 17:03:04 24 4
gpt4 key购买 nike

void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
int *ret;

ret = buffer1 + 12;
(*ret) += 8;//why is it 8??
}

void main() {
int x;

x = 0;
function(1,2,3);
x = 1;
printf("%d\n",x);
}

上面的demo来自这里:

http://insecure.org/stf/smashstack.html

但它在这里不起作用:

D:\test>gcc -Wall -Wextra hw.cpp && a.exe
hw.cpp: In function `void function(int, int, int)':
hw.cpp:6: warning: unused variable 'buffer2'
hw.cpp: At global scope:
hw.cpp:4: warning: unused parameter 'a'
hw.cpp:4: warning: unused parameter 'b'
hw.cpp:4: warning: unused parameter 'c'
1

虽然作者认为我不明白为什么它是 8:

A little math tells us the distance is 8 bytes.

我的 gdb 转储被称为:

Dump of assembler code for function main:
0x004012ee <main+0>: push %ebp
0x004012ef <main+1>: mov %esp,%ebp
0x004012f1 <main+3>: sub $0x18,%esp
0x004012f4 <main+6>: and $0xfffffff0,%esp
0x004012f7 <main+9>: mov $0x0,%eax
0x004012fc <main+14>: add $0xf,%eax
0x004012ff <main+17>: add $0xf,%eax
0x00401302 <main+20>: shr $0x4,%eax
0x00401305 <main+23>: shl $0x4,%eax
0x00401308 <main+26>: mov %eax,0xfffffff8(%ebp)
0x0040130b <main+29>: mov 0xfffffff8(%ebp),%eax
0x0040130e <main+32>: call 0x401b00 <_alloca>
0x00401313 <main+37>: call 0x4017b0 <__main>
0x00401318 <main+42>: movl $0x0,0xfffffffc(%ebp)
0x0040131f <main+49>: movl $0x3,0x8(%esp)
0x00401327 <main+57>: movl $0x2,0x4(%esp)
0x0040132f <main+65>: movl $0x1,(%esp)
0x00401336 <main+72>: call 0x4012d0 <function>
0x0040133b <main+77>: movl $0x1,0xfffffffc(%ebp)
0x00401342 <main+84>: mov 0xfffffffc(%ebp),%eax
0x00401345 <main+87>: mov %eax,0x4(%esp)
0x00401349 <main+91>: movl $0x403000,(%esp)
0x00401350 <main+98>: call 0x401b60 <printf>
0x00401355 <main+103>: leave
0x00401356 <main+104>: ret
0x00401357 <main+105>: nop
0x00401358 <main+106>: add %al,(%eax)
0x0040135a <main+108>: add %al,(%eax)
0x0040135c <main+110>: add %al,(%eax)
0x0040135e <main+112>: add %al,(%eax)
End of assembler dump.

Dump of assembler code for function function:
0x004012d0 <function+0>: push %ebp
0x004012d1 <function+1>: mov %esp,%ebp
0x004012d3 <function+3>: sub $0x38,%esp
0x004012d6 <function+6>: lea 0xffffffe8(%ebp),%eax
0x004012d9 <function+9>: add $0xc,%eax
0x004012dc <function+12>: mov %eax,0xffffffd4(%ebp)
0x004012df <function+15>: mov 0xffffffd4(%ebp),%edx
0x004012e2 <function+18>: mov 0xffffffd4(%ebp),%eax
0x004012e5 <function+21>: movzbl (%eax),%eax
0x004012e8 <function+24>: add $0x5,%al
0x004012ea <function+26>: mov %al,(%edx)
0x004012ec <function+28>: leave
0x004012ed <function+29>: ret

在我的情况下,距离应该是 - = 5,对吗?但它似乎不起作用..

为什么 function 需要 56 字节作为局部变量?( sub $0x38,%esp )

最佳答案

作为joveha pointed outcall指令保存在栈中(返回地址)的EIP值需要增加7字节(0x00401342 - 0x0040133b = 7) 以跳过 x = 1; 指令 (movl $0x1,0xfffffffc(%ebp) ).

你是正确的,为局部变量保留了 56 个字节 (sub $0x38,%esp),所以缺少的部分是 buffer1 之后的字节数stack是保存的EIP。


一些测试代码和内联汇编告诉我,我的测试的神奇值是 28。我无法就为什么它是 28 提供明确的答案,但我假设编译器正在添加填充和/或 stack canaries .

The following code was compiled using GCC 3.4.5 (MinGW) and tested on Windows XP SP3 (x86).


unsigned long get_ebp() {
__asm__("pop %ebp\n\t"
"movl %ebp,%eax\n\t"
"push %ebp\n\t");
}

void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
int *ret;

/* distance in bytes from buffer1 to return address on the stack */
printf("test %d\n", ((get_ebp() + 4) - (unsigned long)&buffer1));

ret = (int *)(buffer1 + 28);

(*ret) += 7;
}

void main() {
int x;

x = 0;
function(1,2,3);
x = 1;
printf("%d\n",x);
}

我可以很容易地使用 gdb 来确定这个值。

(使用 -g 编译以包含调试符号)

(gdb) break function
...
(gdb) run
...
(gdb) p $ebp
$1 = (void *) 0x22ff28
(gdb) p &buffer1
$2 = (char (*)[5]) 0x22ff10
(gdb) quit

(0x22ff28 + 4) - 0x22ff10 = 28

(ebp 值 + 字长) - buffer1 地址 = 字节数


除了Smashing The Stack For Fun And Profit , 我建议阅读我在 my answer to a previous question of yours 中提到的一些文章和/或关于该主题的其他 Material 。很好地理解这种类型的漏洞利用的确切工作原理应该可以帮助您 write more secure code .

关于c - 如何在 GCC、windows XP、x86 中编写缓冲区溢出漏洞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543725/

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