treesummaryrefslogcommitdiff
path: root/pres.md
blob: d761b73b2c11570f8e7ecb0b17fcf30c4d3a5116 (preview) (code) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
---
title: Ouroboros Flash Reader
author: Patrick Schönberger
date: 16.07.2025
transition: none
revealjs-url: https://cdn.jsdelivr.net/npm/reveal.js@5.2.1
slideNumber: true
navigationMode: linear
---

## Structure
<!-- structure: start with a problem and reproduce the work leading to the working solution -->


- Start with the problem
- Work our way to the solution step by step

::: notes

This is my note.

- It can contain Markdown
- like this listt

:::



<!-- access smart home hw on crime scenes -->
<!-- many shelly devices are based on esp32/esp8266 -->
<!-- they dont contain any usable data but contain identifiable user data -->
<!-- this can be used to inquire about the user account -->
<!-- so we need to extract the content of the esp's flash memory -->
<!-- we then also need to extract filesystems from the memory -->
<!-- do it read-only, verifiably -->
<!-- existing solutions (esptool, mos) can also write and erase memory -->
<!-- they are also complex, making understanding and changing the code time consuming -->
<!-- what about writing a custom extraction tool? -->

## The Problem

How to access data on Shelly devices?

## 

- Extract the flash memory and read the data
- Do it read-only and document the process

## 

- Most Shelly devices are based on ESP micro controllers
- Existing solutions (esptool, mos) can modify memory
- They are very complex, and thus hard to modify
- What about a custom tool?



<!-- what is the bare minimum needed to talk to the esp? -->
<!-- - two modes: bootmode and runmode -->
<!-- - decided by GPIO0 at start -->
<!-- - the esp as well as the shelly devices expose uart pins (tx/rx) -->
<!-- - in runmode they output logging information -->
<!-- - in bootmode they listen to a custom serial protocol -->
<!-- so we need a serial connection and the ability to enter boot mode! -->
<!-- what can the serial protocol do? -->
<!-- - sync -->
<!-- - write ram/flash/registers -->
<!-- - configuration etc. -->
<!-- - on the esp32 it can read flash, but not on esp8266, esp32c3, esp32c6 -->

## Talking to the ESP

- What is the bare minimum needed to talk to an ESP?
- Two modes: boot and run
- Serial pins are exposed on some Shelly devices and accessible on others
- In boot mode they can be used to communicate using a custom protocol
<!-- * So we need a serial connection and the ability to enter boot mode -->

## 

- What can we do with the protocol?
  - Synchronize
  - Write RAM, flash and registers
  - Configure memory etc.
  - On the ESP32 it can read flash, but not on the ESP8266, ESP32C3 or ESP32C6
- How do other tools read flash?



<!-- how do other tools read flash? -->
<!-- - we cant directly read flash, but we can write ram -->
<!-- - write a program, load it into ram, run it and then talk to it instead -->
<!-- - flash loader/stub -->
<!-- - esptool uses two different variants, c based and rust based -->
<!-- - the c based one is older and getting replaced, but it is also dramatically simpler and also supports the esp8266 -->
<!-- - so we use the c based one and customize it (remove write and erase flash commands) -->
<!-- technically this means we do have write access until the flash loader is activated -->
<!-- the extraction tool is also small and runs a fixed number of commands -->
<!-- -> as sure as we can be -->

## Reading Flash Memory

- We cannot directly read flash, but we can write RAM
- Write a program for the ESP, load it into RAM and run
- This is called flash loader or flasher stub
- Esptool uses two versions:
  - Legacy C version
  - New Rust version

## 

- C version is deprecated but it is much simpler and supports ESP8266
- Customize this version by removing write and erase commands
- Technically there is write access until the flash loader is activated
- The extraction tool is small and contains no write or erase commands

## 

```txt
$ cloc esp-flasher-stub/
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Rust                 12         327          78        1863
Logos                14          32           0         249
YAML                  3          34          12         214
Markdown              1          34           0          89
TOML                  4           8           2          61
-----------------------------------------------------------
SUM:                 34         435          92        2476
-----------------------------------------------------------
```

## 

```txt
$ cloc esp-hal
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Rust                492       18739       26120      115809
Linker Script        51         499        1404       11315
Markdown             66        2014          12        5262
TOML                 43         505         412        4481
Logos                44         229          15        1105
YAML                 13         151          67        1054
Jinja Template        3          52           0         255
JSON                  2           0           0          48
CSV                   3           0           0          21
SVG                   1           0           0           4
-----------------------------------------------------------
SUM:                718       22189       28030      139354
-----------------------------------------------------------
```

