简体   繁体   English

使用GCC -march = i686反向工程C代码

[英]Reverse engineering C codes with GCC -march=i686

Here're my C codes: 这是我的C代码:

int test(int x, int y){
    int val=4*x;
    if (y>0){
        if (x<y) val=x-y;
        else val=x^y;
    }
    else if (y<-2) val=x+y;
    return val;
}

And here're what I entered in the GCC command line: 这是我在GCC命令行中输入的内容:

gcc -O1 -S -march=i686 test.c

And here is the S file I got (only the calculation part): 这是我得到的S文件(只有计算部分):

pushl   %ebx
movl    8(%esp), %ecx
movl    12(%esp), %edx
testl   %edx, %edx
jle     L2
movl    %ecx, %eax
subl    %edx, %eax
movl    %edx, %ebx
xorl    %ecx, %ebx
cmpl    %edx, %ecx
cmovge  %ebx, %eax
jmp     L4

L2:
leal    0(,%ecx,4), %eax
addl    %edx, %ecx
cmpl    $-2, %edx
cmovl   %ecx, %eax

L4:
popl    %ebx
ret

My question is: can I get back exactly the same C codes using the S file above? 我的问题是:我可以使用上面的S文件找回完全相同的C代码吗? I mean EXACTLY the same. 我的意思是完全一样的。 For example can I determine the default value of val is 4*x (line #2 of C codes)? 例如,我可以确定val的默认值是4*x (C代码的第2行)吗? Can I determine the test-expression of each if statement? 我可以确定每个if语句的test-expression吗?

I really need your help. 我真的需要你的帮助。 Thank you!!! 谢谢!!!

In this case, you can find out that the each registers corresponding to a variable: 在这种情况下,您可以发现每个寄存器对应一个变量:

  • %eax - var %eax - var
  • %ebx - a contextual temporary variable %ebx - 上下文临时变量
  • %ecx - x %ecx - x
  • %edx - y %edx - y

If you mean 'exactly' for the identifiers, it is only possible when a special structure named 'symbol table'. 如果对标识符的意思是“确切”,则只有在名为“符号表”的特殊结构时才有可能。 (compiled with -g flag in GCC) (在GCC中使用-g标志编译)

Anyway, you should know a code can be always optimized by the compiler. 无论如何,您应该知道编译器始终可以优化代码。 It means, in this case, your code is changed to another having same mathematical meaning. 这意味着,在这种情况下,您的代码将更改为具有相同数学含义的另一个代码。 If your code is backward-translated, it should be like this. 如果你的代码是后向翻译的,那应该是这样的。

int test(int x, int y) {
    int val;
    if (y > 0) {
        if (x < y)
            val = x - y;
        else
            val = x ^ y;
    } else {
        if (y < -2)
            val = x + y;
        else
            val = 4 * x;
    }
    return val;
}

If you want no optimization, use flag -O0 instead of -O1 . 如果您不想进行优化,请使用标志-O0而不是-O1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM