Manually UnPacking of EP_v0.2 ----------------------------- --------------------------------------------| Writer: Bengaly | Author: CoDe_InSide | Protection: encryption & scrambling IT | URL: http://members.home.nl/code.inside/ | Tools: SoftICE v4.00 | Hex-Editor | Procdump v1.6.2 | ImportREC (MackT.cjb.net) | --------------------------------------------| Hello! Welcome to my 23rd tutorial, today we will talk about Manual Unpacking and Fixing Import Tables! Let's get started: ------------------ In this essay I will use CoDe_InSide's EP_v0.2 (PE) packer/Encrypter. So I will encrypt Calculator.exe After encryption you will get calcc.exe heh ;D Ok, let's open PrucDump. choose "PE EDITOR" and load calcc.exe You will see a window with this in formation: Entry Point: 00017000 <= this is the Virtual Offset Size of Image: 00018000 Image Base: 01000000 Ok, now that we know the virtual offset (00017000), click the "Sections" button, You will see all section Information in a tab. always look at the final section (so we will patch the offset in HexEditor) Ok, so look under Virtual Offset of the last section, u will see 00017000, but we must Know the Raw offset, so look under Raw Offset of the section, it is 00017000 to, Wow we got lucky it's the same offset ;D (some may use different offsets), but here it's the same ;D Ok, now we know the Raw Offset. (00017000) Open up hex editor (any), and look for 17000 You will see this in Hex-Editor: 17000: 6A 00 60-E9...... 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) So in hex editor just change: 6A To: CC Now it will be like this -> 17000: CC 00 60-E9...... Save the file and exit hexeditor. Now load sICE (Ctrl+D). Set a breakpoint: BPINT 3 Press enter, and exit sICE (Ctrl+D) Load the encrypted file (Calcc.exe), SICE will pop up here: EAX=01017000 EBX=00520000 ECX=81653AB0 EDX=81653AF0 ESI=81653A90 EDI=00000000 EBP=0056FF78 ESP=0056FE3C EIP=01017000 O d I S z a P c CS=0177 DS=017F SS=017F ES=017F FS=5607 GS=0000 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä 0177:01016FFE INVALID 0177:01017000 INT 3 <= we brake here 0177:01017001 ADD [EAX-17],AH 0177:01017004 ADD [ECX],EAX 0177:01017006 ADD [EAX],AL 0177:01017008 ADD [EAX],AL 0177:0101700A ADD [EAX],AL 0177:0101700C INC ESI 0177:0101700D INC EDI 0177:0101700E DIV DWORD PTR [BFF70000] ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Now we need to change the "CC" back to "6A" (the original value), because if we wont then sICE will exit ...so just press: E EIP U will see this: ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄbyteÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROTÄÄÄ(0)ÄÄ 0177:01017000 CC 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35 ..`.........FG.5 0177:01017010 00 00 F7 BF 5C 70 01 00-30 70 01 00 00 00 00 00 ....\p..0p...... 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 0177:01017030 D4 76 F7 BF AC 6D F7 BF-00 00 00 00 C2 01 4C 6F .v...m........Lo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä Change 0177:01017000 CC 00 -> 6A 00 So it will be like this: 0177:01017000 CC 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35 (Only the first byte is changed) Press ALT+D to return to the Command Window, Ha...much better, now we can trace the code ;D We will trace the code to find the OEP (Original Entry Point), and we well trace the Original Import Table so we could be able to fix the table later (encryptor f*ck up the IT ) 1.================================The Hunt For OEP============================================= Ok, so we changed the int3 back to the original value so we can trace in sICE, A very quik way to the the OEP is for looking the POPAD instruction (most Encryptors use PUSHAD and then POPAD instruction..begining and ending of code) A good way to see the POPAD is to scroll the code with CTRL+Down It will take a while but you will see in the end of the code the POPAD & RET commands..so When seeing the POPAD instruction, double click on it (mark it with white) and type: HERE Hehehe, we are breaking on the POPAD instruction: EAX=01017000 EBX=00520000 ECX=81653AB0 EDX=81653AF0 ESI=81653A90 EDI=00000000 EBP=0056FF78 ESP=0056FE3C EIP=01017000 O d I S z a P c CS=0177 DS=017F SS=017F ES=017F FS=5607 GS=0000 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄbyteÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROTÄÄÄ(0)ÄÄ 0177:01017000 6A 00 60 E9 01 01 00 00-00 00 00 00 46 47 F7 35 j.`.........FG.5 0177:01017010 00 00 F7 BF 5C 70 01 00-30 70 01 00 00 00 00 00 ....\p..0p...... 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 0177:01017030 D4 76 F7 BF AC 6D F7 BF-00 00 00 00 C2 01 4C 6F .v...m........Lo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä 0177:01017208 POP EAX 0177:01017209 ADD ESI,14 0177:0101720C MOV DWORD PTR [ESP+20],010119E0 <= OEP 0177:01017214 JMP 01017170 0177:01017219 NOP 0177:0101721A REPZ STOSD 0177:0101721C POPAD <= you will break here 0177:0101721D RET 0177:0101721E ADD [EAX],AL 0177:01017220 ADD [EAX],AL ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ How did i know how trace until here? Well as i told you, look for a POPAD insruction and a RET..that will insure You can find the OEP there...well most of the times Now, go over the RET instruction (f10), and while you are on the RET instruction Type F10 again..u will be here: EAX=01017000 EBX=00520000 ECX=81653AB0 EDX=81653AF0 ESI=81653A90 EDI=00000000 EBP=0056FF78 ESP=0056FE3C EIP=010119E0 o d I s Z a P c CS=0177 DS=017F SS=017F ES=017F FS=5607 GS=0000 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄbyteÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROTÄÄÄ(0)ÄÄ 0177:010119E0 55 8B .. .. .. .. .. .. .. ..and ect <= notice here 0177:010119F0 A1 00 00 00 00 50 64 89-25 00 00 00 00 83 C4 98 .....Pd.%....... 0177:01011A00 53 56 57 89 65 E8 C7 45-FC 00 00 00 00 6A 02 FF SVW.e..E.....j.. 0177:01011A10 15 B8 10 00 01 83 C4 04-C7 05 78 3F 01 01 FF FF ..........x?.... ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä 0177:010119DF INT 3 0177:010119E0 PUSH EBP <= u break here 0177:010119E1 MOV EBP,ESP 0177:010119E3 PUSH FF 0177:010119E5 PUSH 01001570 0177:010119EA PUSH 01011D60 0177:010119EF MOV EAX,FS:[00000000] 0177:010119F5 PUSH EAX 0177:010119F6 MOV FS:[00000000],ESP 0177:010119FD ADD ESP,-68 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Also notice that EIP use the Original Entry Point (010119E0) EAX= First Entry Point (01017000) Now We are going to use the "EBFE" trick! , Do this in sICE: E EIP [enter] EBFE : Change 558B to EBFE (Don't forget to remember 558B!) ALT+D : To return to the Command Window 0177:010119E0 EB FE EC 6A FF 68 70 15-00 01 68 60 1D 01 01 64 <= it will be like this 0177:010119F0 A1 00 00 00 00 50 64 89-25 00 00 00 00 83 C4 98 0177:01011A00 53 56 57 89 65 E8 C7 45-FC 00 00 00 00 6A 02 FF 0177:01011A10 15 B8 10 00 01 83 C4 04-C7 05 78 3F 01 01 FF FF ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä 0177:010119DF INT 3 ==> 010119E0 JMP 010119E0 (JUMP) ; the trap So we set a trap on the OEP ;D, now let's clear all breakpoints : BC * Again set a breakpoint on BPINT 3 , and leave sICE Load calcc.exe, sICE breaks, type: E EIP edit the CC->6A and press Now leave sICE again (without clearing the breakpoints)..calcc.exe runs like a baby Well what's the point of it?? The point is that u wont get a error message when loading calcc.exe (without bpint3,cc->6a) That way i can dump it in prucdump while the program is loaded and dont crashing Prucdump When trying to DUMP it. Now let's open procdump again...now in the TASK window, scroll down and look for The Directory u have the encrypted file, mine is: D:\Dont Tuch\notepade.exe If not there, right click on mouse...press REFRESH LIST...ohh here it is :=) 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. 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 So click "PE Editor" and choose the new dump file we made. Now the entry point is still 00017000, so change it to the real OEP: 119E0 It will be like this now: Entry Point: 000119E0 Now on the "Apply Changes Method:" choose "To PE File" <= so we can kick the encryption Code. After choosing "To PE File", click "SECTIONS" button. Now right click on the last section with mouse and choose: "Kill Section" This will ensure that the encryption code wont be on the dump.exe anymore ;D Now we will change the Size of Image of the dump: Size of Image = Last Virtual Offset + Last Virtual Size (last section) So because we killed the last section, we will use the last section, To fix this take NOW the Offset = 00014000 Virtual Size = 00002618 Size of Image = 00014000 + 00002618 = 00016618 Click "CANCEL" button and we back to the Header Info screen, We see that the "Size Of Image" = 00018000 <= which is not correct! (at least here it is) :-) Change "Size Of Image" = 00018000 -> 00016618 Press OK to rebuild the structure of dump.exe ;D Exit procdump. Try running the file (dump.exe)...what's going on, it's not working...why! >:/ Well it won't work because it runs in a loop that provides form the program from working! Remember: 558B -> EBFE? So open hex editor again and go to the OEP we found (119E0). Change back EBFE -> 558B Save the file... Run Dump.exe...IT CRASH AGAIN!!!!!!!!... >:| Wtf...why :( ? well the main reasone is because the EP v0.2 encryption routine, it basiclly F*CKING UP THE IMPORT TABLE... To know more about IT i suggest to read some tuts about it! 2.==============================The Hunt For Original IT======================================= Ok, open up sICE, Set a BPINT 3 breakpoint, exit sICE, load the encrypted file (not the dump) SICE pop..just E EIP to bring back the original value so we can trace with sICE.. E EIP CC 00 To: 6A 00 or Alt+D Now we can tracing...it wont take a while..try find it your self Didn't find?? So notice this adresses: EAX=01017020 EBX=00520000 ECX=00000000 EDX=7461644F ESI=01012020 <= Original IT EDI=00000000 EBP=01000000 ESP=0056FE18 EIP=01017170 o d I s z a p c CS=0177 DS=017F SS=017F ES=017F FS=1127 GS=0000 DS:0101202C=000122A2 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄbyteÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROTÄÄÄ(0)ÄÄ 0177:01017000 D4 76 F7 BF AC 6D F7 BF-00 10 00 00 00 20 01 00 .v...m....... .. 0177:01017010 00 30 01 00 00 10 00 00-00 00 00 00 20 20 01 00 .0.......... .. 0177:01017020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 0177:01017030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄPROT32Ä 0177:01017163 JMP 0101713C 0177:01017165 ADD ESI,04 0177:01017168 LODSD 0177:01017169 TEST EAX,EAX 0177:0101716B JZ 01017175 0177:0101716D ADD EAX,EBP 0177:0101716F XCHG EAX,ESI 0177:01017170 MOV EAX,[ESI+0C] <= Original IT is moved to eax which is stored in esi 0177:01017173 TEST EAX,EAX 0177:01017175 JZ 010171F6 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A quick: ? ESI Will show you ESI=01012020 So we can assum for that, that Original It is starting at 12020 (write it down) 3.===========================Fixing Broken Import Table======================================== So once that we know the start point of the IT, we open prucDump again, choosing the "PE EDITOR" Button, and choosing our dump.exe That we have created! Now in order to change the IT starting point click the "DIRECTORY" button: You will see: Import Table: RVA Size 00017008 00000034 But we know 00017008 is not the real starting point of the IT, so just edit it and replaced It with the real RVA 17008->12020 Click OK, and OK again..dump.exe is rebuild. Now we need to use a program called "ImportREC" which will fix us the broken Import Table. How to use and fix throw ImportREC?? ===================================Using ImportREC============================================== 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. 2. In the "attach to an active proccess" choose our program -> d:\dont tuch\calcc.exe 3.Enter the correct OEP+IT at "IAT INFOS" OEP = 000119E0 RVA = 00012020 SIZE = 00001000 4. Press the "IAT AutoSearch" button , and u will get the message "Found something!" and some RVA and OEP informations Click OK! 5. Click the "Get Import" button 6. Click the "Fix Dump" button, and choose the dump.exe we made * Note, sometimes u will get an errorMessage but you can ignore it ;) otherWise it works fine. 7. Exit ImportREC Ok so we fixed the dump.exe with good IT..it's time to check the program. You will see a new file is created dump_.exe ;) click it!! it works ;D EP.v0.2 Is Unpacked And Reversed! Have fun ;D My thanks and gratitude goes to: -------------------------------- ----CODE_INSIDE----- ;for helping in cracking -----BLAcKgH0sT------;for being good friend :) -------FusS------ ;asm / keygenning helper