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