PVOID gOldFunctionAddress = NULL;
NTSTATUS MyZwQueryDirectoryFile( IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE ApcRoutine OPTIONAL, IN PVOID ApcContext OPTIONAL, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID FileInformation, IN ULONG Length, IN FILE_INFORMATION_CLASS FileInformationClass, IN BOOLEAN ReturnSingleEntry, IN PUNICODE_STRING FileMask OPTIONAL, IN BOOLEAN RestartScan ) { NTSTATUS status = STATUS_SUCCESS;
typedef NTSTATUS(*typedef_ZwQueryDirectoryFile)( IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE ApcRoutine OPTIONAL, IN PVOID ApcContext OPTIONAL, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID FileInformation, IN ULONG Length, IN FILE_INFORMATION_CLASS FileInformationClass, IN BOOLEAN ReturnSingleEntry, IN PUNICODE_STRING FileMask OPTIONAL, IN BOOLEAN RestartScan );
DbgPrint("MyZwQueryDirectoryFile 自定义功能 \n");
status = ((typedef_ZwQueryDirectoryFile)gOldFunctionAddress)(FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, FileInformation, Length, FileInformationClass, ReturnSingleEntry, FileMask, RestartScan);
return status; }
BOOLEAN SSDTFunctionHook(ULONG64 FunctionAddress) { PMDL pMdl = NULL; PVOID pNewAddress = NULL; ULONG ulNewFuncAddr = 0;
gOldFunctionAddress = FunctionAddress;
pMdl = MmCreateMdl(NULL, &FunctionAddress, sizeof(ULONG)); if (NULL == pMdl) { return FALSE; }
MmBuildMdlForNonPagedPool(pMdl);
pNewAddress = MmMapLockedPages(pMdl, KernelMode); if (NULL == pNewAddress) { IoFreeMdl(pMdl); return FALSE; }
ulNewFuncAddr = (ULONG)MyZwQueryDirectoryFile; RtlCopyMemory(pNewAddress, &ulNewFuncAddr, sizeof(ULONG));
MmUnmapLockedPages(pNewAddress, pMdl); IoFreeMdl(pMdl);
return TRUE; }
BOOLEAN SSDTFunctionUnHook(ULONG64 FunctionAddress) { PMDL pMdl = NULL; PVOID pNewAddress = NULL; ULONG ulOldFuncAddr = 0;
gOldFunctionAddress = FunctionAddress;
pMdl = MmCreateMdl(NULL, &FunctionAddress, sizeof(ULONG)); if (NULL == pMdl) { return FALSE; }
MmBuildMdlForNonPagedPool(pMdl);
pNewAddress = MmMapLockedPages(pMdl, KernelMode); if (NULL == pNewAddress) { IoFreeMdl(pMdl); return FALSE; }
ulOldFuncAddr = (ULONG)gOldFunctionAddress; RtlCopyMemory(pNewAddress, &ulOldFuncAddr, sizeof(ULONG));
MmUnmapLockedPages(pNewAddress, pMdl); IoFreeMdl(pMdl);
return TRUE; }
VOID UnDriver(PDRIVER_OBJECT driver) { SSDTFunctionUnHook(gOldFunctionAddress); DbgPrint("驱动卸载 \n"); }
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { DbgPrint("hello lyshark.com \n");
NTSTATUS status = STATUS_SUCCESS;
HANDLE hFile = NULL; HANDLE hSection = NULL; PVOID pBaseAddress = NULL; UNICODE_STRING FileName = { 0 }; ULONG64 FunctionAddress = 0;
RtlInitUnicodeString(&FileName, L"\\??\\C:\\Windows\\System32\\ntdll.dll");
status = KernelMapFile(FileName, &hFile, &hSection, &pBaseAddress); if (NT_SUCCESS(status)) { DbgPrint("读取内存地址 = %p \n", pBaseAddress); }
FunctionAddress = GetAddressFromFunction(FileName, "ZwQueryDirectoryFile"); DbgPrint("ZwQueryVirtualMemory内存地址 = %p \n", FunctionAddress);
if (FunctionAddress != 0) { BOOLEAN ref = SSDTFunctionHook(FunctionAddress); if (ref == TRUE) { DbgPrint("[+] Hook已挂钩 \n"); } }
Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
|