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
| #include <string.h> #include <ntifs.h> #include <ntdef.h> #include <ntstatus.h> #include <ntddk.h> #include <ntstrsafe.h> #define MAX_BACKTRACE_DEPTH 0x20 #define SYM L"\\??\\Hook_API" #define kprintf(format, ...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, format, __VA_ARGS__) typedef UINT64 u64; typedef UINT32 u32; typedef UINT16 u16; typedef UINT8 u8;
u8 mov_jmp_rax1[12] = { 0x48,0xB8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xFF,0xE0 }, mov_jmp_rax2[12] = { 0x48,0xB8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xFF,0xE0 }, origin1[12] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00 }, origin2[12] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00 };
u8* HookAddr1 = NULL, * HookAddr2 = NULL, Hooked1 = FALSE, Hooked2 = FALSE;
KIRQL writeProtectOFFx64() { KIRQL OldIrql; OldIrql = KeRaiseIrqlToDpcLevel(); __writecr0(__readcr0() & ~0x10000); _disable(); return OldIrql; }
void writeProtectONx64(KIRQL OldIrql) { _enable(); __writecr0(__readcr0() | 0x10000); KeLowerIrql(OldIrql); }
NTSTATUS dohook1(u8 HOOK) { Hooked1 = HOOK; KIRQL irql = writeProtectOFFx64(); if (HookAddr1) { if (!HOOK) { memcpy(HookAddr1, origin1, sizeof(origin1)); } else { memcpy(HookAddr1, mov_jmp_rax1, sizeof(mov_jmp_rax1)); }
} writeProtectONx64(irql); return STATUS_SUCCESS; } NTSTATUS dohook2(u8 HOOK) { Hooked2 = HOOK; KIRQL irql = writeProtectOFFx64(); if (HookAddr2) { if (!HOOK) { memcpy(HookAddr2, origin2, sizeof(origin2)); } else { memcpy(HookAddr2, mov_jmp_rax2, sizeof(mov_jmp_rax2)); }
} writeProtectONx64(irql); return STATUS_SUCCESS; }
typedef NTSTATUS(*createfile_ptr)( _Out_ PHANDLE FileHandle, _In_ ACCESS_MASK DesiredAccess, _In_ PCOBJECT_ATTRIBUTES ObjectAttributes, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _In_opt_ PLARGE_INTEGER AllocationSize, _In_ ULONG FileAttributes, _In_ ULONG ShareAccess, _In_ ULONG CreateDisposition, _In_ ULONG CreateOptions, _In_reads_bytes_opt_(EaLength) PVOID EaBuffer, _In_ ULONG EaLength );
typedef NTSTATUS(*readfile_ptr)( _In_ HANDLE FileHandle, _In_opt_ HANDLE Event, _In_opt_ PIO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcContext, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _Out_writes_bytes_(Length) PVOID Buffer, _In_ ULONG Length, _In_opt_ PLARGE_INTEGER ByteOffset, _In_opt_ PULONG Key );
HANDLE FileHandler = NULL; NTSTATUS mycreatefile( _Out_ PHANDLE FileHandle, _In_ ACCESS_MASK DesiredAccess, _In_ PCOBJECT_ATTRIBUTES ObjectAttributes, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _In_opt_ PLARGE_INTEGER AllocationSize, _In_ ULONG FileAttributes, _In_ ULONG ShareAccess, _In_ ULONG CreateDisposition, _In_ ULONG CreateOptions, _In_reads_bytes_opt_(EaLength) PVOID EaBuffer, _In_ ULONG EaLength ) { dohook1(FALSE); createfile_ptr createfile = (createfile_ptr)HookAddr1; NTSTATUS status = createfile( FileHandle, DesiredAccess, ObjectAttributes, IoStatusBlock, AllocationSize, FileAttributes, ShareAccess, CreateDisposition, CreateOptions, EaBuffer, EaLength ); if (!wcscmp(ObjectAttributes->ObjectName->Buffer, L"\\??\\C:\\card.txt")) { kprintf("Line %d: Reading card.txt detected in NtCreateFile\n", __LINE__); FileHandler = *FileHandle; } dohook1(TRUE); return status; }
NTSTATUS myreadfile( _In_ HANDLE FileHandle, _In_opt_ HANDLE Event, _In_opt_ PIO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcContext, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _Out_writes_bytes_(Length) PVOID Buffer, _In_ ULONG Length, _In_opt_ PLARGE_INTEGER ByteOffset, _In_opt_ PULONG Key ) { dohook2(FALSE); readfile_ptr readfile = (readfile_ptr)HookAddr2; NTSTATUS status = readfile( FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, Buffer, Length, ByteOffset, Key ); if (FileHandler && FileHandler == FileHandle) { kprintf("Line %d: Reading card.txt detected in NtReadFile\n", __LINE__); u8 CorrectPair[] = "ACE-4051574845\x00"; memset(Buffer, 0, 0x1000); memcpy(Buffer, CorrectPair, strlen(CorrectPair)); } dohook2(TRUE); return status; }
void DeleteDevice(PDRIVER_OBJECT pDriver) { kprintf("Line %d: Deleting Driver\n", __LINE__); if (pDriver->DeviceObject) { UNICODE_STRING DeviceName; RtlInitUnicodeString(&DeviceName, SYM); IoDeleteSymbolicLink(&DeviceName); IoDeleteDevice(pDriver->DeviceObject); } kprintf("Line %d: Driver Deleted\n", __LINE__); }
void DriverUnload(PDRIVER_OBJECT pDriver) { kprintf("Line %d: Unloading Driver\n", __LINE__); if (Hooked1) { dohook1(FALSE); } if (Hooked2) { dohook2(FALSE); } DeleteDevice(pDriver); kprintf("Line %d: Driver Unloaded\n", __LINE__); }
NTSTATUS DriverEntry( _In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath ) { DriverObject->DriverUnload = DriverUnload; kprintf("Line %d: Driver Loaded, RegistryPath : %S\n", __LINE__, RegistryPath->Buffer); HookAddr1 = (u64)NtCreateFile; HookAddr2 = (u64)NtReadFile; kprintf("Line %d: HookAddr1 : 0x%llx, HookAddr2 : 0x%llx\n", __LINE__, HookAddr1, HookAddr2); if (!HookAddr1 || !HookAddr2) { kprintf("Line %d: HookAddr1 or HookAddr2 is NULL\n", __LINE__); return STATUS_UNSUCCESSFUL; } memcpy(origin1, HookAddr1, sizeof(origin1)); memcpy(origin2, HookAddr2, sizeof(origin2)); *((u64*)(mov_jmp_rax1 + 2)) = (u64)mycreatefile; *((u64*)(mov_jmp_rax2 + 2)) = (u64)myreadfile; dohook1(TRUE); dohook2(TRUE); kprintf("Line %d: Hooked NtCreateFile and NtReadFile\n", __LINE__); return STATUS_SUCCESS; }
|