libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
fix_dot_net_ep.cpp
Go to the documentation of this file.
1#include "fix_dot_net_ep.h"
2#include <peconv.h>
3
4#include <string>
5#include <map>
6
8{
9public:
14
26
27protected:
28 template <typename T_FIELD, typename T_IMAGE_THUNK_DATA>
30 {
32#ifdef _DEBUG
33 std::cout << "via RVA: " << std::hex << call_via_rva << " : ";
34#endif
35 bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
36 if (!is_by_ord) {
38 LPSTR func_name = reinterpret_cast<LPSTR>(by_name->Name);
39#ifdef _DEBUG
40 std::cout << "name: " << func_name << std::endl;
41#endif
43 }
44 return true;
45 }
46
47 std::map<std::string, DWORD> &nameToAddr;
48};
49
51{
52 std::map<std::string, DWORD> name_to_addr;
55
56 std::map<std::string, DWORD>::iterator found = name_to_addr.find("_CorExeMain");
57 if (found != name_to_addr.end()) return found->second;
58
59 found = name_to_addr.find("_CorDllMain");
60 if (found != name_to_addr.end()) return found->second;
61
62 return 0;
63}
64
66{
67 // search the jump pattern, i.e.:
68 //JMP DWORD NEAR [0X402000] : FF 25 00204000
69 const size_t jmp_size = 2;
70 const BYTE jmp_pattern[jmp_size] = { 0xFF, 0x25 };
71
72 const size_t arg_size = sizeof(DWORD);
73 if ((jmp_size + arg_size) > buf_size) {
74 return nullptr;
75 }
76 const size_t end_offset = buf_size - (jmp_size + arg_size);
77
78 for (size_t i = end_offset; // search backwards
79 (i + 1) != 0; // this is unsigned comparison, so we cannot do: i >= 0
80 i--) // go back by one BYTE
81 {
82 if (buf[i] == jmp_pattern[0] && buf[i + 1] == jmp_pattern[1]) { // JMP
83 DWORD* addr = (DWORD*)(&buf[i + jmp_size]);
84 DWORD rva = static_cast<DWORD>((*addr) - img_base);
85 if (rva == cor_exe_main_thunk) {
86#ifdef _DEBUG
87 std::cout << "Found call to _CorExeMain\n";
88#endif
89 return buf + i;
90 }
91 else {
92 std::cerr << "[!] Mismatch: " << std::hex << rva << " vs _CorExeMain: " << cor_exe_main_thunk << std::endl;
93 }
94 }
95 }
96 return nullptr;
97}
98
100{
101 if (!pe_buffer) return false;
102
104 //64bit .NET files have EP=0
106 return true;
107 }
108
110#ifdef _DEBUG
111 std::cout << "[*] This is a .NET payload and may require Enty Point correction. Current EP: " << std::hex << ep_rva << "\n";
112#endif
114 if (!sec_hdr) {
115 return false;
116 }
117 BYTE* sec_ptr = (BYTE*)((ULONG_PTR)pe_buffer + sec_hdr->VirtualAddress);
119 return false;
120 }
123 if (!cor_exe_main_thunk) {
124 return false;
125 }
127 if (!jump_ptr) {
128 return false;
129 }
132#ifdef _DEBUG
133 std::cout << "[*] Found possible Entry Point: " << std::hex << offset << std::endl;
134#endif
135 return true;
136}
137
virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA *desc, T_FIELD *call_via, T_FIELD ordinal_flag)
ListImportNames(BYTE *_modulePtr, size_t _moduleSize, std::map< std::string, DWORD > &name_to_addr)
std::map< std::string, DWORD > & nameToAddr
ImportThunksCallback(BYTE *_modulePtr, size_t _moduleSize)
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)
bool fix_dot_net_ep(BYTE *pe_buffer, size_t pe_buffer_size)
BYTE * search_jump(BYTE *buf, size_t buf_size, const DWORD cor_exe_main_thunk, const ULONGLONG img_base)
DWORD find_corexemain(BYTE *buf, size_t buf_size)
bool update_entry_point_rva(IN OUT BYTE *pe_buffer, IN DWORD ep)
DWORD get_entry_point_rva(IN const BYTE *pe_buffer)
ULONGLONG get_image_base(IN const BYTE *pe_buffer)
bool process_import_table(IN BYTE *modulePtr, IN SIZE_T moduleSize, IN ImportThunksCallback *callback)
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
bool is64bit(IN const BYTE *pe_buffer)
Master include file, including everything else.