打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【新提醒】快速增长你的逆向编程能力:学会反汇编的角度编写程序!,Win32汇编语言案例解析,典型案例解析(VIP免费),鱼C论坛
小甲鱼用了一个通宵的时间来编写和调试程序,大家有可能不理解的地方也做了详细注释,谨以此献给喜欢鱼C,热爱编程的朋友们!

仅希望不喜的朋友勿喷,喜欢的朋友耐心学习,回复的朋友注意论坛环境,不要回复一堆毫无意义标点符号!

用这种方法写程序,将其编译链接后的可执行文件用IDA等工具将其反汇编后,两者基本保持一致。掌握这种写法,可以加深对逆向的反应和思考能力,适合平时锻炼提高逆向能力!


程序功能如图:




代码及详细注释:

本帖隐藏的内容


  1. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  2. ; by 小甲鱼, http://www.fishc.com
  3. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  4. ; 功能:实现与反汇编同样语法的汇编编程,有点纠结,但非常有利于锻炼逆向思维!
  5. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  6.         .386
  7.         .model flat,stdcall
  8.         option casemap:none

  9. include windows.inc
  10. include user32.inc
  11. include kernel32.inc
  12. includelib user32.lib
  13. includelib kernel32.lib

  14.     .data   
  15. hIcon       dd  0
  16. hWnd        dd  0
  17. hDc         dd  0
  18. winX        dd  0
  19. winY        dd  0
  20. wWidth      dd  0
  21. wHeight     dd  0   
  22. stPs        PAINTSTRUCT <0>
  23. stRect      RECT <0>
  24. wc          WNDCLASSEX  <0>                 ; wc定义为WNDCALSSEX结构,并用零填充
  25. msg         MSG <0>                         ; 用于存放消息
  26. szClassName db  'MyClass', 0
  27. szCaption   db  '欢迎来到鱼C工作室', 0   
  28. szMessage   db  '学习编程最好的地方 -- 鱼C工作室: www.fishc.com', 0

  29.     .code
  30. main:
  31.     push ebp                    ; 保存上一个程序的ebp
  32.     mov ebp, esp                ; ebp == esp,ebp主要用于后边的参数和变量的索引

  33. ;------------------------------------------------------------------
  34. ;......................... 初始化程序 .............................
  35. ;------------------------------------------------------------------
  36.     push 1000                   ; 图标的ID
  37.     push DWORD PTR [ebp+8]      ; 就是在调用main函数前压入的参数,为本模块的句柄
  38.     call LoadIcon
  39.     mov hIcon, eax  
  40.    
  41.     mov wc.cbSize, sizeof WNDCLASSEX
  42.     mov wc.style, CS_BYTEALIGNWINDOW
  43.     mov wc.lpfnWndProc, offset WndProc  
  44.     mov wc.cbClsExtra, NULL
  45.     mov wc.cbWndExtra, NULL
  46.     mov eax, [ebp+8]            ; 就是在调用main函数前压入的参数,为本模块的句柄
  47.     mov wc.hInstance, eax
  48.     mov wc.hbrBackground, COLOR_WINDOW+1
  49.     mov wc.lpszMenuName, NULL
  50.     mov wc.lpszClassName, offset szClassName
  51.    
  52.     push hIcon
  53.     pop eax                     ; 经典做法,相当于mov eax, hIcon
  54.     mov wc.hIcon, eax
  55.    
  56.     push IDC_ARROW
  57.     push NULL
  58.     call LoadCursor
  59.     mov wc.hCursor, eax
  60.    
  61.     push hIcon
  62.     pop eax
  63.     mov wc.hIconSm, eax
  64.    
  65.     push offset wc
  66.     call RegisterClassEx        ; 注册窗口
  67.    
  68.     mov wWidth, 600             ; 窗口宽度
  69.     mov wHeight, 450            ; 窗口高度
  70.     mov winX, 50
  71.     mov winY, 50
  72.    
  73. ;------------------------------------------------------------------
  74. ;........................... 创建窗口 .............................
  75. ;------------------------------------------------------------------      
  76.     push NULL
  77.     push DWORD PTR [ebp+8]      ; 还记得吧,本模块句柄
  78.     push NULL                   ; 菜单句柄,这里不需要,所以木有
  79.     push NULL                   ; 父窗口句柄,这里木有,所以没有
  80.     push wHeight                ; 高度
  81.     push wWidth                 ; 宽度
  82.     push winY                   ; 左上角y值
  83.     push winX                   ; 左上角x值
  84.     push WS_OVERLAPPEDWINDOW    ; 窗口风格
  85.     push offset szCaption       ; 标题
  86.     push offset szClassName     ; 类名
  87.     push WS_EX_LEFT             ; 扩展窗口风格
  88.     call CreateWindowEx         ; 上边的都是这个函数的参数,注意入栈是从右到左
  89.     mov hWnd, eax
  90.    
  91.     push SW_SHOWNORMAL
  92.     push hWnd
  93.     call ShowWindow             ; 显示窗口
  94.    
  95.     push hWnd
  96.     call UpdateWindow           ; 更新窗口
  97.    
  98. ;------------------------------------------------------------------
  99. ;........................... 消息循环 .............................
  100. ;------------------------------------------------------------------  
  101. msgLoop:
  102.     push 0
  103.     push 0
  104.     push NULL
  105.     push offset msg
  106.     call GetMessage             ; 获取消息
  107.    
  108.     cmp eax, 0                  
  109.     je exitLoop                 ; 如果返回值为0,则退出循环,准备结束程序
  110.    
  111.     push offset msg
  112.     call TranslateMessage       ; 转发消息
  113.    
  114.     push offset msg
  115.     call DispatchMessage        ; 分派消息
  116.    
  117.     jmp msgLoop

  118. exitLoop:
  119.     mov esp, ebp                ; 恢复堆栈
  120.     pop ebp                     ; 恢复上一个程序的ebp
  121.     ret                         ; 返回
  122.    
  123. ;------------------------------------------------------------------
  124. ;........................... 窗口过程 .............................
  125. ;------------------------------------------------------------------      
  126. WndProc:
  127.     push ebp
  128.     mov ebp, esp
  129.    
  130. ; 小甲鱼再啰嗦下!WndProc proc hWin, uMsg, wPrarm, lParam
  131. ;                                |     |      |       |
  132. ; 他们分别用ebp的索引值是:    ebp+8 ebp+12 ebp+16  ebp+20
  133.     cmp DWORD PTR [ebp+12], WM_PAINT    ; 判断是否绘制窗口消息
  134.     jne @F
  135.    
  136.     push offset stPs
  137.     push [ebp+8]               ; hWin入栈
  138.     call BeginPaint
  139.     mov hDc, eax
  140.    
  141.     push offset stRect
  142.     push [ebp+8]
  143.     call GetClientRect
  144.    
  145.     push DT_SINGLELINE or DT_CENTER or DT_VCENTER
  146.     push offset stRect
  147.     push -1
  148.     push offset szMessage
  149.     push hDc
  150.     call DrawText
  151.    
  152.     push offset stPs
  153.     push [ebp+8]
  154.     call EndPaint             ; 擦屁股,释放资源
  155.    
  156. @@:
  157.     cmp DWORD PTR [ebp+12], WM_CLOSE    ; 判断是不是获得关闭的消息
  158.     jne @F
  159.    
  160.     push NULL
  161.     call PostQuitMessage
  162.    
  163. @@:
  164.     push DWORD PTR[ebp+20]     ; lParam
  165.     push DWORD PTR[ebp+16]     ; wParam
  166.     push DWORD PTR[ebp+12]     ; uMsg
  167.     push DWORD PTR[ebp+8]      ; hWin
  168.     call DefWindowProc         ; 把我们不关注的消息都让默认函数来处理

  169.     mov esp, ebp
  170.     pop ebp
  171.     ret   

  172. start:      
  173.     push NULL
  174.     call GetModuleHandle        ; 获取本模块句柄,注意现在我们不用invoke,我们时刻要保持反汇编写法
  175.    
  176.     push eax                    ; eax保存着拿到的模块句柄,并作为main函数的参数入栈
  177.     call main   
  178.    
  179.     push 0
  180.     call ExitProcess            ; 退出程序
  181.         
  182.     end start         
