Lab-02-Python PE Parser
Lab-02-Python PE Parser
Python PE Parser
Objectives
Learn how to write a Python Program to Analyze a Windows PE File.
Overview
By now you have an idea what a Windows Portable Executable (PE) file is and used a couple of
tools to analyze a PE file. This time we will see how we can benefit from our Python skills to
write our own PE analyzer.
© 2021 exploit.ashemery.com
1
OFFENSIVE SECURITY & REVERSE ENGINEERING (OSRE) 2021
3. Now after you finished creating a virtualenv, let us activate it and install pefile.
$ source pework/bin/activate
4. You should now be within the pework environment and should see something like this:
6. To test that everything is successful, just open a python interactive interpreter and run:
>>> import pefile
© 2021 exploit.ashemery.com
2
OFFENSIVE SECURITY & REVERSE ENGINEERING (OSRE) 2021
3. Now let’s analyze the file klogger.exe, so we need to load it using pefile. This can be done
like this:
pe = pefile.PE('/usr/share/windows-binaries/klogger.exe')
4. Now suppose we want to know the address of the Image Base. We can use the following:
hex(pe.OPTIONAL_HEADER.ImageBase)
5. What if we wanted to know the address of the Entry Point? This could be done like this:
hex(pe.OPTIONAL_HEADER.AddressOfEntryPoint)
6. Also, if you want to know the number of sections in this PE file, we can do:
hex(pe.FILE_HEADER.NumberOfSections)
7. Okay, that seems nice right ☺? Now let us print all the sections found in the file. This could
be done like this:
for section in pe.sections:
... print (section.Name, \
hex(section.VirtualAddress), \
hex(section.Misc_VirtualSize), \
section.SizeOfRawData )
© 2021 exploit.ashemery.com
3
OFFENSIVE SECURITY & REVERSE ENGINEERING (OSRE) 2021
Deliverable #4: How can you change the Address of the Entry Point to 0xBEEFBEEF?
Suppose you want to check if the file is Packed or not, you can use the Lab-02-packchecker.py
file given. It can be used like this:
packchecker.py putty.exe
Deliverable #5: What is the packchecker code checking for and why (hint: read the code and try
to understand it)? Explain your answer.
Deliverable #6: Please reflect on your learning from this lab and what was not clear to you so
we can discuss it together.
DIY: Now write the changes to a new file called “putty.exe” and also try solving some of the
questions we used in our PE lab, but instead of using CFF Explorer, use your python skills ☺
© 2021 exploit.ashemery.com
4