forked from JKornev/hidden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigs.c
294 lines (233 loc) · 7.48 KB
/
Configs.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
#include "Configs.h"
#include "Helper.h"
#define CONFIG_ALLOC_TAG 'gfnC'
typedef struct _HidConfigContext {
BOOLEAN state;
BOOLEAN stealth;
UNICODE_STRING hideFSDirs;
UNICODE_STRING hideFSFiles;
UNICODE_STRING hideRegKeys;
UNICODE_STRING hideRegValues;
UNICODE_STRING ignoreImages;
UNICODE_STRING protectImages;
UNICODE_STRING hideImages;
} HidConfigContext, *PHidConfigContext;
PHidConfigContext g_configContext = NULL;
VOID ReleaseConfigContext(PHidConfigContext context);
NTSTATUS GetRegistryDWORD(HANDLE hKey, LPCWSTR Value, PULONG Data, ULONG Default);
NTSTATUS QueryAndAllocRegistryData(HANDLE hKey, LPCWSTR Value, ULONG Type, PUNICODE_STRING Data, PUNICODE_STRING Default);
VOID ReleaseRegistryData(PUNICODE_STRING Data);
// =========================================================================================
NTSTATUS InitializeConfigs(PUNICODE_STRING RegistryPath)
{
HidConfigContext config;
OBJECT_ATTRIBUTES attribs;
NTSTATUS status;
HANDLE hkey;
ULONG value;
if (g_configContext)
return STATUS_ALREADY_REGISTERED;
RtlZeroMemory(&config, sizeof(config));
InitializeObjectAttributes(&attribs, RegistryPath, 0, NULL, NULL);
status = ZwOpenKey(&hkey, KEY_ALL_ACCESS, &attribs);
if (!NT_SUCCESS(status))
{
LogError("Error, can't open config registry key, code:%08x", status);
return status;
}
GetRegistryDWORD(hkey, L"Hid_State", &value, 1);
config.state = (value ? TRUE : FALSE);
GetRegistryDWORD(hkey, L"Hid_StealthMode", &value, 0);
config.stealth = (value ? TRUE : FALSE);
QueryAndAllocRegistryData(hkey, L"Hid_HideFsDirs", REG_MULTI_SZ, &config.hideFSDirs, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_HideFsFiles", REG_MULTI_SZ, &config.hideFSFiles, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_HideRegKeys", REG_MULTI_SZ, &config.hideRegKeys, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_HideRegValues", REG_MULTI_SZ, &config.hideRegValues, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_IgnoredImages", REG_MULTI_SZ, &config.ignoreImages, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_ProtectedImages", REG_MULTI_SZ, &config.protectImages, NULL);
QueryAndAllocRegistryData(hkey, L"Hid_HideImages", REG_MULTI_SZ, &config.hideImages, NULL);
ZwClose(hkey);
g_configContext = (PHidConfigContext)ExAllocatePoolWithTag(NonPagedPool, sizeof(config), CONFIG_ALLOC_TAG);
if (!g_configContext)
{
LogError("Error, can't allocate memory for the config context");
ReleaseConfigContext(&config);
return STATUS_NO_MEMORY;
}
RtlCopyMemory(g_configContext, &config, sizeof(config));
LogTrace("Config is initialized");
return STATUS_SUCCESS;
}
NTSTATUS DestroyConfigs()
{
if (!g_configContext)
return STATUS_NOT_FOUND;
ReleaseConfigContext(g_configContext);
ExFreePoolWithTag(g_configContext, CONFIG_ALLOC_TAG);
LogTrace("Config is destroyed");
return STATUS_SUCCESS;
}
// =========================================================================================
BOOLEAN CfgGetDriverState()
{
if (!g_configContext)
return TRUE; // Enable by default
return g_configContext->state;
}
BOOLEAN CfgGetStealthState()
{
if (!g_configContext)
return FALSE; // Disable by default
return g_configContext->stealth;
}
NTSTATUS CfgEnumConfigsTable(enum CfgMultiStringTables Table, CfgMultiStringCallback Callback, PVOID Params)
{
PUNICODE_STRING table;
LPWSTR buffer;
ULONG length;
if (!g_configContext)
return STATUS_NOT_FOUND;
switch (Table)
{
case HideFilesTable:
table = &g_configContext->hideFSFiles;
break;
case HideDirsTable:
table = &g_configContext->hideFSDirs;
break;
case HideRegKeysTable:
table = &g_configContext->hideRegKeys;
break;
case HideRegValuesTable:
table = &g_configContext->hideRegValues;
break;
case IgnoreImagesTable:
table = &g_configContext->ignoreImages;
break;
case ProtectImagesTable:
table = &g_configContext->protectImages;
break;
case HideImagesTable:
table = &g_configContext->hideImages;
break;
default:
return STATUS_INVALID_VARIANT;
}
if (table->Length == 0)
return STATUS_SUCCESS;
buffer = table->Buffer;
length = table->Length;
while (length > 1)
{
UNICODE_STRING entry;
ULONG inx, delta = 0;
for (inx = 0; inx < length / sizeof(WCHAR); inx++)
{
if (buffer[inx] == L'\0')
{
delta = 1;
break;
}
}
entry.Buffer = buffer;
entry.Length = (USHORT)(inx * sizeof(WCHAR));
entry.MaximumLength = entry.Length;
length -= (inx + delta) * sizeof(WCHAR);
buffer += (inx + delta);
if (entry.Length)
Callback(&entry, Params);
}
return STATUS_SUCCESS;
}
// =========================================================================================
VOID ReleaseConfigContext(PHidConfigContext context)
{
ReleaseRegistryData(&context->hideFSDirs);
ReleaseRegistryData(&context->hideFSFiles);
ReleaseRegistryData(&context->hideRegKeys);
ReleaseRegistryData(&context->hideRegValues);
ReleaseRegistryData(&context->ignoreImages);
ReleaseRegistryData(&context->protectImages);
ReleaseRegistryData(&context->hideImages);
}
NTSTATUS GetRegistryDWORD(HANDLE hKey, LPCWSTR Value, PULONG Data, ULONG Default)
{
UCHAR buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
NTSTATUS status;
UNICODE_STRING valueName;
ULONG length;
RtlInitUnicodeString(&valueName, Value);
status = ZwQueryValueKey(hKey, &valueName, KeyValuePartialInformation, buffer, sizeof(buffer), &length);
if (NT_SUCCESS(status) && length <= sizeof(buffer))
{
PKEY_VALUE_PARTIAL_INFORMATION info = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
if (info->Type == REG_DWORD && info->DataLength == sizeof(ULONG))
*Data = *(ULONG*)(info->Data);
else
*Data = Default;
}
else
{
*Data = Default;
}
return STATUS_SUCCESS;
}
NTSTATUS QueryAndAllocRegistryData(HANDLE hKey, LPCWSTR Value, ULONG Type, PUNICODE_STRING Data, PUNICODE_STRING Default)
{
PKEY_VALUE_PARTIAL_INFORMATION info = NULL;
UNICODE_STRING valueName;
ULONG length, dataLength;
NTSTATUS status;
PVOID dataBuffer;
if (Default)
{
dataLength = Default->Length;
dataBuffer = ExAllocatePoolWithTag(NonPagedPool, dataLength, CONFIG_ALLOC_TAG);
if (!dataBuffer)
return STATUS_NO_MEMORY;
RtlCopyMemory(dataBuffer, Default->Buffer, dataLength);
}
else
{
dataLength = 0;
dataBuffer = NULL;
}
RtlInitUnicodeString(&valueName, Value);
status = ZwQueryValueKey(hKey, &valueName, KeyValuePartialInformation, NULL, 0, &length);
if (status != STATUS_BUFFER_OVERFLOW && status != STATUS_BUFFER_TOO_SMALL)
goto end_proc;
if (length < sizeof(KEY_VALUE_PARTIAL_INFORMATION))
goto end_proc;
info = (PKEY_VALUE_PARTIAL_INFORMATION)ExAllocatePoolWithTag(NonPagedPool, length, CONFIG_ALLOC_TAG);
if (!info)
goto end_proc;
status = ZwQueryValueKey(hKey, &valueName, KeyValuePartialInformation, info, length, &length);
if (!NT_SUCCESS(status))
goto end_proc;
if (info->Type != Type)
goto end_proc;
if (info->DataLength == 0 || info->DataLength > 0xFFFF)
goto end_proc;
if (dataBuffer)
ExFreePoolWithTag(dataBuffer, CONFIG_ALLOC_TAG);
dataLength = info->DataLength;
dataBuffer = ExAllocatePoolWithTag(NonPagedPool, dataLength, CONFIG_ALLOC_TAG);
if (!dataBuffer)
{
ExFreePoolWithTag(info, CONFIG_ALLOC_TAG);
return STATUS_NO_MEMORY;
}
RtlCopyMemory(dataBuffer, info->Data, dataLength);
end_proc:
if (info)
ExFreePoolWithTag(info, CONFIG_ALLOC_TAG);
Data->Buffer = (PWCH)dataBuffer;
Data->Length = (USHORT)dataLength;
Data->MaximumLength = (USHORT)dataLength;
return STATUS_SUCCESS;
}
VOID ReleaseRegistryData(PUNICODE_STRING Data)
{
if (Data->Length)
ExFreePoolWithTag(Data->Buffer, CONFIG_ALLOC_TAG);
}