libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
resource_parser.cpp
Go to the documentation of this file.
3
4#ifdef _DEBUG
5#include <iostream>
6#endif
7
8bool parse_resource_dir(BYTE* modulePtr, const size_t moduleSize,
9 IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir,
10 const IMAGE_RESOURCE_DIRECTORY *upper_dir,
11 IMAGE_RESOURCE_DIRECTORY* curr_dir,
13
14bool parse_resource_entry(BYTE* modulePtr, const size_t moduleSize,
15 IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir,
16 const IMAGE_RESOURCE_DIRECTORY *upper_dir,
17 IMAGE_RESOURCE_DIRECTORY_ENTRY* entry,
19{
20 if (!entry->DataIsDirectory) {
21#ifdef _DEBUG
22 std::cout << "Entry is NOT a directory\n";
23#endif
24 DWORD offset = entry->OffsetToData;
25#ifdef _DEBUG
26 std::cout << "Offset: " << offset << std::endl;
27#endif
28 IMAGE_RESOURCE_DATA_ENTRY *data_entry = (IMAGE_RESOURCE_DATA_ENTRY*)(offset + (ULONGLONG)upper_dir);
29 if (!peconv::validate_ptr(modulePtr, moduleSize, data_entry, sizeof(IMAGE_RESOURCE_DATA_ENTRY))) {
30 return false;
31 }
32#ifdef _DEBUG
33 std::cout << "Data Offset: " << data_entry->OffsetToData << " : " << data_entry->Size << std::endl;
34#endif
35 BYTE* data_ptr = (BYTE*)((ULONGLONG)modulePtr + data_entry->OffsetToData);
36 if (!peconv::validate_ptr(modulePtr, moduleSize, data_ptr, data_entry->Size)) {
37 return false;
38 }
39 on_entry(modulePtr, root_dir, data_entry);
40 return true;
41 }
42#ifdef _DEBUG
43 std::cout << "Entry is a directory\n";
44#endif
45 //else: it is a next level directory
46 DWORD offset = entry->OffsetToDirectory;
47#ifdef _DEBUG
48 std::cout << "Offset: " << offset << std::endl;
49#endif
50 IMAGE_RESOURCE_DIRECTORY *next_dir = (IMAGE_RESOURCE_DIRECTORY*)(offset + (ULONGLONG)upper_dir);
51 if (!peconv::validate_ptr(modulePtr, moduleSize, next_dir, sizeof(IMAGE_RESOURCE_DIRECTORY))) {
52 return false;
53 }
54 return parse_resource_dir(modulePtr, moduleSize, root_dir, upper_dir, next_dir, on_entry);
55}
56
57bool parse_resource_dir(BYTE* modulePtr, const size_t moduleSize,
58 IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir,
59 const IMAGE_RESOURCE_DIRECTORY *upper_dir,
60 IMAGE_RESOURCE_DIRECTORY* curr_dir,
62{
63 size_t total_entries = curr_dir->NumberOfIdEntries + curr_dir->NumberOfNamedEntries;
64 IMAGE_RESOURCE_DIRECTORY_ENTRY* first_entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((ULONGLONG)&curr_dir->NumberOfIdEntries + sizeof(WORD));
65 for (size_t i = 0; i < total_entries; i++) {
66 IMAGE_RESOURCE_DIRECTORY_ENTRY* entry = &first_entry[i];
67#ifdef _DEBUG
68 std::cout << "Entry:" << std::hex << i << " ; " << "Id: " << entry->Id << " ; dataOffset:" << entry->OffsetToData << "\n";
69#endif
70 if (root_dir == nullptr) {
71 root_dir = entry;
72 }
73 parse_resource_entry(modulePtr, moduleSize, root_dir, upper_dir, entry, on_entry);
74 }
75 return true;
76}
77
78bool peconv::parse_resources(BYTE* modulePtr, t_on_res_entry_found on_entry)
79{
80 const size_t module_size = peconv::get_image_size(modulePtr);
81 IMAGE_DATA_DIRECTORY *dir = peconv::get_directory_entry(modulePtr, IMAGE_DIRECTORY_ENTRY_RESOURCE);
82 if (!dir || dir->VirtualAddress == 0 || dir->Size == 0) {
83 return false;
84 }
85 IMAGE_RESOURCE_DIRECTORY *res_dir = (IMAGE_RESOURCE_DIRECTORY*)(dir->VirtualAddress + (ULONGLONG)modulePtr);
86 if (!peconv::validate_ptr(modulePtr, module_size, res_dir, sizeof(IMAGE_DEBUG_DIRECTORY))) {
87 return false;
88 }
89 return parse_resource_dir(modulePtr, module_size, nullptr, res_dir, res_dir, on_entry);
90}
bool parse_resources(BYTE *modulePtr, t_on_res_entry_found on_entry)
bool(* t_on_res_entry_found)(BYTE *modulePtr, IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir, IMAGE_RESOURCE_DATA_ENTRY *curr_entry)
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
DWORD get_image_size(IN const BYTE *payload)
IMAGE_DATA_DIRECTORY * get_directory_entry(IN const BYTE *pe_buffer, IN DWORD dir_id, IN bool allow_empty=false)
Wrappers over various fields in the PE header. Read, write, parse PE headers.
bool parse_resource_entry(BYTE *modulePtr, const size_t moduleSize, IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir, const IMAGE_RESOURCE_DIRECTORY *upper_dir, IMAGE_RESOURCE_DIRECTORY_ENTRY *entry, peconv::t_on_res_entry_found on_entry)
bool parse_resource_dir(BYTE *modulePtr, const size_t moduleSize, IMAGE_RESOURCE_DIRECTORY_ENTRY *root_dir, const IMAGE_RESOURCE_DIRECTORY *upper_dir, IMAGE_RESOURCE_DIRECTORY *curr_dir, peconv::t_on_res_entry_found on_entry)
Parsing PE's resource directory.