我开始 tinkering 超出缓冲区,编写以下程序:
#include <unistd.h>
void g() {
execve("/bin/sh", NULL, NULL);
}
void f() {
long *return_address;
char instructions[] = "\xb8\x01\x00\x00\x00\xcd\x80"; // exit(1)
return_address = (long*) (&return_address + 2);
*return_address = (long)&g; // or (long)instructions
}
int main() {
f();
}
它做了我期望的事情:
return_address
用 g
的地址覆盖了 f
的返回地址,从而打开了一个 shell。但是,如果我将返回地址设置为 instructions
,就会出现分段错误,并且 instructions
中的指令都不会被执行。
我使用 GCC 进行编译,使用 -fno-stack-protector
。
我该如何防止这种分段错误的发生?