An ESP32-based USB HID keystroke-injection tool built to study how BadUSB-class devices operate. A learning-focused security prototype, with no weaponized payloads.
Custom ESP32-S3 HID-injection tool in a USB flash-drive form factor. Runs a DuckyScript-compatible payload interpreter directly from a MicroSD card, with keyboard + mouse injection, five keyboard layouts, sequential multi-stage execution, and a browser based OTA firmware updater, all in a single-.ino sketch built around a heap-fragmentation-free, industrial-grade hot path.
The firmware is engineered to reach the same effective per-keystroke throughput class as commercial injectors (Hak5 Rubber Ducky / Flipper Zero BadUSB), while still reading uncompiled .txt payloads from the SD card on the fly, no .bin pre-compilation step required.
| Capability | Detail |
|---|---|
| USB enumeration | Native composite HID (keyboard + mouse) via ESP32-S3 TinyUSB stack, no host-side driver install |
| Payload source | Uncompiled .txt DuckyScript files, read live from MicroSD, no pre-compilation |
| Multi-stage execution | Sequential run of every N.txt file in card root, sorted numerically |
| Keyboard layouts | 5 layouts: EN, TR, FR, DE, JP, switchable mid-payload with LAYOUT |
| Character resolution | O(1) 256-entry lookup tables per layout + tiny sorted overflow table for code points > 255 |
| I/O strategy | 512-byte chunked SD reads into a fixed RAM buffer; zero character-by-character file reads |
| Memory model | Zero use of the Arduino String class. All parsing on char[] buffers with strtok_r/strtol/strchr. No heap allocations after setup() |
| HID timing | delayMicroseconds()-based press/release intervals (default 1 ms), an order of magnitude tighter than a naive Arduino sketch |
| OTA updates | Browser based upload page over a self-hosted WiFi Access Point, gated by a boot-time trigger |
| Status feedback | single LED signaling for ready / SD error / no payload / OTA modes |
Here are the physical production results of the custom designed printed circuit board:

| Aspect | Part / Value |
|---|---|
| Microcontroller | ESP32-S3-WROOM-2 (N32R8V recommended, 32 MB flash / 8 MB PSRAM) |
| Power regulation | AP2112K-3.3 (5 V USB → 3.3 V LDO) |
| USB connector | Molex 48037-2200 Type-A |
| SD connector | Hirose DM3AT-SF-PEJM5 (MicroSD, SPI mode) |
| Status LED | GPIO 46 |
| SD SPI bus | HSPI: CS 10, MOSI 11, SCK 12, MISO 13 |
| OTA trigger pin | GPIO 4 (INPUT_PULLUP, hold to GND at boot) |
PCB schematic and layout designed in KiCad. Full Gerbers, BOM/CPL, 3D models and enclosure for independent fabrication live under rubber_ducky_files/.
The firmware is written to industrial-embedded standards, not hobby Arduino conventions.
Zero-allocation hot path. Every text-processing routine (line reading, tokenisation, command dispatch, UTF-8 decoding) operates on stack-resident char[] buffers. The Arduino String class is not linked anywhere in the runtime, eliminating heap fragmentation entirely.
Chunked buffered I/O. SD payloads are read in 512-byte blocks into a LineReader struct that walks a pointer through the buffer, transparently stitching lines across chunk boundaries. File::readStringUntil() is not used.
O(1) character lookup. Every layout's charmap is expanded once at boot (buildCharTables()) into a flat 256-entry array indexed directly by Unicode code point. A single memory dereference (g_table256[layout][cp]) retrieves the HID report (modifier + keycode) for any Latin-1 character. Code points above 255 (TR İığĞşŞ, FR œŒ, the shared €) live in a per-layout overflow table of ≤ 8 entries, serviced by binary search: mathematically O(log n) on a single-digit n, effectively free.
Microsecond HID timing. Press/release spacing runs on delayMicroseconds() (default 1 ms per phase) rather than the millisecond-scale delay() calls a typical sketch uses. The interval sits above the standard 1 ms USB HID polling interrupt so no reports are dropped, but well below anything a human scale delay would impose, putting the effective character rate in the same class as dedicated commercial injectors.
Fixed size stage table. SD root discovery lands into a static int[256] sorted with an insertion sort. No std::vector, no std::sort, no allocator activity.
Format the card as FAT32. The firmware only touches the root directory.
.txt (case-insensitive extension).g_layout to EN, and clears the REPEAT history; stages do not leak state.No payloads are included in this repository. The interpreter executes only user-supplied
.txtscripts.
All commands are case-sensitive and one per line. Blank lines and lines beginning with REM, //, or # are ignored.
| Command | Behavior |
|---|---|
STRING <text> | Type UTF-8 text through the active layout |
STRINGLN <text> | Same as STRING, then press ENTER |
STRING_DELAY <ms> <text> | Type text with an inter-character pause of <ms> milliseconds |
STRINGLN_DELAY <ms> <text> | As above, then ENTER |
| Command | Behavior |
|---|---|
DELAY <ms> | Blocking wait for <ms> milliseconds |
DEFAULT_DELAY <ms> / DEFAULTDELAY <ms> | Insert this delay after every subsequent command |
Combine any of CTRL/CONTROL, SHIFT, ALT, GUI/WINDOWS/COMMAND/META, ALTGR/RALT with one named key or single character.
ENTER/RETURN, TAB, SPACE, BACKSPACE/BKSP, DELETE/DEL, ESCAPE/ESC, HOME, END, INSERT, PAGEUP/PAGE_UP, PAGEDOWN/PAGE_DOWN, UP/DOWN/LEFT/RIGHT, CAPSLOCK/CAPS_LOCK, NUMLOCK/NUM_LOCK, SCROLLLOCK/SCROLL_LOCK, PRINTSCREEN/PRTSC, PAUSE/BREAK, MENU/APP, F1 through F24.
| Command | Behavior |
|---|---|
MOUSE_MOVE <dx> <dy> | Relative move; any range, auto-chunked into ±127 HID reports |
MOUSE_CLICK LEFT|RIGHT|MIDDLE | Single click |
MOUSE_PRESS LEFT|RIGHT|MIDDLE | Hold button down (no auto-release) |
MOUSE_RELEASE | Release all mouse buttons |
MOUSE_SCROLL <n> | Vertical wheel scroll, -127..127 |
| Command | Behavior |
|---|---|
REPEAT <n> | Run the previous non-REPEAT line <n> times |
LAYOUT <code> | Switch active keyboard layout: EN, TR, FR, DE, JP |
Layouts are compile-time embedded and activated at runtime with LAYOUT.