home *** CD-ROM | disk | FTP | other *** search
/ KeyGen Studio 2002 / KeyGen_Studio_2002.iso / Tutorials / Code Inside / ben_tut23.txt < prev    next >
Encoding:
Text File  |  2001-09-21  |  16.3 KB  |  346 lines

  1.  
  2.               Manually UnPacking of EP_v0.2 
  3.                           -----------------------------
  4.  
  5. --------------------------------------------|
  6. Writer:         Bengaly                     |
  7. Author:         CoDe_InSide                 |
  8. Protection:    encryption & scrambling IT  |
  9. URL:    http://members.home.nl/code.inside/ |
  10. Tools:        SoftICE v4.00               |
  11.         Hex-Editor                  |
  12.         Procdump v1.6.2             |
  13.                 ImportREC (MackT.cjb.net)   |
  14. --------------------------------------------|
  15.  
  16. Hello!
  17. Welcome to my 23rd tutorial, today we will talk about Manual Unpacking and Fixing Import Tables!
  18.  
  19. Let's get started:
  20. ------------------
  21.  
  22. In this essay I will use CoDe_InSide's EP_v0.2 (PE) packer/Encrypter.
  23. So I will encrypt Calculator.exe
  24. After encryption you will get calcc.exe heh ;D
  25. Ok, let's open PrucDump. choose "PE EDITOR" and load calcc.exe
  26.  
  27. You will see a window with this in formation:
  28.  
  29. Entry Point: 00017000    <= this is the Virtual Offset
  30. Size of Image: 00018000
  31. Image Base: 01000000
  32.  
  33. Ok, now that we know the virtual offset (00017000), click the "Sections" button,
  34. You will see all section Information in a tab.
  35.  
  36. always look at the final section (so we will patch the offset in HexEditor)
  37. Ok, so look under Virtual Offset of the last section, u will see 00017000, but we must
  38. Know the Raw offset, so look under Raw Offset of the section, it is 00017000 to,
  39. Wow we got lucky it's the same offset ;D (some may use different offsets), but here it's the same ;D
  40. Ok, now we know the Raw Offset. (00017000)
  41.  
  42. Open up hex editor (any), and look for 17000 
  43. You will see this in Hex-Editor:
  44.  
  45. 17000: 6A 00 60-E9...... 
  46.  
  47. Now, in order to use sICE to break when we load the program (Breaking on BPINT 3), we need to Change the 6A -> CC  (CC = int 3, a dos breakpoint)
  48.  
  49. So in hex editor just change:
  50.  
  51. 6A
  52.  
  53. To:
  54.  
  55. CC
  56.  
  57. Now it will be like this -> 17000: CC 00 60-E9...... 
  58.  
  59. Save the file and exit hexeditor.
  60. Now load sICE (Ctrl+D).
  61. Set a breakpoint: BPINT 3 
  62. Press enter, and exit sICE (Ctrl+D)
  63. Load the encrypted file (Calcc.exe), SICE will pop up here: 
  64.  
  65. EAX=01017000   EBX=00520000   ECX=81653AB0   EDX=81653AF0   ESI=81653A90        
  66. EDI=00000000   EBP=0056FF78   ESP=0056FE3C   EIP=01017000   O d I S z a P c     
  67. CS=0177   DS=017F   SS=017F   ES=017F   FS=5607   GS=0000                       
  68. ─────────────────────────────────────────────────────────────────────────PROT32─
  69. 0177:01016FFE  INVALID                                                         
  70. 0177:01017000  INT       3   <= we brake here                                                 
  71. 0177:01017001  ADD       [EAX-17],AH                                            
  72. 0177:01017004  ADD       [ECX],EAX                                              
  73. 0177:01017006  ADD       [EAX],AL                                               
  74. 0177:01017008  ADD       [EAX],AL                                               
  75. 0177:0101700A  ADD       [EAX],AL                                               
  76. 0177:0101700C  INC       ESI                                                    
  77. 0177:0101700D  INC       EDI                                                  
  78. 0177:0101700E  DIV       DWORD PTR [BFF70000]                             
  79. ────────────────────────────────────────────────────────────────────────────────
  80.  
  81. Now we need to change the "CC" back to "6A" (the original value), because if we wont then sICE will exit ...so just press:
  82. E EIP
  83. U will see this:
  84. ──────────────────────────────────────────────────byte──────────────PROT───(0)──
  85. 0177:01017000 CC 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35  ..`.........FG.5
  86. 0177:01017010 00 00 F7 BF 5C 70 01 00-30 70 01 00 00 00 00 00  ....\p..0p......
  87. 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
  88. 0177:01017030 D4 76 F7 BF AC 6D F7 BF-00 00 00 00 C2 01 4C 6F  .v...m........Lo
  89. ─────────────────────────────────────────────────────────────────────────PROT32─
  90.  
  91. Change 0177:01017000 CC 00 -> 6A 00
  92.  
  93. So it will be like this:
  94. 0177:01017000 CC 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35 (Only the first byte is changed)
  95.  
  96. Press ALT+D to return to the Command Window,
  97. Ha...much better, now we can trace the code ;D
  98. We will trace the code to find the OEP (Original Entry Point), and we well trace the Original 
  99. Import Table so we could be able to fix the table later (encryptor f*ck up the IT )
  100.  
  101. 1.================================The Hunt For OEP=============================================
  102.  
  103. Ok, so we changed the int3 back to the original value so we can trace in sICE,
  104. A very quik way to the the OEP is for looking the  POPAD instruction (most
  105. Encryptors use PUSHAD and then POPAD instruction..begining and ending of code)
  106.  
  107. A good way to see the POPAD is to scroll the code with CTRL+Down
  108. It will take a while but you will see in the end of the code the POPAD & RET commands..so
  109. When seeing the POPAD instruction, double click on it (mark it with white) and type: HERE
  110. Hehehe, we are breaking on the POPAD instruction:
  111.  
  112. EAX=01017000   EBX=00520000   ECX=81653AB0   EDX=81653AF0   ESI=81653A90        
  113. EDI=00000000   EBP=0056FF78   ESP=0056FE3C   EIP=01017000   O d I S z a P c     
  114. CS=0177   DS=017F   SS=017F   ES=017F   FS=5607   GS=0000                       
  115. ──────────────────────────────────────────────────byte──────────────PROT───(0)──
  116. 0177:01017000 6A 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35  j.`.........FG.5
  117. 0177:01017010 00 00 F7 BF 5C 70 01 00-30 70 01 00 00 00 00 00  ....\p..0p......
  118. 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
  119. 0177:01017030 D4 76 F7 BF AC 6D F7 BF-00 00 00 00 C2 01 4C 6F  .v...m........Lo
  120. ─────────────────────────────────────────────────────────────────────────PROT32─
  121. 0177:01017208  POP       EAX                                                   
  122. 0177:01017209  ADD       ESI,14                                               
  123. 0177:0101720C  MOV       DWORD PTR [ESP+20],010119E0    <= OEP                          
  124. 0177:01017214  JMP       01017170                                               
  125. 0177:01017219  NOP                                                              
  126. 0177:0101721A  REPZ STOSD                                                       
  127. 0177:0101721C  POPAD       <= you will break here                                               
  128. 0177:0101721D  RET                                                              
  129. 0177:0101721E  ADD       [EAX],AL                                              
  130. 0177:01017220  ADD       [EAX],AL                                          
  131. ────────────────────────────────────────────────────────────────────────────────
  132.  
  133. How did i know how trace until here?
  134. Well as i told you, look for a POPAD insruction and a RET..that will insure 
  135. You can find the OEP there...well most of the times
  136.  
  137. Now, go over the RET instruction (f10), and while you are on the RET instruction
  138. Type F10 again..u will be here:
  139.  
  140. EAX=01017000   EBX=00520000   ECX=81653AB0   EDX=81653AF0   ESI=81653A90        
  141. EDI=00000000   EBP=0056FF78   ESP=0056FE3C   EIP=010119E0   o d I s Z a P c     
  142. CS=0177   DS=017F   SS=017F   ES=017F   FS=5607   GS=0000                       
  143. ──────────────────────────────────────────────────byte──────────────PROT───(0)──
  144. 0177:010119E0 55 8B .. .. .. .. .. .. .. ..and ect <= notice here
  145. 0177:010119F0 A1 00 00 00 00 50 64 89-25 00 00 00 00 83 C4 98  .....Pd.%.......
  146. 0177:01011A00 53 56 57 89 65 E8 C7 45-FC 00 00 00 00 6A 02 FF  SVW.e..E.....j..
  147. 0177:01011A10 15 B8 10 00 01 83 C4 04-C7 05 78 3F 01 01 FF FF  ..........x?....
  148. ─────────────────────────────────────────────────────────────────────────PROT32─
  149. 0177:010119DF  INT       3                                                     
  150. 0177:010119E0  PUSH      EBP   <= u break here                               
  151. 0177:010119E1  MOV       EBP,ESP                                                
  152. 0177:010119E3  PUSH      FF                                                     
  153. 0177:010119E5  PUSH      01001570                                               
  154. 0177:010119EA  PUSH      01011D60                                               
  155. 0177:010119EF  MOV       EAX,FS:[00000000]                                      
  156. 0177:010119F5  PUSH      EAX                                                    
  157. 0177:010119F6  MOV       FS:[00000000],ESP                                    
  158. 0177:010119FD  ADD       ESP,-68                                          
  159. ────────────────────────────────────────────────────────────────────────────────
  160.  
  161. Also notice that EIP use the Original Entry Point (010119E0)
  162. EAX= First Entry Point (01017000)
  163.  
  164. Now We are going to use the "EBFE" trick! , Do this in sICE:
  165.  
  166. E EIP    [enter]        
  167. EBFE            : Change 558B to EBFE (Don't forget to remember 558B!) 
  168. ALT+D            : To return to the Command Window 
  169.  
  170. 0177:010119E0 EB FE EC 6A FF 68 70 15-00 01 68 60 1D 01 01 64  <= it will be like this
  171. 0177:010119F0 A1 00 00 00 00 50 64 89-25 00 00 00 00 83 C4 98  
  172. 0177:01011A00 53 56 57 89 65 E8 C7 45-FC 00 00 00 00 6A 02 FF  
  173. 0177:01011A10 15 B8 10 00 01 83 C4 04-C7 05 78 3F 01 01 FF FF  
  174. ─────────────────────────────────────────────────────────────────────────PROT32─
  175. 0177:010119DF  INT       3                                                     
  176.  ==> 010119E0  JMP       010119E0          (JUMP)   ; the trap
  177.  
  178. So we set a trap on the OEP ;D, now let's clear all breakpoints : BC * 
  179. Again set a breakpoint on BPINT 3 , and leave sICE
  180. Load calcc.exe, sICE breaks, type: E EIP edit the CC->6A and press <enter>
  181. Now leave sICE again (without clearing the breakpoints)..calcc.exe runs like a baby
  182. Well what's the point of it??
  183. The point is that u wont get a error message when loading calcc.exe (without bpint3,cc->6a)
  184. That way i can dump it in prucdump while the program is loaded and dont crashing Prucdump
  185. When trying to DUMP it.
  186.  
  187. Now let's open procdump again...now in the TASK window, scroll down and look for
  188. The Directory u have the encrypted file, mine is:
  189.  
  190. D:\Dont Tuch\notepade.exe
  191.  
  192. If not there, right click on mouse...press REFRESH LIST...ohh here it is :=)
  193.  
  194. Now select the file, right click with mouse and choose: DUMP (FULL)..if PrucDump still Crash Then i recommend to dump the file using PARTIAL, DUMP (PARTIAL) <= click ok all the time.
  195.  
  196. Save the new dump anywhere u like, choose a name of course ;D (i prefer the name DUMP.EXE ;D) Now again right click on it again and choose: KILL TASK  (press ok after u get a message) Now, The dump we made does not run yet, we need to modify it to make it run...duh! ;D
  197. So click "PE Editor" and choose the new dump file we made.
  198. Now the entry point is still 00017000, so change it to the real OEP: 119E0
  199.  
  200. It will be like this now:
  201.  
  202. Entry Point: 000119E0
  203.  
  204. Now on the "Apply Changes Method:" choose "To PE File" <= so we can kick the encryption Code.
  205. After choosing "To PE File", click "SECTIONS" button.
  206. Now right click on the last section with mouse and choose: "Kill Section"
  207. This will ensure that the encryption code wont be on the dump.exe anymore ;D
  208. Now we will change the Size of Image of the dump:
  209.  
  210. Size of Image = Last Virtual Offset + Last Virtual Size  (last section)
  211.  
  212. So because we killed the last section, we will use the last section, To fix this take NOW the 
  213.  
  214.  
  215. Offset            = 00014000  
  216. Virtual Size    = 00002618
  217.  
  218. Size of Image = 00014000 + 00002618 = 00016618
  219.  
  220. Click "CANCEL" button and we back to the Header Info screen,
  221. We see that the "Size Of Image" = 00018000 <= which is not correct! (at least here it is) :-)  
  222. Change "Size Of Image" = 00018000 -> 00016618 
  223.  
  224. Press OK to rebuild the structure of dump.exe ;D
  225. Exit procdump.
  226. Try running the file (dump.exe)...what's going on, it's not working...why! >:/
  227.  
  228. Well it won't work because it runs in a loop that provides form the program from working!
  229. Remember: 558B -> EBFE?
  230. So open hex editor again and go to the OEP we found (119E0).
  231.  
  232. Change back EBFE -> 558B
  233.  
  234. Save the file...
  235. Run Dump.exe...IT CRASH AGAIN!!!!!!!!... >:|
  236.  
  237. Wtf...why :( ?
  238. well the main reasone is because the EP v0.2 encryption routine, it basiclly F*CKING UP THE
  239. IMPORT TABLE...
  240. To know more about IT i suggest to read some tuts about it!
  241.  
  242. 2.==============================The Hunt For Original IT=======================================
  243.  
  244. Ok, open up sICE, Set a BPINT 3 breakpoint, exit sICE, load the encrypted file (not the dump)
  245. SICE pop..just E EIP to bring back the original value so we can trace with sICE..
  246.  
  247. E EIP
  248. CC 00
  249. To:
  250. 6A 00
  251. <enter> or Alt+D
  252.  
  253. Now we can tracing...it wont take a while..try find it your self
  254. Didn't find??
  255.  
  256. So notice this adresses:
  257.  
  258. EAX=01017020   EBX=00520000   ECX=00000000   EDX=7461644F   ESI=01012020  <= Original IT   
  259. EDI=00000000   EBP=01000000   ESP=0056FE18   EIP=01017170   o d I s z a p c     
  260. CS=0177   DS=017F   SS=017F   ES=017F   FS=1127   GS=0000   DS:0101202C=000122A2
  261. ──────────────────────────────────────────────────byte──────────────PROT───(0)──
  262. 0177:01017000 D4 76 F7 BF AC 6D F7 BF-00 10 00 00 00 20 01 00  .v...m....... ..
  263. 0177:01017010 00 30 01 00 00 10 00 00-00 00 00 00 20 20 01 00  .0..........  ..
  264. 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
  265. 0177:01017030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
  266. ─────────────────────────────────────────────────────────────────────────PROT32─
  267. 0177:01017163  JMP       0101713C                                         
  268. 0177:01017165  ADD       ESI,04                                               
  269. 0177:01017168  LODSD                                                            
  270. 0177:01017169  TEST      EAX,EAX                                                
  271. 0177:0101716B  JZ        01017175                                               
  272. 0177:0101716D  ADD       EAX,EBP                                                
  273. 0177:0101716F  XCHG      EAX,ESI                                                
  274. 0177:01017170  MOV       EAX,[ESI+0C]  <= Original IT is moved to eax which is stored in esi    
  275. 0177:01017173  TEST      EAX,EAX                                               
  276. 0177:01017175  JZ        010171F6                                          
  277. ────────────────────────────────────────────────────────────────────────────────
  278.  
  279. A quick: ? ESI
  280. Will show you ESI=01012020 
  281. So we can assum for that, that Original It is starting at 12020 (write it down)
  282.  
  283. 3.===========================Fixing Broken Import Table========================================
  284.  
  285. So once that we know the start point of the IT, we open prucDump again, choosing the "PE EDITOR"
  286. Button, and choosing our dump.exe That we have created!
  287. Now in order to change the IT starting point click the "DIRECTORY" button:
  288. You will see:
  289.  
  290. Import Table:   RVA           Size
  291.               00017008       00000034
  292.  
  293. But we know 00017008 is not the real starting point of the IT, so just edit it and replaced
  294. It with the real RVA
  295.  
  296. 17008->12020
  297.  
  298. Click OK, and OK again..dump.exe is rebuild.
  299. Now we need to use a program called "ImportREC" which will fix us the broken Import Table.
  300.  
  301. How to use and fix throw ImportREC??
  302.  
  303. ===================================Using ImportREC==============================================
  304.  
  305. 1. Load the encrypted file (not dump) using the CC->6A trick in sICE (wich in the case wont Crash the program) and that will make it active to use in importREC.
  306.  
  307. 2. In the "attach to an active proccess" choose our program -> d:\dont tuch\calcc.exe
  308.  
  309. 3.Enter the correct OEP+IT at "IAT INFOS"
  310. OEP = 000119E0
  311. RVA = 00012020
  312. SIZE = 00001000
  313.  
  314. 4. Press the "IAT AutoSearch" button , and u will get the message "Found something!" and some RVA and OEP informations Click OK!
  315.  
  316. 5. Click the "Get Import" button
  317.  
  318. 6. Click the "Fix Dump" button, and choose the dump.exe we made
  319. * Note, sometimes u will get an errorMessage but you can ignore it ;) otherWise it works fine.
  320.  
  321. 7. Exit ImportREC
  322.  
  323. Ok so we fixed the dump.exe with good IT..it's time to check the program.
  324. You will see a new file is created dump_.exe ;) click it!!
  325.  
  326. it works ;D
  327.  
  328.                      EP.v0.2 Is Unpacked And Reversed!
  329.  
  330.  
  331.  
  332. Have fun ;D
  333.  
  334.                          My thanks and gratitude goes to:
  335.                          --------------------------------
  336.  
  337.                      ----CODE_INSIDE----- ;for helping in cracking
  338.                      -----BLAcKgH0sT------;for being good friend :)
  339.                      -------FusS------    ;asm / keygenning helper
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.