I am using the generic approach so
that you will be able to unpack the executable which is packed with
any version of UPX. Here I am using the
OllyDbg to unpack the PE file. Although you can use any
debugger, OllyDbg is one of the best ring 3 debugger for reverse
engineering.
That is all you needed to know before the start. Lets get into some
action. Load the UPX packed binary file into the OllyDbg and start
tracing the binary, until you encounter a PUSHAD instruction.
Usually this is the first instruction or it will be present in the
first few instructions based on the UPX version that you have used
to pack the PE file. Now put the breakpoint on POPAD instruction. So
when we reach POPAD instruction, all the sections will be unpacked
and all imports will be resolved. After POPAD instruction, it will
jump to OEP.
There are many ways to set the breakpoint at the PUSHAD instruction.
When you are at this instruction, you can put the hardware read
breakpoint at ESP-4 address. If you have command bar plugin
installed then you can just type 'hr esp-4' to set this breakpoint.
Other way is to find the POPAD (opcode 61) instruction in the code
and set the breakpoint for yourself. You can just scroll down in
OllyDbg from the current instruction till you find all zeroes, then
just few instructions above the start of zeroes you will find the
POPAD instruction. Now you can directly set breakpoint on POPAD
instruction.
Once you have set the breakpoint, press F9 and you will break on the
instruction which is immediately after POPAD or on POPAD instruction
based on which method you have used. Now start tracing with F7 and
soon you will encounter a instruction which will jump to OEP that is
somewhere in the unpacked code section.
Now you have found the OEP. Note down this address somewhere. Next
task is to dump the entire binary image from memory. This can be
done using OllyDmp plugin which comes with OllyDbg. Now launch this
plugin and dump the entire binary file to the disk using default
options. It will automatically fix the import table for you so there
is no need to explicitly fix it. That's it and you are done with
unpacking the UPX packed file.