簡體   English   中英

將匯編代碼反向工程為c代碼

[英]Reverse engineer assembly code to c code

我認為這實際上是一個非常簡單的問題。 我必須將此匯編代碼反向工程為c代碼。 我還會提供我認為正在發生的事情,這樣你就可以指出我哪里出錯了,現在我可以從錯誤中吸取教訓。

.LFBO
    pushq   %rbp
    movq    %rsp,%rbp
    movl    %edi,-4(%rbp)
    movl    %esi,-8(%rbp)
    movl    -4(%rbp),%eax
    compl   -8(%rbp),%eax
    jg      .L2
    movl    -8(%rbp),%eax
    jmp     .L3
.L2:
    movl    -4(%rbp),%eax
.L3:
    popq    %rbp
    ret

所以這就是我認為正在發生的事情:.LFBO之后的前兩行:

pushq   %rbp
movq    %rsp,%rbp

只是為即將執行的執行設置堆棧。

movl    %edi,-4(%rbp)

抓住第一個變量,稱之為x

movl    %esi,-8(%rbp)

抓住第二個變量稱之為y

movl    -4(%rbp),%eax

抓住x在下一行進行比較

compl   -8(%rbp),%eax

通過計算xy來比較變量x和y

jg      .L2

如果x> y,則跳轉到.L2

如果x <= y則計算下一行而不跳轉到.L2

movl    -8(%rbp),%eax

復制x = y

jmp     .L3

跳到.L3

如果x> y在jg行,那么你跳轉到.L2:並完成這一行

movl    -4(%rbp),%eax

這是我意識到我真的很困惑的地方。 它在我看來你正在將x復制到x然后.L3已經完成,我認為x是返回的

不要過度思考它。 只需逐漸用C替換組件。這是一個可能的轉換序列。

.LFBO
    pushq   %rbp
    movq    %rsp,%rbp
    movl    %edi,-4(%rbp)
    movl    %esi,-8(%rbp)
    movl    -4(%rbp),%eax
    compl   -8(%rbp),%eax
    jg      .L2
    movl    -8(%rbp),%eax
    jmp     .L3
.L2:
    movl    -4(%rbp),%eax
.L3:
    popq    %rbp
    ret

----

int LFBO (int edi, int esi)
{
    rbp = rsp
    [rbp - 4] = edi
    [rbp - 8] = esi
    eax = [rbp - 4]
    if (eax > [rbp - 8]) goto L2
    eax = [rbp - 8]
    goto L3
L2:
    eax = [rbp - 4]
L3:
    return eax
}

----

int LFBO (int edi, int esi)
{
    int eax;

    eax = edi;
    if (eax > esi) goto L2;
    eax = esi;
    goto L3;
L2:
    eax = edi;
L3:
    return eax;
}

----    

int LFBO (int edi, int esi)
{
    int eax;

    eax = edi;
    if (eax <= esi) {
        eax = esi;
    }
    else {
        eax = edi;
    }
    return eax;
}

----

int LFBO (int edi, int esi)
{
    if (edi <= esi) {
        return esi;
    }
    else {
        return edi;
    }
}

----

int LFBO (int x, int y)
{
    if (x <= y) {
        return y;
    }
    else {
        return x;
    }
}

----

int LFBO (int x, int y)
{
    return (x > y) ? x : y;
}

您可以將此策略應用於任何裝配體。 在這里,我花時間詳細介紹了各種轉換。 通過練習,您可以更快地獲得最終結果。

LFB0(int x, int y){
    if (x<=y){
        x = y;
    }else{
        x = x;
    }
    return(x);
}

在評論的幫助下,我認為這是正確的。

.LFBO
    pushq   %rbp                prolog
    movq    %rsp,%rbp           prolog
    movl    %edi,-4(%rbp)       [ebp-4] = edi
    movl    %esi,-8(%rbp)       [ebp-8] = esi
    movl    -4(%rbp),%eax       eax = [ebp-4] ie edi
    compl   -8(%rbp),%eax       cmp eax with [ebp-8] ie esi 
    jg      .L2                 ;jg requires <= 
    movl    -8(%rbp),%eax       so cutting the junk 
    jmp     .L3                 this effectively becomes
.L2:
    movl    -4(%rbp),%eax       ( edi <= esi ) ? { eax = esi } : { eax= edi} ; return;
.L3:
    popq    %rbp                epilog
    ret                         epilog

檢驗假設

lets compile the code in vc and test should compile unoptimized else 
clever compiler will cast away everything do 
/O1 push 10 pop eax retn;
/O2 mov eax ,10  ret

int main(void) {
int edi=8,esi=10;
if ( edi <= esi) { return esi; } else { return edi;}
}

拆解結果

0:000> uf @eip
image00400000+0x1000:
00401000 55              push    ebp
00401001 8bec            mov     ebp,esp
00401003 83ec08          sub     esp,8
00401006 c745fc08000000  mov     dword ptr [ebp-4],8
0040100d c745f80a000000  mov     dword ptr [ebp-8],0Ah
00401014 8b45fc          mov     eax,dword ptr [ebp-4]
00401017 3b45f8          cmp     eax,dword ptr [ebp-8]
0040101a 7f07            jg      image00400000+0x1023 (00401023)

image00400000+0x101c:
0040101c 8b45f8          mov     eax,dword ptr [ebp-8]
0040101f eb05            jmp     image00400000+0x1026 (00401026)

image00400000+0x1023:
00401023 8b45fc          mov     eax,dword ptr [ebp-4]

image00400000+0x1026:
00401026 8be5            mov     esp,ebp
00401028 5d              pop     ebp
00401029 c3              ret
0:000>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM