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
|
/*
内核下的定时器函数, 和一系列其他函数的使用 0环代码
TAB = 8
*/
#include <ntddk.h>
#define TIMER_OUT 10
typedef struct tagDevice_Ext {
ULONG ulTimeOut;
PKEVENT phEvent; //事件对象指针
KTIMER StTimer; //Dpc计时器对象
KDPC StDpc; //Dpc对象
LARGE_INTEGER liInterger; //Dpc间隔时间
} DEVICE_EXT, *PDEVICE_EXT;
//===========================================================================
//内核线程函数1
//===========================================================================
#pragma code_seg( "PAGE" )
VOID SysRoutine1( PVOID pContext ) {
PEPROCESS pEprocess = NULL;
PTSTR pStrProcessName = NULL;
PAGED_CODE();
//获取进程名称
pEprocess = IoGetCurrentProcess();
pStrProcessName = ( PTSTR )( ( ULONG )pEprocess + 0x174 );
KdPrint( ( "进程名称:%s\n", pStrProcessName ) );
//停止50毫秒,我倒, 我改成5000都没有效果
KeStallExecutionProcessor( 50 );
PsTerminateSystemThread( STATUS_SUCCESS );
}
//===========================================================================
//打印进程和时间信息
//===========================================================================
VOID PrintInfo() {
TIME_FIELDS StTime;
PEPROCESS pEprocess = NULL;
PTSTR pStrProcessName = NULL;
LARGE_INTEGER liSysTime;
LARGE_INTEGER liLocalTime;
//获取进程名称
pEprocess = IoGetCurrentProcess();
pStrProcessName = ( PTSTR )( ( ULONG )pEprocess + 0x174 );
KdPrint( ( "进程名称:%s\n", pStrProcessName ) );
//得到当前系统时间
KeQuerySystemTime( &liSysTime );
//从系统时间转换成当地时区时间
ExSystemTimeToLocalTime( &liSysTime, &liLocalTime );
//由当地时区时间得到月日年信息
RtlTimeToTimeFields( &liLocalTime, &StTime );
//显示年月日等信息
KdPrint( ( "年%d/", StTime.Year ) );
KdPrint( ( "月%d/", StTime.Month ) );
KdPrint( ( "日%d/", StTime.Day ) );
KdPrint( ( "星期%d\n", StTime.Weekday ) );
KdPrint( ( "%d:", StTime.Hour ) );
KdPrint( ( "%d:", StTime.Minute ) );
KdPrint( ( "%d:", StTime.Second ) );
KdPrint( ( "%d\n\n", StTime.Milliseconds ) );
}
//===========================================================================
//定时器过程
//===========================================================================
#pragma code_seg()
VOID TimerRoutine( PDEVICE_OBJECT pDeviceObj, PVOID pContext ) {
PDEVICE_EXT pDeviceExt = NULL;
PAGED_CODE_LOCKED();
PrintInfo();
//---------------------------------------------------------------------------
pDeviceExt = pDeviceObj->DeviceExtension;
ASSERT( pDeviceExt != NULL );
if ( pDeviceExt->ulTimeOut == 0 ) {
KeSetEvent( pDeviceExt->phEvent, IO_NO_INCREMENT, FALSE );
} else {
//原子--
InterlockedDecrement( &pDeviceExt->ulTimeOut );
}
}
//===========================================================================
//Dpc例程(Dpc例程只能搞一次, 所以每次在后面还要设置一次)
//===========================================================================
#pragma code_seg( )
VOID DpcRoutine( PKDPC pDpc, PVOID pContext, PVOID SysArg1, PVOID SysArg2 ) {
PDEVICE_EXT pDeviceExt = NULL;
PAGED_CODE_LOCKED();
PrintInfo();
pDeviceExt = ( ( PDEVICE_OBJECT )pContext )->DeviceExtension;
ASSERT( pDeviceExt != NULL );
if ( pDeviceExt->ulTimeOut == 0 ) {
KeSetEvent( pDeviceExt->phEvent, IO_NO_INCREMENT, FALSE );
} else {
//开启Dpc例程
KeSetTimer( &pDeviceExt->StTimer, pDeviceExt->liInterger,
&pDeviceExt->StDpc );
//原子--
InterlockedDecrement( &pDeviceExt->ulTimeOut );
}
}
//===========================================================================
//驱动入口
//===========================================================================
#pragma code_seg( "INIT" )
NTSTATUS DriverEntry( PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pUSzRegPath ) {
ULONG* pUlTime = NULL;
HANDLE hThread;
NTSTATUS Status;
KEVENT hEvent;
PETHREAD pThread = NULL;
PDEVICE_OBJECT pDevice = NULL;
PDEVICE_EXT pDeviceExt = NULL;
//---------------------------------------------------------------------------
//创建一个线程然后等待, 知道线程对象返回
//---------------------------------------------------------------------------
//创建线程(参数4指定是用户线程函数系统线程, 很明显这里是系统线程)
Status = PsCreateSystemThread( &hThread, 0, NULL, NULL, NULL, &SysRoutine1, NULL );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建线程失败!\n" ) );
return Status;
}
//将线程句柄转化为指针
Status = ObReferenceObjectByHandle( hThread, 0, *PsThreadType,
KernelMode, ( PVOID* )&pThread, NULL );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "线程句柄转换指针失败!\n" ) );
return Status;
}
//等待线程对象(创建线程返回的是句柄, 所以需要转成指针)
KeWaitForSingleObject( pThread, Executive, KernelMode, FALSE, NULL );
//这号函数也是可以等待的, 句柄.
//ZwWaitForSingleObject( hThread, TRUE, 0 );
//---------------------------------------------------------------------------
//创建一个定时器1s一次
//---------------------------------------------------------------------------
KeInitializeEvent( &hEvent, NotificationEvent, FALSE );
Status = IoCreateDevice( pDriverObj, sizeof( DEVICE_EXT ), NULL,
FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevice );
if ( !NT_SUCCESS( Status ) ) {
KdPrint( ( "创建设备失败!\n" ) );
return Status;
}
ASSERT( pDevice != NULL );
pDeviceExt = pDevice->DeviceExtension;
pDeviceExt->phEvent = &hEvent;
pDeviceExt->ulTimeOut = TIMER_OUT;
//初始化定时器对象和启动定时器
IoInitializeTimer( pDevice, &TimerRoutine, NULL );
IoStartTimer( pDevice );
//等待事件对象置位
KeWaitForSingleObject( &hEvent, Executive, KernelMode, FALSE, NULL );
//停止定时器
IoStopTimer( pDevice );
//---------------------------------------------------------------------------
//Dpc例程试验,
//---------------------------------------------------------------------------
pDeviceExt->ulTimeOut = TIMER_OUT;
//负数表示间隔多少时间, 单位是ns 1000ns=1ms, 1000 ms = 1s
pDeviceExt->liInterger = RtlConvertLongToLargeInteger( 1000 * 1000 * 2 * -10 );
//初始化计时器对象
KeInitializeTimer( &pDeviceExt->StTimer );
//初始化DPC对象
KeInitializeDpc( &pDeviceExt->StDpc, &DpcRoutine, pDevice );
//开启Dpc例程
KeSetTimer( &pDeviceExt->StTimer, pDeviceExt->liInterger, &pDeviceExt->StDpc );
KeResetEvent( &hEvent );
//等待事件对象置位
KeWaitForSingleObject( &hEvent, Executive, KernelMode, FALSE, NULL );
//取消定时器和事件对象
KeCancelTimer( &pDeviceExt->StTimer );
KeClearEvent( &hEvent );
//---------------------------------------------------------------------------
if ( pDevice ) {
IoDeleteDevice( pDevice );
}
KdPrint( ( "DriverEntry调用完毕!\n" ) );
return STATUS_UNSUCCESSFUL;
}
|