-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdllMain.c
287 lines (247 loc) · 10.8 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
# include <stdio.h>
# include <windows.h>
# include <winternl.h>
// Defines:
typedef NTSTATUS(NTAPI* MYPROC) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
typedef struct _MY_LDR_DATA_TABLE_ENTRY {
LIST_ENTRY InLoadOrderLinks;
LIST_ENTRY InMemoryOrderLinks;
LIST_ENTRY InInitializationOrderLinks;
PVOID DllBase;
PVOID Reserved2[2];
UNICODE_STRING FullDllName;
BYTE Reserved4[4];
PVOID ShortDllName;
PVOID Reserved5[3];
#pragma warning(push)
#pragma warning(disable: 4201) // we'll always use the Microsoft compiler
union {
ULONG CheckSum;
PVOID Reserved6;
} DUMMYUNIONNAME;
#pragma warning(pop)
ULONG TimeDateStamp;
} MY_LDR_DATA_TABLE_ENTRY, * PMY_LDR_DATA_TABLE_ENTRY;
typedef struct _MY_PEB_LDR_DATA {
BYTE Reserved1[8];
PVOID Reserved2;
PVOID pFirstEntry;
PVOID Reserved3;
LIST_ENTRY InMemoryOrderModuleList;
} MY_PEB_LDR_DATA, * PMY_PEB_LDR_DATA;
# define MAX_STR_LEN (256)
# define RETURN_ERROR_WRONG_USAGE (13)
# define RETURN_ERROR_FAILED_GETTING_HANDLE (2)
# define RETURN_ERROR_FAILED_LOADING_NTDLL (3)
# define RETURN_ERROR_FAILED_GETTING_NTQUERYINFORMATIONPROCESS (4)
# define RETURN_ERROR_FAILED_USING_NTQUERYINFORMATIONPROCESS (5)
# define RETURN_ERROR_FAILED_GETTING_PEB_ADDRESS (6)
# define RETURN_ERROR_FAILED_GETTING_LDR_DATA (7)
# define RETURN_ERROR_PRINTING_LISTS (8)
# define RETURN_ERROR_DELETING_MODULE (9)
# define RETURN_ERROR_FAILED_DELETING_MODULE_FROM_LOADORDER_LIST (10)
# define RETURN_ERROR_FAILED_DELETING_MODULE_FROM_MEMORYORDER_LIST (11)
# define RETURN_ERROR_FAILED_DELETING_MODULE_FROM_INITIALIZATIONORDER_LIST (12)
# define RETURN_ERROR_FAILED_GETTING_MODULE_FILENAME (14)
// Functions Declarations:
void PrintThreeLists(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry);
int DeleteModuleFromThreeLists(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete);
int DeleteModuleFromLoadOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete);
int DeleteModuleFromMemoryOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete);
int DeleteModuleFromInitializationOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete);
// Functions Definitions:
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
DWORD dwResult = 0;
HMODULE hNtdll = NULL;
LONG lReturnLength = 0;
HANDLE hTargetProc = NULL;
PMY_PEB_LDR_DATA pLdrData = NULL;
NTSTATUS ntTargetProcStatus = { 0 };
wchar_t wcaFilename[MAX_STR_LEN] = { 0 };
MY_LDR_DATA_TABLE_ENTRY* myCurEntry = NULL;
MY_LDR_DATA_TABLE_ENTRY* myFirstEntry = NULL;
PROCESS_BASIC_INFORMATION TargetInfo = { 0 };
MYPROC pFunctionNtQueryInformationProcess = NULL;
// for debugging:
OutputDebugStringW(L"\ndllTest module loaded successfully! :)\n");
hTargetProc = GetCurrentProcess();
if (NULL == hTargetProc)
{
printf("Error: Failed to get handle to the target process. Error code is: %d.\nExiting program...", GetLastError());
return RETURN_ERROR_FAILED_GETTING_HANDLE;
}
hNtdll = LoadLibraryW(L"ntdll.dll");
if (NULL == hNtdll)
{
printf("Error: Failed to load NTDLL. Error code is: %d.\nExiting program...", GetLastError());
CloseHandle(hTargetProc);
return RETURN_ERROR_FAILED_LOADING_NTDLL;
}
pFunctionNtQueryInformationProcess = GetProcAddress(hNtdll, "NtQueryInformationProcess");
if (NULL == pFunctionNtQueryInformationProcess)
{
printf("Error: Failed to get function NtQueryInformationProcess. Error code is: %d.\nExiting program...", GetLastError());
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_FAILED_GETTING_NTQUERYINFORMATIONPROCESS;
}
ntTargetProcStatus = pFunctionNtQueryInformationProcess(hTargetProc, ProcessBasicInformation, &TargetInfo, sizeof(TargetInfo), &lReturnLength);
if (ERROR_SUCCESS != ntTargetProcStatus)
{
printf("Error: Failed to get process information with function NtQueryInformationProcess. Error code is: %d.\nYou should check the error code here: https://davidvielmetter.com/tips/ntstatus-error-code-list/ \nExiting program...", ntTargetProcStatus);
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_FAILED_USING_NTQUERYINFORMATIONPROCESS;
}
if (NULL == TargetInfo.PebBaseAddress)
{
printf("Error: Failed to get process PEB address. Error code is: %d.\nYou should check the error code here: https://davidvielmetter.com/tips/ntstatus-error-code-list/ \nExiting program...", ntTargetProcStatus);
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_FAILED_GETTING_PEB_ADDRESS;
}
pLdrData = (MY_PEB_LDR_DATA*)TargetInfo.PebBaseAddress->Ldr;
myFirstEntry = (MY_LDR_DATA_TABLE_ENTRY*)pLdrData->pFirstEntry;
myCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)myFirstEntry->InLoadOrderLinks.Flink;
if (NULL == myCurEntry)
{
printf("Error: Failed to get modules info for the current process. Check last error code: %d", GetLastError());
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_FAILED_GETTING_LDR_DATA;
}
// get the wanted dll name to delete
dwResult = GetModuleFileNameW(hModule, wcaFilename, MAX_STR_LEN);
if (0 == dwResult)
{
printf("Error: An error occured while trying to get module filename. Exiting program.");
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_FAILED_GETTING_MODULE_FILENAME;
}
if (ERROR_SUCCESS != DeleteModuleFromThreeLists(myFirstEntry, wcaFilename))
{
printf("Error: An error occured while trying to delete module from lists via function DeleteModuleFromThreeLists. Exiting program.");
CloseHandle(hTargetProc);
CloseHandle(hNtdll);
return RETURN_ERROR_DELETING_MODULE;
}
// for debugging:
OutputDebugStringW(L"\nModule hiding was sucessful!\n");
// close handles and exit
CloseHandle(hNtdll);
CloseHandle(hTargetProc);
return TRUE;
}
int DeleteModuleFromThreeLists(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete)
{
if (DeleteModuleFromLoadOrder(inFirstListEntry, pModuleToDelete))
{
return RETURN_ERROR_FAILED_DELETING_MODULE_FROM_LOADORDER_LIST;
}
if (DeleteModuleFromMemoryOrder(inFirstListEntry, pModuleToDelete))
{
return RETURN_ERROR_FAILED_DELETING_MODULE_FROM_MEMORYORDER_LIST;
}
if (DeleteModuleFromInitializationOrder(inFirstListEntry, pModuleToDelete))
{
return RETURN_ERROR_FAILED_DELETING_MODULE_FROM_INITIALIZATIONORDER_LIST;
}
return ERROR_SUCCESS;
}
int DeleteModuleFromLoadOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete)
{
MY_LDR_DATA_TABLE_ENTRY* pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)inFirstListEntry->InLoadOrderLinks.Flink;
MY_LDR_DATA_TABLE_ENTRY* pPreEntry = inFirstListEntry;
void* pStop = (void*)pCurEntry->InLoadOrderLinks.Blink->Blink; // we take 2 steps backwards because we want to avoid the "HideDLL.exe" entry and get back the address of the ldr;
do
{
if (0 == wcscmp(pModuleToDelete, pCurEntry->FullDllName.Buffer)) // If this is the wanted DLL to hide
{
pPreEntry->InLoadOrderLinks.Flink = pCurEntry->InLoadOrderLinks.Flink;
pCurEntry->InLoadOrderLinks.Flink->Blink = (LIST_ENTRY*)pPreEntry;
return 0;
}
pPreEntry = pCurEntry;
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)pCurEntry->InLoadOrderLinks.Flink;
} while (pStop != pCurEntry); // This is our way to check is we've seen all of the modules
return 1;
}
int DeleteModuleFromMemoryOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete)
{
MY_LDR_DATA_TABLE_ENTRY* pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)inFirstListEntry->InLoadOrderLinks.Flink;
MY_LDR_DATA_TABLE_ENTRY* pPreEntry = inFirstListEntry;
void* pStop = (void*)pCurEntry->InLoadOrderLinks.Blink->Blink; // we take 2 steps backwards because we want to avoid the "HideDLL.exe" entry and get back the address of the ldr;
do
{
if (0 == wcscmp(pModuleToDelete, pCurEntry->FullDllName.Buffer)) // If this is the wanted DLL to hide
{
pPreEntry->InMemoryOrderLinks.Flink = pCurEntry->InMemoryOrderLinks.Flink;
pCurEntry->InMemoryOrderLinks.Flink->Blink = (LIST_ENTRY*)pPreEntry;
return 0;
}
pPreEntry = pCurEntry;
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)(((char*)pCurEntry->InMemoryOrderLinks.Flink) - sizeof(LIST_ENTRY));
} while (pStop != pCurEntry); // This is our way to check is we've seen all of the modules
return 1;
}
int DeleteModuleFromInitializationOrder(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry, char* pModuleToDelete)
{
MY_LDR_DATA_TABLE_ENTRY* pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)inFirstListEntry->InLoadOrderLinks.Flink;
MY_LDR_DATA_TABLE_ENTRY* pPreEntry = inFirstListEntry;
void* pStop = (void*)pCurEntry->InLoadOrderLinks.Blink->Blink; // we take 2 steps backwards because we want to avoid the "HideDLL.exe" entry and get back the address of the ldr;
do
{
if (0 == wcscmp(pModuleToDelete, pCurEntry->FullDllName.Buffer)) // If this is the wanted DLL to hide
{
pPreEntry->InInitializationOrderLinks.Flink = pCurEntry->InInitializationOrderLinks.Flink;
pCurEntry->InInitializationOrderLinks.Flink->Blink = (LIST_ENTRY*)pPreEntry;
return 0;
}
pPreEntry = pCurEntry;
if (0 == pCurEntry->InInitializationOrderLinks.Flink)
{
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)pCurEntry->InLoadOrderLinks.Flink;
}
else
{
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)(((char*)pCurEntry->InInitializationOrderLinks.Flink) - 2*sizeof(LIST_ENTRY));
}
} while (pStop != pCurEntry); // This is our way to check is we've seen all of the modules
return 1;
}
// the function PrintThreeLists prints the 3 LDR lists.
// the function isn't in use as part of this module, but should be used for debugging only.
void PrintThreeLists(MY_LDR_DATA_TABLE_ENTRY* inFirstListEntry)
{
MY_LDR_DATA_TABLE_ENTRY* pCurEntry = inFirstListEntry;
void* pStop = (void*)pCurEntry->InLoadOrderLinks.Blink->Blink; // we take 2 steps backwards because we want to avoid the "HideDLL.exe" entry and get back the address of the ldr;
int i = 0;
printf("\nPrinting LoadOrder List:\n");
i = 1;
do
{
printf("LoadOrder List, Module n. %d is: %ls\n", i, pCurEntry->FullDllName.Buffer);
i++;
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)pCurEntry->InLoadOrderLinks.Flink;
} while (pStop != pCurEntry);
printf("\nPrinting MemoryOrder List:\n");
i = 1;
pCurEntry = inFirstListEntry;
do
{
printf("MemoryOrder List, Module n. %d is: %ls\n", i, pCurEntry->FullDllName.Buffer);
i++;
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)(((char*)pCurEntry->InMemoryOrderLinks.Flink) - sizeof(LIST_ENTRY));
} while (pStop != pCurEntry);
printf("\nPrinting InitializationOrder List:\n");
i = 1;
pCurEntry = inFirstListEntry;
do
{
printf("InitializationOrder List, Module n. %d is: %ls\n", i, pCurEntry->FullDllName.Buffer);
i++;
pCurEntry = (MY_LDR_DATA_TABLE_ENTRY*)(((char*)pCurEntry->InInitializationOrderLinks.Flink) - 2*sizeof(LIST_ENTRY));
} while (pStop != pCurEntry);
}