HyperDbg Debugger
Loading...
Searching...
No Matches
SsdtHook.c File Reference

Implementation of functions to find SSDT entries for SSDT Hook. More...

#include "pch.h"

Functions

PVOID SyscallHookGetKernelBase (PULONG ImageSize)
 Get the kernel base and Image size.
 
BOOLEAN SyscallHookFindSsdt (PUINT64 NtTable, PUINT64 Win32kTable)
 Find SSDT address of Nt functions and W32Table.
 
PVOID SyscallHookGetFunctionAddress (INT32 ApiNumber, BOOLEAN GetFromWin32k)
 Find entry from SSDT table of Nt functions and W32Table syscalls.
 
NTSTATUS NtCreateFileHook (PHANDLE FileHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength)
 Hook function that hooks NtCreateFile.
 
VOID SyscallHookTest ()
 Make examples for testing hidden hooks.
 

Detailed Description

Implementation of functions to find SSDT entries for SSDT Hook.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-11

Function Documentation

◆ NtCreateFileHook()

NTSTATUS NtCreateFileHook ( PHANDLE FileHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
PIO_STATUS_BLOCK IoStatusBlock,
PLARGE_INTEGER AllocationSize,
ULONG FileAttributes,
ULONG ShareAccess,
ULONG CreateDisposition,
ULONG CreateOptions,
PVOID EaBuffer,
ULONG EaLength )

Hook function that hooks NtCreateFile.

Parameters
FileHandle
DesiredAccess
ObjectAttributes
IoStatusBlock
AllocationSize
FileAttributes
ShareAccess
CreateDisposition
CreateOptions
EaBuffer
EaLength
Returns
NTSTATUS
229{
230 NTSTATUS ConvertStatus;
231 UNICODE_STRING ObjectName;
232 ANSI_STRING FileNameA;
233
234 ObjectName.Buffer = NULL;
235
236 __try
237 {
238 ProbeForRead(FileHandle, sizeof(HANDLE), 1);
239 ProbeForRead(ObjectAttributes, sizeof(OBJECT_ATTRIBUTES), 1);
240 ProbeForRead(ObjectAttributes->ObjectName, sizeof(UNICODE_STRING), 1);
241 ProbeForRead(ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length, 1);
242
244 ObjectName.Length = ObjectAttributes->ObjectName->Length;
245 ObjectName.MaximumLength = ObjectAttributes->ObjectName->MaximumLength;
247 RtlCopyUnicodeString(&ObjectName, ObjectAttributes->ObjectName);
248
249 ConvertStatus = RtlUnicodeStringToAnsiString(&FileNameA, ObjectAttributes->ObjectName, TRUE);
250 LogInfo("NtCreateFile called for : %s", FileNameA.Buffer);
251 }
252 __except (EXCEPTION_EXECUTE_HANDLER)
253 {
254 }
255
256 if (ObjectName.Buffer)
257 {
258 PlatformMemFreePool(ObjectName.Buffer);
259 }
260
262}
#define TRUE
Definition BasicTypes.h:55
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES ObjectAttributes
Definition Hooks.h:131
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK IoStatusBlock
Definition Hooks.h:132
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG FileAttributes
Definition Hooks.h:134
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER AllocationSize
Definition Hooks.h:133
PHANDLE FileHandle
Definition Hooks.h:129
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG ULONG ShareAccess
Definition Hooks.h:135
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG ULONG ULONG ULONG PVOID EaBuffer
Definition Hooks.h:138
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG ULONG ULONG CreateDisposition
Definition Hooks.h:136
PHANDLE ACCESS_MASK DesiredAccess
Definition Hooks.h:130
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG ULONG ULONG ULONG CreateOptions
Definition Hooks.h:137
PHANDLE ACCESS_MASK POBJECT_ATTRIBUTES PIO_STATUS_BLOCK PLARGE_INTEGER ULONG ULONG ULONG ULONG PVOID ULONG EaLength
Definition Hooks.h:139
#define LogInfo(format,...)
Define log variables.
Definition HyperDbgHyperLogIntrinsics.h:71
VOID PlatformMemFreePool(PVOID BufferAddress)
Free (dellocate) a non-paged buffer.
Definition Mem.c:86
PVOID PlatformMemAllocateZeroedNonPagedPool(SIZE_T NumberOfBytes)
Allocate a non-paged buffer (zeroed)
Definition Mem.c:69
NULL()
Definition test-case-generator.py:530
Definition casting.cpp:25
USHORT Length
Definition casting.cpp:26
USHORT MaximumLength
Definition casting.cpp:27
PWSTR Buffer
Definition casting.cpp:28

◆ SyscallHookFindSsdt()

BOOLEAN SyscallHookFindSsdt ( PUINT64 NtTable,
PUINT64 Win32kTable )

Find SSDT address of Nt functions and W32Table.

