【发布时间】:2014-01-25 07:51:46
【问题描述】:
我正在致力于检测和防止 BOF 攻击,我想知道,如何使全局结构溢出?
我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct{
char name[20];
char description[10];
} test;
int main(int argc, char **argv){
if(argc != 2)
exit(-1);
*(*(argv+1)+20) = '\x00'; //terminate string after 20 characters
strcpy(test.name, argv[1]); //no BOF here... stopped at 20
printf("%s\n", test.name);
char *desc;
desc = malloc(10);
if(!desc){
printf("Error allocating memory\n");
exit(-1);
}
scanf("%s", desc); //no bounds checking - this is where I BOF
strcpy(test.description, desc); //copy over 10 characters into 10 char buffer
printf("%s\n", test.description); //this prints out whatever I type in
//even thousands of characters, despite it having a buffer of 10 chars
}
【问题讨论】:
-
首先,如果你真的想防止缓冲区溢出,请不要在生产中使用
scanf()。fegts()救援! -
对于我的测试来说,这更像是一个易受攻击的文件。
-
溢出全局缓冲区的方式与执行任何其他缓冲区类型的方式相同;您在其中存储的数据多于为其分配的字节数。也许问题是“这会造成什么损害”,答案很常见:这取决于。基本上,当您溢出特定的全局缓冲区时,您会覆盖一些其他全局变量,接下来会发生什么取决于是否再次引用另一个变量以及它应该保存的内容。它通常没有函数返回地址等,因此更难利用。
-
@JonathanLeffler 你应该复制/粘贴它作为答案。非常简短而直接。完美。
-
argv[1]指向的内存可写 20 个字符的假设本身就是一个延伸。
标签: c global-variables buffer buffer-overflow