109 Commits

Author SHA1 Message Date
Andrew Ruef
ac15bf8faf Merge pull request #6 from wxsBSD/resources
Implement resource parsing.
2014-01-02 09:37:30 -08:00
Wesley Shields
4e5d6f6902 Document size and RVA changes. 2013-12-30 17:10:44 -05:00
Wesley Shields
ec5c49eaff Make resource parsing more resilient.
I have a UPX packed sample that corrupted the resource directory. These changes
allow the resources to be properly parsed.

They add an RVA and size to the resource struct. This is the address and size
of the resource as it is declared in the directory. If the address is invalid
create a zero-length buffer for the data. If the size is invalid (ie: it goes
off the end of the .rsrc section) create a zero-length buffer for the data.
Otherwise, return the actual data.

This allows consumers of the rsrc to figure out if the resource is corrupt
or not by comparing the length of the buffer to the size element. If the
size is greater than 0 but buffer is empty then it's invalid.

Also, it should never happen but just to be safe make pepy catch NULL
buffers (in pepy_data_converter) and return an empty bytearray.
2013-12-30 16:45:50 -05:00
Wesley Shields
b9d1592c50 Fix a type and expand a comment a bit. 2013-12-25 21:10:23 -05:00
Wesley Shields
8f9662cc92 Fix resource parsing so it works. ;)
I had initially written this in such a way that it would break if there
were multiple entries anywhere other than the first table. This change
now works across more complex samples that I have tested against.

While here, I did a little moving around and had to create a structure
that isn't used other than to know how far to move the offset when
parsing. This is because the struct into which I am parsing the data
keeps track of other things along the way, so it's size is incorrect.

While here, change parse_resource() to be parse_resource_table() as it
is more accurate to what it really does.
2013-12-25 21:01:55 -05:00
Wesley Shields
bc6b67fa0e Fix a bug in test.py.
When iterating through the bytearray it would cause a python crash if
the byte value was 0x78. I have a test sample where the first 8 bytes
at the entry point are 0xe8 0xa6 0x4e 0x0 0x0 0xe9 0x78 0xfe. If I don't
do this dance it crashes when trying to get the 6th (0x78) byte out
of the array.
2013-12-24 15:38:45 -05:00
Wesley Shields
913b3c16d1 Catch if PyInt_FromLong() returns NULL. 2013-12-24 14:43:09 -05:00
Wesley Shields
4a574e0e07 Remove debugging. 2013-12-24 13:34:04 -05:00
Wesley Shields
a6af4cbd18 Implement resource parsing.
While here, fix a memory leak in pepy as I was not decrementing the
reference counter on self->data in section_dealloc().
2013-12-24 12:41:59 -05:00
Dan Guido
b139ae3b39 Merge pull request #5 from wxsBSD/pepy
Pepy
2013-12-15 12:23:58 -08:00
Wesley Shields
f23cbb132f Document the data attribute on sections. 2013-12-14 22:36:56 -05:00
Wesley Shields
dae8606469 Bugfix to get_bytes and add section.data.
If get_bytes does not fill the list, get a slice of what was filled and
use that to convert to a bytearray. I still want to find a way to just
use a bytearray from the start. Luckily with the rest of this commit I
don't have a need to call get_bytes() on sections anymore.

Sections now have a data attribute which is a bytearray of the data that
makes up that section. This way you can just use section.data attribute
to get the entire contents and operate on it as you wish.

Make test.py use section.data to generate an MD5 of the section. It now
also prints the first 10 bytes of each section (if there are bytes).
2013-12-14 22:26:58 -05:00
Wesley Shields
23ebc6e799 Whitespace. 2013-12-12 16:19:07 -05:00
Wesley Shields
9494d96300 Switch to a bytearray for get_bytes().
It probably isn't the best way to do it but I couldn't get anything to work
when trying to generate a bytearray object directly. As a workaround I first
put each byte into a list and then convert the list to a bytearray.
2013-12-12 16:14:53 -05:00
Wesley Shields
ab00385114 Consistent puncutation and whitespace. 2013-12-01 16:08:59 -05:00
Wesley Shields
cd99c9f603 Move code around in test. 2013-12-01 16:05:07 -05:00
Wesley Shields
3b70ce0409 Update README.md
Fill in more docs in README.

Remove descriptions of attributes, you can get them from the docs.
2013-12-01 16:07:51 -05:00
Wesley Shields
cb0549dbb2 Document parsed object. 2013-11-30 23:17:50 -05:00
Wesley Shields
1caefedc6a Start to document things. 2013-11-30 23:08:44 -05:00
Wesley Shields
b867946050 Implement relocations.
This still needs testing.
2013-11-30 22:44:30 -05:00
Wesley Shields
dc4567de3f Merge branch 'master' into pepy 2013-11-30 22:25:50 -05:00
Wesley Shields
5e86f97c96 Implement a bunch of parsed attributes.
These are all the things the dump-prog pulls out already.
2013-11-30 22:21:10 -05:00
Wesley Shields
5fb0afd098 Simplify things a bit.
Instead of having 2 macros for each object simplify by having 1 set of
macros that can work across all objects except the parsed object. I could
make this work for the parsed object by making the parsed object store
PyObject pointers to the parsed values instead of creating them on the fly
while getting an attribute.
2013-11-30 21:54:38 -05:00
Wesley Shields
7abab7bd2e Implement imports and exports.
Might as well do some general cleanup too:

