]> gitweb.ps.run Git - ouroboros-slides/blob - pres.adoc
14a6a0c4234f73a4bd493a3dc8d0b64050b72417
[ouroboros-slides] / pres.adoc
1 = Ourosboros Flash Reader
2 :revealjsdir: ./reveal.js-5.2.1/
3
4 Patrick Schönberger
5
6 16.07.2025
7
8 == Structure
9
10 - Problem
11 - Solution
12 - Implementation
13
14 == Problem
15
16 == Solution
17
18 == Implementation
19
20 structure: start with a problem and reproduce the work leading to the working solution
21
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?
40 - sync
41 - write ram/flash/registers
42 - configuration etc.
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
47 - flash loader/stub
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:
58   - sync
59   - identify chip
60   - read mac
61   - (change baud)
62   - upload stub
63   - read flash
64 modifying the flash loader
65 - stub consists of 6 .c files:
66   - miniz.c         // compression
67   - slip.c          // slip
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
75 - download toolchains
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
85 - pull both low
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
90 overview:
91 - bootmode/serial
92 - serial protocol
93 - flash loader
94 differences between esp versions
95 - identification:
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
98 - mac address:
99   - different registers
100   - esp8266 mac has to be calculated
101 - different flash loader versions
102   - esp8266 has no data section
103 different hosts:
104 - linux (usb)
105 - rpi (gpio)
106 - esp (gpio)
107 extracting the file system
108 - esp8266
109 - esp32
110 interesting files
111 - wifi credentials
112 - certificates
113 - jwt token
114
115
116 DEMO!
117
118
119 == cloc
120
121 ```sh
122 $ cloc esp-flasher-stub/
123       38 text files.
124       34 unique files.                              
125        5 files ignored.
126
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 -----------------------------------------------------------
131 Rust                 12         327          78        1863
132 Logos                14          32           0         249
133 YAML                  3          34          12         214
134 Markdown              1          34           0          89
135 TOML                  4           8           2          61
136 -----------------------------------------------------------
137 SUM:                 34         435          92        2476
138 -----------------------------------------------------------
139
140 $ cloc esp-hal
141      742 text files.
142      718 unique files.                                          
143       35 files ignored.
144
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
152 TOML                 43         505         412        4481
153 Logos                44         229          15        1105
154 YAML                 13         151          67        1054
155 Jinja Template        3          52           0         255
156 JSON                  2           0           0          48
157 CSV                   3           0           0          21
158 SVG                   1           0           0           4
159 -----------------------------------------------------------
160 SUM:                718       22189       28030      139354
161 -----------------------------------------------------------
162
163 $ cloc esptool-legacy-flasher-stub/
164       63 text files.
165       60 unique files.                              
166        4 files ignored.
167
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
173 C                     6        1284         636        7689
174 C/C++ Header          8         463         557        1687
175 make                  1          36          33         130
176 YAML                  4          19           0         114
177 Python                2          25          21          80
178 Markdown              3          58           0          76
179 Bourne Shell          2           9           8          23
180 TOML                  1           1           0          20
181 Jinja Template        1           4           2          14
182 -----------------------------------------------------------
183 SUM:                 60        2686        2445       28584
184 -----------------------------------------------------------
185 ```