/
sampleBK.c
360 lines (301 loc) · 6.64 KB
/
sampleBK.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <wdm.h>
#include <Ntstrsafe.h>
#define STDCALL __stdcall
/*
* Undocument function
*/
NTSTATUS STDCALL ZwCreateEvent(
OUT PHANDLE EventHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
IN EVENT_TYPE EventType,
IN BOOLEAN InitialState);
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#endif
SIZE_T NumberOfBytes=512;
PVOID pEvent;
struct Buffer{
int Size;
char* head;
char* tail;
} ;
struct Buffer RingBuffer;
VOID CreateRingBuffer(){
RingBuffer.head=NULL;
RingBuffer.head=ExAllocatePool(
NonPagedPool,
NumberOfBytes
);
if(RingBuffer.head==NULL){
DbgPrint("Memory problems\n");
}else{
DbgPrint("Memory OK\n");
}
RingBuffer.tail=RingBuffer.head;
RingBuffer.Size=NumberOfBytes;
}
PVOID WriteToRingBuffer(char** dataPtr, SIZE_T BytesNum){
if(RingBuffer.tail-RingBuffer.head>=RingBuffer.Size/3){
KeSetEvent(
hEvent,
0,
TRUE);}
RtlCopyMemory(
RingBuffer.head,
dataPtr,
BytesNum
);
RingBuffer.head+=BytesNum;
}
PVOID FlushRingBuffre(){
IO_STATUS_BLOCK ioStatusBlock;
HANDLE handle;
NTSTATUS ntstatus;
NTSTATUS FlushingToFile;
OBJECT_ATTRIBUTES ObjAttr;
UNICODE_STRING pathBuffer;
size_t cb;
PVOID pEvent;
NTSTATUS Status;
HANDLE hEvent;
OBJECT_ATTRIBUTES oa;
UNICODE_STRING us;
__debugbreak();
RtlInitUnicodeString(
&pathBuffer,
L"\\DosDevices\\C:\\MyBufferLogger.txt");
InitializeObjectAttributes(
&ObjAttr,
&pathBuffer, //ObjectName
OBJ_CASE_INSENSITIVE, //Attributes
NULL, //RootDirectory
NULL); //SecurityDescriptor
// Do not try to perform any file operations at higher IRQL levels.
// Instead, you may use a work item or a system worker thread to perform file operations.
if(KeGetCurrentIrql() != PASSIVE_LEVEL)
// return STATUS_INVALID_DEVICE_STATE;
DbgPrint("STATUS_INVALID_DEVICE_STATE\n");
ntstatus = ZwCreateFile(&handle,
GENERIC_WRITE,
&ObjAttr,
&ioStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0);
RtlInitUnicodeString(
&us,
L"\\BaseNamedObjects\\TestEvent");
InitializeObjectAttributes(
&oa,
&us, //ObjectName
OBJ_CASE_INSENSITIVE, //Attributes
NULL, //RootDirectory
NULL); //SecurityDescriptor
FlushingToFile=ZwWriteFile(
handle,
NULL,
NULL,
NULL,
&ioStatusBlock,
RingBuffer.tail,
RingBuffer.head-RingBuffer.tail,
NULL,
NULL
);
if(FlushingToFile!=STATUS_SUCCESS){
DbgPrint("Writing to file problem");
}else{
DbgPrint("I do not know why");
}
ZwClose(handle);
}
NTSTATUS
DllInitialize( IN PUNICODE_STRING pus )
{
__debugbreak();
DbgPrint("SAMPLE: DllInitialize(%S)\n", pus->Buffer );
return STATUS_SUCCESS;
}
NTSTATUS
DllUnload( )
{
__debugbreak();
DbgPrint("SAMPLE: DllUnload\n");
return STATUS_SUCCESS;
}
__declspec(dllexport) NTSTATUS
SampleDouble( int * pValue )
{
DbgPrint("SampleDouble: %d\n", *pValue);
*pValue *= 2;
return STATUS_SUCCESS;
}
__declspec(dllexport) NTSTATUS
SampleReadRegistry( )
{
DbgPrint("SampleReadRegistry\n");
return STATUS_SUCCESS;
}
__declspec(dllexport) NTSTATUS
Klog( )
{
DbgPrint("Call Klog\n");
return STATUS_SUCCESS;
}
VOID
MyThreadStart(__in PVOID StartContext){
HANDLE handle;//Create File handle
NTSTATUS ntstatus;
NTSTATUS timerStatus;
//IO_STATUS_BLOCK ioStatusBlock;
//OBJECT_ATTRIBUTES ObjAttr;
//UNICODE_STRING path;
LARGE_INTEGER timeout;
//#define BUFFER_SIZE 30
// CHAR buffer[BUFFER_SIZE];
//size_t cb;
NTSTATUS Status;
HANDLE hEvent;
//OBJECT_ATTRIBUTES oa;
//UNICODE_STRING us;
int counter=20;
SIZE_T BytesNUM;
char* dataPtr;
__debugbreak();
dataPtr="123";
BytesNUM=strlen(dataPtr)*sizeof(char);
WriteToRingBuffer(&dataPtr, BytesNUM);
FlushRingBuffre();
RtlInitUnicodeString(
&path,
L"\\DosDevices\\C:\\MyLogger.txt");
InitializeObjectAttributes(
&ObjAttr,
&path, //ObjectName
OBJ_CASE_INSENSITIVE, //Attributes
NULL, //RootDirectory
NULL); //SecurityDescriptor
// Do not try to perform any file operations at higher IRQL levels.
// Instead, you may use a work item or a system worker thread to perform file operations.
if(KeGetCurrentIrql() != PASSIVE_LEVEL)
// return STATUS_INVALID_DEVICE_STATE;
DbgPrint("STATUS_INVALID_DEVICE_STATE\n");
ntstatus = ZwCreateFile(&handle,
GENERIC_WRITE,
&ObjAttr, &ioStatusBlock, NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0);
timeout.QuadPart = -5 * 1000000;
RtlInitUnicodeString(
&us,
L"\\BaseNamedObjects\\TestEvent");
InitializeObjectAttributes(
&oa,
&us, //ObjectName
OBJ_CASE_INSENSITIVE, //Attributes
NULL, //RootDirectory
NULL); //SecurityDescriptor
Status = ZwCreateEvent(&hEvent,
EVENT_ALL_ACCESS,
&oa,
NotificationEvent,
FALSE);
if(NT_SUCCESS(Status)){
DbgPrint("Event created");
} else {
DbgPrint("Event Not created");
}
Status = ObReferenceObjectByHandle(
hEvent, //Handle
EVENT_ALL_ACCESS, //DesiredAccess
NULL, //ObjectType
KernelMode, //AccessMode
&pEvent, //Object
NULL); //HandleInformation
if (!NT_SUCCESS(Status)) {
ZwClose(hEvent);
DbgPrint("Failed to reference event \n");
//return Status;
};
while(counter!=0){
timerStatus = KeWaitForSingleObject(
pEvent,
Executive,
KernelMode,
FALSE,
&timeout
);
if(timerStatus == STATUS_TIMEOUT){
if(NT_SUCCESS(ntstatus)) {
ntstatus = RtlStringCbPrintfA(buffer, sizeof(buffer), "This is %d test\r\n", counter);
if(NT_SUCCESS(ntstatus)) {
ntstatus = RtlStringCbLengthA(buffer, sizeof(buffer), &cb);
if(NT_SUCCESS(ntstatus)) {
ntstatus = ZwWriteFile(handle, NULL, NULL, NULL, &ioStatusBlock,
buffer, cb, NULL, NULL);
}
}
//ZwClose(handle);
}
}
counter--;
}
ZwClose(handle);
}
NTSTATUS
KlogInit( )
{
NTSTATUS status;
HANDLE threadHandle;
CreateRingBuffer();
DbgPrint("Call KlogInit\n");
status=PsCreateSystemThread(&threadHandle,
GENERIC_ALL,
NULL,
NULL,
NULL,
MyThreadStart,
NULL);
if(NT_SUCCESS(status)){
DbgPrint("Thread Succes\n");
}
else{
DbgPrint("No thread\n");}
return STATUS_SUCCESS;
}
__declspec(dllexport) VOID
KlogDone( )
{
DbgPrint("Call KlogDone\n");
// return STATUS_SUCCESS;
}
NTSTATUS
KlogTest( )
{
DbgPrint("Call KlogTest\n");
return STATUS_SUCCESS;
}
// The DLL must have an entry point, but it is never called.
//
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
{
__debugbreak();
KlogInit();
return STATUS_SUCCESS;
}