libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
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
15namespace peconv {
19 const ULONGLONG MAX_HEADER_SIZE = PAGE_SIZE;
20
24 DWORD get_image_size(IN const BYTE *payload);
25
29 bool update_image_size(IN OUT BYTE* payload, IN DWORD new_img_size);
30
34 WORD get_nt_hdr_architecture(IN const BYTE *pe_buffer);
35
39 bool is64bit(IN const BYTE *pe_buffer);
40
45 BYTE* get_nt_hdrs(
46 IN const BYTE *pe_buffer,
47 IN OPTIONAL size_t buffer_size=0, //if buffer_size=0 means size unknown
48 IN OPTIONAL const LONG max_pe_offset=MAX_HEADER_SIZE //max offset past which `e_lfanew` is not allowed
49 );
50
54 IMAGE_NT_HEADERS32* get_nt_hdrs32(IN const BYTE *pe_buffer);
55
59 IMAGE_NT_HEADERS64* get_nt_hdrs64(IN const BYTE *pe_buffer);
60
64 LPVOID get_optional_hdr(IN const BYTE* payload, IN const size_t buffer_size);
65
69 const IMAGE_FILE_HEADER* get_file_hdr(
70 IN const BYTE* payload,
71 IN const size_t buffer_size
72 );
73
77 DWORD get_hdrs_size(IN const BYTE *pe_buffer);
78
82 IMAGE_DATA_DIRECTORY* get_directory_entry(IN const BYTE* pe_buffer, IN DWORD dir_id, IN bool allow_empty = false);
83
87 template <typename IMAGE_TYPE_DIRECTORY>
88 IMAGE_TYPE_DIRECTORY* get_type_directory(IN HMODULE modulePtr, IN DWORD dir_id)
89 {
90 IMAGE_DATA_DIRECTORY *my_dir = peconv::get_directory_entry((const BYTE*)modulePtr, dir_id);
91 if (!my_dir) return nullptr;
92
93 DWORD dir_addr = my_dir->VirtualAddress;
94 if (dir_addr == 0) return nullptr;
95
96 return (IMAGE_TYPE_DIRECTORY*)(dir_addr + (ULONG_PTR)modulePtr);
97 }
98
102 IMAGE_EXPORT_DIRECTORY* get_export_directory(IN HMODULE modulePtr);
103
104 // Fetch Image Base from Optional Header.
105 ULONGLONG get_image_base(IN const BYTE *pe_buffer);
106
110 bool update_image_base(IN OUT BYTE* payload, IN ULONGLONG destImageBase);
111
115 DWORD get_entry_point_rva(IN const BYTE *pe_buffer);
116
120 bool update_entry_point_rva(IN OUT BYTE *pe_buffer, IN DWORD ep);
121
125 size_t get_sections_count(
126 IN const BYTE* buffer,
127 IN const size_t buffer_size
128 );
129
133 bool is_valid_sections_hdr_offset(IN const BYTE* buffer, IN const size_t buffer_size);
134
138 PIMAGE_SECTION_HEADER get_section_hdr(
139 IN const BYTE* pe_buffer,
140 IN const size_t buffer_size,
141 IN size_t section_num
142 );
143
147 WORD get_file_characteristics(IN const BYTE* payload);
148
152 bool is_module_dll(IN const BYTE* payload);
153
157 bool is_dot_net(BYTE *pe_buffer, size_t pe_buffer_size);
158
162 WORD get_dll_characteristics(IN const BYTE* payload);
163
167 bool set_subsystem(IN OUT BYTE* payload, IN WORD subsystem);
168
172 WORD get_subsystem(IN const BYTE* payload);
173
177 bool has_relocations(IN const BYTE *pe_buffer);
178
182 IMAGE_COR20_HEADER* get_dotnet_hdr(
183 IN const BYTE* pe_buffer,
184 IN size_t const buffer_size,
185 IN const IMAGE_DATA_DIRECTORY* dotNetDir
186 );
187
191 DWORD get_sec_alignment(IN const BYTE* modulePtr, IN bool is_raw);
192
196 bool set_sec_alignment(IN OUT BYTE* pe_buffer, IN const bool is_raw, IN const DWORD new_alignment);
197
202 IN const BYTE* pe_hdr,
203 IN const PIMAGE_SECTION_HEADER sec_hdr,
204 IN bool rounded //if set, it rounds it up to the Virtual Alignment
205 );
206
213 PIMAGE_SECTION_HEADER get_last_section(IN const PBYTE pe_buffer, IN const size_t pe_size, IN const bool is_raw);
214
221 DWORD calc_pe_size(
222 IN const PBYTE pe_buffer,
223 IN const size_t pe_size,
224 IN const bool is_raw
225 );
226
233 bool is_valid_sections_alignment(IN const BYTE* buffer, IN const SIZE_T buffer_size, IN const bool is_raw);
234
235}; // 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 has_relocations(IN const BYTE *pe_buffer)
DWORD get_entry_point_rva(IN const BYTE *pe_buffer)
bool set_sec_alignment(IN OUT BYTE *pe_buffer, IN const bool is_raw, IN const DWORD new_alignment)
WORD get_nt_hdr_architecture(IN const BYTE *pe_buffer)
bool set_subsystem(IN OUT BYTE *payload, IN WORD subsystem)
bool is_dot_net(BYTE *pe_buffer, size_t pe_buffer_size)
PIMAGE_SECTION_HEADER get_last_section(IN const PBYTE pe_buffer, IN const size_t pe_size, IN const bool is_raw)
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)
bool is_valid_sections_alignment(IN const BYTE *buffer, IN const SIZE_T buffer_size, IN const bool is_raw)
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)
DWORD calc_pe_size(IN const PBYTE pe_buffer, IN const size_t pe_size, IN const bool is_raw)
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
BYTE * get_nt_hdrs(IN const BYTE *pe_buffer, IN OPTIONAL size_t buffer_size=0, IN OPTIONAL const LONG max_pe_offset=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)
WORD get_dll_characteristics(IN const BYTE *payload)
IMAGE_EXPORT_DIRECTORY * get_export_directory(IN HMODULE modulePtr)
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