## 

```txt
$ cloc esptool-legacy-flasher-stub/
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Linker Script        32         787        1188       18751
C                     6        1284         636        7689
C/C++ Header          8         463         557        1687
make                  1          36          33         130
YAML                  4          19           0         114
Python                2          25          21          80
Markdown              3          58           0          76
Bourne Shell          2           9           8          23
TOML                  1           1           0          20
Jinja Template        1           4           2          14
-----------------------------------------------------------
SUM:                 60        2686        2445       28584
-----------------------------------------------------------
```



<!-- how does the serial protocol work? -->
<!-- - data is encoded using SLIP frames -->
<!-- - the host sends a request and the target (esp) sends a response -->
<!-- - steps to read flash: -->
<!--   - sync -->
<!--   - identify chip -->
<!--   - read mac -->
<!--   - (change baud) -->
<!--   - upload stub -->
<!--   - read flash -->

## Serial Protocol

- Bytes are grouped using SLIP frames
- Host sends requests containing commands
- Target sends a matching reply

::: notes
Exceptions:

- after loading flash loader it sends a slip frame on its own
- after sync request 8 replies are sent
- when reading flash using 0xd2 replies are not slip/request frames
:::

## Steps to Read Flash

- Sync
- Identify Chip
- (Read MAC)
- (Change baud rate)
- Upload Stub
- Read Flash

::: notes
Starting from boot mode
sync always at 115200 but esp detects other baud rates from known payload
:::



<!-- modifying the flash loader -->
<!-- - stub consists of 6 .c files: -->
<!--   - miniz.c         // compression -->
<!--   - slip.c          // slip -->
<!--   - stub_commands.c // handle commands -->
<!--   - stub_flasher.c  // main program -->
<!--   - stub_io.c       // serial communication -->
<!--   - stub_write_flash.c // write flash -->
<!-- - so we remove stub_write_flash.c and modify stub_commands.c -->
<!-- - additionally simplify the makefile -->

## Modifying the Flash Loader

- Consists mostly of six C files:
  ```txt
  miniz.c             // compression
  slip.c              // slip
  stub_commands.c     // handle commands (modify)
  stub_flasher.c      // main program
  stub_io.c           // serial communication
  stub_write_flash.c  // write flash (remove)
  ```



<!-- compiling and uploading the flash loader -->
<!-- - download toolchains -->
<!-- - compile the stub using specific toolchains -->
<!-- - this gives us an elf file -->
<!-- - use a python script to extract the .text and .data sections from the elf -->
<!-- - generate a header file and write the raw bytes to a `const unsigned char[]` -->
<!-- - this header gets compiled with the extraction tool (host) -->
<!-- - at runtime, after the chip is identified, upload .text and .data using MEM_ ram commands -->
<!-- - addresses for the sections and for the entry point are in elf file and get written to header alongside the elf sections -->

## Uploading the Flash Loader

- Compile using specific toolchains
- Extract text and data sections from resulting ELF file
- Generate header file that is compiled with the extraction tool
- This includes section's sizes, addresses and the entry point
- Upload the corresponding loader after identifying the chip



<!-- and how do we make the esp enter bootmode? -->
<!-- - wire two gpio pins to RST and GPIO0 -->
<!-- - pull both low -->
<!--   - RST low turns the esp off -->
<!--   - GPIO0 has to be low when it is turned back on -->
<!-- - pull RST high to turn it on -->
<!-- - pull GPIO0 high after the esp has started -->

## Boot Mode

- GPIO0 low at start
- Wire pins to RST and GPIO0
- Pull both low
- Then pull RST high first and GPIO0 after
- Supported when using Raspberry Pi or ESP as host

::: notes
For development also possible via USB on linux hosts
:::



<!-- overview: -->
<!-- - bootmode/serial -->
<!-- - serial protocol -->
<!-- - flash loader -->

## Summary

- Connect to serial and set to boot mode
- Use serial protocol to identify the chip
- Upload modified flash loader
- Extract

::: notes
what is left:

- differences between esp versions
- how to extract files
- what is on the files?
:::



