<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Ctf on Mal Breaks Things</title>
    <link>https://sec.gd/blog/en/categories/ctf/</link>
    <description>Recent content in Ctf on Mal Breaks Things</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>mal@sec.gd (mal)</managingEditor>
    <webMaster>mal@sec.gd (mal)</webMaster>
    <copyright>&amp;copy;{year}. [License and Info](/blog/about/)</copyright>
    <lastBuildDate>Sun, 28 Jan 2024 23:00:00 +0000</lastBuildDate>
    <sy:updatePeriod>daily</sy:updatePeriod>
    
        <atom:link href="https://sec.gd/blog/en/categories/ctf/index.xml" rel="self" type="application/rss+xml" />
    
    
    
      <item>
        <title>Long Distance 2</title>
        <link>https://sec.gd/blog/en/posts/long-distance-2/</link>
        <pubDate>Sun, 28 Jan 2024 23:00:00 +0000</pubDate>
        <author>mal@sec.gd (mal)</author>
        <atom:modified>Sun, 28 Jan 2024 23:00:00 +0000</atom:modified>
        <guid>https://sec.gd/blog/en/posts/long-distance-2/</guid>
        <description>&lt;p&gt;This weekend I participated in the &lt;a href=&#34;https://ctftime.org/event/2172/&#34;&gt;Real World CTF&lt;/a&gt; with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;WreckTheLine&lt;/a&gt;. Unlike many other CTFs, these challenges were all based on real applications and systems. It&amp;rsquo;s interesting being able to use (and gain) domain knowledge, and while contrived challenges are fun, exploiting a system that exists in the real world – on the real internet – is another level of engagement.&lt;/p&gt;</description>
        <content:encoded>&lt;![CDATA[&lt;p&gt;This weekend I participated in the &lt;a href=&#34;https://ctftime.org/event/2172/&#34;&gt;Real World CTF&lt;/a&gt; with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;WreckTheLine&lt;/a&gt;. Unlike many other CTFs, these challenges were all based on real applications and systems. It&amp;rsquo;s interesting being able to use (and gain) domain knowledge, and while contrived challenges are fun, exploiting a system that exists in the real world – on the real internet – is another level of engagement.&lt;/p&gt;
&lt;h2 id=&#34;challenge&#34;&gt;Challenge&lt;/h2&gt;
&lt;p&gt;This &lt;code&gt;misc&lt;/code&gt; &lt;a href=&#34;https://github.com/chaitin/Real-World-CTF-6th-Challenges/tree/main/Long%20Range%202&#34;&gt;challenge&lt;/a&gt; consisted of a 290 MB WAV file named &lt;code&gt;486_375MHz-1MSps-1MHz.wav&lt;/code&gt;, an 8 MB &lt;code&gt;flash_dump&lt;/code&gt;, and a mandate to recover the information contained in the transmission:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Of late, whispers doth persist behind mine back. Yesterday, under the studio tower, a peculiar contraption was found by me. I am most intrigued to discover the content of their discourse.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&#34;rf&#34;&gt;RF&lt;/h2&gt;
&lt;p&gt;The WAV file metadata showed 51 seconds, 2 channels, 24 bit resolution, and 1 MHz sample rate. Audibly, it was a mostly silent with several bursts of data, sounding like a pop followed by buzzing.&lt;/p&gt;
&lt;p&gt;Audacity was in the mood for 3 hours of plugin updates, so I used &lt;a href=&#34;https://www.sonicvisualiser.org/&#34;&gt;Sonic Visualiser&lt;/a&gt;, which showed the bursts of data in the waveform.
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/1-waveform.png&#34; alt=&#34;Waveform showing two channels of silence followed by a short loud burst and a longer textured section, repeated twice&#34;&gt;
Zooming in, things became more interesting - FM?
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/2-waveform-zoom.png&#34; alt=&#34;Waveform zoomed in to show the individual squiggles getting closer together and farther apart over time in both channels&#34;&gt;
We can use the spectrogram tool to see the distribution of energy (color) across frequencies (Y axis) over time (X axis), with an interesting result&amp;hellip;
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/3-waterfall.jpg&#34; alt=&#34;Waveform for context, and spectrogram showing that the high energy pulse is zigzagging up and down in frequency during the data transmission&#34;&gt;
And further into the data section, discontinuities in the frequency sweeps - Probably the symbols that we needed recover data from.
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/4-waterfall-detail.png&#34; alt=&#34;Another part of the data transmission, where the spectrogram zigzag repeatedly jumps to a different part of the zig or zag&#34;&gt;&lt;/p&gt;
&lt;p&gt;A teammate, qwertboy, had linked a &lt;a href=&#34;https://org.anize.rs/rwctf-2023/misc/long-range&#34;&gt;writeup&lt;/a&gt; of last year&amp;rsquo;s Long Distance challenge by the &lt;code&gt;organizers&lt;/code&gt; team, where they had a similar file containing a RF capture, and found the flag by decoding the LoRaWAN traffic.&lt;/p&gt;
&lt;p&gt;What I was seeing matched signals they found. Both had similar patterns in the waveform and demodulated FM, and after setting up GNU Radio, in the constellation display.&lt;/p&gt;
&lt;h2 id=&#34;flash-image&#34;&gt;Flash Image&lt;/h2&gt;
&lt;p&gt;My first port of call with an unknown firmware image is usually &lt;code&gt;binwalk&lt;/code&gt;. In this case the signatures and strings it found led me to believe that this was an image for an ESP32, used mesh networking, and (incorrectly) that it might be for an IoT lightbulb or lighting controller.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ binwalk flash_dump

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
88595         0x15A13         Neighborly text, &amp;#34;neighbors a simple (0 id) broadcast&amp;#34;
109607        0x1AC27         HTML document header
109988        0x1ADA4         HTML document footer
113954        0x1BD22         Neighborly text, &amp;#34;Neighbor Info module config: Ambient Lighting&amp;#34;
114583        0x1BF97         Neighborly text, &amp;#34;Neighbor Info module config: Ambient Lighting&amp;#34;
118036        0x1CD14         Neighborly text, &amp;#34;NeighborsKET from Node 0x%x to Node 0x%x (last sent by 0x%x)&amp;#34;

[...]
155668        0x26014         Unix path: /home/runner/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src/SPI.cpp
173532        0x2A5DC         AES S-Box
[...]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Extracting the identified files with &lt;code&gt;binwalk -e&lt;/code&gt; found assorted HTML and CSS, along with a JSON blob from 0x3A7000:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Meshtastic&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;short_name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Meshtastic&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;start_url&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;.&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;description&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Meshtastic web app&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;icons&amp;#34;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;src&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/icon.svg&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;sizes&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;any&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;type&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;image/svg+xml&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;theme_color&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#67ea94&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;background_color&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#67ea94&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;display&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;standalone&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So I knew early that we&amp;rsquo;re working with &lt;a href=&#34;https://meshtastic.org/&#34;&gt;Meshtastic&lt;/a&gt;, but didn&amp;rsquo;t yet understand LoRa vs LoRaWAN. Reading through the site found &lt;a href=&#34;https://meshtastic.org/docs/overview/radio-settings&#34;&gt;radio settings&lt;/a&gt; to help with decoding, the Meshtastic &lt;a href=&#34;https://meshtastic.org/docs/overview/mesh-algo&#34;&gt;packet header format&lt;/a&gt; to help with parsing, and an &lt;a href=&#34;https://meshtastic.org/docs/overview/encryption&#34;&gt;overview of the encryption&lt;/a&gt;. I later found a &lt;a href=&#34;https://meshtastic.discourse.group/t/meshtastic-lora-packet-size/7953/9&#34;&gt;diagram&lt;/a&gt; of a LoRa Meshtastic packet which was useful, but note the backwards flags field.&lt;/p&gt;
&lt;h2 id=&#34;lora&#34;&gt;LoRa&lt;/h2&gt;
&lt;p&gt;I spent quite a while trying to get &lt;a href=&#34;https://github.com/rpp0/gr-lora&#34;&gt;rpp0/gr-lora&lt;/a&gt; set up (it officially requires the long-dead python2) and working, and a while more seeing if &lt;a href=&#34;https://github.com/tapparelj/gr-lora_sdr&#34;&gt;tapparelj/gr-lora_sdr&lt;/a&gt; or its fork &lt;a href=&#34;https://github.com/martynvdijke/gr-lora_sdr&#34;&gt;martynvdijke/gr-lora_sdr&lt;/a&gt; (as used by the AUR package) were any more cooperative. In the end, I replaced the python2-foo dependencies with python3-foo, and it didn&amp;rsquo;t complain. Still, the correct configuration for the decoder eluded me.&lt;/p&gt;
&lt;p&gt;Saturday morning started with another teammate, tcode2k16, finding a potential signal decoding result before I&amp;rsquo;d finished breakfast. We individually chased our tails for a while on this, and it turned out to have two issues - The decoded data was incorrect, and we were confusing LoRa (physical layer) with LoRaWAN (a communication protocol built on LoRa) and trying to parse the payload as the latter.&lt;/p&gt;
&lt;h2 id=&#34;we-need-to-go-deeper&#34;&gt;We Need to Go Deeper&lt;/h2&gt;
&lt;p&gt;Once tcode was making progress on decoding and parsing, I went back to the firmware image. The source for this build, less any CTF modifications, is at &lt;a href=&#34;https://github.com/meshtastic/firmware/tree/v2.2.14.57542ce&#34;&gt;meshtastic/firmware@v2.2.14.57542ce&lt;/a&gt;, and tcode found a &lt;a href=&#34;https://github.com/meshtastic/firmware/blob/v2.2.14.57542ce/src/mesh/Channels.cpp#L10&#34;&gt;default AES key&lt;/a&gt; there, but we should check for a custom one in flash.&lt;/p&gt;
&lt;p&gt;To start, I dropped the image into a hex editor, which wasn&amp;rsquo;t particularly useful until I looked into how ESP-IDF partitioning works. The default partition table isn&amp;rsquo;t at the beginning of flash, but at 0x8000.&lt;/p&gt;
&lt;p&gt;Rather than spend time parsing the partition table correctly, I guessed the layout based on the hex dump:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;00008000  aa 50 01 02 00 90 00 00  00 50 00 00 6e 76 73 00  |.P.......P..nvs.|
00008010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00008020  aa 50 01 00 00 e0 00 00  00 20 00 00 6f 74 61 64  |.P....... ..otad|
00008030  61 74 61 00 00 00 00 00  00 00 00 00 00 00 00 00  |ata.............|
00008040  aa 50 00 10 00 00 01 00  00 00 25 00 61 70 70 00  |.P........%.app.|
00008050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00008060  aa 50 00 11 00 00 26 00  00 00 0a 00 66 6c 61 73  |.P....&amp;amp;.....flas|
00008070  68 41 70 70 00 00 00 00  00 00 00 00 00 00 00 00  |hApp............|
00008080  aa 50 01 82 00 00 30 00  00 00 10 00 73 70 69 66  |.P....0.....spif|
00008090  66 73 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |fs..............|
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Much later I realized the partition table, human-readable with extra info, is also available right in the firmware repository as &lt;a href=&#34;https://github.com/meshtastic/firmware/blob/v2.2.14.57542ce/partition-table.csv&#34;&gt;partition-table.csv&lt;/a&gt;. Here&amp;rsquo;s that version, to save at least one of us some squinting at hex:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-csv&#34; data-lang=&#34;csv&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;# Name&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;   Type&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; SubType&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; Offset&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;  Size&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; Flags&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;nvs&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;      data&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; nvs&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;     0x009000&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; 0x005000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;otadata&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;  data&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; ota&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;     0x00e000&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; 0x002000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;app&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;      app&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;  ota_0&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;   0x010000&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; 0x250000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;flashApp&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; app&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;  ota_1&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;   0x260000&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; 0x0A0000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;spiffs&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;   data&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; spiffs&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;  0x300000&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt; 0x100000&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;ldquo;NVS&amp;rdquo; is used by the ESP-IDF &lt;a href=&#34;https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html&#34;&gt;Non-Volatile Storage Library&lt;/a&gt;, which seemed like a good place to store settings. Carve out 0x5000 bytes starting at 0x9000 in the image with &lt;code&gt;dd if=flash_dump bs=$((0x1000)) skip=9 count=5 of=nvs.bin&lt;/code&gt;, and parse it with &lt;code&gt;nvs_tool.py&lt;/code&gt; from the &lt;a href=&#34;https://github.com/espressif/esp-idf/tree/b3f7e2c8a4d354df8ef8558ea7caddc07283a57b/components/nvs_flash/nvs_partition_tool&#34;&gt;ESP-IDF repository&lt;/a&gt;: &lt;code&gt;python nvs_tool.py -i nvs.bin&lt;/code&gt;. Unfortunately none of the data it held looked like a 128-bit or 256-bit AES key, or for that matter, any of the other configuration parameters I was expecting.&lt;/p&gt;
&lt;p&gt;Where else would the firmware store its configuration? Looking back at the partition table, &amp;ldquo;nvs&amp;rdquo; was a bust, &amp;ldquo;otadata&amp;rdquo; seems update-related, &amp;ldquo;app&amp;rdquo; is probably the running code, &amp;ldquo;flashApp&amp;rdquo; may be for in-progress updates, or &amp;ldquo;spiffs&amp;rdquo;. The &amp;ldquo;SPI Flash FileSystem&amp;rdquo; sounded promising, so I extricated it with &lt;code&gt;dd if=flash_dump bs=$((0x100000)) skip=3 count=1 of=spiffs.bin&lt;/code&gt;, but &lt;a href=&#34;https://github.com/igrr/mkspiffs&#34;&gt;igrr/mkspiffs&lt;/a&gt; didn&amp;rsquo;t like it. &lt;code&gt;file&lt;/code&gt; wasn&amp;rsquo;t familiar. &lt;code&gt;hexdump&lt;/code&gt; showed this at the beginning of the file:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;00000000  03 00 00 00 f0 0f ff f7  6c 69 74 74 6c 65 66 73  |........littlefs|
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Ah. littlefs on a partition named spiffs.  Probably some backwards-compatibility thing&amp;hellip;&lt;/p&gt;
&lt;p&gt;Regardless, the &lt;a href=&#34;https://github.com/littlefs-project/littlefs&#34;&gt;littlefs readme&lt;/a&gt; had a lot of useful tools. &lt;a href=&#34;https://github.com/tniessen/littlefs-disk-img-viewer&#34;&gt;tniessen/littlefs-disk-img-viewer&lt;/a&gt; is a webapp with a &lt;a href=&#34;https://tniessen.github.io/littlefs-disk-img-viewer/&#34;&gt;live demo&lt;/a&gt;, which after some block size guessing (4096) was able to read the filesystem.
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/5-littlefs.png&#34; alt=&#34;littlefs viewer showing spiffs.bin open, that 121/256 blocks are used, and two folders inside named “prefs” and “static”&#34;&gt;
&lt;code&gt;static/&lt;/code&gt; held the Meshtastic webui resources, and &lt;code&gt;prefs/&lt;/code&gt; had &lt;code&gt;channels.proto&lt;/code&gt;, &lt;code&gt;config.proto&lt;/code&gt;, and &lt;code&gt;db.proto&lt;/code&gt;, which I downloaded.&lt;/p&gt;
&lt;h3 id=&#34;protobuf&#34;&gt;Protobuf&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://protobuf.dev/&#34;&gt;Protobuf&lt;/a&gt; (protocol buffers) is a relatively common data interchange system, which Meshtastic uses for both preferences and over-the-air communication. At first I tried to use &lt;code&gt;protoc&lt;/code&gt; with the Meshtastic &lt;a href=&#34;https://github.com/meshtastic/protobufs/tree/c845b7848eebb11150ca0427773303bf8758e533/meshtastic&#34;&gt;protobuf definitions&lt;/a&gt; to produce Python code to read the files, and then &lt;a href=&#34;https://www.protobufpal.com/&#34;&gt;protobufpal&lt;/a&gt;, but they weren&amp;rsquo;t parsing the prefs files, so in the interest of time I ended up using &lt;a href=&#34;https://github.com/mildsunrise/protobuf-inspector&#34;&gt;mildsunrise/protobuf-inspector&lt;/a&gt;.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ protobuf_inspector &amp;lt;channels.proto
root:
    1 &amp;lt;chunk&amp;gt; = message:
        2 &amp;lt;chunk&amp;gt; = message:
            2 &amp;lt;chunk&amp;gt; = bytes (1)
                0000   01                                                                       .
        3 &amp;lt;varint&amp;gt; = 1
    1 &amp;lt;chunk&amp;gt; = message:
        1 &amp;lt;varint&amp;gt; = 1
        2 &amp;lt;chunk&amp;gt; = message:
            2 &amp;lt;chunk&amp;gt; = bytes (32)
                0000   CE F8 DB 8E 8E 60 17 FD 6D CC A2 1D B8 A1 47 6D 45 14 80 AC D7 F4 F9 F7  .....`..m.....GmE.......
                0018   69 A7 63 F5 28 C0 11 F7                                                  i.c.(...
            3 &amp;lt;chunk&amp;gt; = &amp;#34;Buddies&amp;#34;
            4 &amp;lt;32bit&amp;gt; = 0x00000001 / 1 / 1.40130e-45
        3 &amp;lt;varint&amp;gt; = 2
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 2, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 3, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 4, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 5, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 6, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    1 &amp;lt;chunk&amp;gt; = message(1 &amp;lt;varint&amp;gt; = 7, 2 &amp;lt;chunk&amp;gt; = empty chunk)
    2 &amp;lt;varint&amp;gt; = 22
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The leading number associates a value with its attribute in the protobuf definition. The root seems to be an array of Channel objects, so within each of those we should be able to look for the attribute numbers in &lt;a href=&#34;https://github.com/meshtastic/protobufs/blob/c845b7848eebb11150ca0427773303bf8758e533/meshtastic/channel.proto&#34;&gt;channel.proto&lt;/a&gt;. Channel-&amp;gt;2 is a &lt;code&gt;ChannelSettings settings&lt;/code&gt;, and ChannelSettings-&amp;gt;2 is &lt;code&gt;bytes psk&lt;/code&gt;. We&amp;rsquo;ve found our channel key!&lt;/p&gt;
&lt;p&gt;Since then I&amp;rsquo;ve realized that &lt;code&gt;prefs/channels.proto&lt;/code&gt; isn&amp;rsquo;t a &lt;code&gt;Channel&lt;/code&gt; from channel.proto, it&amp;rsquo;s a &lt;code&gt;ChannelFile&lt;/code&gt; from &lt;code&gt;deviceonly.proto&lt;/code&gt;, so here&amp;rsquo;s the interesting parts in a more readable format:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;channels&amp;#34;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;index&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;settings&amp;#34;&lt;/span&gt;: {&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;...&lt;/span&gt;},
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;role&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;PRIMARY&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;index&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;settings&amp;#34;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;channel_num&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;psk&amp;#34;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#ae81ff&#34;&gt;206&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;248&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;219&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;142&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;142&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;96&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;23&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;253&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;109&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;204&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;162&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;29&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;184&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;161&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;71&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;109&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#ae81ff&#34;&gt;69&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;128&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;172&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;215&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;244&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;249&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;247&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;105&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;99&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;245&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;40&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;192&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;17&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;247&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Buddies&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;id&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;uplink_enabled&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;downlink_enabled&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;role&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SECONDARY&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    [&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;followed&lt;/span&gt; &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;by&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;empty&lt;/span&gt; &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;role:&lt;/span&gt; &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;DISABLED&lt;/span&gt; &lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;channels&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;version&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;22&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And the LoRa part of &lt;code&gt;prefs/config.proto&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;lora&amp;#34;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;ignore_incoming&amp;#34;&lt;/span&gt;: [],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;use_preset&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;modem_preset&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;LONG_FAST&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;bandwidth&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;spread_factor&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;coding_rate&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;frequency_offset&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;region&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;CN&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;hop_limit&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;tx_enabled&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;tx_power&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;19&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;channel_num&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;66&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;override_duty_cycle&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;sx126x_rx_boosted_gain&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;override_frequency&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We no longer have to guess our LoRa settings, we can just copy the &lt;code&gt;LONG_FAST&lt;/code&gt; parameters from the &lt;a href=&#34;https://meshtastic.org/docs/overview/radio-settings&#34;&gt;radio settings&lt;/a&gt; page!&lt;/p&gt;
&lt;h2 id=&#34;back-to-decoding&#34;&gt;Back to Decoding&lt;/h2&gt;
&lt;p&gt;With known radio settings we could revisit the GNU Radio flow graph with gr-lora. Here&amp;rsquo;s what I ended up with:
&lt;a href=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/lora_RX.grc&#34;&gt;download grc&lt;/a&gt;
&lt;img src=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/lora_RX.svg&#34; alt=&#34;GNU Radio flow graph. Both channels from WAV file source are converted from floats to complex numbers, sent through a bypassed Throttle block, then to a disabled QT GUI Sink block and a LoRa Receiver block&#34;&gt;&lt;/p&gt;
&lt;p&gt;And its output: &lt;a href=&#34;https://sec.gd/blog/en/posts/long-distance-2/resources/lora_RX.log&#34;&gt;full log&lt;/a&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Bits (nominal) per symbol: 	5.5
Bins per symbol: 	2048
Samples per symbol: 	8192
Decimation: 		4
 2d 31 e0 bc 4b 6c fa c4 c0 6d fa 66 26 d2 02 0b 08 a7 92 b3 78 fb 63 77 d7 e0 54 d7 4f 67 1e c0 2d f1 8c 7d 04 66 c9 31 bb 22 40 0f c9 ec 25 c8 71 33 (Klmf&amp;amp;xcwTOg-}f1&amp;#34;@%q3)
[...]
 22 31 70 ff ff ff ff c4 c0 6d fa 5f 8a 54 22 02 04 a7 92 53 1f 89 a3 6f ea 30 18 c9 ce b7 e7 1f a3 cd 72 71 ed a7 3a (m_T&amp;#34;So0rq:)
 09 11 40/usr/include/c++/13.2.1/bits/stl_vector.h:1125: std::vector&amp;lt;_Tp, _Alloc&amp;gt;::reference std::vector&amp;lt;_Tp, _Alloc&amp;gt;::operator[](size_type) [with _Tp = unsigned char; _Alloc = std::allocator&amp;lt;unsigned char&amp;gt;; reference = unsigned char&amp;amp;; size_type = long unsigned int]: Assertion &amp;#39;__n &amp;lt; this-&amp;gt;size()&amp;#39; failed.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The crash may have been because of my python2/python3 change. I hoped it wasn&amp;rsquo;t preventing decoding of the message with the flag, and it turned out fine.&lt;/p&gt;
&lt;p&gt;This still looked a bit nonsensical consider the Meshtastic &lt;a href=&#34;https://meshtastic.org/docs/overview/mesh-algo&#34;&gt;header format&lt;/a&gt;. When aligning the first part of each message, I would have expected to see repeated source and destination IDs, channel hash bytes, and padding, and flag bytes with the high nibble empty.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;DEST_NODEID SRC_NODEID_ PKT_UNIQ_ID FL CH PAD__ DATA...
2d 31 e0 bc 4b 6c fa c4 c0 6d fa 66 26 d2 02 0b 08 a7 92 b3 78 fb ...
1b 31 e0 c4 c0 6d fa bc 4b 6c fa 29 5d 91 38 03 08 a7 92 12 19 7e ...
49 31 20 c4 c0 6d fa bc 4b 6c fa a6 41 be 1e 0b 08 a7 92 b9 4d 99 ...
1b 31 e0 bc 4b 6c fa c4 c0 6d fa 0f 21 39 44 03 08 a7 92 b8 56 c7 ...
5f 30 00 ff ff ff ff c4 c0 6d fa a3 f3 0f 12 03 08 a7 92 aa c1 8d ...
5f 30 00 ff ff ff ff c4 c0 6d fa a3 f3 0f 12 02 08 a7 92 aa c1 8d ...
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There are some columns that do have the properties I would expect, though&amp;hellip; Let&amp;rsquo;s shift the column headers over a byte at a time and see if they line up:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;?? ?? ?? DEST_NODEID SRC_NODEID_ PKT_UNIQ_ID FL CH PAD__ DATA...
2d 31 e0 bc 4b 6c fa c4 c0 6d fa 66 26 d2 02 0b 08 a7 92 b3 78 fb ...
1b 31 e0 c4 c0 6d fa bc 4b 6c fa 29 5d 91 38 03 08 a7 92 12 19 7e ...
49 31 20 c4 c0 6d fa bc 4b 6c fa a6 41 be 1e 0b 08 a7 92 b9 4d 99 ...
1b 31 e0 bc 4b 6c fa c4 c0 6d fa 0f 21 39 44 03 08 a7 92 b8 56 c7 ...
5f 30 00 ff ff ff ff c4 c0 6d fa a3 f3 0f 12 03 08 a7 92 aa c1 8d ...
5f 30 00 ff ff ff ff c4 c0 6d fa a3 f3 0f 12 02 08 a7 92 aa c1 8d ...
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That looks better, the fields all had the properties I expected. The leading 3 bytes ended up being the LoRa header, as seen in the &lt;a href=&#34;https://meshtastic.discourse.group/t/meshtastic-lora-packet-size/7953/9&#34;&gt;diagram&lt;/a&gt; - Payload length, coding rate, CRC flag, header CRC, padding.&lt;/p&gt;
&lt;h2 id=&#34;on-to-parsing&#34;&gt;On to Parsing&lt;/h2&gt;
&lt;p&gt;Disassembling the headers in python was relatively straightforward, but decrypting the Meshtastic payload was aggravating. The encrypt and decrypt functions are the same for AES-CTR, and for us are in &lt;a href=&#34;https://github.com/meshtastic/firmware/blob/v2.2.14.57542ce/src/platform/esp32/ESP32CryptoEngine.cpp#L40&#34;&gt;ESP32CryptoEngine.cpp&lt;/a&gt;. I should have looked at the relevant bits for other platforms, since they must all be compatible and &lt;a href=&#34;https://github.com/meshtastic/firmware/blob/v2.2.14.57542ce/src/platform/rp2040/rp2040CryptoEngine.cpp#L17&#34;&gt;others&lt;/a&gt; are more straightforward, but I didn&amp;rsquo;t. The problem boiled down to me chasing endianness issues before double checking that I was putting the &lt;a href=&#34;https://github.com/meshtastic/firmware/blob/v2.2.14.57542ce/src/mesh/CryptoEngine.cpp#L30&#34;&gt;parts of the nonce&lt;/a&gt; together in the wrong order or trying to decrypt other messages.&lt;/p&gt;
&lt;p&gt;While I worked on building a custom &lt;code&gt;Counter&lt;/code&gt; for PyCryptodome, tcode used my parsing and the python-mbedtls&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; library to successfully decrypt it. A few minutes later I got mine working, using the PyCryptodome, the default &lt;code&gt;Counter&lt;/code&gt;, and the correct nonce ordering and message.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#!/usr/bin/env python3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; Crypto.Cipher &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; AES
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; Crypto.Util &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Counter
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pkt &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fromhex(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;683060ffffffffc4c06dfa8899c2020304a7925001502e35d73ab793c3e9ad6acc9da6a4955bc56b8054e9989d76f5355cf2868b90bffae321d51077be4e1774fc074f63a4c0af6ba38f33a829b0784bdadbcc738b16e930e741c48f4d6c0cab012e5605122dce40eaeb7b7626&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;psk &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fromhex(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;CEF8DB8E8E6017FD6DCCA21DB8A1476D451480ACD7F4F9F769A763F528C011F7&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;lora_header &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pkt[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;has_crc &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (lora_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;lora_payload &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pkt[&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;:]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;lora_payload_crc &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lora_payload[&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;:] &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; has_crc &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; has_crc:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    lora_payload &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lora_payload[:&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;lora_payload_len &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lora_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; len(lora_payload) &lt;span style=&#34;color:#f92672&#34;&gt;!=&lt;/span&gt; lora_payload_len:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    print(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Warning: Incorrect payload length. expected &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;, got &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;format(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            lora_payload_len, len(lora_payload)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_header &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lora_payload[:&lt;span style=&#34;color:#ae81ff&#34;&gt;16&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_dest &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_src &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_packet_id &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_flags &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_flags_hop_limit &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_flags &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0b111&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_flags_want_ack &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (mt_flags &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_channel_hash &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;13&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_header_padding &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_header[&lt;span style=&#34;color:#ae81ff&#34;&gt;14&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;16&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_payload &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lora_payload[&lt;span style=&#34;color:#ae81ff&#34;&gt;16&lt;/span&gt;:]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;nonce &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mt_packet_id &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\0&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mt_src
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;cipher &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; AES&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(psk, AES&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;MODE_CTR, initial_value&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, nonce&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;nonce)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mt_payload_decrypted &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; cipher&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decrypt(mt_payload)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;open(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;decrypt.out&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;wb&amp;#34;&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(mt_payload_decrypted)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(mt_payload_decrypted)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;victory&#34;&gt;Victory!&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;b&amp;#39;\x08\x01\x12Talright alright. the key is rwctf{No_h0p_th1s_tim3_c831bcad725935ba25c0a3708e49c0c8}&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Fully parsed:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;portnum&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;TEXT_MESSAGE_APP&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;payload&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;alright alright. the key is rwctf{No_h0p_th1s_tim3_c831bcad725935ba25c0a3708e49c0c8}&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Thanks to tcode for working with me on this challenge. At one point during decoding they said &amp;ldquo;I was doing something silly&amp;rdquo;. In my experience, that&amp;rsquo;s all of programming, CTFing, and many other technical endeavors. I hope this post was worth the read despite its length, but I think it&amp;rsquo;s important to not gloss over the challenges and mistakes instead of reinforcing anyone&amp;rsquo;s imposter syndrome.&lt;/p&gt;
&lt;p&gt;WreckTheLine finished in 7th place of 2291 teams. This challenge was worth 320 points out of our 1662 (19%), and was solved by a total of 9 teams.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;I would recommend against using Synss/python-mbedtls for real cryptography. It has basically no documentation I can find besides examples, and what is there (docstrings) is scary.
For &lt;a href=&#34;https://github.com/Synss/python-mbedtls/blob/ce46bdefc0e5234dd0d3c4e63ce5c2d9fe8555e5/src/mbedtls/cipher/AES.py#L60&#34;&gt;example&lt;/a&gt;, &lt;code&gt;AES.new(key, mode, iv, ad)&lt;/code&gt; (note that iv=nonce+counter, effectively):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;iv: The initialization vector (IV). The IV is required for every mode but ECB and CTR where it is ignored. If not set, the IV is initialized to all 0, which should not be used for encryption.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;AES-CTR must &lt;em&gt;never&lt;/em&gt; use the same (key, nonce, counter) twice or its security properties are broken. Luckily failing to specify an &lt;code&gt;iv&lt;/code&gt; parameter does produce an error. Additionally, the &lt;code&gt;ad&lt;/code&gt; (Associated Data / Additional Authenticated Data, I assume) parameter is not even in the docstring.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;]]></content:encoded>
        <dc:creator>mal</dc:creator>
        <media:content url="https://sec.gd/blog/images/long-distance-2/4-waterfall-detail.png" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        <media:content url="https://sec.gd/blog/images/long-distance-2/4-waterfall-detail.png" medium="image"><media:title type="html">meta image</media:title></media:content>
        
          
            
              <category>real world ctf</category>
            
          
            
              <category>radio</category>
            
          
            
              <category>misc</category>
            
          
        
        
          
            
              <category>ctf</category>
            
          
        
        
      </item>
    
      <item>
        <title>Hacker&#39;s Playground: Legonigma</title>
        <link>https://sec.gd/blog/en/posts/legonigma/</link>
        <pubDate>Tue, 27 Sep 2022 04:00:00 +0000</pubDate>
        <author>mal@sec.gd (mal)</author>
        <atom:modified>Tue, 27 Sep 2022 04:00:00 +0000</atom:modified>
        <guid>https://sec.gd/blog/en/posts/legonigma/</guid>
        <description>&lt;p&gt;I recently participated in Samsung&amp;rsquo;s &lt;a href=&#34;https://ctftime.org/event/1715/&#34;&gt;Hacker&amp;rsquo;s Playground CTF&lt;/a&gt; with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;Wreck The Line&lt;/a&gt;. We finished with 1592 points, in 26th place of 1075 teams. Most of my time was spent on Legonigma, an interesting 500 point challenge based on two renders of a Lego cryptography gadget.&lt;/p&gt;</description>
        <content:encoded>&lt;![CDATA[&lt;p&gt;I recently participated in Samsung&amp;rsquo;s &lt;a href=&#34;https://ctftime.org/event/1715/&#34;&gt;Hacker&amp;rsquo;s Playground CTF&lt;/a&gt; with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;Wreck The Line&lt;/a&gt;. We finished with 1592 points, in 26th place of 1075 teams. Most of my time was spent on Legonigma, an interesting 500 point challenge based on two renders of a Lego cryptography gadget.&lt;/p&gt;
&lt;h2 id=&#34;the-challenge&#34;&gt;The Challenge&lt;/h2&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/Legonigma_Front.jpg&#34; alt=&#34;Front image of Lego gadget&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/Legonigma_Back.jpg&#34; alt=&#34;Rear image of Lego gadget&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/Legonigma_Instructions.jpg&#34; alt=&#34;Instruction image, showing that the 36 gear teeth are A-Z then 0-9 clockwise, green gears are input, and yellow are output&#34;&gt;&lt;/p&gt;
&lt;p&gt;Decipher ciphertext:
&lt;code&gt;59x8wl9pjsava3grn0il79aoq307f20a6huc3xnos289cd8xv1fn2znuoa2bq8959chbktwfow8c3azpvo3c59jz4&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;first-impressions&#34;&gt;First Impressions&lt;/h2&gt;
&lt;p&gt;Cool!&lt;/p&gt;
&lt;p&gt;I had a limited selection of possible parts, assuming it was built with production Lego pieces. The only moving parts were gears and a differential, so I suspected the relationships between input and outputs were just a series of ratios.&lt;/p&gt;
&lt;p&gt;Why were there three outputs? Maybe it produced 3 ciphertext characters per plaintext character? No, the ciphertext was 89 characters total, which isn&amp;rsquo;t divisible by 3&amp;hellip; I must need to guess the order?&lt;/p&gt;
&lt;h2 id=&#34;pieces-in-play&#34;&gt;Pieces in Play&lt;/h2&gt;
&lt;p&gt;Using the images and information from a &lt;a href=&#34;https://themomentmakers.org/2019/09/26/gears-types-of-gears/&#34;&gt;blog post&lt;/a&gt; about Lego gears I listed the pieces I might face, by type, tooth count, and features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Regular gears (no bevel)
&lt;ul&gt;
&lt;li&gt;8 (axle)&lt;/li&gt;
&lt;li&gt;16 (axle and small spaces)&lt;/li&gt;
&lt;li&gt;24 (axle and four round post holes)&lt;/li&gt;
&lt;li&gt;40 (axle and a bunch of axle and post holes)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Double Bevel
&lt;ul&gt;
&lt;li&gt;12 (axle)&lt;/li&gt;
&lt;li&gt;20 (axle, spaces)&lt;/li&gt;
&lt;li&gt;28 (axle and four post holes)&lt;/li&gt;
&lt;li&gt;36 (axle plus two posts and two axles)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Differential
&lt;ul&gt;
&lt;li&gt;Three identical single-bevel inside, so ratios don&amp;rsquo;t matter&lt;/li&gt;
&lt;li&gt;Outside: 16 and 24 tooth&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;attempt-one-procedural&#34;&gt;Attempt One: Procedural&lt;/h2&gt;
&lt;p&gt;My first instinct was to follow the gearing through, noting each number of teeth:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;36 (input, green)
hard link to 20 (grey)
rotates 12 (blue)
rotates 20 (grey)
splits to A

A1:
rotates 12 (blue)
hard link to 36 (grey)
rotates 12 (blue)
hard link to 36 (yellow, out1)

A2:
hard link to 28 (blue)
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I intended to turn this into a simple procedure that would just do the math for each step, keeping track of the angle of each gear, but I realized there&amp;rsquo;s a much easier way to simplify it&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;attempt-two-mathematical&#34;&gt;Attempt Two: Mathematical&lt;/h2&gt;
&lt;p&gt;If I pretend I&amp;rsquo;ve turned the input one full rotation clockwise, how does each gear – and therefore each output – behave?&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;input 1 CW
grey 1 CW
blue (irrelevant) CCW
grey 1 CW (split A)

A1 (left):
blue 20/12=5/3 CCW
grey 5/3 CCW
blue 5 CW
yellow 5 CW (out1)

A2:
blue (irrelevant) CCW
grey 1 CW
blue 20/12=5/3 CCW (split B)

...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Following this through, I arrived at the following ratios between input and output:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1:5 for the front left output&lt;/li&gt;
&lt;li&gt;1:5/3 for the front right output&lt;/li&gt;
&lt;li&gt;1:38/9 for the rear output&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Having made up simple ciphers in the past, after finding the 1:5 output I was kind of expecting the others to also be prime numbers. Oh well, the math doesn&amp;rsquo;t lie&amp;hellip; Right?&lt;/p&gt;
&lt;p&gt;But after writing some python code to show outputs for every possible first couple characters of input, I couldn&amp;rsquo;t get the outputs to match the ciphertext. Notably, for lots of valid inputs, the rear and right-side outputs sat between letters. Unless this machine would be provided to users with specific instructions, that seemed odd.&lt;/p&gt;
&lt;p&gt;I spent a long time chasing guesses about what those instructions might be - &amp;ldquo;Keep turning until the outputs are all exactly at a tooth/letter&amp;rdquo;? &amp;ldquo;Keep turning until the one output for this step is exactly at a tooth/letter&amp;rdquo;? After some rubber duck debugging and a gentle nudge from the challenge author, I re-examined my understanding of the machine, but still couldn&amp;rsquo;t find anything wrong.&lt;/p&gt;
&lt;h2 id=&#34;the-nuclear-option&#34;&gt;The Nuclear Option&lt;/h2&gt;
&lt;p&gt;If I couldn&amp;rsquo;t logic it out, I&amp;rsquo;d try to rebuild the device&amp;rsquo;s mechanics using a tool.&lt;/p&gt;
&lt;p&gt;There were several relatively simple tools for Lego gear ratios, but besides them being a bit difficult to build a complex machine in, it didn&amp;rsquo;t seem they would be able to handle the differential, or possibly even the splits.&lt;/p&gt;
&lt;p&gt;I ended up finding the Technic Brick Power &lt;a href=&#34;https://technicbrickpower.com/tools/gearing_ratio_calculator&#34;&gt;Gearing Ratio Calculator&lt;/a&gt;, which seemed to do exactly what I needed! &amp;hellip;when provided with a &amp;ldquo;Lego Technic .ldr file&amp;rdquo;. Okay, it seems I need to use something like &lt;a href=&#34;https://www.leocad.org/&#34;&gt;LeoCAD&lt;/a&gt; to build that&amp;hellip;&lt;/p&gt;
&lt;p&gt;A lot of twiddling and building later, I had reproduced the machine:
&lt;img src=&#34;../../../images/legonigma/sim-wrong.jpg&#34; alt=&#34;Image of the machine in LeoCAD&#34;&gt;&lt;/p&gt;
&lt;p&gt;But when I loaded it into the gearing ratio tool, attaching a motor, and running it, the entire model turned red, indicating a jammed motor.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-angry.png&#34; alt=&#34;Screenshot of the ratio calculator with all parts colored red&#34;&gt;&lt;/p&gt;
&lt;p&gt;I assumed the tool just didn&amp;rsquo;t know how to handle the differential, and removed the gear before the rear output to let both sides of that branch run separately, then combined the adjacent gears&amp;rsquo; motion myself. The results matched my original calculations perfectly, and I was lost again.&lt;/p&gt;
&lt;p&gt;After many more lines of code and attempts to figure out how the machine would be used, I eventually arrived at a realization: In a row of three gears, turning the first clockwise at 1 RPM and the third clockwise at 2 RPM does not cause the middle one to rotate counterclockwise at 3 RPM. Don&amp;rsquo;t ask how I had arrived at my original understanding – I do not know, and it is as obviously wrong in retrospect as it is to you. The resulting conclusion was that the branch through the front-right output and the differential was not modifying the ratio of the rear output. The simulation was correct. My understanding of the machine actually &lt;em&gt;was&lt;/em&gt; a deadlock.&lt;/p&gt;
&lt;p&gt;I stared at the challenge&amp;rsquo;s renders more, and eventually found the problem: There was in fact no motion going &lt;em&gt;through&lt;/em&gt; the front-right output&amp;rsquo;s axle, and the direction of control through the differential is the opposite of what I thought.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/Legonigma_Front.jpg&#34; alt=&#34;Front view of Lego gadget&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-wrong.jpg&#34; alt=&#34;Front view of incorrect recreation&#34;&gt;&lt;/p&gt;
&lt;p&gt;See it? I sure didn&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/Legonigma_Front-line.jpg&#34; alt=&#34;Front view of Lego gadget with a line highlighting the difference&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-wrong-line.jpg&#34; alt=&#34;Front view of incorrect recreation with a line highlighting the difference&#34;&gt;&lt;/p&gt;
&lt;p&gt;After some logic, some trial-and error, and a lot of staring at close-ups of small details in the original render, I moved a few elements around. Here&amp;rsquo;s a bottom view of the change, with the front-right output gear at the top center:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-wrong-bottom.jpg&#34; alt=&#34;Bottom view of incorrect recreation&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-bottom.jpg&#34; alt=&#34;Bottom view of corrected recreation&#34;&gt;&lt;/p&gt;
&lt;p&gt;Then I loaded the amended model into the simulator:&lt;/p&gt;
&lt;video class=&#34;video-animation&#34; autoplay loop muted playsinline aria-label=&#34;Corrected model in the gearing ratio calculator&#34;&gt;
    &lt;source src=&#34;../../../images/legonigma/sim-fixed.mp4&#34; type=&#34;video/mp4&#34;&gt;
    
    Your browser does not seem to support this video.
&lt;/video&gt;

&lt;p&gt;And got some much nicer ratios for the outputs&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../images/legonigma/sim-ratios.png&#34; alt=&#34;Screenshot of sim info panel for output gears, showing ratios of 1:5, 1:-7, and 1:13&#34;&gt;&lt;/p&gt;
&lt;p&gt;So I updated my code to use these much more reasonable ratios, aaaaand&amp;hellip; No valid input produced an output matching the first three characters of ciphertext.&lt;/p&gt;
&lt;h2 id=&#34;reaching-further&#34;&gt;Reaching further&lt;/h2&gt;
&lt;p&gt;Lacking any idea how the outputs were meant to be read, I figured it was time to investigate the challenge name&amp;rsquo;s call-out to the German Enigma machine to look for hints.&lt;/p&gt;
&lt;p&gt;Enigma&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Known-plaintext attack&lt;/li&gt;
&lt;li&gt;Not ECB (advances rotors by each character)&lt;/li&gt;
&lt;li&gt;Random mapping in each code wheel and on plugboard&lt;/li&gt;
&lt;li&gt;Never encrypted a letter to itself&lt;/li&gt;
&lt;li&gt;Decryption == encryption (if a-&amp;gt;r, r-&amp;gt;a)&lt;/li&gt;
&lt;li&gt;Key is rotor selection, rotor position, reflector position, plugboard config&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This machine&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No known-plaintext available&lt;/li&gt;
&lt;li&gt;ECB (all rotors end up back at A when input is rotated back to A)&lt;/li&gt;
&lt;li&gt;Probably not enough ciphertext to do statistics for random mappings&lt;/li&gt;
&lt;li&gt;Encrypts A to A,A,A&lt;/li&gt;
&lt;li&gt;How to decrypt, besides &amp;ldquo;turn input til all three outputs match your ciphertext block&amp;rdquo;?&lt;/li&gt;
&lt;li&gt;If there&amp;rsquo;s a key, how is it configured? 4 pointer positions * 4 rotors * 6 output rotor orders = 96 possibilities?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Maybe I was missing an instruction like &amp;ldquo;Advance the input by n teeth, where n is the index of the next plaintext character&amp;rdquo;? I couldn&amp;rsquo;t figure out how to turn the three outputs into a ciphertext.&lt;/p&gt;
&lt;h2 id=&#34;feeling-dumb-again&#34;&gt;Feeling Dumb (Again)&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s no galaxy brain instructions, I just needed read the outputs in a particular order, trivially discovered. 1:13 first, then 1:5, then 1:7.&lt;/p&gt;
&lt;p&gt;I achieved the second solve for Legonigma, for 460 points, 17 minutes after the first team.
&lt;code&gt;SCTF{th3w0rld1sadang3rou5placeno7becau5e0fth0sewh0do3v1lbutbecau5eofth053wh0l00konandd0n0th1ng}&lt;/code&gt;
(&amp;ldquo;The world is a dangerous place, not because of those who do evil, but because of those who look on and do nothing&amp;rdquo;)&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I wish the ratios hadn&amp;rsquo;t been whole numbers, so the &amp;ldquo;no reversing&amp;rdquo; instruction meant something!&lt;/p&gt;
&lt;p&gt;Thanks to &amp;ldquo;Your expectations are reasonable&amp;rdquo; Cybrosis for a really neat challenge and a couple nudges, rebuilding the machine was fun.
Thanks also to Samsung for organizing this event, and my teammates at WreckTheLine for all of their work and for putting up with my rubber ducking.&lt;/p&gt;
&lt;h2 id=&#34;downloads&#34;&gt;Downloads&lt;/h2&gt;
&lt;p&gt;My Python code, including a lot of commented-out wrong attempts, and overbuilt since I originally expected one full input rotation to not return to the starting state: &lt;a href=&#34;https://sec.gd/blog/en/posts/legonigma/legonigma.py&#34;&gt;legonigma.py&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The .ldr model of the machine I built in LeoCAD: &lt;a href=&#34;https://sec.gd/blog/en/posts/legonigma/legonigma.ldr&#34;&gt;legonigma.ldr&lt;/a&gt;&lt;/p&gt;]]></content:encoded>
        <dc:creator>mal</dc:creator>
        <media:content url="https://sec.gd/blog/images/legonigma/Legonigma_Front.jpg" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        <media:content url="https://sec.gd/blog/images/legonigma/Legonigma_Front.jpg" medium="image"><media:title type="html">meta image</media:title></media:content>
        
          
            
              <category>hacker&#39;s playground</category>
            
          
            
              <category>crypto</category>
            
          
            
              <category>misc</category>
            
          
        
        
          
            
              <category>ctf</category>
            
          
        
        
      </item>
    
      <item>
        <title>ImaginaryCTF 2021</title>
        <link>https://sec.gd/blog/en/posts/imaginaryctf-2021/</link>
        <pubDate>Tue, 27 Jul 2021 04:00:00 +0000</pubDate>
        <author>mal@sec.gd (mal)</author>
        <atom:modified>Tue, 27 Jul 2021 04:00:00 +0000</atom:modified>
        <guid>https://sec.gd/blog/en/posts/imaginaryctf-2021/</guid>
        <description>This weekend I participated in &lt;a href=&#34;https://2021.imaginaryctf.org/&#34;&gt;ImaginaryCTF 2021&lt;/a&gt; with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;WreckTheLine&lt;/a&gt;. We finished third out of 1018 entrants, the final team to complete all 55 challenges and hit 11330 points, missing second place by 3.5 minutes. Here are my writeups for System Hardening 5 and New Technology.</description>
        <content:encoded>&lt;![CDATA[&lt;p&gt;&lt;img src=&#34;../../../images/imaginaryctf-2021/ictf-2021-banner.jpg&#34; alt=&#34;ImaginaryCTF 2021 Banner&#34;&gt;&lt;/p&gt;
&lt;p&gt;This weekend I participated in &lt;a href=&#34;https://2021.imaginaryctf.org/&#34;&gt;ImaginaryCTF 2021&lt;/a&gt;
with &lt;a href=&#34;https://wrecktheline.com/&#34;&gt;WreckTheLine&lt;/a&gt;. We finished third out of 1018
entrants, the final team to complete all 55 challenges and hit 11330 points,
missing second place by 3.5 minutes.&lt;/p&gt;
&lt;h2 id=&#34;system-hardening-5&#34;&gt;System Hardening 5&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;&amp;ldquo;CyberPatriot but run by roo fanatics&amp;hellip; What could go wrong&amp;hellip;&amp;rdquo;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;At 450 points, System Hardening 5 was in the highest value category of
challenges. It was in the pattern of CyberPatriot, where teams download and
run a VM, find and fix all of the vulnerabilities. A scoring engine checks the
scored items every minute or so and reports to the scoreboard.&lt;/p&gt;
&lt;p&gt;As with CyberPatriot, we start with the &lt;a href=&#34;https://system-hardening-5.chal.imaginaryctf.org/readme&#34;&gt;readme&lt;/a&gt;.
The major constrants today are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No updates at all. Makes sense, that&amp;rsquo;s time-consuming and uninteresting.&lt;/li&gt;
&lt;li&gt;Critical services: Remote desktop, print spooler.&lt;/li&gt;
&lt;li&gt;Don&amp;rsquo;t disable SMB or the scoring engine may break&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;forensics&#34;&gt;Forensics&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s start&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; with the Forensics questions, as the readme says, so we don&amp;rsquo;t
accidentally destroy artifacts we&amp;rsquo;ll need to answer them.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;An [sic] user on this system was compromised, allowing rooReaper to break in. What is
the username of this user?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Well, let&amp;rsquo;s look around. Event log? Exploit artifacts? Let&amp;rsquo;s check this other
file on the desktop first, &lt;code&gt;text-messages-from-mom.txt.txt&lt;/code&gt; [sic].&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; hi roo! this is definitely mom here&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; oh hi mom didnt see you there all too well&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; ?&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; nvm, what did you text me for&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; and is this a new number? i dont see the message history&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; uhh yea this is a new number&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; oh ok&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; can you disable your firewall? and enable smb pls and disable your antivirus&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; ok whatever you say mom&amp;hellip;&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; ill look up some tutorials&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; NO DONT LOOK AT TUTORIALS&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; ok i wont what do i do&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; ill show you&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; btw what is rooPOG&amp;rsquo;s password&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; i heard he loves sharing&lt;br&gt;
&lt;strong&gt;rooYay:&lt;/strong&gt; its just password1337 nothing fancy&lt;br&gt;
&lt;strong&gt;MOMtotallynotrooreaper:&lt;/strong&gt; thanks!&lt;br&gt;
&amp;hellip;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Looks like rooYay fell for the classic &amp;ldquo;hi this is your mother&amp;rdquo; phish, and gave
out &lt;strong&gt;rooPOG&lt;/strong&gt;&amp;rsquo;s password.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Forensics Question 1 correct - 7 points&lt;/div&gt;
&lt;p&gt;That also gives us the answer to the next forensics question:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What is the password of the compromised user from the previous question?&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;password1337&lt;/strong&gt; nothing fancy&lt;/p&gt;&lt;/blockquote&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Forensics Question 2 correct - 7 points&lt;/div&gt;
&lt;p&gt;The other forensics questions require some research.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What is the CVE ID of the vulnerability that allowed rooReaper to escalate
privileges?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I poked around in rooPOG&amp;rsquo;s home directory for a bit, finding &lt;code&gt;xconsole.exe&lt;/code&gt; and
&lt;code&gt;vlib.dll&lt;/code&gt; in &lt;code&gt;Downloads\foobar&lt;/code&gt;. The latter was detected as Win32/Mamson.A!ac,
which doesn&amp;rsquo;t obviously use any exploits. I enabled Windows Defender and
started a scan, and before I had found anything in the event viewer, it had
found &lt;code&gt;C:\temp\PrintNightmareLPE.exe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Print Nightmare, &lt;a href=&#34;https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-34527&#34;&gt;CVE-2021-34527&lt;/a&gt;,
is a recent vulnerability that allows a remote attacker to install a malicious
printer driver configuration DLL to gain arbitrary code execution as SYSTEM. A
variant, &lt;a href=&#34;https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-1675&#34;&gt;&lt;strong&gt;CVE-2021-1675&lt;/strong&gt;&lt;/a&gt;,
is useful for local exploitation, and was accepted as the answer.&lt;/p&gt;
&lt;p&gt;Both of these drop the payload, in this case &lt;code&gt;vlib.dll&lt;/code&gt;, in
&lt;code&gt;C:\Windows\System32\spool\drivers\x64\3\&lt;/code&gt;, where Defender found it alongside
&lt;code&gt;reverse_64bit.dll&lt;/code&gt; which it detected as Win64/Meterpreter.B. I removed the
whole folder, along with &lt;code&gt;C:\temp\&lt;/code&gt; and the contents of rooPOG&amp;rsquo;s Downloads
folder.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Forensics Question 3 correct - 7 points&lt;br&gt;
Removed privilege escalation payload - 5 points&lt;/div&gt;
&lt;p&gt;The final forensics question seemed like a steganography problem, but referred
to another challenge in the CTF:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The background image on the Desktop contains a secret message. What is it?&lt;br&gt;
HINT: It&amp;rsquo;s related to another challenge in this CTF, a reversing challenge.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The wallpaper was &lt;code&gt;background.png&lt;/code&gt; in rooYay&amp;rsquo;s Pictures folder. It consisted of
a grid of tiles, in a repeating pattern &lt;code&gt;Axxxx&lt;/code&gt;, where there were 3 options for
&lt;code&gt;x&lt;/code&gt;. No obvious pattern was shown by &lt;code&gt;stegsolve&lt;/code&gt;, and it wasn&amp;rsquo;t clear how to
decode information in the tile pattern. I put it aside until a teammate,
JaGoTu, solved &lt;code&gt;roolang&lt;/code&gt;, which seemed potentially-related. Sure enough:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;JaGoTu: roolang was just a vm that was calculating fibonacii very slowly&lt;br&gt;
JaGoTu: can you send the raw png somewhere?&lt;br&gt;
JaGoTu: cause the roolang basically runs png files&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;They ran their roolang solution on the PNG, and sent back the output:
&lt;strong&gt;Hello, and welcome to the roos&amp;rsquo; server!&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Forensics Question 4 correct - 7 points&lt;/div&gt;
&lt;h3 id=&#34;pups-not-the-fun-kind&#34;&gt;PUPs (Not the Fun Kind)&lt;/h3&gt;
&lt;p&gt;With the forensics questions out of the way, let&amp;rsquo;s continue through the readme.
We have a list of needed services and software, so let&amp;rsquo;s remove anything that&amp;rsquo;s
not on it, starting with the faux-Clippy in the corner of the screen.&lt;/p&gt;
&lt;p&gt;First, the Startup folder for All Users in
&lt;code&gt;C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\&lt;/code&gt;.
Two shortcuts:\&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;chksec - Shortcut.lnk&lt;/code&gt; to &lt;code&gt;C:\Windows\System32\chksec.exe&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;essential - Shortcut.lnk&lt;/code&gt; to &lt;code&gt;C:\Windows\System32\essential.bat&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Delete the shortcuts, kill the processes, delete the Clippy executable.
&lt;code&gt;essential.bat&lt;/code&gt; printed some ascii art text, copied itself to the current
user&amp;rsquo;s start menu Startup folder, opened a
&lt;a href=&#34;https://www.youtube.com/watch?v=xvFZjo5PgG0&#34;&gt;video&lt;/a&gt;, and then &lt;code&gt;pause&lt;/code&gt;d.
Delete the batch file from System32 and from rooYay&amp;rsquo;s
&lt;code&gt;%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Removed PUP Clippy - 3 points&lt;br&gt;
Removed rickroll malware - 4 points&lt;/div&gt;
&lt;h3 id=&#34;installed-programs&#34;&gt;Installed Programs&lt;/h3&gt;
&lt;p&gt;There&amp;rsquo;s a couple shortcuts on the desktop to software that wasn&amp;rsquo;t mentioned as
required in the readme, so let&amp;rsquo;s remove those. I replaced Chrome with Firefox
portable, since the roos prefer IE and I very much do not. Nothing else in
add/remove programs looked relevant.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Removed unauthorized program Minecraft Launcher - 3 points&lt;br&gt;
Removed unauthorized program SuperAntiSpyware - 3 points&lt;/div&gt;
&lt;p&gt;I also looked through the running processes, but nothing else there raised
suspicion.&lt;/p&gt;
&lt;h3 id=&#34;users&#34;&gt;Users&lt;/h3&gt;
&lt;p&gt;Next, let&amp;rsquo;s deal with users, which is easiest from
&lt;code&gt;compmgmt.msc&lt;/code&gt;/&lt;code&gt;lusrmgr.msc&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;roocursion&lt;/code&gt; as requested in the readme&lt;/li&gt;
&lt;li&gt;Rename the local administrator account as requested (can be done here or in
group policy)&lt;/li&gt;
&lt;li&gt;Make sure nobody unauthorized is in Administrators or other privileged groups&lt;/li&gt;
&lt;li&gt;Make sure no non-builtin users exist who aren&amp;rsquo;t mentioned, poking through
their home folders before deleting&lt;/li&gt;
&lt;li&gt;Disable the Guest account, and rename for good measure&lt;/li&gt;
&lt;li&gt;Change the password for all users&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;rsquo;ll mention here that CyberPatriot often uses significantly outdated best
practices and ignores how things would be done in the real world, and we also
don&amp;rsquo;t know what this challenge will score on, so a lot of the things I check
and changes I make to this machine are excessive, paranoid, annoying, or
otherwise just weird. If we lose points for something we can easily undo the
change.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Removed unauthorized user rooReaper - 3 points&lt;br&gt;
Removed unauthorized user rooDevil - 3 points&lt;br&gt;
Removed unauthorized user rooRage - 3 points&lt;br&gt;
User rooFrozen is not an administrator - 3 points&lt;br&gt;
User roocursion has been created - 3 points&lt;br&gt;
Administrator account renamed - 3 points&lt;br&gt;
Guest account is disabled - 4 points&lt;/div&gt;
&lt;h3 id=&#34;services-and-firewall&#34;&gt;Services and Firewall&lt;/h3&gt;
&lt;p&gt;Interspersed with everything else, I poke around &lt;code&gt;services.msc&lt;/code&gt; and the control
panel to make sure everything is sane. Enable the firewall
MOMtotallynotrooreaper told us to disable, then make sure there&amp;rsquo;s no
nonstandard or scary rules enabled. Enable cloud protection and sample
sumbission for Defender, make sure the scan finished. Right click My Computer
and click Properties, check $PATH and other environment variables for anything
weird, make sure Data Execution Prevention is enabled for all applications, and
disable NetBIOS.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Windows Firewall is enabled - 5 points&lt;/div&gt;
&lt;h3 id=&#34;group-policy&#34;&gt;Group Policy&lt;/h3&gt;
&lt;p&gt;Before I forget, let&amp;rsquo;s make sure we&amp;rsquo;ve mitigated Print Nightmare. The
&lt;a href=&#34;https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-34527&#34;&gt;MSRC post&lt;/a&gt;
about it recommends updating (oops, can&amp;rsquo;t), disabling the print spooler (oops,
can&amp;rsquo;t), or disabling inbound remote printing with Group Policy.&lt;/p&gt;
&lt;p&gt;I fire up &lt;code&gt;gpedit.msc&lt;/code&gt;, open Computer Configuration -&amp;gt;
Windows Settings -&amp;gt; &amp;hellip; Where&amp;rsquo;s Security Settings?&lt;/p&gt;
&lt;p&gt;This is where I spent about half my time on this challenge. When launching
&lt;code&gt;gpedit.msc&lt;/code&gt;, Security Settings was missing. &lt;code&gt;secpol.msc&lt;/code&gt; would show the tree
starting at Windows Settings instead of Security Settings, and also omit the
latter. Many answers on the internet offered a simple &lt;code&gt;regsvr32 wsecedit.dll&lt;/code&gt;,
which doesn&amp;rsquo;t work as the DLL doesn&amp;rsquo;t have the correct entrypoints for that.
This is where I ran &lt;code&gt;dism /online /cleanup-image /restorehealth&lt;/code&gt; and
&lt;code&gt;sfc /scannow&lt;/code&gt;, which didn&amp;rsquo;t find any problems.&lt;/p&gt;
&lt;p&gt;I never did figure out what was done to break that, as I eventually gave up and
tried importing an exported policy, and later started setting the policy
registry entries by hand. After a while, I opened the mmc snapin again, and
Security Settings was there!&lt;/p&gt;
&lt;p&gt;It seems that it&amp;rsquo;s only broken for the rooYay user, and after setting UAC to
require a password, I was unintentionally elevating as rooAstro, for whom it
worked correctly. Something in my profile was breaking it. Some windows
LD_LIBRARY_PATH type thing? Something in HKEY_CURRENT_USER? It didn&amp;rsquo;t directly
affect security, so I put it aside again.&lt;/p&gt;
&lt;p&gt;From there, I could much more easily continue with group policy, basically just
going through every option in Security Settings -&amp;gt; Account Policies and Local
Policies and some folders in Administrative Templates -&amp;gt; Windows Settings,
setting everything to the most paranoid sane option.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Remember 24 passwords&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;60 day maximum password age&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;10 day minimum password age&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;14 character minimum length&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Must meet complexity requirements&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;No reversible encryption&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;15 minute account lockout duration&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;10 attempt lockout threshold&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;15 minute counter reset timer&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enable all auditing&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Go through User Rights Assignment and restrict everything as much as possible&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Hey, why is &lt;code&gt;Everyone&lt;/code&gt; allowed to take ownership of files??&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You know what, let&amp;rsquo;s just disable the Administrator account&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I don&amp;rsquo;t like Microsoft account signin&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;More audit everything&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Prevent users from installing printer drivers (well&amp;hellip; try to)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Restrict CD-ROM access to locally logged-on users&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Require CTRL+ALT+DEL, force inactivity limit, require strong encryption&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You get the idea.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Everyone is not allowed to take ownership of files and other objects - 5 points&lt;/div&gt;
&lt;h3 id=&#34;critical-services&#34;&gt;Critical Services&lt;/h3&gt;
&lt;p&gt;And let&amp;rsquo;s not forget to secure our critical services. The MSRC Print Nightmare
article points me to Administrative Templates -&amp;gt; Printers to disable &amp;ldquo;Allow
Print Spooler to accept client connections&amp;rdquo;. There have been a few RDP
vulnerabilities, but none with straightforward workarounds besides patching, so
let&amp;rsquo;s just be sure to flip on all of the RDP server security policies. Lastly,
we weren&amp;rsquo;t disallowed from disabling old versions of SMB, so use the powershell
command in the
&lt;a href=&#34;https://docs.microsoft.com/en-us/windows-server/storage/file-server/troubleshoot/detect-enable-and-disable-smbv1-v2-v3&#34;&gt;docs&lt;/a&gt;,
to check for SMBv1 and see it&amp;rsquo;s already disabled.&lt;/p&gt;
&lt;div class=&#34;alert alert-info&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Inbound remote printing is disabled (PrintNightmare mitigation) - 9 points&lt;br&gt;
Remote Desktop uses network level authentication - 4 points&lt;/div&gt;
&lt;h3 id=&#34;other-stuff&#34;&gt;Other Stuff&lt;/h3&gt;
&lt;p&gt;In an advanced challenge, in addition to &lt;code&gt;sfc&lt;/code&gt; and &lt;code&gt;dism&lt;/code&gt; to look for
&amp;ldquo;creatively modified&amp;rdquo; system files, &lt;code&gt;hijackthis&lt;/code&gt; can be helpful. In case
of a well-disguised network backdoor, it&amp;rsquo;s also good to check open sockets.&lt;/p&gt;
&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;And with that, I had 91 points, 20/21 items, and the flag appeared in the score
report. There was one scored item I never found, worth 9 points. I&amp;rsquo;ll be
waiting for a writeup from tirefire, the only person with 100 points, to see
what I missed!&lt;/p&gt;
&lt;div class=&#34;alert alert-success&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Unique ID: &lt;code&gt;8edd464b&lt;/code&gt;&lt;br&gt;
Flag: &lt;code&gt;ictf{th4nks_s0_much_f0r_s3cur1ng_0ur_s3rver!_h3r3s_4_fl4g!!!}&lt;/code&gt;&lt;/div&gt;
&lt;h3 id=&#34;update&#34;&gt;Update&lt;/h3&gt;
&lt;p&gt;First, Eth007, one of the challenge authors, let me know that the last scored
item I couldn&amp;rsquo;t find was
&lt;a href=&#34;https://docs.microsoft.com/en-us/windows-server/security/credentials-protection-and-management/configuring-additional-lsa-protection&#34;&gt;LSA protection&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Second, thank you to the iCTF team for selecting this as a prize-winning writeup!&lt;/p&gt;
&lt;h2 id=&#34;new-technology&#34;&gt;New Technology&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;&amp;ldquo;If it&amp;rsquo;s not Windows New Technology, what else could NT stand for?&amp;rdquo;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This was a 300pt cryptography challenge in the form of a &lt;a href=&#34;https://imaginaryctf.org/r/3204-new_technology.py&#34;&gt;python
script&lt;/a&gt;.
It generates a random key consisting of 5 tuples, each a 512-bit prime &lt;code&gt;p&lt;/code&gt; and
an integer exponent $1 \le e &amp;lt; 4$.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;gen&lt;/span&gt;():
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    private &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; _ &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        p &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; getPrime(&lt;span style=&#34;color:#ae81ff&#34;&gt;512&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        e &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; getRandomRange(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        private&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;append((p, e))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; private, normalize(private)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The public key is derived by the &lt;code&gt;normalize()&lt;/code&gt; function, which multiplies
together &lt;code&gt;p**e&lt;/code&gt; for each private key component.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;normalize&lt;/span&gt;(fac):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; p, e &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; fac:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        n &lt;span style=&#34;color:#f92672&#34;&gt;*=&lt;/span&gt; p&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;e
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Since these are 512-bit primes, the resulting public key isn&amp;rsquo;t easy to factor.&lt;/p&gt;
&lt;p&gt;The private key is put through the &lt;code&gt;deriv()&lt;/code&gt; function, which has a nested
loop based on &lt;code&gt;divs()&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;deriv&lt;/span&gt;(priv):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; d1 &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; divs(priv):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; d2 &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; divs(d1):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            res &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; normalize(d2) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; phi(d2) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; phi(div(d1, d2))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; res
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;divs()&lt;/code&gt; takes the same &lt;code&gt;List[Set[prime: int, exponent: int], ...]&lt;/code&gt; private key
format and yields the cartesian product of them based on the range 0..e of
their exponents:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;divs&lt;/span&gt;(fac, pre&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; pre &lt;span style=&#34;color:#f92672&#34;&gt;is&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        pre &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; fac:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;yield&lt;/span&gt; pre
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        p, e &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; fac[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, e &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;yield from&lt;/span&gt; divs(fac[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;:], pre &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; [(p, i)]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; priv &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; pprint(list(divs(priv)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; [(&lt;span style=&#34;color:#ae81ff&#34;&gt;167&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;149&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)]]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For each entry in this list, &lt;code&gt;d1&lt;/code&gt;, &lt;code&gt;deriv()&lt;/code&gt; again calls &lt;code&gt;divs()&lt;/code&gt;, and loops on
that &lt;code&gt;d2&lt;/code&gt;, adding &lt;code&gt;normalize(d2) * phi(d2) * phi(div(d1, d2))&lt;/code&gt; to a running
total.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;phi&lt;/span&gt;(fac):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; p, e &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; fac:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; e: &lt;span style=&#34;color:#66d9ef&#34;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        res &lt;span style=&#34;color:#f92672&#34;&gt;*=&lt;/span&gt; (p&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;(e &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; (p &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; res
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;div&lt;/span&gt;(a, b):  &lt;span style=&#34;color:#75715e&#34;&gt;# a=d1, b=d2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    b &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; dict(b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; p, e &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; a:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;assert&lt;/span&gt; e &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;=&lt;/span&gt; b[p]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        res&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;append((p, e &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; b[p]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; res
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At some point here things started to smell funny - we&amp;rsquo;re collecting the sum of
some math done on every divisor of every divisor&amp;hellip; Just for fun, let&amp;rsquo;s see how
this behaves when run all the way through with more manageable numbers, maybe
12 bits.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;gen_custom&lt;/span&gt;(count&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;512&lt;/span&gt;, max_exp&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    private &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; _ &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(count):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        p &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; getPrime(size)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        e &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; getRandomRange(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, max_exp&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        private&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;append((p, e))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; private, normalize(private)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; priv, pub &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; gen_custom(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; priv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[(&lt;span style=&#34;color:#ae81ff&#34;&gt;3251&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;3863&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;2917&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;3761&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;), (&lt;span style=&#34;color:#ae81ff&#34;&gt;3989&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; pub
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;90459172118968888949999762493557&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; key &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; deriv(priv)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; key
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;8182861820449238390507452368725018700352715647046709310466512249&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sage.all &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; factor(pub)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2917&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3251&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3761&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3863&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3989&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; factor(key)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2917&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3251&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3761&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3863&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3989&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you don&amp;rsquo;t have &lt;a href=&#34;https://sagemath.org/&#34;&gt;sagemath&lt;/a&gt; installed, you can also use
a handy dandy &lt;a href=&#34;https://www.alpertron.com.ar/ECM.HTM&#34;&gt;webassembly ECM factorization tool&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The factors of the public key aren&amp;rsquo;t surprising, that&amp;rsquo;s the exact math we used
to derive it from the private key, but possible to reverse since we used small
numbers. On the other hand, what&amp;rsquo;s up with the private key? Each factor
just has double the exponent? And since $x^2 * y^2 = (xy)^2$, and
$(x^3)^2 = x^6$&amp;hellip; Our private key is just the square of the public key??&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; key &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; pub&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Well then! Let&amp;rsquo;s assign those commented output values to variables, square the
public key, get a new AES instance for decrypting, and&amp;hellip;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pub &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0x281ab467e16cdedb97a298249bdd334f0cc7d54177ed0946c04ec26da111&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ciphertext &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fromhex(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;d2463ccc52075674effbad1b1ea5ae9a9c8106f1...&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;key &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pub&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;cipher &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; AES&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    hashlib&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sha256(str(key)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;encode(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;utf-8&amp;#34;&lt;/span&gt;))&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;digest(),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    AES&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;MODE_CBC,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    iv&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\0&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;16&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plaintext &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; cipher&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decrypt(ciphertext)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(plaintext)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And, drumroll&amp;hellip;
&lt;code&gt;b&#39;ictf{Would_number_theory_be_new_technology?}\x04\x04\x04\x04&#39;&lt;/code&gt;!
For good practice, let&amp;rsquo;s clean that up a bit by removing the padding and
turning it back into a unicode string:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; Crypto.Util.Padding &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; unpad
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(unpad(plaintext, &lt;span style=&#34;color:#ae81ff&#34;&gt;16&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;utf-8&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And there we have it!&lt;/p&gt;
&lt;div class=&#34;alert alert-success&#34; role=&#34;alert&#34; data-dir=&#34;ltr&#34;&gt;Flag: &lt;code&gt;ictf{Would_number_theory_be_new_technology?}&lt;/code&gt;&lt;/div&gt;
&lt;h2 id=&#34;thanks&#34;&gt;Thanks&lt;/h2&gt;
&lt;p&gt;Thank you to my team for a friendly place to be the dumbest person in the room,
and to the ImaginaryCTF platform folks and challenge developers for the
interesting and challenging &amp;hellip; challenges.&lt;/p&gt;
&lt;p&gt;The header image is from the ImaginaryCTF 2021 site (&lt;a href=&#34;https://github.com/et3rnos/imaginaryctf&#34;&gt;repo&lt;/a&gt;),
used with permission from Astro.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;I&amp;rsquo;ve taken some liberties with the order I present objectives here,
since a writeup that followed my actual path would be impossible to follow.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
]]></content:encoded>
        <dc:creator>mal</dc:creator>
        <media:content url="https://sec.gd/blog/images/imaginaryctf-2021/ictf-2021-banner.jpg" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        <media:content url="https://sec.gd/blog/images/imaginaryctf-2021/ictf-2021-banner.jpg" medium="image"><media:title type="html">meta image</media:title></media:content>
        
          
            
              <category>imaginaryctf</category>
            
          
            
              <category>crypto</category>
            
          
            
              <category>misc</category>
            
          
            
              <category>sysadmin</category>
            
          
        
        
          
            
              <category>ctf</category>
            
          
        
        
      </item>
    
  </channel>
</rss>