簡體   English   中英

逆向工程一種遞歸函數

[英]Reverse Engineering a recursive function

我有作為扭轉功能的功課

匯編程序輸出如下:

0x00000000004010c4 <+0>:    sub    $0x18,%rsp
0x00000000004010c8 <+4>:    lea    0xc(%rsp),%rcx
0x00000000004010cd <+9>:    lea    0x8(%rsp),%rdx
0x00000000004010d2 <+14>:   mov    $0x402995,%esi
0x00000000004010d7 <+19>:   mov    $0x0,%eax
0x00000000004010dc <+24>:   callq  0x400cb0 <__isoc99_sscanf@plt>
0x00000000004010e1 <+29>:   cmp    $0x2,%eax
0x00000000004010e4 <+32>:   jne    0x4010ed <phase_4+41>
0x00000000004010e6 <+34>:   cmpl   $0xe,0x8(%rsp)
0x00000000004010eb <+39>:   jbe    0x4010f2 <phase_4+46>
0x00000000004010ed <+41>:   callq  0x401671 <explode_bomb>
0x00000000004010f2 <+46>:   mov    $0xe,%edx
0x00000000004010f7 <+51>:   mov    $0x0,%esi
0x00000000004010fc <+56>:   mov    0x8(%rsp),%edi
0x0000000000401100 <+60>:   callq  0x401086 <func4>
0x0000000000401105 <+65>:   cmp    $0x3,%eax
0x0000000000401108 <+68>:   jne    0x401111 <phase_4+77>
0x000000000040110a <+70>:   cmpl   $0x3,0xc(%rsp)
0x000000000040110f <+75>:   je     0x401116 <phase_4+82>
0x0000000000401111 <+77>:   callq  0x401671 <explode_bomb>
0x0000000000401116 <+82>:   add    $0x18,%rsp
0x000000000040111a <+86>:   retq   

我對此函數的解決方案如下所示:我認為func4應該返回3

int phase4(const char* read ) {
int var1, var2;
if ((sscanf(read, "%d %d", &var1, &var2) != 2) || (var1 < 0xe))
    explode_bomb();

if (func4(var1, 0, 0xe /*14*/) != 3)
    explode_bomb();

if (var2 != 3)
    explode_bomb();
return 3;
}

func4看起來像這樣:

0x0000000000401086 <+0>:    sub    $0x8,%rsp
0x000000000040108a <+4>:    mov    %edx,%eax
0x000000000040108c <+6>:    sub    %esi,%eax
0x000000000040108e <+8>:    mov    %eax,%ecx
0x0000000000401090 <+10>:   shr    $0x1f,%ecx
0x0000000000401093 <+13>:   add    %ecx,%eax
0x0000000000401095 <+15>:   sar    %eax
0x0000000000401097 <+17>:   lea    (%rax,%rsi,1),%ecx
0x000000000040109a <+20>:   cmp    %edi,%ecx
0x000000000040109c <+22>:   jle    0x4010aa <func4+36>
0x000000000040109e <+24>:   lea    -0x1(%rcx),%edx
0x00000000004010a1 <+27>:   callq  0x401086 <func4>
0x00000000004010a6 <+32>:   add    %eax,%eax
0x00000000004010a8 <+34>:   jmp    0x4010bf <func4+57>
0x00000000004010aa <+36>:   mov    $0x0,%eax
0x00000000004010af <+41>:   cmp    %edi,%ecx
0x00000000004010b1 <+43>:   jge    0x4010bf <func4+57>
0x00000000004010b3 <+45>:   lea    0x1(%rcx),%esi
0x00000000004010b6 <+48>:   callq  0x401086 <func4>
0x00000000004010bb <+53>:   lea    0x1(%rax,%rax,1),%eax
0x00000000004010bf <+57>:   add    $0x8,%rsp
0x00000000004010c3 <+61>:   retq   

我的c代碼看起來像這樣:

int func4(unsigned rsi, unsigned rdi, unsigned rdx) {
unsigned rax = rdx;
rax -= rsi;
unsigned rcx = rax;
rcx >>= (unsigned)0x1f;
rax += rcx;
rax >>= (signed)1;
rcx = rax + rsi;
if (rcx <= rdi) {
    rax = 0;
    if (rcx >= rdi)
        return rax;
    else {
        rax = func4(rdi, rsi + 1, rdx);
        rax = rax + rax + 1;
    }
} else {
    rdx = rcx - 1;
    rax = func4(rdi, rsi, rdx);
    rax = rax + rax;
}
return rax;
}

但是當我嘗試從-512到512的值時,我從來沒有得到3; 我究竟做錯了什么?

編輯:

我發現它看起來像這樣的解決方案:

int func4(int32_t di, int32_t si, int32_t dx) {
int32_t ax = dx;
ax = ax - si;
int32_t cx = ax;
cx = (uint32_t)cx >> (uint32_t)0x1f;
ax = ax + cx;
ax = (int32_t)ax >> (int32_t)1;
cx = ax + si;

if (cx <= di)
    goto first;

dx = cx - 1;
ax = func4(di, si, dx);
ax = ax + ax;
goto fin;

first:
   ax = 0;
   if (cx >= di)
       goto fin;

si = cx + 1;
ax = func4(di, si, dx);
ax = ax + ax + 1;

fin:
    return ax;
}

從快速瀏覽一下,問題可能在這里:

rax >>= (signed)1;           // sar    %eax

這相當於:

rax = rax >> (signed)1;

這是無符號移位(因為移位運算符的符號由第一個操作數確定,而不是第二個操作數)。 所以相反你應該寫:

rax = (unsigned)((signed)rax >> 1);

編輯:同樣,你錯誤地翻譯了jlejge 這些指令執行簽名比較,而相應的C代碼執行無符號比較。 修復它:

if ((signed)rcx <= (signed)rdi) {
    rax = 0;
    if ((signed)rcx >= (signed)rdi)
...

如何從匯編生成C:

寫下裝配。 然后使用與寄存器相同的名稱聲明C變量,並使用C指令替換每個算術或邏輯匯編指令的程序集,並使用if goto構造替換每個分支。 如果你有電話,你當然必須知道電話會議。

一旦C起作用,逐漸使它更像人類,在每個點測試它對組件的行為(如果你有),或類似組件的C(如果你不能組裝組件)。

暫無
暫無

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

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