Author: canutethegreat

  • Flipper Zero, part 4: USB keystroke injection and how to lock USB down

    Flipper Zero, part 4: USB keystroke injection and how to lock USB down

    Part 4 of 5 in the Flipper Zero series. Part 1: what it actually is and what it can’t do. Part 2: why so many office badges are weaker than they look. Part 3: Sub-GHz remotes, rolling codes and how gates should be built.

    A keyboard is the most trusted thing you can plug into a computer. There’s no driver prompt and no consent dialog, and the operating system has no way to ask whether whatever is typing is a person. Keystroke injection exploits exactly that. Anything that enumerates as a USB keyboard can type at machine speed into whichever window has focus.

    Same rules as the earlier parts: my own device, a machine I own, a defender’s goal. Typing into a computer you aren’t authorized to test is unauthorized access in most places, whatever the script says.

    What the Flipper does here

    The Bad USB app turns the Flipper into a USB keyboard and plays a script from the SD card. The script format is DuckyScript style, named after the Hak5 USB Rubber Ducky, which made the idea popular. Here’s my Flipper’s main menu with Bad USB highlighted.

    Flipper Zero main menu photo with the Bad USB entry highlighted

    A harmless script looks like this:

    REM types one line into the focused window and nothing else
    DELAY 1000
    STRING hello from a keyboard that isn't one
    ENTER

    Each STRING becomes a series of HID key presses, translated through a keyboard layout. Keys are sent as positions rather than characters, so a script written for a US layout types the wrong characters on a machine set to another layout, unless you pick the matching one.

    The Flipper isn’t special. A Rubber Ducky, a Raspberry Pi Pico, a Digispark-style board or a phone in gadget mode does the same job. In 2014, Karsten Nohl and Jakob Lell showed at Black Hat that the firmware in common USB controller chips could be rewritten so an ordinary thumb drive presented itself as a keyboard. They called it BadUSB. In January 2022 the FBI warned that the FIN7 group was mailing parcels to US businesses containing USB devices that acted as keyboards. So this happens outside conference talks too.

    Why the host goes along with it

    A USB device describes itself with descriptors. A keyboard claims interface class 0x03 (HID), subclass 1 (boot interface), protocol 1 (keyboard), and the host loads its built-in HID driver. Nothing authenticates the device. The vendor ID, product ID and serial number are all self-reported, so anyone who has seen the IDs of your approved keyboard can claim the same ones.

    I think that’s the weak spot in most USB policies. An allow list keyed on VID:PID stops the casual attacker and the unmodified gadget, and does very little against someone who prepared for your site. I rank controls that don’t depend on device identity higher.

    What the attack needs

    It needs an unlocked session. Keystrokes arriving at a lock screen go to the password field, not to a shell. It then needs a way to run something, usually the Run dialog or a terminal, followed by a command. The payload runs as the logged-in user.

    That gives three places to cut: refuse the device, make sure the session is locked, or make a freshly opened shell unable to do anything useful.

    The flowchart shows those cuts in order. I drew it. It isn’t a capture from any tool.

    Flowchart: USB policy, session lock and application control each stop a keystroke injection before the payload runs

    Refusing the device on Linux

    The kernel authorizes new USB devices by default. You can flip that for a bus so nothing is trusted until something approves it:

    echo 0 | sudo tee /sys/bus/usb/devices/usb1/authorized_default

    USBGuard is the practical way to manage this. usbguard generate-policy writes a rule set from whatever is plugged in right now, so run it only when your known-good devices are attached. After that, tighten it by hand. Two rules from my own template:

    # one specific keyboard, and only as a keyboard
    allow id 046d:c31c with-interface 03:01:01
    # anything claiming to be mass storage and HID at the same time
    reject with-interface all-of { 08:*:* 03:*:* }

    The first ID is an example (a Logitech K120), so substitute your own from usbguard list-devices. The second rule matters more than it looks. Composite devices that present a drive and a keyboard are the classic disguise.

    Windows and macOS

    Windows has Device Installation Restrictions under Computer Configuration, Administrative Templates, System, Device Installation in Group Policy. You can block by device setup class or allow only listed hardware IDs. It has the same identity weakness as above. A blanket block on keyboards will also block your own the next time someone replaces one, so test on a pilot group first.

    On Apple silicon laptops, macOS 13 and later can ask before a new accessory connects (Settings, Privacy & Security, Allow accessories to connect). It’s a prompt for the user. That makes it a decent control for people who read it and a poor one for people who click through everything.

    Making the typing useless

    If the device gets through, the next control is what a shell can do. On Windows, application control with WDAC or AppLocker, and PowerShell in Constrained Language Mode, turn most pasted one-liners into error messages. A screen lock after five minutes or less closes the window for the unattended-desk case. This helps least on developer laptops, since developers legitimately need shells, and those are usually the machines worth attacking.

    Detection

    Windows logs new external devices as event 6416 when the Audit PNP Activity policy is on, with the hardware IDs and device class. On Linux, the kernel log line is New USB device found with idVendor, idProduct and the manufacturer and product strings. A keyboard whose product string reads like a development board stands out. A new HID keyboard on a server or a kiosk is a signal by itself, since nobody types on those.

    Typing speed is the obvious second signal, and I’d be careful with it. Barcode scanners are HID keyboards and send hundreds of characters in a fraction of a second, as do macro keyboards and some password-manager hardware. A rule that fires on speed alone will page you for the warehouse. Pair it with a process event: a shell started from the Run dialog within seconds of a 6416 event for a new keyboard is a much better thing to alert on.

    What I would do first

    On any machine that holds something worth stealing, start with the USB allow list, because it removes the attack before anything else matters. The lock timeout and application control come next, since they hold when the allow list is bypassed. Event 6416 logging costs almost nothing and gives you evidence afterwards. Reception desks and kiosks get physical port blockers, which are cheap and work.

    Part 5 moves from the USB port to the radio side, with the Wi-Fi dev board and what rogue access points look like from a defender’s chair.

    Other parts of the Flipper Zero series: part 1, part 2, part 3, part 4, part 5.

  • Flipper Zero, part 3: Sub-GHz remotes, rolling codes and how gates should be built

    Flipper Zero, part 3: Sub-GHz remotes, rolling codes and how gates should be built

    Part 3 of 5 in the Flipper Zero series. Part 1: what it actually is and what it can’t do. Part 2: why so many office badges are weaker than they look.

    Garage doors, gates, car fobs, doorbells and cheap alarm sensors all talk on a handful of license-free bands. Depending on where you live, that’s mostly 315, 433.92, 868 and 915 MHz. The Flipper Zero’s CC1101 transceiver covers 300 to 348, 387 to 464 and 779 to 928 MHz, with regional limits on where it’s allowed to transmit. That makes it a handy way to look at your own remotes and ask whether the thing guarding your driveway was designed by someone who had ever met an attacker.

    Same rules as the earlier parts: my own device, my own remotes, and a defender’s goal. Transmitting captured codes at a gate or car you don’t own is illegal in most places, and the receiver you’re testing may belong to a landlord or an employer. Get permission first.

    What the Flipper shows you

    The Sub-GHz menu has Read, Read RAW, Saved and Add Manually. Read tries to decode a signal against the protocols it knows and prints the frequency, modulation and bits. Read RAW records the pulse timing without interpreting it.

    Here’s my Flipper’s main menu with Sub-GHz selected, followed by the Sub-GHz submenu.

    Flipper Zero main menu with Sub-GHz highlighted
    Flipper Zero Sub-GHz submenu: Read, Read RAW, Saved, Add Manually

    Press a cheap fixed-code remote near it and the Flipper shows the same bits every time. Press a rolling code remote and the bits change on every press. Seeing that on your own remote takes about ten seconds and teaches more than any slide.

    Fixed codes: nothing to defend

    Old remotes with DIP switches, and the cheapest 433 MHz clones, send the same code on every press. Anyone who hears it once can send it forever. There’s no cryptography to attack, and that’s the whole problem. If your gate still uses one, replace the receiver and remotes. No setting makes a fixed code safe.

    Rolling codes: what they are

    A rolling code remote sends a serial number and a counter, protected so the receiver can check that the counter has moved forward. The best known design is Microchip’s KeeLoq. It uses a 32 bit block cipher with a 64 bit key and 528 rounds. The hopping part of the message carries a 16 bit synchronisation counter, and the serial number goes out in the clear beside it. In the common learning schemes, the per-remote key is derived from the serial number and a manufacturer key.

    That last detail is where the trouble started. Several groups published cryptanalysis of KeeLoq in 2007 and 2008. In 2008, Eisenbarth and colleagues presented “On the Power of Power Analysis in the Real World” at CRYPTO. They used power analysis on the receiver side to extract the manufacturer key. With that key, anyone who hears one transmission from a remote can compute its key from the serial number. That breaks the whole product family, not just one gate.

    Receivers also have to tolerate lost presses. Say you pressed the button in your pocket twice while out of range. The receiver has to accept counter 5 after seeing counter 2, so it accepts any value inside a look-ahead window. Here’s a toy model of that logic. It isn’t KeeLoq. It uses HMAC-SHA256 truncated to 4 bytes, but the receiver behaviour has the same shape:

    import hmac, hashlib
    
    KEY = b"per-remote-secret"
    WINDOW = 16
    
    def code(counter):
        msg = counter.to_bytes(4, "big")
        return hmac.new(KEY, msg, hashlib.sha256).digest()[:4].hex()
    
    class CounterReceiver:
        def __init__(self):
            self.last = 0
        def press(self, c):
            for n in range(self.last + 1, self.last + 1 + WINDOW):
                if hmac.compare_digest(code(n), c):
                    self.last = n
                    return f"OPEN (counter {n})"
            return "rejected"

    Jam and capture

    Samy Kamkar showed the practical weakness at DEF CON 23 in 2015 with a device he called RollJam. The attacker jams the receiver and records the owner’s press at the same time. The owner presses again. The attacker jams and records that press too, but this time releases the earlier code to the receiver, so the door opens and the owner has no reason to think anything happened. The attacker now holds a valid code the receiver has never seen.

    The diagram compares the two receiver designs.

    Sequence diagram comparing a counter-only receiver, where jammed and recorded codes open the gate twice, with a challenge-response receiver, where a recorded answer fails against a fresh nonce. Annotation boxes added for explanation.

    The core problem is that a counter-only receiver has no idea when a code was made. A code from yesterday is as good as one from a second ago, as long as the counter hasn’t passed it. I ran the toy model through the scenario and the output is below. My script generates the jammed presses, so this simulates the logic and doesn’t capture a real remote.

    Terminal output of the toy rolling code receiver showing a replay rejected, a jam-and-capture attack succeeding twice, and a challenge-response receiver rejecting a captured answer

    Look at the third line of the middle block. The receiver accepts the stored second code long after the owner walked off. The stored code only works once, since replaying it afterwards fails, but one use is enough to open a gate.

    Counter windows have caused real vulnerabilities too. CVE-2021-46145 covers the Honda Civic 2012 key fob, where the rolling code doesn’t expire and the counter resynchronisation can be abused to replay unlock commands. CVE-2022-27254 is a worse case on the 2018 Civic, where the remote sends the same signal for every unlock request, so it was never rolling at all.

    How a gate should be built

    If I were specifying a new gate or garage opener, I’d require four things.

    First, the receiver should reject codes older than a few seconds, or use challenge-response. In the last block of the demo, the receiver sends a random nonce and the remote answers with an HMAC of it. A captured answer fails against the next nonce, so storing it gains nothing. This needs a two way radio, which costs a little more, and that’s the price of getting rid of the stored-code attack.

    Second, use a modern authenticated protocol with a per-device key that isn’t derived from a shared manufacturer secret. AES-based systems exist. Ask the installer what the remote actually uses. If the answer is “rolling code” with no name attached, treat it as a fixed code until proven otherwise.

    Third, keep the radio the weaker half of the system. A gate that opens on a single radio message shouldn’t also be the only barrier to anything you care about. Put cameras on it, put a lock on the inner door, and don’t rely on it to separate a vehicle from the street.

    Fourth, plan to replace the remotes. Lost ones should be removed from the receiver, not just forgotten, so choose a receiver that lets you delete individual remotes.

    Testing your own gear

    With the Flipper you can check three things on your own remote. Does Read decode it as a known protocol, or do you get raw pulses only? Do the bits change between presses? If they don’t, you have a fixed code and a replacement project. And can you find the manufacturer and model on the receiver, so you can read its actual security claims instead of guessing?

    I wouldn’t go further than that with equipment I don’t control.

    Part 4 moves to USB, keystroke injection from the Bad USB menu, and how to lock endpoints down.

    Other parts of the Flipper Zero series: part 1, part 2, part 3, part 4, part 5.

  • Hardware hacking, part 1: updating the firmware on a Pinecil V2

    Hardware hacking, part 1: updating the firmware on a Pinecil V2

    Part 1 of 7 in the Hardware hacking series.

    I do most of my soldering and rework with a Pine64 Pinecil V2, and it is the first tool in this series because it is the one I touch every time I open a device. This series is about the bench gear I actually own: the Pinecil, an FTDI serial cable, a cheap logic analyzer, an XGecu T48 programmer and a probe frame for chips I don’t want to desolder. Everything here is done on my own hardware, from the defender’s side. If you understand how a device looks from the inside, you know what an attacker with ten minutes and a screwdriver can learn about it.

    Firmware first, then. A soldering iron that talks USB-C power delivery and Bluetooth is a small computer, and it deserves updates like any other. The V2 runs IronOS, the open source firmware by Ralim, on a Bouffalo BL706 chip.

    Which release and which file

    As I write this (October 2026) the latest stable IronOS release is v2.23, published on 31 August 2025. Its changelog has a few items I care about: Bluetooth LE is now off by default on the V2, the default USB-PD negotiation uses a more conservative profile, and there are fixes for PD supplies that caused reboots with some laptops. Bluetooth support for the V2 first arrived in v2.21, so if you are on v2.21 or v2.22 this update switches it off by default, and anything older has no Bluetooth to switch off.

    The release page lists assets for every supported iron. For the V2 you want Pinecilv2.zip. The similarly named Pinecil.zip is for the original V1, which uses a different chip and a different file format.

    The Assets list of the IronOS v2.23 release on GitHub, with Pinecilv2.zip boxed in red and Pinecil.zip boxed in orange. Red annotation labels were added to the screenshot.

    That screenshot is the real GitHub page, with the two boxes and labels added by me. GitHub also shows a SHA-256 digest for each asset. After downloading I compared it with the local file:

    $ sha256sum Pinecilv2.zip
    fe75a2e3227ba53b7fdd8b0d8a362fc2ff0af264ce442ba76e26f21a3abb7503  Pinecilv2.zip

    That matches the full SHA-256 digest GitHub’s API reports for the asset (the release page truncates it, so compare all 64 characters, not just the start). It only proves the file I got is the file GitHub is serving, which is still worth ten seconds. Inside the zip there is one .bin per language. I use Pinecilv2_EN.bin, 194,508 bytes in this release. Ignore the .dfu and .hex files for a normal update. Pine64’s wiki is explicit that flashing a .dfu as firmware leaves the iron unable to boot.

    Putting the iron in flash mode

    This is the one step you can’t do from software. First a safety note: let the tip cool completely and park the iron in its stand before you unplug it or start fiddling with cables. If it boots normally instead of staying black, it is a live iron on a USB supply, so unplug it straight away rather than pressing buttons. Now unplug everything, hold the minus button, and only then plug the USB-C cable into the iron. Pine64’s wiki says to keep holding for around 10 to 15 seconds after plugging in. If the screen stays black, you are in flash mode. If it boots normally, unplug and try again, because the order matters.

    Screenshot of the Flash Mode section of the Pine64 wiki page on Pinecil firmware, including the warning not to use the DC barrel jack while updating.

    The warning at the top of that section matters. Pine64’s wiki says that using the DC barrel jack during a firmware update may destroy both your computer and the Pinecil, and that the iron is designed to use only one power port at a time. So for the whole update only the USB-C cable goes in: no barrel jack, no second supply, nothing on the other rear port.

    I have not photographed the iron’s own screen and buttons for this post yet, so the black-screen state and the on-device menus are described here rather than shown. In flash mode the iron does not appear as a USB drive. According to the wiki it shows up on Linux as a ttyACM serial device, and Windows lists it as a COM port.

    Flashing with blisp

    blisp is Pine64’s command line flasher for Bouffalo chips, and it is what I would reach for first. Version 0.0.5 (August 2025) is the current release and there are prebuilt binaries for Linux (x86_64, aarch64, armv7, riscv64), macOS and Windows. Unzip it, put it next to the .bin and run:

    sudo ./blisp write -c bl70x --reset Pinecilv2_EN.bin

    On Windows it is .\blisp.exe write -c bl70x --reset .\Pinecilv2_EN.bin from an administrator terminal, and the leading .\ matters. I can’t plug the iron into the machine I used for the screenshots, so I ran the real tool with no iron attached, in a throwaway Ubuntu 24.04 container:

    Terminal output in a dark theme showing blisp v0.0.5, the sha256sum of Pinecilv2.zip, the size of Pinecilv2_EN.bin, and the blisp write command ending in Device not found with exit status 3.

    “Device not found” with exit status 3 is what you get when the iron isn’t in flash mode, or when the cable is charge-only. If you see it with the iron attached, check the troubleshooting list below. I haven’t seen the success output because there was no iron on that machine. With --reset, blisp restarts the chip after the write.

    PineFlash, the GUI option

    Pine64’s wiki lists PineFlash as the GUI route. It is a community project by Spagett1, not a Pine64 product, and its latest release is 0.5.5 from April 2024. I ran the Linux AppImage in the same kind of container, under a virtual display:

    The PineFlash 0.5.5 window with the status Soldering Iron Disconnected, Pinecil V2 selected, and an empty version dropdown.

    The layout is simple: pick the iron, pick a version, pick a language, press Update. The Connection Guide section repeats the hold-minus instruction with a diagram. The problem showed up in the log panel:

    PineFlash log panel showing the line Invalid json downloaded, could not fetch versions, highlighted with a red box and an annotation label.

    On my run the version list never filled in, and the only entry in the dropdown was “Custom”. My network could reach GitHub fine, so it is probably not a plain connectivity problem, although I haven’t ruled out something like API rate limiting. I suspect the app is tripping on the newer release layout (v2.23 ships a metadata.zip asset), but I haven’t confirmed that. In practice, with PineFlash 0.5.5 you may need to download the .bin yourself and use the custom file option, at which point blisp is less work. I haven’t tested a real flash with PineFlash, so I won’t claim it works end to end.

    Checking the version and getting back out

    After the flash, unplug the iron and plug it in normally. Pine64’s wiki says to hold the minus button for a couple of seconds on the home screen to see the firmware version, and it should read v2.23. I would rather show you my own screen than paraphrase a wiki, so a photo of it will go in here later.

    Pine64’s wiki says the Pinecil is very hard to brick with a firmware update, and that re-flashing usually fixes a bad one: enter flash mode again and write a known good file. The flash-mode bootloader sits in the chip’s ROM, which is why that route is always available. To roll back, download the older release from the same GitHub page, take its Pinecilv2_EN.bin and run the same command. If something goes wrong, work through these in order:

    • Hold the minus button before the cable goes in, and keep holding for 10 seconds or more.
    • Try another cable. Some USB-A to USB-C cables carry power only.
    • Try another port, or another computer. Some laptop USB-C ports negotiate badly, and a plain USB-A port often works.
    • On Windows, name the port with -p COM3 (or whatever Device Manager shows). On macOS, allow the new device when prompted, then replug in flash mode.
    • Make sure you used the .bin, not the .dfu.

    Next is the rest of the bench: the tools I use to open up devices, and why each one earns its place.

    Other parts of the Hardware hacking series: 1, updating the firmware on a Pinecil V2; 2, the bench I work on; 3, using my 5V FTDI cable on 3.3V UARTs; 4, finding the UART console on a router I own; 5, reading SPI flash without wrecking it; 6, decoding I2C and SPI with sigrok; 7, JTAG and SWD basics.