2011年10月13日星期四

[轉載]【修改】打造自己的反彙編引擎



作 者: DoItFreely
時 間: 2011-10-12,12:32:02
鏈接: http://bbs.pediy.com/showthread.php?t=141285

原始信息來自http://bbs.pediy.com/showthread.php?t=75094
本人水平有限,沒有太多理論,只是增強了PE的處理,並稍微hack了一下代碼。
為什麼要增強?處理特定文件格式(比如PE、ELF)並不是反彙編器的功能,原作者僅僅演示了一下如何編寫和使用反彙編器,沒有深入處理特定文件格式也不奇怪
主要增強如下:
1) load整個PE,而不是只處理.text節
         這樣就可以處理.sys等entry point不在.text段的程序
         跨段的調用、引用也能正常處理

2) 從entry point開始反彙編,只有引用到的代碼才反彙編
         反彙編整個.text很可能會得到不准確的結果。對於用C編寫的程序,導出函數和entry point引用到的函數覆蓋了所有的代碼。
         數據段、資源段等“非代碼”段在開始反彙編之前就被排除掉了
         當前的輸出沒有按照地址排序,比較凌亂

3) 檢測對字符常量和導入函數的引用
          如果找到了引用的目標,就輸出在後面

4) 檢測代碼塊的結束
         無條件跳轉指令(JMP)和返回指令(ret,retn)

5) 增加了對實模式代碼的處理(不完善)
         檢測返回DOS的調用

主要修改:
1) 一處原作者忘記轉換偏移量的類型(應該是signed displacement)而導致算錯偏移量的問題
     與IDA對比時發現的

2) 增加了浮點指令的反彙編(有問題)
      沒有讀intel的指令手冊,僅僅是“依葫蘆畫瓢”,結果可想而知。 (原來遇到浮點指令顯示為???,現在是亂抓了一條來充數)


待增強功能:
1)反彙編浮點(d8~df)和f0 xx序列


文件說明:
    dasm.exe 修改後的執行文件
    dasm.txt 用dasm.exe反彙編它自己的輸出
    filedisk.sys v1.7版(?)的filedisk驅動(該軟件開源,地球人應該都知道吧?)
    filedisk.asm IDA 反彙編filedisk.sys的輸出
    filedisk.idb IDA 5.2生成的數據庫,6.1版的IDA應該也能打開吧(?)
    filedisk.txt 用dasm.exe反彙編filedisk.sys的輸出
    dasmMain.exe 原作者編譯的執行文件
    *.h *.c *.dsw *.dsp 工程文件和源碼

(原作者的代碼可以在前面的鏈接頁面裡面找到,dasm.rar。)



實例效果:(來自filedisk.txt)
===============================
11550: Proc_11550 proc


11550: Loc_11550:
11550: mov edi, edi ;8B FF
11552: push ebp ;55
11553: mov ebp, esp ;8B EC
11555: mov eax, dword ptr [ebp + 8h] ;8B 45 08
11558: push esi ;56
11559: mov esi, dword ptr [eax + 28h] ;8B 70 28
1155C: push 0h ;6A 00
1155E: push dword ptr [esi + Ch] ;FF 76 0C
11561: call dword ptr [10C04h] ;FF 15 04 0C 01 00 ExFreePoolWithTag
11567: push dword ptr [esi + 4h] ;FF 76 04
1156A: call dword ptr [10C38h] ;FF 15 38 0C 01 00 ZwClose
11570: mov eax, dword ptr [ebp + Ch] ;8B 45 0C
11573: mov byte ptr [esi], 0h ;C6 06 00
11576: and dword ptr [eax + 18h], 0h ;83 60 18 00
1157A: and dword ptr [eax + 1Ch], 0h ;83 60 1C 00
1157E: xor eax, eax ;33 C0
11580: pop esi ;5E
11581: pop ebp ;5D
11582: retn 8h ;C2 08 00
11550: Proc_12FD9C endp
===============================


(來自dasm.txt, main()片段)

===============================
402180: Proc_402180 proc


