-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
245 lines (189 loc) · 5.49 KB
/
logger.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
#include <definitions.h>
#include "intrinsics.h"
#include "runtime.h"
#include "logger.h"
UINT32 LogQueueLock;
UINT32 LogFlushTime;
PVOID LogPushPoolBuffer;
PVOID LogPullPoolBuffer;
UINT32 LogPushPoolLength;
UINT32 LogPullPoolLength;
UINT32 LogQueuerState;
PETHREAD LogQueuerThread;
VOID NTAPI
LogSyncPrint (
IN PCSTR Format,
IN ...
)
{
va_list ArgList;
va_start(ArgList, Format);
vDbgPrintExWithPrefix("[mirror]\t",
DPFLTR_IHVDRIVER_ID,
DPFLTR_ERROR_LEVEL,
Format,
ArgList);
va_end(ArgList);
}
VOID NTAPI
LogSendMessage (
IN PVOID Buffer,
IN UINT32 Length,
IN UINT32 Format
)
{
PMESSAGE_HEADER Message = NULL;
KIRQL Irql;
RtAcquireSpinLock(&LogQueueLock, &Irql);
if (NULL != LogPushPoolBuffer) {
if (LogPushPoolLength < MSG_POOL_MAX_LENGTH) {
if ((Length + MESSAGE_HEADER_LENGTH) <= (MSG_POOL_MAX_LENGTH - LogPushPoolLength)) {
Message = PoolToMessage(LogPushPoolBuffer, LogPushPoolLength);
Message->Format = Format;
Message->Length = Length;
RtlCopyMemory(Message->Body, Buffer, Length);
LogPushPoolLength += GetMessageFullLength(Message);
}
}
}
RtReleaseSpinLock(&LogQueueLock, Irql);
}
VOID NTAPI
LogPrint (
IN PCSTR Format,
IN ...
)
{
va_list ArgList;
CHAR Buffer[512];
UINT Length;
va_start(ArgList, Format);
Length = _vsnprintf(Buffer, sizeof(Buffer), Format, ArgList);
LogSendMessage(Buffer, Length + 1, MSG_FORMAT_ANSI);
va_end(ArgList);
}
VOID NTAPI
LogMsgPrintCallback (
IN PMESSAGE_HEADER Message
)
{
switch (Message->Format) {
case MSG_FORMAT_ANSI:
LogSyncPrint("%s", Message->Body);
break;
case MSG_FORMAT_UNICODE:
LogSyncPrint("%ws", Message->Body);
break;
}
}
VOID NTAPI
LogMsgHandler (
IN PVOID PoolBuffer,
IN UINT32 PoolLength
)
{
PMESSAGE_HEADER Message = NULL;
Message = LogPullPoolBuffer;
while (0 != LogPullPoolLength) {
LogMsgPrintCallback(Message);
LogPullPoolLength -= GetMessageFullLength(Message);
Message = GetNextMessage(Message);
}
}
VOID NTAPI
LogMsgQueuer (
IN PVOID StartContext
)
{
LARGE_INTEGER Interval;
KIRQL Irql;
while (LogQueuerState != LOGGER_STATE_STOPED) {
InterlockedCompareExchange(&LogQueuerState,
LOGGER_STATE_STOPED,
LOGGER_STATE_STOPPING);
RtAcquireSpinLock(&LogQueueLock, &Irql);
LogPullPoolLength = LogPushPoolLength;
LogPushPoolLength = 0;
RtlCopyMemory(LogPullPoolBuffer,
LogPushPoolBuffer,
LogPullPoolLength);
RtReleaseSpinLock(&LogQueueLock, Irql);
LogMsgHandler(LogPullPoolBuffer, LogPullPoolLength);
Interval.QuadPart = Int32x32To64(LogFlushTime, -10 * 1000);
KeDelayExecutionThread(KernelMode, TRUE, &Interval);
}
PsTerminateSystemThread(STATUS_SUCCESS);
}
VOID NTAPI
LogUninitialize (
VOID
)
{
NTSTATUS Status;
KIRQL Irql;
PVOID PushPoolBuffer;
PVOID PullPoolBuffer;
InterlockedExchange(&LogQueuerState, LOGGER_STATE_STOPPING);
KeAlertThread(LogQueuerThread, KernelMode);
Status = KeWaitForSingleObject(LogQueuerThread,
Executive,
KernelMode,
FALSE,
NULL);
ObDereferenceObject(LogQueuerThread);
LogQueuerThread = NULL;
RtAcquireSpinLock(&LogQueueLock, &Irql);
PushPoolBuffer = LogPushPoolBuffer;
PullPoolBuffer = LogPullPoolBuffer;
LogPushPoolBuffer = NULL;
LogPullPoolBuffer = NULL;
RtReleaseSpinLock(&LogQueueLock, Irql);
RtFreeMemory(PushPoolBuffer);
RtFreeMemory(PullPoolBuffer);
}
NTSTATUS NTAPI
LogInitialize (
IN UINT32 FlushTime
)
{
NTSTATUS Status;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE ThreadHandle;
LogQueuerState = LOGGER_STATE_RUNING;
LogPushPoolLength = 0;
LogPullPoolLength = 0;
LogFlushTime = FlushTime;
RtInitializeSpinLock(&LogQueueLock);
LogPushPoolBuffer = RtAllocateMemory(MSG_POOL_MAX_LENGTH);
if (NULL == LogPushPoolBuffer) {
return STATUS_INSUFFICIENT_RESOURCES;
}
LogPullPoolBuffer = RtAllocateMemory(MSG_POOL_MAX_LENGTH);
if (NULL == LogPullPoolBuffer) {
RtFreeMemory(LogPushPoolBuffer);
return STATUS_INSUFFICIENT_RESOURCES;
}
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
Status = PsCreateSystemThread(&ThreadHandle,
THREAD_ALL_ACCESS,
&ObjectAttributes,
NULL,
NULL,
LogMsgQueuer,
NULL);
if (FALSE == NT_SUCCESS(Status)) {
RtFreeMemory(LogPushPoolBuffer);
RtFreeMemory(LogPullPoolBuffer);
LogPushPoolBuffer = NULL;
LogPullPoolBuffer = NULL;
return Status;
}
ObReferenceObjectByHandle(ThreadHandle,
SYNCHRONIZE,
NULL,
KernelMode,
&LogQueuerThread,
NULL);
ZwClose(ThreadHandle);
return STATUS_SUCCESS;
}