I'm experimenting with hooking ZwQueryDirectoryFile on win 7 x32 but I'm getting strange behavior:
The code that I have is the following:
...
My second issue is that I cannot unload the driver without crashing the system, the bugcheck is: Driver unloaded without cancelling pending operations
I'm guessing this is hapenning because the driver is unloading while amidst performing an operation within the hook function? So how can I avoid this situation - my driver unload currently looks like:
The code that I have is the following:
Code: Select all
My driver entry: NTSTATUS newZwQueryDirectoryFile(HANDLE FileHandle,HANDLE Event, PIO_APC_ROUTINE ApcRoutine,PVOID ApcContext,PIO_STATUS_BLOCK IoStatusBlock,PVOID FileInformation,ULONG Length, FILE_INFORMATION_CLASS FileInformationClass,BOOLEAN ReturnSingleEntry,PUNICODE_STRING FileName,BOOLEAN RestartScan)
{
NTSTATUS status;
BYTE *tempMath;
PFILE_BOTH_DIR_INFORMATION currFileItem;
PFILE_BOTH_DIR_INFORMATION prevFileItem;
status = myNtQueryDirectoryFile(FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, FileInformation, Length, FileInformationClass, ReturnSingleEntry, FileName, RestartScan);
if(!NT_SUCCESS(status) || FileInformationClass != FileBothDirectoryInformation)
return status;
currFileItem = (PFILE_BOTH_DIR_INFORMATION) FileInformation;
prevFileItem = NULL;
if( NT_SUCCESS( status ) &&
(FileInformationClass == FileDirectoryInformation ||
FileInformationClass == FileFullDirectoryInformation ||
FileInformationClass == FileIdFullDirectoryInformation ||
FileInformationClass == FileBothDirectoryInformation ||
FileInformationClass == FileIdBothDirectoryInformation ||
FileInformationClass == FileNamesInformation )
) {
PVOID currentEntry = FileInformation;
PVOID prevEntry = NULL;
BOOLEAN lastInList;
int myLength = 8;
do {
lastInList = !getDirEntryNextOffset(currentEntry, FileInformationClass);
if(getDirEntryFileLength(currentEntry, FileInformationClass) >= myLength) {
if(RtlCompareMemory(getDirEntryFileName(currentEntry, FileInformationClass), L"hid2", myLength) == myLength) {
DbgPrint("We are going to hide a file BRAT!\n");
if(lastInList) {
if(currentEntry == FileInformation) {
status = STATUS_NO_MORE_FILES;
} else {
setDirEntryNextOffset(prevEntry, FileInformationClass, 0);
}
} else {
int Pos = ((ULONG)currentEntry) - (ULONG)FileInformation;
int remainingBytes = (DWORD)Length - Pos - getDirEntryNextOffset(currentEntry, FileInformationClass);
RtlCopyMemory(currentEntry, (BYTE *)currentEntry + getDirEntryNextOffset(currentEntry, FileInformationClass), (DWORD)remainingBytes);
continue;
}
}
}
prevEntry = currentEntry;
currentEntry = (BYTE *)currentEntry + getDirEntryNextOffset(currentEntry, FileInformationClass);
} while(!lastInList);
}
return status;
}
...
Code: Select all
The function is hooked successfuly because I can see "We are going to hide a file\n" when I enter into a direcotry which has a file whose name starts with "hid2" but the file is not hidden and when I click it I get an error saying that the security settings of the computer prevented opening the file. So what am I doing wrong while hooking it?typedef NTSTATUS (__stdcall *QUERY_DIR_INFO)(
__in HANDLE FileHandle,
__in_opt HANDLE Event,
__in_opt PIO_APC_ROUTINE ApcRoutine,
__in_opt PVOID ApcContext,
__out PIO_STATUS_BLOCK IoStatusBlock,
__out PVOID FileInformation,
__in ULONG Length,
__in FILE_INFORMATION_CLASS FileInformationClass,
__in BOOLEAN ReturnSingleEntry,
__in_opt PUNICODE_STRING FileName,
__in BOOLEAN RestartScan
);
QUERY_DIR_INFO myNtQueryDirectoryFile = NULL;
//skipped irrelevant parts
myNtQueryDirectoryFile = (QUERY_DIR_INFO) hookSSDTEntry((BYTE *) ZwQueryDirectoryFile, (BYTE *)newZwQueryDirectoryFile, (DWORD *)state.address);
My second issue is that I cannot unload the driver without crashing the system, the bugcheck is: Driver unloaded without cancelling pending operations
I'm guessing this is hapenning because the driver is unloading while amidst performing an operation within the hook function? So how can I avoid this situation - my driver unload currently looks like:
Code: Select all
void SSDThookUnload(IN PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING Win32Device;
WPGLOBAL state;
//removed code - disabling SSDT protection through MDL
DbgPrint("Removing SSDT HOOK");
_asm cli
unhookSSDTEntry((BYTE *) ZwQueryDirectoryFile, (BYTE *)newZwQueryDirectoryFile, (DWORD *)state.address);
_asm sti
//removed code
}