402180: Loc_402180:
402180: mov eax, dword ptr [esp + 4h] ;8B 44 24 04
402184: sub esp, 164h ;81 EC 64 01 00 00
40218A: cmp eax, 2h ;83 F8 02
40218D: push ebx ;53
40218E: push ebp ;55
40218F: push esi ;56
402190: push edi ;57
402191: jz short Loc_4021A9 ;74 16 ->
402193: push 408418h ;68 18 84 40 0​​0 string 'Usage: dasm.exe PEFile
'
402198: call dword ptr [40702Ch] ;FF 15 2C 70 40 0​​0 printf
40219E: add esp, 4h ;​​83 C4 04
4021A1: push 0h ;6A 00
4021A3: call dword ptr [40703Ch] ;FF 15 3C 70 40 0​​0 exit
4021A9: mov esi, dword ptr [esp + 17Ch + 0h];8B B4 24 7C 01 00 00
4021B0: mov eax, dword ptr [esi + 4h] ;8B 46 04
4021B3: push eax ;50
4021B4: call Proc_405430 ;E8 77 32 00 00 =>
4021B9: mov ecx, dword ptr [esi + 4h] ;8B 4E 04
4021BC: mov ebx, dword ptr [40702Ch] ;8B 1D 2C 70 40 0​​0 printf
4021C2: push ecx ;51
4021C3: push 408414h ;68 14 84 40 0​​0 string '%s
'
4021C8: mov ebp, eax ;8B E8
4021CA: call ebx ;FF D3
4021CC: push 408404h ;68 04 84 40 0​​0 string 'DOS Header:
'
4021D1: call ebx ;FF D3
4021D3: push 4083B8h ;68 B8 83 40 0​​0 string '====================================== ===================================
'
4021D8: call ebx ;FF D3
4021DA: lea edx, dword ptr [esp + 28h] ;8D 54 24 28
4021DE: push edx ;52
4021DF: push ebp ;55
4021E0: call Proc_405470 ;E8 8B 32 00 00 =>
4021E5: sub esp, 24h ;83 EC 24
4021E8: mov ecx, Fh ;B9 0F 00 00 00
4021ED: lea esi, dword ptr [esp + 54h] ;8D 74 24 54
4021F1: mov edi, esp ;8B FC
4021F3: rep movsd ;F3 A5
4021F5: movsw ;66 A5
4021F7: call Proc_4054C0 ;E8 C4 32 00 00 =>
4021FC: mov eax, dword ptr [esp + 90h + 0h] ;8B 84 24 90 00 00 00
402203: add esp, 40h ;83 C4 40
402206: cmp ax, 1Eh ;66 3D 1E 00
40220A: jb Loc_402325 ;0F 82 15 01 00 00 ->
402210: mov ecx, dword ptr [esp + 2Ch] ;8B 4C 24 2C
402214: test cx, cx ;66 85 C9
402217: jz short Loc_40222D ;74 14 ->
402219: cmp cx, ax ;66 3B C8
40221C: jnb short Loc_40222D ;73 0F ->
40221E: and ecx, FFFFh ;81 E1 FF FF 00 00
402224: and eax, FFFFh ;25 FF FF 00 00
402229: sub eax, ecx ;2B C1
40222B: jmp short Loc_402235 ;EB 08 ->


402235: Loc_402235:
402235: mov esi, eax ;8B F0
402237: push esi ;56
402238: push esi ;56
402239: push 408388h ;68 88 83 40 0​​0 string '
disassemly dos stub code, %d(0x%x) bytes
'
40223E: call ebx ;FF D3
402240: push esi ;56
402241: call dword ptr [407094h] ;FF 15 94 70 40 0​​0 malloc
402247: mov edi, eax ;8B F8
402249: add esp, 10h ;83 C4 10
40224C: test edi, edi ;85 FF
40224E: jnz short Loc_40226F ;75 1F ->
402250: mov eax, dword ptr [407020] ;A1 20 70 40 0​​0 _iob
402255: push 40836Ch ;68 6C 83 40 0​​0 string 'Memory allocation error !
'
40225A: add eax, 40h ;83 C0 40
40225D: push eax ;50
40225E: call dword ptr [40701Ch] ;FF 15 1C 70 40 0​​0 fprintf
402264: add esp, 8h ;83 C4 08
402267: push FFh ;6A FF
402269: call dword ptr [40703Ch] ;FF 15 3C 70 40 0​​0 exit
40226F: mov eax, dword ptr [esp + 2Ch] ;8B 44 24 2C
402273: cmp ax, word ptr [esp + 50h] ;66 3B 44 24 50
402278: jnb short Loc_40228C ;73 12 ->
40227A: and eax, FFFFh ;25 FF FF 00 00
40227F: push 0h ;6A 00
402281: push eax ;50
402282: push ebp ;55
402283: call dword ptr [407038h] ;FF 15 38 70 40 0​​0 fseek
402289: add esp, Ch ;83 C4 0C
40228C: push ebp ;55
40228D: push esi ;56
40228E: push 1h ;6A 01
402290: push edi ;57
402291: call dword ptr [407034h] ;FF 15 34 70 40 0​​0 fread
402297: mov ecx, dword ptr [esp + 3Ah] ;8B 4C 24 3A
40229B: push esi ;56
40229C: and ecx, FFFFh ;81 E1 FF FF 00 00
4022A2: shl ecx, 10h ;C1 E1 10
4022A5: push ecx ;51
4022A6: call Proc_401000 ;E8 55 ED FF FF <=
4022AB: mov edx, dword ptr [esp + 42h] ;8B 54 24 42
4022AF: mov eax, dword ptr [esp + 40h] ;8B 44 24 40
4022B3: and edx, FFFFh ;81 E2 FF FF 00 00
4022B9: and eax, FFFFh ;25 FF FF 00 00
4022BE: shl edx, 10h ;C1 E2 10
4022C1: or edx, eax ;0B D0
4022C3: push edx ;52
4022C4: call Proc_401270 ;E8 A7 EF FF FF <=
4022C9: mov ecx, dword ptr [esp + 46h] ;8B 4C 24 46
4022CD: mov edx, dword ptr [esp + 44h] ;8B 54 24 44
4022D1: and ecx, FFFFh ;81 E1 FF FF 00 00
4022D7: and edx, FFFFh ;81 E2 FF FF 00 00
4022DD: shl ecx, 10h ;C1 E1 10
4022E0: or ecx, edx ;0B CA
4022E2: mov dword ptr [40AB34h], 1h ;C7 05 34 AB 40 0​​0 01 00 00 00
4022EC: push ecx ;51
4022ED: push 408350h ;68 50 83 40 0​​0 string '%X: realmode_entry_point:
下載請到:http://bbs.pediy.com/showthread.php?t=141285

没有评论:

发表评论