Parameters
NtTable[Out] Address of Nt Syscall Table
Win32kTable[Out] Address of Win32k Syscall Table
Returns
BOOLEAN Returns true if it can find the nt tables and win32k successfully otherwise returns false
87{
88 ULONG KernelSize = 0;
89 ULONG_PTR KernelBase;
90 const unsigned char KiSystemServiceStartPattern[] = {0x8B, 0xF8, 0xC1, 0xEF, 0x07, 0x83, 0xE7, 0x20, 0x25, 0xFF, 0x0F, 0x00, 0x00};
91 const ULONG SignatureSize = sizeof(KiSystemServiceStartPattern);
92 BOOLEAN Found = FALSE;
93 LONG RelativeOffset = 0;
94 ULONG_PTR AddressAfterPattern;
95 ULONG_PTR Address;
96 SSDTStruct * Shadow;
97 PVOID TableNT;
98 PVOID TableWin32k;
99
100 //
101 // x64 code
102 //
103 KernelBase = (ULONG_PTR)SyscallHookGetKernelBase(&KernelSize);
104
105 if (KernelBase == 0 || KernelSize == 0)
106 return FALSE;
107
108 //
109 // Find KiSystemServiceStart
110 //
111 ULONG KiSSSOffset;
112 for (KiSSSOffset = 0; KiSSSOffset < KernelSize - SignatureSize; KiSSSOffset++)
113 {
114 if (RtlCompareMemory(((unsigned char *)KernelBase + KiSSSOffset), KiSystemServiceStartPattern, SignatureSize) == SignatureSize)
115 {
116 Found = TRUE;
117 break;
118 }
119 }
120
121 if (!Found)
122 return FALSE;
123
124 AddressAfterPattern = KernelBase + KiSSSOffset + SignatureSize;
125 Address = AddressAfterPattern + 7; // Skip lea r10,[nt!KeServiceDescriptorTable]
126
127 //
128 // lea r11, KeServiceDescriptorTableShadow
129 //
130 if ((*(unsigned char *)Address == 0x4c) &&
131 (*(unsigned char *)(Address + 1) == 0x8d) &&
132 (*(unsigned char *)(Address + 2) == 0x1d))
133 {
134 RelativeOffset = *(LONG *)(Address + 3);
135 }
136
137 if (RelativeOffset == 0)
138 return FALSE;
139
140 Shadow = (SSDTStruct *)(Address + RelativeOffset + 7);
141
142 TableNT = (PVOID)Shadow;
143 TableWin32k = (PVOID)((ULONG_PTR)Shadow + 0x20); // Offset showed in Windbg
144
145 *NtTable = (UINT64)TableNT;
146 *Win32kTable = (UINT64)TableWin32k;
147
148 return TRUE;
149}
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned long ULONG
Definition BasicTypes.h:37
UINT64 Address
Definition HyperDbgScriptImports.h:67
PVOID SyscallHookGetKernelBase(PULONG ImageSize)
Get the kernel base and Image size.
Definition SsdtHook.c:21
SSDT structure.
Definition Hooks.h:58

◆ SyscallHookGetFunctionAddress()

PVOID SyscallHookGetFunctionAddress ( INT32 ApiNumber,
BOOLEAN GetFromWin32k )

Find entry from SSDT table of Nt functions and W32Table syscalls.

Parameters
ApiNumberThe Syscall Number
GetFromWin32kIs this syscall from Win32K
Returns
PVOID Returns the address of the function from SSDT, otherwise returns NULL
160{
161 SSDTStruct * SSDT;
162 BOOLEAN Result;
163 ULONG_PTR SSDTbase;
164 UINT64 NtTable, Win32kTable;
165
166 //
167 // Read the address og SSDT
168 //
169 Result = SyscallHookFindSsdt(&NtTable, &Win32kTable);
170
171 if (!Result)
172 {
173 LogError("Err, SSDT not found");
174 return 0;
175 }
176
177 if (!GetFromWin32k)
178 {
179 SSDT = (SSDTStruct *)NtTable;
180 }
181 else
182 {
183 //
184 // Win32k APIs start from 0x1000
185 //
186 ApiNumber = ApiNumber - 0x1000;
187 SSDT = (SSDTStruct *)Win32kTable;
188 }
189
190 SSDTbase = (ULONG_PTR)SSDT->pServiceTable;
191
192 if (!SSDTbase)
193 {
194 LogError("Err, ServiceTable not found");
195 return 0;
196 }
197 return (PVOID)((SSDT->pServiceTable[ApiNumber] >> 4) + SSDTbase);
198}
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
BOOLEAN SyscallHookFindSsdt(PUINT64 NtTable, PUINT64 Win32kTable)
Find SSDT address of Nt functions and W32Table.
Definition SsdtHook.c:86
LONG * pServiceTable
Definition Hooks.h:59

◆ SyscallHookGetKernelBase()

PVOID SyscallHookGetKernelBase ( PULONG ImageSize)

Get the kernel base and Image size.

