【发布时间】:2011-09-07 09:24:49
【问题描述】:
我正在尝试在 Mac OS X 10.6 64 位上使用 C 编写一个简单的缓冲区溢出。这是概念:
void function() {
char buffer[64];
buffer[offset] += 7; // i'm not sure how large offset needs to be, or if
// 7 is correct.
}
int main() {
int x = 0;
function();
x += 1;
printf("%d\n", x); // the idea is to modify the return address so that
// the x += 1 expression is not executed and 0 gets
// printed
return 0;
}
这是 main 的汇编程序转储的一部分:
...
0x0000000100000ebe <main+30>: callq 0x100000e30 <function>
0x0000000100000ec3 <main+35>: movl $0x1,-0x8(%rbp)
0x0000000100000eca <main+42>: mov -0x8(%rbp),%esi
0x0000000100000ecd <main+45>: xor %al,%al
0x0000000100000ecf <main+47>: lea 0x56(%rip),%rdi # 0x100000f2c
0x0000000100000ed6 <main+54>: callq 0x100000ef4 <dyld_stub_printf>
...
我想跳过 movl 指令,这意味着我需要将返回地址增加 42 - 35 = 7(对吗?)。现在我需要知道返回地址的存储位置,以便计算正确的偏移量。
我尝试手动搜索正确的值,但要么打印出 1,要么我得到 abort trap - 是否有某种缓冲区溢出保护正在进行?
在我的机器上使用 88 的偏移量。我使用了 Nemo 的方法来查找返回地址。
【问题讨论】:
-
如果你的堆栈没有被清理并恢复保存的寄存器,你可能会在主函数的寄存器中得到垃圾。谁对此负责取决于编译器使用的函数调用约定。 en.wikipedia.org/wiki/X86_calling_conventions
-
你应该把它标记为作业还是什么?您可能不希望人们认为您这样做是为了学习以外的目的。
-
@filipe:这样做了,我最初没有这样做,因为这真的很基础(你在大学第一年就学会了)。
标签: c buffer-overflow