gpt4 book ai didi

c - 我的溢出代码不起作用

转载 作者:太空狗 更新时间:2023-10-29 12:21:03 26 4
gpt4 key购买 nike

以下代码来自知名文章Smashing The Stack For Fun And Profit .

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

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

我想我必须解释一下这段代码的目标。堆栈模型如下。单词下面的数字是堆栈中变量的字节数。所以,如果我想重写 RET 以跳过我想要的语句,我计算从 buffer1 的偏移量。 RET 是 8+4=12。由于架构是 x86 Linux。

buffer2 buffer1 BSP RET   a    b    c
(12) (8) (4) (4) (4) (4) (4)

我想跳过语句 x=1;printf()输出 0在屏幕上。

我编译代码:

gcc  stack2.c  -g

并在 gdb 中运行:

gdb ./a.out

gdb 给我这样的结果:

Program received signal SIGSEGV, Segmentation fault.
main () at stack2.c:17
17 x = 1;

我认为 Linux 使用某种机制来防止堆栈溢出。也许 Linux 将 RET 地址存储在另一个地方,并在函数返回之前比较堆栈中的 RET 地址。

这个机制的细节是什么?我应该如何重写代码才能使程序输出0

好的,反汇编代码在下面。它来自 gdb 的输出,因为我认为对您来说更容易阅读。谁能告诉我如何粘贴长代码序列?一个一个地复制和粘贴让我太累了……

Dump of assembler code for function main:
0x08048402 <+0>: push %ebp
0x08048403 <+1>: mov %esp,%ebp
0x08048405 <+3>: sub $0x10,%esp
0x08048408 <+6>: movl $0x0,-0x4(%ebp)
0x0804840f <+13>: movl $0x3,0x8(%esp)
0x08048417 <+21>: movl $0x2,0x4(%esp)
0x0804841f <+29>: movl $0x1,(%esp)
0x08048426 <+36>: call 0x80483e4 <function>
0x0804842b <+41>: movl $0x1,-0x4(%ebp)
0x08048432 <+48>: mov $0x8048520,%eax
0x08048437 <+53>: mov -0x4(%ebp),%edx
0x0804843a <+56>: mov %edx,0x4(%esp)
0x0804843e <+60>: mov %eax,(%esp)
0x08048441 <+63>: call 0x804831c <printf@plt>
0x08048446 <+68>: mov $0x0,%eax
0x0804844b <+73>: leave
0x0804844c <+74>: ret


Dump of assembler code for function function:
0x080483e4 <+0>: push %ebp
0x080483e5 <+1>: mov %esp,%ebp
0x080483e7 <+3>: sub $0x14,%esp
0x080483ea <+6>: lea -0x9(%ebp),%eax
0x080483ed <+9>: add $0x3,%eax
0x080483f0 <+12>: mov %eax,-0x4(%ebp)
0x080483f3 <+15>: mov -0x4(%ebp),%eax
0x080483f6 <+18>: mov (%eax),%eax
0x080483f8 <+20>: lea 0x8(%eax),%edx
0x080483fb <+23>: mov -0x4(%ebp),%eax
0x080483fe <+26>: mov %edx,(%eax)
0x08048400 <+28>: leave
0x08048401 <+29>: ret

我检查了汇编代码,发现我的程序有一些错误,我重写了(*ret)+=8(*ret)+=7 , 因为 0x08048432 <+48>0x0804842b <+41>是 7.

最佳答案

因为那篇文章是 1996 年的,假设不正确。

请参阅“粉碎现代堆栈以获得乐趣和利润”

http://www.ethicalhacker.net/content/view/122/24/

来自上面的链接:

However, the GNU C Compiler (gcc) has evolved since 1998, and as a result, many people are left wondering why they can't get the examples to work for them, or if they do get the code to work, why they had to make the changes that they did.

关于c - 我的溢出代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399967/

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