-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwin32k.cpp
245 lines (184 loc) · 5.32 KB
/
win32k.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
#include <ntifs.h>
#include "winnt.h"
PEPROCESS
GetProcessByName(
PWCHAR wszProcessName,
PETHREAD *FirstThread OPTIONAL
);
PSERVICE_DESCRIPTOR_TABLE KeServiceDescriptorTableShadow;
PVOID W32BaseAddress;
PVOID g_SelfBase;
PVOID WinStbBase;
BOOLEAN
FindShadowTable(
)
/**
Find KeServiceDescriptorTableShadow
*/
{
ULONG Sdt = (ULONG) KeServiceDescriptorTable;
KdPrint(("KeServiceDescriptorTable = %X\n", Sdt));
for (ULONG Shadow = Sdt - PAGE_SIZE; Shadow < Sdt + PAGE_SIZE; Shadow += sizeof(SERVICE_DESCRIPTOR_TABLE))
{
if (MmIsAddressValid((PVOID)Shadow))
{
SERVICE_DESCRIPTOR_TABLE *ShadowPtr = (SERVICE_DESCRIPTOR_TABLE*) Shadow;
if (ShadowPtr != KeServiceDescriptorTable &&
ShadowPtr->ServiceTable == KeServiceDescriptorTable->ServiceTable &&
ShadowPtr->ArgumentTable == KeServiceDescriptorTable->ArgumentTable &&
ShadowPtr->TableSize == KeServiceDescriptorTable->TableSize)
{
KeServiceDescriptorTableShadow = ShadowPtr;
return TRUE;
}
}
}
return FALSE;
}
PVOID
RtlFindImageProcedureByNameOrPointer(
IN PVOID Base,
IN PCHAR FunctionName OPTIONAL,
IN PVOID FunctionEntry OPTIONAL
)
{
PIMAGE_DOS_HEADER mz;
PIMAGE_FILE_HEADER pfh;
PIMAGE_OPTIONAL_HEADER poh;
PIMAGE_EXPORT_DIRECTORY pexd;
PULONG AddressOfFunctions;
PULONG AddressOfNames;
PUSHORT AddressOfNameOrdinals;
ULONG i;
// Get headers
*(PUCHAR*)&mz = (PUCHAR)Base;
*(PUCHAR*)&pfh = (PUCHAR)Base + mz->e_lfanew + sizeof(IMAGE_NT_SIGNATURE);
*(PIMAGE_FILE_HEADER*)&poh = pfh + 1;
// Get export
*(PUCHAR*)&pexd = (PUCHAR)Base + poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
*(PUCHAR*)&AddressOfFunctions = (PUCHAR)Base + pexd->AddressOfFunctions;
*(PUCHAR*)&AddressOfNames = (PUCHAR)Base + pexd->AddressOfNames;
*(PUCHAR*)&AddressOfNameOrdinals = (PUCHAR)Base + pexd->AddressOfNameOrdinals;
// Find function
for( i=0; i<pexd->NumberOfNames; i++ )
{
PCHAR name = ((char*)Base + AddressOfNames[i]);
PVOID addr = (PVOID*)((ULONG)Base + AddressOfFunctions[AddressOfNameOrdinals[i]]);
if (ARGUMENT_PRESENT (FunctionName))
{
if( !strcmp( name, FunctionName ) )
{
return addr;
}
}
else if (ARGUMENT_PRESENT (FunctionEntry))
{
if (FunctionEntry == addr)
return name;
}
else
{
ASSERTMSG ("SHOULD NOT REACH HERE", ARGUMENT_PRESENT(FunctionName) || ARGUMENT_PRESENT(FunctionEntry));
}
}
return NULL;
}
PEPROCESS CsrProcess;
KAPC_STATE ApcState;
VOID
W32PrepareCall(
)
{
KeStackAttachProcess ((PKPROCESS)CsrProcess, &ApcState);
}
VOID
W32ReleaseCall(
)
{
KeUnstackDetachProcess (&ApcState);
}
NTSTATUS
W32FindAndSwapIAT(
)
/*++
Routine Description
This function performs search of WIN32K.SYS image and fill our
IAT enties of winstb.sys with win32k.sys export symbols.
Arguments
None
Return Address
NTSTATUS of operation
--*/
{
NTSTATUS Status = STATUS_NOT_FOUND;
if ((g_SelfBase = FindSelfBase()) == NULL)
goto _exit0;
KdPrint(("Found self base at %X\n", g_SelfBase));
if (!CsrProcess)
{
CsrProcess = GetProcessByName (L"csrss.exe", NULL);
if (!CsrProcess)
{
KdPrint(("Could not find csrss\n"));
goto _exit0;
}
}
KdPrint(("CsrProcess %x\n", CsrProcess));
W32PrepareCall ();
if (!FindShadowTable())
goto _exit1;
KdPrint(("Found shadow table at %X\n", KeServiceDescriptorTableShadow));
if (!FindBaseAndSize (KeServiceDescriptorTableShadow[1].ServiceTable, &W32BaseAddress, NULL))
goto _exit1;
KdPrint(("Found win32k base at %X\n", W32BaseAddress));
WinStbBase = __base();
KdPrint(("winstb base %X\n", WinStbBase));
if (!WinStbBase)
goto _exit1;
// Get nt headers
PIMAGE_NT_HEADERS W32NtHeaders = (PIMAGE_NT_HEADERS) RtlImageNtHeader (W32BaseAddress);
PIMAGE_NT_HEADERS SelfNtHeaders = (PIMAGE_NT_HEADERS) RtlImageNtHeader (g_SelfBase);
ULONG ImportDescriptorSize = 0;
// Get our import
PIMAGE_IMPORT_DESCRIPTOR ImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)
RtlImageDirectoryEntryToData( g_SelfBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ImportDescriptorSize);
KdPrint(("ImpDesc = %X Size = %X\n", ImpDesc, ImportDescriptorSize));
if (ImpDesc == NULL)
{
Status = STATUS_INVALID_IMAGE_FORMAT;
goto _exit1;
}
for (; ImpDesc->Name; ImpDesc++)
{
char *DllName = (LPSTR) RVATOVA (ImpDesc->Name, g_SelfBase);
KdPrint(("Dllname %s\n", DllName));
char LocalDllName[128];
strcpy (LocalDllName, DllName);
_strupr (LocalDllName);
if (!strcmp (LocalDllName, "WINSTB.SYS"))
{
//
// Find WINSTB.SYS
//
ULONG RvaOfThunks = ImpDesc->FirstThunk;
ASSERT (ImpDesc->TimeDateStamp != -1);
for (PIMAGE_THUNK_DATA Thunk = (PIMAGE_THUNK_DATA)RVATOVA(RvaOfThunks,g_SelfBase); Thunk->u1.Function; Thunk++)
{
PCHAR Name = (PCHAR) RtlFindImageProcedureByNameOrPointer (WinStbBase, 0, (PVOID)Thunk->u1.Function);
KdPrint(("Function = %X, Name %s ", Thunk->u1.Function, Name));
if (strcmp (Name, "__base") != 0)
{
PVOID Entry = RtlFindImageProcedureByNameOrPointer (W32BaseAddress, Name, 0);
KdPrint(("W32 = %X", Entry));
Thunk->u1.Function = (ULONG) Entry;
}
KdPrint(("\n"));
}
Status = STATUS_SUCCESS;
break;
}
}
_exit1:
_exit0:
return Status;
}