<!-- differences between esp versions -->
<!-- - identification: -->
<!--   - ESP32-C3 and later use GET_SECURITY_INFO which contains a chip_id -->
<!--   - previous models have a register with a magic value identifying the chip -->
<!-- - mac address: -->
<!--   - different registers -->
<!--   - esp8266 mac has to be calculated -->
<!-- - different flash loader versions -->
<!--   - esp8266 has no data section -->

## Different ESP Versions

- ESP32-C3 and later are identified using `GET_SECURITY_INFO` command
- Earlier versions have a register with a magic value
- Try security info first and reset on failure
- Different registers containing MAC address

::: notes
- sec info first bc it fails with unknown command
- read register is a known command
- reset -> bootmode -> sync again
- on ESP8266 mac has to be calculated
- esp8266 has no data section when compiling flash loader
:::



<!-- extracting the file system -->
<!-- - esp8266 -->
<!-- - esp32 -->

## Extracting File Systems

- SPIFFS/Littlefs
- Gen 1 devices have known layout
- On newer devices file system is found using metadata
- Files can be extracted using specialized tools



<!-- interesting files -->
<!-- - wifi credentials -->
<!-- - certificates -->
<!-- - jwt token -->

#### Shelly Plus 1 File System

```txt
api_math.js
ca.pem                // signing CA
conf0.json            // empty configuration
conf3.json            // initial configuration
conf9.json            // current configuration
index.html.gz         // web frontend
init.js
rpc_acl_auth.json     // list of rpc endpoints
rpc_acl_no_auth.json
shelly_cloud.pem      // Shelly Cloud certificate
shelly_plugin_api.js
storage.json          // user data
tzinfo
updater.dat
```

#### `conf9.json`

```txt
{
  "wifi": {
    "sta": {
      "enable": true,
      "ssid": "SSID",
      "pass": "PASSWORD"
      "last_bssid": "aa:17:5d:15:ae:03",
    },
  },
  "shelly": {
    "cloud": {
      "enable": true,
      "server": "192.168.112.231:6022/jrpc",
      "token": "eyJhbGciOiJIUzI1NiIsInR..."
    },
  },
  ...
}
```

#### `shelly_cloud.pem`

```txt
Subject: O = Allterco
Not Before: Aug  4 12:03:41 2020 GMT
Not After : Aug  2 12:03:41 2030 GMT
-----BEGIN CERTIFICATE-----
MIICrTCCAZUCFCuIEEAQJOFLZuEtr/CWkvxi9YPAMA0GCSqGSIb3DQEBCwUAMBMx
ETAPBgNVBAoMCEFsbHRlcmNvMB4XDTIwMDgwNDEyMDM0MVoXDTMwMDgwMjEyMDM0
MVowEzERMA8GA1UECgwIQWxsdGVyY28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQDHGDBDHpPUbtC9QAAjX3bi487AVY5JgYB2gyp6R9cjdsNGMbYnWdxn
BmsIUKJPg7B5NcQObiMe6djUvwo0c2Xl9L+P9LOskP2WNDdpquX3XJu580hXHHVB
mwOgJ0fi+5U9mOFHhc1gYGLmhO9oqsE80SgpmsPQHloMIqmcaolLzgC9PWGu8nSD
ToJq+dXyNFHzLVyBEugHQpeIR8Fq0do4dtlsfTWvv9U+fpGPegjdkPenSxGrOVwd
syFzNahxQGKmpZE/1fsq5QSh9+ZgwpdDChVNpkj9TBC1ApDTUasNco/6Meb/0Xur
pxpWPNfkIpZ7ebtGHVd/ZkGTPUnL7FXHAgMBAAEwDQYJKoZIhvcNAQELBQADggEB
ADwbvD7Mf7SOinV8JkOue8D/tvp+OiYTYLHYppzCLcBK3D1kQ7aqla2T8ebEFbLh
hpau7MxJcizVWZs4vJvFYxEwBTQldobhmG5lHnoKQcOSxis1kWr5xdKhji/QYo6T
wS/cird9hAcuc+RmLCMdpEDyia/vX+vvvRdyKsmB7A6Vkdu8s2B2jlhQNkitYzvq
UDKogJrWe6fQUTpTThMyGbqhp9cQ64M4DJG1cwSBZ/hiUAMKO/y5WVNWFbXIb/Om
xwkXgof2RXN1AjjqMcBh3GNVK4ZV5XM9WCocZjOCi2yZxaxaRWyuGR7EqAQZ+wdr
P5XrzYspeVl1WtBzwGFssPc=
-----END CERTIFICATE-----
```

