Lost your ESP32? You just gave away your Wi-Fi password

An Arduino sketch that calls WiFi.begin() leaves the network name and password in the ESP32-S3 flash, in plain text. Anyone holding the board can read them over USB in seconds, and the usual ways to “forget the network” do not remove them.

In short

  • WiFi.begin(ssid, pass) writes the SSID and password to the NVS partition in plain text. Reading those 20 KB with esptool takes 3.7 s. A password written in the sketch is also in the firmware.
  • Uploading another sketch does not touch NVS. WiFi.disconnect(true, true) returns 1, but the password stays as a live entry.
  • The Wi-Fi driver keeps a list of up to 5 networks, so the passwords of earlier networks stay too.
  • WiFi.eraseAP() clears the list, but the old bytes stay in flash, marked as erased. After six networks there were six copies of the oldest password.
  • What removes it: nvs_flash_erase() in your code, or erasing the whole flash (esptool erase-flash, or Erase All Flash Before Sketch Upload: Enabled): 0 copies left.

When a device leaves your hands

This is not about attacking a Wi-Fi network. It is about the device itself ending up with someone else:

Whoever holds the board can read its flash over USB. Everything below is about what they find there.

All network names and passwords on this page are made up: OldRouter-2G / Summer2019!, Pixel-Hotspot / tacos4ever, HomeNet-5G / blue-lemon-42.

One command

A typical sketch: it calls WiFi.begin() with a network name and a password, and nothing else.

#include <WiFi.h>

void setup() {
  WiFi.begin("HomeNet-5G", "blue-lemon-42");
}
void loop() {}

The NVS partition starts at 0x9000 and is 20 KB (0x5000) long in the default partition table. esptool reads it out. It is the same tool Arduino uses to upload sketches, and arduino-esp32 3.3.12 ships it (v5.3.1):

$ esptool --port /dev/ttyACM0 read-flash 0x9000 0x5000 nvs.bin
esptool v5.3.1
Connected to ESP32-S3 on /dev/ttyACM0:
Chip type:          ESP32-S3 (QFN56) (revision v0.2)
...
Read 20480 bytes from 0x00009000 in 1.8 seconds (89.0 kbit/s) to 'nvs.bin'.
Hard resetting via RTS pin...

The data transfer takes 1.8 s. The whole command, including connecting to the chip and loading the flasher stub, took 3.7, 3.7, 3.6, 3.6 and 3.6 s in five runs.

Then list the readable text in the dump. Here is part of the strings output:

