libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
pe_loader.cpp
Go to the documentation of this file.
1#include "peconv/pe_loader.h"
2
3#include "peconv/relocate.h"
8
9#include <tchar.h>
10#include <iostream>
11
12using namespace peconv;
13
14namespace peconv {
32};
33
35{
37 return NULL;
38 }
41 }
42 // by default, allow to load the PE at any base:
44 // if relocating is required, but the PE has no relocation table...
46 // ...enforce loading the PE image at its default base (so that it will need no relocations)
48 }
49 // load a virtual image of the PE file at the desired_base address (random if desired_base is NULL):
51 if (mappedDLL) {
52 //if the image was loaded at its default base, relocate_module will return always true (because relocating is already done)
54 // relocating was required, but it failed - thus, the full PE image is useless
55 std::cerr << "[!] Could not relocate the module!\n";
58 }
59 } else {
60 std::cerr << "[!] Could not allocate memory at the desired base!\n";
61 }
62 return mappedDLL;
63}
64
66{
67 size_t r_size = 0;
69 if (!dllRawData) {
70#ifdef _DEBUG
71 std::cerr << "Cannot load the file: " << filename << std::endl;
72#endif
73 return NULL;
74 }
77 return mappedPE;
78}
79
81{
83 if (!loaded_pe) {
84 std::cerr << "[-] Loading failed!\n";
85 return NULL;
86 }
87#if _DEBUG
88 printf("Loaded at: %p\n", loaded_pe);
89#endif
92 printf("[-] Loading imports failed!");
94 return NULL;
95 }
96 }
97 else {
98 printf("[-] PE doesn't have a valid Import Table!\n");
99 }
100 return loaded_pe;
101}
102
103
105{
106#if _DEBUG
107 _tprintf(TEXT("Module: %s\n"), my_path);
108#endif
109 BYTE* loaded_pe = load_pe_module(my_path, v_size, true, true);
110 if (!loaded_pe) {
111 printf("[-] Loading failed!\n");
112 return NULL;
113 }
114#if _DEBUG
115 printf("Loaded at: %p\n", loaded_pe);
116#endif
118 printf("[-] Loading imports failed!");
120 return NULL;
121 }
122 return loaded_pe;
123}
Definitions of the used buffer types. Functions for their allocation and deallocation.
bool parse_delayed_desc(BYTE *modulePtr, const size_t moduleSize, const ULONGLONG img_base, LPSTR lib_name, const T_FIELD ordinal_flag, IMAGE_DELAYLOAD_DESCRIPTOR *desc, peconv::t_function_resolver *func_resolver)
Searching specific functions in PE's Exports Table.
Definitions of basic Imports Resolver classes. They can be used for filling imports when the PE is lo...
Parsing and filling the Import Table.
bool has_relocations(IN const BYTE *pe_buffer)
bool has_valid_import_table(const PBYTE modulePtr, size_t moduleSize)
BYTE * load_pe_executable(BYTE *payload_raw, size_t r_size, OUT size_t &v_size, t_function_resolver *import_resolver=NULL)
Definition pe_loader.cpp:80
peconv::UNALIGNED_BUF load_file(IN LPCTSTR filename, OUT size_t &r_size)
Definition file_util.cpp:11
BYTE * load_no_sec_pe(BYTE *dllRawData, size_t r_size, OUT size_t &v_size, bool executable)
Definition pe_loader.cpp:15
ULONGLONG get_image_base(IN const BYTE *pe_buffer)
BYTE * load_pe_module(BYTE *payload_raw, size_t r_size, OUT size_t &v_size, bool executable, bool relocate)
Definition pe_loader.cpp:34
DWORD get_image_size(IN const BYTE *payload)
bool free_pe_buffer(ALIGNED_BUF buffer, size_t buffer_size=0)
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
BYTE * pe_raw_to_virtual(IN const BYTE *rawPeBuffer, IN size_t rawPeSize, OUT size_t &outputSize, IN OPTIONAL bool executable=true, IN OPTIONAL ULONGLONG desired_base=0)
size_t get_sections_count(IN const BYTE *buffer, IN const size_t buffer_size)
bool relocate_module(IN BYTE *modulePtr, IN SIZE_T moduleSize, IN ULONGLONG newBase, IN ULONGLONG oldBase=0)
Definition relocate.cpp:158
void free_file(IN peconv::UNALIGNED_BUF buffer)
BYTE * get_nt_hdrs(IN const BYTE *pe_buffer, IN OPTIONAL size_t buffer_size=0)
bool load_imports(BYTE *modulePtr, t_function_resolver *func_resolver=nullptr)
#define PAGE_SIZE
Loading PE from a file with the help of the custom loader.
Operating on PE file's relocations table.