all repos — site @ 5b6bcf864a7fe357c1d9592118340178b089d828

source for my site, found at icyphox.sh

build/blog/break-the-ice/index.html (view raw)

  1<!DOCTYPE html>
  2<html lang=en>
  3<link rel="stylesheet" href="/static/style.css" type="text/css">
  4<link rel="shortcut icon" type="images/x-icon" href="/static/favicon.ico">
  5<meta content="Memeing security since forever." name=description>
  6<meta name="viewport" content="initial-scale=1">
  7<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  8<meta content="#021012" name="theme-color">
  9<meta name="HandheldFriendly" content="true">
 10<meta name="twitter:card" content="summary_large_image">
 11<meta name="twitter:site" content="@icyphox">
 12<meta name="twitter:title" content="Anirudh">
 13<meta name="twitter:description" content="Memeing security since forever.">
 14<meta name="twitter:image" content="/static/icyphox.png">
 15<meta property="og:title" content="Anirudh">
 16<meta property="og:type" content="website">
 17<meta property="og:description" content="Memeing security since forever.">
 18<meta property="og:url" content="https://icyphox.sh">
 19<meta property="og:image" content="/static/icyphox.png">
 20<html>
 21  <title>
 22    Anirudh
 23  </title>
 24<script src="//instant.page/1.1.0" type="module" integrity="sha384-EwBObn5QAxP8f09iemwAJljc+sU+eUXeL9vSBw1eNmVarwhKk2F9vBEpaN9rsrtp"></script>
 25<div class="container">
 26  <header class="header">
 27    <a href="/">icyphox.sh</a> (<a href="https://github.com/icyphox/site">src</a>)
 28  </header>
 29<body class="noselect"> 
 30  <div class="introduction">
 31    <h1 align="center">
 32      <h1>Break the Ice — Hardware CTF</h1>
 33
 34<h2>SecureLayer7’s hardware CTF at Nullcon ’19, Goa</h2>
 35
 36<p>Earlier this month at Nullcon Goa, we had the chance to attempt a hardware CTF challenge designed by the folks at <a href="https://securelayer7.net">SecureLayer7</a>. We weren’t able to solve it during the period of 2 days that we had (we had talks and parties to be at), but the SL7 guys were kind enough to let us keep the hardware and solve it back at home. Which we did, otherwise this write-up wouldn’t have happened :)</p>
 37
 38<h3>The Hardware</h3>
 39
 40<p>So what’s this cryptic “hardware” I keep mentioning, you wonder? It’s an ESP8266 board — better known as a <strong>NodeMCU</strong>. Here’s a picture.</p>
 41
 42<p><img src="1*cWpvtbXan4LjdJBldelW-g.jpeg" alt="" /></p>
 43
 44<p>Oh, and it came with a pretty OLED display too. So the obvious task at hand was to connect the display to the board. A quick search, and we found an (ever helpful) <a href="https://www.instructables.com/id/Interface-LCD-Using-NodeMCU/">Instructables</a> link with the image down below.</p>
 45
 46<p><img src="1*1avLAYsHDTGU-JS3e6oVrA.jpeg" alt="Not the same display, but it works" /><em>Not the same display, but it works</em></p>
 47
 48<p>Mind you, we struggled quite a bit at this seemingly trivial step, but hey we’re CS students ;)</p>
 49
 50<p>On connecting the device via USB, the board spins up a wireless hotspot called “Device-6”.</p>
 51
 52<p><img src="1*wJ3ZY2EskoSSfvCjliP_jQ.png" alt="" /></p>
 53
 54<p>We tried to connect to this, but it was password protected. We’ll get back to it later.</p>
 55
 56<h3>Flash dump analysis</h3>
 57
 58<p>During one of the many web searches I made with regard to this board, an interesting tool showed up — <a href="https://github.com/espressif/esptool">esptool</a>. A Python utility to communicate with the ESP8266. Wonderful.</p>
 59
 60<p>This tool allows us to do a bunch of operations on the board, but what we’re actually interested in is reading the flash. After looking up the syntax for it, we arrived at:</p>
 61
 62<pre><code>› sudo ./esptool.py -p /dev/ttyUSB0 -b 460800 read_flash 0 0x400000 flash_contents.bin
 63Serial port /dev/ttyUSB0
 64Connecting....
 65Detecting chip type... ESP8266
 66Chip is ESP8266EX
 67Features: WiFi
 68MAC: 84:f3:eb:05:83:1e
 69Uploading stub...
 70Running stub...
 71Stub running...
 72Changing baud rate to 460800
 73Changed.
 744194304 (100 %)
 754194304 (100 %)
 76Read 4194304 bytes at 0x0 in 100.8 seconds (333.0 kbit/s)...
 77Hard resetting via RTS pin...
 78</code></pre>
 79
 80<p>The command is fairly easy to understand, the <code>-p</code> flag denotes the serial port of our device, <code>-b</code> specifies the Baud rate and <code>read_flash</code>, well, reads the flash starting at <code>0</code> till <code>0x400000</code> which is 4MB. 
 81We faced a lot of trouble here, since we kept reading only upto 2MB. Why? Because that’s what the command on the Internet said.</p>
 82
 83<p>Anyway, we have our flash dumped into a file <code>flash_contents.bin</code>.</p>
 84
 85<p>We then decided to run <code>strings</code> on the flash binary and peruse through the thousands of lines it had. Brilliant right? It was, actually. We found a bunch of interesting strings, along with what we guessed to be the wireless hotspot’s password. Spoiler alert: it was.</p>
 86
 87<p><img src="1*5Hc-_XYFw-4_hw3iZpfqkQ.png" alt="The entire dump was 6000+ lines. Did we actually do this D:" /><em>The entire dump was 6000+ lines. Did we actually do this D:</em></p>
 88
 89<p>The go-to utility to (actually) analyze binaries is <code>binwalk</code>. The <code>-e</code> flag extracts the known file types it recognizes within the binary.</p>
 90
 91<pre><code>› binwalk -e flash_contents.bin
 92
 93DECIMAL       HEXADECIMAL     DESCRIPTION
 94--------------------------------------------------------------------------------
 95283960        0x45538         Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WiFi/src/include/DataSource.h
 96289387        0x46A6B         HTML document footer
 97291156        0x47154         HTML document header
 98291296        0x471E0         Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/abi.cpp
 993145728       0x300000        Squashfs filesystem, little endian, version 4.0, compression:gzip, size: 139733 bytes, 10 inodes, blocksize: 131072 bytes, created: 2019-02-25 09:14:19