## Thank You

title: Ouroboros Flash Reader author: Patrick Schönberger date: 16.07.2025 transition: none revealjs-url: https://cdn.jsdelivr.net/npm/reveal.js@5.2.1 slideNumber: true navigationMode: linear

Structure

  • Start with the problem
  • Work our way to the solution step by step

::: notes

This is my note.

  • It can contain Markdown
  • like this listt

:::

The Problem

How to access data on Shelly devices?

  • Extract the flash memory and read the data
  • Do it read-only and document the process

  • Most Shelly devices are based on ESP micro controllers
  • Existing solutions (esptool, mos) can modify memory
  • They are very complex, and thus hard to modify
  • What about a custom tool?

Talking to the ESP

  • What is the bare minimum needed to talk to an ESP?
  • Two modes: boot and run
  • Serial pins are exposed on some Shelly devices and accessible on others
  • In boot mode they can be used to communicate using a custom protocol

  • What can we do with the protocol?
    • Synchronize
    • Write RAM, flash and registers
    • Configure memory etc.
    • On the ESP32 it can read flash, but not on the ESP8266, ESP32C3 or ESP32C6
  • How do other tools read flash?

Reading Flash Memory

  • We cannot directly read flash, but we can write RAM
  • Write a program for the ESP, load it into RAM and run
  • This is called flash loader or flasher stub
  • Esptool uses two versions:
    • Legacy C version
    • New Rust version

  • C version is deprecated but it is much simpler and supports ESP8266
  • Customize this version by removing write and erase commands
  • Technically there is write access until the flash loader is activated
  • The extraction tool is small and contains no write or erase commands

$ cloc esp-flasher-stub/
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Rust                 12         327          78        1863
Logos                14          32           0         249
YAML                  3          34          12         214
Markdown              1          34           0          89
TOML                  4           8           2          61
-----------------------------------------------------------
SUM:                 34         435          92        2476
-----------------------------------------------------------

$ cloc esp-hal
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Rust                492       18739       26120      115809
Linker Script        51         499        1404       11315
Markdown             66        2014          12        5262
TOML                 43         505         412        4481
Logos                44         229          15        1105
YAML                 13         151          67        1054
Jinja Template        3          52           0         255
JSON                  2           0           0          48
CSV                   3           0           0          21
SVG                   1           0           0           4
-----------------------------------------------------------
SUM:                718       22189       28030      139354
-----------------------------------------------------------

$ cloc esptool-legacy-flasher-stub/
-----------------------------------------------------------
Language          files       blank     comment        code
-----------------------------------------------------------
Linker Script        32         787        1188       18751
C                     6        1284         636        7689
C/C++ Header          8         463         557        1687
make                  1          36          33         130
YAML                  4          19           0         114
Python                2          25          21          80
Markdown              3          58           0          76
Bourne Shell          2           9           8          23
TOML                  1           1           0          20
Jinja Template        1           4           2          14
-----------------------------------------------------------
SUM:                 60        2686        2445       28584
-----------------------------------------------------------

Serial Protocol

  • Bytes are grouped using SLIP frames
  • Host sends requests containing commands
  • Target sends a matching reply

::: notes Exceptions:

  • after loading flash loader it sends a slip frame on its own
  • after sync request 8 replies are sent
  • when reading flash using 0xd2 replies are not slip/request frames :::

Steps to Read Flash

  • Sync
  • Identify Chip
  • (Read MAC)
  • (Change baud rate)
  • Upload Stub
  • Read Flash

::: notes Starting from boot mode sync always at 115200 but esp detects other baud rates from known payload :::

Modifying the Flash Loader

  • Consists mostly of six C files:
    miniz.c             // compression
    slip.c              // slip
    stub_commands.c     // handle commands (modify)
    stub_flasher.c      // main program
    stub_io.c           // serial communication
    stub_write_flash.c  // write flash (remove)
    

Uploading the Flash Loader

  • Compile using specific toolchains
  • Extract text and data sections from resulting ELF file
  • Generate header file that is compiled with the extraction tool
  • This includes section's sizes, addresses and the entry point
  • Upload the corresponding loader after identifying the chip

