-
Notifications
You must be signed in to change notification settings - Fork 46
/
StrongOD.c
348 lines (288 loc) · 8.99 KB
/
StrongOD.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
346
347
348
/*++
Copyright (c) Bombs
Module Name:
StrongOD.c
Author :Bombs
Time :2014-4-29 11:29:14
Abstract:
This is the main module of the driver
--*/
#include "StrongOD.h"
#include "CommDef.h"
#include "HelperFunc.h"
#include "Ioctl.h"
//
// Global variable
//
ULONG g_ulBuildNum;
ULONG g_ulMajorVer;
ULONG g_ulMinorVer;
DWORD g_dwCsrssProcId;
DWORD g_dwExplorerProcId;
DWORD g_dwRefCount;
DWORD g_dwIsSSDTHooked;
DWORD g_dwIsHidenProcess;
DWORD g_dwIsHidenWindow;
DWORD g_dwIsProtectProcess;
HANDLE g_hForegroundWindow;
PVOID g_pDriverStart;
ULONG g_ulDriverSize;
LIST_NODE g_ProcList[100]; // white process(OD), black process(the Debuggee)
KSPIN_LOCK g_SpinLock; // used to synchronize access to the g_ProcList
ULONG g_CommunicateKey = 0xFB123456; // used for IRP_MJ_DEVICE_CONTROL
ULONG g_IsINT1Hooked = 0; // flag
ULONG g_IsSupportLBR = 0; // if support branch trace
ULONG g_IsEnableLBR = 0; // enable branch trace or not
SERVICE_DESCRIPTOR_TABLE KeServiceDescriptorTableShadow;
SERVICE_DESCRIPTOR_TABLE *pSerDesTable;
KSERVICE_TABLE_DESCRIPTOR win32k;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, CommDispatch)
#pragma alloc_text(PAGE, DriverUnload)
#endif
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath)
/*++
Routine Description:
The entry point of the driver, create dev&sym, initialize global variable, etc.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING unNtDevName, unDosDevName;
PDEVICE_OBJECT pDevObj = NULL;
PsGetVersion(&g_ulMajorVer, &g_ulMinorVer, &g_ulBuildNum, NULL);
RtlInitUnicodeString(&unNtDevName, NT_DEVICE_NAME);
status = IoCreateDevice(pDriverObject,
4, // not used
&unNtDevName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
0,
&pDevObj);
if(NT_SUCCESS(status))
{
RtlInitUnicodeString(&unDosDevName, DOS_DEVICE_NAME);
status = IoCreateSymbolicLink(&unDosDevName, &unNtDevName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
}
pDriverObject->MajorFunction[IRP_MJ_CREATE] = CommDispatch;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = CommDispatch;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = CommDispatch;
pDriverObject->DriverUnload = DriverUnload;
}
g_dwCsrssProcId = (DWORD)GetCsrssProcId();
g_dwExplorerProcId = (DWORD)GetProcIdByName(L"explorer.exe");
pSerDesTable = (PSERVICE_DESCRIPTOR_TABLE)GetServiceDescriptorTableShadowAddr();
KdPrint(("DriverEntry:g_dwCsrssProcId = %d, g_dwExplorerProcId = %d, pSerDesTable = %08x\n",
g_dwCsrssProcId, g_dwExplorerProcId, pSerDesTable));
if(pSerDesTable != NULL)
{
win32k.Base = pSerDesTable->win32k.Base;
win32k.Count = pSerDesTable->win32k.Count;
win32k.Limit = pSerDesTable->win32k.Limit;
win32k.Number = pSerDesTable->win32k.Number;
RtlCopyMemory(&KeServiceDescriptorTableShadow, pSerDesTable, sizeof(KeServiceDescriptorTableShadow));
}
g_dwRefCount = 0;
g_IsINT1Hooked = 0;
g_dwIsSSDTHooked = 0;
g_dwIsHidenProcess = 0;
g_dwIsHidenWindow = 0;
g_dwIsProtectProcess = 0;
g_hForegroundWindow = NULL;
RtlZeroMemory(g_ProcList, sizeof(g_ProcList));
KeInitializeSpinLock(&g_SpinLock);
PsSetCreateProcessNotifyRoutine(ProcCreateNotify, FALSE);
g_pDriverStart = pDriverObject->DriverStart;
g_ulDriverSize = pDriverObject->DriverSize;
SSDTHookInit();
return status;
}
NTSTATUS CommDispatch(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
/*++
Routine Description:
The driver dispatch routine, handle IRP_MJ_CREATE, IRP_MJ_CLOSE and IRP_MJ_DEVICE_CONTROL.
IRP_MJ_DEVICE_CONTROL InputBuffer Layout
| | | | | | | | |
| g_CommunicateKey | g_dwCsrssProcId | g_dwIsHidenProcess | g_dwIsHidenWindow | g_dwIsProtectProcess | g_IsSupportLBR | g_IsEnableLBR| g_dwBlackProcId |
offset | 0 | 4 | 8 | 12 | 16 | 20 | 24 | 28 |
--*/
{
NTSTATUS status;
PVOID pBuffer = pIrp->AssociatedIrp.SystemBuffer;
PIO_STACK_LOCATION pIoStack = IoGetCurrentIrpStackLocation(pIrp);
ULONG ulInputBufferLen = pIoStack->Parameters.DeviceIoControl.InputBufferLength;
ULONG ulOutputBufferLen = pIoStack->Parameters.DeviceIoControl.OutputBufferLength;
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0;
if(pIoStack->MajorFunction == IRP_MJ_DEVICE_CONTROL)
{
EncAndDecBuffer(pBuffer, ulInputBufferLen);
switch (pIoStack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_SOD_ADD_WHITE_PROC:
{
if( g_CommunicateKey == *(PULONG)pBuffer )
{
if(AddProcInfoToList(IoGetCurrentProcess(), 0, SOD_WHITE_PROCESS))
{
if(0 != *((PULONG)pBuffer + 1))
{
InterlockedExchange(&g_dwCsrssProcId, *((PULONG)pBuffer + 1));
InterlockedExchange(&g_IsSupportLBR, *((PULONG)pBuffer + 5));
InterlockedExchange(&g_IsEnableLBR, *((PULONG)pBuffer + 6));
InterlockedExchange(&g_dwIsHidenWindow, *((PULONG)pBuffer + 3));
InterlockedExchange(&g_dwIsHidenProcess, *((PULONG)pBuffer + 2));
InterlockedExchange(&g_dwIsProtectProcess, *((PULONG)pBuffer + 4));
if(GetProcCount(SOD_WHITE_PROCESS))
{
HookSSDT();
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
else
{
KdPrint(("null white process\n"));
}
}
}
else
{
KdPrint(("add white process failed: %X\n", IoGetCurrentProcess()));
}
}
else
{
KdPrint(("Invalid Key! RealKey = [%08X], BufferKey = [%08X]\n",
g_CommunicateKey, *(PULONG)pBuffer));
}
}
break;
case IOCTL_SOD_DEL_WHITE_PROC:
{
if(g_CommunicateKey == *(PULONG)pBuffer)
{
DelProcInfoFromList(IoGetCurrentProcess(), 0, SOD_WHITE_PROCESS);
*(PULONG)pBuffer = GetProcCount(SOD_WHITE_PROCESS);
if(*(PULONG)pBuffer == 0)
{
UnhookSSDT();
}
pIrp->IoStatus.Information = 4;
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
else
{
KdPrint(("Invalid Key! RealKey = [%08X], BufferKey = [%08X]\n",
g_CommunicateKey, *(PULONG)pBuffer));
}
}
break;
case IOCTL_SOD_ADD_BLACK_PROC:
{
if(g_CommunicateKey == *(PULONG)pBuffer)
{
if(*((PULONG)pBuffer + 7) != 0)
{
if(AddProcInfoToList(NULL, *((PULONG)pBuffer + 7), SOD_BLACK_PROCESS) == -1) // [Warning:]
{
*(PULONG)pBuffer = 0;
}
else
{
// return user mode shared memory addr
*(PULONG)pBuffer = (ULONG)(GetProcInfoFromList(NULL, *((PULONG)pBuffer + 7)))->pUserAddrOfSharedMem;
}
}
pIrp->IoStatus.Information = 4;
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
else
{
KdPrint(("Invalid Key! RealKey = [%08X], BufferKey = [%08X]\n",
g_CommunicateKey, *(PULONG)pBuffer));
}
}
break;
case IOCTL_SOD_DEL_BLACK_PROC:
{
if(g_CommunicateKey == *(PULONG)pBuffer)
{
if(*((PULONG)pBuffer + 7) != 0)
{
DelProcInfoFromList(NULL, *((PULONG)pBuffer + 7), SOD_BLACK_PROCESS);
if(0 == GetProcCount(SOD_BLACK_PROCESS))
{
UnhookINT1();
}
}
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
else
{
KdPrint(("Invalid Key! RealKey = [%08X], BufferKey = [%08X]\n",
g_CommunicateKey, *(PULONG)pBuffer));
}
}
break;
case IOCTL_SOD_HOOK_INT1:
{
if(GetProcCount(SOD_BLACK_PROCESS) != 0)
{
HookINT1();
}
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
break;
case IOCTL_SOD_UNHOOK_INT1:
{
if(0 == GetProcCount(SOD_BLACK_PROCESS))
{
UnhookINT1();
}
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
break;
default:
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
break;
}
if(pIrp->IoStatus.Status == STATUS_SUCCESS)
{
EncAndDecBuffer(pBuffer, pIrp->IoStatus.Information);
}
}
status = pIrp->IoStatus.Status;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return status;
}
VOID DriverUnload(PDRIVER_OBJECT pDriverObject)
/*++
Routine Description:
The driver unload routine, do some clean work.
--*/
{
UNICODE_STRING unDosDevName;
LARGE_INTEGER liTimeout;
int nCount = 0;
KdPrint(("Begin Unhook!\n"));
UnhookSSDT();
KdPrint(("Unhook Done!\n"));
PsSetCreateProcessNotifyRoutine(ProcCreateNotify, TRUE);
liTimeout.QuadPart = -10 * 1000; // one millisecond
KdPrint(("KeDelayExecutionThread\n"));
do
{
KeDelayExecutionThread(KernelMode, FALSE, &liTimeout);
if(0 == g_dwRefCount)
{
break;
}
nCount++;
} while (nCount < 10);
RtlInitUnicodeString(&unDosDevName, DOS_DEVICE_NAME);
IoDeleteSymbolicLink(&unDosDevName);
IoDeleteDevice(pDriverObject->DeviceObject);
KdPrint(("UNLOAD SUCCESS!\n"));
}