【发布时间】:2020-03-04 17:10:56
【问题描述】:
从事网络安全项目:
在 c 中编辑 .exe 文件的代码时,可以编辑其他 exe 文件的代码,但不能编辑 exe 文件本身。它会导致分段错误。
有没有办法解决这个问题?
产生分段错误的代码:
沙盒.c
#include <stdio.h>
int main(){
FILE *fp2 = fopen("sandbox", "r+");
char cbuffer [100000];
int exe_len = fread(cbuffer, 1, sizeof(cbuffer), fp2);
fwrite (cbuffer , sizeof(char), sizeof(cbuffer), fp2);
static char a[10000] = "hello goodbye";
printf("%s\n", a );
return 0;
}
不会出错的代码,还有 sandbox.c:
#include <stdio.h>
int main(){
FILE *fp2 = fopen("readme", "r+");
char cbuffer [100000];
int exe_len = fread(cbuffer, 1, sizeof(cbuffer), fp2);
fwrite (cbuffer , sizeof(char), sizeof(cbuffer), fp2);
static char a[10000] = "hello goodbye";
printf("%s\n", a );
return 0;
}
Error: Segmentation fault (core dumped)
【问题讨论】:
-
也许你需要检查
fopen()是否返回一个空指针,这意味着它打开文件失败。另外,请确保您获得了该文件的正确权限。 -
刚刚检查过了。它确实返回了一个空指针!
标签: c ubuntu gcc data-security