Manually Unpack UPX with the Isolated System Emulator
In this how-to we are going to manually unpack a UPX sample using the Isolated System Emulator (ISE, from now on). We're going to use the sample with SHA256 hash B1715924A653879892E556DC0087446DF43328CAE7A1D16639A604E914C5B812. While unpacking UPX is simple in general, this tutorial aims to help with manually unpacking binaries that use different packers; UPX is only used as a simple use case.
Prerequisites
This tutorial assumes ISE is already configured and skips this part. For more information about how to configure ISE, see Isolated System Emulation.
Set a breakpoint on the tail call
When we open the aforementioned binary in IDA it will show the single function it was able to create, start at 0x00430A40. Now, let's put a breakpoint in the very first instruction at the entry point and just press the "Start isolated system emulator" button as shown in the screenshot:

Execution will stop at that instruction. Now, let's take a look at the control flow graph for this function:

The highlighted basic block at the bottom left is the exit node containing the jump to the Original Entry Point (OEP) in the binary. Let's put another breakpoint here and press F9, or Debugger → Continue Process, to let IDA resume execution until the breakpoint is reached. At some point it will stop, as shown in the next screenshot:

Now, let's press F7, or Debugger → Step Into. It will jump to the OEP as shown in the screenshot below.

Press P to create a function at this point, and then rename it by pressing N and using a new name: OriginalEntryPoint. Any name works, but OriginalEntryPoint is the default name the Generic Unpacker gives to the OEP. The next step is to take a memory snapshot so the bytes in the IDA database reflect the changes the unpacking code did in memory by doing Debugger → Take Memory Snapshot, selecting only "Loader Segments", and then pressing OK:

After this we can stop the debugger, no need to keep it running any more. At this point we have the bytes of the unpacked binary in the IDA database, but there are various other little tasks that we have to manually fix. Be sure that you are at the first instruction of the OriginalEntryPoint function, then go to the Python prompt in IDA and type the following:
This will run the same code that IDA does when first opening a binary, trying to apply startup signatures (FLIRT) for this binary. Startup signatures do more than applying FLIRT signatures normally, which only renames the library functions they match: they also set the compiler, find the main function, and load the corresponding type library and library signatures. The result is shown in the next screenshot:

IDA detected that the initially used type library and chosen compiler were wrong: we're dealing with a binary made with Borland C++ Builder. So, in the dialog IDA shows, first choose "Change compiler", and then, when asked again, click on "Unload library". IDA will change the compiler, load the appropriate type library, apply FLIRT signatures where appropriate, and do code analysis in some parts of the binary. After it finishes, it will show an IDA database with multiple discovered functions, some symbol names, etc., as shown in the screenshot below:

There are portions of the database that were not 100% analyzed, and in order to do a second pass we are going to execute the following code in the Python prompt:
This code will reanalyze the whole database, from the minimum address in the database, to the last available address. In case you are curious: no, it's not the same as the "Reanalyze Program" option, it does more things. After running it, the list of discovered functions jumps from 401 functions to 447, not bad. We can just jump to the symbol main, discovered by the startup signatures, and start analyzing the binary.
Recovering the Import Address Table
One more little thing was not completely handled in the previous steps: discovering the Import Address Table (IAT). We are going to recover it to know which Windows APIs it is calling. First, let's put breakpoints in the UPX unpacker stub at 00430B5A and 00430B6F, the instructions shown below:
These are the functions the UPX unpacker stub uses for loading DLLs and resolving Windows APIs. After setting up the breakpoints, run ISE again by pressing the "Start isolated system emulator" button, then press F9 to let it run until the first breakpoint is hit. The target of that function call is "LoadLibraryA" as shown below:
There are other offsets to functions I manually created (by pressing O on each instruction) to show what is in that area. Now, we are going to select this area and then go to File → Script file... and in the dialog that opens we will select the script $IDA_DIR/idc/renimp.idc (a script to rename entries of a dynamically built import table). After running it the selected area will look like this:
As we can see, it created offsets with proper names and function prototypes for these functions. But these are just the ones used by UPX itself to unpack the binary. Let's press F9 to continue execution until we reach the breakpoint at 00430B6F:
This is the code resolving function names and storing them where EBX points. Let's remember the value of EBX at this time (0042B12C) and let the binary run freely until it reaches the OEP. When the breakpoint is hit go to the previously mentioned address, where EBX pointed, as it will be the beginning of the IAT:
As in the previous case, we need to create an offset for every instruction, by going to each one and pressing O, and then run the renimp.idc script. After manually doing all of this, it will look like this:
Now, last but not least, select Debugger → Take Memory Snapshot, choosing only "Loader Segments", so our changes are saved into the IDA database before stopping the debugger. After taking the memory snapshot it's safe to stop it.
Let the Generic Unpacker finish a manual unpack
Most of the work after reaching the OEP, like taking the memory snapshot, rebuilding the IAT and reanalyzing the database, is the same for every packer. The Generic Unpacker has a heuristic, already-unpacked, that does not unpack anything and runs only these steps. It is useful for packers the Generic Unpacker cannot unpack by itself: you do the packer-specific part by hand, and let the Generic Unpacker do the rest.
To use it, follow the steps in Set a breakpoint on the tail call until you reach the OEP with F7, and do not stop the debugger. Then press Shift + P, or choose Extensions → Malware Analysis → Unpacking → Unpack now..., and select already-unpacked in the heuristic dialog. The Generic Unpacker uses the running debugger session: it finds the IAT and renames the API offsets, takes the memory snapshot, reanalyzes the whole database and looks for the original entry point with the startup signatures. If you already renamed the OEP function, it keeps your name.
Compare with the automated unpacker
Manually unpacking packed samples, while possible, is a bit cumbersome as there are multiple little steps that must be followed. The Generic Unpacker, part of the Malware Analysis add-on, handles the steps mentioned before, and much more, automatically. Let's reopen the binary and start from scratch. After IDA's auto-analysis is done, the Generic Unpacker, if configured to do so, will ask us if we want to unpack the binary, as it looks packed:

We can choose "Yes" here or, alternatively, we can say "No" and run the Generic Unpacker later by pressing its shortcut, Shift + P, or choosing Extensions → Malware Analysis → Unpacking → Unpack now.... When the Generic Unpacker is executed it will ask for confirmation if we really want to run it, as it will destroy almost everything in this database; we can safely choose "Yes" in this case, and then another dialog will be shown:

In this dialog we can choose which heuristic we want to use for the binary to unpack. In general, run-until-timeout is a good default heuristic, but the Generic Unpacker detected that this packer might benefit from another heuristic, run-to-exit-point, and offers it by default. Indeed, for UPX it is the best heuristic we can choose. Now, just press OK and wait. At some point the Generic Unpacker will finish its work and will show a database like the following one:

Everything that we did before manually is automatically done by the Generic Unpacker: it discovers, renames, and adds to the entry points list the OEP; discovers the IAT, applies offsets and renames entries; it also reanalyzes the whole database, forces function creation whenever it decides it is appropriate; applies FLIRT signatures, etc.
It's simpler to use the Generic Unpacker than to do it by hand, but IDA lets you do it manually as well, and now you know how to do it either way.
Last updated
Was this helpful?
