-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.c
175 lines (148 loc) · 3.8 KB
/
Utility.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
#include "Utility.h"
#include "wdmuk.h"
#include "Trace.h"
#include "Utility.tmh"
extern WDM_DRIVER gDriver;
NTSTATUS
WdmQueryFileBasicInfo(
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_opt_ PLARGE_INTEGER ChangeTime
)
{
NTSTATUS status = STATUS_UNSUCCESSFUL;
FILE_BASIC_INFORMATION fileBasicInfo = { 0 };
__try
{
status = FltQueryInformationFile(
Instance,
FileObject,
&fileBasicInfo,
sizeof(fileBasicInfo),
FileBasicInformation,
NULL);
if (!NT_SUCCESS(status))
{
LogErrorNt("FltQueryInformationFile", status);
__leave;
}
status = STATUS_SUCCESS;
}
__finally
{
if (ChangeTime != NULL)
{
if (NT_SUCCESS(status))
{
WdmLargeIntCopy(*ChangeTime, fileBasicInfo.ChangeTime);
}
else
{
RtlZeroMemory(ChangeTime, sizeof(*ChangeTime));
}
}
}
return status;
}
NTSTATUS
WdmGetFniParsed(
_In_ PFLT_CALLBACK_DATA Data,
_Outptr_ PFLT_FILE_NAME_INFORMATION *FileNameInformation
)
{
NTSTATUS retStatus = STATUS_UNSUCCESSFUL;
NTSTATUS status = STATUS_UNSUCCESSFUL;
PFLT_FILE_NAME_INFORMATION fileNameInformation = NULL;
__try
{
status = FltGetFileNameInformation(
Data,
FLT_FILE_NAME_OPENED | FLT_FILE_NAME_QUERY_DEFAULT,
&fileNameInformation);
if (!NT_SUCCESS(status))
{
//LogErrorNt("FltGetFileNameInformation", status);
__leave;
}
status = FltParseFileNameInformation(fileNameInformation);
if (!NT_SUCCESS(status))
{
__leave;
}
retStatus = STATUS_SUCCESS;
}
__finally
{
if (NT_SUCCESS(retStatus))
{
*FileNameInformation = fileNameInformation;
}
else
{
*FileNameInformation = NULL;
}
}
return retStatus;
}
VOID
WdmReleaseFniParsed(
_Inout_ PFLT_FILE_NAME_INFORMATION *FileNameInformation
)
{
ASSERT(*FileNameInformation != NULL);
FltReleaseFileNameInformation(*FileNameInformation);
*FileNameInformation = NULL;
return;
}
NTSTATUS
WdmScanFile(
_In_ UNICODE_STRING FilePath,
_Out_ PLONG FileState
)
{
NTSTATUS status = STATUS_UNSUCCESSFUL;
ULONG repLength = 0;
NOTIFICATION_REPLY repData = { 0 };
NOTIFICATION msgData = { 0 };
__try
{
repLength = sizeof(FILTER_REPLY_HEADER) + sizeof(NOTIFICATION_REPLY);
msgData.CommandCode = cmdNotification;
RtlCopyMemory(msgData.FilePath, FilePath.Buffer, FilePath.Length + 1);
msgData.FilePath[FilePath.Length + 1] = '\0';
status = FltSendMessage(
gDriver.Filter,
&gDriver.ClientPort,
&msgData,
sizeof(NOTIFICATION),
&repData,
&repLength,
NULL);
if (!NT_SUCCESS(status))
{
LogErrorNt("FltSendMessage", status);
__leave;
}
status = STATUS_SUCCESS;
}
__finally
{
if (NT_SUCCESS(status))
{
if (repData.BlockCreate)
{
InterlockedExchange(FileState, WdmFileFuckedUp);
}
else
{
InterlockedExchange(FileState, WdmFileClean);
}
}
else
{
// scan failed, we cannot trust this file
InterlockedExchange(FileState, WdmFileFuckedUp);
}
}
return status;
}