-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathWorkItem.bat
226 lines (153 loc) · 7.05 KB
/
WorkItem.bat
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
;@echo off
;goto make
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;
; WorkItem - How to use a work item.
;
; Written by Four-F ([email protected])
;
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.386
.model flat, stdcall
option casemap:none
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; I N C L U D E F I L E S
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
include \masm32\include\w2k\ntstatus.inc
include \masm32\include\w2k\ntddk.inc
include \masm32\include\w2k\ntoskrnl.inc
includelib \masm32\lib\w2k\ntoskrnl.lib
include \masm32\Macros\Strings.mac
IOCTL_WORK equ CTL_CODE(FILE_DEVICE_UNKNOWN, 800h, METHOD_BUFFERED, 0)
WORK STRUCT
pIoWorkItem PVOID ? ; PIO_WORKITEM
nWorkNumber DWORD ?
WORK ENDS
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; C O N S T A N T S
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.const
CCOUNTED_UNICODE_STRING "\\Device\\WorkItem", g_usDeviceName, 4
CCOUNTED_UNICODE_STRING "\\DosDevices\\WorkItem", g_usSymbolicLinkName, 4
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; U N I N I T I A L I Z E D D A T A
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.data?
g_fTimerStarted BOOL ?
g_nWorkToDo DWORD ?
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; N O N D I S C A R D A B L E C O D E
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.code
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; WorkItemRoutine
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
WorkItemRoutine proc uses esi pDeviceObject:PDEVICE_OBJECT, pContext:PVOID
mov esi, pContext
assume esi:ptr WORK
invoke DbgPrint, $CTA0("WorkItem: Work #%d is done\n"), [esi].nWorkNumber
invoke IoFreeWorkItem, [esi].pIoWorkItem
assume esi:nothing
invoke ExFreePool, esi
ret
WorkItemRoutine endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; TimerRoutine
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
TimerRoutine proc uses esi pDeviceObject:PDEVICE_OBJECT, pContext:PVOID
; This routine is called at IRQL DISPATCH_LEVEL !
local pIoWorkItem:PVOID ; PIO_WORKITEM
.if g_nWorkToDo != 0
invoke IoAllocateWorkItem, pDeviceObject
.if eax != NULL
mov pIoWorkItem, eax
; A caller executing at DISPATCH_LEVEL must specify a NonPagedXxx value for PoolType.
invoke ExAllocatePool, NonPagedPool, sizeof WORK
.if eax != NULL
mov esi, eax
assume esi:ptr WORK
mov eax, pIoWorkItem
mov [esi].pIoWorkItem, eax
mov eax, g_nWorkToDo
mov [esi].nWorkNumber, eax
assume esi:nothing
invoke IoQueueWorkItem, pIoWorkItem, offset WorkItemRoutine, DelayedWorkQueue, esi
dec g_nWorkToDo
.endif
.endif
.else
invoke IoStopTimer, pDeviceObject
and g_fTimerStarted, FALSE
.endif
ret
TimerRoutine endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; DriverUnload
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
DriverUnload proc pDriverObject:PDRIVER_OBJECT
invoke IoDeleteSymbolicLink, addr g_usSymbolicLinkName
.if g_fTimerStarted
mov eax, pDriverObject
invoke IoStopTimer, (DRIVER_OBJECT PTR [eax]).DeviceObject
and g_fTimerStarted, FALSE
invoke DbgPrint, $CTA0("WorkItem: Timer stopped\n")
.endif
mov eax, pDriverObject
invoke IoDeleteDevice, (DRIVER_OBJECT PTR [eax]).DeviceObject
ret
DriverUnload endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; D I S C A R D A B L E C O D E
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.code INIT
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; DriverEntry
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
DriverEntry proc pDriverObject:PDRIVER_OBJECT, pusRegistryPath:PUNICODE_STRING
local status:NTSTATUS
local pDeviceObject:PDEVICE_OBJECT
mov status, STATUS_DEVICE_CONFIGURATION_ERROR
; Explicity initialize global variables
and g_fTimerStarted, FALSE
and g_nWorkToDo, 0
; Create exclusive device
invoke IoCreateDevice, pDriverObject, 0, addr g_usDeviceName, FILE_DEVICE_UNKNOWN, 0, TRUE, addr pDeviceObject
.if eax == STATUS_SUCCESS
invoke IoCreateSymbolicLink, addr g_usSymbolicLinkName, addr g_usDeviceName
.if eax == STATUS_SUCCESS
mov eax, pDriverObject
assume eax:ptr DRIVER_OBJECT
mov [eax].DriverUnload, offset DriverUnload
assume eax:nothing
invoke IoInitializeTimer, pDeviceObject, TimerRoutine, NULL
.if eax == STATUS_SUCCESS
mov g_nWorkToDo, 5 ; Number of jobs to do
; Our TimerRoutine routine will be called once per second.
invoke IoStartTimer, pDeviceObject
mov g_fTimerStarted, TRUE
invoke DbgPrint, $CTA0("WorkItem: Timer started\n")
mov status, STATUS_SUCCESS
.else
invoke DbgPrint, $CTA0("WorkItem: Couldn't initialize timer. Status: %08X\n"), eax
invoke IoDeleteSymbolicLink, addr g_usSymbolicLinkName
invoke IoDeleteDevice, pDeviceObject
.endif
.else
invoke DbgPrint, $CTA0("WorkItem: Couldn't create device. Status: %08X\n"), eax
invoke IoDeleteDevice, pDeviceObject
.endif
.endif
mov eax, status
ret
DriverEntry endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
end DriverEntry
:make
set drv=WorkItem
\masm32\bin\ml /nologo /c /coff %drv%.bat
\masm32\bin\link /nologo /driver /base:0x10000 /align:32 /out:%drv%.sys /subsystem:native /ignore:4078 %drv%.obj
del %drv%.obj
echo.
pause