【发布时间】:2014-04-06 19:47:51
【问题描述】:
我已经完成了关于粉碎堆栈的演练。一个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。我做错了什么?你能解释一下如何正确地做到这一点以及为什么它是正确的方法吗?
谢谢。
【问题讨论】: