forked from pulpocaminante/PPL-0day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportInterface.hpp
executable file
·323 lines (275 loc) · 11 KB
/
ExportInterface.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#pragma once
#include <phnt_windows.h>
#include <phnt.h>
#include <stdio.h>
#include <time.h>
#include <SetupAPI.h>
#include <strsafe.h>
#include <iostream>
#include <vector>
#include <sddl.h>
#include <shellapi.h>
#include "config.h"
#include "util.h"
#include <comdef.h>
#include "wmi_defs.h"
// class for locating exported functions in a DLL and
// retrieving their addresses
class IExport
{
public:
template<typename... Args, typename = std::enable_if_t<(std::is_same_v<LPCSTR, Args> && ...)>>
// Unused, needs to be rewritten
static const std::vector<LPVOID> LoadAndFindExports(LPCSTR lpDll, Args ... args)
{
// Declarations
std::vector vArgs = { args ... }; // unpack variadic list
std::vector<LPVOID> fnMap; // our function map for return
HMODULE hModule;// Module handle
PIMAGE_EXPORT_DIRECTORY pIED; // Image export directory
IMAGE_EXPORT_DIRECTORY IED;
PBYTE pBase; // base address of module
char* cDll;
wchar_t* lpwDll;
// Initializations
pIED = &IED;
cDll = new CHAR[strlen(lpDll) + sizeof(CHAR)];
lpwDll = new WCHAR[strlen(lpDll) * sizeof(wchar_t) + sizeof(wchar_t)];
// Decipher DLL name
railfence_decipher(5, lpDll, cDll);
mbstowcs(lpwDll, cDll, strlen(cDll) + sizeof(wchar_t));
// Load DLL
hModule = (HMODULE)LoadLibrary(lpwDll);
//ILog("Loading %ls\n", lpwDll);
// Initialize pBase
pBase = (PBYTE)hModule;
// Make sure library was loaded
if (!hModule)
{
// LoadLibrary failed
ILog("Failed to load module: %d\n", GetLastError());
delete[] lpwDll;
delete[] cDll;
return fnMap;
}
// Get PIMAGE_EXPORT_DIRECTORY
pIED = GetExportDirectoryTable(hModule);
if (pIED == NULL)
{
delete[] lpwDll;
delete[] cDll;
return fnMap;
}
//ILog("%ls: %u named exports\n", lpwDll, pIED->NumberOfNames);
// Get pointers to the name and function arrays
PDWORD pNames = (PDWORD)(pBase + pIED->AddressOfNames);
PDWORD pFunctions = (PDWORD)(pBase + pIED->AddressOfFunctions);
PWORD pOrdinals = (PWORD)(pBase + pIED->AddressOfNameOrdinals);
for (DWORD i = 0; i < pIED->NumberOfNames; i++) {
// Get the name and function address for this entry
PSTR pName = (PSTR)(pBase + pNames[i]);
// Look up the ordinal value for the function in the address lookup table
WORD ordinal = pIED->Base + pOrdinals[i];
//ILog("%s: 0x%d\n", pName, ordinal);
for (auto& fn : vArgs)
{
// Declarations & initializations
char* lpszFn = new char[strlen(pName) + 1];
railfence_encipher(5, pName, lpszFn);
// Compare strings to enciphered module name
if (strncmp(fn, lpszFn, strlen(lpszFn)) == 0)
{
LPVOID fnAddress = util::GetModuleFunc(lpwDll, (LPCSTR)ordinal);
//ILog("Found %s @ 0x%llx\n", pName, fnAddress);
fnMap.push_back(fnAddress);
}
// Call destructor
delete[] lpszFn;
}
}
// Destructors
delete[] cDll;
delete[] lpwDll;
return fnMap;
}
static const LPVOID LoadAndFindSingleExport(LPCSTR lpDll, LPCSTR function)
{
// Declarations
HMODULE hModule;// Module handle
PIMAGE_EXPORT_DIRECTORY pIED; // Image export directory
IMAGE_EXPORT_DIRECTORY IED;
PBYTE pBase; // base address of module
LPVOID fnAddress;
char* cDll;
wchar_t* lpwDll;
// Initializations
pIED = &IED;
cDll = new CHAR[strlen(lpDll) + sizeof(CHAR)];
lpwDll = new WCHAR[( (strlen(lpDll) * sizeof(wchar_t)) ) + sizeof(wchar_t)];
// Decipher DLL name
railfence_decipher(5, lpDll, cDll);
if (MultiByteToWideChar(CP_ACP, 0, cDll, -1, lpwDll, (strlen(cDll) + 1) * sizeof(wchar_t)) == 0)
{
ILog("Failed to convert verb to wide char\n");
delete[] cDll;
delete[] lpwDll;
return nullptr;
}
// Load DLL
hModule = (HMODULE)LoadLibrary(lpwDll);
//ILog("Loading %ls\n", lpwDll);
// Initialize pBase
pBase = (PBYTE)hModule;
// Make sure library was loaded
if (!hModule)
{
// LoadLibrary failed
ILog("Failed to load module: %d\n", GetLastError());
delete[] cDll;
delete[] lpwDll;
return nullptr;
}
// Get PIMAGE_EXPORT_DIRECTORY
pIED = GetExportDirectoryTable(hModule);
if (pIED == NULL)
{
delete[] cDll;
delete[] lpwDll;
return nullptr;
}
//ILog("%ls: %u named exports\n", lpwDll, pIED->NumberOfNames);
// Get pointers to the name and function arrays
PDWORD pNames = (PDWORD)(pBase + pIED->AddressOfNames);
PDWORD pFunctions = (PDWORD)(pBase + pIED->AddressOfFunctions);
PWORD pOrdinals = (PWORD)(pBase + pIED->AddressOfNameOrdinals);
for (DWORD i = 0; i < pIED->NumberOfNames; i++) {
// Get the name and function address for this entry
PSTR pName = (PSTR)(pBase + pNames[i]);
// Look up the ordinal value for the function in the address lookup table
DWORD ordinal = pIED->Base + pOrdinals[i];
// There was a ninja segfault here because of a zero length
// or corrupted export name
if ((strlen(pName) != 0) && (strlen(pName) == strlen(function)))
{
// Declarations & initializations
char* lpszFn = new char[strlen(pName) + 2];
railfence_encipher(5, pName, lpszFn);
// Compare strings to enciphered module name
if (strncmp(function, lpszFn, strlen(lpszFn)) == 0)
{
if (ordinal != NULL)
{
if ((fnAddress = util::GetModuleFunc(lpwDll, (LPCSTR)ordinal)) != NULL)
{
//ILog("Found %s @ 0x%llx\n", pName, fnAddress);
delete[] lpszFn;
delete[] cDll;
delete[] lpwDll;
return fnAddress;
}
}
else
{
if ((fnAddress = util::GetModuleFunc(lpwDll, pName)) != NULL)
{
//ILog("Found %s @ 0x%llx\n", pName, fnAddress);
delete[] lpszFn;
delete[] cDll;
delete[] lpwDll;
return fnAddress;
}
}
}
// Destructor
delete[] lpszFn;
}
}
// Destructors
delete[] cDll;
delete[] lpwDll;
ILog("Failed to find export %s in %s\n", function, lpDll);
return nullptr;
}
// Railfence cipher encode
static void railfence_encipher(int key, const char* plaintext, char* ciphertext)
{
int line, i, skip, length = strlen(plaintext), j = 0, k = 0;
for (line = 0; line < key - 1; line++) {
skip = 2 * (key - line - 1);
k = 0;
for (i = line; i < length;) {
ciphertext[j] = plaintext[i];
if ((line == 0) || (k % 2 == 0)) i += skip;
else i += 2 * (key - 1) - skip;
j++; k++;
}
}
for (i = line; i < length; i += 2 * (key - 1)) ciphertext[j++] = plaintext[i];
ciphertext[j] = '\0'; // Null terminate
}
// Railfence cipher decode
static void railfence_decipher(int key, const char* ciphertext, char* plaintext)
{
int i, length = strlen(ciphertext), skip, line, j, k = 0;
for (line = 0; line < key - 1; line++) {
skip = 2 * (key - line - 1);
j = 0;
for (i = line; i < length;) {
plaintext[i] = ciphertext[k++];
if ((line == 0) || (j % 2 == 0)) i += skip;
else i += 2 * (key - 1) - skip;
j++;
}
}
for (i = line; i < length; i += 2 * (key - 1)) plaintext[i] = ciphertext[k++];
plaintext[length] = '\0'; /* Null terminate */
}
// Railfence cipher wchar_t* decode
static void railfence_wdecipher(int key, const wchar_t* ciphertext, wchar_t* plaintext)
{
int i, length = wcslen(ciphertext), skip, line, j, k = 0;
for (line = 0; line < key - 1; line++) {
skip = 2 * (key - line - 1);
j = 0;
for (i = line; i < length;) {
plaintext[i] = ciphertext[k++];
if ((line == 0) || (j % 2 == 0)) i += skip;
else i += 2 * (key - 1) - skip;
j++;
}
}
for (i = line; i < length; i += 2 * (key - 1)) plaintext[i] = ciphertext[k++];
plaintext[length] = '\00'; /* Null terminate */
}
// Get the export directory table from the module
static const PIMAGE_EXPORT_DIRECTORY GetExportDirectoryTable(HMODULE hModule)
{
PBYTE pBase; // base address of module
PIMAGE_FILE_HEADER pIFH; // COFF file header
PIMAGE_EXPORT_DIRECTORY pIED; // export directory table (EDT)
DWORD RVA; // relative virtual address of EDT
PIMAGE_DOS_HEADER pIDH; // MS-DOS stub
PIMAGE_OPTIONAL_HEADER pIOH; // so-called "optional" header
PDWORD peSig; // PE signature
//ILog("Module base: 0x%llx\n", (HMODULE)hModule);
pBase = (PBYTE)hModule;
pIDH = (PIMAGE_DOS_HEADER)hModule;
peSig = (PDWORD)(pBase + pIDH->e_lfanew);
if (IMAGE_NT_SIGNATURE != *peSig)
{
// PE signature is invalid
ILog("Incorrect PE signature, got: %d\n", *peSig);
return NULL;
}
//ILog("PE Signature: %d\n", *peSig);
pIFH = (PIMAGE_FILE_HEADER)(peSig + 1);
pIOH = (PIMAGE_OPTIONAL_HEADER)(pIFH + 1);
if (IMAGE_DIRECTORY_ENTRY_EXPORT >= pIOH->NumberOfRvaAndSizes) {
// This image doesn't have an export directory table.
return NULL;
}
RVA = pIOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
pIED = (PIMAGE_EXPORT_DIRECTORY)(pBase + RVA);
return pIED;
}
};