【发布时间】:2016-09-19 17:45:20
【问题描述】:
我有一个易受攻击的 C 程序,它将文本文件从一个文件夹复制到另一个文件夹。 如果我们将文本增加到超过 2048 个字符,就会出现缓冲区溢出。
现在,我想通过这个包含 NOP+shell+RET ADD 的文本文件插入我的有效负载
但是,当它读取文件时,它认为文本是 ASCII 格式,并将单个字符存储在内存中。
如何通过此文本插入我的有效负载?
int copy_file(const char* src_name, const char* dst_name){
char buf[2048], *p1, *p2;
FILE *src_file, *dst_file;
int c;
if (link(src_name, dst_name) == 0) {
unlink(src_name);
return 0;
} else {
src_file = fopen(src_name, "r");
if (src_file == NULL) {
fprintf(stderr, "Failed to open source file: %s\n",
src_name);
return 1;
}
p1 = buf;
while ((c = fgetc(src_file)) != EOF) {
*p1 = c;
p1++;
}
fclose(src_file);
dst_file = fopen(dst_name, "w");
if (dst_file == NULL) {
fprintf(stderr, "Failed to open destination file: %s\n",
dst_name);
return 1;
}
p2 = buf;
while (p2 != p1) {
fputc(*p2, dst_file);
p2++;
}
fclose(dst_file);
}
return 0;
}
【问题讨论】:
-
while(fgets(buf, sizeof buf, src_file)) fputs(buf, dst_file); -
@BLUEPIXY 你没抓住重点。
-
@immibis 插入将在 OP 最喜欢的地方。请注意,条件尚未提出。
-
如何在我的文本文件中插入 0x90....shellcode ...ret 地址?基本上我能做的就是创建一个文本文件并作为争论传递......例如。 ./submit myfile.txt
-
您是否考虑过同时打开两个文件并使用 read() 和 write() 函数?它会使您的程序更短,您可以避免更大文件的缓冲区溢出。放置 shellcode 会容易得多: unsigned char code = 0x90;然后将其写入文件。
标签: c buffer-overflow