12 memset(&page_info, 0,
sizeof(MEMORY_BASIC_INFORMATION));
13 SIZE_T out = VirtualQueryEx(processHandle, moduleBase, &page_info,
sizeof(page_info));
14 if (out !=
sizeof(page_info)) {
22 if (page_info.Type == 0) {
25 if ((BYTE*)page_info.BaseAddress > moduleBase) {
28 const size_t offset = (ULONG_PTR)moduleBase - (ULONG_PTR)page_info.BaseAddress;
29 const size_t area_size = page_info.RegionSize - offset;
35 MEMORY_BASIC_INFORMATION page_info = { 0 };
45 MEMORY_BASIC_INFORMATION page_info = { 0 };
49 if (page_info.Type == 0) {
52 return (ULONGLONG) page_info.AllocationBase;
61 SIZE_T
_search_readable_size(HANDLE processHandle, LPVOID start_addr, OUT BYTE* buffer,
const size_t buffer_size,
const SIZE_T minimal_size)
63 if (!buffer || buffer_size == 0) {
66 if ((buffer_size < minimal_size) || minimal_size == 0) {
69 SIZE_T last_failed_size = buffer_size;
70 SIZE_T last_success_size = 0;
72 SIZE_T test_read_size = 0;
73 if (!ReadProcessMemory(processHandle, start_addr, buffer, minimal_size, &test_read_size)) {
75 return test_read_size;
77 last_success_size = minimal_size;
80 SIZE_T to_read_size = buffer_size/2;
82 while (to_read_size > minimal_size && to_read_size < buffer_size)
85 if (ReadProcessMemory(processHandle, start_addr, buffer, to_read_size, &read_size)) {
86 last_success_size = to_read_size;
89 last_failed_size = to_read_size;
91 const size_t delta = (last_failed_size - last_success_size) / 2;
92 if (delta == 0)
break;
93 to_read_size = last_success_size + delta;
95 if (last_success_size) {
97 memset(buffer, 0, buffer_size);
98 if (ReadProcessMemory(processHandle, start_addr, buffer, last_success_size, &read_size)) {
106size_t peconv::read_remote_memory(HANDLE processHandle, LPVOID start_addr, OUT BYTE* buffer,
const size_t buffer_size,
const SIZE_T minimal_size)
108 if (!buffer || buffer_size == 0) {
111 memset(buffer, 0, buffer_size);
112 DWORD last_error = ERROR_SUCCESS;
114 SIZE_T read_size = 0;
115 if (!ReadProcessMemory(processHandle, start_addr, buffer, buffer_size, &read_size)) {
116 last_error = GetLastError();
117 if (last_error == ERROR_PARTIAL_COPY) {
119 LOG_DEBUG(
"peconv::search_readable_size res: 0x%zx.", read_size);
123 if (read_size == 0) {
124 LOG_WARNING(
"Cannot read memory. Last Error: %lu.", last_error);
125 }
else if (read_size < buffer_size) {
126 LOG_WARNING(
"Read size: 0x%zx is smaller than requested: 0x%zx. Last Error: %lu.", (
size_t)read_size, buffer_size, last_error);
128 return static_cast<size_t>(read_size);
131size_t peconv::read_remote_region(HANDLE processHandle, LPVOID start_addr, OUT BYTE* buffer,
const size_t buffer_size,
const bool force_access,
const SIZE_T minimal_size)
133 if (!buffer || buffer_size == 0) {
136 MEMORY_BASIC_INFORMATION page_info = { 0 };
140 if ((page_info.State & MEM_COMMIT) == 0) {
144 if (region_size == 0) {
148 const size_t size_to_read = (region_size > buffer_size) ? buffer_size : region_size;
150 const bool is_accessible = (page_info.Protect & PAGE_NOACCESS) == 0;
151 BOOL access_changed = FALSE;
152 DWORD oldProtect = 0;
155 if (force_access && !is_accessible) {
156 access_changed = VirtualProtectEx(processHandle, start_addr, region_size, PAGE_READONLY, &oldProtect);
157 if (!access_changed) {
158 DWORD err = GetLastError();
159 if (err != ERROR_ACCESS_DENIED) {
160 LOG_WARNING(
"0x%llx : 0x%zx inaccessible area, changing page access failed: %lu.", (
unsigned long long)(ULONG_PTR)start_addr, region_size, err);
165 size_t size_read = 0;
166 if (is_accessible || access_changed) {
168 if ((size_read == 0) && (page_info.Protect & PAGE_GUARD)) {
169 LOG_DEBUG(
"Guarded page, trying to read again.");
174 if (access_changed) {
175 VirtualProtectEx(processHandle, start_addr, region_size, oldProtect, &oldProtect);
180size_t peconv::read_remote_area(HANDLE processHandle, LPVOID start_addr, OUT BYTE* buffer,
const size_t buffer_size,
const bool force_access,
const SIZE_T minimal_size)
182 if (!buffer || !start_addr || buffer_size == 0) {
185 memset(buffer, 0, buffer_size);
187 size_t real_read = 0;
188 size_t last_valid = 0;
190 size_t buf_index = 0;
191 for (buf_index = 0; buf_index < buffer_size; ) {
192 LPVOID remote_chunk = LPVOID((ULONG_PTR)start_addr + buf_index);
194 MEMORY_BASIC_INFORMATION page_info = { 0 };
199 if (region_size == 0) {
207 (BYTE*)((ULONG_PTR)buffer + buf_index),
208 buffer_size - buf_index,
212 if (read_chunk == 0) {
214 buf_index += region_size;
217 buf_index += read_chunk;
218 real_read += read_chunk;
219 last_valid = buf_index;
221 if (real_read == 0) {
229 if (buffer ==
nullptr) {
232 SIZE_T read_size =
read_remote_area(processHandle, start_addr, buffer, buffer_size, force_access);
233 if (read_size == 0) {
237 if (nt_ptr ==
nullptr) {
240 const size_t nt_offset = nt_ptr - buffer;
241 const size_t nt_size =
peconv::is64bit(buffer) ?
sizeof(IMAGE_NT_HEADERS64) :
sizeof(IMAGE_NT_HEADERS32);
242 const size_t min_size = nt_offset + nt_size;
244 if (read_size < min_size) {
245 LOG_ERROR(
"[PID %lu][0x%llx] Read size: 0x%zx is smaller than the minimal size: 0x%lx.",
get_process_id(processHandle), (
unsigned long long)(ULONGLONG)start_addr, read_size,
get_hdrs_size(buffer));
258 size_t parts = size / unit;
259 if (size % unit) parts++;
272 if (section_hdr == NULL || section_hdr->Misc.VirtualSize == 0) {
275 size_t buffer_size = section_hdr->Misc.VirtualSize;
282 if (module_code == NULL) {
285 size_t read_size =
peconv::read_remote_memory(processHandle, LPVOID((ULONG_PTR)start_addr + section_hdr->VirtualAddress), module_code, buffer_size);
286 if (read_size == 0) {
288 read_size =
read_remote_area(processHandle, LPVOID((ULONG_PTR)start_addr + section_hdr->VirtualAddress), module_code, buffer_size, force_access);
290 if (read_size == 0) {
294 section_size = buffer_size;
298size_t peconv::read_remote_pe(
const HANDLE processHandle, LPVOID start_addr,
const size_t mod_size, OUT BYTE* buffer,
const size_t bufferSize)
300 if (buffer ==
nullptr) {
301 LOG_ERROR(
"Invalid output buffer: NULL pointer.");
305 LOG_ERROR(
"Invalid output buffer: size too small.");
309 PBYTE hdr_buffer = buffer;
312 LOG_ERROR(
"Failed to read the module header.");
316 LOG_ERROR(
"Section headers are invalid or atypically aligned.");
320 LOG_DEBUG(
"Sections: %zu.", sections_count);
323 for (
size_t i = 0; i < sections_count; i++) {
326 LOG_ERROR(
"Failed to read the header of section: %zu.", i);
329 const DWORD sec_va = hdr->VirtualAddress;
331 if (sec_va + sec_vsize > bufferSize) {
332 LOG_ERROR(
"No more space in the buffer.");
335 if (sec_vsize > 0 && !
read_remote_memory(processHandle, LPVOID((ULONG_PTR)start_addr + sec_va), buffer + sec_va, sec_vsize)) {
336 LOG_WARNING(
"Failed to read module section %zu at 0x%llx.", i, (
unsigned long long)((ULONG_PTR)start_addr + sec_va));
339 size_t new_end = sec_va + sec_vsize;
340 if (new_end > read_size) read_size = new_end;
342 LOG_DEBUG(
"Total read size: %zu.", read_size);
357 IN
const HANDLE processHandle,
358 IN LPVOID start_addr,
368 if (buffer ==
nullptr) {
369 LOG_ERROR(
"Failed allocating buffer. Error: %lu.", GetLastError());
373 const size_t read_size =
read_remote_pe(processHandle, start_addr, mod_size, buffer, mod_size);
374 if (read_size == 0) {
375 LOG_ERROR(
"Failed reading module. Error: %lu.", GetLastError());
383 reinterpret_cast<ULONGLONG
>(start_addr),
384 dump_mode, exportsMap);
Functions and classes responsible for fixing Import Table. A definition of ImportedDllCoverage class.
#define LOG_DEBUG(fmt,...)
#define LOG_ERROR(fmt,...)
#define LOG_WARNING(fmt,...)
DWORD get_process_id(HANDLE hProcess)
peconv::UNALIGNED_BUF get_remote_pe_section(HANDLE processHandle, LPVOID moduleBase, const size_t sectionNum, OUT size_t §ionSize, bool roundup, bool force_access=false)
DWORD get_virtual_sec_size(IN const BYTE *pe_hdr, IN const PIMAGE_SECTION_HEADER sec_hdr, IN bool rounded)
bool dump_remote_pe(IN LPCTSTR outputFilePath, IN const HANDLE processHandle, IN LPVOID moduleBase, IN OUT t_pe_dump_mode &dump_mode, IN OPTIONAL peconv::ExportsMapper *exportsMap=nullptr)
bool is_valid_sections_hdr_offset(IN const BYTE *buffer, IN const size_t buffer_size)
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, void *desired_base=nullptr)
size_t read_remote_region(HANDLE processHandle, LPVOID start_addr, OUT BYTE *buffer, const size_t buffer_size, const bool force_access, const SIZE_T minimal_size=0x100)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
size_t read_remote_memory(HANDLE processHandle, LPVOID start_addr, OUT BYTE *buffer, const size_t buffer_size, const SIZE_T minimal_size=0x100)
bool fetch_region_info(HANDLE processHandle, LPVOID start_addr, MEMORY_BASIC_INFORMATION &page_info)
DWORD get_image_size(IN const BYTE *payload)
size_t read_remote_area(HANDLE processHandle, LPVOID start_addr, OUT BYTE *buffer, const size_t buffer_size, const bool force_access, const SIZE_T minimal_size=0x100)
bool free_pe_buffer(ALIGNED_BUF buffer, size_t buffer_size=0)
size_t roundup_to_unit(size_t size, size_t unit)
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)
SIZE_T _search_readable_size(HANDLE processHandle, LPVOID start_addr, OUT BYTE *buffer, const size_t buffer_size, const SIZE_T minimal_size)
const ULONGLONG MAX_HEADER_SIZE
bool read_remote_pe_header(HANDLE processHandle, LPVOID moduleBase, OUT BYTE *buffer, const size_t bufferSize, bool force_access=false)
bool dump_pe(IN LPCTSTR outputFilePath, IN OUT BYTE *buffer, IN size_t buffer_size, IN const ULONGLONG module_base, IN OUT t_pe_dump_mode &dump_mode, IN OPTIONAL const peconv::ExportsMapper *exportsMap=nullptr)
DWORD get_hdrs_size(IN const BYTE *pe_buffer)
BYTE * get_nt_hdrs(IN const BYTE *pe_buffer, IN OPTIONAL size_t buffer_size=0)
size_t read_remote_pe(const HANDLE processHandle, LPVOID moduleBase, const size_t moduleSize, OUT BYTE *buffer, const size_t bufferSize)
size_t fetch_region_size(HANDLE processHandle, LPVOID start_addr)
ULONGLONG fetch_alloc_base(HANDLE processHandle, LPVOID start_addr)
void free_unaligned(UNALIGNED_BUF section_buffer)
DWORD get_remote_image_size(IN const HANDLE processHandle, IN LPVOID start_addr)
size_t _fetch_region_size(MEMORY_BASIC_INFORMATION &page_info, LPVOID moduleBase)
Reading from a PE module that is loaded within a remote process.
Miscellaneous utility functions.