Blocking BadUSB Keystrokes on macOS with Swift
Learn how to block BadUSB attacks on macOS in under 500 milliseconds by building a Swift-based keystroke quarantine guard using CGEventTap.

Stock photo for illustration only, not from the actual event
- OMG Cable can masquerade as a keyboard and execute malicious payloads within 500 milliseconds.
- macOS Ventura accessory alerts are too slow to react against automated USB keystroke injection.
- RoamSwitch combines IOHIDManager and CGEventTap to instantly drop unauthorized keystrokes in user space.
- The utility also automatically unmounts and ejects unverified external USB flash drives.
Witnessing a demonstration of an OMG Cable targeting a Mac highlights the severe risks of physical hardware vulnerabilities. Externally, the charging cord looks identical to a standard braided USB-C cable and charges a smartphone normally. However, the moment it connects to a laptop, an embedded microcontroller registers as a virtual USB keyboard and blasts keystrokes at over 1,000 words per minute.
The entire compromise occurs in under 500 milliseconds without requiring users to double-click downloaded files or interact with browser pop-ups. Victims only notice Spotlight flickering for a split second, Terminal popping up, and a one-line payload downloading malicious scripts via curl.

Stock photo for illustration only, not from the actual event
During the development of RoamSwitch, a lightweight macOS security utility, the creator realized that network security perimeters mean little if attackers can compromise the local shell through physical access. This insight led to the creation of a hardware keystroke-quarantine guard implemented in Swift to drop BadUSB keystrokes before macOS can process them.
Although Apple introduced the accessory connection alert dialog in macOS Ventura, it faces two practical limitations. The operating system treats all inputs from authorized keyboards as intentional user commands, and waiting for human intervention to click block is far too slow against automated attacks.
"Before your brain even registers that a device was plugged in, you see Spotlight flicker for a fraction of a second, Terminal pops up, and a one-line payload is executed."
The effective solution relies on a fail-closed quarantine approach. Upon plugging in a new USB keyboard, an IOHIDManager callback detects unwhitelisted devices, activates blocking status, and uses a CGEventTap callback to return nil for all key events, resulting in an instant drop.
Additional Context: While writing a DriverKit System Extension is Apple's formal method for USB security, it introduces massive friction for indie utilities. Pairing IOHIDManager and CGEventTap entirely within user space provides identical protection with zero reboots, zero custom kernel drivers, and zero special entitlements.
Addressing physical port security requires more than just stopping keyboards; it also involves preventing unauthorized USB flash drives from copying data or dropping binaries. RoamSwitch pairs its keyboard protection with a volume guard that listens to NSWorkspace mount notifications, instantly unmounting untrusted external mass-storage drives before macOS indexes them.
Ultimately, treating newly attached physical HID devices as untrusted by default applies robust zero-trust principles to hardware ports, closing a direct unauthenticated pipeline straight into the user session that traditional network defenses often ignore.
Source: Dev.to
Found something wrong in this article? Report an issue with this article
Comments
Leave a Comment