-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
426 lines (361 loc) · 19.4 KB
/
main.cpp
File metadata and controls
426 lines (361 loc) · 19.4 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <iostream>
#include <cassert>
#include <memory>
#include <string>
#include <locale>
#include <codecvt>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <intrin.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include "NTFS_Structure.h"
[[noreturn]] void ExitErrorWinApi(std::string msg);
LONGLONG ExtractLowerBytesSigned(LONGLONG val, BYTE nBytesExtract);
/** Iterate and print file records
*
* @param lpParm [in] Pointer of IterateFileRecordsParam
*/
unsigned __stdcall IterateFileRecords(LPVOID lpParm);
struct IterateFileRecordsParam {
BYTE* FileRecords_Raw;
QWORD ClustorNumber;
QWORD ClusterCount;
std::vector<BYTE>* OutStream;
};
WORD gBytesPerSector = 0;
BYTE gSectorsPerCluster = 0;
QWORD gBytesPerCluster = 0;
ULONG gBytesPerFileRecord = 1024; // Typically NTFS's MFT size is 1KB.
int wmain(int argc, wchar_t** argv)
{
const std::locale utf16_locale = std::locale(std::locale(), new std::codecvt_utf16<wchar_t>());
//std::wofstream f(L"output.list");
//f.imbue(utf16_locale);
// Open logical disk
// TODO: Test performance of FILE_FLAG_OVERLAPPED, FILE_FLAG_NO_BUFFERING
WCHAR diskPath[] = L"\\\\.\\C:";
HANDLE hDrive = CreateFileW(diskPath, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDrive == INVALID_HANDLE_VALUE) {
ExitErrorWinApi("Need administrator permission.");
}
std::wcout << "Success to open logical disk. HANDLE: " << hDrive << std::endl;
// Read NTFS boot sector
std::wcout << "Read NTFS boot sector..." << std::endl;
NTFS_BootSector bootSector{};
{
DWORD nBytesRead;
if (!ReadFile(hDrive, &bootSector, sizeof(NTFS_BootSector), &nBytesRead, NULL)) {
ExitErrorWinApi("Failed to read boot sector.");
}
}
gBytesPerSector = bootSector.BPB.BytesPerSector;
gSectorsPerCluster = bootSector.BPB.SectorsPerClustor;
gBytesPerCluster = gBytesPerSector * gSectorsPerCluster;
// Read $MFT's file record header to get entry size
const QWORD loc_$MFT_FileRecord = (QWORD)bootSector.BPB.LCN_$MFT * gBytesPerCluster;
const LONG loc_$MFT_FileRecord_high = (loc_$MFT_FileRecord >> 32) & 0xFFFFFFFF;
const LONG loc_$MFT_FileRecord_low = loc_$MFT_FileRecord & 0xFFFFFFFF;
// Read $MFT's file record content
std::wcout << "Read $MFT's file record content..." << std::endl;
std::unique_ptr<char[]> $MFT_FileRecord_Raw = std::make_unique<char[]>(gBytesPerFileRecord);
{
OVERLAPPED hOverlapped;
ZeroMemory(&hOverlapped, sizeof(hOverlapped));
hOverlapped.Offset = (DWORD)(loc_$MFT_FileRecord & 0xFFFFFFFF);
hOverlapped.OffsetHigh = (DWORD)((loc_$MFT_FileRecord >> 32) & 0xFFFFFFFF);
DWORD nBytesRead;
if (!ReadFile(hDrive, $MFT_FileRecord_Raw.get(), gBytesPerFileRecord, &nBytesRead, &hOverlapped)) {
ExitErrorWinApi("Failed to read $MFT's MFT entry.");
}
}
NTFS_FileRecordHeader* $MFT_FileRecord_Header = (NTFS_FileRecordHeader*)$MFT_FileRecord_Raw.get();
assert($MFT_FileRecord_Header->BytesPerFileRecord == gBytesPerFileRecord);
assert($MFT_FileRecord_Header->Flags == eNTFS_FileRecordHeader_flags::FILE_RECORD_SEGMENT_IN_USE);
CloseHandle(hDrive);
hDrive = NULL;
// Find $DATA attribute from $MFT Attribute chain
std::wcout << "Find $DATA attribute from $MFT Attribute chain..." << std::endl;
NTFS_FileRecord_AttrHeader* $MFT_FileRecord_DataAttr = NULL;
{
NTFS_FileRecord_AttrHeader* loc_$MFT_FileRecord_FirstAttribute = (NTFS_FileRecord_AttrHeader*)((SIZE_T)$MFT_FileRecord_Header + $MFT_FileRecord_Header->FirstAttributeOffset);
NTFS_FileRecord_AttrHeader* loc_$MFT_FileRecord_end = (NTFS_FileRecord_AttrHeader*)((SIZE_T)$MFT_FileRecord_Header + $MFT_FileRecord_Header->BytesUsedFileRecord);
NTFS_FileRecord_AttrHeader* pAttr = loc_$MFT_FileRecord_FirstAttribute;
while ((LPBYTE)pAttr <= (LPBYTE)loc_$MFT_FileRecord_end - sizeof(NTFS_FileRecord_AttrHeader))
{
if (pAttr->TypeCode == eNTFS_FileRecord_AttrHeader_TypeCode::$DATA) {
assert($MFT_FileRecord_DataAttr == NULL); //< No handling of multiple DATA attribute.
$MFT_FileRecord_DataAttr = pAttr;
}
// Advance to next attribute
pAttr = (NTFS_FileRecord_AttrHeader*)((SIZE_T)pAttr + pAttr->RecordLength);
}
}
assert($MFT_FileRecord_DataAttr);
// Read content of $DATA attribute
std::wcout << "Read content of $DATA attribute..." << std::endl;
assert($MFT_FileRecord_DataAttr->Flags == 0 || $MFT_FileRecord_DataAttr->Flags == ATTRIBUTE_FLAG_SPARSE);
if ($MFT_FileRecord_DataAttr->FormCode == eNTFS_FileRecord_AttrHeader_FormCode::RESIDENT_FORM)
{
// TODO:
assert(0);
LPBYTE loc_$MFT_FileRecord_DataAttr_Content = (LPBYTE)$MFT_FileRecord_DataAttr + $MFT_FileRecord_DataAttr->Form.Resident.ValueOffset;
}
else if ($MFT_FileRecord_DataAttr->FormCode == eNTFS_FileRecord_AttrHeader_FormCode::NONRESIDENT_FORM)
{
assert($MFT_FileRecord_DataAttr->Form.Nonresident.CompressionUnitSize == 0); //< Only handle the uncompressed data.
// Search runlists(mapping pairs)
std::wcout << "Search runlists(mapping pairs)..." << std::endl;
std::vector<IterateFileRecordsParam> runIterThreads_RunParamList;
QWORD currentLCN = 0;
QWORD nBytesTotalRunContent = 0;
LPBYTE loc_$MFT_FileRecord_DataAttr_Runlists = (LPBYTE)$MFT_FileRecord_DataAttr + $MFT_FileRecord_DataAttr->Form.Nonresident.MappingPairsOffset;
LPBYTE loc_$MFT_FileRecord_DataAttr_End = (LPBYTE)$MFT_FileRecord_DataAttr + $MFT_FileRecord_DataAttr->RecordLength;
LPBYTE pRunlist = loc_$MFT_FileRecord_DataAttr_Runlists;
while (pRunlist < loc_$MFT_FileRecord_DataAttr_End)
{
// Read run header
const BYTE runHeader = *((BYTE*)pRunlist);
const BYTE nBytesRunLength = runHeader & 0xF;
const BYTE nBytesRunOffset = (runHeader >> 4) & 0xF;
pRunlist += 1;
if (runHeader == 0) {
continue;
}
const LONGLONG runLength = ExtractLowerBytesSigned(*((LONGLONG*)(pRunlist)), nBytesRunLength);
const LONGLONG runOffset = ExtractLowerBytesSigned(*((LONGLONG*)(pRunlist + nBytesRunLength)), nBytesRunOffset); //< Can be negative
assert(runLength >= 0); //< Idk it can be negative too.
pRunlist += (SIZE_T)nBytesRunLength + nBytesRunOffset;
currentLCN += runOffset;
nBytesTotalRunContent += runLength * gBytesPerCluster;
// Push run information parameter
IterateFileRecordsParam param;
param.FileRecords_Raw = NULL;
param.OutStream = NULL;
param.ClustorNumber = currentLCN;
param.ClusterCount = runLength;
runIterThreads_RunParamList.push_back(param);
}
// Load all file records into memory (as FILE_FLAG_OVERLAPPED)
std::wcout << "Load all file records into memory..." << std::endl;
hDrive = CreateFileW(diskPath, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (hDrive == INVALID_HANDLE_VALUE) {
ExitErrorWinApi("Need administrator permission.");
}
std::unique_ptr<BYTE[]> fileRecords_Raw = std::make_unique<BYTE[]>(nBytesTotalRunContent);
{
std::vector<HANDLE> fileReadEvents;
std::vector<DWORD> fileReadLengthList;
QWORD nBytesReadOffset = 0;
for (IterateFileRecordsParam& paramIt : runIterThreads_RunParamList)
{
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hEvent == NULL) {
ExitErrorWinApi("Failed CreateEvent().");
}
fileReadEvents.push_back(hEvent);
fileReadLengthList.push_back((DWORD)0);
OVERLAPPED overlapped;
ZeroMemory(&overlapped, sizeof(OVERLAPPED));
overlapped.hEvent = hEvent;
overlapped.Offset = (DWORD)((paramIt.ClustorNumber * gBytesPerCluster) & 0xFFFFFFFF);
overlapped.OffsetHigh = (DWORD)(((paramIt.ClustorNumber * gBytesPerCluster) >> 32) & 0xFFFFFFFF);
assert((QWORD)paramIt.ClusterCount * gBytesPerCluster <= MAXDWORD);
if (!ReadFile(hDrive, fileRecords_Raw.get() + nBytesReadOffset, (DWORD)paramIt.ClusterCount * gBytesPerCluster, &fileReadLengthList.back(), &overlapped)) {
if (GetLastError() != ERROR_IO_PENDING) {
ExitErrorWinApi("Failed ReadFile()");
}
}
nBytesReadOffset += paramIt.ClusterCount * gBytesPerCluster;
}
assert(fileReadEvents.size() <= MAXDWORD);
if (WaitForMultipleObjects((DWORD)fileReadEvents.size(), fileReadEvents.data(), TRUE, INFINITE) == WAIT_FAILED) {
ExitErrorWinApi("Failed to wait for all file records read.");
}
}
CloseHandle(hDrive);
hDrive = NULL;
// Iterate all of file records
std::vector<HANDLE> runIterThreadList;
std::vector<IterateFileRecordsParam*> runIterThreads_ParamList;
std::vector< std::vector< BYTE >* > runIterThreads_OutStreamList;
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
const int numCpuCore = sysinfo.dwNumberOfProcessors;
const QWORD nThreadMax = numCpuCore; //< or x2
const QWORD nRecordPerCluster = gBytesPerCluster / gBytesPerFileRecord;
assert(nBytesTotalRunContent % gBytesPerCluster == 0);
const QWORD totalRecordClusterCount = nBytesTotalRunContent / gBytesPerCluster;
const QWORD numRecordClusterPerThread = totalRecordClusterCount / nThreadMax;
const QWORD numRecordClusterPerThread_Remain = totalRecordClusterCount % nThreadMax;
QWORD clusterOffset = 0;
for (size_t threadIdx = 0; threadIdx < nThreadMax; threadIdx++)
{
IterateFileRecordsParam* param = new IterateFileRecordsParam();
param->FileRecords_Raw = fileRecords_Raw.get();
param->ClustorNumber = clusterOffset;
param->ClusterCount = numRecordClusterPerThread;
if (threadIdx < numRecordClusterPerThread_Remain) {
param->ClusterCount += 1;
}
runIterThreads_OutStreamList.push_back(new std::vector<BYTE>());
param->OutStream = runIterThreads_OutStreamList.back();
clusterOffset += param->ClusterCount / nRecordPerCluster;
runIterThreads_ParamList.push_back(param);
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, IterateFileRecords, param, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);
if (hThread == (HANDLE)0 || hThread == (HANDLE)-1L) {
ExitErrorWinApi("_beginthreadex failed.");
}
runIterThreadList.push_back(hThread);
std::wcout << "[LOG]Begin runlist entry thread: " << threadIdx << std::endl;
}
if (WaitForMultipleObjects((DWORD)nThreadMax, runIterThreadList.data(), TRUE, INFINITE) == WAIT_FAILED) {
ExitErrorWinApi("Failed to wait for runlist entry threads.");
}
// Write result to file
std::ofstream fsResult("output.list", std::ios::out | std::ios::binary);
//fsResult.imbue(utf16_locale);
DWORD UTF16_BOM = 0xFEFF;
fsResult.write((LPSTR)&UTF16_BOM, sizeof(DWORD));
for (std::vector<BYTE>* it : runIterThreads_OutStreamList) {
fsResult.write((LPSTR)it->data(), it->size());
delete(it);
}
fsResult.close();
runIterThreads_OutStreamList.clear();
for (IterateFileRecordsParam* it : runIterThreads_ParamList) {
delete(it);
}
}
}
std::wcout << "[LOG]Reached to end of main." << std::endl;
return 0;
}
void ExitErrorWinApi(std::string msg)
{
const DWORD lastError = GetLastError();
std::wcout << msg.c_str() << ": Code[" << lastError << "]" << std::endl;
char* szMessageBuffer = NULL;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&szMessageBuffer, 0, NULL);
std::wcout << szMessageBuffer << std::endl;
exit(1);
}
LONGLONG ExtractLowerBytesSigned(LONGLONG val, BYTE nBytesExtract)
{
assert(nBytesExtract <= 8);
const DWORD bitsExtractIndex = nBytesExtract * 8;
const QWORD sign = (val & ((QWORD)1 << (bitsExtractIndex - 1))) << (63 - (bitsExtractIndex - 1));
const QWORD extracted = _bzhi_u64(val, bitsExtractIndex);
return extracted | ((LONGLONG)sign >> (64 - bitsExtractIndex));
}
unsigned __stdcall IterateFileRecords(LPVOID lpParm)
{
IterateFileRecordsParam* Param = (IterateFileRecordsParam*)lpParm;
std::vector<BYTE>* outStream = Param->OutStream;
// Iterate file records
LPBYTE pCurrentRecord = (LPBYTE)Param->FileRecords_Raw + (Param->ClustorNumber * gBytesPerCluster);
LPBYTE loc_RecordIteration_End = (LPBYTE)pCurrentRecord + (Param->ClusterCount * gBytesPerFileRecord);
for (; pCurrentRecord < loc_RecordIteration_End; pCurrentRecord += gBytesPerFileRecord)
{
NTFS_FileRecordHeader* currentRecord_Header = (NTFS_FileRecordHeader*)pCurrentRecord;
if (strncmp((LPSTR)currentRecord_Header->Signature, "FILE", sizeof(currentRecord_Header->Signature)) != 0) {
// std::wcout << "[LOG] NowRecord = " << pCurrentRecord << std::endl;
if (strncmp((LPSTR)currentRecord_Header->Signature, "BAAD", sizeof(currentRecord_Header->Signature)) != 0) {
std::wcout << "::[LOG] Detected BAAD MFT entry." << std::endl;
continue;
}
break;
}
assert(currentRecord_Header->BytesPerFileRecord == gBytesPerFileRecord);
// Find $FILE_NAME attribute from attribute chain
NTFS_FileRecord_AttrHeader* pFileNameAttr = NULL;
{
NTFS_FileRecord_AttrHeader* loc_FirstAttribute = (NTFS_FileRecord_AttrHeader*)(pCurrentRecord + currentRecord_Header->FirstAttributeOffset);
NTFS_FileRecord_AttrHeader* loc_Entry_End = (NTFS_FileRecord_AttrHeader*)(pCurrentRecord + currentRecord_Header->BytesUsedFileRecord);
NTFS_FileRecord_AttrHeader* pAttr = loc_FirstAttribute;
while ((LPBYTE)pAttr <= (LPBYTE)loc_Entry_End - sizeof(NTFS_FileRecord_AttrHeader))
{
if (pAttr->TypeCode == eNTFS_FileRecord_AttrHeader_TypeCode::$FILE_NAME) {
// @note. There can exist multiple $FILE_NAME.
pFileNameAttr = pAttr;
}
// Advance to next attribute
pAttr = (NTFS_FileRecord_AttrHeader*)((SIZE_T)pAttr + pAttr->RecordLength);
}
}
if (pFileNameAttr == NULL) {
// [End of cluster] or [Empty space of cluster]
//std::wcout << "[No Name]" << std::endl;
continue;
}
// Get file name
assert(pFileNameAttr->FormCode == eNTFS_FileRecord_AttrHeader_FormCode::RESIDENT_FORM);
NTFS_FileRecord_Attr_Filename* attrFileName = (NTFS_FileRecord_Attr_Filename*)((LPBYTE)pFileNameAttr + pFileNameAttr->Form.Resident.ValueOffset);
switch (attrFileName->FileNameType)
{
case FILENAME_NAMETYPE_WIN32_7_DOS:
case FILENAME_NAMETYPE_DOS:
case FILENAME_NAMETYPE_POSIX:
case FILENAME_NAMETYPE_WIN32: {
static_assert(sizeof(WCHAR) == 2, "wchar_t is not 2byte.");
static_assert(sizeof(attrFileName->FileNameLength) == 1, "Maximum of FileNameLength is not 256.");
std::wstring fileNameResult = std::wstring(attrFileName->FileName, (size_t)attrFileName->FileNameLength);
// [DEBUG] Dereference parent directory to get path
QWORD currParentDirectoryReference = attrFileName->ParentDirectory;
while (true)
{
const QWORD parentDirectoryRecordIndex = (currParentDirectoryReference & 0x0000FFFFFFFFFFFF);
const QWORD parentDirectorySequenceNum = (currParentDirectoryReference & 0xFFFF000000000000) >> 60;
NTFS_FileRecordHeader* parentDirectory_RecordHeader = (NTFS_FileRecordHeader*)(Param->FileRecords_Raw + (parentDirectoryRecordIndex * gBytesPerFileRecord));
// Find $FILE_NAME attribute from parent's attribute chain
NTFS_FileRecord_AttrHeader* pFileNameAttr = NULL;
{
NTFS_FileRecord_AttrHeader* loc_FirstAttribute = (NTFS_FileRecord_AttrHeader*)((LPBYTE)parentDirectory_RecordHeader + parentDirectory_RecordHeader->FirstAttributeOffset);
NTFS_FileRecord_AttrHeader* loc_Entry_End = (NTFS_FileRecord_AttrHeader*)((LPBYTE)parentDirectory_RecordHeader + parentDirectory_RecordHeader->BytesUsedFileRecord);
NTFS_FileRecord_AttrHeader* pAttr = loc_FirstAttribute;
while ((LPBYTE)pAttr <= (LPBYTE)loc_Entry_End - sizeof(NTFS_FileRecord_AttrHeader))
{
if (pAttr->TypeCode == eNTFS_FileRecord_AttrHeader_TypeCode::$TYPECODE_END) {
break;
}
if (pAttr->TypeCode == eNTFS_FileRecord_AttrHeader_TypeCode::$FILE_NAME) {
// @note. There can exist multiple $FILE_NAME.
pFileNameAttr = pAttr;
}
// Advance to next attribute
pAttr = (NTFS_FileRecord_AttrHeader*)((SIZE_T)pAttr + pAttr->RecordLength);
}
}
if (pFileNameAttr == NULL) {
//std::wcout << "[No Name]" << std::endl;
break;
}
NTFS_FileRecord_Attr_Filename* parentDirecoryName = (NTFS_FileRecord_Attr_Filename*)((LPBYTE)pFileNameAttr + pFileNameAttr->Form.Resident.ValueOffset);
fileNameResult += L"<-" + std::wstring(parentDirecoryName->FileName, parentDirecoryName->FileNameLength);
if (parentDirecoryName->FileName == std::wstring(L".")) {
break;
}
currParentDirectoryReference = parentDirecoryName->ParentDirectory;
}
const size_t originalSize = outStream->size();
const size_t adjustSize = originalSize + ((fileNameResult.length() * 2) + (std::wstring(L"\r\n").length() * 2));
outStream->resize(adjustSize);
memcpy(outStream->data() + originalSize, fileNameResult.data(), fileNameResult.length() * 2);
memcpy(outStream->data() + originalSize + (fileNameResult.length() * 2), std::wstring(L"\r\n").data(), std::wstring(L"\r\n").length() * 2);
break;
}
default: {
_assume(0);
}
}
}
return 0;
}