libPeConv
A library to load, manipulate, dump PE files.
pe_hdrs_helper.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <windows.h>
9#include "buffer_util.h"
10
11#ifndef PAGE_SIZE
12#define PAGE_SIZE 0x1000
13#endif
14
15#define MASK_TO_DWORD(val) (val & 0xffffffff)
16#define MASK_TO_WORD(val) (val & 0xffff)
17
18namespace peconv {
22 const ULONGLONG MAX_HEADER_SIZE = PAGE_SIZE;
23
27 DWORD get_image_size(IN const BYTE *payload);
28
32 bool update_image_size(IN OUT BYTE* payload, IN DWORD new_img_size);
33
37 WORD get_nt_hdr_architecture(IN const BYTE *pe_buffer);
38
42 bool is64bit(IN const BYTE *pe_buffer);
43
48 BYTE* get_nt_hdrs(
49 IN const BYTE *pe_buffer,
50 IN OPTIONAL size_t buffer_size=0 //if buffer_size=0 means size unknown
51 );
52
56 IMAGE_NT_HEADERS32* get_nt_hdrs32(IN const BYTE *pe_buffer);
57
61 IMAGE_NT_HEADERS64* get_nt_hdrs64(IN const BYTE *pe_buffer);
62
66 LPVOID get_optional_hdr(IN const BYTE* payload, IN const size_t buffer_size);
67
71 const IMAGE_FILE_HEADER* get_file_hdr(
72 IN const BYTE* payload,
73 IN const size_t buffer_size
74 );
75
79 DWORD get_hdrs_size(IN const BYTE *pe_buffer);
80
84 IMAGE_DATA_DIRECTORY* get_directory_entry(IN const BYTE* pe_buffer, IN DWORD dir_id, IN bool allow_empty = false);
85
89 template <typename IMAGE_TYPE_DIRECTORY>
90 IMAGE_TYPE_DIRECTORY* get_type_directory(IN HMODULE modulePtr, IN DWORD dir_id)
91 {
92 IMAGE_DATA_DIRECTORY *my_dir = peconv::get_directory_entry((const BYTE*)modulePtr, dir_id);
93 if (!my_dir) return nullptr;
94
95 DWORD dir_addr = my_dir->VirtualAddress;
96 if (dir_addr == 0) return nullptr;
97
98 return (IMAGE_TYPE_DIRECTORY*)(dir_addr + (ULONG_PTR)modulePtr);
99 }
100
104 IMAGE_EXPORT_DIRECTORY* get_export_directory(IN HMODULE modulePtr);
105
106 // Fetch Image Base from Optional Header.
107 ULONGLONG get_image_base(IN const BYTE *pe_buffer);
108
112 bool update_image_base(IN OUT BYTE* payload, IN ULONGLONG destImageBase);
113
117 DWORD get_entry_point_rva(IN const BYTE *pe_buffer);
118
122 bool update_entry_point_rva(IN OUT BYTE *pe_buffer, IN DWORD ep);
123
127 size_t get_sections_count(
128 IN const BYTE* buffer,
129 IN const size_t buffer_size
130 );
131
135 bool is_valid_sections_hdr_offset(IN const BYTE* buffer, IN const size_t buffer_size);
136
140 PIMAGE_SECTION_HEADER get_section_hdr(
141 IN const BYTE* pe_buffer,
142 IN const size_t buffer_size,
143 IN size_t section_num
144 );
145
149 WORD get_file_characteristics(IN const BYTE* payload);
150
154 bool is_module_dll(IN const BYTE* payload);
155
159 WORD get_dll_characteristics(IN const BYTE* payload);
160
164 bool set_subsystem(IN OUT BYTE* payload, IN WORD subsystem);
165
169 WORD get_subsystem(IN const BYTE* payload);
170
174 bool has_relocations(IN const BYTE *pe_buffer);
175
179 IMAGE_COR20_HEADER* get_dotnet_hdr(
180 IN const BYTE* pe_buffer,
181 IN size_t const buffer_size,
182 IN const IMAGE_DATA_DIRECTORY* dotNetDir
183 );
184
188 DWORD get_sec_alignment(IN const BYTE* modulePtr, IN bool is_raw);
189
193 bool set_sec_alignment(IN OUT BYTE* pe_buffer, IN bool is_raw, IN DWORD new_alignment);
194
199 IN const BYTE* pe_hdr,
200 IN const PIMAGE_SECTION_HEADER sec_hdr,
201 IN bool rounded //if set, it rounds it up to the Virtual Alignment
202 );
203
210 PIMAGE_SECTION_HEADER get_last_section(IN const PBYTE pe_buffer, IN size_t pe_size, IN bool is_raw);
211
218 DWORD calc_pe_size(
219 IN const PBYTE pe_buffer,
220 IN size_t pe_size,
221 IN bool is_raw
222 );
223
230 bool is_valid_sectons_alignment(IN const BYTE* buffer, IN const SIZE_T buffer_size, IN bool is_raw);
231
232}; // namespace peconv
Definitions of the used buffer types. Functions for their allocation and deallocation.
bool update_entry_point_rva(IN OUT BYTE *pe_buffer, IN DWORD ep)
bool set_sec_alignment(IN OUT BYTE *pe_buffer, IN bool is_raw, IN DWORD new_alignment)
bool has_relocations(IN const BYTE *pe_buffer)
DWORD get_entry_point_rva(IN const BYTE *pe_buffer)
WORD get_nt_hdr_architecture(IN const BYTE *pe_buffer)
bool set_subsystem(IN OUT BYTE *payload, IN WORD subsystem)
DWORD get_virtual_sec_size(IN const BYTE *pe_hdr, IN const PIMAGE_SECTION_HEADER sec_hdr, IN bool rounded)
const IMAGE_FILE_HEADER * get_file_hdr(IN const BYTE *payload, IN const size_t buffer_size)
bool update_image_base(IN OUT BYTE *payload, IN ULONGLONG destImageBase)
bool is_valid_sections_hdr_offset(IN const BYTE *buffer, IN const size_t buffer_size)
ULONGLONG get_image_base(IN const BYTE *pe_buffer)
WORD get_file_characteristics(IN const BYTE *payload)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
IMAGE_TYPE_DIRECTORY * get_type_directory(IN HMODULE modulePtr, IN DWORD dir_id)
IMAGE_NT_HEADERS64 * get_nt_hdrs64(IN const BYTE *pe_buffer)
IMAGE_COR20_HEADER * get_dotnet_hdr(IN const BYTE *pe_buffer, IN size_t const buffer_size, IN const IMAGE_DATA_DIRECTORY *dotNetDir)
DWORD get_image_size(IN const BYTE *payload)
DWORD get_sec_alignment(IN const BYTE *modulePtr, IN bool is_raw)
bool is64bit(IN const BYTE *pe_buffer)
size_t get_sections_count(IN const BYTE *buffer, IN const size_t buffer_size)
IMAGE_NT_HEADERS32 * get_nt_hdrs32(IN const BYTE *pe_buffer)
bool update_image_size(IN OUT BYTE *payload, IN DWORD new_img_size)
const ULONGLONG MAX_HEADER_SIZE
DWORD get_hdrs_size(IN const BYTE *pe_buffer)
IMAGE_DATA_DIRECTORY * get_directory_entry(IN const BYTE *pe_buffer, IN DWORD dir_id, IN bool allow_empty=false)
PIMAGE_SECTION_HEADER get_last_section(IN const PBYTE pe_buffer, IN size_t pe_size, IN bool is_raw)
WORD get_dll_characteristics(IN const BYTE *payload)
IMAGE_EXPORT_DIRECTORY * get_export_directory(IN HMODULE modulePtr)
bool is_valid_sectons_alignment(IN const BYTE *buffer, IN const SIZE_T buffer_size, IN bool is_raw)
BYTE * get_nt_hdrs(IN const BYTE *pe_buffer, IN OPTIONAL size_t buffer_size=0)
DWORD calc_pe_size(IN const PBYTE pe_buffer, IN size_t pe_size, IN bool is_raw)
bool is_module_dll(IN const BYTE *payload)
WORD get_subsystem(IN const BYTE *payload)
LPVOID get_optional_hdr(IN const BYTE *payload, IN const size_t buffer_size)
#define PAGE_SIZE