9 std::cerr <<
"[-] Module not initialized" << std::endl;
13 std::cerr <<
"[-] Failed to read the module header" << std::endl;
19 BYTE hdr_buffer1[peconv::MAX_HEADER_SIZE] = { 0 };
21 my_report->
is64 = peconv::is64bit(hdr_buffer1);
26 size_t hdrs_size = peconv::get_hdrs_size(hdr_buffer1);
27 if (hdrs_size > peconv::MAX_HEADER_SIZE) {
28 hdrs_size = peconv::MAX_HEADER_SIZE;
31 BYTE hdr_buffer2[peconv::MAX_HEADER_SIZE] = { 0 };
35 const DWORD ep1 = peconv::get_entry_point_rva(hdr_buffer1);
36 const DWORD ep2 = peconv::get_entry_point_rva(hdr_buffer2);
40 const DWORD arch1 = peconv::get_nt_hdr_architecture(hdr_buffer1);
41 const DWORD arch2 = peconv::get_nt_hdr_architecture(hdr_buffer2);
49 peconv::update_image_base(hdr_buffer1, 0);
50 peconv::update_image_base(hdr_buffer2, 0);
52 zeroUnusedFields(hdr_buffer1, hdrs_size);
53 zeroUnusedFields(hdr_buffer2, hdrs_size);
56 if (memcmp(hdr_buffer1, hdr_buffer2, hdrs_size) == 0) {
61 my_report->
dosHdrModified = isDosHdrModified(hdr_buffer1, hdr_buffer2, hdrs_size);
62 my_report->
fileHdrModified = isFileHdrModified(hdr_buffer1, hdr_buffer2, hdrs_size);
63 my_report->
ntHdrModified = isNtHdrModified(hdr_buffer1, hdr_buffer2, hdrs_size);
64 my_report->
secHdrModified = isSecHdrModified(hdr_buffer1, hdr_buffer2, hdrs_size);
67 const bool dotNetFileHdrModif = isFileHdrModified(hdr_buffer1, hdr_buffer2, hdrs_size, my_report->
archMismatch);
69 std::cout <<
"[#] .NET module detected as SUSPICIOUS\n";
73 && !dotNetFileHdrModif
79 std::cout <<
"[#] Filtered out modifications typical for .NET files, setting as not suspicious\n";
89bool pesieve::HeadersScanner::zeroUnusedFields(PBYTE hdr_buffer,
size_t hdrs_size)
91 bool is_modified =
false;
92 const size_t section_num = peconv::get_sections_count(hdr_buffer, hdrs_size);
94 for (
size_t i = 0; i < section_num; i++) {
95 PIMAGE_SECTION_HEADER sec_hdr = peconv::get_section_hdr(hdr_buffer, hdrs_size, i);
96 if (sec_hdr ==
nullptr)
continue;
98 if (sec_hdr->SizeOfRawData == 0) {
99 sec_hdr->PointerToRawData = 0;
106bool pesieve::HeadersScanner::isDosHdrModified(
const PBYTE hdr_buffer1,
const PBYTE hdr_buffer2,
const size_t hdrs_size)
108 if (hdrs_size <
sizeof(IMAGE_DOS_HEADER)) {
111 IMAGE_DOS_HEADER* hdr1 = (IMAGE_DOS_HEADER*)hdr_buffer1;
112 IMAGE_DOS_HEADER* hdr2 = (IMAGE_DOS_HEADER*)hdr_buffer2;
113 if (memcmp(hdr1, hdr2,
sizeof(IMAGE_DOS_HEADER)) != 0) {
117 LONG new_hdr = hdr2->e_lfanew;
118 if (memcmp(hdr1, hdr2, new_hdr) != 0) {
124bool pesieve::HeadersScanner::isSecHdrModified(
const PBYTE hdr_buffer1,
const PBYTE hdr_buffer2,
const size_t hdrs_size)
126 size_t section_num1 = peconv::get_sections_count(hdr_buffer1, hdrs_size);
127 size_t section_num2 = peconv::get_sections_count(hdr_buffer2, hdrs_size);
128 if (section_num1 != section_num2) {
132 for (
size_t i = 0; i < section_num1; i++) {
133 PIMAGE_SECTION_HEADER sec_hdr1 = peconv::get_section_hdr(hdr_buffer1, hdrs_size, i);
134 PIMAGE_SECTION_HEADER sec_hdr2 = peconv::get_section_hdr(hdr_buffer2, hdrs_size, i);
135 if (!sec_hdr1 && !sec_hdr2) {
138 else if (!sec_hdr1 || !sec_hdr2) {
142 if (sec_hdr1->VirtualAddress != sec_hdr2->VirtualAddress) {
145 if (sec_hdr1->Misc.VirtualSize != sec_hdr2->Misc.VirtualSize) {
148 if (sec_hdr1->PointerToRawData != sec_hdr2->PointerToRawData) {
155bool pesieve::HeadersScanner::isFileHdrModified(
const PBYTE hdr_buffer1,
const PBYTE hdr_buffer2,
const size_t hdrs_size,
bool mask_arch_mismatch)
157 const IMAGE_FILE_HEADER *file_hdr1 = peconv::get_file_hdr(hdr_buffer1, hdrs_size);
158 const IMAGE_FILE_HEADER *file_hdr2 = peconv::get_file_hdr(hdr_buffer2, hdrs_size);
160 if (!file_hdr1 && !file_hdr2)
return false;
161 if (!file_hdr1 || !file_hdr2)
return true;
163 if (memcmp(file_hdr1, file_hdr2,
sizeof(IMAGE_FILE_HEADER)) == 0) {
166 if (mask_arch_mismatch) {
167 if (file_hdr1->Machine == file_hdr2->Machine
168 && file_hdr1->Characteristics == file_hdr2->Characteristics
169 && file_hdr1->NumberOfSections == file_hdr2->NumberOfSections
170 && file_hdr1->TimeDateStamp == file_hdr2->TimeDateStamp
171 && file_hdr1->SizeOfOptionalHeader != file_hdr2->SizeOfOptionalHeader)
180bool pesieve::HeadersScanner::isNtHdrModified(
const PBYTE hdr_buffer1,
const PBYTE hdr_buffer2,
const size_t hdrs_size)
182 const bool is64 = peconv::is64bit(hdr_buffer1);
183 if (peconv::is64bit(hdr_buffer2) != is64) {
186 const BYTE *nt1 = peconv::get_nt_hdrs(hdr_buffer1, hdrs_size);
187 const BYTE *nt2 = peconv::get_nt_hdrs(hdr_buffer2, hdrs_size);
188 if (!nt1 && !nt2)
return false;
189 if (!nt1 || !nt2)
return true;
191 const size_t nt_hdr_size = is64 ?
sizeof(IMAGE_NT_HEADERS64) : sizeof(IMAGE_NT_HEADERS32);
192 if (memcmp(nt1, nt2, nt_hdr_size) == 0) {
ULONGLONG getHdrImageBase()
RemoteModuleData & remoteModData
ULONGLONG getHdrImageBase()
BYTE headerBuffer[peconv::MAX_HEADER_SIZE]