libPeConv
A library to load, manipulate, dump PE files.
exports_mapper.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <windows.h>
9
10#include <string>
11#include <map>
12#include <set>
13#include <sstream>
14
15#include "pe_hdrs_helper.h"
16#include "pe_raw_to_virtual.h"
18#include "peconv/file_util.h"
19
20namespace peconv {
21
23
24 public:
25
32 size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, ULONGLONG moduleBase);
33
41 size_t add_to_lookup(std::string moduleName, HMODULE modulePtr)
42 {
43 return add_to_lookup(moduleName, modulePtr, reinterpret_cast<ULONGLONG>(modulePtr));
44 }
45
49 const std::set<ExportedFunc>* find_exports_by_va(ULONGLONG va) const
50 {
51 std::map<ULONGLONG, std::set<ExportedFunc>>::const_iterator itr = va_to_func.find(va);
52 if (itr != va_to_func.end()) {
53 const std::set<ExportedFunc> &fSet = itr->second;
54 return &fSet;
55 }
56 return NULL;
57 }
58
62 std::string get_dll_path(std::string short_name) const
63 {
64 std::map<std::string, std::string>::const_iterator found = this->dll_shortname_to_path.find(short_name);
65 if (found == dll_shortname_to_path.end()) {
66 return "";
67 }
68 return found->second;
69 }
70
74 std::string get_dll_fullname(std::string short_name) const
75 {
76 std::string dll_path = get_dll_path(short_name);
77 if (dll_path.length() == 0) return "";
78
79 return get_file_name(dll_path);
80 }
81
85 const ExportedFunc* find_export_by_va(ULONGLONG va) const
86 {
87 const std::set<ExportedFunc>* exp_set = find_exports_by_va(va);
88 if (exp_set == NULL) return NULL;
89
90 std::set<ExportedFunc>::iterator fItr = exp_set->begin();
91 const ExportedFunc* func = &(*fItr);
92 return func;
93 }
94
95 void print_va_to_func(std::stringstream &stream) const;
96 void print_func_to_va(std::stringstream &stream) const;
97
98
99 private:
100 enum ADD_FUNC_RES { RES_INVALID = 0, RES_MAPPED = 1, RES_FORWARDED = 2 };
101 ADD_FUNC_RES add_function_to_lookup(HMODULE modulePtr, ULONGLONG moduleBase, size_t moduleSize, ExportedFunc &currFunc, DWORD callRVA);
102
103 bool add_forwarded(ExportedFunc &currFunc, DWORD callRVA, PBYTE modulePtr, size_t moduleSize);
104 bool add_to_maps(ULONGLONG va, ExportedFunc &currFunc);
105
106 size_t resolve_forwarders(const ULONGLONG va, ExportedFunc &currFunc);
107 size_t make_ord_lookup_tables(PVOID modulePtr, size_t moduleSize, std::map<PDWORD, DWORD> &va_to_ord);
108
109 protected:
113 void associateVaAndFunc(ULONGLONG va, const ExportedFunc& func)
114 {
115 va_to_func[va].insert(func);
116 func_to_va[func] = va;
117 }
118
122 std::map<ULONGLONG, std::set<ExportedFunc>> va_to_func;
123
127 std::map<ExportedFunc, std::set<ExportedFunc>> forwarders_lookup;
128
132 std::map<ExportedFunc, ULONGLONG> func_to_va;
133
137 std::map<std::string, std::string> dll_shortname_to_path;
138 };
139
140}; //namespace peconv
void print_va_to_func(std::stringstream &stream) const
std::map< std::string, std::string > dll_shortname_to_path
size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, ULONGLONG moduleBase)
size_t add_to_lookup(std::string moduleName, HMODULE modulePtr)
void associateVaAndFunc(ULONGLONG va, const ExportedFunc &func)
std::string get_dll_path(std::string short_name) const
std::map< ExportedFunc, ULONGLONG > func_to_va
void print_func_to_va(std::stringstream &stream) const
const ExportedFunc * find_export_by_va(ULONGLONG va) const
std::string get_dll_fullname(std::string short_name) const
const std::set< ExportedFunc > * find_exports_by_va(ULONGLONG va) const
std::map< ExportedFunc, std::set< ExportedFunc > > forwarders_lookup
std::map< ULONGLONG, std::set< ExportedFunc > > va_to_func
A definition of ExportedFunc class - used for storing the details of the exported function....
Functions related to operations on files. Wrappers for read/write.
std::string get_file_name(IN const std::string full_path)
Definition: file_util.cpp:132
Wrappers over various fields in the PE header. Read, write, parse PE headers.
Converting PE from raw to virtual format.