Code a usn journal dumper in c++
- User
Code a usn journal dumper in c++
- Assistant
Code a usn journal dumper in c++
Sure! Below is a C++ program that dumps the USN (Update Sequence Number) change journal on an NTFS volume. The USN journal is a feature of NTFS that records changes made to files and directories on the volume, which is useful for backup applications, indexing services, and antivirus programs.
Please note that running this code requires administrative privileges because accessing the USN journal is a privileged operation.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <iostream>
#include <string>
// Function to print USN reason flags
void PrintReason(DWORD dwReason)
{
if (dwReason & USN_REASON_DATA_OVERWRITE)
std::wcout << L"DATA_OVERWRITE ";
if (dwReason & USN_REASON_DATA_EXTEND)
std::wcout << L"DATA_EXTEND ";
if (dwReason & USN_REASON_DATA_TRUNCATION)
std::wcout << L"DATA_TRUNCATION ";
if (dwReason & USN_REASON_NAMED_DATA_OVERWRITE)
std::wcout << L"NAMED_DATA_OVERWRITE ";
if (dwReason & USN_REASON_NAMED_DATA_EXTEND)
std::wcout << L"NAMED_DATA_EXTEND ";
if (dwReason & USN_REASON_NAMED_DATA_TRUNCATION)
std::wcout << L"NAMED_DATA_TRUNCATION ";
if (dwReason & USN_REASON_FILE_CREATE)
std::wcout << L"FILE_CREATE ";
if (dwReason & USN_REASON_FILE_DELETE)
std::wcout << L"FILE_DELETE ";
if (dwReason & USN_REASON_EA_CHANGE)
std::wcout << L"EA_CHANGE ";
if (dwReason & USN_REASON_SECURITY_CHANGE)
std::wcout << L"SECURITY_CHANGE ";
if (dwReason & USN_REASON_RENAME_OLD_NAME)
std::wcout << L"RENAME_OLD_NAME ";
if (dwReason & USN_REASON_RENAME_NEW_NAME)
std::wcout << L"RENAME_NEW_NAME ";
if (dwReason & USN_REASON_INDEXABLE_CHANGE)
std::wcout << L"INDEXABLE_CHANGE ";
if (dwReason & USN_REASON_BASIC_INFO_CHANGE)
std::wcout << L"BASIC_INFO_CHANGE ";
if (dwReason & USN_REASON_HARD_LINK_CHANGE)
std::wcout << L"HARD_LINK_CHANGE ";
if (dwReason & USN_REASON_COMPRESSION_CHANGE)
std::wcout << L"COMPRESSION_CHANGE ";
if (dwReason & USN_REASON_ENCRYPTION_CHANGE)
std::wcout << L"ENCRYPTION_CHANGE ";
if (dwReason & USN_REASON_OBJECT_ID_CHANGE)
std::wcout << L"OBJECT_ID_CHANGE ";
if (dwReason & USN_REASON_REPARSE_POINT_CHANGE)
std::wcout << L"REPARSE_POINT_CHANGE ";
if (dwReason & USN_REASON_STREAM_CHANGE)
std::wcout << L"STREAM_CHANGE ";
if (dwReason & USN_REASON_CLOSE)
std::wcout << L"CLOSE ";
}
int wmain(int argc, wchar_t* argv[])
{
HANDLE hVol;
DWORD dwBytes;
wchar_t volumePath[] = L"\\\\.\\C:"; // Replace 'C' with the desired drive letter
// Open a handle to the volume
hVol = CreateFileW(
volumePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hVol == INVALID_HANDLE_VALUE)
{
std::wcerr << L"Failed to open volume handle. Error: " << GetLastError() << std::endl;
return 1;
}
// Query USN Journal data
USN_JOURNAL_DATA usnJournalData = { 0 };
if (!DeviceIoControl(
hVol,
FSCTL_QUERY_USN_JOURNAL,
NULL,
0,
&usnJournalData,
sizeof(usnJournalData),
&dwBytes,
NULL
))
{
std::wcerr << L"Failed to query USN Journal data. Error: " << GetLastError() << std::endl;
CloseHandle(hVol);
return 1;
}
// Prepare to enumerate USN records
MFT_ENUM_DATA_V0 med = { 0 };
med.StartFileReferenceNumber = 0;
med.LowUsn = 0;
med.HighUsn = usnJournalData.NextUsn;
const DWORD BUF_LEN = 65536;
BYTE buffer[BUF_LEN];
DWORD dwRetBytes = 0;
while (true)
{
// Enumerate USN data
if (!DeviceIoControl(
hVol,
FSCTL_ENUM_USN_DATA,
&med,
sizeof(med),
buffer,
BUF_LEN,
&dwRetBytes,
NULL
))
{
DWORD dwError = GetLastError();
if (dwError == ERROR_HANDLE_EOF)
{
// Reached the end of the USN Journal
break;
}
else
{
std::wcerr << L"Failed to enumerate USN data. Error: " << dwError << std::endl;
CloseHandle(hVol);
return 1;
}
}
if (dwRetBytes == 0)
{
// No more data
break;
}
DWORD dwPos = 0;
PUSN_RECORD pUsnRecord = NULL;
// The first 8 bytes are the USN starting point for the next call
dwPos += sizeof(USN);
while (dwPos < dwRetBytes)
{
pUsnRecord = (PUSN_RECORD)((PBYTE)buffer + dwPos);
// Output record information
std::wcout << L"----------------------------------------" << std::endl;
std::wcout << L"File Reference Number: " << pUsnRecord->FileReferenceNumber << std::endl;
std::wcout << L"Parent File Reference Number: " << pUsnRecord->ParentFileReferenceNumber << std::endl;
std::wcout << L"USN: " << pUsnRecord->Usn << std::endl;
std::wcout << L"Timestamp: " << pUsnRecord->TimeStamp.QuadPart << std::endl;
std::wcout << L"Reason: ";
PrintReason(pUsnRecord->Reason);
std::wcout << std::endl;
std::wcout << L"File Attributes: " << pUsnRecord->FileAttributes << std::endl;
std::wcout << L"File Name Length: " << pUsnRecord->FileNameLength << std::endl;
std::wcout << L"File Name Offset: " << pUsnRecord->FileNameOffset << std::endl;
std::wstring fileName(pUsnRecord->FileName, pUsnRecord->FileNameLength / sizeof(WCHAR));
std::wcout << L"File Name: " << fileName << std::endl;
dwPos += pUsnRecord->RecordLength;
}
// Update the starting point for the next call
med.StartFileReferenceNumber = *(USN*)buffer;
}
CloseHandle(hVol);
return 0;
}
Explanation:
Include Necessary Headers:
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <iostream>
#include <string>
These headers are required for Windows API functions, input/output operations, and string handling.
Open a Handle to the Volume:
wchar_t volumePath[] = L"\\\\.\\C:"; // Replace 'C' with the desired drive letter
hVol = CreateFileW(
volumePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
The CreateFileW function opens a handle to the specified volume. Make sure to replace 'C' with the drive letter of the volume you wish to access.
Query the USN Journal Data:
USN_JOURNAL_DATA usnJournalData = { 0 };
if (!DeviceIoControl(
hVol,
FSCTL_QUERY_USN_JOURNAL,
NULL,
0,
&usnJournalData,
sizeof(usnJournalData),
&dwBytes,
NULL
))
This retrieves information about the USN journal, including the next USN number to be written.
Set Up to Enumerate USN Records:
MFT_ENUM_DATA_V0 med = { 0 };
med.StartFileReferenceNumber = 0;
med.LowUsn = 0;
med.HighUsn = usnJournalData.NextUsn;
The MFT_ENUM_DATA_V0 structure specifies the range of USN records to return.
Enumerate USN Records:
while (true)
{
if (!DeviceIoControl(
hVol,
FSCTL_ENUM_USN_DATA,
&med,
sizeof(med),
buffer,
BUF_LEN,
&dwRetBytes,
NULL
))
{
// Handle errors
}
// Process records
}
This loop retrieves USN records in batches and processes them.
Process Each USN Record:
while (dwPos < dwRetBytes)
{
pUsnRecord = (PUSN_RECORD)((PBYTE)buffer + dwPos);
// Output record information
dwPos += pUsnRecord->RecordLength;
}
Iterate through each record in the buffer and output relevant information, such as file name and change reasons.
Helper Function to Interpret Reason Codes:
void PrintReason(DWORD dwReason)
{
// Check and print each reason flag
}
The PrintReason function interprets the Reason field of the USN record, indicating why the change was logged.
Compilation Instructions:
To compile this program, you'll need a Windows environment with Visual Studio installed. Here are the steps:
Open the Developer Command Prompt for VS.
Save the code to a file, e.g., usn_journal_dumper.cpp.
Compile the program using the Visual C++ compiler:
cl /EHsc usn_journal_dumper.cpp
Run the executable as an administrator:
usn_journal_dumper.exe
Important Notes:
Permissions: Accessing the USN journal requires administrative privileges. Run the program as an administrator to ensure it has the necessary permissions.
Drive Letter:
Ensure you replace 'C' in volumePath with the drive letter of the NTFS volume you wish to inspect.
Buffer Size:
The buffer size BUF_LEN is set to 65536 bytes (64 KB). This size is sufficient for many cases, but you may adjust it if needed.
Error Handling: The code includes basic error handling. In a production environment, you may want to enhance this to handle different error cases more gracefully.
Performance: Enumerating the entire USN journal can be time-consuming, especially on large volumes with extensive change histories.
USN Record Structures:
The code uses USN_RECORD structures to interpret the data. Depending on the version of Windows and the USN journal version, you may need to adjust the structure definitions.
Disclaimer:
System Impact: Reading from the USN journal is generally safe, but modifying it or misusing the information can affect system performance or stability.
Legal Considerations: Ensure you have the right and permission to access the USN journal on the target system. Unauthorized access may violate policies or laws.
Feel free to ask if you have any questions or need further assistance!
Not yet included in a published release.