1 = Ourosboros Flash Reader
2 :revealjsdir: ./reveal.js-5.2.1/
20 structure: start with a problem and reproduce the work leading to the working solution
22 access smart home hw on crime scenes
23 many shelly devices are based on esp32/esp8266
24 they dont contain any usable data but contain identifiable user data
25 this can be used to inquire about the user account
26 so we need to extract the content of the esp's flash memory
27 we then also need to extract filesystems from the memory
28 do it read-only, verifiably
29 existing solutions (esptool, mos) can also write and erase memory
30 they are also complex, making understanding and changing the code time consuming
31 what about writing a custom extraction tool?
32 what is the bare minimum needed to talk to the esp?
33 - two modes: bootmode and runmode
34 - decided by GPIO0 at start
35 - the esp as well as the shelly devices expose uart pins (tx/rx)
36 - in runmode they output logging information
37 - in bootmode they listen to a custom serial protocol
38 so we need a serial connection and the ability to enter boot mode!
39 what can the serial protocol do?
41 - write ram/flash/registers
43 - on the esp32 it can read flash, but not on esp8266, esp32c3, esp32c6
44 how do other tools read flash?
45 - we cant directly read flash, but we can write ram
46 - write a program, load it into ram, run it and then talk to it instead
48 - esptool uses two different variants, c based and rust based
49 - the c based one is older and getting replaced, but it is also dramatically simpler and also supports the esp8266
50 - so we use the c based one and customize it (remove write and erase flash commands)
51 technically this means we do have write access until the flash loader is activated
52 the extraction tool is also small and runs a fixed number of commands
53 -> as sure as we can be
54 how does the serial protocol work?
55 - data is encoded using SLIP frames
56 - the host sends a request and the target (esp) sends a response
57 - steps to read flash:
64 modifying the flash loader
65 - stub consists of 6 .c files:
66 - miniz.c // compression
68 - stub_commands.c // handle commands
69 - stub_flasher.c // main program
70 - stub_io.c // serial communication
71 - stub_write_flash.c // write flash
72 - so we remove stub_write_flash.c and modify stub_commands.c
73 - additionally simplify the makefile
74 compiling and uploading the flash loader
76 - compile the stub using specific toolchains
77 - this gives us an elf file
78 - use a python script to extract the .text and .data sections from the elf
79 - generate a header file and write the raw bytes to a `const unsigned char[]`
80 - this header gets compiled with the extraction tool (host)
81 - at runtime, after the chip is identified, upload .text and .data using MEM_ ram commands
82 - addresses for the sections and for the entry point are in elf file and get written to header alongside the elf sections
83 and how do we make the esp enter bootmode?
84 - wire two gpio pins to RST and GPIO0
86 - RST low turns the esp off
87 - GPIO0 has to be low when it is turned back on
88 - pull RST high to turn it on
89 - pull GPIO0 high after the esp has started
94 differences between esp versions
96 - ESP32-C3 and later use GET_SECURITY_INFO which contains a chip_id
97 - previous models have a register with a magic value identifying the chip
100 - esp8266 mac has to be calculated
101 - different flash loader versions
102 - esp8266 has no data section
107 extracting the file system
122 $ cloc esp-flasher-stub/
127 github.com/AlDanial/cloc v 2.04 T=0.02 s (2259.9 files/s, 199599.0 lines/s)
128 -----------------------------------------------------------
129 Language files blank comment code
130 -----------------------------------------------------------
136 -----------------------------------------------------------
138 -----------------------------------------------------------
145 github.com/AlDanial/cloc v 2.04 T=0.36 s (1978.1 files/s, 522278.6 lines/s)
146 -----------------------------------------------------------
147 Language files blank comment code
148 -----------------------------------------------------------
149 Rust 492 18739 26120 115809
150 Linker Script 51 499 1404 11315
151 Markdown 66 2014 12 5262
155 Jinja Template 3 52 0 255
159 -----------------------------------------------------------
160 SUM: 718 22189 28030 139354
161 -----------------------------------------------------------
163 $ cloc esptool-legacy-flasher-stub/
168 github.com/AlDanial/cloc v 2.04 T=0.06 s (952.9 files/s, 535446.8 lines/s)
169 -----------------------------------------------------------
170 Language files blank comment code
171 -----------------------------------------------------------
172 Linker Script 32 787 1188 18751
174 C/C++ Header 8 463 557 1687
179 Bourne Shell 2 9 8 23
181 Jinja Template 1 4 2 14
182 -----------------------------------------------------------
183 SUM: 60 2686 2445 28584
184 -----------------------------------------------------------