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
|
#include "kifast.h"
//
//globle
//
int g_Org_call;
UCHAR KEYCODE[5] = { 0x2b, 0xe1, 0xc1, 0xe9, 0x02 };
ULONG g_NtCreateFileAddr;
NTSTATUS Flag;
ULONG g_HookAddr;
ULONG NextAddr;
//开启页保护
VOID PageprotectOn()
{
__asm
{
mov eax, cr0
or eax, 10000h
mov cr0, eax
sti
}
}
//关闭页保护
VOID PageprotectOff()
{
_asm
{
cli
mov eax, cr0
and eax, not 10000h
mov cr0, eax
}
}
//利用特征码搜索Hook点
NTSTATUS SearchHookAddr(ULONG g_BeginAddr)
{
UCHAR* p;
ULONG u_index;
p = (UCHAR*)g_BeginAddr;
for (u_index = 0; u_index < 200; u_index++)
{
if (*p == KEYCODE[0] && *(p + 1) == KEYCODE[1] &&
*(p + 2) == KEYCODE[2] &&
*(p + 3) == KEYCODE[3] &&
*(p + 4) == KEYCODE[4]
)
{
g_HookAddr = (ULONG)p;
return STATUS_SUCCESS;
}
p--;
}
return STATUS_UNSUCCESSFUL;
}
// 钩子函数调用 过滤函数
// 判断当前是否是 SSDT
// 若是则判断 NtOpenProcess 是被那个进程调用
VOID FilterFuction(ULONG index, ULONG BaseAddr)
{
if (BaseAddr == (ULONG)KeServiceDescriptorTable.ServiceTableBase)
{
if (index == 0x25)
{
KdPrint(("from :%s", (UCHAR*)PsGetCurrentProcess() + 0x174));
}
}
}
// 不生成 栈帧 所以用汇编写
__declspec(naked)
VOID MyhookCall()
{
__asm
{
pushad;
pushf;
push edi;
push eax;
call FilterFuction;
popf;
popad;
// 转入正常流程
sub esp, ecx
shr ecx, 2
JMP NextAddr
}
}
VOID HookKiFastCallEntry()
{
UCHAR JMP_CODE[5];
ULONG offset;
JMP_CODE[0] = 0xE9;
offset = (ULONG)MyhookCall - g_HookAddr - 5; //JMP的偏移地址
*(ULONG*)&JMP_CODE[1] = offset;
// 5 inline hook KifastcallEntry
KdPrint(("offset %x", *(ULONG*)&JMP_CODE[1]));
PageprotectOff();
RtlCopyMemory((PVOID)g_HookAddr, JMP_CODE, 5);
PageprotectOn();
KdPrint(("Hook Successful", g_HookAddr));
}
NTSTATUS
MyNtCreateFile(
__out PHANDLE FileHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in_opt PLARGE_INTEGER AllocationSize,
__in ULONG FileAttributes,
__in ULONG ShareAccess,
__in ULONG CreateDisposition,
__in ULONG CreateOptions,
__in_bcount_opt(EaLength) PVOID EaBuffer,
__in ULONG EaLength
)
{
ULONG g_BeginAddr = NULL;
NTSTATUS status;
// | stack |
// mov edi ,edi
// push ebp
// mov ebp ,esp 执行上述操作后
// ---------------------------------
// | ebp : old ebp |
// | ebp + 4 : ret addr | | 这里就是 KiFastCallEntry 栈空间
//
// 2 栈回溯 找kiFastCallEntry地址空间
__asm
{
pushad
mov eax, [ebp + 4]
mov g_BeginAddr, eax
popad
}
//
// 从获取的地址空间 向前查找特征码
//
status = SearchHookAddr(g_BeginAddr);
if (NT_SUCCESS(status))
{
KdPrint(("Find Hook Address %x", g_HookAddr));
NextAddr = g_HookAddr + 5; //计算正常程序流程的下条指令地址 方便 hook 后转入继续执行
// 3 hook KiFastCallEntry
HookKiFastCallEntry();
//4 恢复 NtCreateFile Hook
PageprotectOff();
KeServiceDescriptorTable.ServiceTableBase[0x25] = (ULONG)g_NtCreateFileAddr;
PageprotectOn();
}
return ((PNtCreateFile)g_NtCreateFileAddr)(FileHandle, DesiredAccess, ObjectAttributes, IoStatusBlock, AllocationSize, FileAttributes,
ShareAccess, CreateDisposition, CreateOptions, EaBuffer, EaLength);
}
NTSTATUS HookSSDT()
{
; // 1 利用SSDT Hook 进入目标API调用栈
g_NtCreateFileAddr = KeServiceDescriptorTable.ServiceTableBase[0x25];
PageprotectOff();
KeServiceDescriptorTable.ServiceTableBase[0x25] = (ULONG)MyNtCreateFile;
PageprotectOn();
}
VOID HookKifastcallentry()
{
HookSSDT();
}
VOID MyUnload(PDRIVER_OBJECT pDriverObject)
{
PageprotectOff();
RtlCopyMemory((PVOID)g_HookAddr, KEYCODE, 5);
PageprotectOn();
KdPrint(("Unhook Successful", g_HookAddr));
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING RegistryPath)
{
KdPrint(("int DriverEntry"));
HookKifastcallentry();
pDriverObject->DriverUnload = MyUnload;
KdPrint(("out DriverEntry"));
return STATUS_SUCCESS;
}
|