gpt4 book ai didi

c++ - 获取文件的完整路径给定其 "Reference Number"

转载 作者:可可西里 更新时间:2023-11-01 11:23:30 26 4
gpt4 key购买 nike

我想使用 Windows 的更改日志来跟踪一些文件更改。我设法使用示例代码访问上述日志并从中读取记录。我现在面临的问题是记录只包含相关的文件名,而不是它的绝对路径。我尝试将 FSCTL_GET_NTFS_FILE_RECORD 与 DeviceIoControl 一起使用来检查我是否可以通过这种方式获得绝对路径,但是调用 DeviceIoControl 总是返回相同的文件记录,缓冲区中有“FILE0”,我给它的引用号没有它的 HighPart(例如,我给出了 0x001400000015adec 的引用编号,当函数返回时,输出的引用编号为 0x000000000015adec。

这是我的代码(我在 Windows 10 上运行)

#include <Windows.h>
#include <WinIoCtl.h>
#include <stdio.h>

#define BUF_LEN (1024 * 1024)

void main()
{
HANDLE hVol;
CHAR * pBuffer;
pBuffer = new CHAR[BUF_LEN];

USN_JOURNAL_DATA JournalData;
READ_USN_JOURNAL_DATA_V0 ReadData = {0, 0xFFFFFFFF, FALSE, 0, 0};
PUSN_RECORD UsnRecord;

DWORD dwBytes;
DWORD dwRetBytes;
DWORD dwFileRecordSize;
BYTE * pFileRecord;

hVol = CreateFile( TEXT("\\\\.\\D:"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);

if( hVol == INVALID_HANDLE_VALUE )
{
printf("CreateFile failed (%d)\n", GetLastError());
return;
}

if( !DeviceIoControl( hVol,
FSCTL_QUERY_USN_JOURNAL,
NULL,
0,
&JournalData,
sizeof(JournalData),
&dwBytes,
NULL) )
{
printf( "Query journal failed (%d)\n", GetLastError());
return;
}

ReadData.UsnJournalID = JournalData.UsnJournalID;

printf( "Journal ID: %I64x\n", JournalData.UsnJournalID );
printf( "FirstUsn: %I64x\n\n", JournalData.FirstUsn );

// get single file record size
NTFS_VOLUME_DATA_BUFFER volBuffer;
if( !DeviceIoControl( hVol,
FSCTL_GET_NTFS_VOLUME_DATA,
NULL,
0,
&volBuffer,
sizeof(volBuffer),
&dwBytes,
NULL))
{
printf( "Get volume data size failed (%d)\n", GetLastError());
return;
}
dwFileRecordSize = volBuffer.BytesPerFileRecordSegment + sizeof(NTFS_FILE_RECORD_OUTPUT_BUFFER) - 1;
pFileRecord = new BYTE[dwFileRecordSize];

bool bEnd = FALSE;
WORD lastDay = 0;
WORD lastMonth = 0;
WORD lastYear = 0;
DWORD nbEntries = 0;

while (!bEnd)
{
memset( pBuffer, 0, BUF_LEN );

if( !DeviceIoControl( hVol,
FSCTL_READ_USN_JOURNAL,
&ReadData,
sizeof(ReadData),
pBuffer,
BUF_LEN,
&dwBytes,
NULL) )
{
printf( "Read journal failed (%d)\n", GetLastError());
return;
}

dwRetBytes = dwBytes - sizeof(USN);

bEnd = (dwRetBytes == 0); // we finished reading the journal

// Find the first record
UsnRecord = (PUSN_RECORD)(((PUCHAR)pBuffer) + sizeof(USN));

// This loop could go on for a long time, given the current buffer size.
while( dwRetBytes > 0 )
{
SYSTEMTIME systemTime;
FILETIME fileTime;
fileTime.dwHighDateTime = UsnRecord->TimeStamp.HighPart;
fileTime.dwLowDateTime = UsnRecord->TimeStamp.LowPart;
FileTimeToSystemTime(&fileTime, &systemTime);

if (lastDay != systemTime.wDay || lastMonth != systemTime.wMonth || lastYear != systemTime.wYear)
{
if (nbEntries != 0)
{
printf("%u entries read\n", nbEntries);
}
nbEntries = 0;
lastDay = systemTime.wDay;
lastMonth = systemTime.wMonth;
lastYear = systemTime.wYear;
printf("reading entries for %u/%u/%u...\n", lastDay, lastMonth, lastYear);
}

++nbEntries;

NTFS_FILE_RECORD_INPUT_BUFFER inputBuff;
memset(pFileRecord, 0, dwFileRecordSize);
inputBuff.FileReferenceNumber.QuadPart = UsnRecord->FileReferenceNumber;
if (!DeviceIoControl( hVol,
FSCTL_GET_NTFS_FILE_RECORD,
&inputBuff,
32,
pFileRecord,
dwFileRecordSize,
&dwBytes,
NULL))
{
printf( "get file record failed (%d)\n", GetLastError());
return;
}

NTFS_FILE_RECORD_OUTPUT_BUFFER * pFileRecordBuff = (NTFS_FILE_RECORD_OUTPUT_BUFFER *)(pFileRecord);
if (pFileRecordBuff->FileReferenceNumber.QuadPart == inputBuff.FileReferenceNumber.QuadPart)
{
printf("file record found\n%.*S\n",
pFileRecordBuff->FileRecordLength / 2,
pFileRecordBuff->FileRecordBuffer);
}

dwRetBytes -= UsnRecord->RecordLength;

// Find the next record
UsnRecord = (PUSN_RECORD)(((PCHAR)UsnRecord) +
UsnRecord->RecordLength);
}
// Update starting USN for next call
ReadData.StartUsn = *(USN *)pBuffer;
}

delete[] pBuffer;
delete[] pFileRecord;

CloseHandle(hVol);

system("pause");

}

我做错了什么吗?或者是否有另一种方法可以使用 USN_RECORD 实例获取文件的完整路径?

最佳答案

您需要先通过调用打开文件句柄 OpenFileById使用 USN_RECORD 中的 FileReferenceNumber 函数(你不能将它的高 16 位归零!)然后你可以通过 GetFileInformationByHandleEx 查询文件路径与 FileNameInfo .你有 FILE_NAME_INFO在输出上。或调用NtQueryInformationFileFileNameInformation。只要文件打开,调用方就可以查询此信息,对 DesiredAccess 没有任何特殊要求。所以代码看起来像。

ULONG GepPathById(HANDLE hVolumeHint, ULONGLONG FileReferenceNumber, PFILE_NAME_INFO* ppfni)
{
FILE_ID_DESCRIPTOR fid = { sizeof(fid), FileIdType };
fid.FileId.QuadPart = FileReferenceNumber;

HANDLE hFile = OpenFileById(hVolumeHint, &fid, 0, FILE_SHARE_VALID_FLAGS, 0, 0);

if (hFile == INVALID_HANDLE_VALUE)
{
return GetLastError();
}

ULONG dwErrorCode = NOERROR;
ULONG cbNeed = 0x20;
do
{
if (PFILE_NAME_INFO pfni = (PFILE_NAME_INFO)LocalAlloc(0, cbNeed))
{
dwErrorCode = GetFileInformationByHandleEx(hFile, FileNameInfo,
pfni, cbNeed - sizeof(WCHAR)) ? 0 : GetLastError();

if (!dwErrorCode)
{
*(PWSTR)((PBYTE)pfni->FileName + pfni->FileNameLength) = 0;
*ppfni = pfni;
break;
}

cbNeed = FIELD_OFFSET(FILE_NAME_INFO, FileName) + pfni->FileNameLength + sizeof(WCHAR);

LocalFree(pfni);
}
else
{
dwErrorCode = GetLastError();
break;
}

} while (dwErrorCode == ERROR_MORE_DATA);

CloseHandle(hFile);

return dwErrorCode;
}

和用法

    PFILE_NAME_INFO pfni;
if (!GepPathById(hVol, UsnRecord->FileReferenceNumber, &pfni))
{
DbgPrint("%S\n", pfni->FileName);
LocalFree(pfni);
}

关于c++ - 获取文件的完整路径给定其 "Reference Number",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53504921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com