Boot Mode

  • GPIO0 low at start
  • Wire pins to RST and GPIO0
  • Pull both low
  • Then pull RST high first and GPIO0 after
  • Supported when using Raspberry Pi or ESP as host

::: notes For development also possible via USB on linux hosts :::

Summary

  • Connect to serial and set to boot mode
  • Use serial protocol to identify the chip
  • Upload modified flash loader
  • Extract

::: notes what is left:

  • differences between esp versions
  • how to extract files
  • what is on the files? :::

Different ESP Versions

  • ESP32-C3 and later are identified using GET_SECURITY_INFO command
  • Earlier versions have a register with a magic value
  • Try security info first and reset on failure
  • Different registers containing MAC address

::: notes

  • sec info first bc it fails with unknown command
  • read register is a known command
  • reset -> bootmode -> sync again
  • on ESP8266 mac has to be calculated
  • esp8266 has no data section when compiling flash loader :::

Extracting File Systems

  • SPIFFS/Littlefs
  • Gen 1 devices have known layout
  • On newer devices file system is found using metadata
  • Files can be extracted using specialized tools

Shelly Plus 1 File System

api_math.js
ca.pem                // signing CA
conf0.json            // empty configuration
conf3.json            // initial configuration
conf9.json            // current configuration
index.html.gz         // web frontend
init.js
rpc_acl_auth.json     // list of rpc endpoints
rpc_acl_no_auth.json
shelly_cloud.pem      // Shelly Cloud certificate
shelly_plugin_api.js
storage.json          // user data
tzinfo
updater.dat

conf9.json

{
  "wifi": {
    "sta": {
      "enable": true,
      "ssid": "SSID",
      "pass": "PASSWORD"
      "last_bssid": "aa:17:5d:15:ae:03",
    },
  },
  "shelly": {
    "cloud": {
      "enable": true,
      "server": "192.168.112.231:6022/jrpc",
      "token": "eyJhbGciOiJIUzI1NiIsInR..."
    },
  },
  ...
}

shelly_cloud.pem

Subject: O = Allterco
Not Before: Aug  4 12:03:41 2020 GMT
Not After : Aug  2 12:03:41 2030 GMT
-----BEGIN CERTIFICATE-----
MIICrTCCAZUCFCuIEEAQJOFLZuEtr/CWkvxi9YPAMA0GCSqGSIb3DQEBCwUAMBMx
ETAPBgNVBAoMCEFsbHRlcmNvMB4XDTIwMDgwNDEyMDM0MVoXDTMwMDgwMjEyMDM0
MVowEzERMA8GA1UECgwIQWxsdGVyY28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQDHGDBDHpPUbtC9QAAjX3bi487AVY5JgYB2gyp6R9cjdsNGMbYnWdxn
BmsIUKJPg7B5NcQObiMe6djUvwo0c2Xl9L+P9LOskP2WNDdpquX3XJu580hXHHVB
mwOgJ0fi+5U9mOFHhc1gYGLmhO9oqsE80SgpmsPQHloMIqmcaolLzgC9PWGu8nSD
ToJq+dXyNFHzLVyBEugHQpeIR8Fq0do4dtlsfTWvv9U+fpGPegjdkPenSxGrOVwd
syFzNahxQGKmpZE/1fsq5QSh9+ZgwpdDChVNpkj9TBC1ApDTUasNco/6Meb/0Xur
pxpWPNfkIpZ7ebtGHVd/ZkGTPUnL7FXHAgMBAAEwDQYJKoZIhvcNAQELBQADggEB
ADwbvD7Mf7SOinV8JkOue8D/tvp+OiYTYLHYppzCLcBK3D1kQ7aqla2T8ebEFbLh
hpau7MxJcizVWZs4vJvFYxEwBTQldobhmG5lHnoKQcOSxis1kWr5xdKhji/QYo6T
wS/cird9hAcuc+RmLCMdpEDyia/vX+vvvRdyKsmB7A6Vkdu8s2B2jlhQNkitYzvq
UDKogJrWe6fQUTpTThMyGbqhp9cQ64M4DJG1cwSBZ/hiUAMKO/y5WVNWFbXIb/Om
xwkXgof2RXN1AjjqMcBh3GNVK4ZV5XM9WCocZjOCi2yZxaxaRWyuGR7EqAQZ+wdr
P5XrzYspeVl1WtBzwGFssPc=
-----END CERTIFICATE-----

Thank You