forked from irohaneABC/ShellCodeBuildandloadexe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMY_API.cpp
More file actions
428 lines (331 loc) · 13.1 KB
/
Copy pathMY_API.cpp
File metadata and controls
428 lines (331 loc) · 13.1 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "TOOL.h"
#include <iostream>
#include<windows.h>
//字符串哈希算法
DWORD Hash_GetDigest(char* strFunName)
{
//字符串长度大于3保证Hash值中不包含0
DWORD dwDigest = 0;
while (*strFunName)
{
dwDigest = (dwDigest << 25 | dwDigest >> 7);
dwDigest += *strFunName;
strFunName++;
}
return dwDigest;
}
bool Hash_CmpString(char* strFunName, int nHash)
{
unsigned int nDigest = 0;
while (*strFunName)
{
nDigest = ((nDigest << 25) | (nDigest >> 7));
nDigest = nDigest + *strFunName;
strFunName++;
}
return nHash == nDigest ? true : false;
}
bool _Is_64Peformat(char* lpPeBufer)
{
PIMAGE_FILE_HEADER _File = (PIMAGE_FILE_HEADER) & ((PIMAGE_NT_HEADERS)((((PIMAGE_DOS_HEADER)lpPeBufer)->e_lfanew) + lpPeBufer))->FileHeader;
if (_File->Machine == IMAGE_FILE_MACHINE_AMD64 ||
_File->Machine == IMAGE_FILE_MACHINE_IA64) // may be IMAGE_FILE_MACHINE_I386
{
return TRUE;
}
else
{
return FALSE;
}
}
DWORD64 GetFunAddrByHash(int nHashDigest, HMODULE hModule)
{
// 1. 获取DOS头、NT头
PIMAGE_DOS_HEADER pDos_Header;
// 2. 获取导出表项
PIMAGE_DATA_DIRECTORY pDataDir;
PIMAGE_EXPORT_DIRECTORY pExport;
pDos_Header = (PIMAGE_DOS_HEADER)hModule;
if (_Is_64Peformat((char*)(hModule)) == TRUE)
{
PIMAGE_NT_HEADERS64 pNt_Header;
pNt_Header = (PIMAGE_NT_HEADERS64)((DWORD64)hModule + pDos_Header->e_lfanew);
pDataDir = pNt_Header->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT;
}
else
{
PIMAGE_NT_HEADERS pNt_Header;
pNt_Header = (PIMAGE_NT_HEADERS)((DWORD64)hModule + pDos_Header->e_lfanew);
pDataDir = pNt_Header->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT;
}
pExport = (PIMAGE_EXPORT_DIRECTORY)((DWORD64)hModule + pDataDir->VirtualAddress);
// 3. 获取导出表详细信息
PDWORD pAddrOfFun = (PDWORD)(pExport->AddressOfFunctions + (DWORD64)hModule);
PDWORD pAddrOfNames = (PDWORD)(pExport->AddressOfNames + (DWORD64)hModule);
PWORD pAddrOfOrdinals = (PWORD)(pExport->AddressOfNameOrdinals + (DWORD64)hModule);
// 4. 处理以函数名查找函数地址的请求,循环获取ENT中的函数名(因为是以函数名
// 为基准,因此不考虑无函数名的情况),并与传入值对比,如能匹配上则在EAT
// 中以指定序号作为索引,并取出其地址值。
DWORD64 dwFunAddr;
for (DWORD i = 0; i < pExport->NumberOfNames; i++)
{
PCHAR lpFunName = (PCHAR)(pAddrOfNames[i] + (DWORD64)hModule);
if (Hash_CmpString(lpFunName, nHashDigest))
{
dwFunAddr = pAddrOfFun[pAddrOfOrdinals[i]] + (DWORD64)hModule;
break;
}
if (i == pExport->NumberOfNames - 1)
return 0;
}
return dwFunAddr;
}
char* RetX32RunExeAdr(char* pExeAddr, HMODULE Kernel32)
{
// 1. 局部字符串
CHAR szUser32[] = { 'u','s','e','r','3','2','.','d','l','l','\0' };
CHAR szKernel32[] = { 'k','e','r','n','e','l','3','2','.','d','l','l','\0' };
CHAR szntdll[] = { 'n','t','d','l','l','.','d','l','l','\0' };
CHAR szCMD[] = { 'c','m','d','.','e','x','e','\0' };
CHAR csVirtualAlloc[] = { 'V','i','r','t','u','a','l','A','l','l','o','c' ,'\0' };
CHAR csVirtualProtect[] = { 'V','i','r','t','u','a','l','P','r','o','t','e','c','t','\0' };
CHAR csGetProcAddress[] = { 'G','e','t','P','r','o','c','A','d','d','r','e','s','s','\0' };
CHAR csCreateThread[] = { 'C','r','e','a','t','e','T','h','r','e','a','d' ,'\0' };
CHAR csWaitForSingleObject[] = { 'W','a','i','t','F','o','r','S','i','n','g','l','e','O','b','j','e','c','t','\0' };
// 2. 获取关键模块基址
HMODULE hKernel32By64 = 0;
HMODULE hNtDll = 0;
DWORD ImageBase = 0;
HMODULE hUser32 = 0;
// 3. 获取关键模块基址
DefineFuncPtr(LoadLibraryExA, Kernel32);
hKernel32By64 = My_LoadLibraryExA(szKernel32, 0, 0);
hNtDll = My_LoadLibraryExA(szntdll, 0, 0);
hUser32 = My_LoadLibraryExA(szUser32, 0, 0);
DefineFuncPtr(VirtualAlloc, hKernel32By64);
DefineFuncPtr(VirtualProtect, hKernel32By64);
DefineFuncPtr(CreateThread, hKernel32By64);
DefineFuncPtr(WaitForSingleObject, hKernel32By64);
DefineFuncPtr(GetProcAddress, hKernel32By64);
//User32
DefineFuncPtr(MessageBoxA, hUser32);
//ntdll
DefineFuncPtr(memcpy, hNtDll);
DefineFuncPtr(sprintf, hNtDll);
HANDLE hFile = NULL;
HANDLE hFileMaping = NULL;
//获取一些基本变量
//PE初始化
// PIMAGE_NT_HEADERS
PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)pExeAddr;
PIMAGE_NT_HEADERS pNtHdr = NULL;
PIMAGE_SECTION_HEADER pSectionHdr = NULL;
int SectionCount = 0;
int SectionAlignment = 0;
int FileAlignment = 0;
//--------------------------------------------------------------------------------------------------------------------------------------申请新内存
pNtHdr = (PIMAGE_NT_HEADERS)((DWORD)pExeAddr + ((PIMAGE_DOS_HEADER)pExeAddr)->e_lfanew);
pSectionHdr = (PIMAGE_SECTION_HEADER)((DWORD)&pNtHdr->OptionalHeader + pNtHdr->FileHeader.SizeOfOptionalHeader);
SectionCount = pNtHdr->FileHeader.NumberOfSections; //区段数
SectionAlignment = pNtHdr->OptionalHeader.SectionAlignment; //内存对其方式
FileAlignment = pNtHdr->OptionalHeader.FileAlignment; //文件对其方式
//申请一个载入 内存后大小的空间
PVOID pNewPe = My_VirtualAlloc(NULL, pNtHdr->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (pNewPe == NULL)
{
return 0;
}
//拉伸PE 扩展块
My_memcpy((void*)pNewPe, (void*)pDosHdr, pNtHdr->OptionalHeader.SizeOfHeaders);
for (int i = 0; i < SectionCount; i++)
{
My_memcpy(
(void*)((DWORD)pNewPe + pSectionHdr[i].VirtualAddress), //VirtualAddress Rva 直接 加PE Va即可
(void*)((DWORD)pExeAddr + pSectionHdr[i].PointerToRawData), //直接从文件区段读取即可
pSectionHdr[i].SizeOfRawData); //拷贝区段大小
}
//-------------------------------------------------------------------------------------------------------------------------------------修复导入表
PIMAGE_IMPORT_DESCRIPTOR pImportTable = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)pNewPe + pNtHdr->OptionalHeader.DataDirectory[1].VirtualAddress);
do
{
const char* pDllPath = (const char*)(pImportTable->Name + (DWORD)pNewPe);
PDWORD pIat = (PDWORD)(pImportTable->FirstThunk + (DWORD)pNewPe);
if (*pIat == NULL)
{
pImportTable++;
continue;
}
HMODULE hModule = My_LoadLibraryExA(pDllPath, 0, 0);
if (hModule == NULL)
{
return 0;
}
else
{
do
{
if (((*pIat) >> 31) != 1)
{
PIMAGE_IMPORT_BY_NAME Name = (PIMAGE_IMPORT_BY_NAME)((DWORD)pNewPe + *pIat);
*pIat = (DWORD)My_GetProcAddress(hModule, Name->Name);
}
else
{
*pIat = (DWORD)My_GetProcAddress(hModule, (const char*)((*pIat) & 0x7fffffff));
}
pIat++;
} while (*pIat);
}
pImportTable++;
} while (pImportTable->Name != NULL);
//--------------------------------------------------------------------------------------------------------------------------------------修复重定位表
DWORD Offset = NULL;
int BaseTableSize = NULL;
int NowSize = NULL;
PIMAGE_BASE_RELOCATION pBaseTable = NULL;
Offset = (DWORD)pNewPe - pNtHdr->OptionalHeader.ImageBase;//计算需要重定位的值
if (pNtHdr->OptionalHeader.DataDirectory[5].VirtualAddress == 0)
{
return 0;
}
pBaseTable = (PIMAGE_BASE_RELOCATION)((DWORD)pNewPe + pNtHdr->OptionalHeader.DataDirectory[5].VirtualAddress);
BaseTableSize = pNtHdr->OptionalHeader.DataDirectory[5].Size;
while ((NowSize < BaseTableSize) && (pBaseTable->VirtualAddress != 0))
{
PWORD pBase = (PWORD)((DWORD)pBaseTable + sizeof(IMAGE_BASE_RELOCATION));
PDWORD pAageBase = (PDWORD)((DWORD)pNewPe + pBaseTable->VirtualAddress);
int Count = (pBaseTable->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
for (int i = 0; i < Count; i++)
{
if (pBase[i] >= 0x3000)//1修改高16位 2修改低16位
{
PDWORD UpdataAddr = (PDWORD)((pBase[i] & 0xfff) + (DWORD)pAageBase);
*UpdataAddr = Offset + *UpdataAddr;
}
}
NowSize += pBaseTable->SizeOfBlock;
pBaseTable = (PIMAGE_BASE_RELOCATION)((DWORD)pBaseTable + pBaseTable->SizeOfBlock);
}
char* mapadd = ((char*)pNewPe + pNtHdr->OptionalHeader.AddressOfEntryPoint);
return mapadd;
}
char* RetX64RunExeAdr(char* pExeAddr, HMODULE Kernel32)
{
// 1. 局部字符串
CHAR szUser32[] = { 'u','s','e','r','3','2','.','d','l','l','\0' };
CHAR szKernel32[] = { 'k','e','r','n','e','l','3','2','.','d','l','l','\0' };
CHAR szntdll[] = { 'n','t','d','l','l','.','d','l','l','\0' };
CHAR szCMD[] = { 'c','m','d','.','e','x','e','\0' };
CHAR csVirtualAlloc[] = { 'V','i','r','t','u','a','l','A','l','l','o','c' ,'\0' };
CHAR csVirtualProtect[] = { 'V','i','r','t','u','a','l','P','r','o','t','e','c','t','\0' };
CHAR csGetProcAddress[] = { 'G','e','t','P','r','o','c','A','d','d','r','e','s','s','\0' };
CHAR csCreateThread[] = { 'C','r','e','a','t','e','T','h','r','e','a','d' ,'\0' };
CHAR csWaitForSingleObject[] = { 'W','a','i','t','F','o','r','S','i','n','g','l','e','O','b','j','e','c','t','\0' };
// 2. 获取关键模块基址
HMODULE hKernel32By64 = 0;
HMODULE hNtDll = 0;
DWORD ImageBase = 0;
HMODULE hUser32 = 0;
// 3. 获取关键模块基址
DefineFuncPtr(LoadLibraryExA, (HMODULE)Kernel32);
hKernel32By64 = My_LoadLibraryExA(szKernel32, 0, 0);
hNtDll = My_LoadLibraryExA(szntdll, 0, 0);
hUser32 = My_LoadLibraryExA(szUser32, 0, 0);
DefineFuncPtr(VirtualAlloc, hKernel32By64);
DefineFuncPtr(VirtualProtect, hKernel32By64);
DefineFuncPtr(CreateThread, hKernel32By64);
DefineFuncPtr(WaitForSingleObject, hKernel32By64);
DefineFuncPtr(GetProcAddress, hKernel32By64);
//User32
DefineFuncPtr(MessageBoxA, hUser32);
//ntdll
DefineFuncPtr(memcpy, hNtDll);
DefineFuncPtr(sprintf, hNtDll);
DWORD64 SectionCount = 0;
DWORD64 SectionAlignment = 0;
DWORD64 FileAlignment = 0;
PIMAGE_NT_HEADERS64 pNtHdr = NULL;
//申请新内存
pNtHdr = (PIMAGE_NT_HEADERS64)((char*)pExeAddr + ((PIMAGE_DOS_HEADER)pExeAddr)->e_lfanew);
SectionCount = pNtHdr->FileHeader.NumberOfSections;
SectionAlignment = pNtHdr->OptionalHeader.SectionAlignment;
FileAlignment = pNtHdr->OptionalHeader.FileAlignment;
LPVOID pNewPe = My_VirtualAlloc(NULL, pNtHdr->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (pNewPe == NULL)
{
return 0;
}
//拉伸PE
My_memcpy((void*)pNewPe, (void*)pExeAddr, pNtHdr->OptionalHeader.SizeOfHeaders);
PIMAGE_SECTION_HEADER pSectionHdr = (PIMAGE_SECTION_HEADER)IMAGE_FIRST_SECTION(pNtHdr);
for (int i = 0; i < SectionCount; i++)
{
My_memcpy((char*)((DWORD64)pNewPe + pSectionHdr[i].VirtualAddress),
(char*)((DWORD64)pExeAddr + pSectionHdr[i].PointerToRawData),
pSectionHdr[i].SizeOfRawData);
}
//修复导入表
PIMAGE_DOS_HEADER pDos64 = (PIMAGE_DOS_HEADER)pNewPe;
PIMAGE_NT_HEADERS64 pNt64 = (PIMAGE_NT_HEADERS64)(pDos64->e_lfanew + (char*)pNewPe);
PIMAGE_IMPORT_DESCRIPTOR ImportTable = (PIMAGE_IMPORT_DESCRIPTOR)((char*)pNewPe + pNt64->OptionalHeader.DataDirectory[1].VirtualAddress);
//printf("Nt%x---ImportTable=%x---%x\n", pNt64, ImportTable, pNt64->OptionalHeader.DataDirectory[1].VirtualAddress);
//遍历导入表 以全0结构体结尾
while (ImportTable->Name != 0)
{
//计算出DLL名称通过LoadLibrary
CHAR* DllName = (CHAR*)(ImportTable->Name + ((char*)pNewPe));
//加载DLL
HMODULE hModule = My_LoadLibraryExA(DllName, 0, 0);
//获取IAT表
PIMAGE_THUNK_DATA64 Iat = (PIMAGE_THUNK_DATA64)(ImportTable->FirstThunk + ((char*)pNewPe));
//检测是否有效
while (Iat->u1.AddressOfData)
{
ULONGLONG FunctionAddr = 0;
//最高位为0
if (!IMAGE_SNAP_BY_ORDINAL(Iat->u1.AddressOfData))
{
PIMAGE_IMPORT_BY_NAME Name = (PIMAGE_IMPORT_BY_NAME)(Iat->u1.Function + ((char*)pNewPe));
FunctionAddr = (ULONGLONG)My_GetProcAddress(hModule, Name->Name);
}
else
{
//如果没有名字,低16位就是序号
FunctionAddr = (ULONGLONG)My_GetProcAddress(hModule, (LPCSTR)(Iat->u1.Ordinal & 0xffff));
}
Iat->u1.Function = (ULONGLONG)FunctionAddr;
++Iat;
}
ImportTable++;
}
//-------------------------------------------------修复重定位表
DWORD64 Offset = NULL;
int BaseTableSize = NULL;
int NowSize = NULL;
PIMAGE_BASE_RELOCATION pBaseTable = NULL;
Offset = (DWORD64)pNewPe - pNt64->OptionalHeader.ImageBase;//计算需要重定位的值
if (pNt64->OptionalHeader.DataDirectory[5].VirtualAddress == 0)
{
return 0 ;
}
pBaseTable = (PIMAGE_BASE_RELOCATION)((DWORD64)(pNewPe)+pNt64->OptionalHeader.DataDirectory[5].VirtualAddress);
BaseTableSize = pNt64->OptionalHeader.DataDirectory[5].Size;
while ((NowSize < BaseTableSize) && (pBaseTable->VirtualAddress != 0))
{
PWORD pBase = (PWORD)((DWORD64)pBaseTable + sizeof(IMAGE_BASE_RELOCATION));
PDWORD64 pAageBase = (PDWORD64)((DWORD64)(pNewPe)+pBaseTable->VirtualAddress);
int Count = (pBaseTable->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
for (int i = 0; i < Count; i++)
{
if (pBase[i] >= 0x3000)//1修改高16位 2修改低16位
{
PDWORD64 UpdataAddr = (PDWORD64)((pBase[i] & 0xfff) + (DWORD64)pAageBase);
*UpdataAddr = Offset + *UpdataAddr;
}
}
NowSize += pBaseTable->SizeOfBlock;
pBaseTable = (PIMAGE_BASE_RELOCATION)((DWORD64)pBaseTable + pBaseTable->SizeOfBlock);
}
char* mapadd = ((char*)pNewPe + pNtHdr->OptionalHeader.AddressOfEntryPoint);
return mapadd;
}