{"id":275,"date":"2022-08-19T19:30:16","date_gmt":"2022-08-19T17:30:16","guid":{"rendered":"https:\/\/areyou1or0.it\/?p=275"},"modified":"2022-10-30T16:21:12","modified_gmt":"2022-10-30T15:21:12","slug":"hevd-windows-kernel-exploitation-4-null-pointer-dereference","status":"publish","type":"post","link":"https:\/\/areyou1or0.it\/index.php\/2022\/08\/19\/hevd-windows-kernel-exploitation-4-null-pointer-dereference\/","title":{"rendered":"HEVD Windows Kernel Exploitation 4 &#8211; Null Pointer Dereference"},"content":{"rendered":"\n<p>So as we exploit the 3rd vulnerability on HEVD, I&#8217;ll use this as a tradition and will give history on what we&#8217;ve done so far and whats up next:<\/p>\n\n\n\n<ul><li>With Stack Overfow:&nbsp;&nbsp;put your shellcode in userland in an allocated memory and execute in kernelland<\/li><li>With Arbitraty Overwrite: writing the value pointed by what to the memory location referenced by where<\/li><li>With Null Pointer Dereference:&nbsp;writing to a pointer location where the value of the pointer is NULL and used by an application that points to a valid memory location<\/li><\/ul>\n\n\n\n<h2>Strategy:<\/h2>\n\n\n\n<p>The strategy for this blogpost will be as follows:<\/p>\n\n\n\n<p>Initial Phase:<\/p>\n\n\n\n<ul><li>Some Theory<\/li><li>Source Code Review &#8211; very brief<\/li><li>Finding IOCTL<\/li><li>Verifying the vulnerability with the initial script<\/li><li>IDA Analysis #2 After the Initial Exploit:<\/li><\/ul>\n\n\n\n<p>Final Exploit Steps:<\/p>\n\n\n\n<ul><li>create shellcode variable from token stealing shellcode (refer to the previous blogs)<\/li><li>place the shellcode into a string buffer (we&#8217;ll use&nbsp;create_string_buffer&nbsp;from&nbsp;ctypes)<\/li><li>VirtualAlloc() to bypass DEP and allocate memory to copy our shellcode there (we&#8217;ll&nbsp;use&nbsp;memmove()&nbsp;from&nbsp;ctypes this time)<\/li><\/ul>\n\n\n\n<ul><li>Place a pointer to our shellcode buffer at memory address&nbsp;0x4 (we&#8217;ll&nbsp;use&nbsp;memmove()&nbsp;from&nbsp;ctypes this time)<\/li><li>call the shellcode<\/li><\/ul>\n\n\n\n<h2>Theory 1: Non-Paged vs Paged Memory:<\/h2>\n\n\n\n<p>At each bootup, the memeory manager creates 2 memory pool (Paged Pool and NonPaged Pool)<\/p>\n\n\n\n<ul><li>these are dynamically sized<\/li><li>they are used for kernel-components to allocate system memory<\/li><li>they start at a certain size (based on the physical memory in the system)&nbsp;<\/li><li>the size of the pool can grow up to a maximum size (determined by the system at boot time)&nbsp;<\/li><li>The paged pool can page out or can be lowered<\/li><li>The paged pool consists of virtual memory that can be paged in and out of the system.<\/li><li>The NonPaged Pool cannot be paged out (used by drivers so they can be accessed at any Interrupt Request Level)<\/li><\/ul>\n\n\n\n<ul><li>The nonpaged pool consists of virtual memory addresses that are guaranteed to reside in physical memory as long as the corresponding kernel objects are allocated.<\/li><li>To improve performance, systems with a single processor have three &#8220;paged pools&#8221;, and multiprocessor systems have five paged pools.<\/li><\/ul>\n\n\n\n<h2>Theory 2:&nbsp;NULL pointer dereference in Windows OS<\/h2>\n\n\n\n<p>We&#8217;ll use Windows 7 x86 on this blogpost. This can be exploited on Win 10 x32 as well however starting with Win 8, Microsoft mitigated this vulnerability by making&nbsp;NULL page unavailable.&nbsp;<\/p>\n\n\n\n<h2>Source Code Review:<\/h2>\n\n\n\n<p>We have 2 files again: the header file and the source file<\/p>\n\n\n\n<p>As the previous blogpost explained, header file will create a few variables to initialize and call later in the source code file. We&#8217;ll have a look at the source code file to understand the vulnerability:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Driver\/HEVD\/Windows\/NullPointerDereference.h\">https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Driver\/HEVD\/Windows\/NullPointerDereference.h<\/a><\/li><li><a href=\"https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Driver\/HEVD\/Windows\/NullPointerDereference.c\">https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Driver\/HEVD\/Windows\/NullPointerDereference.c<\/a><\/li><\/ul>\n\n\n\n<p>The initial part of the code compare the UserValue with the value 0xBAD0B0B0, if they are different,&nbsp;<em>NullPointerDereference<\/em>&nbsp;is set to NULL.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"964\" height=\"464\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-9.png\" alt=\"\" class=\"wp-image-277\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-9.png 964w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-9-300x144.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-9-768x370.png 768w\" sizes=\"(max-width: 964px) 100vw, 964px\" \/><\/figure>\n\n\n\n<p>In the secure version, the developer checks if NullPointerDereference is not set to NULL<\/p>\n\n\n\n<p>In the vulnerable version, no checks are made.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"625\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-13-1024x625.png\" alt=\"\" class=\"wp-image-281\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-13-1024x625.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-13-300x183.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-13-768x469.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-13.png 1376w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The vulnerability is so easy to read and detect in the source file so I&#8217;ll focus on more how it looks like on IDA and WinDBG in this blogpost.<\/p>\n\n\n\n<h2>Finding the IOCTL Code:<\/h2>\n\n\n\n<p>As always, we&#8217;ll check&nbsp;IrpDeviceIoCtlHandler function and where NullPointerDereferenceIOCTLHandler call is, which will follow to the TriggerNullPointerDereference call. When we follow in this order, the IOCTL shows up as 0x22202Bh.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"203\" height=\"146\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-8.png\" alt=\"\" class=\"wp-image-276\"\/><\/figure>\n\n\n\n<h2>Initial Exploit<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"378\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-1024x378.png\" alt=\"\" class=\"wp-image-282\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-1024x378.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-300x111.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-768x284.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-1536x567.png 1536w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46-1568x579.png 1568w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-19-at-17.57.46.png 1744w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2>IDA Analysis #2 After the Initial Exploit:<\/h2>\n\n\n\n<p>Let&#8217;s look at the&nbsp;<strong><em>TriggerNullPointerDereference<\/em><\/strong>&nbsp;function in disassembly:<\/p>\n\n\n\n<p>there is a ExAllocatePoolWithTag API call with the following parameters before the call and between the previous call:<\/p>\n\n\n\n<ul><li>push 6B636148h (which is kacH in ASCII)<\/li><li>push 8&nbsp;<\/li><li>push edi (which is 0 as it was cleared few lines before with xor edi, edi)<\/li><\/ul>\n\n\n\n<p>When we look at the API call reference:&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/ddi\/wdm\/nf-wdm-exallocatepoolwithtag\">https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/ddi\/wdm\/nf-wdm-exallocatepoolwithtag<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"188\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-12-1024x188.png\" alt=\"\" class=\"wp-image-280\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-12-1024x188.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-12-300x55.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-12-768x141.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-12.png 1210w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>According to the syntax each paramter value refers as below:<\/p>\n\n\n\n<ul><li>PoolType: 0 (which refers to NonPagedPool)<\/li><li>NumberOfBytes: 8<\/li><li>Tag: Hack<\/li><\/ul>\n\n\n\n<p>The next 3 lines after ExAllocatePoolWithTag call is also worth noting:<\/p>\n\n\n\n<ul><li>mov esi,eax: move the API call return value to esi<\/li><li>cmp esi, edi: compare the API call return value&nbsp;&nbsp;with esi (0) if it&#8217;s 0<\/li><li>jnz short loc_14E5B &#8211;&gt; when the result is 0, we&#8217;ll trigger the exploit<\/li><\/ul>\n\n\n\n<p>When we check the manual for ExAllocatePoolWithTag&nbsp;&nbsp;API call, its mentioned under Return value section as below:<\/p>\n\n\n\n<ul><li><em>ExAllocatePoolWithTag returns NULL if there is insufficient memory in the free pool to satisfy the request.<\/em><\/li><\/ul>\n\n\n\n<p>When we follow the path, we see that our user value is compared with 0xBAD0B0B0h<\/p>\n\n\n\n<p>jnz will take place&nbsp;if those values do NOT match. So we need to make sure our user provided value is not 0xBAD0B0B0h<\/p>\n\n\n\n<p>When we go down in the code, we see the following last line which triggers the exploit:<\/p>\n\n\n\n<p>call dword ptr [esi+4] &#8211;&gt; esi was 0 as we saw before and we have a pointer at address 0x00000004<\/p>\n\n\n\n<ul><li>That\u2019s going to reside on the&nbsp;NULL page<\/li><li>Our code will dereference&nbsp;a null pointer<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"549\" height=\"135\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-11.png\" alt=\"\" class=\"wp-image-279\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-11.png 549w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-11-300x74.png 300w\" sizes=\"(max-width: 549px) 100vw, 549px\" \/><\/figure>\n\n\n\n<h2>NtAllocateVirtualMemory<\/h2>\n\n\n\n<p>We&#8217;ll refer to the original API documentation:&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/ddi\/ntifs\/nf-ntifs-ntallocatevirtualmemory\">https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/ddi\/ntifs\/nf-ntifs-ntallocatevirtualmemory<\/a><\/p>\n\n\n\n<p>Well use NtAllocateVirtualMemory call to allocate a shellcode buffer at the pointer location esi+4 as given above<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"299\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-10-1024x299.png\" alt=\"\" class=\"wp-image-278\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-10-1024x299.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-10-300x88.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-10-768x224.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/image-10.png 1102w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>result = ntdll.NtAllocateVirtualMemory(GetCurrentProcess(),pointer(c_void_p(1)),0,pointer(c_ulong(4096)),0x3000,0x40)&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<ul><li>ProcessHandle &#8211;&gt;we&#8217;ll get the handle to the current process using GetCurrentProcess()<\/li><li>BaseAddress &#8211;&gt; will be a pointer to a PVOID 0x1<\/li><li>Zerobits &#8211;&gt; 0<\/li><li>RegionSize &#8211;&gt; we will give 4096 bytes<\/li><li>AllocationType &#8211;&gt; 0x3000&nbsp;&nbsp;(the constant hex value for MEM_COMMIT | MEM_RESERVE)<\/li><li>Protect &#8211;&gt; 0x40 (the constant hex value for PAGE_EXECUTE_READWRITE)<\/li><\/ul>\n\n\n\n<p>result returning 0 means we mapped a NULL page<\/p>\n\n\n\n<p>References:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/previous-versions\/windows\/internet-explorer\/ie-developer\/windows-scripting\/reference\/ijsdebugdatatarget-allocatevirtualmemory-method\">https:\/\/docs.microsoft.com\/en-us\/previous-versions\/windows\/internet-explorer\/ie-developer\/windows-scripting\/reference\/ijsdebugdatatarget-allocatevirtualmemory-method<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/memory\/memory-protection-constants\">https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/memory\/memory-protection-constants<\/a><\/li><\/ul>\n\n\n\n<h2>Final Exploit<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>import struct, sys, ctypes<br>from ctypes import *<br>from ctypes.wintypes import *<br>from subprocess import *\u00a0<br><br>## DLLs<br>\u00a0\u00a0<br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/1-1.png\" alt=\"\"><br><br>## NtAllocateVirtualMemory\u00a0\u00a0<br><br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/2-1-1024x157.png\" alt=\"\"><br><br>## shellcode\u00a0\u00a0<br><br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/3-1-1024x517.png\" alt=\"\"><br><br>## VirtualAlloc\u00a0\u00a0<br><br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/4-1-1024x110.png\" alt=\"\"><br><br>## CreateFileA &amp; DeviceIOControl\u00a0\u00a0\u00a0<br><br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/5-1-1024x195.png\" alt=\"\"><br><br>## buffer and call the shell\u00a0\u00a0\u00a0<br><br><img decoding=\"async\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/08\/6-1.png\" alt=\"\"><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>So as we exploit the 3rd vulnerability on HEVD, I&#8217;ll use this as a tradition and will give history on what we&#8217;ve done so far and whats up next: With Stack Overfow:&nbsp;&nbsp;put your shellcode in userland in an allocated memory and execute in kernelland With Arbitraty Overwrite: writing the value pointed by what to the&hellip; <a class=\"more-link\" href=\"https:\/\/areyou1or0.it\/index.php\/2022\/08\/19\/hevd-windows-kernel-exploitation-4-null-pointer-dereference\/\">Continue reading <span class=\"screen-reader-text\">HEVD Windows Kernel Exploitation 4 &#8211; Null Pointer Dereference<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts\/275"}],"collection":[{"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":4,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts\/275\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}