Jump to content
Nytro

Manual Unpacking of UPX using OllyDbg

Recommended Posts

Posted

Manual Unpacking of UPX using OllyDbg

[TABLE]

[TR]

[TD=class: page_subheader]Introduction[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD]In this tutorial, you will learn how to unpack any UPX packed Executable file using OllyDbg

UPX is a free, portable, executable packer for several different executable formats. It achieves an excellent compression ratio and offers very fast decompression.

[/TD]

[/TR]

[TR]

[TD=align: center] [/TD]

[/TR]

[TR]

[TD=align: center]unpacking-upx-ollydbg-mainscreen.jpg[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD]Here we will do live debugging using OllyDbg to fully unpack and produce the original Executable FILE from the packed file. [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]Packing EXE using UPX[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD]To start with, we need to pack sample EXE file with UPX. First you need to download latest UPX packer from UPX website and then use the following command to pack your sample EXE file.[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_code]upx -9 c:\sample.exe[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD]If you already have UPX packed binary file then proceed further. In such case make sure to use PEiD or 'RDG Packer Detector' to confirm if it is packed with UPX as shown in the screenshot below.[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=align: center]upx-packer-detector-rdg.jpg[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]UPX Unpacking Process[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD=align: justify] Before we begin with unpacking exercise, lets try to understand the working of UPX.

When you pack any Executable with UPX, all existing sections (text, data, rsrc etc) are compressed. Each of these sections are named as UPX0, UPX1 etc. Then it adds new code section at the end of file which will actually decompress all the packed sections at execution time.

Here is what happens during the execution of UPX packed EXE file..

[/TD]

[/TR]

[TR]

[TD]

  • Execution starts from new OEP (from newly added code section at the end of file)
  • First it saves the current Register Status using PUSHAD instruction
  • All the Packed Sections are Unpacked in memory
  • Resolve the import table of original executable file.
  • Restore the original Register Status using POPAD instruction
  • Finally Jumps to Original Entry point to begin the actual execution

[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]Manual Unpacking of UPX[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD]Here are the standard steps involved in any Unpacking operation

  • Debug the EXE to find the real OEP (Original Entry Point)
  • At OEP, Dump the fully Unpacked Program to Disk
  • Fix the Import Table

[/TD]

[/TR]

[TR]

[TD=align: justify] Based on type and complexity of Packer, unpacking operation may vary in terms of time and difficulty.

UPX is the basic Packer and serves as great example for anyone who wants to learn Unpacking.

Here we will use OllyDbg to debug & unpack the UPX packed EXE file. Although you can use any debugger, OllyDbg is one of the best ring 3 debugger for Reverse Engineering with its useful plugins.

Here is the screenshot of OllyDbg in action [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=align: center]unpacking-upx-ollydbg.jpg[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD]Lets start the unpacking operation[/TD]

[/TR]

[TR]

[TD]

  • Load the UPX packed EXE file into the OllyDbg
  • Start tracing the EXE, until you encounter a PUSHAD instruction. Usually this is the first instruction or it will be present after first few instructions based on the UPX version.
  • When you reach PUSHAD instruction, put the Hardware Breakpoint (type 'hr esp-4' at command bar) so as to stop at POPAD instruction. This will help us to stop the execution when the POPAD instruction is executed later on.
  • Other way is to manually search for POPAD (Opcode 61) instruction and then set Breakpoint on it.
  • Once you set up the breakpoint, continue the execution (press F9).
  • Shortly, it will break on the instruction which is immediately after POPAD or on POPAD instruction based on the method you have chosen.
  • Now start step by step tracing with F7 and soon you will encounter a JMP instruction which will take us to actual OEP in the original program.
  • When you reach OEP, dump the whole program using OllyDmp plugin (use default settings). It will automatically fix all the Import table as well.
  • That is it, you have just unpacked UPX !!!

[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]Fixing Import Table[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD] In the current example, OllyDmp plugin will take care of fixing the Import table.

However for most of the packers, we need to use advanced tool called ImpRec (Import Reconstructor). ImpREC is highly advanced tool used for fixing the import table. It provides multiple methods to trace the API functions as well as allow writing custom plugins.

[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=align: center]unpacking-upx-imprec.jpg[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD]For interested users, here are simple instructions on how to fix Import Table using ImpRec.[/TD]

[/TR]

[TR]

[TD]

  • When you are at the OEP of the program, just dump the memory image of binary file using Ollydmp WITHOUT asking it to fix the Import table.
  • Now launch the ImpREC tool and select the process that you are currently debugging.
  • Then in the ImpREC, enter the actual OEP (enter only RVA, not a complete address).
  • Next click on 'IAT Autosearch' button to automatically search for Import table.
  • Now click on 'Get Imports' to retrieve all the imported functions. You will see all the import functions listed under their respective DLL names.
  • If you find any import function which is invalid (marked as VALID: NO) then remove it by by right clicking on it and then from the popup menu, click on 'Delete Thunks'.
  • Once all the import functions are identified, click on "Fix Dump" button in ImpREC and then select the previously dumped file from OllyDbg.
  • Now run the final fixed executable to see if everything is alright.

[/TD]

[/TR]

[TR]

[TD]For advanced packers, you may have to use different methods in ImpRec and some times need to write your own custom plugin to resolve the import table functions.

For more interesting details refer to our PESpin ImpRec plugin.

[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]Video Demonstration[/TD]

[/TR]

[/TABLE]

[TABLE]

[TR]

[TD]This video demonstration uses slightly different way to put a hardware breakpoint than described in the article. Also it uses ImpREC to fix import table which is useful while unpacking advanced packers.

Here are the steps shown in video

[/TD]

[/TR]

[TR]

[TD]

  • Load your EXE in Ollydbg
  • Step Over (Shortcut-F8) PUSHAD instruction
  • Next Go to ESP (right click and follow in DUMP Window)
  • Put Hardware Read Breakpoint (Access) on first dword at ESP. (This is similar 'hr esp-4 at PUSHAD instruction as described earlier)
  • Now Run EXE until we hit breakpoint (shortcut-F9)
  • It will break right after POPAD instruction.
  • You will see a JMP instruction few lines below the current instructions. Put breakpoint on JMP
  • Run exe again until it stops at JMP instruction (shortcut-F9)
  • Step Over JMP (Shortcut- F8)
  • Now we are at OEP, Here just Dump Process using OllyDump without fixing Import table.
  • Here we will use ImpREC to fix the import table as mentioned in 'Fixing Import Table' section.
  • Finally after fixing import table, run the new unpacked EXE to make sure it is perfect !

[/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD] [/TD]

[/TR]

[TR]

[TD=class: page_subheader]References[/TD]

[/TR]

[TR]

[TD][/TD]

[/TR]

[TR]

[TD]

  1. UPX: Ultimate Packer for Executables.
  2. OllyDbg: Popular Ring 3 Debugger.
  3. ImpREC: Import Table Reconstruction Tool
  4. PESpin Plugin for ImpREC
  5. RDG Packer Detector
  6. PEid Packer Detector

[/TD]

[/TR]

[/TABLE]

Sursa: Manual Unpacking of UPX Packed Binary File - www.SecurityXploded.com

Posted

@Nytro

UPX si Aspack sunt usor de despachetat.

Poate postezi ceva si despre Themida, adica cum sa despachetezi Themida, dar fara vreun script, asa pas cu pas, logic.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...