$ strings nvs.bin
...
sta.ssid
HomeNet-5G
=Psta.ssid
sta.pswd
*blue-lemon-42
Psta.pswd
...
?sta.apinfo
HomeNet-5G
blue-lemon-42
~[sta.apinfo
sta.apinfo

The network name and the password are there in plain text, twice. In this test the board never connected to anything: there was no access point in any of the runs. The values are written when WiFi.begin() is called, not after a successful connection.

It's in the firmware too

Because the password is written in the sketch as a constant, it is also inside the compiled firmware in the app0 partition. Reading that partition (1.25 MB) takes much longer: 120.7 s for the whole command, 118.9 s of it data transfer. The result is the same:

$ esptool --port /dev/ttyACM0 read-flash 0x10000 0x140000 app.bin
$ strings app.bin | grep -n -B1 -A1 'blue-lemon-42'
9:blue-lemon-42
10-HomeNet-5G

A full dump of all 16 MB of flash (three runs, each with a fresh random password) found the password in exactly three places: two in NVS and one in app0.

PartitionOffsetSizePassword found
nvs0x900020 KByes: 0x99C0, 0x9D84
otadata0xE000no
app00x100001280 KByes: 0x10135 (constant in the sketch)
app10x1500001280 KBno
spiffs0x290000no
coredump0x3F0000no
Default partition table, ESP32-S3 with 16 MB flash, arduino-esp32 3.3.12. Scenario: credentials written in the sketch.

When the password does not come from the sketch but arrives at run time (over Serial in the test, as it would from a setup page), the firmware has no copy. NVS still has the same two.

Why it's there

By default the Wi-Fi driver saves its configuration to flash, so the board can reconnect after a reboot. The ESP-IDF documentation says so in several places:

ESP-IDF: Wi-Fi API and NVS Encryption

esp_wifi_set_storage: “The default value is WIFI_STORAGE_FLASH”.

esp_wifi_set_config: “The configuration will be stored in NVS for station and soft-AP”.

NVS Encryption: “the Wi-Fi driver stores credentials (like SSID and passphrase) in the default NVS partition”.

Arduino keeps that default. In arduino-esp32 3.3.12, WiFiGeneric.cpp starts with persistence on, and only switches the driver to RAM storage when you call WiFi.persistent(false):

// arduino-esp32 3.3.12, libraries/WiFi/src/WiFiGeneric.cpp
bool WiFiGenericClass::_persistent = true;
...
if (!persistent) esp_wifi_set_storage(WIFI_STORAGE_RAM);

NVS can be encrypted, but the prebuilt ESP-IDF libraries in the Arduino core are configured without it. From tools/esp32s3-libs/3.3.12/sdkconfig:

CONFIG_ESP_WIFI_NVS_ENABLED=y
# CONFIG_NVS_ENCRYPTION is not set
# CONFIG_SECURE_FLASH_ENC_ENABLED is not set
# CONFIG_SECURE_BOOT is not set

The NVS documentation is direct about what that means: “If NVS encryption is not used, it is possible for anyone with physical access to the flash chip to alter, erase, or add key-value pairs.”

Where exactly the password sits

Parsing the dump with nvs_tool.py from ESP-IDF shows the keys in the nvs.net80211 namespace:

KeySizeContents
sta.ssid36 Bthe network name
sta.pswd65 Bthe password, plain text
sta.apsw2 Blist size and current slot (see below)
sta.apinfo700 Ba list of saved networks, SSID and password in plain text

This is the sta.apinfo entry from the dump above. Its first slot starts at 0x9D60: 4 bytes of SSID length (10), then 32 bytes of SSID, then 64 bytes of password starting at 0x9D84:

00009d40  02 42 16 00 3f 41 c2 3f 73 74 61 2e 61 70 69 6e  .B..?A.?sta.apin
00009d50  66 6f 00 00 00 00 00 00 a0 02 ff ff bb 2d f8 0b  fo...........-..
00009d60  0a 00 00 00 48 6f 6d 65 4e 65 74 2d 35 47 00 00  ....HomeNet-5G..
00009d70  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00009d80  00 00 00 00 62 6c 75 65 2d 6c 65 6d 6f 6e 2d 34  ....blue-lemon-4
00009d90  32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  2...............

Uploading a new sketch doesn't clear it

An upload from the Arduino IDE writes the new firmware and leaves NVS alone. The board menu has an option for that, Tools → Erase All Flash Before Sketch Upload, and it is Disabled by default:

# arduino-esp32 3.3.12, boards.txt
esp32s3.menu.EraseFlash.none=Disabled
...
esp32s3.menu.EraseFlash.all.upload.erase_cmd=-e

The first menu entry is the default, and arduino-cli board details marks it as Disabled ✔. With the option enabled, platform.txt turns the upload into write-flash -e …, which erases the whole chip first.

The test: set a network, then upload a different sketch that does not use Wi-Fi at all. Both live copies of the password were still in NVS afterwards, in all three runs. The password survives a reflash.

“Forget the network” doesn't

The documented way to make the board forget a network is WiFi.disconnect() with the second argument set:

WiFi.disconnect(true, true);   // wifioff, eraseap
Arduino-ESP32: Wi-Fi API

eraseap: “use true to erase the AP configuration from the NVS memory”.

The call returned 1 (success). Reading the flash again:

Copy of the passwordBeforeAfter disconnect(true, true)
sta.pswdliverewritten with zeros
sta.apinfolivestill live at 0xA284, with the old SSID and password
raw flash2 copies1 live + 2 erased, readable
ESP32-S3, arduino-esp32 3.3.12. Same result in 3 of 3 runs.

The remaining copy is not leftover bytes. It is a valid, live NVS entry. sta.ssid and sta.pswd were zeroed, and the second byte of sta.apsw changed from 00 to 01. The next section explains why.

It also stays live after the board moves on. In another test the board got network 0, then disconnect(true, true), then a reboot, then network 1. Network 0 was still there as 1 live copy plus 3 erased ones.

Five saved networks

The driver does not remember one network. It keeps a list of up to five, in sta.apinfo: 700 bytes = 5 slots × 140 bytes.

Where this comes from

The list is not described in the documentation. It was found by testing on the board and by disassembling the closed-source driver library libnet80211.a from esp32s3-libs/3.3.12 with xtensa-esp32s3-elf-objdump. What follows is our reading of the machine code, not documented behaviour.

When a new station configuration is set, wifi_set_config_process picks a slot in this order:

  1. compare the new configuration with each of the 5 slots (SSID, password, SAE identifier, flags) and reuse the slot that matches;
  2. otherwise take an empty slot (SSID length 0 or 0xFFFFFFFF);
  3. otherwise take the next slot in a ring (counter 0…4), which replaces the oldest network.

Then it writes sta.ssid, sta.pswd, the slot number in sta.apsw and the whole sta.apinfo array to NVS.

That explains disconnect(true, true). In Arduino it calls esp_wifi_set_config() with an all-zero configuration (STAClass::disconnect in STA.cpp). For the driver this is just one more network. It goes into the free slot 1 (hence sta.apsw going from 05 00 to 05 01), and slot 0 with the real password stays where it was.

Three networks, one after another

A board set up for an old router, then a phone hotspot, then the home network, each in a separate boot, the way a device is reconfigured in real life. The strings output of NVS afterwards ends like this:

...
sta.apinfo
OldRouter-2G
Summer2019!
Pixel-Hotspot
tacos4ever
HomeNet-5G
blue-lemon-42
sta.apinfo

All three passwords are in the list, and they stay live after disconnect(true, true). The full sequence from the demonstration run:

StageWhat was doneSummer2019!tacos4everblue-lemon-42
Aerase flash; sketch with HomeNet-5G as a constant——2 live; 1 in app.bin
Berase flash; networks Old → Phone → Home, each in its own boot1 live + 3 erased1 live + 2 erased2 live
Cthen WiFi.disconnect(true, true), returned 11 live + 4 erased1 live + 3 erased1 live + 2 erased
Dthen WiFi.eraseAP(), returned 15 erased4 erased3 erased
Ethen nvs_flash_erase() + nvs_flash_init()——— (strings finds nothing)
Copies of each password in the NVS partition. ESP32-S3, arduino-esp32 3.3.12.

The test runs with random passwords gave the same picture (3 of 3 runs each):

ScenarioStepsResult
3 networksnetworks 0, 1, 2 in turnnetwork 2 (current): 2 live; network 1: 1 live + 2 erased; network 0: 1 live + 3 erased
6 networksnetworks 0…5 in turnnetworks 1–4: 1 live each; network 5 (current): 2 live; network 0 pushed out of the list, but 6 erased copies remain
disconnect, then newnetwork 0 → disconnect(true, true) → reboot → network 1network 0: 1 live + 3 erased; network 1: 2 live
eraseAP, then newnetwork 0 → eraseAP() → reboot → network 1network 0: 2 erased; network 1: 2 live

A device that has moved between networks gives away the passwords of all of them, up to five live and more as erased copies.

Erased, but still readable

There is a call that does clear the list: WiFi.eraseAP(). In arduino-esp32 it calls esp_wifi_restore() (STAClass::erase in STA.cpp), which, per the ESP-IDF documentation, will “Restore WiFi stack persistent settings to default values”, including the esp_wifi_set_config related ones.

bool ok = WiFi.eraseAP();   // returned 1

After it, the live sta.pswd, sta.apsw and sta.apinfo entries are filled with 0xFF. The board itself no longer finds any password: no live copies. But the raw flash still has all of them (stage D above).

Why: NVS is a log

NVS never overwrites a value in place. The documentation describes it:

ESP-IDF: Non-Volatile Storage

“a new key-value pair is added at the end of the log and the old key-value pair is marked as erased”

“Invalidation of old values doesn't require immediate flash erase operations”

Each NVS page has a state bitmap with 2 bits per entry: 10 means written (live), 00 means erased. Marking an entry as erased only changes those bits; the data stays. A page is wiped only when NVS garbage-collects it. So every reconfiguration adds copies instead of replacing them, and the copies stay until their page is wiped.

That is how the six-network test ended up with six erased copies of the oldest password, which was no longer even in the list.

What actually erases it

Every method tested on the ESP32-S3, starting from one network set at run time (two live copies):

What you doPassword in flash?Details
upload another sketchyes, live2 live copies in NVS
WiFi.disconnect(true, true)yes, live1 live + 2 erased
WiFi.eraseAP()yes, erased but readable0 live + 2 erased
WiFi.persistent(false)not in NVS, still in firmwareNVS: 0; app0: 1 (constant in the sketch)
nvs_flash_erase() + nvs_flash_init()no0, not even in the raw dump
esptool erase-flash / Erase All Flash Before Sketch Uploadno0 in NVS and app0
ESP32-S3, arduino-esp32 3.3.12, Arduino IDE default settings. Each scenario: 3 runs from a fully erased chip, all identical.

In your code: nvs_flash_erase()

For a factory reset, erase the whole NVS partition and initialise it again. nvs_flash.h: “Erases all contents of the default NVS partition (one with label "nvs")”.

#include <nvs_flash.h>

void factoryReset() {
  nvs_flash_erase();
  nvs_flash_init();
}

Both calls returned 0 (ESP_OK), and strings on the NVS dump printed nothing. Note that this erases everything else stored in the default NVS partition as well, not only Wi-Fi.

Before the board leaves your hands: erase the whole flash

$ esptool --port /dev/ttyACM0 erase-flash
...
Erasing flash memory (this may take a while)...
Flash memory erased successfully in 2.4 seconds.

$ esptool --port /dev/ttyACM0 read-flash 0x9000 0x5000 nvs.bin
$ strings nvs.bin
$

The whole command took 4.3 s. In the Arduino IDE the same effect comes from Tools → Erase All Flash Before Sketch Upload: Enabled. With it, the next upload left 0 copies in NVS and app0 (3 of 3 runs); without it, the password stayed.

WiFi.persistent(false) helps only half-way

Calling WiFi.persistent(false) before WiFi.begin() switches the driver to RAM storage, and NVS gets no copy. But a password written in the sketch is still in the firmware. Getting the password at run time (for example, from a setup page) keeps it out of the firmware, but then it is saved in NVS, unless persistence is off.

What to do

Flash and NVS encryption

The real fix is to encrypt the flash and NVS. With flash encryption enabled, the ESP-IDF documentation says that “physical readout of flash will not be sufficient to recover most flash contents”. NVS needs its own encryption on top of that: “The nvs partition cannot be encrypted, because the NVS library is not directly compatible with flash encryption”.

It is a one-way step

On the ESP32-S3 the key is written into BLOCK_KEYN eFuses, and “Each eFuse is a one-bit field which can be programmed to 1 after which it cannot be reverted back to 0.”

After that, a normal Arduino IDE upload is plain, unencrypted firmware (write-flash … -z, no --encrypt), and per the documentation “flashing of plaintext data will soft-brick the ESP32-S3. The device will reboot continuously”.

There is no option for it in the Arduino IDE: boards.txt in 3.3.12 has no encryption or secure boot entries, and the Arduino-ESP32 FAQ says changing such sdkconfig options needs ESP-IDF.

This page deliberately does not give step-by-step instructions for enabling encryption: the step cannot be undone, and a mistake can leave the chip unusable. For a hobby device, a separate network is the practical fix.

What was not tested

Test setup and code

BoardESP32-S3 (QFN56) rev v0.2, 16 MB flash, 8 MB PSRAM, ESP-ROM esp32s3-20210327
Corearduino-esp32 3.3.12 (ESP-IDF v5.5.5), board esp32:esp32:esp32s3, Arduino IDE default settings, arduino-cli from Arduino IDE 2.3.8
Uploadnormal arduino-cli upload, Erase All Flash Before Sketch Upload: Disabled (the default) unless stated
Flash readsesptool 4.13.0 for the test runs; esptool v5.3.1 bundled with arduino-esp32 for the demonstration
Partitionsdefault table: nvs 0x9000 (20 KB), otadata 0xE000, app0 0x10000 (1280 KB), app1 0x150000, spiffs 0x290000, coredump 0x3F0000
Methodevery run starts with esptool erase_flash; a new random SSID and password (Pw- + 14 characters) in every run; NVS, app0 and app1 are read back and searched for the password as a byte string
Classificationeach copy in NVS is classified as live or erased from the page's entry state bitmap; checked against nvs_tool.py -d all from ESP-IDF on 13 dumps, full match
Repeats3 runs per scenario; in every scenario all 3 runs matched byte for byte in offsets
Networkno access point; the board never connected

One sketch covers all scenarios; the mode is chosen with a define. Several networks were set by rebooting the board between them and sending a new SSID and password over Serial each time.

wifi_probe.ino (shortened) · full file
#include <WiFi.h>
#include <nvs_flash.h>

#define MODE_CONST            1  // WiFi.begin() with credentials written in the sketch
#define MODE_NOPERSIST        2  // the same, after WiFi.persistent(false)
#define MODE_SERIAL           3  // credentials arrive over Serial, like from a setup page
#define MODE_DISCONNECT_ERASE 4  // WiFi.disconnect(true, true)
#define MODE_ERASEAP          5  // WiFi.eraseAP()
#define MODE_NOWIFI           6  // an unrelated sketch, no Wi-Fi at all
#define MODE_NVSERASE         7  // nvs_flash_erase() + nvs_flash_init()

#define MODE MODE_CONST

void setup() {
  Serial.begin(115200);
  delay(1500);

#if MODE == MODE_CONST
  WiFi.begin("HomeNet-5G", "blue-lemon-42");
  delay(3000);
#elif MODE == MODE_NOPERSIST
  WiFi.persistent(false);
  WiFi.begin("HomeNet-5G", "blue-lemon-42");
  delay(3000);
#elif MODE == MODE_DISCONNECT_ERASE
  WiFi.mode(WIFI_STA);
  delay(500);
  bool ok = WiFi.disconnect(true, true);   // wifioff, eraseap
  Serial.printf("disconnect(true,true)=%d\n", ok);
#elif MODE == MODE_ERASEAP
  WiFi.mode(WIFI_STA);
  delay(500);
  bool ok = WiFi.eraseAP();
  Serial.printf("eraseAP()=%d\n", ok);
#elif MODE == MODE_NVSERASE
  esp_err_t e = nvs_flash_erase();          // no Wi-Fi started
  Serial.printf("nvs_flash_erase()=%d\n", (int)e);
  e = nvs_flash_init();
  Serial.printf("nvs_flash_init()=%d\n", (int)e);
#endif
  // ... MODE_SERIAL (credentials over Serial): see the full file
  Serial.println("=== END ===");
}

void loop() { delay(1000); }

After each run, read the partitions back and search them:

esptool --port /dev/ttyACM0 read-flash 0x9000 0x5000 nvs.bin        # NVS, about 4 s
esptool --port /dev/ttyACM0 read-flash 0x10000 0x140000 app.bin     # app0, about 2 min
strings nvs.bin

To see which copies are live and which are erased, parse the dump with nvs_tool.py -d all from ESP-IDF.

Use made-up credentials for tests like this: a password written in the sketch ends up in the firmware, and in any board you later hand over.

Sources

  1. ESP-IDF v5.5: Non-Volatile Storage Library (ESP32-S3) “a new key-value pair is added at the end of the log and the old key-value pair is marked as erased” · “Invalidation of old values doesn't require immediate flash erase operations” · “If NVS encryption is not used, it is possible for anyone with physical access to the flash chip to alter, erase, or add key-value pairs.”
  2. ESP-IDF v5.5: NVS Encryption (ESP32-S3) “the Wi-Fi driver stores credentials (like SSID and passphrase) in the default NVS partition”
  3. ESP-IDF v5.5: Wi-Fi API esp_wifi_set_storage: “The default value is WIFI_STORAGE_FLASH” · esp_wifi_set_config: “The configuration will be stored in NVS for station and soft-AP” · esp_wifi_restore: “Restore WiFi stack persistent settings to default values”
  4. ESP-IDF v5.5: Flash Encryption (ESP32-S3) “physical readout of flash will not be sufficient to recover most flash contents” · “writes it into respectively one or two BLOCK_KEYN eFuses” · “The nvs partition cannot be encrypted, because the NVS library is not directly compatible with flash encryption” · “flashing of plaintext data will soft-brick the ESP32-S3. The device will reboot continuously”
  5. ESP-IDF v5.5: eFuse Manager (ESP32-S3) “Each eFuse is a one-bit field which can be programmed to 1 after which it cannot be reverted back to 0.”
  6. Arduino-ESP32: Wi-Fi API disconnect(wifioff, eraseap): “use true to erase the AP configuration from the NVS memory”
  7. Arduino-ESP32 FAQ: How to modify an sdkconfig option in Arduino? “this feature is not available in Arduino IDE” · “you need to use ESP-IDF to compile Arduino libraries”
  8. arduino-esp32 3.3.12 source WiFiGeneric.cpp: _persistent = true, esp_wifi_set_storage(WIFI_STORAGE_RAM) when off · STA.cpp: disconnect sets an all-zero wifi_config_t, erase calls esp_wifi_restore() · boards.txt: EraseFlash.none=Disabled · platform.txt: write-flash {upload.erase_cmd} -z … · tools/esp32s3-libs/3.3.12/sdkconfig: # CONFIG_NVS_ENCRYPTION is not set
  9. ESP-IDF v5.5.6 source: components/nvs_flash/include/nvs_flash.h nvs_flash_erase: “Erases all contents of the default NVS partition (one with label "nvs")”
  10. Disassembly of libnet80211.a from esp32s3-libs/3.3.12 (our analysis, not documentation) wifi_nvs_cfg_init, wifi_nvs_validate_ap_num, wifi_set_config_process: sta.apinfo = 5 × 140 bytes, slot chosen by match → empty → ring

Video

A short video on the same topic is on the ESPSSS YouTube channel (4:55, 2026-11-21). This page goes into more detail.

Also as a YouTube Short.