libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
load_config_util.cpp
Go to the documentation of this file.
3
4BYTE* peconv::get_load_config_ptr(BYTE* buffer, size_t buf_size)
5{
6 if (!buffer || !buf_size) return nullptr;
7 IMAGE_DATA_DIRECTORY* dir = peconv::get_directory_entry(buffer, IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
8 if (!dir) {
9 return 0;
10 }
11 DWORD entry_rva = dir->VirtualAddress;
12 DWORD entry_size = dir->Size;
13 if (!peconv::validate_ptr(buffer, buf_size, buffer + entry_rva, entry_size)) {
14 return 0;
15 }
16 IMAGE_LOAD_CONFIG_DIRECTORY32* ldc = reinterpret_cast<IMAGE_LOAD_CONFIG_DIRECTORY32*>((ULONG_PTR)buffer + entry_rva);
17 return reinterpret_cast<BYTE*>(ldc);
18}
19
20peconv::t_load_config_ver peconv::get_load_config_version(BYTE* buffer, size_t buf_size, void* ld_config_ptr)
21{
22 if (!buffer || !buf_size || !ld_config_ptr) {
24 }
25 if (!peconv::validate_ptr(buffer, buf_size, ld_config_ptr, sizeof(DWORD))) {
27 }
28
29 DWORD* size_ptr = static_cast<DWORD*>(ld_config_ptr);
30 const size_t curr_size = (*size_ptr);
31 if (curr_size == 0) {
33 }
34 if (!peconv::validate_ptr(buffer, buf_size, ld_config_ptr, curr_size)) {
36 }
37 const bool is64b = peconv::is64bit(buffer);
38 if (is64b) {
39 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR64_W10)) {
41 }
42 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR64_W8)) {
44 }
45 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR64_W7)) {
47 }
48 }
49 else {
50 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR32_W10)) {
52 }
53 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR32_W8)) {
55 }
56 if (curr_size >= sizeof(peconv::IMAGE_LOAD_CONFIG_DIR32_W7)) {
58 }
59 }
61}
Fetching Load Config Directory and recognizing its version.
BYTE * get_load_config_ptr(BYTE *buffer, size_t buf_size)
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
Definition: buffer_util.cpp:9
bool is64bit(IN const BYTE *pe_buffer)
t_load_config_ver get_load_config_version(BYTE *buffer, size_t buf_size, void *ld_config_ptr)
IMAGE_DATA_DIRECTORY * get_directory_entry(IN const BYTE *pe_buffer, IN DWORD dir_id, IN bool allow_empty=false)
@ LOAD_CONFIG_NONE
@ LOAD_CONFIG_UNK_VER
@ LOAD_CONFIG_W8_VER
@ LOAD_CONFIG_W7_VER
@ LOAD_CONFIG_W10_VER
Wrappers over various fields in the PE header. Read, write, parse PE headers.