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