<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by VL on Medium]]></title>
        <description><![CDATA[Stories by VL on Medium]]></description>
        <link>https://medium.com/@VL1729_JustAT3ch?source=rss-ebf5acc09c4e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*K8mWoFWzfpA8_fJE</url>
            <title>Stories by VL on Medium</title>
            <link>https://medium.com/@VL1729_JustAT3ch?source=rss-ebf5acc09c4e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:31:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@VL1729_JustAT3ch/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[BFS 2022 exploitation challenge]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/intro-6011bf5aefc6?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6011bf5aefc6</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Fri, 06 Jan 2023 19:52:41 GMT</pubDate>
            <atom:updated>2023-01-08T06:54:23.993Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro</h3><p>This is my take on the BFS 2022 exploitation challenge. The description can be found using the following link</p><p><a href="https://labs.bluefrostsecurity.de/blog.html/2022/10/25/bfs-ekoparty-2022-exploitation-challenges/">https://labs.bluefrostsecurity.de/blog.html/2022/10/25/bfs-ekoparty-2022-exploitation-challenges/</a></p><p>System info</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/0*3jrT9w9FNS3bsExJ" /></figure><h3>Reversing the program</h3><p>The program is a simple server running on port 31415</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/0*4IWhorpdVtdwezQg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/0*F0Xz_1EUI6aTMYZI" /></figure><p>Opening the program in IDA we can see the first thing is the allocation of a page of</p><p>executable memory using VirtualAlloc</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/0*FNWeuJPIa6ctJWua" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/0*NtOmcQXfgso-acYG" /></figure><p>This will come into play later on.</p><p>The first thing is to understand how the communication with the server is done.</p><p>First we can see that in order to establish a connection the packet must contain the string “Hello”.</p><p>On line 38, sub_7FF63FF41160 will check if the data received contains “Hello”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/0*xaqJ9iSC9caA7RqS" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/0*4fyckvl2IwdWF_b4" /></figure><p>On line 49 we can see the function which handles the request once the connection is established</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*YQWwtchH5c6ntBA-" /></figure><p>The first thing that stands out in the handler function is that the previously mentioned allocated memory is being filled with the following bytes</p><ul><li><strong>0x5050505050505050</strong></li><li><strong>0xCF58585858585858</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/0*2Q1M5PfjCXnAch7Z" /></figure><p>The next few lines of code specify some checks which are done on the sent data:</p><ul><li><strong>Size</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/0*WoevGFfYRSSCfmdV" /></figure><ul><li><strong>Cookie value: </strong>Should contain the value 0x323230326F6B45</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/0*pxPmdXEUGCFFimVR" /></figure><ul><li><strong>Packet type: </strong>Must be of type ‘T’</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/0*WY6wg4mpU2dPYvgE" /></figure><ul><li><strong>Packet size:</strong> Cannot exceed 3840</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/0*_CGzGgIRMwjPvHdP" /></figure><p>Now if all the conditions are met, the server will receive another data from the user and send back a message with the received data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/0*wNRh2YXzEo-gSurS" /></figure><p>Now, in case the type specified in the packet is ‘X’ then the server will execute some function</p><p>Based on the received data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/0*qFjz3SCU6aEVI3UL" /></figure><p>Cool, so far nothing criminal.</p><h3>Vulnerability analysis</h3><p>First thing to notice is that the size of the data the server receives once all the initial conditions are met is supplied by the user and stored in to the stack before being copied into the allocated memory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/0*1FHxzxWrACwhq6O-" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/0*BjgNmNdRdLfRBo_W" /></figure><p>It seems that supplied size is checked so to avoid exceeding 3840 which is the size of the stack buffer it is eventually copied into, to avoid overflowing the buffer.</p><p>The problem is that v10, which gets the data size from the initial packet, is forced to be a signed value which means a sign extension will take place</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/0*Tnk-D1YjYwTW-a_r" /></figure><p>This means a value of 0x8000 will be signed and extended and will become a negative number which will pass the check. While the recv() will zero extend the value and consider it a positive number</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/0*gdXnX47-lERm6WHB" /></figure><p>In which case the value 0x8000 will be much larger than 3840, so it will overflow the CmdLine buffer.</p><p>Now, while the case is a regular buffer overflow, the idea is to use the the buffer overflow in order to trigger the function on line 53</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/0*drgGEL0mCn0VmMb4" /></figure><p>Since the expected packet type is of type ‘T’ and not ‘X’ the code will not reach this block.</p><p>But looking at the stack layout, we can see that the packet type variable is held right after the CmdLine buffer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/0*_aWLiGJ7qzgsmZrO" /></figure><p>This means it’s possible to send a packet of type ‘T’ which will trigger the overflow and overflow the packet type so that the code block on line 53 will be executed.</p><p>This will cause the code to execute the bytes stored in the executable memory allocated at the start of the program.</p><p>While looking at the values stored in the allocated memory we can see the following instructions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/0*bXbFyS8wnybIh_BZ" /></figure><p>Now, in order to hijack the execution the idea which comes to mind is to somehow use the iretd instruction to make the code return to a controlled location. But first, some segmentation theory is needed.</p><h3>Segmentation and iret</h3><p><strong>Segmentation</strong> is a processor related feature which was originally used as a memory scheme to organize memory on a 16 bit system.</p><p>Segmentation uses segment selectors in order to access different code segments of the program. Those segment selectors are stored as entries in a data structure called Global descriptor table(GDT).</p><p>Windows generally ignores segmentation but one important use can be to transition code between 32 bit and 64 bit.</p><p>The transition is happening using the appropriate entries in the GDT for the code segment.</p><p>To do the transition from 32 to 64 bit, 0x33 segment selector is used.</p><p>To do the transition from 64 to 32 bit, 0x23 segment selector is used.</p><p>This will be the key points in order to control the execution of the program using the iretd.</p><p>The following article from malwaretech can be used for a wonderful explanation on the subject:</p><p><a href="https://www.malwaretech.com/2014/02/the-0x33-segment-selector-heavens-gate.html">https://www.malwaretech.com/2014/02/the-0x33-segment-selector-heavens-gate.html</a></p><ul><li><strong>iretd</strong> is an instruction used to return control to the program from an interrupt or an exception.</li></ul><p>In order to do that, iretd instruction uses the following information stored on the stack</p><ul><li>Return instruction pointer</li><li>Code segment selector</li><li>Eflags</li><li>Stack pointer address</li><li>Stack segment selector</li></ul><h3>Execution hijacking</h3><p>The general idea to control the execution flow using the iretd is to set up a fake stack frame for the instruction to use which will include the appropriate values:</p><ul><li>Instruction pointer value</li><li>CS</li><li>Eflags</li><li>Stack pointer value</li><li>SS</li></ul><p>The iret instruction in general can handle values of different types and the specific iretd instruction handles dwords which means the fake stack frame must contain values appropriate for 32 bit. This basically means that the code will do the transition from 32 bit to 64 bit.</p><p>Since the payload sent is present after the iretd instruction which is preceded by a set of “push rax” and “pop rax” instruction, we need to set the appropriate offset set to the overflow so that once these instructions will be executed, the stack will point to the fake stack frame before executing the iretd instruction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OZXdfc0oU81vsxQK" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/0*pH0GddTmHkBugKQ6" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/0*KscWQWhArUV6Pv26" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/0*tQxqRHdm_gNgSc7K" /></figure><p>More information on the more general very cool technique of exception oriented programming can be found in the following article:</p><p><a href="https://ietresearch.onlinelibrary.wiley.com/doi/10.1049/iet-ifs.2015.0372">https://ietresearch.onlinelibrary.wiley.com/doi/10.1049/iet-ifs.2015.0372</a></p><p>As mentioned in the previous section, 0x23 will be used for the code segment. For the stack segment selector, we will have to find the appropriate segment selector.</p><p>By dumping the Global descriptor table data, we can locate the data selector.</p><p>We can see several entries for the data selector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/0*ChjASQ3jD6lnyoPH" /></figure><p>As explained by malwaretech, the segment selector value is calculated based on the combination of the entry in the GDT and the TL+RPL flags. Since we are looking for a data selector in user mode we need the flags:</p><ul><li>TL = 0 (use GDT)</li><li>RPL = 11 to specify ring 3(user mode)</li></ul><p>So the selector candidates will be 0x28 and 0x50 and the segment selector, along with the TL and RPL bits, then becomes:</p><p>0x28 | 0 | 11 = 00101000 | 0 | 11 -&gt; 00101011 = 0x2b</p><p>0x50 | 0 | 11 = 01010000 | 0 | 11 -&gt; 01010011 = 0x53</p><p>Using the 0x2b segment selector, the byte was converted to 0 so that was not very nice. But the second data selector worked i.e. 0x53.</p><p>The stack can be set up to just point to some accessible address such as the allocated memory.</p><p>Now, since I wanted to stay in 64 bit but had to return first to 32 bit, the idea then is as follows:</p><p>iretd -&gt; 32 bit -&gt; jmp 0x33:target_address -&gt; Shellcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/0*BDtDs_vxUJ7jSrJa" /></figure><p>The last thing which needs to be restored after going back to 64 bit is the correct stack pointer which was used before the jump to 32 bit mode.</p><p>But since the argument which the last function received is the CmdLine stack buffer, the stack pointer address will be stored in the rcx register. So it can be retrieved by an instruction such as xchg rcx, rsp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Iy_wiH9gthLfcLGK" /></figure><p>So the steps become:</p><p>iretd -&gt; 32 bit -&gt; jmp 0x33:target_address -&gt; xchg rcx, rsp -&gt; Shellcode.</p><p>The entire process can be executed using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/0*VN8vt6CL847VsnR5" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0um4fqRyGJX6r_5S" /></figure><p>For the complete exploit:</p><p><a href="https://github.com/JustaT3ch/BFS-Exploitation-challanges/blob/main/BFS-Exploitation-challange-2022/bfs2022.py">BFS-Exploitation-challanges/bfs2022.py at main · JustaT3ch/BFS-Exploitation-challanges</a></p><p>Additional useful information links:</p><p><a href="https://resources.infosecinstitute.com/topic/translating-virtual-to-physical-address-on-windows-segmentation/">Translating Virtual to Physical Address on Windows: Segmentation | Infosec Resources</a></p><p><a href="https://www.malwaretech.com/2013/06/rise-of-dual-architecture-usermode.html">https://www.malwaretech.com/2013/06/rise-of-dual-architecture-usermode.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6011bf5aefc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SANS SEC760 Challenge]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/sans-sec760-challenge-ab3a396ee189?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab3a396ee189</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Thu, 22 Dec 2022 12:29:01 GMT</pubDate>
            <atom:updated>2023-01-06T20:00:06.287Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro</h3><p>This is a writeup on the SANS SEC760 babyHeap challenge posted on twitter.</p><p>The original post can be found in the following link:</p><p><a href="https://twitter.com/steph3nsims/status/1233119624658481157">https://twitter.com/steph3nsims/status/1233119624658481157</a></p><h3><strong>Security check and system info</strong></h3><p>Ubuntu 18.04.6 LTS with Glibc version 2.27</p><p>All protections are enabled</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/0*sR5oqoCvkxxNdCqK" /></figure><h3>Running the program</h3><p>When first run , the program expects an input string which results in the hash value displayed in the console message, using SHA256</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/0*aCA2RliNebwfK0nY" /></figure><p>If the value is incorrect or the allowed time(15 seconds) has passed the program exits.</p><p>Since the final value of the comparison is only the final 6 digits of the hash, it is possible to brute force the hashes and find the correct value which results in the displayed hash.</p><p>Using python itertools library to generate all the permutations of 4 letters strings and comparing the values with the displayed value in the console we can brute force the hash using the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*_vMMO5JRaN8Zkqzi" /></figure><h3>Reversing the binary</h3><p>The program’s menu contains several options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*ZaKoX2v4ZKC9lU1NtouvrQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/0*pcNTpBIfUaLoZTCj" /></figure><p>The login functionality presents the user with two inputs: Username and Password.</p><p>On line 19 the provided username is being modified by some function and the result is then compared to the provided password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/0*G2jiDv4i9woGwE1Q" /></figure><p>Before statically reversing the sub_5555555553B0(), which looked time consuming, I decided to debug the application with gdb and trying some values for the username and password.</p><p>Setting a breakpoint before the call to strcmp() ,the value generated by the sub_5555555553B0() looked like a hash.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/0*FU2eogyFsGY5LprJ" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/0*RXifX_W7Xi1JyE0N" /></figure><p>Attempting several known hash algorithms, it was not long before I found the hash in question was an MD5 hash</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/0*NraC-NMHm3oinavh" /></figure><p>This fact will be useful later.</p><p>The create() and delete() functions allow for your standard dynamic memory allocation functionality</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/0*B819mPYCcTn6yyrT" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/0*1FEE6pb8_O4M60z6" /></figure><p>While the info() function displayed the username and included some information regarding the __free_hook array in glibc, which will come in handy later</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/0*-scgWKhOD1mppYBF" /></figure><h3>Locating the vulnerabilities</h3><p>The <strong>first bug</strong> is a format string vulnerability present in the info() function.</p><p>On line 10 the printf() function takes the user supplied username string as the format string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/0*-nqZXq0LA14Gm47u" /></figure><p>The <strong>second bug</strong> is an off-by-one on the heap in the create() function.</p><p>On line 23, the user supplies the data which is handled by a call to get_user_input() which also gets the allocated size by malloc() before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/0*JBu1E4B_-C94HKG_" /></figure><p>In the get_user_input() , on line 6 , there is a read() which copies 1 more than the allocated buffer size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/0*MJu7ADdaySAojemo" /></figure><h3>ASLR bypass</h3><p>As stated before in the reversing section, the info() function resolves the address of __free_hook which is a symbol exported from libc.</p><p>By leaking the __free_hook address we can leak the libc address using the __free_hook offset inside the libc binary.</p><p>In order to leak the __free_hook address, the format string bug in the info() function can be used.</p><p>Since the format string used is the username provided by the user, which also used for the login, a special format string and it’s appropriate MD5 hash must be supplied</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/0*4ewIIn_ND7kFEwhv" /></figure><p>The leak can then be used to also leak the address of system(), which will be used later, using the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/0*G4uf0Gvw-sABK2-u" /></figure><h3>Crafting an arbitrary write</h3><p>In order to hijack the execution of the program ,some useful pointer needs to be overwritten which will allow us to redirect the execution flow.</p><p>Since the leak of the __free_hook, we can overwrite the first pointer of the __free_hook array with an address of whatever code we want to execute by calling the free() function.</p><p>But first in order to be able to write to that address we need to craft a write primitive which will allow us to write to an arbitrary address.</p><p>For crafting the primitive, the off-by-one bug in the create() call will be used.</p><h4>But some heap stuff first…</h4><p>The binary is using tcache which is used in multithreaded programs by providing several arenas for the threads instead of a single arena in libc.</p><p>But without diving into the specifics of tcache(As that deserves it’s own article), the main points are:</p><ul><li>tcache uses bins of different sizes to store the free chunk (this is not specific to Tcache), Which is fancy(or not so much ?) way to say it’s just a list which holds the chunks which were freed by free().</li><li>It’s a singly linked list and as such every free chunk holds a pointer to the next free chunk in the list etc.</li><li>Every requested chunk will be taken from the start of the list and every freed chunk will be attached to the end of the list.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IhLAH90y41NIePfn" /></figure><h4><strong>Now for the main idea…</strong></h4><p>The goal is to link a fake chunk, which is actually the target address, to the list for some chunk size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DOcn_q--jjUlDHR-" /></figure><p>This way when we request that chunk from that list by calling malloc(size x) we can write data to that target address.</p><p>Now, since the pointers of each chunk in the list are placed at the beginning of each chunk, by overwriting them with the target address we can link the target address to the list.</p><p>The only problem is that the bug we have is just an off by one which allows us to overwrite just a single byte.</p><p>So in order to overcome that issue, we will overlap some chunks.</p><p>The trick here is that the first information of the chunk metadata is the chunk header which specifies the size of the chunk and if the chunk is free or in use.</p><p>Thus the first byte basically specifies the size. So we can use the off-by-one to overwrite the chunk size field.</p><p>After establishing that, the next question is what will be the new size?</p><p>Since the goal is to overlap the chunks, the new size needs to be big enough so that the chunk will overlap the next chunk, which basically means bigger then the current size and as big as we want the overlap to be.</p><h4>Anyway, some heap grooming needed!</h4><p>The steps:</p><ol><li>We allocate 3 chunks: A, B, C of some size</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/0*s7rw8dJmxSyRB0Ud" /></figure><p>2. Use chunk A to overwrite chunk B’s size field using the off-by-one bug.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/0*btcy4Ol075HB5bFp" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/0*bHWrYLaOwT_7fQUt" /></figure><p>3. Free chunk C so that it will be placed in the tcache bins</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/0*4E7I9n1voUWtrDsy" /></figure><p>4. Free chunk B</p><p>5. Allocate chunk from the tcache bin of chunk B size so that the new chunk will be allocated in place of chunk B. Now since chunk B is large enough to overlap Chunk C, the pointers of chunk C can be overwritten with the target address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/0*RSkP4GnaPeFA_t0M" /></figure><p>6. The final step is to allocate from the tcache bins where chunk C is located to get rid of it from the list and make the next pointer in the list to be of our target address.</p><p>At the end of the heap grooming we can see that we linked the __free_hook address to the tcache bins using the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/0*EpGm-gdWjGKB9uth" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/0*Ga9sLUavqBN7ir2k" /></figure><h4>Code execution</h4><p>In order to execute shell commands the “/bin/sh” string is set to be executed once</p><p>the call to system() will be made after calling free(), giving us an interactive shell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/0*FdamqQxQFblKb2oR" /></figure><p>The complete exploit script can be found in my github:</p><p><a href="https://github.com/JustaT3ch/SANS-SEC760-Challenge">https://github.com/JustaT3ch/SANS-SEC760-Challenge</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab3a396ee189" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HEVD buffer overflow windows 7 x86/64.]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/hevd-buffer-overflow-windows-7-x86-64-713699ca76bb?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/713699ca76bb</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Sun, 18 Sep 2022 09:51:46 GMT</pubDate>
            <atom:updated>2022-09-18T09:51:46.347Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s begin with 32 bit code. First find the offset to EIP.</p><p>Opening IDA we can see the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*HSczgUZlVjVnMBQRkuMZag.png" /></figure><p>The kernel buffer starts at offset of 2076 from ebp, thus eip will be at an offset of 2080 since the eip located after ebp in the x86 architecture.</p><p>Notice this is not the case with x64, since rbp is not necessarily have to be frame pointer. There, it’s just another register and used as such, and optionally can be used as frame pointer but does not have to be.</p><p>In fact, if we look at the 64-bit version of the same code, we see the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*ZLDlge8HUETO9sbncRB23Q.png" /></figure><p>Here we can see that we are walking the stack using rsp and not rbp. In fact, rbp never used here as a base frame pointer. We can see 3 pushes followed by allocation of 2080 bytes on the stack for local storage. Which means at this point the return address will be 2080 + (8 * 3) = 2080 + 24 = 3004 bytes away from rsp.</p><p>But we can also see that kernel buffer starts at rsp + 32 which means, if we look at that place as the base address for us, which we should since that’s where we start the input which will lead to the overflow, then we can conclude that that rip offset = 2048 + (8 * 3) = 2048 + 24 = 2072.</p><p>Note that since at first rsp points to location of the form rsp — 2080 and the buffer starts at rsp + 32 from there then we get rsp — 2080 + 32 = rsp — 2048. So, this is where 2048 comes from in case you were wondering.</p><p>Confirming the offset, we get a crash on windows 7 32 bits:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*h5_lcofy1E3xIcwoWQct_g.png" /></figure><p>The rest is trivial.</p><p>Going to windows 7 64 bit we can control the register rip using the offset of 2072 as calculated before.</p><p>At this point we should consider how to craft the exploit in such a way so that it won’t break the system.</p><p>Using IDA we can see that after the call which the return of we are hijacking, that it has the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*3WuHRWmn4GE5hORQRiR1nQ.png" /></figure><p>So, if we add those lines to the shellcode AND make sure we restore the stack (if we change it at all) to the previous state then the correct return address will be obtained and the execution will continue as usual, Cool.</p><p>I was lazy with writing a shellcode so I took a made one from the following source:</p><p><a href="https://www.abatchy.com/2018/01/kernel-exploitation-2#token-stealing-payload-windows-7-x64-sp1">https://www.abatchy.com/2018/01/kernel-exploitation-2#token-stealing-payload-windows-7-x64-sp1</a></p><p>It’s a great explanation on the whole process. Any ways the final shellcode is as follows:</p><p>xor rax, rax</p><p>mov rax, gs:[rax + 0x188]</p><p>mov rax, [rax + 0x70]</p><p>mov rcx, rax</p><p>mov r11, rcx</p><p>and r11, 7</p><p>mov rdx, 0x4</p><p>SearchSystemPID:</p><p>mov rax, [rax + 0x188]</p><p>sub rax, 0x188</p><p>cmp [rax + 0x180], rdx</p><p>jne SearchSystemPID</p><p>mov rdx, [rax + 0x208]</p><p>and rdx, 0xfffffffffffffff0</p><p>or rdx, r11</p><p>mov [rcx + 0x208], rdx</p><p>add rsp,0x28</p><p>Ret</p><p>As you can see from the assembly code, no stack place was harmed during the process, and by adding the mentioned instructions we get the execution continue as usual after the shellcode is done.</p><p>And the final exploit looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*IIU5EldCr98FH4gzwfRn0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*GVbPBq6uSweMWGQ5uV0zAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*TjalE3hH-80qRWn0PCOXoA.png" /></figure><p>Now this is a super easy thing as far as exploitation goes, I mean it’s a most vanilla buffer overflow there is but it makes a good intro to the kernel stuff.</p><p>Moreover, the whole point is to ovoid fuzzing as much as possible and being able to look at the code in IDA to find the vulnerable code, the offsets the addresses etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=713699ca76bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[BFS Ekoparty 2019 Exploitation Challenge]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/bfs-ekoparty-2019-exploitation-challenge-975b76de45c0?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/975b76de45c0</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Sat, 17 Sep 2022 16:57:44 GMT</pubDate>
            <atom:updated>2022-09-17T16:59:01.088Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro</h3><p>The official challenge post can be found using the following URL:</p><p><a href="https://labs.bluefrostsecurity.de/blog/2019/09/07/bfs-ekoparty-2019-exploitation-challenge/">BFS Ekoparty 2019 Exploitation Challenge - Override Banking Restrictions to get US Dollars</a></p><p>The goal of the challenge is to remotely exploit the program while bypassing ASLR and DEP on Windows 10 64 bit as stated in the description.</p><p>The system I used for this challenge is Windows 10 21H1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/0*Lx-ttn1DLq4iR9s-" /></figure><p>Reversing the program</p><p>Running the program provides us with the following information</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/0*ZoLrm3EVJKyE5DUm" /></figure><p>So the target is a server which waits for user requests. The first requires us to understand how to communicate with the server, so opening the target program in IDA pro we can see that the server listens on port 54321</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/0*CdnQfaxyzBttyk0_" /></figure><p>The connection is handled by the function on line 31</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/0*aNP5myoBYVxG_dWr" /></figure><p>First the program will receive a header and check if the first 8 bytes contain the following value</p><p>0x393130326F6B45</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/0*nYeCEEFpOBTI5BQw" /></figure><p>The next value provided along with the header is the size value for the message data which the user sends later and this size is checked for upper bound of 512 before being copied to the destination buffer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/0*NxDWzJ6SHbNSCRNf" /></figure><p>Next the program will get the length of the sent message which will be sent back to the user. The program will initialize a function BaseAddress() which will provide the value to be sent back:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/0*J7t3Bk35BarCIwAx" /></figure><p>The BaseAddress() function body is obtained from the buffer on line 39:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/0*dWV8PapzIotwxJHg" /></figure><p>Since v9 is a local variable with the hardcoded value of 62:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/0*9YCfxEq4wYGtnqxO" /></figure><p>The calculation v9 % 256 means that the body of BaseAddress() is stored in a buffer initialized earlier in the program at offset 62.</p><p>We can see the setup of that array on lines 11–12:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/0*_x-e4o7nbZD6SV2C" /></figure><p>Using WinDbg and the following python script to debug the target</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/0*-htPBOepP3a9WNqY" /></figure><p>We can see that the value at offset 62 contains the following bytes</p><p>0x3e 0x48, 0x8b, 0x01, 0xc3, 0xc3, 0xc3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MfsaIjXel4-OzlfB" /></figure><p>which translates to the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/0*kFy4zgPbORot3Uic" /></figure><p>Which is basically used in the BaseAddress() function where rcx register holds the address of the length of the message formed after the call to printf from earlier</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tOexMjjLGkAVIV5q" /></figure><p>So to summarize, the server listens for incoming connections expecting a specific header to be sent in the initial request. Then the server accepts data sent from the user and responds with the length of the message sent in addition to the message.</p><h3>Vulnerability analysis</h3><p>The main vulnerability is a stack based buffer overflow, present in the following code section</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/0*WcULmfxOP0HKSviE" /></figure><p>On line 28 there is a copy of the user supplied data in the request sent to the server, into a buffer present of the stack.</p><p>The length of the message to be copied to the buffer is user controlled while the buffer itself is fixed in length</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/0*SXKBPvYi0HokkiOx" /></figure><p>That means the user can supply a buffer which is more than the length of the buffer. On line 25 there is a check to prevent the overflow by checking that the length of the user supplied value does not exceed 512 which is the length of the buffer.</p><p>The problem is that user_msg_size variable is a signed value</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/0*Q_tR0IkEUcsNg6q6" /></figure><p>Furthermore there is no check to sanitize variables for negative values. So if the user sends a negative value of 0xffffffff, the check on line 25 will be bypassed and since recv gets an unsigned type for user_msg_size, it will convert the variable to a large value which will cause an overflow.</p><p>To summarize the bug:</p><p>Type conversion bug which causes an improper bound check on the length of the supplied data which leads to out of bounds write on the stack.</p><h3>Arbitrary Read primitive</h3><p>By looking at the stack layout of the target we can see that after the vulnerable buffer, we have the variable which contains the address of the length of the message which is eventually sent to the user on line 11</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/0*mhXHkq4eaU6jNNsY" /></figure><p>Since that value will be used to obtain the value to be sent back to the user, by overwriting that variable with a target address we want to read from, BaseAddress() will dereference that address to obtain the appropriate data to send which will result in arbitrary read primitive</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/0*qRo3tV_4nMu12z9b" /></figure><h3>ASLR Bypass</h3><p>Using the read primitive we can bypass ASLR completely. ASLR randomizes two pieces of information I needed to get to proceed with the exploitation process</p><ul><li>The stack addresses</li><li>The addresses of loaded modules</li></ul><p>In order to obtain the addresses of the loaded modules, one way is to query that information using the Process Environment Block(PEB).</p><p>PEB stores the information on all the loaded modules of the process in a linked list which is pointed by _PEB_LDR_DATA</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*My4TC7Rn-zQMqxJ74_6r3Q.png" /></figure><p>PEB is located at offset 0x60 on a 64 bit system in another data structure called Thread Environment block, which contains some important thread related information, But the problem is that we do not have the address of the TEB structure.</p><p>Going back to the initialization of the function BaseAddress, we can see that the array which contains the bytes of the function body, also contains some other instructions which could be interesting.</p><p>TEB can be accessed via segment register GS with the following instruction:</p><p>Mov reg1, QWORD gs[0]</p><p>Looking the the instructions array, we can see that the following instruction is present</p><p>0x65 0x48 0x8b 0x01 0xc3 0xc3 0xc3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/0*Z8Cu8MXU9tXJWJ2U" /></figure><p>Which translates to</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/0*RhDlCJw_MuNk2NBq" /></figure><p>Remember that the value of rcx contains the address of the length of the bytes which is sent to the user which is used in the arbitrary read primitive. Also while the value of the offset which sets the instructions for BaseAddress() is hardcoded 62 in the code, it can be overwritten by the buffer overflow since it is present after the buffer on the stack</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*ILC2QuXeaL2qTKTa" /></figure><p>The by combining all this, if we set rcx to point to the offset in the TEB we want to read from and setting the offset to 0x65, we can get to PEB using offset 0x60 and then locate the _PEB_LDR_DATA and the addresses of the loaded modules using the appropriate offsets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/0*IydHjH-e_dc3-6BV" /></figure><p>In order to obtain stack related addresses we can use TEB parsing again and the stack limits</p><p>Which are present at in a structure which is located at the beginning of the TEB structure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/0*A3ykJS11v_GBkgoE" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/0*9Qpba0yfIDOIdHoP" /></figure><p><strong>Note:</strong></p><p>Another loaded module leak is present in the target binary.</p><p>On line 41 there is a use of WriteProcesssMemory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/0*BNW5COtibzln6bU1" /></figure><p>A pointer to that API call is stored at the following section along with some other Win API’s:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/0*pMkDy_TNpL7DDr-X" /></figure><p>By obtaining the address of the executable module and reading the pointer to WriteProcessMemory API, it is possible to obtain the address of kernel32 without parsing all the list of the loaded modules in PEB.</p><p>I did not see a point in restricting myself only to kernel32 leak since to parse the rest of the modules did not require a lot of effort.</p><h3>Stack Cookie Bypass</h3><p>In order to bypass the stack cookie, the stack leak can be used to leak the cookie value.</p><p>The idea is to scan the stack starting from the stack limit leaked earlier until the buffer payload</p><p>Is reached</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*T8fd6aCzn6XELR1E" /></figure><p>The same stack leak will be used later in order to save the original stack value so the application will not crash.</p><h3>DEP Bypass</h3><p>In order to bypass DEP a call to VirtualProtect with a stack address to make the stack</p><p>executable.</p><p>Using the stack base address leaked at earlier steps and calculating two pages from that address, VirtualProtect will change the permissions on enough pages on the stack so that</p><p>final shellcode will be on an executable page.</p><p>On a 64 bit system, VirtualProtect gets the first 4 parameters using the registers: rcx, rdx, r8, r9.</p><p>Generally other parameters will go on the stack but since VirtualProtect gets only 4, setting up those registers will do.</p><p>To find the ROP gadgets for setting up the registers, using Ropper , all the loaded modules were searched resulting in the following gadgets</p><ul><li>Pop rcx; ret</li><li>Pop rdx; ret</li><li>Pop r8; ret</li><li>Pop r9; pop r10; pop r11; ret</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I2S-OEML3uuhUaPb" /></figure><p>NOTE: Another possible approach would be to use NtContinue. This would allow setting the registers using the Context structure passed as a parameter.</p><p>This approach would provide a way to avoid the search for the needed gadgets.</p><p>In this case, there was no particular issue with the gadgets</p><p>After the call to VirtualProtect a stack pivot gadget along with a “Jmp rsp” gadget were used to execute the final shellcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*53_GYI3vieuNL6Mg" /></figure><h3>Restoring the stack</h3><p>In order to avoid crashing the application once the exploit is executed, the stack needs to be</p><p>restored to contain the appropriate value as in the usual execution flow.</p><p>I chose to save the stack values using the stack leak as before and appending a stack pivot gadget to the end of the shellcode in order to point the stack pointer back to the normal state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OdNro03k8xRDaqpz" /></figure><p>In this case the shellcode was returning back to the stack which allowed me to append the pivot gadget afterwards, but if another shellcode is used then a different approach might need to be taken based on how the shellcode behaves once it’s done.</p><p>The following diagram shows the entire payload</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/0*j3dTNvMEFmGlytrw" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AjikqZf4Yf3VfJfI" /></figure><p>For the complete exploit :</p><p><a href="https://github.com/JustaT3ch/BFS-Ekoparty-2019">https://github.com/JustaT3ch/BFS-Ekoparty-2019</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=975b76de45c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IOCL’s and Windows kernel]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/just-want-to-talk-to-this-windows-kernel-driver-6642f9d27dc9?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6642f9d27dc9</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Sat, 25 Sep 2021 09:27:27 GMT</pubDate>
            <atom:updated>2022-09-18T09:52:38.181Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro:</h3><p>This is just a walkthrough of finding the IO Control routine registered in a driver and the associated IOCTL’s with static analysis using IDA.</p><p>As first step, into researching kernel driver’s possible vulnerabilities, is to talk to the thing, it might be useful to get the IOCTL’s with which the driver will know what we want it do.</p><p>The driver which will be used as an example is the well known HEVD:</p><p><a href="https://github.com/hacksysteam/HackSysExtremeVulnerableDriver">GitHub - hacksysteam/HackSysExtremeVulnerableDriver: HackSys Extreme Vulnerable Driver (HEVD) - Windows &amp; Linux</a></p><p>I chose this one mainly because I was playing around with some of it’s functionally as a reference and since the source code is given, we can confirm the results seen in IDA.</p><h3><strong>The Goal …</strong></h3><p>Find the IOCTL information associated with a specific function which the driver handles.</p><h3>It’s all about Data structures…</h3><p>The first Structure which is important here is the Driver_Object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*EXGfeFU755ZG25Or5kKAeg.png" /></figure><p>At the end of this structure, we have an array of function pointers by the name MajorFunction.</p><p>This array contains routine pointers to handle requests sent to the driver such as create, read, close etc.</p><p>Drivers work with another structure called IRP which is a huge structure which specifies instructions for the driver along with other information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*aAkWJ4qCY_SpSYdeuNapOA.png" /></figure><p>For more information on IRP’s refer to :</p><p><a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_irp"><strong>https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_irp</strong></a></p><p>The function of interest in most cases is the Device IO Control function which is registered with IRP_MJ_DEVICE_CONTROL in the MajorFunction array.</p><p>This function will essentially get a some value and based on that value will decide what to do.</p><p>This value comes in the form of IOCTL which is just a 32-bit value of the following form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*nHQQJqw75D_EzB4rR71_ng.png" /></figure><p><a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/defining-i-o-control-codes">Defining I/O Control Codes - Windows drivers</a></p><p>Hopefully it will become more concrete with the analysis ahead. This is the important part and retrieving those will help a great deal in establishing the communication with the driver.</p><p><strong>NOTE:</strong></p><p>There are automated tools for the job and this process of course not necessary but I think it’s interesting and useful to establish a better understanding.</p><h3><strong>Let’s start …</strong></h3><p>Lets start from the beginning, at first there is the entry point:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*tw5nWZN7nXZbRBsglJwNXQ.png" /></figure><p>We start at the DriverEntry and we can see that DriverObject (of type DRIVER_OBJECT struct discussed before), being first parameter in DriverEntry, passed by convention on 64 bit systems in RCX, to the function which I renamed real_driver_entry.</p><p>Going into the function we can see that DriverObject makes it’s way to the RBX register:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*AVlrMqTx8nYBMWYTGrRY3g.png" /></figure><p>And here comes the relevant part:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*9zkfpo90jyRy4V4rAVLsUQ.png" /></figure><p>There are few functions: sub_140085058, sub_140085078, sub_140085000 which are passed to RAX and then being set to some offsets from RBX which is the DriverObject which means those functions are being initialized to some values in the DRIVER_OBJECT structure: at offsets 0x70, 0x80, 0x0E.</p><p>Checking the values related to the offsets with Windbg:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*tjx3l5HjT-PkZEMpYQUrvw.png" /></figure><p>At the offset 0x70 we can see the start of the MajorFunction array and using the following information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*4aR5z2af_DohKvANis8cKg.png" /></figure><p>It can be seen that sub_140085058 is being set as IRP_MJ_CREATE(Start of the MajorFunction array):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*vF8xq0X9By7PPN6W6qV_1w.png" /></figure><p>Each entry in the array, being a 64-bit pointer, will be of size 8 bytes thus we can sum it up:</p><p>IRP_MJ_CREATE = 0x70.</p><p>IRP_MJ_CREATE_NAME_PIPE = 0x70 + (0x8 * 1) = 0x78</p><p>IRP_MJ_CLOSE = 0x70 + (0x8 * 2) = 0x80</p><p>etc…</p><p>And for the routine in question:</p><p>IRP_MJ_DEVICE_CONTROL = 0x70 + (0x8 * 0xe) = 0xE0.</p><p>So from all this we can deduce the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iDCGUuPvXBWCw3mS_OQifA.png" /></figure><p>So now that we have the Io control function, let’s go to the next part:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*MvBbIRkRH0rJmVHqlC7gPw.png" /></figure><p>It looks rather complicated at first, but really it’s just a lot of switch statements.</p><p>The value which used for the switch statement is store in R9:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*z3CBDOBDKIED3V3gkQmlsA.png" /></figure><p>To understand where it comes from, we can see that R9 is set from whatever is pointed by R14 with the offset of 0x18.</p><p>So , let’s see what is there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*86NIhKKgY2_VkFqPG1rL1w.png" /></figure><p>R14 is initialized with whatever is pointed by RDX+0xB8.</p><p>RDX is the second parameter which was passed to this function and from MSDN:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*0UXItPf-SRuT_IL5Ctyxwg.png" /></figure><p>We can see that it is a pointer to a struct of type IRP(The one discussed in the beginning).</p><p>Using windbg:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*YsO2DupO9cttTpsuHEOejQ.png" /></figure><p>We can only see until offset 0x78 and we need to look 0x40 bytes beyond that value in search of whatever is at 0xB9:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*5th8p-MtgHfCZ3RSqtPhwQ.png" /></figure><p>We can see that at 0x40 bytes from tail we have CurrentStackLocation pointer.</p><p>Thus, R14 points to the CurrentStackLocation member of the IRP which is of type IO_STACK_LOCATION.</p><p>More information on this one can be found here:</p><p><a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_io_stack_location">https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_io_stack_location</a></p><p>Recall that R14 is where R9 gets its value following offset 0x18?</p><p>So what is at offset 0x18 of the IO_STACK_LOCATION?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5iApkj9pPY3O-vscOKvwg.png" /></figure><p>We can see at 0x8 we have Parameters, and checking 0x10 past that:</p><p>Since this is a union of structs, it will not show the offsets:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*qtH4K_YtDx2fGSGFsEoYVQ.png" /></figure><p>but dealing with IO Control function, we can look at the struct specifically:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*TFnFW4nsfwhDclFYaPor9w.png" /></figure><p>And so, we see that at offset 0x10 we get IoControlCode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*rCxigZ8fnFg2JKU73fvJBg.png" /></figure><p>The source code looks like that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*cidyZ4xFzNcT8ugxL2S0Vw.png" /></figure><p>Going back to the definition of IOCTL we know that it encodes the following information:</p><ul><li>Device type.</li><li>Function code.</li><li>Required access.</li><li>Method type.</li></ul><p>So in case of Stack Buffer overflow which provided in the Driver:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8nEZ0i-OxBTKz9N-pM1iuQ.png" /></figure><p>We can see the IoControlCode is 0x22203.</p><p>Decoding the value with OSR ioctl decoder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*fl6EuoZKL5i626vz3ooK_A.png" /></figure><p>And confirming with the source code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*6azZ0nBQWigNmP-BYGhpbA.png" /></figure><h3><strong>References:</strong></h3><p>Practical Reverse Engineering , Chapter 3 : Windows Kernel:</p><p><a href="https://www.amazon.com/Practical-Reverse-Engineering-Reversing-Obfuscation/dp/1118787315">Practical Reverse Engineering: x86, x64, ARM, Windows Kernel, Reversing Tools, and Obfuscation</a></p><p><a href="https://posts.specterops.io/methodology-for-static-reverse-engineering-of-windows-kernel-drivers-3115b2efed83">https://posts.specterops.io/methodology-for-static-reverse-engineering-of-windows-kernel-drivers-3115b2efed83</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6642f9d27dc9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Removing Process Creation Kernel Callbacks]]></title>
            <link>https://medium.com/@VL1729_JustAT3ch/removing-process-creation-kernel-callbacks-c5636f5c849f?source=rss-ebf5acc09c4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5636f5c849f</guid>
            <dc:creator><![CDATA[VL]]></dc:creator>
            <pubDate>Wed, 22 Sep 2021 13:41:56 GMT</pubDate>
            <atom:updated>2021-09-22T13:41:56.415Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>Kernel callbacks is a popular mechanism for AV/EDR products which provides those products with a way to monitor process activity on the system. Windows provides a way to notify the security vendors of things such as:</p><ul><li>Process creation.</li><li>Thread creation.</li><li>Handle request.</li><li>Image loading.</li></ul><p>From a stand point view of evasion, if we can remove the notification of process creation, for example, then maybe we can run a malicious process without being detected. Kernel callbacks have been discussed in detail from a stand point view of malware and evasion. The idea is far from new. I mean just consider this awesome FireEye article:</p><p><a href="https://www.fireeye.com/blog/threat-research/2012/06/bypassing-process-monitoring.html">How Advanced Malware Bypasses Process Monitoring</a></p><p>and it’s from 2012!!</p><p>This article does not provide new insights on the subject nor does it go into detailed explanation of the mechanics beyond what is required for the sake of the discussion. For detailed explanations and discussions on the subject refer to the articles at the end.</p><p>Rather, the aim here is to present an approach of attacking this mechanism and to mess around with some assembly and reversing along the way (which I am always up for :)).</p><h3><strong>Brief overview</strong></h3><p>The focus here is on process creation notification but the case of thread creation and image load is the same.</p><p>Suppose we have a way of arbitrary read-write in the Kernel, using the many vulnerable drivers such as MSI Afterburner RTCore64 (CVE-2019–16098) which used for the POC in this article, but really any driver which gives the ability to read and write in the kernel will do just fine.</p><p>So…we aim to remove the callback/callbacks which the EDR registered in the system which notifies of a new process being created.</p><p>Those callbacks are stored in an array by the name PspCreateProcessNotifyRoutine which we don’t know the address of.</p><p>But we do know that the callback is registered using a function by the name PsSetCreateProcessNotifyRoutine because…MSDN.</p><p>Taking a look at the PsSetCreateProcessNotifyRoutine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TRkTNnmtXFL1jOuJ1MFBJw.png" /></figure><p>We can see it invokes a function by the name PspSetCreateProcessNotifyRoutine which then load the address of the callback array:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t6-jS-l0Khmr_Hf35yUSXA.png" /></figure><p>Again, external sources mentioned at the end can explain this little process in greater detail.</p><p>So to remove the callback we need first to find the array.</p><h3><strong>How to find the callback array ?</strong></h3><p>General steps:</p><ol><li>Find the address of PsSetCreateProcessNotifyRoutine function.</li><li>Find the address of PspSetCreateProcessNotifyRoutine function.</li><li>Find the address of PspCreateProcessNotifyRoutine array.</li><li><strong>Find the address of PsSetCreateProcessNotifyRoutine function</strong>:</li></ol><p>This one is relatively simple and includes the following steps:</p><ol><li>Load the Kernel module into process memory.</li><li>Get the offset to PsSetCreateProcessNotifyRoutine using the loaded module.</li><li>Get the Kernel base address.</li><li>Get the address of PsSetCreateProcessNotifyRoutine using Kernel base address and the offset.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oLFJ8x5TTeTkiFkU3wHRpA.png" /><figcaption>Getting the PsSetCreateProcessNotifyroutine address.</figcaption></figure><p>The next steps are a bit more tricky. The technique, to find PspSetCreateProcessNotifyRoutine and the callback array, which will be used is basically signature scanning.</p><p><strong>But which bytes?</strong></p><p>The issue with looking for some signatures in memory, is that the bytes can vary between OS versions making this method not very reliable.</p><p>So the idea is to look for bytes which can be considered as an invariant which means they will stay consistent, across multiple OS versions, as much as possible anyways. The term “consistent” here is used loosely but still, we can try…</p><p><strong>NOTE:</strong> The OS versions and the code in question only deals with 64 bit.</p><p>Let’s take several kernel images as test cases and see what the code looks like:</p><p>The cases which I reviewed:</p><p>1 .Windows Server 2012 9600.</p><p>2. Windows Server 2016 1607.</p><p>3. Windows 10 2004.</p><p>4. Windows 7 7601.</p><p>5. Windows Server 2019 1809.</p><p>Should be enough to give a fair indication of which bytes to target.</p><p>Windows 2012 9600:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*yKDv9LNp9NneNA3FG_Okqg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ym722Hr9rSkl0soe6A6Pnw.png" /></figure><p>Windows Server 2016 1607:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*-dLhDYOH7Gtt45K569j7-g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rOiXOQwe2qleq_Zym4QS5Q.png" /></figure><p>Windows 10 2004:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*AUghij8Inc5AP9Wj5A9JqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQ5dqm2e7UzsFGkUrUQfjw.png" /></figure><p>Windows 7 7601:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V0gbcDktvkZRNZ70mJ43uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*nZ3TfyG4Lc6bTUzAnNQx_g.png" /></figure><p>Windows Server 2019:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uoq-MlSRVPZjhmgbbSqycQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qJj8P8jiBCS7HW8877Hwyw.png" /></figure><p>From the disassembled samples above it is a fair assumption that two main things can be targeted:</p><ol><li>JMP/CALL instruction which transfer execution from PsSetCreateProcessNotifyRoutine to PspSetCreateProcessNotifyRoutine.</li><li>LEA instruction which which loads the address of PspCreateProcessNotifyRoutine array.</li></ol><p><strong>NOTE: </strong>The first might not be necessary and the case of LEA instruction could suffice but for consistency and because it feels right for some reason, I will target both.</p><p><strong>2.</strong> <strong>Find the address of PspSetCreateProcessNotifyRoutine function</strong>:</p><p>In this case we have either JMP or CALL instruction depending on the OS in question.</p><p>Looking at Intel’s Developer’s manual for the CALL instruction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*IwPpc1VZTAgQixMG8kPD5Q.png" /></figure><p>The opcode for the instruction is 0xE8 with a displacement of 4 bytes relative to the next instruction.</p><p>So for example in case of Windows 10 2004:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*q8GFHTYmu-78S5mIjn6dRQ.png" /></figure><p>Address of PspSetCreateProcessNotifyRoutine = 0x0000000140781D5D + 5 + 0x000001B6 = = 0000000140781F18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ckLvSV-ip3yLtFw7qJFFzA.png" /></figure><p>Performing the same steps for the JMP instruction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*bbEbJ4G7salsmLbNNrvP3w.png" /></figure><p>The opcode for the JMP instruction is 0xE9 with a 4 byte displacement relative to the next instruction.</p><p>For example in the case of Windows Server 2012 9600:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SkjXWYC_1BadqjN9aGljEQ.png" /></figure><p>Address of PspSetCreateProcessNotifyRoutine = 0x00000001404E54E3 + 5 + 0x000000BC = 0x00000001404E55A4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*kLh6i9aIIACtM6kfL7kyRA.png" /></figure><p>The following code snippet will search for the JMP/CALL instruction from the start of PsSetCreateProcessNotifyRoutine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EdD2zw3sixTX-K4pQ3H81w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gtjqEx8nBiWYJzpljPir5g.png" /></figure><p><strong>NOTE:</strong> All the offsets are sign extended so if we take an 8 byte value as the base case and a 4 byte offset which specifies a negative value then we will have to sign extend.</p><p>3. <strong>Find the address of PspCreateProcessNotifyRoutine array:</strong></p><p>To find the address of the array we are looking for LEA instruction.</p><p>Let’s consider the following to decode the LEA instruction:</p><p>A 64 bit operand is used so we have a prefix which specified by the first byte.</p><p>The second byte is the opcode of the LEA instruction. Looking at the following table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Fda4HV688-aNhIH1jnCtmw.png" /></figure><p>We can see the opcode is 0x8D.</p><p>The instruction operand encoding uses ModR/M according to the following table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*EpSnHfMKNlC1wrrzyTVAKg.png" /></figure><p>ModR/M has the following fields specified: Mod, R/M , REG.</p><p>REG will specify the register and R/M combined with Mod bits will specify the addressing mode.</p><p>Let’s decode the LEA instruction used in Windows 10 2004:</p><p>4C 8D 2D D5 F6 49 00</p><p>So we know that 0x4C is the prefix and 0x8D is the LEA opcode. Next we have the ModR/M byte 0x2D.</p><p>Looking at the bits: 0x2D = 00101101</p><p>Mod = 00</p><p>R/M = 101</p><p>REG = 101</p><p>From the following table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*L5xqbpRKDfzJle57Jc0qVw.png" /></figure><p>We can see that mod = 00 and R/M = 101 will result in a 4 byte displacement operation. In long mode this comes down to displacement of 4 bytes from the instruction pointer.</p><p>We can see from the table that the value for the third byte in the instruction is changing only by the 3 bits of REG which specifies the register used.</p><p>05 = 000 = r8</p><p>0D = 001 = r9</p><p>15 = 010 = r10</p><p>1D = 011 = r11</p><p>25 = 100 = r12</p><p>2D = 101 = r13</p><p>35 = 110 = r14</p><p>3D = 111 = r15</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*fcfzErjGLx9VzWEYAhE_sQ.png" /><figcaption>32/64-bit address table from <a href="https://wiki.osdev.org/X86-64_Instruction_Encoding">https://wiki.osdev.org/X86-64_Instruction_Encoding</a>.</figcaption></figure><p>We can see that the result fits with what the disassembler shows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*9OPQXoiYOCu0NvEs2nYdvw.png" /></figure><p>So the address of PspCreatePRocessNotifyRoutine = 0x000000014084CC44 + 7 + 0x0049F6D5 = 0x0000000140CEC320.</p><p>From these conditions the target bytes in this case can be:</p><ol><li>0x4C and 0x8D for the first two bytes.</li><li>For the third byte the set of bytes that specify the register can be used:</li></ol><p>0x05, 0x0D,0x15,0x1D,0x25,0x2D,0x35,0x3D(Even if I only saw uses of r12 — r15).</p><p>The following code snippet will search for the LEA opcode from the start of PspSetCreateProcessNotifyRoutine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*70MClrgbZNiTDB_q6uPmgg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2fpD0TtOjAjWiqGzGYHV7g.png" /></figure><p><strong>What’s next ?</strong></p><p>Once the address of PspCreateProcessNotifyRoutine array has been found it’s a matter of looping through the array in search of the target callback and replacing the callback with 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/1*SGEFpsYfiGPxdmPJ7fSvlQ.png" /></figure><p>Thus making the EDR blind to new processes which are created in the system.</p><p><strong>OS Versions tested:</strong></p><p>Windows 10 20H2</p><p>Windows 10 1909</p><p>Windows 10 1903</p><p>Windows 10 21H1</p><p>Windows Server 2019 1809</p><p>Windows 7 7601</p><p>Windows Server 2012 9600</p><p>Windows Server 2016 1607</p><p>The full code implementation can be found in the following GitHub page: <a href="https://github.com/JustaT3ch/Kernel-Snooping">https://github.com/JustaT3ch/Kernel-Snooping</a>.</p><p><strong>NOTE: </strong>The code considers a single callback target so once or if the callback found it will stop searching the rest of the array. So in case of multiple callbacks some minor changes has to be made.</p><h3><strong>What about thread creation ? image loading notifications ?</strong></h3><p>The same approach with some modifications can be used for thread creation and image loading callbacks and will be discussed in the next article. As for handle creation, it’s an interesting case and definitely requires a discussion all by itself.</p><h3>Final Thoughts</h3><p>Hopefully this gives some insight into a different approach then getting&#39;s some bytes signatures which are OS based, checking OS versions etc. hoping not to get a BSOD.</p><p>Although I cannot promise this will be BSOD free, it did try to make it as smooth as possible.</p><p>As stated before the MSI driver was used in this case and thus it’s read write primitives were implemented.</p><p>But there are many vulnerable drivers out there and the major change which will have to be made for this to work with a new one, is to implement the corresponding write/read primitives i.e. how the data is being passed to the driver , what are the sizes of the read value which is passed etc. But the logic of finding the callback array remains the same.</p><p>If You did not fall asleep until this point then I guess I did something right.</p><p>Thanks for reading :)</p><h3><strong>References:</strong></h3><p><a href="https://wiki.osdev.org/X86-64_Instruction_Encoding">X86-64 Instruction Encoding</a></p><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/91aa6a33331519602f8840da3917d863/href">https://medium.com/media/91aa6a33331519602f8840da3917d863/href</a></iframe><ul><li><a href="https://www.matteomalvica.com/blog/2020/07/15/silencing-the-edr/">Silencing the EDR. How to disable process, threads and image-loading detection callbacks.</a></li><li><a href="https://www.triplefault.io/2017/09/enumerating-process-thread-and-image.html">Enumerating process, thread, and image load notification callback routines in Windows</a></li></ul><p><a href="https://itw01.com/8SRQMEH.html"><strong>https://itw01.com/8SRQMEH.html</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5636f5c849f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>