【问题标题】:Segmentation Fault when editing code of executable file编辑可执行文件代码时出现分段错误
【发布时间】: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


【解决方案1】:

无法打开当前以 'r+' 模式运行的 exe 文件。这就是为什么当打开另一个名称的第二个文件时,它会产生 Seg Fault。而是做以下工作:

用不同的名称保存文件,然后使用 mv 更新名称并使用 chmod 生成可执行文件:

FILE *fp3 = fopen("x.x","w+");
fwrite (ebuffer , sizeof(char), sizeof(ebuffer), fp3);
fclose(fp3);
system("mv x.x readme; chmod +x readme");

这最终解决了。这需要#include &lt;stdlib.h&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多