BearParser
Portable Executable parsing library (from PE-bear)
Toggle main menu visibility
Loading...
Searching...
No Matches
parser
pe
PECore.cpp
Go to the documentation of this file.
1
#include "
pe/PECore.h
"
2
3
#define DEFAULT_IMGBASE 0x10000
4
5
void
PECore::reset
()
6
{
7
dos
= NULL;
8
fHdr
= NULL;
9
opt32
= NULL;
10
opt64
= NULL;
11
}
12
13
bool
PECore::wrap
(
AbstractByteBuffer
*v_buf)
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
51
Executable::exe_bits
PECore::getHdrBitMode
()
const
52
{
53
if
(
opt32
)
return
Executable::BITS_32
;
54
if
(
opt64
)
return
Executable::BITS_64
;
55
56
return
Executable::BITS_32
;
// DEFAULT
57
}
58
59
Executable::exe_arch
PECore::getHdrArch
()
const
60
{
61
if
(!this->
fHdr
) {
62
return
Executable::ARCH_UNKNOWN
;
63
}
64
if
(this->
fHdr
->Machine == M_I386 || this->fHdr->Machine == M_AMD64) {
65
return
Executable::ARCH_INTEL
;
66
}
67
if
(this->
fHdr
->Machine == M_ARM || this->fHdr->Machine == M_ARM64LE) {
68
return
Executable::ARCH_ARM
;
69
}
70
return
Executable::ARCH_UNKNOWN
;
71
}
72
73
offset_t
PECore::peSignatureOffset
()
const
74
{
75
if
(!
dos
)
return
INVALID_ADDR
;
76
return
static_cast<
offset_t
>
(
dos
->e_lfanew);
77
}
78
79
offset_t
PECore::peFileHdrOffset
()
const
80
{
81
const
offset_t
offset =
peSignatureOffset
();
82
if
(offset ==
INVALID_ADDR
) {
83
return
INVALID_ADDR
;
84
}
85
const
offset_t
signSize =
sizeof
(DWORD);
86
return
offset + signSize;
87
}
88
89
offset_t
PECore::peOptHdrOffset
()
const
90
{
91
const
offset_t
offset =
peFileHdrOffset
();
92
if
(offset ==
INVALID_ADDR
) {
93
return
INVALID_ADDR
;
94
}
95
return
offset +
sizeof
(IMAGE_FILE_HEADER);
96
}
97
98
bufsize_t
PECore::peNtHeadersSize
()
const
99
{
100
if
(this->
getHdrBitMode
() ==
Executable::BITS_64
)
101
return
sizeof
(IMAGE_NT_HEADERS64);
102
103
return
sizeof
(IMAGE_NT_HEADERS32);
104
}
105
106
offset_t
PECore::secHdrsOffset
()
const
107
{
108
const
offset_t
offset =
peOptHdrOffset
();
109
if
(offset ==
INVALID_ADDR
) {
110
return
INVALID_ADDR
;
111
}
112
if
(!
fHdr
) {
113
return
INVALID_ADDR
;
114
}
115
const
offset_t
size =
static_cast<
offset_t
>
(this->
fHdr
->SizeOfOptionalHeader);
116
return
offset + size;
117
}
118
119
bufsize_t
PECore::getAlignment
(
Executable::addr_type
aType)
const
120
{
121
if
(this->
opt32
) {
122
if
(aType ==
Executable::RAW
)
return
opt32
->FileAlignment;
123
return
opt32
->SectionAlignment;
124
}
125
if
(this->
opt64
) {
126
if
(aType ==
Executable::RAW
)
return
opt64
->FileAlignment;
127
return
opt64
->SectionAlignment;
128
}
129
return
0;
130
}
131
132
bufsize_t
PECore::getImageSize
()
133
{
134
bufsize_t
imgSize = 0;
135
if
(this->
opt32
) {
136
imgSize =
opt32
->SizeOfImage;
137
}
138
if
(this->
opt64
) {
139
imgSize =
opt64
->SizeOfImage;
140
}
141
return
imgSize;
142
}
143
144
bufsize_t
PECore::hdrsSize
()
const
145
{
146
bufsize_t
hdrsSize
= 0;
147
if
(this->
opt32
) {
148
hdrsSize
=
opt32
->SizeOfHeaders;
149
}
150
if
(this->
opt64
) {
151
hdrsSize
=
opt64
->SizeOfHeaders;
152
}
153
return
hdrsSize
;
154
}
155
156
offset_t
PECore::getImageBase
(
bool
recalculate)
157
{
158
offset_t
imgBase = 0;
159
if
(this->
opt32
) {
160
imgBase =
opt32
->ImageBase;
161
}
162
if
(this->
opt64
) {
163
imgBase =
opt64
->ImageBase;
164
}
165
//can be null, under XP. In this case, the binary will be relocated to 10000h
166
//(quote: http://code.google.com/p/corkami/wiki/PE)
167
if
(imgBase == 0 && recalculate) {
168
imgBase =
DEFAULT_IMGBASE
;
169
}
170
//in 32 bit PEs: it can be any value as long as ImageBase + 'SizeOfImage' < 80000000h
171
//if the ImageBase is bigger than that, the binary will be relocated to 10000h
172
if
(this->
opt32
) {
173
offset_t
maxOffset = this->
getImageSize
() + imgBase;
174
if
(maxOffset >= 0x80000000 && recalculate) {
175
imgBase =
DEFAULT_IMGBASE
;
176
}
177
}
178
return
imgBase;
179
}
180
INVALID_ADDR
const offset_t INVALID_ADDR
Definition
AbstractByteBuffer.h:21
offset_t
uint64_t offset_t
Definition
AbstractByteBuffer.h:20
bufsize_t
size_t bufsize_t
Definition
AbstractByteBuffer.h:17
DEFAULT_IMGBASE
#define DEFAULT_IMGBASE
Definition
PECore.cpp:3
PECore.h
AbstractByteBuffer
Definition
AbstractByteBuffer.h:36
ExeException
Definition
Executable.h:9
Executable::addr_type
addr_type
Definition
Executable.h:42
Executable::RAW
@ RAW
Definition
Executable.h:44
Executable::exe_bits
exe_bits
Definition
Executable.h:28
Executable::BITS_32
@ BITS_32
Definition
Executable.h:31
Executable::BITS_64
@ BITS_64
Definition
Executable.h:32
Executable::exe_arch
exe_arch
Definition
Executable.h:35
Executable::ARCH_UNKNOWN
@ ARCH_UNKNOWN
Definition
Executable.h:36
Executable::ARCH_ARM
@ ARCH_ARM
Definition
Executable.h:38
Executable::ARCH_INTEL
@ ARCH_INTEL
Definition
Executable.h:37
PECore::getHdrArch
Executable::exe_arch getHdrArch() const
Definition
PECore.cpp:59
PECore::getAlignment
virtual bufsize_t getAlignment(Executable::addr_type aType) const
Definition
PECore.cpp:119
PECore::hdrsSize
bufsize_t hdrsSize() const
Definition
PECore.cpp:144
PECore::peOptHdrOffset
offset_t peOptHdrOffset() const
Definition
PECore.cpp:89
PECore::opt32
IMAGE_OPTIONAL_HEADER32 * opt32
Definition
PECore.h:57
PECore::wrap
bool wrap(AbstractByteBuffer *v_buf)
Definition
PECore.cpp:13
PECore::getImageBase
virtual offset_t getImageBase(bool recalculate=false)
Definition
PECore.cpp:156
PECore::peFileHdrOffset
offset_t peFileHdrOffset() const
Definition
PECore.cpp:79
PECore::buf
AbstractByteBuffer * buf
Definition
PECore.h:53
PECore::reset
void reset()
Definition
PECore.cpp:5
PECore::getImageSize
virtual bufsize_t getImageSize()
Definition
PECore.cpp:132
PECore::peSignatureOffset
offset_t peSignatureOffset() const
Definition
PECore.cpp:73
PECore::fHdr
IMAGE_FILE_HEADER * fHdr
Definition
PECore.h:56
PECore::peNtHeadersSize
bufsize_t peNtHeadersSize() const
Definition
PECore.cpp:98
PECore::secHdrsOffset
offset_t secHdrsOffset() const
Definition
PECore.cpp:106
PECore::opt64
IMAGE_OPTIONAL_HEADER64 * opt64
Definition
PECore.h:58
PECore::getHdrBitMode
Executable::exe_bits getHdrBitMode() const
Definition
PECore.cpp:51
PECore::dos
IMAGE_DOS_HEADER * dos
Definition
PECore.h:55
Generated by
1.17.0