Packing and Unpacking:

Malware is often obfuscated and or “packed” to make it harder to reverse and examine. Packing can modify various data sections. When dealing with packed malware you will often need to reconstruct the import section.

In genreal we will want to find the “unpacking stub” which will lead to the OEP Original Entry Point of the executable. From there we can reconstruct the program. The unpacked program will not be identical to the original as the PE header is reconstructed.

For more info on packing see this eariler post: https://saadams.github.io/posts/mal-antirev/

Steps for Unpacking:

Automatic:

1
Use a tool like PE explorer, PEID

Manual:

Repairing Imports Manually:


Sample 1

DIE (UPX detected)

alt text

Here we can see that the file is detected to have used UPX to be packed.

alt text

We can also observe that the entropy is very high which is a good indicator that it is packed.

Due to the fact that this file is using a modified version of UPX we will not be able to simply unpack it with UPX.

alt text

Manual Unpacking:

DIE,unpack

alt text

alt text

alt text

sus jump

alt text alt text

alt text

alt text

alt text

alt text

alt text

alt text


Sample 2

DIE results:

alt text

Here we can see that DIE believes the file is packed due to repeating section names.

Lets check out the binary sections to see what is going on.

alt text

We can also observe that the entropy is very high which is a good indicator that it is packed.

We can also see that the DOS stub has been modifyed.

alt text

This differs from what would we expect to see “This program cannot be run in DOS mode.”

Within PEStudio we can see that the packer used is FSG 1.0.

alt text

Unpacking:

alt text

alt text

This jump while at first it stands out it ends up just being part of a looping call for the function.

However if we follow this call we can see that there is another jump instruction

alt text

This jump appears to go to the end of a code section with invalid instructions and it jumps far away from the current addresses of the code being executed. This could likely be a tail jump.

Below we can observe the section the code jumps to.

alt text

Lets pick this apart in a debugger and see what is going on. I am going to set a breakpoint on the first address we found as well as the second.

alt text

As the program runs you can watch the addresses increase in esi and edi this is likely used with the packing algorithim.

alt text

alt text

alt text

Now im going to jump to the next breakpoint that we marked.

alt text

This time the jump is not taken so lets run the program until we see the jump is taken.

alt text

Each time we run it we can see a new string stored in edi

alt text

Here we can see the jump will be taken.

alt text

Lets take it…

alt text

Now lets run an analysis in x32dbg using anlaysis > "analyze module"

Then we can pick apart the disasembly and make sure it looks like unpacked code…

Here we can see the functions:

alt text

Clicking into sub_401090 which is where our jump took us we can open up a disassembly page and see what it is doing.

After picking around a little bit I found the following:

alt text

That looks pretty unpacked to me. Lets dump it and open it up with IDA.

I will be dumping with scylla

alt text

Remmeber to use the IAT autosearch first.

alt text

You can additionally use fix dump on the dumped file if something doesnt look right.

Now we can work in IDA

alt text


Sample 3

DIE results:

alt text

Here we can see that DIE believes the file is packed using PE Compact.

pestuido:

alt text

We get the same conclusion in pestudio.

Looking in IDA we can see a small ammount of imports, functions and no readable strings.

alt text

alt text

Auto Unpacking:

Do to this sample using PE Compact we can attempt to unpack it with peid and the generic unpacker plugin.

alt text

After clicking the arrow we can see it found a OEP

alt text

alt text

alt text

After unpacking with peid we can open the unpacked binary with IDA and see all sorts of unpacked strings.

alt text

We could now continue analysis in IDA or another dissasembler with the unpacked binary.

Manual Unpacking:


Sample 5 (Lab18_05.exe):

LoadLibraryA often used with packed malware. alt text

DIE (Upack like) alt text

High entropy alt text

References/Resources: