-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
282 lines (222 loc) · 8.88 KB
/
main.cpp
File metadata and controls
282 lines (222 loc) · 8.88 KB
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
#include <Windows.h>
#include <stdio.h>
#include <string>
#include <filesystem>
#include <format>
#include "../../bindfltapi.h"
#include "util.h"
const std::wstring g_TestingRootPath = L"D:\\vfs-testing-root";
decltype(&BfSetupFilter) PfnBfSetupFilter;
decltype(&BfRemoveMapping) PfnBfRemoveMapping;
decltype(&BfGetMappings) PfnBfGetMappings;
bool TestLoadImports()
{
// Expected from Win10 RS6 or newer
const auto bindfltModule = LoadLibraryW(L"bindfltapi.dll");
PfnBfSetupFilter = reinterpret_cast<decltype(&BfSetupFilter)>(GetProcAddress(bindfltModule, "BfSetupFilter"));
PfnBfRemoveMapping = reinterpret_cast<decltype(&BfRemoveMapping)>(GetProcAddress(bindfltModule, "BfRemoveMapping"));
PfnBfGetMappings = reinterpret_cast<decltype(&BfGetMappings)>(GetProcAddress(bindfltModule, "BfGetMappings"));
if (!bindfltModule || !PfnBfSetupFilter || !PfnBfRemoveMapping || !PfnBfGetMappings)
{
printf("Failed to load bindfltapi DLL: %u\n", GetLastError());
return false;
}
return true;
}
bool TestDumpMappings(HANDLE JobHandle)
{
printf("Dumping bindflt mappings for job 0x%p...\n\n", JobHandle);
ULONG bufferSize = 128 * 1024;
auto buffer = reinterpret_cast<uintptr_t>(_alloca(bufferSize));
const auto hr = PfnBfGetMappings(
JobHandle ? BINDFLT_GET_MAPPINGS_FLAG_SILO : BINDFLT_GET_MAPPINGS_FLAG_VOLUME, // Mutually exclusive flags
JobHandle,
JobHandle ? nullptr : g_TestingRootPath.c_str(), // Default to any on drive D:
nullptr,
&bufferSize,
reinterpret_cast<void *>(buffer));
const auto info = reinterpret_cast<const PBINDFLT_GET_MAPPINGS_INFO>(buffer);
if (FAILED(hr) || info->Status != 0)
{
printf("BfGetMappings failed: 0x%X (0x%X)\n", hr, info->Status);
return false;
}
for (uint32_t i = 0; i < info->MappingCount; i++)
{
auto& mapping = info->Entries[i];
std::wstring_view virtRoot(
reinterpret_cast<const wchar_t *>(buffer + mapping.VirtRootOffset),
mapping.VirtRootLength / sizeof(wchar_t));
printf("Mapping %u:\n", i);
printf("\tVirtRoot: %.*S\n", static_cast<uint32_t>(virtRoot.length()), virtRoot.data());
printf("\tFlags: 0x%X\n", mapping.Flags);
for (uint32_t j = 0; j < mapping.NumberOfTargets; j++)
{
auto target = reinterpret_cast<const PBINDFLT_GET_MAPPINGS_TARGET_ENTRY>(
buffer + mapping.TargetEntriesOffset + (j * sizeof(BINDFLT_GET_MAPPINGS_TARGET_ENTRY)));
std::wstring_view targetRoot(
reinterpret_cast<const wchar_t *>(buffer + target->TargetRootOffset),
target->TargetRootLength / sizeof(wchar_t));
printf("\tTarget %u:\n", j);
printf("\t\tTargetRoot: %.*S\n", static_cast<uint32_t>(targetRoot.length()), targetRoot.data());
}
printf("\n");
}
return true;
}
bool TestMergeMultipleDirectories(HANDLE JobHandle)
{
printf("Mapping multiple directories for job 0x%p...\n\n", JobHandle);
const std::wstring testingRoot = g_TestingRootPath;
PfnBfRemoveMapping(JobHandle, std::format(L"{}\\virtualfinal", testingRoot).c_str());
PfnBfRemoveMapping(JobHandle, std::format(L"{}\\aliased_to_virtualfinal", testingRoot).c_str());
std::filesystem::create_directories(std::format(L"{}\\physical1\\", testingRoot));
std::filesystem::create_directories(std::format(L"{}\\physical2\\", testingRoot));
std::filesystem::create_directories(std::format(L"{}\\physical3\\", testingRoot));
fclose(_wfopen(std::format(L"{}\\physical1\\file1.txt", testingRoot).c_str(), L"ab+"));
fclose(_wfopen(std::format(L"{}\\physical2\\file2.txt", testingRoot).c_str(), L"ab+"));
fclose(_wfopen(std::format(L"{}\\physical3\\file3.txt", testingRoot).c_str(), L"ab+"));
auto hr = S_OK;
hr |= PfnBfSetupFilter(
JobHandle,
BINDFLT_FLAG_READ_ONLY_MAPPING | BINDFLT_FLAG_MERGED_BIND_MAPPING, // readonly isn't actually readonly in merge mode
std::format(L"{}\\virtualfinal", testingRoot).c_str(),
std::format(L"{}\\physical1", testingRoot).c_str(),
nullptr,
0);
hr |= PfnBfSetupFilter(
JobHandle,
BINDFLT_FLAG_READ_ONLY_MAPPING | BINDFLT_FLAG_MERGED_BIND_MAPPING,
std::format(L"{}\\virtualfinal", testingRoot).c_str(),
std::format(L"{}\\physical2", testingRoot).c_str(),
nullptr,
0);
hr |= PfnBfSetupFilter(
JobHandle,
BINDFLT_FLAG_READ_ONLY_MAPPING | BINDFLT_FLAG_MERGED_BIND_MAPPING,
std::format(L"{}\\virtualfinal", testingRoot).c_str(),
std::format(L"{}\\physical3", testingRoot).c_str(),
nullptr,
0);
hr |= PfnBfSetupFilter(
JobHandle,
BINDFLT_FLAG_READ_ONLY_MAPPING | BINDFLT_FLAG_MERGED_BIND_MAPPING,
std::format(L"{}\\aliased_to_virtualfinal", testingRoot).c_str(),
std::format(L"{}\\virtualfinal", testingRoot).c_str(), // this ends up pointing to physical3 for whatever reason. wrong.
nullptr,
0);
if (FAILED(hr))
{
printf("BfSetupFilter failed: 0x%X\n", hr);
return false;
}
return true;
}
bool TestMapFileSourceToDest(HANDLE JobHandle)
{
printf("Mapping virtual file for job 0x%p...\n\n", JobHandle);
const std::wstring testingRoot = g_TestingRootPath;
const auto sourcePath = std::format(L"{}\\physical_file_1.txt", testingRoot);
const auto destPath = std::format(L"{}\\virtual_file_1.txt", testingRoot);
fclose(_wfopen(sourcePath.c_str(), L"ab+"));
auto hr = S_OK;
hr = PfnBfRemoveMapping(JobHandle, destPath.c_str());
hr = PfnBfSetupFilter(JobHandle, BINDFLT_FLAG_USE_CURRENT_SILO_MAPPING, destPath.c_str(), sourcePath.c_str(), nullptr, 0);
if (FAILED(hr))
{
printf("BfSetupFilter failed: 0x%X\n", hr);
return false;
}
return true;
}
bool TestSpawnProcessInSiloWithFileRemapping(const wchar_t *ProcessPath)
{
return SpawnProcess(
ProcessPath,
[](HANDLE JobObject)
{
TestMapFileSourceToDest(JobObject);
TestDumpMappings(JobObject);
},
nullptr);
}
bool TestSpawnProcessInSiloWithSystemLibraryRemapping(const wchar_t *ProcessPath)
{
return SpawnProcess(
ProcessPath,
[](HANDLE JobObject)
{
printf("Mapping virtual file for job 0x%p...\n\n", JobObject);
CopyFileW(L"C:\\Windows\\System32\\KernelBase.dll", std::format(L"{}\\KernelBase_copy.dll", g_TestingRootPath).c_str(), false);
CopyFileW(L"C:\\Windows\\System32\\kernel32.dll", std::format(L"{}\\kernel32_copy.dll", g_TestingRootPath).c_str(), false);
auto physPath = std::format(L"{}\\kernel32_copy.dll", g_TestingRootPath);
auto virtPath = std::format(L"C:\\Windows\\System32\\kernel32.dll");
auto hr = PfnBfRemoveMapping(JobObject, virtPath.c_str());
hr = PfnBfSetupFilter(
JobObject,
BINDFLT_FLAG_USE_CURRENT_SILO_MAPPING | BINDFLT_FLAG_PREVENT_CASE_SENSITIVE_BINDING,
virtPath.c_str(),
physPath.c_str(),
nullptr,
0);
physPath = std::format(L"{}\\KernelBase_copy.dll", g_TestingRootPath);
virtPath = std::format(L"C:\\Windows\\System32\\KernelBase.dll");
hr = PfnBfRemoveMapping(JobObject, virtPath.c_str());
hr = PfnBfSetupFilter(
JobObject,
BINDFLT_FLAG_USE_CURRENT_SILO_MAPPING | BINDFLT_FLAG_PREVENT_CASE_SENSITIVE_BINDING,
virtPath.c_str(),
physPath.c_str(),
nullptr,
0);
if (FAILED(hr))
{
printf("BfSetupFilter failed: 0x%X\n", hr);
__debugbreak();
}
TestDumpMappings(JobObject);
},
[](HANDLE JobObject, HANDLE Process)
{
using PfnNtQueryInformationProcess = LONG(WINAPI *)(HANDLE, UINT, PVOID, ULONG, PULONG);
typedef struct _PROCESS_BASIC_INFORMATION
{
LONG ExitStatus;
struct PEB *PebBaseAddress;
KAFFINITY AffinityMask;
UINT BasePriority;
HANDLE UniqueProcessId;
HANDLE InheritedFromUniqueProcessId;
} PROCESS_BASIC_INFORMATION, *PPROCESS_BASIC_INFORMATION;
PROCESS_BASIC_INFORMATION pbi = {};
auto ntdllHandle = GetModuleHandleW(L"ntdll.dll");
auto ntQIP = reinterpret_cast<PfnNtQueryInformationProcess>(GetProcAddress(ntdllHandle, "NtQueryInformationProcess"));
// !!! Set PEB->IsProtectedProcess to 1 to prevent KnownDLLs section mapping from being used. Otherwise ntdll never
// !!! queries the filesystem for the DLL and we can't remap it.
if (ntQIP(Process, 0, &pbi, sizeof(pbi), nullptr) == 0)
{
printf("Process PEB: 0x%p\n", pbi.PebBaseAddress);
const uintptr_t pebFlagsAddress = reinterpret_cast<uintptr_t>(pbi.PebBaseAddress) + 0x3;
const bool status = WriteProcessMemory(Process, reinterpret_cast<void *>(pebFlagsAddress), "\x02", 1, nullptr);
printf("Enable PEB->IsProtectedProcess: %s\n", status ? "Succeeded" : "Failed");
}
printf("\n");
});
}
int main(int argc, char **argv)
{
TestLoadImports();
printf("Using scratch directory: %S\n", g_TestingRootPath.c_str());
// Globally remap one file to another
//TestMapFileSourceToDest(nullptr);
// Globally remap and merge multiple directories to other virtual directories
//TestMergeMultipleDirectories(nullptr);
// Globally list mappings
//TestDumpMappings(nullptr);
// Per-process remap one file to another and list mappings
//TestSpawnProcessInSiloWithFileRemapping(L"C:\\Windows\\System32\\cmd.exe");
// Per-process remap system library (kernel32.dll, kernelbase.dll) and list mappings
TestSpawnProcessInSiloWithSystemLibraryRemapping(L"C:\\Windows\\System32\\cmd.exe");
printf("Done\n");
return 0;
}