pages/blog/break-the-ice.md (view raw)
1# Break the Ice — Hardware CTF
2
3## SecureLayer7’s hardware CTF at Nullcon ’19, Goa
4
5Earlier this month at Nullcon Goa, we had the chance to attempt a hardware CTF challenge designed by the folks at [SecureLayer7](https://securelayer7.net). 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 :)
6
7### The Hardware
8
9So what’s this cryptic “hardware” I keep mentioning, you wonder? It’s an ESP8266 board — better known as a **NodeMCU**. Here’s a picture.
10
11![](1*cWpvtbXan4LjdJBldelW-g.jpeg)
12
13Oh, 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) [Instructables](https://www.instructables.com/id/Interface-LCD-Using-NodeMCU/) link with the image down below.
14
15![Not the same display, but it works](1*1avLAYsHDTGU-JS3e6oVrA.jpeg)*Not the same display, but it works*
16
17Mind you, we struggled quite a bit at this seemingly trivial step, but hey we’re CS students ;)
18
19On connecting the device via USB, the board spins up a wireless hotspot called “Device-6”.
20
21![](1*wJ3ZY2EskoSSfvCjliP_jQ.png)
22
23We tried to connect to this, but it was password protected. We’ll get back to it later.
24
25### Flash dump analysis
26
27During one of the many web searches I made with regard to this board, an interesting tool showed up — [esptool](https://github.com/espressif/esptool). A Python utility to communicate with the ESP8266. Wonderful.
28
29This 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:
30
31```
32› sudo ./esptool.py -p /dev/ttyUSB0 -b 460800 read_flash 0 0x400000 flash_contents.bin
33Serial port /dev/ttyUSB0
34Connecting....
35Detecting chip type... ESP8266
36Chip is ESP8266EX
37Features: WiFi
38MAC: 84:f3:eb:05:83:1e
39Uploading stub...
40Running stub...
41Stub running...
42Changing baud rate to 460800
43Changed.
444194304 (100 %)
454194304 (100 %)
46Read 4194304 bytes at 0x0 in 100.8 seconds (333.0 kbit/s)...
47Hard resetting via RTS pin...
48```
49
50
51The command is fairly easy to understand, the `-p` flag denotes the serial port of our device, `-b` specifies the Baud rate and `read_flash`, well, reads the flash starting at `0` till `0x400000` which is 4MB.
52We faced a lot of trouble here, since we kept reading only upto 2MB. Why? Because that’s what the command on the Internet said.
53
54Anyway, we have our flash dumped into a file `flash_contents.bin`.
55
56We then decided to run `strings` 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.
57
58![The entire dump was 6000+ lines. Did we actually do this D:](1*5Hc-_XYFw-4_hw3iZpfqkQ.png)*The entire dump was 6000+ lines. Did we actually do this D:*
59
60The go-to utility to (actually) analyze binaries is `binwalk`. The `-e` flag extracts the known file types it recognizes within the binary.
61
62```
63› binwalk -e flash_contents.bin
64
65DECIMAL HEXADECIMAL DESCRIPTION
66--------------------------------------------------------------------------------
67283960 0x45538 Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WiFi/src/include/DataSource.h
68289387 0x46A6B HTML document footer
69291156 0x47154 HTML document header
70291296 0x471E0 Unix path: /root/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/abi.cpp
713145728 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
72```
73
74
75We see a squashfs filesystem here. `binwalk` creates a directory in your current path containing all the files and folders it managed to extract. `cd`ing into our squashfs folder, we see this:
76
77![:O](1*VsEzd8PSYMIUwjBLNFFetA.png)*:O*
78
79Oooh yes. `cat`ting the file, we see:
80
81```
82› cat 1/Hidden.txt
83
84######################################### Hints :) ########################################
85
86---telnet server on esp
87
88--Hunt the key to get MQTT creds
89 --
90--MQTT box
91
92--Publish the correct message to get ^FLAG^
93
94<<<<<<<<<<<<<<<< PUBLISH..... DISPLAY.... SUBMIT.... :) >>>>>>>>>>>>>>>>>>>>>>
95```
96
97
98Looking inside the directory named `2`, we see another dir `3` containing a JPEG image and a file telling us about steganography.
99
100![](1*68k1Y6IoK0XTCPTQRn_0fw.png)
101
102And the final directory `4` had nothing in it but a file with the string `flag`. Probably to show up as a false positive in the `strings` output of the flash dump.
103
104### Connecting to “Device-6”
105
106The first file we came across, containing the hints, mentioned a `telnet` 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.
107On doing so, we’re prompted with a captive portal:
108
109![](1*XelmAgITUw-9aZc26meUDQ.png)
110
111A 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.
112
113![](1*oQcTNKOFGphPbX50K2pmlg.png)
114
115Once 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!
116
117![That’s so sad. Alexa play Despacito.](1*lzKOxEkzJqo8TNI4WckmOg.png)*That’s so sad. Alexa play Despacito.*
118
119### The telnet server
120
121Once our host machine and the ESP are on the same network, we can `nmap` our subnet to find our ESP’s IP.
122
123![nmap scan report](1*lPNqoIFmNfxfabdt4sqYSQ.png)*nmap scan report*
124
125We see an `http` server running, which was obviously the captive portal, and our `telnet` server on port 23.
126
127```
128› telnet 192.168.43.223
129Trying 192.168.43.223...
130Connected to 192.168.43.223.
131Escape character is '^]'.
132Press Enter & sumbit your key :)
133somekey
134Wrong Key!!!
135```
136
137
138On connecting, we see a prompt asking for a key. And no, ‘sumbit’ was spelt that way ;)
139
140Where 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.
141
142Although you won’t need it, I downloaded this Docker image for cracking stego — [stego-toolkit](https://hub.docker.com/r/dominicbreuker/stego-toolkit/). We then tossed the image under a bunch of steganography detection and breaking tools, but to no avail.
143
144After a good while `steghide` gave us something:
145
146```
147› steghide extract -sf 10071856.jpg
148Enter passphrase:
149```
150
151
152This took *really* long for us to figure but the password was the name of the image file itself. Urgh. On entering the password, we get a `keys.txt` file. Here’s what it looked like:
153
154```
155So you guessed the password i think...
156
157Nice!!!
158
159Key is somewhere hidden in this strings ...
160
161XH}<
162TJJ*
163Y#pU
164<g?/N
165gr[i}5
166>+h1
167...snip...
168jlW8B
169yjbm
170M4%'
171tx;ZzL
1723 k]
173wPUf'rc
174)Pz#
1750AwN\
176Lgr:J2
177!H9u
1784bSVy
179(*-C
180nOf2E\
181
182Aaaaaand key is not guessable ....
183
184WARNING:Manual checking for correct key might take you 2 days to complete the challange!!
185```
186
187
188Nearly 600 lines of gibberish. We guessed that one of these strings had to be they key for our `telnet` session. We tried to automate it, but the `telnet` 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.
189
190![yeet](1*vY84DrSpJU1H4c9pSvoB5Q.png)*yeet*
191
192As 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.
193
194For those out-of-the-loop, [MQTT](https://en.wikipedia.org/wiki/MQTT) 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.
195
196### Capturing the flag
197
198To interact with the MQTT server, we’ll be using the [Mosquitto](https://mosquitto.org) client. We then use the credentials and attempt to “publish” a message:
199
200```
201› mosquitto_pub -h 'm16.cloudmqtt.com' -p 17551 -t 'inTopic/web/test' -u 'hchzbuhr' -P 'Sz4plHnlVnHc' -m '(^.^)'
202```
203
204
205![UwU](1*W_iVf3vDf4UaelycMbvPvw.png)*UwU*
206
207After 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… *dramatic pause* we got what you’d expect.
208
209![We were 10 days late, mind you](1*sO9vDtGgGjejxklF46gTlg.jpeg)*We were 10 days late, mind you*
210
211### Conclusion
212
213This was our first time playing a hardware CTF, and to be honest, there wasn’t *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* letting us keep the ESP :)
214
215That’s it. The end.