复制代码

以下是用IDA逆向后的反汇编程序,请鱼油们注意比较,两者基本保持一致。


  1. .text:00401000 ; int __cdecl sub_401000(HINSTANCE hInstance)
  2. .text:00401000 sub_401000      proc near               ; CODE XREF: start+8p
  3. .text:00401000
  4. .text:00401000 hInstance       = dword ptr  8
  5. .text:00401000
  6. .text:00401000                 push    ebp
  7. .text:00401001                 mov     ebp, esp
  8. .text:00401003                 push    3E8h            ; lpIconName
  9. .text:00401008                 push    [ebp+hInstance] ; hInstance
  10. .text:0040100B                 call    LoadIconA
  11. .text:00401010                 mov     dword_403000, eax
  12. .text:00401015                 mov     stru_40306C.cbSize, 30h
  13. .text:0040101F                 mov     stru_40306C.style, 2000h
  14. .text:00401029                 mov     stru_40306C.lpfnWndProc, offset sub_40114B
  15. .text:00401033                 mov     stru_40306C.cbClsExtra, 0
  16. .text:0040103D                 mov     stru_40306C.cbWndExtra, 0
  17. .text:00401047                 mov     eax, [ebp+hInstance]
  18. .text:0040104A                 mov     stru_40306C.hInstance, eax
  19. .text:0040104F                 mov     stru_40306C.hbrBackground, 6
  20. .text:00401059                 mov     stru_40306C.lpszMenuName, 0
  21. .text:00401063                 mov     stru_40306C.lpszClassName, offset ClassName ; "MyClass"
  22. .text:0040106D                 push    dword_403000
  23. .text:00401073                 pop     eax
  24. .text:00401074                 mov     stru_40306C.hIcon, eax
  25. .text:00401079                 push    7F00h           ; lpCursorName
  26. .text:0040107E                 push    0               ; hInstance
  27. .text:00401080                 call    LoadCursorA
  28. .text:00401085                 mov     stru_40306C.hCursor, eax
  29. .text:0040108A                 push    dword_403000
  30. .text:00401090                 pop     eax
  31. .text:00401091                 mov     stru_40306C.hIconSm, eax
  32. .text:00401096                 push    offset stru_40306C ; WNDCLASSEXA *
  33. .text:0040109B                 call    RegisterClassExA
  34. .text:004010A0                 mov     nWidth, 258h
  35. .text:004010AA                 mov     nHeight, 1C2h
  36. .text:004010B4                 mov     X, 32h
  37. .text:004010BE                 mov     Y, 32h
  38. .text:004010C8                 push    0               ; lpParam
  39. .text:004010CA                 push    [ebp+hInstance] ; hInstance
  40. .text:004010CD                 push    0               ; hMenu
  41. .text:004010CF                 push    0               ; hWndParent
  42. .text:004010D1                 push    nHeight         ; nHeight
  43. .text:004010D7                 push    nWidth          ; nWidth
  44. .text:004010DD                 push    Y               ; Y
  45. .text:004010E3                 push    X               ; X
  46. .text:004010E9                 push    0CF0000h        ; dwStyle
  47. .text:004010EE                 push    offset WindowName ; "欢迎来到鱼C工?
  48. .text:004010F3                 push    offset ClassName ; "MyClass"
  49. .text:004010F8                 push    0               ; dwExStyle
  50. .text:004010FA                 call    CreateWindowExA
  51. .text:004010FF                 mov     hWnd, eax
  52. .text:00401104                 push    1               ; nCmdShow
  53. .text:00401106                 push    hWnd            ; hWnd
  54. .text:0040110C                 call    ShowWindow
  55. .text:00401111                 push    hWnd            ; hWnd
  56. .text:00401117                 call    UpdateWindow
  57. .text:0040111C
  58. .text:0040111C loc_40111C:                             ; CODE XREF: sub_401000+145j
  59. .text:0040111C                 push    0               ; wMsgFilterMax
  60. .text:0040111E                 push    0               ; wMsgFilterMin
  61. .text:00401120                 push    0               ; hWnd
  62. .text:00401122                 push    offset Msg      ; lpMsg
  63. .text:00401127                 call    GetMessageA
  64. .text:0040112C                 cmp     eax, 0
  65. .text:0040112F                 jz      short loc_401147
  66. .text:00401131                 push    offset Msg      ; lpMsg
  67. .text:00401136                 call    TranslateMessage
  68. .text:0040113B                 push    offset Msg      ; lpMsg
  69. .text:00401140                 call    DispatchMessageA
  70. .text:00401145                 jmp     short loc_40111C
  71. .text:00401147 ; ---------------------------------------------------------------------------
  72. .text:00401147
  73. .text:00401147 loc_401147:                             ; CODE XREF: sub_401000+12Fj
  74. .text:00401147                 mov     esp, ebp
  75. .text:00401149                 pop     ebp
  76. .text:0040114A                 retn
  77. .text:0040114A sub_401000      endp
  78. .text:0040114A
  79. .text:0040114B
  80. .text:0040114B ; =============== S U B R O U T I N E =======================================
  81. .text:0040114B
  82. .text:0040114B ; Attributes: bp-based frame
  83. .text:0040114B
  84. .text:0040114B ; int __cdecl sub_40114B(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  85. .text:0040114B sub_40114B      proc near               ; DATA XREF: sub_401000+29o
  86. .text:0040114B
  87. .text:0040114B hWnd            = dword ptr  8
  88. .text:0040114B Msg             = dword ptr  0Ch
  89. .text:0040114B wParam          = dword ptr  10h
  90. .text:0040114B lParam          = dword ptr  14h
  91. .text:0040114B
  92. .text:0040114B                 push    ebp
  93. .text:0040114C                 mov     ebp, esp
  94. .text:0040114E                 cmp     [ebp+Msg], 0Fh
  95. .text:00401152                 jnz     short loc_401199
  96. .text:00401154                 push    offset Paint    ; lpPaint
  97. .text:00401159                 push    [ebp+hWnd]      ; hWnd
  98. .text:0040115C                 call    BeginPaint
  99. .text:00401161                 mov     hdc, eax
  100. .text:00401166                 push    offset Rect     ; lpRect
  101. .text:0040116B                 push    [ebp+hWnd]      ; hWnd
  102. .text:0040116E                 call    GetClientRect
  103. .text:00401173                 push    25h             ; format
  104. .text:00401175                 push    offset Rect     ; lprc
  105. .text:0040117A                 push    0FFFFFFFFh      ; cchText
  106. .text:0040117C                 push    offset chText   ; "学习编程最好的地方 -- 鱼C工?
  107. .text:00401181                 push    hdc             ; hdc
  108. .text:00401187                 call    DrawTextA
  109. .text:0040118C                 push    offset Paint    ; lpPaint
  110. .text:00401191                 push    [ebp+hWnd]      ; hWnd
  111. .text:00401194                 call    EndPaint
  112. .text:00401199
  113. .text:00401199 loc_401199:                             ; CODE XREF: sub_40114B+7j
  114. .text:00401199                 cmp     [ebp+Msg], 10h
  115. .text:0040119D                 jnz     short loc_4011A6
  116. .text:0040119F                 push    0               ; nExitCode
  117. .text:004011A1                 call    PostQuitMessage
  118. .text:004011A6
  119. .text:004011A6 loc_4011A6:                             ; CODE XREF: sub_40114B+52j
  120. .text:004011A6                 push    [ebp+lParam]    ; lParam
  121. .text:004011A9                 push    [ebp+wParam]    ; wParam
  122. .text:004011AC                 push    [ebp+Msg]       ; Msg
  123. .text:004011AF                 push    [ebp+hWnd]      ; hWnd
  124. .text:004011B2                 call    DefWindowProcA
  125. .text:004011B7                 mov     esp, ebp
  126. .text:004011B9                 pop     ebp
  127. .text:004011BA                 retn
  128. .text:004011BA sub_40114B      endp
  129. .text:004011BA
复制代码



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MFC程序逆向 – 消息篇(上)[转自看雪]
另一个类型的窗口汇编程序及反汇编程序
MFC程序逆向 – 消息篇(下)
Switch语句的实现机制
【原创】OllyDBG分析报告系列(8)
适合新手的160个creakme(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服