Parameters
pImageSize[Out] Image size
Returns
PVOID
22{
23 NTSTATUS Status;
25 UNICODE_STRING RoutineName;
26 PVOID ModuleBase = NULL;
27 PSYSTEM_MODULE_INFORMATION SystemInfoBuffer = NULL;
28 ULONG SystemInfoBufferSize = 0;
29
30 RtlInitUnicodeString(&RoutineName, L"ZwQuerySystemInformation");
31 ZwQSI = (ZWQUERYSYSTEMINFORMATION)MmGetSystemRoutineAddress(&RoutineName);
32 if (!ZwQSI)
33 return NULL;
34
35 Status = ZwQSI(SystemModuleInformation,
36 &SystemInfoBufferSize,
37 0,
38 &SystemInfoBufferSize);
39
40 if (!SystemInfoBufferSize)
41 {
42 LogError("Err, ZwQuerySystemInformation (1) failed");
43 return NULL;
44 }
45
46 SystemInfoBuffer = (PSYSTEM_MODULE_INFORMATION)PlatformMemAllocateNonPagedPool(SystemInfoBufferSize * 2);
47
48 if (!SystemInfoBuffer)
49 {
50 LogError("Err, insufficient memory");
51 return NULL;
52 }
53
54 memset(SystemInfoBuffer, 0, SystemInfoBufferSize * 2);
55
56 Status = ZwQSI(SystemModuleInformation,
57 SystemInfoBuffer,
58 SystemInfoBufferSize * 2,
59 &SystemInfoBufferSize);
60
61 if (NT_SUCCESS(Status))
62 {
63 ModuleBase = SystemInfoBuffer->Module.ImageBase;
64 if (ImageSize)
65 *ImageSize = SystemInfoBuffer->Module.ImageSize;
66 }
67 else
68 {
69 LogError("Err, ZwQuerySystemInformation (2) failed");
70 return NULL;
71 }
72
73 ExFreePool(SystemInfoBuffer);
74 return ModuleBase;
75}
NTSTATUS(NTAPI * ZWQUERYSYSTEMINFORMATION)(IN SYSTEM_INFORMATION_CLASS SystemInformationClass, OUT PVOID SystemInformation, IN ULONG SystemInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition Hooks.h:121
struct _SYSTEM_MODULE_INFORMATION * PSYSTEM_MODULE_INFORMATION
@ SystemModuleInformation
Definition Hooks.h:116
PVOID PlatformMemAllocateNonPagedPool(SIZE_T NumberOfBytes)
Allocate a non-paged buffer.
Definition Mem.c:41
ULONG ImageSize
Definition Hooks.h:90
PVOID ImageBase
Definition Hooks.h:89
System Information for modules.
Definition Hooks.h:104
SYSTEM_MODULE_ENTRY Module
Definition Hooks.h:106

◆ SyscallHookTest()

VOID SyscallHookTest ( )

Make examples for testing hidden hooks.

THIS EXAMPLE IS NOT VALID ANYMORE, PLEASE USE !syscall OR !epthook2 COMMANDS

Returns
VOID
272{
273 //
274 // THIS EXAMPLE IS NOT VALID ANYMORE, PLEASE USE !syscall OR !epthook2 COMMANDS
275 //
276
277 //
278 // Note that this syscall number is only valid for Windows 10 1909,
279 // you have to find the syscall number of NtCreateFile based on
280 // Your Windows version, please visit https://j00ru.vexillium.org/syscalls/nt/64/
281 // for finding NtCreateFile's Syscall number for your Windows
282 //
283
284 INT32 ApiNumberOfNtCreateFile = 0x0055;
285 PVOID ApiLocationFromSSDTOfNtCreateFile = SyscallHookGetFunctionAddress(ApiNumberOfNtCreateFile, FALSE);
286
287 if (!ApiLocationFromSSDTOfNtCreateFile)
288 {
289 LogError("Err, address finding for syscall base address");
290 return;
291 }
292
293 //
294 // THIS EXAMPLE IS NOT VALID ANYMORE, PLEASE USE !syscall OR !epthook2 COMMANDS
295 //
296 if (EptHookInlineHook(&g_GuestState[KeGetCurrentProcessorNumberEx(NULL)],
297 ApiLocationFromSSDTOfNtCreateFile,
298 (PVOID)NtCreateFileHook,
299 HANDLE_TO_UINT32(PsGetCurrentProcessId())))
300 {
301 LogInfo("Hook appkied to address of API Number : 0x%x at %llx\n", ApiNumberOfNtCreateFile, ApiLocationFromSSDTOfNtCreateFile);
302 }
303}
signed int INT32
Definition BasicTypes.h:44
BOOLEAN EptHookInlineHook(VIRTUAL_MACHINE_STATE *VCpu, PVOID TargetAddress, PVOID HookFunction, UINT32 ProcessId)
This function applies EPT hook 2 (inline) to the target EPT table.
Definition EptHook.c:1542
VIRTUAL_MACHINE_STATE * g_GuestState
Save the state and variables related to virtualization on each to logical core.
Definition GlobalVariables.h:38
#define HANDLE_TO_UINT32(_var)
Definition MetaMacros.h:39
PVOID SyscallHookGetFunctionAddress(INT32 ApiNumber, BOOLEAN GetFromWin32k)
Find entry from SSDT table of Nt functions and W32Table syscalls.
Definition SsdtHook.c:159
NTSTATUS NtCreateFileHook(PHANDLE FileHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength)
Hook function that hooks NtCreateFile.
Definition SsdtHook.c:217