home *** CD-ROM | disk | FTP | other *** search
/ KeyGen Studio 2002 / KeyGen_Studio_2002.iso / Tutorials / ReverseMes / cre-rm1.txt < prev    next >
Encoding:
Text File  |  2001-09-21  |  9.8 KB  |  212 lines

  1. Reversing the Hell out of Muad'Dib's ReverseMe1
  2. - Calling functions dynamically -
  3. By CaptRE
  4.  
  5. ----------------------------------------------------------------
  6. Publisher's Note: Muad'Dib's ReverseMe can be found here:
  7. www.immortaldescendants.org/database/muad/rm1.zip
  8.  
  9. The reversed exe from this essay can be found here:
  10. www.immortaldescendants.org/database/muad/solutions/rm1-rev.exe
  11. ----------------------------------------------------------------
  12.  
  13. This is going to be my first tutorial. I hope it's worth reading 
  14. for y'all.
  15.  
  16. I like the ReverseMe's.... But, wouldn't it be very easy to just 
  17. do the thing we're asked for: make the exit-button work? Yes, it 
  18. would,. Therefore i'll reverse a bit further...
  19.  
  20. ----------------------------------------------------------------
  21. Tools:
  22. ----------------------------------------------------------------
  23. This is gonna be a tutorial where all the changes are directly 
  24. made with HIEW.  Also W32Dasm may be a great help.
  25.  
  26. ----------------------------------------------------------------
  27. Goal:
  28. ----------------------------------------------------------------
  29. 1. Make the messagebox let you prompt for  "Wanna see my 
  30.    AboutBox before exiting?" with  "Yes/No" buttons and a 
  31.    Questionmark Icon.
  32. 2. Exit the program on "No"
  33. 3. Show the AboutBox on "Yes"  with  a special Icon and our own 
  34.    Text 
  35. 4. Exit on  klicking "OK"...
  36.  
  37. ----------------------------------------------------------------
  38. How to do it:
  39. ----------------------------------------------------------------
  40. MY GOD! What have I said.. Is it too tough to be done? Don't 
  41. know, but let's try it out... The first thing is that when we 
  42. deadlist, the Shel32.dll (where the AboutBox-Function is at) 
  43. isn't loaded. This means it has to be loaded, for otherwise we 
  44. can't execute the Functions we need. This means we must the 
  45. "GetProcAddress" Function to dynamically retreive the address 
  46. of  the Functions. We can type the "GetProcAddress" over the 
  47. "GetModuleHandleA" in the importtable (Address 402084, Offset 
  48. 684). But how do we get the handle to "KERNEL32.DLL"???  There 
  49. goes "ExitProcess", overwiritten by "LoadLibraryA" at Address 
  50. 402076, Offset 676. Now we have 2 functions to get dynamically 
  51. all the Handles and Functions we need! You can even see those 
  52. changes when deadlisting the Exe-file. The ExitProcess is a 
  53. function  that will be gone forever, but the EndDialog-function 
  54. will do the Exit-Job just fine.
  55.  
  56. ----------------------------------------------------------------
  57. "On-Paper" coding:
  58. ----------------------------------------------------------------
  59. Next step: make a list of all the Functions we need.
  60. * ShellAboutA    -    Shell32.dll
  61. * ExtractIconA    -    Shell32.dll
  62.  
  63. This means that we need the String "SHELL32.DLL" for the 
  64. LoadLibrary Function. What other Strings do we need?
  65.  
  66. * The strings of the 2 needed Functions "ShellAboutA" and 
  67.   "ExtractIconA"
  68. * The Title of our AboutBox: "See what you can do!!"
  69. * The first Textline in the AboutBox: "By CaptRE". HEY... what 
  70.   do I see our Win32.hlp-file has to say about ShellAbout? 
  71.   "Points to text that the function displays in the title bar 
  72.   of the Shell About dialog box and on the first line of the 
  73.   dialog box after the text "Microsoft Windows" or "Microsoft 
  74.   Windows NT." If the text contains a "#" separator, dividing 
  75.   it into two parts, the function displays the first part in 
  76.   the title bar, and the second part on the first line after 
  77.   the text "Microsoft Windows" or "Microsoft Windows NT". So 
  78.   we'll make it one String then: "See what you can do!!#By 
  79.   CaptRE"
  80. * The Title of our MessageBox: "WannaSeeAbout?"
  81. * The text of our MessageBox: "Do you want to see my AboutBox 
  82.   before you leave?"
  83.  
  84. Now find a place to insert those Strings. I decided to take 
  85. the .Data-Section. I calculated (F8/F6 in Hiew) 
  86. Offset+VirtSize=800+3C=83C. That's a free Space... but after 
  87. some Trial and Error I found out that I had to leave a few 
  88. spaces (that's Zero's in Hex!), for at execution my first 
  89. String was beeing crippled. I started at 00403041 
  90. (Offset 841) and inserted my Strings:
  91.  
  92. Notice: you have to leave a Space (thats NOT a Spacebar, but 2 
  93. Zero's in Hex!) between each String. Now let's go to work!
  94.  
  95. ----------------------------------------------------------------
  96. Make the changes:
  97. ----------------------------------------------------------------
  98. Change the messagebox into our own Title and Text with a YesNo 
  99. button and a Questionmark Icon.
  100.  
  101. 1. Search in the appropriate Header-file or API-viewer the 
  102.    Values for  MB_YESNO +  MB_ICONQUESTION (Public Const 
  103.    MB_YESNO = &H4&, Public Const MB_ICONQUESTION = &H20&) So the 
  104.    Value needed is 20h+4h=24h.
  105. 2. Change the Push at Offset 43E into PUSH 024
  106. 3. Change the Push at Offset 440h into PUSH 403075  = 
  107.    "WannaSeeAbout?" (Don't know how to get this Address? Very 
  108.    simple: in Hiew change to Hex-Mode (F4/F2) then search for the 
  109.    text we've just added. Put the cursor on the first Character of 
  110.    the String and read the Address in the Titlebar of Hiew!).
  111. 4. Change the Push at Offset 445 into PUSH 403084 (Text: "Do 
  112.    you want...")
  113. 5. Save with F9
  114. Done! Next!
  115.  
  116. ----------------------------------------------------------------
  117. Some more "On-Paper" coding:
  118. ----------------------------------------------------------------
  119. Redirect the original call to GetModuleHandleA:
  120.  
  121. 1. Let's see where we can put our new code. In Hiew: F8/F6. In 
  122.    the .TEXT-section we can insert it at 
  123.    Offset+VirtSize=400+8A=48A. 
  124. 2. Jump from original to new Code. A good place to do this is 
  125.    where the call to the MessageBox is made.
  126. 3. Change the call at offset 402 into the call to "LoadLibraryA". 
  127.    See your Win32.hlp-file: the two functions are almost 
  128.    identical!
  129. 4. At the new code location: call the MessageboxA function
  130. 5. Yes/No clicks are saved in EAX. Check if "Yes" or "No" is 
  131.    pushed
  132. 6. If No: stop program
  133. 7. Push string "Shell32.dll"
  134. 8. LoadLibrary "Shell32.dll". The handle of Shell32.dll will be 
  135.    returned in EAX.
  136. 9. Push string "ExtractIconA"
  137. 10. Push the pointer of Shell32.dll (=EAX)
  138. 11. Call GetProcAddress. The returnvalue will be in EAX, that's the 
  139.     pointer to the function "ExtractIconA".
  140. 12. Push indexnumber of the icon. Let's take number 35 (map with 
  141.     tools). The hex-value has to be pushed, i.e. 32.
  142. 13. Push string "Shell32.dll"
  143. 14. Push instancehandle. Let it be 0.
  144. 15. Call ExtractIconA. That is: call EAX (remember?).
  145. 16. The returnvalue will be in EAX. Save the Returnvalue (Handle of 
  146.     the Icon) into a Buffer, otherwise it will be overwritten when 
  147.     calling the ShellAboutA Function. Which Buffer?? The Buffer as 
  148.     used in the original code of course! Look at Offset 407...
  149. 17. Push String "Shell32.dll" again
  150. 18. Call "LoadLibrary" again The returnvalue will be in EAX.
  151. 19. Push the string "ShellAboutA"
  152. 20. Push the pointer of Shell32.dll (=EAX)
  153. 21. Call "GetProcAddress"
  154. 22. Push the iconhandle. We saved it into a buffer in pt.15, so: 
  155.     push this buffer!
  156. 23. Push the string with my name. I use this string twice (see 
  157.     pt 23).
  158. 24. Push the string with the title and first line of text
  159. 25. Push the handle of the parent window. Let it be 0.
  160. 26. Call "ShellAboutA" (=EAX).
  161. 27. Jump to original code to stop the program.
  162. LET's DO IT!
  163.  
  164. ----------------------------------------------------------------
  165. Insert the code:
  166. ----------------------------------------------------------------
  167. At Offset 402: CALL 00000046C
  168.  
  169. 0000048A: E8F5FFFFFF    call      000000484        MessageBoxA   
  170. 0000048F: 50            push eax                    Save result of the click
  171. 00000490: 83F807        cmp       eax,007 ;" "      Is it NO?
  172. 00000493: 74C5          je        00000045A           It is! Exit!
  173. 00000495: 68B6304000    push      0004030B6 ;" @0╢"     "Shell32.dll"
  174. 0000049A: E8CDFFFFFF    call      00000046C          LoadLibraryA
  175. 0000049F: 68C2304000    push      0004030C2 ;" @0┬"     "ExtractIconA"
  176. 000004A4: 50            push      eax                   Pointer shell32.dll
  177. 000004A5: E8C8FFFFFF    call      000000472           GetProcAddress
  178. 000004AA: 6A23          push      023                   Index Iconhandle in Shell32.dll
  179. 000004AC: 68B6304000    push      0004030B6 ;" @0╢"     "Shell32.dll"
  180. 000004B1: 6A00          push      000                   InstanceHandle
  181. 000004B3: FFD0          call      eax                   ExtractIconA
  182. 000004B5: A338304000    mov       [000403038],eax       Save IconHandle in buffer
  183. 000004BA: 68B6304000    push      0004030B6 ;" @0╢"     "Shell32.dll"
  184. 000004BF: E8A8FFFFFF    call      00000046C           LoadLibrary
  185. 000004C4: 6841304000    push      000403041 ;" @0A"     "ShellAboutA"
  186. 000004C9: 50            push      eax                   Pointer Shell32.dll
  187. 000004CA: E8A3FFFFFF    call      000000472           GetProcAddress
  188. 000004CF: FF3538304000  push      d,[000403038]         Iconhandle
  189. 000004D5: 6866304000    push      000403066 ;" @0f"     My Name-string
  190. 000004DA: 684D304000    push      00040304D ;" @0M"     Title & First line-String
  191. 000004DF: 6A00          push      000                   ParentWindow Handle
  192. 000004E1: FFD0          call      eax                   ShellAboutA
  193. 000004E3: E972FFFFFF    jmp       00000045A           Exit!
  194.  
  195. ----------------------------------------------------------------
  196. Final notes:
  197. ----------------------------------------------------------------
  198. I know I could have made some JUMPS so that the repeated code for 
  199. getting the pointer to Shel32.dll isn't redundant, but I kept it 
  200. simple for educational purpouses. The same thing for the strings 
  201. used in the MessageBox: I could have overwritten the original 
  202. strings to keep the space needed minimized. Also I could have 
  203. loaded the ExitProcess function to make a clean exit.....
  204.  
  205. Look at the code... Think.... Right!! You've got total controll 
  206. of every program when implementing this dynamic calling of 
  207. functions!! 
  208.  
  209. Have Fun and Learn.....
  210.  
  211. CaptRE
  212. CaptRE@Hotmail.com