34 Commits

Author SHA1 Message Date
Alessandro Gario
0d0ca1861c Squash (#56)
- CMake: Refactor, added more warnings.
- Refactor
- Added Windows support
- Added a missing include file for linux.
- Do not set CMAKE_CXX_STANDARD on Windows
- Always initialize the stat struct
- CMake: update the required version, request C++11, disable GNU extensions
- CMake: Add default switch cases, fix GCC warnings.
- Prefer assignment from an empty object when initializing
2017-10-19 12:13:35 -04:00
Jozef Kolek
9f74016e0d Perform endian swap as needed 2017-04-12 00:24:03 +02:00
Jozef Kolek
cb2fd5736f Added clang-format spec - issue #35 2017-03-31 17:01:20 +02:00
Jozef Kolek
1183047cbb Changes made according to clang-tidy review.
* NULL is replaced by nullptr
* Added parameter std::nothrow to operator new so in case of
  failure it returns nullptr instead of throwing exception
  std::bad_alloc. This is important due to check that follows
  the statement. Example:
  parsed_pe *p = new(std::nothrow) parsed_pe();
  if (p == nullptr) {
    ...
  }
* Using range-based for loops.
* Removed redundant boolean literals.
  Example: if (readWord(...) == false) => if (!readWord(...))
* Resolved implicit casts.
  Example: if (!ch) => if (ch == 0u)
* Created functions getImports, getExports, getRelocations from
  parts of ParsePEFromFile to make it smaller and more readable.
* Using reinterpret_cast instead of C-style cast to convert
  between unrelated types.
* Added braces around statements to improve readability.
2017-03-31 00:21:20 +02:00
Jozef Kolek
326ca41bc6 Symbol table parsing 2017-03-16 22:18:08 +01:00
hobo-ru
465ed4ba6e Make the sources MS VC compatible (define the macro for __typeof__) 2017-03-09 19:25:20 +03:00
hobo-ru
1a5455e06b Get rid of boost integer types 2017-03-09 19:10:17 +03:00
artemdinaburg
a8ccfb9df3 Put all of peparse in the peparse namespace. (#26)
* Put all of peparse in the peparse namespace.
* Fixes dupicate symbol problems when using the library inside other applications, namely Python
* Closes #25
2017-03-03 14:41:14 -05:00
munin
e75e0383dc small change 2014-08-08 17:55:35 -04:00
Wesley Shields
77b72f3cc9 Implement PE32+ and error reporting.
Teach the parser to properly handle PE32+ binaries.

The major differences are:
  - Fields in the OptionalHeader which are not relative are now 64 bits.
  - Base addresses should all be 64 bits.
  - The BaseOfData field is not available on PE32+

There is now a 16 bit field tacked on to the end of nt_header_32 called
OptionalMagic. This is a duplicate of the Magic field in optional_header_32
and optional_header_64, but is stored in nt_header_32 to make it easier
to determine which optional header is being used.

I also added support for better error reporting. Now when something fails
to parse you can use a couple of functions to find out what happened and
where it happened:
  - GetPEErr(): Return the error as an integer.
  - GetPEErrString(): Return the error as a string.
  - GetPEErrLoc(): Return the function and line number of the error.

Made some changes to pepy to account for these changes. The interface
into pepy is identical. Only externally visible changes are that
pepy.parse() will now return the error string and location when parsing
fails and the baseofdata attribute will throw an exception if the binary
is PE32+.

to_string.h is now included from parse.h, so remove it from dump.cpp.

While here do a bunch of cleanups to make printing consistent. Use '0x'
where appropriate and ensure exceptions are punctuated correctly.
2014-03-07 13:18:24 -05:00
Wesley Shields
acd76dbcda Cleanup all the macro definitions.
Instead of constantly defining and redefining the macros to read values
just define them once. There are now the three main ones (READ_WORD,
READ_DWORD and READ_BYTE) along with READ_DWORD_PTR and READ_DWORD_NULL.

Each macro takes a pointer to a bounded_buffer (what to read), an offset
(where to read), a structure and member (what to read into). You should
use READ_DWORD_PTR when you have a pointer to a structure. You can
use READ_DWORD_NULL when failure to read should return NULL as all the
rest return false.

Fixes #7.
2014-01-17 00:34:58 -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
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
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
munin
5531d3a249 . 2013-11-11 15:09:57 -05:00
munin
10bdaac799 now uses mmap instead of malloc 2013-07-31 14:04:56 -04:00
munin
5b55d18c2f change some names to reflect reality 2013-07-31 13:38:33 -04:00
Andrew
2244945059 add an API for reading a byte from a VA 2013-07-30 19:09:31 -04:00
Andrew
e83e171b8c support to print out relocations 2013-07-30 17:55:52 -04:00
Andrew
fea370c30e find section logic 2013-07-26 17:52:53 -04:00
Andrew
3e1b2ea8d2 some definitions 2013-07-26 17:12:47 -04:00
Andrew
feb1d3391a parsing sections 2013-07-26 13:13:24 -04:00
Andrew
240928b373 pretty-printing meta-programming 2013-07-26 09:36:05 -04:00
Andrew
1a3b1f2ccb license time 2013-07-25 18:59:25 -04:00
Andrew
beb0e72a6a . 2013-07-25 15:20:17 -04:00
Andrew
e6f3f4f948 . 2013-07-25 15:19:00 -04:00
Andrew
166dd4e8df . 2013-07-24 19:15:53 -04:00
Andrew
4a2eb58e82 . 2013-07-24 18:52:38 -04:00
Andrew
25f62aed6c . 2013-07-24 18:43:02 -04:00
Andrew
20aee7ccb9 . 2013-07-24 18:32:56 -04:00
Andrew
50ff629e28 . 2013-07-24 17:57:58 -04:00
Andrew
58643abf49 . 2013-07-24 17:35:25 -04:00
Andrew
d57fa227a8 . 2013-07-24 17:32:23 -04:00
Andrew
c96cbbce7c . 2013-07-24 16:38:00 -04:00