-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkcpplib.cpp
142 lines (119 loc) · 3.66 KB
/
kcpplib.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
#include "kcpplib.h"
#include <fltKernel.h>
#include <ntimage.h>
#define NTSTRSAFE_LIB
#define NTSTRSAFE_NO_CB_FUNCTIONS
#include <ntstrsafe.h>
//-------------------------------------------------------
// global
//-------------------------------------------------------
PDRIVER_OBJECT g_pDriverObject = NULL;
PVOID g_pDrvImageBase = NULL;
SIZE_T g_DrvImageSize = 0;
wchar_t g_DriverRegistryKey[/*MAX_PATH*/ 260];
wchar_t g_DrvServiceKeyName[/*MAX_PATH*/ 260];
//-------------------------------------------------------
// internel function
//-------------------------------------------------------
void
OnUnload(__in DRIVER_OBJECT *driverObject)
{
UNREFERENCED_PARAMETER(driverObject);
//调用 外部提供
KcpplibDriverUnLoad(driverObject);
_cexit();
}
EXTERN_C
NTSTATUS
DriverEntry(__in DRIVER_OBJECT *driverObject, __in UNICODE_STRING *registryPath)
{
//
// struct
//
typedef struct _KLDR_DATA_TABLE_ENTRY_COMMON
{
LIST_ENTRY InLoadOrderLinks;
PVOID ExceptionTable;
ULONG ExceptionTableSize;
// ULONG padding on IA64
PVOID GpValue;
PNON_PAGED_DEBUG_INFO NonPagedDebugInfo;
PVOID DllBase;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
USHORT LoadCount;
USHORT __Unused5;
PVOID SectionPointer;
ULONG CheckSum;
// ULONG padding on IA64
PVOID LoadedImports;
PVOID PatchInformation;
} KLDR_DATA_TABLE_ENTRY_COMMON, *PKLDR_DATA_TABLE_ENTRY_COMMON;
//
// Do Base Operation
//
NTSTATUS ns = STATUS_UNSUCCESSFUL;
if (_cinit() != 0)
{
//这里存在潜在的风险(理论100%成功,失败需要GG)
// return STATUS_APP_INIT_FAILURE;
}
if (driverObject)
{
driverObject->DriverUnload = (DRIVER_UNLOAD *)(OnUnload);
g_pDriverObject = driverObject;
KLDR_DATA_TABLE_ENTRY_COMMON *pEntry = (KLDR_DATA_TABLE_ENTRY_COMMON *)(g_pDriverObject->DriverSection);
if (pEntry)
{
g_pDrvImageBase = pEntry->DllBase;
g_DrvImageSize = (SIZE_T)pEntry->SizeOfImage;
RtlSecureZeroMemory(g_DrvServiceKeyName, sizeof(g_DrvServiceKeyName));
wchar_t wtemp[] = {//%wZ
L'%',
L'w',
L'Z',
0,
0};
RtlStringCchPrintfW(
g_DrvServiceKeyName,
RTL_NUMBER_OF(g_DrvServiceKeyName),
wtemp,
&driverObject->DriverExtension->ServiceKeyName);
//修正PsSetCreateProcessNotifyRoutineEx返回STATUS_ACCESS_DENIED
*(PULONG)((PCHAR)driverObject->DriverSection + 13 * sizeof(void *)) |= 0x20;
}
else
{
//防止被人抹去
ns = STATUS_VIRUS_DELETED;
goto _exit;
}
}
if (registryPath)
{
if (registryPath != (UNICODE_STRING *)-1)
{
RtlSecureZeroMemory(g_DriverRegistryKey, sizeof(g_DriverRegistryKey));
wchar_t wtemp[] = {//%wZ
L'%',
L'w',
L'Z',
0,
0};
RtlStringCchPrintfW(g_DriverRegistryKey, RTL_NUMBER_OF(g_DriverRegistryKey), wtemp, registryPath);
}
}
else
{
//防止被人无模块加载利用
ns = STATUS_VIRUS_INFECTED;
goto _exit;
}
//调用外部提供者
ns = KcpplibDriverEntry(driverObject, registryPath);
_exit:
return ns;
}