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
|
/*
Windows内核与Win32进行同步操作 0环代码
编译方法参见makefile. TAB = 8
*/
#include <ntddk.h>
typedef struct tagDeviceExt {
PDEVICE_OBJECT pDeviceObj;
UNICODE_STRING uSzDeviceName;
UNICODE_STRING uSzSysLinkName;
} DEVICE_EXT, *PDEVICE_EXT;
#define IOCTL_CONTROL_CREATETHREAD CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS )
#define IOCTL_CONTROL_SHOREEVENT CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS )
#define DEVICE_NAME L"\\Device\\DevWaitForEvent"
#define SYS_LINK_NAME L"\\??\\SysLinkWaitForEvent"
#pragma code_seg("PAGE")
//===========================================================================
// 用户线程Routine
VOID SysRoutine( PVOID pContext ) {
PEPROCESS pEprocess;
PTSTR pStrProcessName;
PKEVENT phEvent;
PAGED_CODE();
phEvent = ( PKEVENT )pContext;
//获取进程名称
pEprocess = IoGetCurrentProcess();
pStrProcessName = ( PTSTR )( ( ULONG )pEprocess + 0x174 );
KdPrint( ( "进程名称:%s\n", pStrProcessName ) );
//停止50毫秒
KeStallExecutionProcessor( 50 );
//置位事件
KeSetEvent( phEvent, IO_NO_INCREMENT, FALSE );
//结束本进程
PsTerminateSystemThread( STATUS_SUCCESS );
}
//===========================================================================
// 系统线程
VOID UserRoutine( PVOID pContext ) {
PKEVENT phEvent;
PEPROCESS pEprocess;
PTSTR pStrProcessName;
PAGED_CODE();
phEvent = ( PKEVENT )pContext;
//获取进程名称
pEprocess = IoGetCurrentProcess();
pStrProcessName = ( PTSTR )( ( ULONG )pEprocess + 0x174 );
KdPrint( ( "进程名称:%s\n", pStrProcessName ) );
//停止50毫秒
KeStallExecutionProcessor( 50 );
//置位事件
KeSetEvent( phEvent, IO_NO_INCREMENT, FALSE );
//结束本进程
PsTerminateSystemThread( STATUS_SUCCESS );
}
//===========================================================================
//驱动卸载例程
VOID DriverUnload( PDRIVER_OBJECT pDriverObj ) {
PDEVICE_OBJECT pNextDeviceObj;
PDEVICE_EXT pDeviceExt;
PAGED_CODE();
pNextDeviceObj = pDriverObj->DeviceObject;
while( pNextDeviceObj != NULL ) {
pDeviceExt = ( PDEVICE_EXT )pNextDeviceObj->DeviceExtension;
IoDeleteDevice( pDeviceExt->pDeviceObj );
IoDeleteSymbolicLink( &pDeviceExt->uSzSysLinkName );
KdPrint( ( "删除设备%wZ成功!\n", &pDeviceExt->uSzDeviceName ) );
pNextDeviceObj = pNextDeviceObj->NextDevice;
}
}
//===========================================================================
// 所有不关心的例程处理
NTSTATUS DispatchRoutine( PDEVICE_OBJECT pDeviceObj, PIRP pIrp ) {
pIrp->IoStatus.Information = 0;
pIrp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
//===========================================================================
#pragma code_seg()
// DeviceIoControl函数IRP处理
NTSTATUS DispatchControl( PDEVICE_OBJECT pDeviceObj, PIRP pIrp ) {
ULONG Code;
KIRQL OldIrp;
HANDLE hThread\[2\];
KEVENT hEvent\[2\];
HANDLE hUserEvent;
PKEVENT phEvent;
NTSTATUS Status;
PIO_STACK_LOCATION Stack;
//KWAIT_BLOCK Arrblock\[5\];
Stack = IoGetCurrentIrpStackLocation( pIrp );
//获取IO控制码
Code = Stack->Parameters.DeviceIoControl.IoControlCode;
Status = STATUS_UNSUCCESSFUL;
//===========================================================================
//创建多个线程试验
//===========================================================================
if ( Code == IOCTL_CONTROL_CREATETHREAD ) {
//初始化事件对象(初始化为未激发状态)
KeInitializeEvent( &hEvent\[0\], NotificationEvent, FALSE );
KeInitializeEvent( &hEvent\[1\], NotificationEvent, FALSE );
//保证这个代码必须在Dispatch级别运行
ASSERT( KeGetCurrentIrql() <= DISPATCH_LEVEL );
//提升IRP
KeRaiseIrql( DISPATCH_LEVEL, &OldIrp );
//恢复先前的IRP
KeLowerIrql( OldIrp );
//创建线程(参数4指定是用户线程函数系统线程, 很明显这里是系统线程)
Status = PsCreateSystemThread( &hThread\[0\], 0, NULL, NULL, NULL, &SysRoutine, &hEvent\[0\] );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建系统线程失败!\n" ) );
return Status;
}
//创建线程(这里指定的是用户线程)
Status = PsCreateSystemThread( &hThread\[1\], 0, NULL,
NtCurrentProcess(), NULL, &UserRoutine, &hEvent\[1\] );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建系统线程失败!\n" ) );
return Status;
}
//等待事件对象置位(不等待, ThreadRoutine用了栈上的变量, 要崩溃
KeWaitForSingleObject( &hEvent\[0\], Executive, KernelMode, FALSE, NULL );
KeWaitForSingleObject( &hEvent\[1\], Executive, KernelMode, FALSE, NULL );
//等待所有对象位置位才返回
//KeWaitForMultipleObjects(2, (PVOID)hEvent, WaitAll, Executive, \
// KernelMode, FALSE, 0, &Arrblock\[0\] );
//清除同步对象
KeClearEvent( &hEvent\[0\] );
KeClearEvent( &hEvent\[1\] );
Status = STATUS_SUCCESS;
KdPrint( ( "IOCTL_CONTROL_CREATETHREAD 控制码成功执行!\n" ) );
//===========================================================================
// 利用用户层创建的同步对象进行同步
//===========================================================================
} else if ( Code == IOCTL_CONTROL_SHOREEVENT ) {
hUserEvent = *( HANDLE* )pIrp->AssociatedIrp.SystemBuffer;
if ( hUserEvent ) {
//将用户层传入的句柄转换成指针
Status = ObReferenceObjectByHandle( hUserEvent, EVENT_MODIFY_STATE,
*ExEventObjectType, KernelMode, ( PVOID* )&phEvent, NULL );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "转换用户层句柄失败!\n" ) );
} else {
KeSetEvent( phEvent, IO_NO_INCREMENT, FALSE );
KdPrint( ( "成功将用户层传入的句柄转换为指针, 并置位" ) );
}
Status = STATUS_SUCCESS;
} else {
Status = STATUS_UNSUCCESSFUL;
}
KdPrint( ( "IOCTL_CONTROL_SHOREEVENT 控制码成功执行!\n" ) );
}
//---------------------------------------------------------------------------
pIrp->IoStatus.Information = 0;
pIrp->IoStatus.Status = Status;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return Status;
}
//===========================================================================
// 驱动程序的入口
#pragma code_seg( "INIT" )
NTSTATUS DriverEntry( PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pUSzRegPath ) {
ULONG i;
NTSTATUS Status;
PDEVICE_OBJECT pDeviceObj;
PDEVICE_EXT pDeviceExt;
UNICODE_STRING uSzDeviceName = RTL_CONSTANT_STRING( DEVICE_NAME );
UNICODE_STRING uSzSysLinkName = RTL_CONSTANT_STRING( SYS_LINK_NAME );
//创建设备
Status = IoCreateDevice( pDriverObj, sizeof( DEVICE_EXT ), &uSzDeviceName,
FILE_DEVICE_UNKNOWN, 0, TRUE, &pDeviceObj );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建设备失败!\n" ) );
return Status;
}
//创建符号链接
Status = IoCreateSymbolicLink( &uSzSysLinkName, &uSzDeviceName );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建符号链接失败!\n" ) );
return Status;
}
//设置设备属性, 和设备扩展内容
pDeviceExt = ( PDEVICE_EXT )pDeviceObj->DeviceExtension;
pDeviceExt->pDeviceObj = pDeviceObj;
pDeviceExt->uSzDeviceName = uSzDeviceName;
pDeviceExt->uSzSysLinkName = uSzSysLinkName;
pDeviceObj->Flags |= DO_BUFFERED_IO;
for( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++ ) {
pDriverObj->MajorFunction\[i\] = &DispatchRoutine;
}
pDriverObj->DriverUnload = &DriverUnload;
pDriverObj->MajorFunction\[IRP_MJ_DEVICE_CONTROL\] = &DispatchControl;
return Status;
}
|