PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
iat_block.h
Go to the documentation of this file.
1#pragma once
2
3#include <peconv.h>
4
5#include <sstream>
6#include <map>
7#include <set>
8
9namespace pesieve {
10
12 {
13 public:
14 IATThunksSeries(DWORD start_offset)
15 : startOffset(start_offset), endOffset(start_offset), cov(nullptr), covered(false)
16 {
17 }
18
20 {
21 delete cov;
22 }
23
24 bool operator<(const IATThunksSeries &other) const
25 {
26 return startOffset < other.startOffset;
27 }
28
29 bool insert(DWORD rva, ULONGLONG funcAddr)
30 {
31 rvaToFuncVA[rva] = funcAddr;
32 funcAddresses.insert(funcAddr);
33 if (rva > endOffset) {
34 endOffset = rva;
35 }
36 return true;
37 }
38
39 bool makeCoverage(IN const peconv::ExportsMapper* exportsMap);
40
41 bool isCovered()
42 {
43 return covered;
44 }
45
46 size_t funcCount()
47 {
48 return rvaToFuncVA.size();
49 }
50
51 std::string getDllName();
52
53 //calculate the number of bytes required for filling imports names
54 size_t sizeOfNamesSpace(bool is64b);
55
56 // fill the buffer with imports thunks and names
57 bool fillNamesSpace(const BYTE* buf_start, size_t buf_size, DWORD bufRVA, bool is64b);
58
59 std::map<DWORD, ULONGLONG> getRvaToFuncMap()
60 {
61 return rvaToFuncVA;
62 }
63
65 DWORD endOffset;
66
67 private:
68 bool covered;
69 std::string dllFullName;
70 std::set<ULONGLONG> funcAddresses;
71 std::map<DWORD, ULONGLONG> rvaToFuncVA;
72
73 peconv::ImportedDllCoverage *cov;
74 };
75
77 {
78 bool operator()(const IATThunksSeries* lhs, const IATThunksSeries* rhs) const
79 {
80 if (!lhs || !rhs) return false;
81 return *lhs < *rhs;
82 }
83 };
84
85 typedef std::set<IATThunksSeries*, IATThunksSeriesPtrCompare> IATThunksSeriesSet;
86
88 {
89 public:
90 IATBlock(bool _is64bit, DWORD _iat_offset)
91 : is64bit(_is64bit),
92 iatOffset(_iat_offset), iatSize(0),
93 isInMain(false), isTerminated(false), isCoverageComplete(false),
95 {
96 }
97
99 {
101 }
102
103 bool operator<(const IATBlock &other) const
104 {
105 return iatOffset < other.iatOffset;
106 }
107
108 bool append(DWORD rva, ULONGLONG functionVA, const peconv::ExportedFunc *exp)
109 {
110 if (!exp) return false;
111
112 functions[rva] = exp;
113 addrToFunctionVA[rva] = functionVA;
114
115 IATThunksSeries* mySeries = nullptr;
116 for (auto itr = thunkSeries.begin(); itr != thunkSeries.end(); ++itr) {
117 IATThunksSeries* series = *itr;
118 // is the next offset in the series:
119 if ((series->endOffset + sizeof(rva)) == rva) {
120 mySeries = series;
121 break;
122 }
123 }
124 if (!mySeries) {
125 mySeries = new IATThunksSeries(rva);
126 thunkSeries.insert(mySeries);
127 }
128 mySeries->insert(rva, functionVA);
129 return true;
130 }
131
132 bool isCovered() const
133 {
134 return isCoverageComplete;
135 }
136
137 bool isValid() const
138 {
139 //allow for every block with complete coverage
140 return isCovered();
141 }
142
143 //how many functions the IAT has
144 size_t countThunks() const
145 {
146 return functions.size();
147 }
148
149 std::string toString();
150
152 {
153 IATThunksSeriesSet::iterator itr;
154 for (itr = this->thunkSeries.begin(); itr != thunkSeries.end(); ++itr) {
155 delete *itr;
156 }
157 thunkSeries.clear();
158 }
159
160 bool makeCoverage(IN const peconv::ExportsMapper* exportsMap);
161
162 size_t maxDllLen();
163 size_t sizeOfDllsSpace();
164
165 bool isTerminated; // is the IAT finished by 0
166 bool isInMain; // is the IAT included in the one set in the Data Directory
167
169 size_t iatSize;
170
172
173 protected:
174 IATThunksSeriesSet splitSeries(IN IATThunksSeries* notCoveredSeries, IN const peconv::ExportsMapper& exportsMap);
175
177
180
181 std::map<ULONGLONG, const peconv::ExportedFunc*> functions;
182 std::map<ULONGLONG, ULONGLONG> addrToFunctionVA;
183
184 friend class ImpReconstructor;
185 };
186
187};
IATBlock(bool _is64bit, DWORD _iat_offset)
Definition iat_block.h:90
std::map< ULONGLONG, const peconv::ExportedFunc * > functions
Definition iat_block.h:181
friend class ImpReconstructor
Definition iat_block.h:184
size_t countThunks() const
Definition iat_block.h:144
bool operator<(const IATBlock &other) const
Definition iat_block.h:103
bool append(DWORD rva, ULONGLONG functionVA, const peconv::ExportedFunc *exp)
Definition iat_block.h:108
bool isValid() const
Definition iat_block.h:137
bool isCovered() const
Definition iat_block.h:132
DWORD importTableOffset
Definition iat_block.h:171
void deleteThunkSeries()
Definition iat_block.h:151
std::string toString()
size_t sizeOfDllsSpace()
bool makeCoverage(IN const peconv::ExportsMapper *exportsMap)
IATThunksSeriesSet thunkSeries
Definition iat_block.h:176
std::map< ULONGLONG, ULONGLONG > addrToFunctionVA
Definition iat_block.h:182
IATThunksSeriesSet splitSeries(IN IATThunksSeries *notCoveredSeries, IN const peconv::ExportsMapper &exportsMap)
bool makeCoverage(IN const peconv::ExportsMapper *exportsMap)
Definition iat_block.cpp:25
IATThunksSeries(DWORD start_offset)
Definition iat_block.h:14
bool insert(DWORD rva, ULONGLONG funcAddr)
Definition iat_block.h:29
bool operator<(const IATThunksSeries &other) const
Definition iat_block.h:24
size_t sizeOfNamesSpace(bool is64b)
Definition iat_block.cpp:80
bool fillNamesSpace(const BYTE *buf_start, size_t buf_size, DWORD bufRVA, bool is64b)
Definition iat_block.cpp:39
std::map< DWORD, ULONGLONG > getRvaToFuncMap()
Definition iat_block.h:59
std::set< IATThunksSeries *, IATThunksSeriesPtrCompare > IATThunksSeriesSet
Definition iat_block.h:85
bool operator()(const IATThunksSeries *lhs, const IATThunksSeries *rhs) const
Definition iat_block.h:78