gpt4 book ai didi

c - 粉碎堆栈不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:33 24 4
gpt4 key购买 nike

我已经完成了关于粉碎堆栈的演练。两者都是http://insecure.org/stf/smashstack.html这里和我在这里找到的一个Trying to smash the stack .我知道应该发生什么,但我无法让它正常工作。

这与其他场景一样。我需要跳过 x=1 并打印 0 作为 x 的值。

我编译:

gcc file.c

原代码:

void function(){
char buffer[8];
}

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

当我运行时

objdump -dS a.out

我明白了

0000000000400530 <function>:
400530: 55 push %rbp
400531: 48 89 e5 mov %rsp,%rbp
400534: 5d pop %rbp
400535: c3 retq

0000000000400536 <main>:
400536: 55 push %rbp
400537: 48 89 e5 mov %rsp,%rbp
40053a: 48 83 ec 20 sub $0x20,%rsp
40053e: 89 7d ec mov %edi,-0x14(%rbp)
400541: 48 89 75 e0 mov %rsi,-0x20(%rbp)
400545: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
40054c: b8 00 00 00 00 mov $0x0,%eax
400551: e8 da ff ff ff callq 400530 <function>
400556: c7 45 fc 01 00 00 00 movl $0x1,-0x4(%rbp)
40055d: 8b 45 fc mov -0x4(%rbp),%eax
400560: 89 c6 mov %eax,%esi
400562: bf 10 06 40 00 mov $0x400610,%edi
400567: b8 00 00 00 00 mov $0x0,%eax
40056c: e8 9f fe ff ff callq 400410 <printf@plt>
400571: c9 leaveq
400572: c3 retq
400573: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
40057a: 00 00 00
40057d: 0f 1f 00 nopl (%rax)

在函数中,我需要计算出返回地址超出缓冲区开头的字节数。我不确定这个值。但是由于从函数开始到返回有 6 个字节;我会向缓冲区添加 7 个字节吗?

然后我需要跳过指令 x=1;由于该指令有 7 个字节长。我会加 7 来返回指针吗?

是这样的吗?

void function(){
char buffer[8];
int *ret = buffer + 7;
(*ret) += 7;
}

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

这会引发警告:

warning: initialization from incompatible pointer type [enabled by default]
int *ret = buffer1 + 5;
^

输出是 1。我做错了什么?你能解释一下如何正确地做到这一点以及为什么它是正确的方法吗?

谢谢。

最佳答案

尝试下面的函数,我为 32 位编译器编写它尝试使用 (-m32 gcc flag) 或者稍微努力一下,你可以让它与你的 64 位编译器一起工作(注意在您的 objdump 列表中,您在调用 function 和下一条指令之间有 7 字节偏移量,因此请使用 7 而不是8

void function(void)
{
unsigned long *x;
/* &x will more likely be at -4(ebp) */
/* Adding 1 (+4) gets us to stored ebp */
/* Adding 2 (+8) gets us to stored return address */
x = (unsigned long *)(&x + 2);

/* This is the tricky part */
/* TODO: On my 32-bit compiler gap between call to function
and the next instruction is 8 */
*x += 8;
}

关于c - 粉碎堆栈不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22186130/

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