BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
PECore.cpp
Go to the documentation of this file.
1#include "pe/PECore.h"
2
3#define DEFAULT_IMGBASE 0x10000
4
6{
7 dos = NULL;
8 fHdr = NULL;
9 opt32 = NULL;
10 opt64 = NULL;
11}
12
14{
15 buf = v_buf;
16 bool allowExceptions = true;
17 // reset all:
18 reset();
19
20 offset_t offset = 0;
21 this->dos = (IMAGE_DOS_HEADER*) buf->getContentAt(offset, sizeof(IMAGE_DOS_HEADER), allowExceptions);
22 if (dos == NULL) throw ExeException("Could not wrap PECore: invalid DOS Header!");
23
24 offset = dos->e_lfanew + sizeof(DWORD); //skip 'PE' signature
25 this->fHdr = (IMAGE_FILE_HEADER*) buf->getContentAt(offset, sizeof(IMAGE_FILE_HEADER), allowExceptions);
26 if (fHdr == NULL) throw ExeException("Could not wrap PECore!");
27
28 offset = offset + sizeof(IMAGE_FILE_HEADER);
29 WORD *magic = (WORD*) buf->getContentAt(offset, sizeof(WORD), allowExceptions);
30 if (magic == NULL) throw ExeException("Could not wrap PECore: invalid FileHeader");
31
33 if ((*magic) == pe::OH_NT64) {//32 = 0x10B) {
35 }
36
37 if (mode == Executable::BITS_32) {
38 this->opt32 = (IMAGE_OPTIONAL_HEADER32*) buf->getContentAt(offset, sizeof(IMAGE_OPTIONAL_HEADER32), allowExceptions);
39
40 } else if (mode == Executable::BITS_64) {
41 this->opt64 = (IMAGE_OPTIONAL_HEADER64*) buf->getContentAt(offset, sizeof(IMAGE_OPTIONAL_HEADER64), allowExceptions);
42 }
43 if ( this->opt32 == NULL && this->opt64 == NULL) {
44 throw ExeException("Could not wrap PECore : invalid OptionalHeader");
45 }
46 return true;
47}
48
50{
51 if (opt32) return Executable::BITS_32;
52 if (opt64) return Executable::BITS_64;
53
54 return Executable::BITS_32; // DEFAULT
55}
56
58{
59 return static_cast<offset_t> (dos->e_lfanew);
60}
61
63{
64 const offset_t offset = peSignatureOffset();
65 if (offset == INVALID_ADDR) {
66 return INVALID_ADDR;
67 }
68 const offset_t signSize = sizeof(DWORD);
69 return offset + signSize;
70}
71
73{
74 const offset_t offset = peFileHdrOffset();
75 if (offset == INVALID_ADDR) {
76 return INVALID_ADDR;
77 }
78 return offset + sizeof(IMAGE_FILE_HEADER);
79}
80
82{
84 return sizeof(IMAGE_NT_HEADERS64);
85
86 return sizeof(IMAGE_NT_HEADERS32);
87}
88
90{
91 const offset_t offset = peOptHdrOffset();
92 if (offset == INVALID_ADDR) {
93 return INVALID_ADDR;
94 }
95 if (!fHdr) {
96 return INVALID_ADDR;
97 }
98 const offset_t size = static_cast<offset_t>(this->fHdr->SizeOfOptionalHeader);
99 return offset + size;
100}
101
103{
104 if (this->opt32) {
105 if (aType == Executable::RAW) return opt32->FileAlignment;
106 return opt32->SectionAlignment;
107 }
108 if (this->opt64) {
109 if (aType == Executable::RAW) return opt64->FileAlignment;
110 return opt64->SectionAlignment;
111 }
112 return 0;
113}
114
116{
117 bufsize_t imgSize = 0;
118 if (this->opt32) {
119 imgSize = opt32->SizeOfImage;
120 }
121 if (this->opt64) {
122 imgSize = opt64->SizeOfImage;
123 }
124 return imgSize;
125}
126
128{
130 if (this->opt32) {
131 hdrsSize = opt32->SizeOfHeaders;
132 }
133 if (this->opt64) {
134 hdrsSize = opt64->SizeOfHeaders;
135 }
136 return hdrsSize;
137}
138
140{
141 offset_t imgBase = 0;
142 if (this->opt32) {
143 imgBase = opt32->ImageBase;
144 }
145 if (this->opt64) {
146 imgBase = opt64->ImageBase;
147 }
148 //can be null, under XP. In this case, the binary will be relocated to 10000h
149 //(quote: http://code.google.com/p/corkami/wiki/PE)
150 if (imgBase == 0) {
151 imgBase = DEFAULT_IMGBASE;
152 }
153
154 //in 32 bit PEs: it can be any value as long as ImageBase + 'SizeOfImage' < 80000000h
155 //if the ImageBase is bigger than that, the binary will be relocated to 10000h
156 if (this->opt32) {
157 offset_t maxOffset = this->getImageSize() + imgBase;
158 if (maxOffset >= 0x80000000) {
159 imgBase = DEFAULT_IMGBASE;
160 }
161 }
162 return imgBase;
163}
164
uint32_t bufsize_t
const offset_t INVALID_ADDR
uint64_t offset_t
#define DEFAULT_IMGBASE
Definition: PECore.cpp:3
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual bufsize_t getAlignment(Executable::addr_type aType)
Definition: PECore.cpp:102
bufsize_t hdrsSize() const
Definition: PECore.cpp:127
offset_t peOptHdrOffset() const
Definition: PECore.cpp:72
IMAGE_OPTIONAL_HEADER32 * opt32
Definition: PECore.h:41
bool wrap(AbstractByteBuffer *v_buf)
Definition: PECore.cpp:13
virtual offset_t getImageBase()
Definition: PECore.cpp:139
offset_t peFileHdrOffset() const
Definition: PECore.cpp:62
AbstractByteBuffer * buf
Definition: PECore.h:37
void reset()
Definition: PECore.cpp:5
virtual bufsize_t getImageSize()
Definition: PECore.cpp:115
offset_t peSignatureOffset() const
Definition: PECore.cpp:57
IMAGE_FILE_HEADER * fHdr
Definition: PECore.h:40
bufsize_t peNtHeadersSize() const
Definition: PECore.cpp:81
offset_t secHdrsOffset() const
Definition: PECore.cpp:89
IMAGE_OPTIONAL_HEADER64 * opt64
Definition: PECore.h:42
Executable::exe_bits getHdrBitMode() const
Definition: PECore.cpp:49
IMAGE_DOS_HEADER * dos
Definition: PECore.h:39