-
Notifications
You must be signed in to change notification settings - Fork 26
/
ssdt.cpp
288 lines (261 loc) · 8 KB
/
ssdt.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "ssdt.h"
#include "undocumented.h"
#include "pe.h"
#include "ntdll.h"
//structures
struct SSDTStruct
{
LONG* pServiceTable;
PVOID pCounterTable;
#ifdef _WIN64
ULONGLONG NumberOfServices;
#else
ULONG NumberOfServices;
#endif
PCHAR pArgumentTable;
};
//Based on: https://github.com/hfiref0x/WinObjEx64
static SSDTStruct* SSDTfind()
{
static SSDTStruct* SSDT = 0;
if(!SSDT)
{
#ifndef _WIN64
//x86 code
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"KeServiceDescriptorTable");
SSDT = (SSDTStruct*)MmGetSystemRoutineAddress(&routineName);
#else
//x64 code
ULONG kernelSize;
ULONG_PTR kernelBase = (ULONG_PTR)Undocumented::GetKernelBase(&kernelSize);
if(kernelBase == 0 || kernelSize == 0)
return NULL;
// Find KiSystemServiceStart
const unsigned char KiSystemServiceStartPattern[] = { 0x8B, 0xF8, 0xC1, 0xEF, 0x07, 0x83, 0xE7, 0x20, 0x25, 0xFF, 0x0F, 0x00, 0x00 };
const ULONG signatureSize = sizeof(KiSystemServiceStartPattern);
bool found = false;
ULONG KiSSSOffset;
for(KiSSSOffset = 0; KiSSSOffset < kernelSize - signatureSize; KiSSSOffset++)
{
if(RtlCompareMemory(((unsigned char*)kernelBase + KiSSSOffset), KiSystemServiceStartPattern, signatureSize) == signatureSize)
{
found = true;
break;
}
}
if(!found)
return NULL;
// lea r10, KeServiceDescriptorTable
ULONG_PTR address = kernelBase + KiSSSOffset + signatureSize;
LONG relativeOffset = 0;
if((*(unsigned char*)address == 0x4c) &&
(*(unsigned char*)(address + 1) == 0x8d) &&
(*(unsigned char*)(address + 2) == 0x15))
{
relativeOffset = *(LONG*)(address + 3);
}
if(relativeOffset == 0)
return NULL;
SSDT = (SSDTStruct*)(address + relativeOffset + 7);
#endif
}
return SSDT;
}
PVOID SSDT::GetFunctionAddress(const char* apiname)
{
//read address from SSDT
SSDTStruct* SSDT = SSDTfind();
if(!SSDT)
{
DbgPrint("[DeugMessage] SSDT not found...\r\n");
return 0;
}
ULONG_PTR SSDTbase = (ULONG_PTR)SSDT->pServiceTable;
if(!SSDTbase)
{
DbgPrint("[DeugMessage] ServiceTable not found...\r\n");
return 0;
}
ULONG readOffset = NTDLL::GetExportSsdtIndex(apiname);
if(readOffset == -1)
return 0;
if(readOffset >= SSDT->NumberOfServices)
{
DbgPrint("[DeugMessage] Invalid read offset...\r\n");
return 0;
}
#ifdef _WIN64
return (PVOID)((SSDT->pServiceTable[readOffset] >> 4) + SSDTbase);
#else
return (PVOID)SSDT->pServiceTable[readOffset];
#endif
}
static void InterlockedSet(LONG* Destination, LONG Source)
{
//Change memory properties.
PMDL g_pmdl = IoAllocateMdl(Destination, sizeof(LONG), 0, 0, NULL);
if(!g_pmdl)
return;
MmBuildMdlForNonPagedPool(g_pmdl);
LONG* Mapped = (LONG*)MmMapLockedPages(g_pmdl, KernelMode);
if(!Mapped)
{
IoFreeMdl(g_pmdl);
return;
}
InterlockedExchange(Mapped, Source);
//Restore memory properties.
MmUnmapLockedPages((PVOID)Mapped, g_pmdl);
IoFreeMdl(g_pmdl);
}
#ifdef _WIN64
static PVOID FindCaveAddress(PVOID CodeStart, ULONG CodeSize, ULONG CaveSize)
{
unsigned char* Code = (unsigned char*)CodeStart;
for(unsigned int i = 0, j = 0; i < CodeSize; i++)
{
if(Code[i] == 0x90 || Code[i] == 0xCC) //NOP or INT3
j++;
else
j = 0;
if(j == CaveSize)
return (PVOID)((ULONG_PTR)CodeStart + i - CaveSize + 1);
}
return 0;
}
#endif //_WIN64
HOOK SSDT::Hook(const char* apiname, void* newfunc)
{
SSDTStruct* SSDT = SSDTfind();
if(!SSDT)
{
DbgPrint("[DeugMessage] SSDT not found...\r\n");
return 0;
}
ULONG_PTR SSDTbase = (ULONG_PTR)SSDT->pServiceTable;
if(!SSDTbase)
{
DbgPrint("[DeugMessage] ServiceTable not found...\r\n");
return 0;
}
int FunctionIndex = NTDLL::GetExportSsdtIndex(apiname);
if(FunctionIndex == -1)
return 0;
if((ULONGLONG)FunctionIndex >= SSDT->NumberOfServices)
{
DbgPrint("[DeugMessage] Invalid API offset...\r\n");
return 0;
}
HOOK hHook = 0;
LONG oldValue = SSDT->pServiceTable[FunctionIndex];
LONG newValue;
#ifdef _WIN64
/*
x64 SSDT Hook;
1) find API addr
2) get code page+size
3) find cave address
4) hook cave address (using hooklib)
5) change SSDT value
*/
static ULONG CodeSize = 0;
static PVOID CodeStart = 0;
if(!CodeStart)
{
ULONG_PTR Lowest = SSDTbase;
ULONG_PTR Highest = Lowest + 0x0FFFFFFF;
DbgPrint("[DeugMessage] Range: 0x%p-0x%p\r\n", Lowest, Highest);
CodeSize = 0;
CodeStart = PE::GetPageBase(Undocumented::GetKernelBase(), &CodeSize, (PVOID)((oldValue >> 4) + SSDTbase));
if(!CodeStart || !CodeSize)
{
DbgPrint("[DeugMessage] PeGetPageBase failed...\r\n");
return 0;
}
DbgPrint("[DeugMessage] CodeStart: 0x%p, CodeSize: 0x%X\r\n", CodeStart, CodeSize);
if((ULONG_PTR)CodeStart < Lowest) //start of the page is out of range (impossible, but whatever)
{
CodeSize -= (ULONG)(Lowest - (ULONG_PTR)CodeStart);
CodeStart = (PVOID)Lowest;
DbgPrint("[DeugMessage] CodeStart: 0x%p, CodeSize: 0x%X\r\n", CodeStart, CodeSize);
}
DbgPrint("[DeugMessage] Range: 0x%p-0x%p\r\n", CodeStart, (ULONG_PTR)CodeStart + CodeSize);
}
PVOID CaveAddress = FindCaveAddress(CodeStart, CodeSize, sizeof(HOOKOPCODES));
if(!CaveAddress)
{
DbgPrint("[DeugMessage] FindCaveAddress failed...\r\n");
return 0;
}
DbgPrint("[DeugMessage] CaveAddress: 0x%p\r\n", CaveAddress);
hHook = Hooklib::Hook(CaveAddress, (void*)newfunc);
if(!hHook)
return 0;
newValue = (LONG)((ULONG_PTR)CaveAddress - SSDTbase);
newValue = (newValue << 4) | oldValue & 0xF;
//update HOOK structure
hHook->SSDTindex = FunctionIndex;
hHook->SSDTold = oldValue;
hHook->SSDTnew = newValue;
hHook->SSDTaddress = (oldValue >> 4) + SSDTbase;
#else
/*
x86 SSDT Hook:
1) change SSDT value
*/
newValue = (ULONG)newfunc;
hHook = (HOOK)RtlAllocateMemory(true, sizeof(HOOKSTRUCT));
//update HOOK structure
hHook->SSDTindex = FunctionIndex;
hHook->SSDTold = oldValue;
hHook->SSDTnew = newValue;
hHook->SSDTaddress = oldValue;
#endif
InterlockedSet(&SSDT->pServiceTable[FunctionIndex], newValue);
DbgPrint("[DeugMessage] SSDThook(%s:0x%p, 0x%p)\r\n", apiname, hHook->SSDTold, hHook->SSDTnew);
return hHook;
}
void SSDT::Hook(HOOK hHook)
{
if(!hHook)
return;
SSDTStruct* SSDT = SSDTfind();
if(!SSDT)
{
DbgPrint("[DeugMessage] SSDT not found...\r\n");
return;
}
LONG* SSDT_Table = SSDT->pServiceTable;
if(!SSDT_Table)
{
DbgPrint("[DeugMessage] ServiceTable not found...\r\n");
return;
}
InterlockedSet(&SSDT_Table[hHook->SSDTindex], hHook->SSDTnew);
}
void SSDT::Unhook(HOOK hHook, bool free)
{
if(!hHook)
return;
SSDTStruct* SSDT = SSDTfind();
if(!SSDT)
{
DbgPrint("[DeugMessage] SSDT not found...\r\n");
return;
}
LONG* SSDT_Table = SSDT->pServiceTable;
if(!SSDT_Table)
{
DbgPrint("[DeugMessage] ServiceTable not found...\r\n");
return;
}
InterlockedSet(&SSDT_Table[hHook->SSDTindex], hHook->SSDTold);
#ifdef _WIN64
if(free)
Hooklib::Unhook(hHook, true);
#else
if(free)
RtlFreeMemory(hHook);
#endif
}