Rename the len attribute of a section to length.

The section, import and export callbacks return 0 on success and anything else
on failure.

Whitespace fixes.

Fix a bunch of copy/paste mistakes in the test script.
2013-11-30 21:36:05 -05:00
Postmodern
e2e44dfc62 All files should have an ext. 2013-11-30 15:15:42 -08:00
Wesley Shields
2083f6f358 Sections are now their own type.
Do not return a list of dictionaries from get_sections(). Now it returns a
list of section objects, which expose the information via attributes.
2013-11-29 23:32:32 -05:00
Wesley Shields
b4ad87819e Support section, symbols and characteristics.
While here, make it easier to extend by providing macros to eliminate
the mundane that goes into writing getset members.
2013-11-29 21:56:12 -05:00
Wesley Shields
912a892e47 Switch from using members to getseters.
This means I don't have to store anything in the pepy_parsed object (PyObject
pointers or native C types). Use a macro to get things out of the parsed
structures and into python objects.
2013-11-29 19:04:45 -05:00
Wesley Shields
3c7d1c1052 Turns out I like using native types.
Switch back to using native types. This is less memory for me to manage.
2013-11-29 16:29:45 -05:00
Wesley Shields
53fb7e7d2c Fix crash, convert back to PyObject pointers.
There was some weird memory corruption caused by how pepy_parsed_init()
was parsing arguments. The result was that accessing attributes or methods
which didn't exist would periodically cause segfaults. This code was leftover
from an earlier way of doing things and doesn't need to be done this way.
Just parse straight to a C style string instead of this crap.

Also implement support for signature, machine support.

Also, add Py_TPFLAGS_BASETYPE as you should.
2013-11-29 16:20:44 -05:00
Wesley Shields
860fbff4e4 Don't store parsed values in python objects.
Convert the PyObject pointers used inside pepy_parsed into their corresponding
native types and use those. Teach the members array to return them accordingly.

While here might as well add support for signature and machine values.

Also, convert test.py to have shorter output by not using pprint.
2013-11-29 14:28:39 -05:00
Wesley Shields
b816e1cbf9 Remove unused import. 2013-11-29 14:12:37 -05:00
Wesley Shields
ed77443f31 Implement timedatestamp member.
While here, DECREF the string used in init. Also, make a note that I really
want to use a bytearray instead of a list for get_bytes().
2013-11-29 14:11:01 -05:00
Wesley Shields
6d8a39ad72 Add a bunch of constants.
These are useful for checking values I'll be adding support for later.

import pepy
print hex(pepy.MZ_MAGIC)
2013-11-27 16:17:22 -05:00
Wesley Shields
20869810cf Silence warnings in pepy.cpp. 2013-11-27 16:16:55 -05:00
Wesley Shields
5ac15ad285 Add README. Still barebones. 2013-11-27 15:59:34 -05:00
Wesley Shields
14e2848aa2 Add a test file that uses the bindings. 2013-11-27 15:53:20 -05:00
Wesley Shields
a928a15b8b Initial commit of pepy (pronounced p-pie).
This is a set of python bindings to pe-parse. It is nowhere near feature
complete yet but I'll keep working on it.
2013-11-27 15:52:24 -05:00
Wesley Shields
94d9b4300c Add python build stuff to .gitignore 2013-11-27 15:51:59 -05:00
Wesley Shields
320eb4e840 Add .swp to .gitignore. 2013-11-27 15:47:06 -05:00
Dan Guido
9147a6728f Create LICENSE 2013-11-23 13:03:08 -05:00
munin
5956722feb oops 2013-11-22 21:19:43 -05:00
Postmodern
5e84744095 Attempt to fix markdown. 2013-11-22 17:17:15 -08:00
Postmodern
6141749257 Added a .gitignore file. 2013-11-22 16:46:23 -08:00
Postmodern
bdc87bb606 Add instructions on how to install boost and cmake on Linux/OSX. 2013-11-22 16:46:23 -08:00
Postmodern
d005c2e4f8 Link to the cmake and boost websites. 2013-11-22 16:45:42 -08:00
Postmodern
c6edada7a1 Fixed a typo. 2013-11-22 16:45:01 -08:00
munin
58a5507b49 Merge branch 'master' of github.com:trailofbits/pe-parse
Conflicts:
	README.md
2013-11-22 19:32:20 -05:00
munin
5f4b8866f2 . 2013-11-22 19:31:11 -05:00
Dan Guido
2534a8512d Update README.md 2013-11-22 19:30:31 -05:00