libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
pe_dumper.cpp
Go to the documentation of this file.
1#include "peconv/pe_dumper.h"
2
6#include "peconv/file_util.h"
8#include "fix_dot_net_ep.h"
9#include <iostream>
10
11using namespace peconv;
12
13t_pe_dump_mode peconv::detect_dump_mode(IN const BYTE* buffer, IN size_t mod_size)
14{
15 const t_pe_dump_mode default_mode = peconv::PE_DUMP_UNMAP;
16 if (peconv::is_pe_raw(buffer, mod_size)) {
18 }
19 if (peconv::is_pe_expanded(buffer, mod_size)) {
21 }
22 return default_mode;
23}
24
26 IN LPCTSTR out_path,
27 IN OUT BYTE *buffer, IN size_t mod_size,
28 IN const ULONGLONG start_addr,
29 IN OUT t_pe_dump_mode &dump_mode,
30 IN OPTIONAL const peconv::ExportsMapper* exportsMap
31)
32{
33 // if the exportsMap is supplied, attempt to recover the (destroyed) import table:
34 if (exportsMap != nullptr) {
35 if (!peconv::fix_imports(buffer, mod_size, *exportsMap, NULL)) {
36 std::cerr << "[-] Unable to fix imports!" << std::endl;
37 }
38 }
39 if (dump_mode == PE_DUMP_AUTO || dump_mode >= PE_DUMP_MODES_COUNT) {
40 dump_mode = detect_dump_mode(buffer, mod_size);
41 }
42
43 BYTE* dump_data = buffer;
44 size_t dump_size = mod_size;
45 size_t out_size = 0;
46 BYTE* unmapped_module = nullptr;
47
48 if (dump_mode == peconv::PE_DUMP_UNMAP || dump_mode == peconv::PE_DUMP_REALIGN) {
49 //if the image base in headers is invalid, set the current base and prevent from relocating PE:
50 if (peconv::get_image_base(buffer) == 0) {
51 peconv::update_image_base(buffer, (ULONGLONG)start_addr);
52 }
53 if (is_dot_net(buffer, mod_size)) {
54 fix_dot_net_ep(buffer, mod_size);
55 }
56 const ULONGLONG hdr_base = peconv::get_image_base(buffer);
57 ULONGLONG target_base = start_addr;
58 if (dump_mode != peconv::PE_DUMP_VIRTUAL && peconv::has_relocations(buffer)) {
59 // enforce relocation to the original base
60 target_base = hdr_base;
61 peconv::update_image_base(buffer, (ULONGLONG)start_addr);
62 }
63 if (dump_mode == peconv::PE_DUMP_UNMAP) {
64 unmapped_module = pe_virtual_to_raw(buffer, mod_size, (ULONGLONG)target_base, out_size, false);
65 }
66 else if (dump_mode == peconv::PE_DUMP_REALIGN) {
67 unmapped_module = peconv::pe_realign_raw_to_virtual(buffer, mod_size, (ULONGLONG)target_base, out_size);
68 }
69 // unmap the PE file (convert from the Virtual Format into Raw Format)
70 if (unmapped_module) {
71 dump_data = unmapped_module;
72 dump_size = out_size;
73 }
74 }
75 // save the read module into a file
76 const bool is_dumped = dump_to_file(out_path, dump_data, dump_size);
77
78 peconv::free_pe_buffer(unmapped_module, mod_size);
79 return is_dumped;
80}
Functions related to operations on files. Wrappers for read/write.
bool fix_dot_net_ep(BYTE *pe_buffer, size_t pe_buffer_size)
Functions and classes responsible for fixing Import Table. A definition of ImportedDllCoverage class.
bool dump_to_file(IN LPCTSTR path, IN PBYTE dump_data, IN size_t dump_size)
bool has_relocations(IN const BYTE *pe_buffer)
bool is_dot_net(BYTE *pe_buffer, size_t pe_buffer_size)
bool is_pe_raw(IN const BYTE *pe_buffer, IN size_t pe_size)
bool update_image_base(IN OUT BYTE *payload, IN ULONGLONG destImageBase)
bool fix_imports(IN OUT PVOID modulePtr, IN size_t moduleSize, IN const peconv::ExportsMapper &exportsMap, OUT OPTIONAL peconv::ImpsNotCovered *notCovered)
ULONGLONG get_image_base(IN const BYTE *pe_buffer)
BYTE * pe_virtual_to_raw(IN BYTE *payload, IN size_t in_size, IN ULONGLONG loadBase, OUT size_t &outputSize, IN OPTIONAL bool rebuffer=true)
t_pe_dump_mode
Definition pe_dumper.h:16
@ PE_DUMP_REALIGN
Definition pe_dumper.h:20
@ PE_DUMP_MODES_COUNT
Definition pe_dumper.h:21
@ PE_DUMP_UNMAP
Definition pe_dumper.h:19
@ PE_DUMP_AUTO
Definition pe_dumper.h:17
@ PE_DUMP_VIRTUAL
Definition pe_dumper.h:18
bool free_pe_buffer(ALIGNED_BUF buffer, size_t buffer_size=0)
t_pe_dump_mode detect_dump_mode(IN const BYTE *buffer, IN size_t buffer_size)
Definition pe_dumper.cpp:13
bool is_pe_expanded(IN const BYTE *pe_buffer, IN size_t pe_size)
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)
Definition pe_dumper.cpp:25
BYTE * pe_realign_raw_to_virtual(IN const BYTE *payload, IN size_t in_size, IN ULONGLONG loadBase, OUT size_t &outputSize)
Dumping PE from the memory buffer into a file.
Wrappers over various fields in the PE header. Read, write, parse PE headers.
Detecting in which mode is the PE in the supplied buffer (i.e. raw, virtual). Analyzes PE features ty...
Converting PE from virtual to raw format.