After reading the last post we have some general idea’s of what we are looking for, let’s look at using dumpbin and pefile to analyze a file and see if we can determine the presence of a packer.
For the sake of the post/ learning I have copied notepad.exe from c:\windows\system32 to a local working directory. I have downloaded the latest copy of UPX and packed the notepad file using the command line:
upx -9 -o noteupx.exe notepad.exe
Doing this allows us to compare and contrast differences as we look at the packed file. This will enhance your learning experience. Afterall, if you don’t know what the data should look like, how will you be able to tell if something is not right.
Dumpbin comes with Microsoft Visual c++. The utility provides information about the format and symbols provided in an executable, library or DLL file. A full description of dumpbin can be found here. http://support.microsoft.com/kb/177429.
Using the /imports option we can dump a list of imports for our file.

We see that our file has hardly any imports. Looking at the original file, we dump the imports and we see quite a few imports in the file. Going back to the last blog post, We know a lack of imports is indicative of a packer.
Utilizing strings from sysinternals, or on a Linux machine we find that the original PE file has quite a few strings in it. When we dump the strings on the packed file however, we find what appears to be mostly garbage data. Strings can be used simply by typing the following on the windows or Linux command line.
strings
Next we want to take a look at the sections. We know what sections should be there, but we don’t know if they will be. After all if the PE is Packed, there is a chance the packer changed the section names. Using Dumpbin we can obtain a list of the sections names. On the command line we simply type
dumpbin /HEADERS noteupx.exe
We recieve some output as follows:

We see that within out headers we have a few sections with weird names. First section that comes to our attention is UPX0, The Second is UPX1. The .RSRC section is our Resources Section and we would expect that to be there. If you are following along with the notes on PE format, you will note that the UPX sections are obviously different from what we would expect. A little googling will reveal that UPX (although we already knew that) is the name of a packer that is very common.
With the information we just obtained we could easily go on to Tuts4you.com and grab a tutorial on unpacking UPX. Of course, for UPX there are some automated utilities that will help in unpacking the PE for you.
Moving on, let’s look at pefile. Pefile is a “multiplatorm python module to read and work with PE files….” pefile is hosted on google code and can be located at http://code.google.com/p/pefile/. To install pefile simply download the code, browse to the directory on the windows command line and execute the setup.py file by running python setup.py install. From here on out we will be writing some code in python to utilize pefile. All our python scripts will start with
import pefile
Lets use pefile to look at our sections:
import pefile
import sys
file = pefile.PE(sys.argv[1])
for section in file.sections:
print(section.Name, hex(section.VirtualAddress),
hex(section.Misc_VirtualSize), section.SizeOfRawData)
When run against each of our PE files, we recieve a nice dump of the sections along with their virtual addresses, sizes and the size of the raw data. From the screen shot below, you can see the differences not only in the section names, but also the sizes.

Next we will use pefile to take a look at the imports of the PE. Like the previous example this one is pretty simple to code up and works very well.
import pefile
import sys
file = pefile.PE(sys.argv[1])
for entry in file.DIRECTORY_ENTRY_IMPORT:
print entry.dll
for imp in entry.imports:
print 't', hex(imp.address), imp.name

running our code against both executables again reveals what we figured it would. The packed PE has very few imports, the unpacked PE on the other hand scrolled off the screen with all the imports.
This post was a rough introduction to Pefile and dumpbin. You can do a lot of cool stuff with Pefile and I recommend you go check out the wiki and play around with it a bit. http://code.google.com/p/pefile/w/list