Linux 如何将 ELF 可执行文件转换为 C 代码?生成的 C 代码不需要是人类可读的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17247505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:16:51  来源:igfitidea点击:

How to convert an ELF executable to C code? The generated C code need not be human-readable

clinuxtransformelf

提问by Harry

I have an ELF file that I would like to decompile into Ccode, and make simple changes to the resulting Ccode and rebuild it into an ELF.

我有一个 ELF 文件,我想将其反编译为C代码,并对生成的C代码进行简单的更改并将其重建为 ELF。

The decompiled Ccode need not be fully human readable. Eg, if variables and function names come out obfuscated, it is okay.

反编译的C代码不需要是完全人类可读的。例如,如果变量和函数名称被混淆了,那没关系。

Which tools can I use to accomplish this on Linux?

我可以使用哪些工具在 Linux 上完成此任务?

PS: If decompiling to Cis not possible or is not easy, I'm willing to consider decompiling to assembly language, though tweaking the assembly source will be very difficult for me.

PS:如果反编译为C不可能或不容易,我愿意考虑反编译为汇编语言,尽管调整汇编源代码对我来说非常困难。

UPDATE:You may assume that I'm using the following Cprogram to get my a.outELF. Now, assume further that I've lost this original Csource. So, I would now like to decompile it to (a possibly obfuscated) Csource in which I'm at least able to change small things like the strings "world", "Hello", and "Bye", or be able to reverse the sense of the ifstatement, etc.

更新:您可能认为我正在使用以下C程序来获取我的a.outELF。现在,进一步假设我已经丢失了这个原始C来源。所以,我现在想将其编译到(一个可能混淆),C其中我至少能够改变像琴弦的小东西源"world""Hello""Bye",或能扭转感if等语句

#include <stdio.h>
#include <string.h>

char buf[256];

const char *Hello = "Hello";
const char *Bye = "Bye";
const char *Who = "world";

char * greet(const char *greeting, const char *str) {
    strcpy(buf, greeting);
    strcat(buf, ", ");
    strcat(buf, str);
    strcat(buf, "!");
    return buf;
}

int main(int argc, char *argv[]) {
    int sayHello = 0;

    if(sayHello) {
        printf("%s\n", greet(Hello, Who));
    } else {
        printf("%s\n", greet(Bye, Who));
    }
    return 0;   
}

回答by gcbenison

This will give you (almost) an assembly code translation:

这会给你(几乎)一个汇编代码翻译:

objdump --disassemble <elf file>

I say almost because the output contains some annotations like binary file position markers and can't serve directly as input to an assembler, but it's close.

我说几乎是因为输出包含一些注释,如二进制文件位置标记,并且不能直接作为汇编器的输入,但它很接近。

回答by Malcolm McLean

You write a simulator for the processor, then you run the elf instructions through your simulated processor. It's generally not too much of a task because even CISC processors have relatively small, contained instruction sets that perform simple operations.

你为处理器编写一个模拟器,然后通过你的模拟处理器运行精灵指令。这通常不是一项太多的任务,因为即使是 CISC 处理器也有相对较小的、包含执行简单操作的指令集。

When you've got that working, look at more efficient solutions, like outputting C code to match instructions.

当你开始工作时,看看更有效的解决方案,比如输出 C 代码来匹配指令。