我一直致力于SEED 的缓冲区溢出漏洞实验室(实验室描述和任务)。环境是 Ubuntu 12.04 32 位。请考虑以下代码:
/* stack.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
char buffer[24];
/* The following statement has a buffer overflow problem */
strcpy(buffer, str);
return 1;
}
int main(int argc, char **argv)
{
char str[517];
FILE *badfile;
badfile = fopen("badfile", "r");
fread(str, sizeof(char), 517, badfile);
bof(str);
printf("Returned Properly\n");
return 1;
}
我发现为了覆盖返回地址,我们必须从第 36 位开始覆盖数组,因为我们必须跳转 24 个字节(数组的大小)加上 4 个字节(用于前一帧指针)加上 8 个字节一些编译器数据(未指定用于什么目的)。我用 EBP 和缓冲区地址的差异计算了这个(使用调试器)。
我意识到为了跳转到恶意代码,我们可以跳转到 NOP sled 上的任何地址。对于足够大的值,攻击成功。我试图计算最小的可能地址。逻辑(和讲师)说它是 EBP + 8,因为我们想同时传递返回地址和前一个帧指针(每个 4 个字节)。不幸的是,当我尝试使用该值运行它时,攻击没有成功。我通过详尽的搜索发现最小可能的地址0xBFFFF168是 EBP + 48,我不明白为什么。我什至尝试在没有 NOP 雪橇的情况下运行攻击,但遇到了问题。
有人可以解释一下吗?下面是溢出前后堆栈的描述以及一些附加值。
非常感谢您提前。
14 strcpy(buffer, str);
(gdb) x/20x $sp
0xbffff100: 0x0804b008 0xbffff157 0x00000205 0xb7e34374
0xbffff110: 0xb7fc4ff4 0xb7fc4ff4 0x00000000 0xb7e1f900
0xbffff120: 0xbffff368 0xb7ff26b0 0x0804b008 0xb7fc4ff4
0xbffff130: 0x00000000 0x00000000 0xbffff368 0x080484ff
0xbffff140: 0xbffff157 0x00000001 0x00000205 0x0804b008
(gdb) next
16 return 1;
(gdb) x/20x $sp
0xbffff100: 0xbffff118 0xbffff157 0x00000205 0xb7e34374
0xbffff110: 0xb7fc4ff4 0xb7fc4ff4 0x90909090 0x90909090
0xbffff120: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff130: 0x90909090 0x90909090 0x90909090 0xbffff168
0xbffff140: 0x90909090 0x90909090 0x90909090 0x90909090
(gdb) p &buffer
$1 = (char (*)[24]) 0xbffff118
(gdb) p $ebp
$2 = (void *) 0xbffff138
(gdb) p $sp
$3 = (void *) 0xbffff100