BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
ImportBaseDirWrapper.cpp
Go to the documentation of this file.
2
3//---------------------------------
4
6
7bool imports_util::isNameValid(Executable *pe, char* myName)
8{
9 if (!myName) return false; // do not parse, invalid entry
10 bufsize_t upperLimit = pe->getMaxSizeFromPtr((BYTE*) myName);
11 if (upperLimit == 0) return false;
12
13 bool isInvalid = pe_util::hasNonPrintable(myName, upperLimit);
14 if (isInvalid) return false;
15 if (pe_util::noWhiteCount(myName) == 0) return false;
16
17 return true;
18}
19//---------------------------------
20
21using namespace imports_util;
22
24 if (bits == Executable::BITS_32) return sizeof (uint32_t);
25 else if (bits == Executable::BITS_64) return sizeof (uint64_t);
26 return 0;
27}
28
29
31{
32 // Every imported function has its own thunk in the file, so a well-formed file
33 // cannot have more functions than thunks that fit in it. Without this limit,
34 // many descriptors pointing to the same thunk array make the parsing quadratic.
35 const bufsize_t tSize = thunkSize(m_Exe->getBitMode());
36 if (tSize == 0) return 0;
37 return static_cast<size_t>(m_Exe->getRawSize() / tSize);
38}
39
41{
42 ImportBaseFuncWrapper* func = dynamic_cast<ImportBaseFuncWrapper*> (funcNode);
43 if (func == NULL) return;
44
45 offset_t via = func->callVia();
46 if (via == INVALID_ADDR) return;
47 /*
48 if (m_Exe->isValidVA(via)) {
49 via = m_Exe->VaToRva(via);
50 }*/
51 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*>(func->getParentNode());
52 if (!lib) return;
53
54 this->thunksList.push_back(via);
55 size_t num = lib->getEntryId();
56 thunkToLibMap[via] = num;
57
58 num = func->getEntryId();
59 lib->thunkToFuncMap[via] = num;
60}
61
63{
64 thunksList.clear();
65 thunkToLibMap.clear();
66}
67
68
70{
72 size_t entriesCount = this->entries.size();
73
74 for (size_t i = 0; i < entriesCount; i++) {
75 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*> (this->getEntryAt(i));
76 if (!lib) continue;
77
78 size_t funcCount = lib->getEntriesCount();
79 for (size_t fI = 0; fI < funcCount; fI++) {
80 addMapping(lib->getEntryAt(fI));
81 }
82 }
83}
84
86{
87 std::map<offset_t, size_t>::iterator libItr = thunkToLibMap.find(thunk);
88 if (libItr == thunkToLibMap.end()) return NULL;
89
90 size_t libId = libItr->second;
91 ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*>(this->getEntryAt(libId));
92 return lib;
93}
94
96{
98 if (!lib) return NULL;
99
100 std::map<offset_t, size_t>::iterator funcItr = lib->thunkToFuncMap.find(thunk);
101 if (funcItr == lib->thunkToFuncMap.end()) return NULL;
102
103 ImportBaseFuncWrapper* func = dynamic_cast<ImportBaseFuncWrapper*>(lib->getEntryAt(funcItr->second));
104 return func;
105}
106
108{
110 if (func == NULL) return "";
111 if (shortName) {
112 return func->getShortName();
113 }
114 return func->getName();
115}
116
118{
120 if (!lib) return "";
121 return lib->getName();
122}
123
125{
126 clearMapping();
127 clear();
128
129 size_t oldCount = this->importsCount;
130 this->importsCount = 0;
131 this->invalidEntries = 0;
132 this->funcsCount = 0;
133
134 if (!getDataDirectory()) {
135 return (oldCount != this->importsCount); //has count changed
136 }
137
138 const size_t LIMIT = (-1);
139 const size_t INVALID_LIMIT = 100;
140 size_t cntr = 0;
141 size_t invalidSeries = 0;
142 for (cntr = 0; cntr < LIMIT; cntr++) {
143 if (loadNextEntry(cntr) == false) break;
144 ExeNodeWrapper* entry = this->entries.at(cntr);
145 if (!entry) break;
146 if (entry->isValid()) {
147 invalidSeries = 0;
148 }
149 else {
150 invalidSeries++;
151 this->invalidEntries++;
152 if (invalidSeries >= INVALID_LIMIT) break;
153 }
154 }
155
156 this->importsCount = cntr;
157 return (oldCount != this->importsCount); //has count changed
158}
159
161{
162 if (this->invalidEntries > 0) return false;
163
164 const QList<offset_t> thunks = getThunksList();
165 if (!thunks.size()) return false;
166 return true;
167}
168
169//--------------------------------------------------------------------------------------------------------------
170
172{
173 if (this->invalidEntries > 0) return false;
174 char *libName = this->getLibraryName();
175 if (!imports_util::isNameValid(m_Exe, libName)) return false;
176 return true;
177}
178
180{
181 if (impDir) {
182 // the functions of this entry are counted again below
184 }
185 clear();
186 thunkToFuncMap.clear();
187
188 this->invalidEntries = 0;
189
190 size_t LIMIT = (-1);
191 if (impDir) {
192 const size_t funcsLimit = impDir->getFuncsLimit();
193 LIMIT = (impDir->funcsCount < funcsLimit) ? (funcsLimit - impDir->funcsCount) : 0;
194 }
195 const size_t INVALID_LIMIT = 100;
196 if (!isValid()) {
197 return false;
198 }
199
200 if (this->getPtr() == NULL) {
201 return false;
202 }
203
204 size_t cntr = 0;
205 size_t invalidSeries = 0;
206 for (cntr = 0; cntr < LIMIT; cntr++) {
207 if (loadNextEntry(cntr) == false) break;
208 ExeNodeWrapper* entry = this->entries.at(cntr);
209 if (!entry) break;
210 if (entry->isValid()) {
211 invalidSeries = 0;
212 }
213 else {
214 invalidSeries++;
215 this->invalidEntries++;
216 if (invalidSeries >= INVALID_LIMIT) break;
217 }
218 }
219 if (impDir) {
220 impDir->funcsCount += entries.size();
221 if (LIMIT && cntr == LIMIT) {
222 Logger::append(Logger::D_WARNING, "Imports: the limit of functions has been reached: %lu", static_cast<unsigned long>(impDir->funcsCount));
223 }
224 }
225 //printf("Entries: %d\n", entries.size());
226 return true;
227}
228
229//--------------------------------------------------------------------------------------------------------------
231{
232 QString functionName;
233 if (isByOrdinal()) {
234 uint64_t val = getOrdinal();
235 QString out;
236#if QT_VERSION >= 0x050000
237 out = QString::asprintf("<ord: %llX>", static_cast<unsigned long long>(val));
238#else
239 out.sprintf("<ord: %llX>", static_cast<unsigned long long>(val));
240#endif
241 functionName = out;
242 } else {
243 char *fName = this->getFunctionName();
244 if (!fName) return "";
245 functionName = fName;
246 }
247 return functionName;
248}
249
251{
253 if (!p) return "";
254
255 char *libName = p->getLibraryName();
256 if (!libName) return "";
257
258 return QString(libName);
259}
260
262{
263 QString libName = getLibName();
264 QString functionName = getShortName();
265
266 if (!libName.length()) return functionName;
267
268 return "[" + QString(libName) + "]." + functionName;
269}
270
272{
274 if (!p) return false;
275
276 char *libName = p->getLibraryName();
277 if (!imports_util::isNameValid(m_Exe, libName)) return false;
278
279 if (!isByOrdinal()) {
280 char *fName = this->getFunctionName();
281 if (!imports_util::isNameValid(m_Exe, fName)) return false;
282 }
283 return true;
284}
const offset_t INVALID_ADDR
uint64_t offset_t
size_t bufsize_t
IMAGE_DATA_DIRECTORY * getDataDirectory()
virtual void * getPtr()=0
virtual QString getName()=0
ExeNodeWrapper(Executable *pe, ExeNodeWrapper *parent=NULL)
virtual ExeNodeWrapper * getEntryAt(size_t fieldId)
std::vector< ExeNodeWrapper * > entries
size_t getEntryId()
virtual bool loadNextEntry(size_t entryNum)
virtual bool isValid()
virtual size_t getEntriesCount()
virtual void clear()
virtual exe_bits getBitMode()
Definition Executable.h:58
virtual offset_t getRawSize() const
Definition Executable.h:64
static bufsize_t thunkSize(Executable::exe_bits bits)
ImportBaseEntryWrapper * thunkToLib(offset_t thunk)
QList< offset_t > thunksList
friend class ImportBaseEntryWrapper
std::map< offset_t, size_t > thunkToLibMap
QList< offset_t > getThunksList()
QString thunkToLibName(offset_t thunk)
ImportBaseFuncWrapper * thunkToFunction(offset_t thunk)
void addMapping(ExeNodeWrapper *func)
QString thunkToFuncName(offset_t thunk, bool shortName=true)
size_t invalidEntries
ImportBaseDirWrapper * impDir
static bufsize_t NameLenLimit
bool wrap()
virtual bool isValid()
virtual char * getLibraryName()=0
std::map< offset_t, size_t > thunkToFuncMap
virtual uint64_t getOrdinal()=0
virtual char * getFunctionName()=0
virtual offset_t callVia()=0
virtual bool isByOrdinal()=0
virtual PENodeWrapper * getParentNode()
bool append(dbg_level lvl, const char *format,...)
Definition Util.cpp:8
@ D_WARNING
Definition Util.h:26
bool isNameValid(Executable *pe, char *myName)
bool hasNonPrintable(const char *ptr, size_t maxInp)
Definition Util.cpp:70
size_t noWhiteCount(char *buf, size_t bufSize)
Definition Util.cpp:131