PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
module_data.h
Go to the documentation of this file.
1#pragma once
2
3#include <windows.h>
4#include <psapi.h>
5#include <map>
6#include <set>
7
8#include <peconv.h>
10#include "module_cache.h"
11
12namespace pesieve {
13
15 class ModuleData {
16
17 public:
27
28 ModuleData(HANDLE _processHandle, HMODULE _module, std::string module_name, bool _useCache)
32 {
34 memcpy(this->szModName, module_name.c_str(), module_name.length());
35 }
36
38 {
39 peconv::free_pe_buffer(original_module, original_size);
40 }
41
42 bool is64bit()
43 {
44 if (original_module == nullptr) {
45 return false;
46 }
47 return peconv::is64bit(original_module);
48 }
49
50 bool isDotNet() { return this->is_dot_net; }
51
53 {
54 if (module_base == 0) {
55 module_base = reinterpret_cast<ULONGLONG>(this->moduleHandle);
56 }
57 return module_base + rva;
58 }
59
61 {
62 if (module_base == 0) {
63 module_base = reinterpret_cast<ULONGLONG>(this->moduleHandle);
64 }
65 if (va < module_base) {
66 return 0; // not this module
67 }
68 if (va > module_base + this->original_size) {
69 return 0; // not this module
70 }
71 ULONGLONG diff = (va - module_base);
72 return static_cast<DWORD>(diff);
73 }
74
76 {
77 return isPEBConnected;
78 }
79
81 {
82 return original_module != nullptr;
83 }
84
86 {
87 if (!original_module) return 0;
88 return peconv::get_image_base((const BYTE*)original_module);
89 }
90
91 bool loadOriginal();
92
93 bool switchToWow64Path();
94 bool reloadWow64();
96 bool loadRelocatedFields(std::set<DWORD>& fields_rvas);
97 bool loadImportThunks(std::set<DWORD>& fields_rvas);
98 bool loadImportsList(peconv::ImportsCollection &collection);
99
104
107
108 protected:
109 bool _loadOriginal(bool disableFSredir);
110 bool loadModuleName();
112 bool isDotNetManagedCode();
113
117
118 friend class PeSection;
119 };
120
123 {
124 public:
127
131 {
132 isHdrReady = false;
133 memset(headerBuffer, 0, peconv::MAX_HEADER_SIZE);
134 init();
135 }
136
138 {
140 }
141
142 bool isSectionEntry(const size_t section_number);
146 {
147 if (!isHdrReady && !init()) {
148 return false;
149 }
150 return true;
151 }
152
153 bool is64bit()
154 {
155 if (!isHdrReady) return false;
156 return peconv::is64bit(headerBuffer);
157 }
158
160 {
161 if (!isHdrReady) return 0;
162 return peconv::get_image_size((const BYTE*)headerBuffer);
163 }
164
166 {
167 if (!isHdrReady) return 0;
168 return peconv::get_image_base((const BYTE*)headerBuffer);
169 }
170
172 {
173 if (imgBufferSize) {
174 return imgBufferSize;
175 }
176 return getHdrImageSize();
177 }
178
180 {
181 return peconv::MAX_HEADER_SIZE;
182 }
183
184 bool loadFullImage();
185 bool isFullImageLoaded() { return (imgBuffer != nullptr) && (imgBufferSize != 0); }
187 bool loadImportsList(peconv::ImportsCollection& collection);
188
190 {
191 return (ULONGLONG)modBaseAddr;
192 }
193
194 BYTE headerBuffer[peconv::MAX_HEADER_SIZE];
195
196 protected:
197 bool init();
198 bool loadHeader();
199 size_t calcImgSize();
200
201 bool _loadFullImage(size_t v_size);
202
204 {
205 peconv::free_pe_buffer(imgBuffer);
206 imgBuffer = nullptr;
207 imgBufferSize = 0;
208 }
209
211 const bool isReflection;
213
216
217 private:
218 bool isHdrReady;
219
220 friend class PeSection;
221 friend class IATScanner;
222 };
223
224}; //namespace pesieve
225
A scanner for detection of IAT hooking.
Definition iat_scanner.h:62
Loads a module from the disk, corresponding to the module in the scanned process' memory.
Definition module_data.h:15
ULONGLONG rvaToVa(DWORD rva, ULONGLONG module_base=0)
Definition module_data.h:52
ModuleData(HANDLE _processHandle, HMODULE _module, bool _isPEBConnected, bool _useCache)
Definition module_data.h:18
bool relocateToBase(ULONGLONG new_base)
DWORD vaToRva(ULONGLONG va, ULONGLONG module_base=0)
Definition module_data.h:60
bool loadRelocatedFields(std::set< DWORD > &fields_rvas)
bool _loadOriginal(bool disableFSredir)
ModuleData(HANDLE _processHandle, HMODULE _module, std::string module_name, bool _useCache)
Definition module_data.h:28
char szModName[MAX_PATH]
ULONGLONG getHdrImageBase()
Definition module_data.h:85
bool loadImportsList(peconv::ImportsCollection &collection)
bool loadImportThunks(std::set< DWORD > &fields_rvas)
Buffers the defined PE section belonging to the module loaded in the scanned process into the local m...
Definition pe_section.h:12
Buffers the data from the module loaded in the scanned process into the local memory.
bool _loadFullImage(size_t v_size)
bool loadImportsList(peconv::ImportsCollection &collection)
static std::string getModuleName(HANDLE _processHandle, HMODULE _modBaseAddr)
BYTE headerBuffer[peconv::MAX_HEADER_SIZE]
bool hasExecutableSection(bool allow_data, bool allow_inaccessible)
bool isSectionExecutable(const size_t section_number, bool allow_data, bool allow_inaccessible)
static std::string getMappedName(HANDLE _processHandle, LPVOID _modBaseAddr)
RemoteModuleData(HANDLE _processHandle, bool _isRefl, HMODULE _modBaseAddr)
ULONGLONG getRemoteSectionVa(const size_t section_num)
bool isSectionEntry(const size_t section_number)
int MAX_PATH
Definition pesieve.py:10
size_t fill_iat(BYTE *vBuf, size_t vBufSize, IN const peconv::ExportsMapper *exportsMap, IN OUT IATBlock &iat, IN ThunkFoundCallback *callback)
Definition iat_finder.h:31