100</code></pre>
101
102<p>We see a squashfs filesystem here. <code>binwalk</code> creates a directory in your current path containing all the files and folders it managed to extract. <code>cd</code>ing into our squashfs folder, we see this:</p>
103
104<p><img src="1*VsEzd8PSYMIUwjBLNFFetA.png" alt=":O" /><em>:O</em></p>
105
106<p>Oooh yes. <code>cat</code>ting the file, we see:</p>
107
108<pre><code>› cat 1/Hidden.txt
109
110######################################### Hints :) ########################################
111
112---telnet server on esp
113
114--Hunt the key to get MQTT creds
115          -- 
116--MQTT box
117
118--Publish the correct message to get ^FLAG^
119
120&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;     PUBLISH..... DISPLAY.... SUBMIT.... :)  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
121</code></pre>
122
123<p>Looking inside the directory named <code>2</code>, we see another dir <code>3</code> containing a JPEG image and a file telling us about steganography.</p>
124
125<p><img src="1*68k1Y6IoK0XTCPTQRn_0fw.png" alt="" /></p>
126
127<p>And the final directory <code>4</code> had nothing in it but a file with the string <code>flag</code>. Probably to show up as a false positive in the <code>strings</code> output of the flash dump.</p>
128
129<h3>Connecting to “Device-6”</h3>
130
131<p>The first file we came across, containing the hints, mentioned a <code>telnet</code> server running on the board. But how do we reach it? Yep, via the wireless hotspot it exposes — “Device-6”. We authenticated using the PSK we found earlier. 
132On doing so, we’re prompted with a captive portal:</p>
133
134<p><img src="1*XelmAgITUw-9aZc26meUDQ.png" alt="" /></p>
135
136<p>A few things can be done here, configure WiFi on the board, view some info about the board, and reset it. Let’s connect the ESP to our own SSID — like a mobile hotstpot.</p>
137
138<p><img src="1*oQcTNKOFGphPbX50K2pmlg.png" alt="" /></p>
139
140<p>Once that’s done, we should see the “Device-6” SSID disappear, indicating that the board is now connected to our own wireless hotstpot. Another thing we notice is the board lights up, and so does our display!</p>
141
142<p><img src="1*lzKOxEkzJqo8TNI4WckmOg.png" alt="That’s so sad. Alexa play Despacito." /><em>That’s so sad. Alexa play Despacito.</em></p>
143
144<h3>The telnet server</h3>
145
146<p>Once our host machine and the ESP are on the same network, we can <code>nmap</code> our subnet to find our ESP’s IP.</p>
147
148<p><img src="1*lPNqoIFmNfxfabdt4sqYSQ.png" alt="nmap scan report" /><em>nmap scan report</em></p>
149
150<p>We see an <code>http</code> server running, which was obviously the captive portal, and our <code>telnet</code> server on port 23.</p>
151
152<pre><code>› telnet 192.168.43.223
153Trying 192.168.43.223...
154Connected to 192.168.43.223.
155Escape character is '^]'.
156Press Enter &amp; sumbit your key :)
157somekey
158Wrong Key!!!
159</code></pre>
160
161<p>On connecting, we see a prompt asking for a key. And no, ‘sumbit’ was spelt that way ;)</p>
162
163<p>Where could this key possibly be? Well, the only unexplored part of this CTF so far is the image file we came across before. So… steganography.</p>
164
165<p>Although you won’t need it, I downloaded this Docker image for cracking stego — <a href="https://hub.docker.com/r/dominicbreuker/stego-toolkit/">stego-toolkit</a>. We then tossed the image under a bunch of steganography detection and breaking tools, but to no avail.</p>
166
167<p>After a good while <code>steghide</code> gave us something:</p>
168
169<pre><code>› steghide extract -sf 10071856.jpg            
170Enter passphrase:
171</code></pre>
172
173<p>This took <em>really</em> long for us to figure but the password was the name of the image file itself. Urgh. On entering the password, we get a <code>keys.txt</code> file. Here’s what it looked like:</p>
174
175<pre><code>So you guessed the password i think...
176
177Nice!!!
178
179Key is somewhere hidden in this strings ...
180
181XH}&lt;
182TJJ*
183Y#pU
184&lt;g?/N
185gr[i}5
186&gt;+h1
187...snip...
188jlW8B
189yjbm
190M4%'
191tx;ZzL
1923 k]
193wPUf'rc
194)Pz#
1950AwN\
196Lgr:J2
197!H9u
1984bSVy
199(*-C
200nOf2E\
201
202Aaaaaand key is not guessable ....
203
204WARNING:Manual checking for correct key might take you 2 days to complete the challange!!
205</code></pre>
206
207<p>Nearly 600 lines of gibberish. We guessed that one of these strings had to be they key for our <code>telnet</code> session. We tried to automate it, but the <code>telnet</code> session was very unstable. So being the madmen we were, we did it manually. We had all the time in the world. Off we went, copy/pasting the keys in batches of 5… and it worked.</p>
208
209<p><img src="1*vY84DrSpJU1H4c9pSvoB5Q.png" alt="yeet" /><em>yeet</em></p>
210
211<p>As the hint file mentioned, we had to connect to an MQTT instance somewhere and publish something for the flag. So this is what they were talking about.</p>
212
213<p>For those out-of-the-loop, <a href="https://en.wikipedia.org/wiki/MQTT">MQTT</a> is the protocol used in IoT basec client-server interactions, among other things. Go read about it if you want to understand the next bit.</p>
214
215<h3>Capturing the flag</h3>
216
217<p>To interact with the MQTT server, we’ll be using the <a href="https://mosquitto.org">Mosquitto</a> client. We then use the credentials and attempt to “publish” a message:</p>
218
219<pre><code>› mosquitto_pub -h 'm16.cloudmqtt.com' -p 17551  -t 'inTopic/web/test' -u 'hchzbuhr' -P 'Sz4plHnlVnHc' -m '(^.^)'
220</code></pre>
221
222<p><img src="1*W_iVf3vDf4UaelycMbvPvw.png" alt="UwU" /><em>UwU</em></p>
223
224<p>After messing around with this for quite a bit (as is evident from the screen behind), we tried sending the string ‘flag’ as our message and… <em>dramatic pause</em> we got what you’d expect.</p>
225
226<p><img src="1*sO9vDtGgGjejxklF46gTlg.jpeg" alt="We were 10 days late, mind you" /><em>We were 10 days late, mind you</em></p>
227
228<h3>Conclusion</h3>
229
230<p>This was our first time playing a hardware CTF, and to be honest, there wasn’t <em>much *of “hacking” involved — at least by the word’s textbook definition. A lot of guesswork too, which made some parts of it excruciatingly painful to figure out. But all things considered, it was probably the most fun CTF I’ve played yet. Here’s a shoutout to the folks at SL7 for making this CTF *and</em> letting us keep the ESP :)</p>
231
232<p>That’s it. The end.</p>
233
234    </h1>
235   </div>
236
237    <div class="footer">
238      <div class="left">
239      &copy; 2019 — <a href="mailto:icyph0x@pm.me">icyph0x@pm.me</a>
240      </div>
241
242      <div class="right">
243        <a href="https://github.com/icyphox" target="_blank">GitHub</a>
244        <a href="https://twitter.com/icyphox" target="_blank">Twitter</a>
245        <a href="/blog" target="_blank">Blog</a>
246        <a href="/about" target="_blank">About</a>
247      </div>
248      </body>
249    </div>
250</html>