Page Menu
Home
desp's stash
Search
Configure Global Search
Log In
Files
F554806
PeParser.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
23 KB
Subscribers
None
PeParser.cpp
View Options
#include
"PeParser.h"
#include
"ProcessAccessHelp.h"
#include
<algorithm>
PeParser
::
PeParser
()
{
initClass
();
}
PeParser
::
PeParser
(
const
WCHAR
*
file
,
bool
readSectionHeaders
)
{
initClass
();
filename
=
file
;
if
(
filename
&&
wcslen
(
filename
)
>
3
)
{
readPeHeaderFromFile
(
readSectionHeaders
);
if
(
readSectionHeaders
)
{
if
(
isValidPeFile
())
{
getSectionHeaders
();
}
}
}
}
PeParser
::
PeParser
(
const
DWORD_PTR
moduleBase
,
bool
readSectionHeaders
)
{
initClass
();
moduleBaseAddress
=
moduleBase
;
if
(
moduleBaseAddress
)
{
readPeHeaderFromProcess
(
readSectionHeaders
);
if
(
readSectionHeaders
)
{
if
(
isValidPeFile
())
{
getSectionHeaders
();
}
}
}
}
PeParser
::~
PeParser
()
{
if
(
headerMemory
)
{
delete
[]
headerMemory
;
}
if
(
fileMemory
)
{
delete
[]
fileMemory
;
}
for
(
size_t
i
=
0
;
i
<
listPeSection
.
size
();
i
++
)
{
if
(
listPeSection
[
i
].
data
)
{
delete
[]
listPeSection
[
i
].
data
;
}
}
listPeSection
.
clear
();
}
void
PeParser
::
initClass
()
{
fileMemory
=
0
;
headerMemory
=
0
;
pDosHeader
=
0
;
pDosStub
=
0
;
dosStubSize
=
0
;
pNTHeader32
=
0
;
pNTHeader64
=
0
;
overlayData
=
0
;
overlaySize
=
0
;
filename
=
0
;
fileSize
=
0
;
moduleBaseAddress
=
0
;
hFile
=
INVALID_HANDLE_VALUE
;
}
bool
PeParser
::
isPE64
()
{
if
(
isValidPeFile
())
{
return
(
pNTHeader32
->
OptionalHeader
.
Magic
==
IMAGE_NT_OPTIONAL_HDR64_MAGIC
);
}
else
{
return
false
;
}
}
bool
PeParser
::
isPE32
()
{
if
(
isValidPeFile
())
{
return
(
pNTHeader32
->
OptionalHeader
.
Magic
==
IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
}
else
{
return
false
;
}
}
bool
PeParser
::
isTargetFileSamePeFormat
()
{
#ifdef _WIN64
return
isPE64
();
#else
return
isPE32
();
#endif
}
bool
PeParser
::
isValidPeFile
()
{
bool
retValue
=
false
;
if
(
pDosHeader
)
{
if
(
pDosHeader
->
e_magic
==
IMAGE_DOS_SIGNATURE
)
{
if
(
pNTHeader32
)
{
if
(
pNTHeader32
->
Signature
==
IMAGE_NT_SIGNATURE
)
{
retValue
=
true
;
}
}
}
}
return
retValue
;
}
bool
PeParser
::
hasDirectory
(
const
int
directoryIndex
)
{
if
(
isPE32
())
{
return
(
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
directoryIndex
].
VirtualAddress
!=
0
);
}
else
if
(
isPE64
())
{
return
(
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
directoryIndex
].
VirtualAddress
!=
0
);
}
else
{
return
false
;
}
}
bool
PeParser
::
hasExportDirectory
()
{
return
hasDirectory
(
IMAGE_DIRECTORY_ENTRY_EXPORT
);
}
bool
PeParser
::
hasTLSDirectory
()
{
return
hasDirectory
(
IMAGE_DIRECTORY_ENTRY_TLS
);
}
bool
PeParser
::
hasRelocationDirectory
()
{
return
hasDirectory
(
IMAGE_DIRECTORY_ENTRY_BASERELOC
);
}
DWORD
PeParser
::
getEntryPoint
()
{
if
(
isPE32
())
{
return
pNTHeader32
->
OptionalHeader
.
AddressOfEntryPoint
;
}
else
if
(
isPE64
())
{
return
pNTHeader64
->
OptionalHeader
.
AddressOfEntryPoint
;
}
else
{
return
0
;
}
}
bool
PeParser
::
readPeHeaderFromProcess
(
bool
readSectionHeaders
)
{
bool
retValue
=
false
;
DWORD
correctSize
=
0
;
DWORD
readSize
=
getInitialHeaderReadSize
(
readSectionHeaders
);
headerMemory
=
new
BYTE
[
readSize
];
if
(
ProcessAccessHelp
::
readMemoryPartlyFromProcess
(
moduleBaseAddress
,
readSize
,
headerMemory
))
{
retValue
=
true
;
getDosAndNtHeader
(
headerMemory
,
(
LONG
)
readSize
);
if
(
isValidPeFile
())
{
correctSize
=
calcCorrectPeHeaderSize
(
readSectionHeaders
);
if
(
readSize
<
correctSize
)
{
readSize
=
correctSize
;
delete
[]
headerMemory
;
headerMemory
=
new
BYTE
[
readSize
];
if
(
ProcessAccessHelp
::
readMemoryPartlyFromProcess
(
moduleBaseAddress
,
readSize
,
headerMemory
))
{
getDosAndNtHeader
(
headerMemory
,
(
LONG
)
readSize
);
}
}
}
}
return
retValue
;
}
bool
PeParser
::
readPeHeaderFromFile
(
bool
readSectionHeaders
)
{
bool
retValue
=
false
;
DWORD
correctSize
=
0
;
DWORD
numberOfBytesRead
=
0
;
DWORD
readSize
=
getInitialHeaderReadSize
(
readSectionHeaders
);
headerMemory
=
new
BYTE
[
readSize
];
if
(
openFileHandle
())
{
fileSize
=
(
DWORD
)
ProcessAccessHelp
::
getFileSize
(
hFile
);
if
(
ReadFile
(
hFile
,
headerMemory
,
readSize
,
&
numberOfBytesRead
,
0
))
{
retValue
=
true
;
getDosAndNtHeader
(
headerMemory
,
(
LONG
)
readSize
);
if
(
isValidPeFile
())
{
correctSize
=
calcCorrectPeHeaderSize
(
readSectionHeaders
);
if
(
readSize
<
correctSize
)
{
readSize
=
correctSize
;
if
(
fileSize
>
0
)
{
if
(
fileSize
<
correctSize
)
{
readSize
=
fileSize
;
}
}
delete
[]
headerMemory
;
headerMemory
=
new
BYTE
[
readSize
];
SetFilePointer
(
hFile
,
0
,
0
,
FILE_BEGIN
);
if
(
ReadFile
(
hFile
,
headerMemory
,
readSize
,
&
numberOfBytesRead
,
0
))
{
getDosAndNtHeader
(
headerMemory
,
(
LONG
)
readSize
);
}
}
}
}
closeFileHandle
();
}
return
retValue
;
}
bool
PeParser
::
readPeSectionsFromProcess
()
{
bool
retValue
=
true
;
DWORD_PTR
readOffset
=
0
;
listPeSection
.
reserve
(
getNumberOfSections
());
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
readOffset
=
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
+
moduleBaseAddress
;
listPeSection
[
i
].
normalSize
=
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
;
if
(
!
readSectionFromProcess
(
readOffset
,
listPeSection
[
i
]))
{
retValue
=
false
;
}
}
return
retValue
;
}
bool
PeParser
::
readPeSectionsFromFile
()
{
bool
retValue
=
true
;
DWORD
readOffset
=
0
;
listPeSection
.
reserve
(
getNumberOfSections
());
if
(
openFileHandle
())
{
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
readOffset
=
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
;
listPeSection
[
i
].
normalSize
=
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
;
if
(
!
readSectionFromFile
(
readOffset
,
listPeSection
[
i
]))
{
retValue
=
false
;
}
}
closeFileHandle
();
}
else
{
retValue
=
false
;
}
return
retValue
;
}
bool
PeParser
::
getSectionHeaders
()
{
PIMAGE_SECTION_HEADER
pSection
=
IMAGE_FIRST_SECTION
(
pNTHeader32
);
PeFileSection
peFileSection
;
listPeSection
.
clear
();
listPeSection
.
reserve
(
getNumberOfSections
());
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
memcpy_s
(
&
peFileSection
.
sectionHeader
,
sizeof
(
IMAGE_SECTION_HEADER
),
pSection
,
sizeof
(
IMAGE_SECTION_HEADER
));
listPeSection
.
push_back
(
peFileSection
);
pSection
++
;
}
return
true
;
}
bool
PeParser
::
getSectionNameUnicode
(
const
int
sectionIndex
,
WCHAR
*
output
,
const
int
outputLen
)
{
CHAR
sectionNameA
[
IMAGE_SIZEOF_SHORT_NAME
+
1
]
=
{
0
};
output
[
0
]
=
0
;
memcpy
(
sectionNameA
,
listPeSection
[
sectionIndex
].
sectionHeader
.
Name
,
IMAGE_SIZEOF_SHORT_NAME
);
//not null terminated
return
(
swprintf_s
(
output
,
outputLen
,
L
"%S"
,
sectionNameA
)
!=
-
1
);
}
WORD
PeParser
::
getNumberOfSections
()
{
return
pNTHeader32
->
FileHeader
.
NumberOfSections
;
}
void
PeParser
::
setNumberOfSections
(
WORD
numberOfSections
)
{
pNTHeader32
->
FileHeader
.
NumberOfSections
=
numberOfSections
;
}
std
::
vector
<
PeFileSection
>
&
PeParser
::
getSectionHeaderList
()
{
return
listPeSection
;
}
void
PeParser
::
getDosAndNtHeader
(
BYTE
*
memory
,
LONG
size
)
{
pDosHeader
=
(
PIMAGE_DOS_HEADER
)
memory
;
pNTHeader32
=
0
;
pNTHeader64
=
0
;
dosStubSize
=
0
;
pDosStub
=
0
;
if
(
pDosHeader
->
e_lfanew
>
0
&&
pDosHeader
->
e_lfanew
<
size
)
//malformed PE
{
pNTHeader32
=
(
PIMAGE_NT_HEADERS32
)((
DWORD_PTR
)
pDosHeader
+
pDosHeader
->
e_lfanew
);
pNTHeader64
=
(
PIMAGE_NT_HEADERS64
)((
DWORD_PTR
)
pDosHeader
+
pDosHeader
->
e_lfanew
);
if
(
pDosHeader
->
e_lfanew
>
sizeof
(
IMAGE_DOS_HEADER
))
{
dosStubSize
=
pDosHeader
->
e_lfanew
-
sizeof
(
IMAGE_DOS_HEADER
);
pDosStub
=
(
BYTE
*
)((
DWORD_PTR
)
pDosHeader
+
sizeof
(
IMAGE_DOS_HEADER
));
}
}
}
DWORD
PeParser
::
calcCorrectPeHeaderSize
(
bool
readSectionHeaders
)
{
DWORD
correctSize
=
pDosHeader
->
e_lfanew
+
50
;
//extra buffer
if
(
readSectionHeaders
)
{
correctSize
+=
getNumberOfSections
()
*
sizeof
(
IMAGE_SECTION_HEADER
);
}
if
(
isPE32
())
{
correctSize
+=
sizeof
(
IMAGE_NT_HEADERS32
);
}
else
if
(
isPE64
())
{
correctSize
+=
sizeof
(
IMAGE_NT_HEADERS64
);
}
else
{
correctSize
=
0
;
//not a valid PE
}
return
correctSize
;
}
DWORD
PeParser
::
getInitialHeaderReadSize
(
bool
readSectionHeaders
)
{
DWORD
readSize
=
sizeof
(
IMAGE_DOS_HEADER
)
+
200
+
sizeof
(
IMAGE_NT_HEADERS64
);
if
(
readSectionHeaders
)
{
readSize
+=
(
10
*
sizeof
(
IMAGE_SECTION_HEADER
));
}
return
readSize
;
}
DWORD
PeParser
::
getSectionHeaderBasedFileSize
()
{
DWORD
lastRawOffset
=
0
,
lastRawSize
=
0
;
//this is needed if the sections aren't sorted by their RawOffset (e.g. Petite)
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
(
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
>
lastRawOffset
)
{
lastRawOffset
=
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
;
lastRawSize
=
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
;
}
}
return
(
lastRawSize
+
lastRawOffset
);
}
DWORD
PeParser
::
getSectionHeaderBasedSizeOfImage
()
{
DWORD
lastVirtualOffset
=
0
,
lastVirtualSize
=
0
;
//this is needed if the sections aren't sorted by their RawOffset (e.g. Petite)
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
(
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
>
lastVirtualOffset
)
{
lastVirtualOffset
=
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
;
lastVirtualSize
=
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
;
}
}
return
(
lastVirtualSize
+
lastVirtualOffset
);
}
bool
PeParser
::
openFileHandle
()
{
if
(
hFile
==
INVALID_HANDLE_VALUE
)
{
if
(
filename
)
{
hFile
=
CreateFile
(
filename
,
GENERIC_READ
,
FILE_SHARE_READ
,
0
,
OPEN_EXISTING
,
0
,
0
);
}
else
{
hFile
=
INVALID_HANDLE_VALUE
;
}
}
return
(
hFile
!=
INVALID_HANDLE_VALUE
);
}
bool
PeParser
::
openWriteFileHandle
(
const
WCHAR
*
newFile
)
{
if
(
newFile
)
{
hFile
=
CreateFile
(
newFile
,
GENERIC_WRITE
,
FILE_SHARE_WRITE
,
0
,
CREATE_ALWAYS
,
FILE_ATTRIBUTE_NORMAL
,
0
);
}
else
{
hFile
=
INVALID_HANDLE_VALUE
;
}
return
(
hFile
!=
INVALID_HANDLE_VALUE
);
}
void
PeParser
::
closeFileHandle
()
{
if
(
hFile
!=
INVALID_HANDLE_VALUE
)
{
CloseHandle
(
hFile
);
hFile
=
INVALID_HANDLE_VALUE
;
}
}
bool
PeParser
::
readSectionFromFile
(
DWORD
readOffset
,
PeFileSection
&
peFileSection
)
{
const
DWORD
maxReadSize
=
100
;
BYTE
data
[
maxReadSize
];
DWORD
bytesRead
=
0
;
bool
retValue
=
true
;
DWORD
valuesFound
=
0
;
DWORD
currentOffset
=
0
;
DWORD
readSize
=
0
;
peFileSection
.
data
=
0
;
peFileSection
.
dataSize
=
0
;
readSize
=
peFileSection
.
normalSize
;
if
(
!
readOffset
||
!
readSize
)
{
return
true
;
//section without data is valid
}
if
(
readSize
<=
maxReadSize
)
{
peFileSection
.
dataSize
=
readSize
;
peFileSection
.
normalSize
=
readSize
;
return
readPeSectionFromFile
(
readOffset
,
peFileSection
);
}
currentOffset
=
readOffset
+
readSize
-
maxReadSize
;
while
(
currentOffset
>=
readOffset
)
//start from the end
{
SetFilePointer
(
hFile
,
currentOffset
,
0
,
FILE_BEGIN
);
ZeroMemory
(
data
,
sizeof
(
data
));
if
(
!
ReadFile
(
hFile
,
data
,
sizeof
(
data
),
&
bytesRead
,
0
))
{
retValue
=
false
;
break
;
}
valuesFound
=
isMemoryNotNull
(
data
,
sizeof
(
data
));
if
(
valuesFound
)
{
//found some real code
currentOffset
+=
valuesFound
;
if
(
readOffset
<
currentOffset
)
{
//real size
peFileSection
.
dataSize
=
currentOffset
-
readOffset
;
}
break
;
}
currentOffset
-=
maxReadSize
;
}
if
(
peFileSection
.
dataSize
)
{
retValue
=
readPeSectionFromFile
(
readOffset
,
peFileSection
);
}
return
retValue
;
}
DWORD
PeParser
::
isMemoryNotNull
(
BYTE
*
data
,
int
dataSize
)
{
for
(
int
i
=
(
dataSize
-
1
);
i
>=
0
;
i
--
)
{
if
(
data
[
i
]
!=
0
)
{
return
i
+
1
;
}
}
return
0
;
}
bool
PeParser
::
savePeFileToDisk
(
const
WCHAR
*
newFile
)
{
bool
retValue
=
true
;
DWORD
dwFileOffset
=
0
,
dwWriteSize
=
0
;
if
(
getNumberOfSections
()
!=
listPeSection
.
size
())
{
return
false
;
}
if
(
openWriteFileHandle
(
newFile
))
{
//Dos header
dwWriteSize
=
sizeof
(
IMAGE_DOS_HEADER
);
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
,
pDosHeader
))
{
retValue
=
false
;
}
dwFileOffset
+=
dwWriteSize
;
if
(
dosStubSize
&&
pDosStub
)
{
//Dos Stub
dwWriteSize
=
dosStubSize
;
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
,
pDosStub
))
{
retValue
=
false
;
}
dwFileOffset
+=
dwWriteSize
;
}
//Pe Header
if
(
isPE32
())
{
dwWriteSize
=
sizeof
(
IMAGE_NT_HEADERS32
);
}
else
{
dwWriteSize
=
sizeof
(
IMAGE_NT_HEADERS64
);
}
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
,
pNTHeader32
))
{
retValue
=
false
;
}
dwFileOffset
+=
dwWriteSize
;
//section headers
dwWriteSize
=
sizeof
(
IMAGE_SECTION_HEADER
);
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
,
&
listPeSection
[
i
].
sectionHeader
))
{
retValue
=
false
;
break
;
}
dwFileOffset
+=
dwWriteSize
;
}
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
(
!
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
)
continue
;
if
(
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
>
dwFileOffset
)
{
dwWriteSize
=
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
-
dwFileOffset
;
//padding
if
(
!
writeZeroMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
))
{
retValue
=
false
;
break
;
}
dwFileOffset
+=
dwWriteSize
;
}
dwWriteSize
=
listPeSection
[
i
].
dataSize
;
if
(
dwWriteSize
)
{
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
,
dwWriteSize
,
listPeSection
[
i
].
data
))
{
retValue
=
false
;
break
;
}
dwFileOffset
+=
dwWriteSize
;
if
(
listPeSection
[
i
].
dataSize
<
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
)
//padding
{
dwWriteSize
=
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
-
listPeSection
[
i
].
dataSize
;
if
(
!
writeZeroMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
))
{
retValue
=
false
;
break
;
}
dwFileOffset
+=
dwWriteSize
;
}
}
}
//add overlay?
if
(
overlaySize
&&
overlayData
)
{
dwWriteSize
=
overlaySize
;
if
(
!
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
dwFileOffset
,
dwWriteSize
,
overlayData
))
{
retValue
=
false
;
}
dwFileOffset
+=
dwWriteSize
;
}
SetEndOfFile
(
hFile
);
closeFileHandle
();
}
return
retValue
;
}
bool
PeParser
::
writeZeroMemoryToFile
(
HANDLE
hFile
,
DWORD
fileOffset
,
DWORD
size
)
{
bool
retValue
=
false
;
PVOID
zeromemory
=
calloc
(
size
,
1
);
if
(
zeromemory
)
{
retValue
=
ProcessAccessHelp
::
writeMemoryToFile
(
hFile
,
fileOffset
,
size
,
zeromemory
);
free
(
zeromemory
);
}
return
retValue
;
}
void
PeParser
::
removeDosStub
()
{
if
(
pDosHeader
)
{
dosStubSize
=
0
;
pDosStub
=
0
;
//must not delete []
pDosHeader
->
e_lfanew
=
sizeof
(
IMAGE_DOS_HEADER
);
}
}
bool
PeParser
::
readPeSectionFromFile
(
DWORD
readOffset
,
PeFileSection
&
peFileSection
)
{
DWORD
bytesRead
=
0
;
peFileSection
.
data
=
new
BYTE
[
peFileSection
.
dataSize
];
SetFilePointer
(
hFile
,
readOffset
,
0
,
FILE_BEGIN
);
return
(
ReadFile
(
hFile
,
peFileSection
.
data
,
peFileSection
.
dataSize
,
&
bytesRead
,
0
)
!=
FALSE
);
}
bool
PeParser
::
readSectionFromProcess
(
DWORD_PTR
readOffset
,
PeFileSection
&
peFileSection
)
{
const
DWORD
maxReadSize
=
100
;
BYTE
data
[
maxReadSize
];
bool
retValue
=
true
;
DWORD
valuesFound
=
0
;
DWORD
readSize
=
0
;
DWORD_PTR
currentOffset
=
0
;
peFileSection
.
data
=
0
;
peFileSection
.
dataSize
=
0
;
readSize
=
peFileSection
.
normalSize
;
if
(
!
readOffset
||
!
readSize
)
{
return
true
;
//section without data is valid
}
if
(
readSize
<=
maxReadSize
)
{
peFileSection
.
dataSize
=
readSize
;
peFileSection
.
normalSize
=
readSize
;
return
readPeSectionFromProcess
(
readOffset
,
peFileSection
);
}
currentOffset
=
readOffset
+
readSize
-
maxReadSize
;
while
(
currentOffset
>=
readOffset
)
//start from the end
{
if
(
!
ProcessAccessHelp
::
readMemoryPartlyFromProcess
(
currentOffset
,
sizeof
(
data
),
data
))
{
retValue
=
false
;
break
;
}
valuesFound
=
isMemoryNotNull
(
data
,
sizeof
(
data
));
if
(
valuesFound
)
{
//found some real code
currentOffset
+=
valuesFound
;
if
(
readOffset
<
currentOffset
)
{
//real size
peFileSection
.
dataSize
=
(
DWORD
)(
currentOffset
-
readOffset
);
}
break
;
}
currentOffset
-=
maxReadSize
;
}
if
(
peFileSection
.
dataSize
)
{
retValue
=
readPeSectionFromProcess
(
readOffset
,
peFileSection
);
}
return
retValue
;
}
bool
PeParser
::
readPeSectionFromProcess
(
DWORD_PTR
readOffset
,
PeFileSection
&
peFileSection
)
{
peFileSection
.
data
=
new
BYTE
[
peFileSection
.
dataSize
];
return
ProcessAccessHelp
::
readMemoryPartlyFromProcess
(
readOffset
,
peFileSection
.
dataSize
,
peFileSection
.
data
);
}
DWORD
PeParser
::
alignValue
(
DWORD
badValue
,
DWORD
alignTo
)
{
return
(((
badValue
+
alignTo
-
1
)
/
alignTo
)
*
alignTo
);
}
bool
PeParser
::
addNewLastSection
(
const
CHAR
*
sectionName
,
DWORD
sectionSize
,
BYTE
*
sectionData
)
{
size_t
nameLength
=
strlen
(
sectionName
);
DWORD
fileAlignment
=
0
,
sectionAlignment
=
0
;
PeFileSection
peFileSection
;
if
(
nameLength
>
IMAGE_SIZEOF_SHORT_NAME
)
{
return
false
;
}
if
(
isPE32
())
{
fileAlignment
=
pNTHeader32
->
OptionalHeader
.
FileAlignment
;
sectionAlignment
=
pNTHeader32
->
OptionalHeader
.
SectionAlignment
;
}
else
{
fileAlignment
=
pNTHeader64
->
OptionalHeader
.
FileAlignment
;
sectionAlignment
=
pNTHeader64
->
OptionalHeader
.
SectionAlignment
;
}
memcpy_s
(
peFileSection
.
sectionHeader
.
Name
,
IMAGE_SIZEOF_SHORT_NAME
,
sectionName
,
nameLength
);
//last section doesn't need SizeOfRawData alignment
peFileSection
.
sectionHeader
.
SizeOfRawData
=
sectionSize
;
//alignValue(sectionSize, fileAlignment);
peFileSection
.
sectionHeader
.
Misc
.
VirtualSize
=
alignValue
(
sectionSize
,
sectionAlignment
);
peFileSection
.
sectionHeader
.
PointerToRawData
=
alignValue
(
getSectionHeaderBasedFileSize
(),
fileAlignment
);
peFileSection
.
sectionHeader
.
VirtualAddress
=
alignValue
(
getSectionHeaderBasedSizeOfImage
(),
sectionAlignment
);
peFileSection
.
sectionHeader
.
Characteristics
=
IMAGE_SCN_MEM_EXECUTE
|
IMAGE_SCN_MEM_READ
|
IMAGE_SCN_MEM_WRITE
|
IMAGE_SCN_CNT_CODE
|
IMAGE_SCN_CNT_INITIALIZED_DATA
;
peFileSection
.
normalSize
=
peFileSection
.
sectionHeader
.
SizeOfRawData
;
peFileSection
.
dataSize
=
peFileSection
.
sectionHeader
.
SizeOfRawData
;
if
(
sectionData
==
0
)
{
peFileSection
.
data
=
new
BYTE
[
peFileSection
.
sectionHeader
.
SizeOfRawData
];
ZeroMemory
(
peFileSection
.
data
,
peFileSection
.
sectionHeader
.
SizeOfRawData
);
}
else
{
peFileSection
.
data
=
sectionData
;
}
listPeSection
.
push_back
(
peFileSection
);
setNumberOfSections
(
getNumberOfSections
()
+
1
);
return
true
;
}
DWORD_PTR
PeParser
::
convertRVAToOffsetVector
(
DWORD_PTR
dwRVA
)
{
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
((
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
<=
dwRVA
)
&&
((
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
+
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
)
>
dwRVA
))
{
return
((
dwRVA
-
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
)
+
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
);
}
}
return
0
;
}
DWORD_PTR
PeParser
::
convertOffsetToRVAVector
(
DWORD_PTR
dwOffset
)
{
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
((
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
<=
dwOffset
)
&&
((
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
+
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
)
>
dwOffset
))
{
return
((
dwOffset
-
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
)
+
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
);
}
}
return
0
;
}
void
PeParser
::
fixPeHeader
()
{
DWORD
dwSize
=
pDosHeader
->
e_lfanew
+
sizeof
(
DWORD
)
+
sizeof
(
IMAGE_FILE_HEADER
);
if
(
isPE32
())
{
//delete bound import directories
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].
VirtualAddress
=
0
;
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].
Size
=
0
;
//max 16
if
(
pNTHeader64
->
OptionalHeader
.
NumberOfRvaAndSizes
>
0x10
)
{
pNTHeader64
->
OptionalHeader
.
NumberOfRvaAndSizes
=
0x10
;
}
pNTHeader32
->
OptionalHeader
.
SizeOfImage
=
getSectionHeaderBasedSizeOfImage
();
pNTHeader32
->
OptionalHeader
.
SizeOfHeaders
=
alignValue
(
dwSize
+
pNTHeader32
->
FileHeader
.
SizeOfOptionalHeader
+
(
getNumberOfSections
()
*
sizeof
(
IMAGE_SECTION_HEADER
)),
pNTHeader32
->
OptionalHeader
.
FileAlignment
);
}
else
{
//delete bound import directories
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].
VirtualAddress
=
0
;
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].
Size
=
0
;
//max 16
if
(
pNTHeader64
->
OptionalHeader
.
NumberOfRvaAndSizes
>
0x10
)
{
pNTHeader64
->
OptionalHeader
.
NumberOfRvaAndSizes
=
0x10
;
}
pNTHeader64
->
OptionalHeader
.
SizeOfImage
=
getSectionHeaderBasedSizeOfImage
();
pNTHeader64
->
OptionalHeader
.
SizeOfHeaders
=
alignValue
(
dwSize
+
pNTHeader64
->
FileHeader
.
SizeOfOptionalHeader
+
(
getNumberOfSections
()
*
sizeof
(
IMAGE_SECTION_HEADER
)),
pNTHeader64
->
OptionalHeader
.
FileAlignment
);
}
removeIatDirectory
();
}
void
PeParser
::
removeIatDirectory
()
{
DWORD
searchAddress
=
0
;
if
(
isPE32
())
{
searchAddress
=
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
VirtualAddress
;
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
VirtualAddress
=
0
;
pNTHeader32
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
Size
=
0
;
}
else
{
searchAddress
=
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
VirtualAddress
;
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
VirtualAddress
=
0
;
pNTHeader64
->
OptionalHeader
.
DataDirectory
[
IMAGE_DIRECTORY_ENTRY_IAT
].
Size
=
0
;
}
if
(
searchAddress
)
{
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
if
((
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
<=
searchAddress
)
&&
((
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
+
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
)
>
searchAddress
))
{
//section must be read and writable
listPeSection
[
i
].
sectionHeader
.
Characteristics
|=
IMAGE_SCN_MEM_READ
|
IMAGE_SCN_MEM_WRITE
;
}
}
}
}
void
PeParser
::
setDefaultFileAlignment
()
{
if
(
isPE32
())
{
pNTHeader32
->
OptionalHeader
.
FileAlignment
=
FileAlignmentConstant
;
}
else
{
pNTHeader64
->
OptionalHeader
.
FileAlignment
=
FileAlignmentConstant
;
}
}
bool
PeFileSectionSortByPointerToRawData
(
const
PeFileSection
&
d1
,
const
PeFileSection
&
d2
)
{
return
d1
.
sectionHeader
.
PointerToRawData
<
d2
.
sectionHeader
.
PointerToRawData
;
}
bool
PeFileSectionSortByVirtualAddress
(
const
PeFileSection
&
d1
,
const
PeFileSection
&
d2
)
{
return
d1
.
sectionHeader
.
VirtualAddress
<
d2
.
sectionHeader
.
VirtualAddress
;
}
void
PeParser
::
alignAllSectionHeaders
()
{
DWORD
sectionAlignment
=
0
;
DWORD
fileAlignment
=
0
;
DWORD
newFileSize
=
0
;
if
(
isPE32
())
{
sectionAlignment
=
pNTHeader32
->
OptionalHeader
.
SectionAlignment
;
fileAlignment
=
pNTHeader32
->
OptionalHeader
.
FileAlignment
;
}
else
{
sectionAlignment
=
pNTHeader64
->
OptionalHeader
.
SectionAlignment
;
fileAlignment
=
pNTHeader64
->
OptionalHeader
.
FileAlignment
;
}
std
::
sort
(
listPeSection
.
begin
(),
listPeSection
.
end
(),
PeFileSectionSortByPointerToRawData
);
//sort by PointerToRawData ascending
newFileSize
=
pDosHeader
->
e_lfanew
+
sizeof
(
DWORD
)
+
sizeof
(
IMAGE_FILE_HEADER
)
+
pNTHeader32
->
FileHeader
.
SizeOfOptionalHeader
+
(
getNumberOfSections
()
*
sizeof
(
IMAGE_SECTION_HEADER
));
for
(
WORD
i
=
0
;
i
<
getNumberOfSections
();
i
++
)
{
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
=
alignValue
(
listPeSection
[
i
].
sectionHeader
.
VirtualAddress
,
sectionAlignment
);
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
=
alignValue
(
listPeSection
[
i
].
sectionHeader
.
Misc
.
VirtualSize
,
sectionAlignment
);
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
=
alignValue
(
newFileSize
,
fileAlignment
);
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
=
alignValue
(
listPeSection
[
i
].
dataSize
,
fileAlignment
);
newFileSize
=
listPeSection
[
i
].
sectionHeader
.
PointerToRawData
+
listPeSection
[
i
].
sectionHeader
.
SizeOfRawData
;
}
std
::
sort
(
listPeSection
.
begin
(),
listPeSection
.
end
(),
PeFileSectionSortByVirtualAddress
);
//sort by VirtualAddress ascending
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Wed, Feb 11, 12:02 AM (1 d, 15 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
d6/a8/a3487624ae2fc727bac5ecaf6538
Attached To
rSCY Scylla
Event Timeline
Log In to Comment