Every operating system trusts a new keyboard the moment it is plugged in, and keystroke injection devices exploit exactly that trust. A device such as the USB Rubber Ducky, or a cable with hidden electronics such as the O.MG Cable, announces itself as a human interface device (HID) and types at machine speed with the signed-in user's rights. Storage controls do not see it, and allowlists built on vendor and product IDs can be fooled by devices that copy those IDs. This guide goes past the general USB material: how malicious HID devices present to the host, how to build HID allowlists by setup class, device ID and interface on Windows, Linux and macOS, what keystroke-timing detection can and cannot catch, and how to investigate a suspected device. It is written for endpoint engineers, SOC analysts and the teams that run privileged and shared workstations. Storage policy, USB drop attacks and the found-device process are covered in the Removable Media and USB Control Guide and the USB Drop Attacks Guide and are not repeated here.
Key takeaways
- The device is a keyboard, not a drive. Keystroke injection maps to ATT&CK T1200 Hardware Additions, and only controls that act on device installation or USB interfaces stop it; removable storage policies never see it.
- VID and PID are claims, not proof. Commercial injection cables are documented as able to spoof USB identifiers, so an allowlist that trusts a keyboard model ID alone can be bypassed; pin high-risk hosts to device instances, ports or descriptor hashes.
- Interface combinations are the strongest tell. A storage device that also exposes a keyboard, or a cable that exposes a keyboard and a network interface, is suspicious by design, and Linux USBGuard can reject such combinations outright.
- Typing-speed detection helps but is not enough. Inter-keystroke timing catches naive injection, yet the Rubber Ducky documentation lists jitter and randomisation features built to look human.
- Least privilege limits the damage. Injected keystrokes run as the signed-in user, so locked screens, no local admin and script controls decide whether an injection becomes an intrusion.
How malicious HID devices work
USB devices describe themselves to the host through descriptors: a vendor ID (VID), a product ID (PID), an optional serial number and one or more interfaces, each with a class, subclass and protocol. The HID interface class is 03; a boot keyboard is 03:01:01 and a boot mouse 03:01:02. Hosts load a keyboard driver for any device that reports a keyboard interface, with no authentication and no prompt on most systems. The device then sends keystrokes like any other keyboard.
| Device type | How it presents | What defenders should know |
|---|---|---|
| Keystroke injection stick (for example the USB Rubber Ducky) | HID keyboard, with optional mass storage; Hak5 documents "HID & Storage attack modes" | Runs scripts in DuckyScript 3.0; the documentation lists jitter and randomisation, which defeats simple typing-speed thresholds |
| Malicious cable (for example the O.MG Cable) | Behaves as a normal USB 2.0 cable while dormant; when triggered injects keystrokes and mouse input | Vendor material lists WiFi triggers, geofencing, self-destruct, USB identifier and MAC address spoofing, and on higher tiers a hardware keylogger and an encrypted network C2 channel |
| Composite device | Storage plus keyboard, or keyboard plus network adapter, in one device | The combination itself is the indicator; legitimate storage rarely needs a keyboard interface |
| Inline keylogger cable or adapter | Sits between a real keyboard and the host | Captures typed credentials (ATT&CK T1056.001 Keylogging); inspect detachable keyboard cables on sensitive hosts |
| Reprogrammed peripheral (BadUSB style) | A genuine-looking device with altered firmware that adds a keyboard interface | Looks like the real product until it enumerates; interface checks and new-device alerts catch it |
Two properties matter more than any product name. First, the device acts with the rights of whoever is signed in, so an unlocked administrator session is the worst case. Second, the follow-on activity is ordinary: a run dialog or terminal, a script interpreter such as PowerShell (T1059.001), and a download. Detection and prevention therefore work at two layers, the device arriving and the commands it types. ATT&CK lists M1034 Limit Hardware Installation as the matching mitigation, and D3FEND models the defensive side as D3-IDA Input Device Analysis and D3-HCI Hardware Component Inventory. See the MITRE ATT&CK T1200 Hardware Additions page for the technique itself.
Building HID allowlists that hold up
The removable media guide explains the Windows device installation policies. The question here is which identifier to allow, because that choice decides whether a spoofing device gets through.
| Match on | What it pins | Spoofing resistance | Operational cost | Use for |
|---|---|---|---|---|
| Setup class (Windows Keyboard, Mouse, HIDClass) | Every device of that type | None: any keyboard matches | Low | Blocking new keyboards entirely on servers and locked kiosks |
| Device ID or VID:PID | A product model | Low: a device that copies the IDs matches | Low to medium | Standard keyboards on general endpoints, with detection behind it |
| Device instance ID or serial number | A specific unit (where the device reports a unique serial) | Medium: harder to guess, but serials can be copied if known | High: needs an inventory and a replacement process | Privileged access workstations and admin consoles |
| Port and parent device (USBGuard via-port, parent-hash) | A device in a known physical position | Medium to high in combination with other attributes | Medium on fixed desks, poor for laptops | Kiosks, OT engineering stations, reception PCs |
| Descriptor hash (USBGuard hash) | The device attributes and USB descriptor data | Highest of the options here; USBGuard calls it the most specific value | High | Small fixed sets of devices on the most sensitive hosts |
| Interface pattern (USBGuard with-interface) | What the device claims to be | Strong against composite devices, weak against a pure keyboard clone | Low | Rejecting storage-plus-keyboard and keyboard-plus-network devices everywhere |
Decision guidance: no identifier is proof of identity, because USB has no device authentication in common use. Combine a specific identifier with a second control. On general laptops, allow standard keyboards by device ID and rely on detection. On privileged access workstations, pin instance IDs or descriptor hashes and lock ports. On servers in a data centre, block new keyboard and HID devices altogether and use out-of-band consoles. Small organisations can stop at the general laptop tier plus detection and add the stricter tiers only for admin and finance machines.
Platform controls
Platforms are listed alphabetically.
Linux
- Kernel USB authorisation: the kernel exposes an authorized attribute per device and an authorized_default attribute per host controller. Setting the default to 0 leaves new devices unconfigured until user space authorises them, and a value of 2 authorises only devices on internal ports. The kernel documentation warns that checking only class, type and protocol is weak verification.
- USBGuard: a daemon that applies allow, block or reject rules using attributes such as VID:PID, serial, hash, parent-hash, via-port and with-interface. The shipped daemon configuration sets ImplicitPolicyTarget to block and applies policy to devices present at start-up and inserted later. Its documentation includes a rule set that rejects any storage device that also exposes a HID keyboard, communications or wireless interface, and a rule that allows a keyboard-only device only when no keyboard is already allowed. The project explicitly calls default allow the wrong approach.
- Audit trail: USBGuard writes decisions to its audit log (by default under /var/log/usbguard/), which should be forwarded to the SIEM. See the Linux Server Hardening Guide for where this sits in a server baseline.
macOS
- Accessory approval: on Mac laptops with Apple silicon, System Settings, Privacy & Security, Accessories offers "Allow accessories to connect" with four options: Always ask, Ask for new accessories (the default), Automatically allow when unlocked, and Always allow. A new accessory cannot be approved while the Mac is locked, and a Mac locked for three or more days may need unlocking before a previously allowed accessory works again.
- Management: Apple says administrators of supervised Mac laptops with Apple silicon can control the setting with Apple Configurator. Do not set Always allow on managed fleets. See the macOS Security Guide for Enterprises.
- Limit: approval protects a locked or unattended Mac well; on an unlocked Mac the user decides, so it supports rather than replaces training and screen locking.
Windows
- Class GUIDs that matter for HID: Keyboard {4d36e96b-e325-11ce-bfc1-08002be10318}, Mouse {4d36e96f-e325-11ce-bfc1-08002be10318}, HIDClass {745a17a0-74d3-11d0-b6fe-00a0c90f57da} and Net {4d36e972-e325-11ce-bfc1-08002be10318}. A USB keyboard installs under both HIDClass and Keyboard, so blocking only one does not give a clean result.
- Intune: the DeviceInstallation policy CSP carries the same controls, including EnableInstallationPolicyLayering, PreventInstallationOfDevicesNotDescribedByOtherPolicySettings, AllowInstallationOfMatchingDeviceIDs, AllowInstallationOfMatchingDeviceInstanceIDs and PreventInstallationOfMatchingDeviceSetupClasses, under ./Device/Vendor/MSFT/Policy/Config/DeviceInstallation/. All are device scoped.
- Finding the right IDs: take hardware IDs from Device Manager (Details tab) or from PnPUtil's device enumeration on a reference machine with your standard keyboards, docks and headsets attached, including the host controllers and hubs in the chain. Microsoft warns that missing a hub or controller can lock users out of their keyboards and mice.
- Admin override: leave "Allow administrators to override Device Installation Restriction policies" disabled on hosts where HID allowlisting matters. See the Endpoint Privilege Management Guide for removing standing admin rights.
Detecting keystroke injection
Detection works at three points: the device arriving, the typing pattern, and the commands that follow.
| Signal | Source | What it catches | Limits |
|---|---|---|---|
| Event 6416, a new external device was recognised, with ClassName Keyboard or HIDClass | Windows Security log, Audit PNP Activity | Any new keyboard; the event records device ID, class GUID, hardware IDs and location information | Noisy on laptops with docks; most useful on servers and fixed hosts |
| Event 6423, the installation of this device is forbidden by system policy | Windows Security log, Audit PNP Activity (Windows 10 1511 and later) | Blocked attempts; Microsoft recommends monitoring every instance | Only fires where device installation restrictions are enforced |
| Second keyboard appearing while one is already present | Device inventory or EDR device telemetry | Composite and add-on devices | Docking stations and receivers legitimately add keyboards |
| Abnormal inter-keystroke timing | Host agent such as Google's open source USB Keystroke Injection Protection (UKIP) for Linux | Injection faster than human typing | Jitter features slow injection to human speed; fast typists and macro keyboards cause false positives |
| New HID device followed within seconds by a shell, script host or run dialog | EDR process telemetry correlated with device events | The classic injection sequence | Payloads that wait or type slowly widen the window |
| USBGuard reject or block decisions | USBGuard audit log | Composite devices and unknown keyboards on Linux | Only as good as the rule set |
| Unexpected WiFi access point or client near sensitive hosts | Wireless intrusion detection | Cables with WiFi control channels | Many environments do not monitor the radio spectrum |
Keystroke-timing detection in practice: UKIP watches a window of keystrokes (its README suggests five as effective) and flags inter-arrival times below a configurable threshold. In monitor mode it logs the device to syslog; in hardening mode it unbinds the driver to eject the device. An allowlist can exempt known devices, even limited to specific characters, which covers legitimate macro keyboards. Google notes that it is not an officially supported product, so treat it as a reference design to pilot rather than a supported control. On Windows, check whether your EDR or device control product documents keystroke-timing analysis before relying on it.
Forward PNP events centrally with the rest of the Security log; the Windows Event Log Guide covers collection. PowerShell script block logging and constrained language mode make the second stage visible and harder; see the PowerShell Security Guide.
Implementation plan
- Inventory and audit (weeks 1 to 2): enable Audit PNP Activity everywhere and USBGuard in a generated allow-current-devices policy on Linux pilots. Collect a month of 6416 events to learn which keyboards, docks, receivers and headsets are normal per device class.
- Tier the estate (week 3): sort hosts into servers, privileged access workstations, kiosks and shared machines, OT engineering stations and general endpoints, and choose the identifier strategy from the table above for each.
- Interface rules everywhere (week 4): on Linux, deploy USBGuard rules that reject storage devices with keyboard, communications or wireless interfaces. On Windows, alert on composite devices that add a keyboard interface.
- Enforce on the strict tiers (weeks 5 to 8): block new Keyboard and HIDClass installs on servers and kiosks; allowlist instance IDs on privileged workstations. Pilot with an IT ring first, keep a remote management path open, and do not use the retroactive "already installed" option until the allowlist is proven.
- Add detection (weeks 6 to 10): correlation rules for new HID followed by shell activity, timing-based monitoring on a Linux pilot group, and alerts on every 6423 event.
- Roll back safely: keep the previous policy version in your management tool, document a local recovery route (an approved PS/2 or built-in keyboard, or a remote console), and roll back by ring rather than estate-wide.
The Kiosk and Shared Device Security Guide and the Privileged Access Workstation Standard Template carry the wider baselines for the two tiers that need HID allowlisting most.
Investigating a suspected device
- Do not plug it in elsewhere: a suspect device or cable goes to an isolated analysis machine, never a spare corporate PC.
- Move quickly: vendor material for malicious cables lists self-destruct and geofencing features, so a recovered device may wipe itself or go inert. Photograph it, record where it was found and who used it, and bag it.
- Pull the host record: 6416 and 6423 events (device ID, hardware IDs, location information), EDR process trees from the minute after connection, and PowerShell logs.
- Check the session: who was signed in, whether the screen was unlocked, and what that account could reach. Reset that user's credentials if the injected commands could have captured them.
- Look for wireless: if the device could have a WiFi channel, review wireless monitoring for a new access point near the host at the time.
Where identity fits
A keystroke injection device is an identity attack in hardware form: it borrows the signed-in session. Three identity controls cut its value. Remove standing local admin rights so typed commands cannot install drivers, services or persistence. Use separate privileged accounts on dedicated workstations so an injected session on a daily laptop does not reach admin tools. And use phishing-resistant authenticators such as FIDO2 security keys, which require a physical touch that injected keystrokes cannot supply; see the Hardware Security Keys Guide. Approved security keys also enumerate as HID devices, so include them in every HID allowlist before enforcing.
Common failure patterns
- Trusting VID and PID on sensitive hosts: an allowlist of keyboard models stops opportunistic devices but not one that copies a common keyboard's IDs, which malicious cable vendors document as a feature.
- Blocking HIDClass but not Keyboard, or the reverse: a USB keyboard installs under both classes, so a half-configured policy gives unpredictable results in testing and production.
- Forgetting security keys, docks and receivers: enforcing an allowlist that omits FIDO2 keys, docking stations or wireless receivers locks people out and leads to the whole control being rolled back.
- Relying on typing speed alone: jitter features slow injection to human speed, so timing analysis must sit beside device and process detection.
- Unlocked unattended sessions: no device control helps if an administrator leaves a session open; short screen lock timeouts matter as much as policy.
- Ignoring cables: teams police drives and keyboards while accepting any charging cable; malicious cables look and charge like ordinary ones while dormant. See the Juice Jacking Guide for the charging side.
Measuring it
- Percentage of servers, kiosks and privileged workstations with HID allowlisting enforced.
- Unknown keyboard-class devices detected per month on allowlisted hosts, each investigated and closed.
- Count of 6423 blocked-installation events, with a reason recorded for each cluster.
- Mean time from a new-HID-plus-shell alert to triage in the SOC.
- Percentage of privileged sessions with a screen lock of five minutes or less.
Framework mapping
| Control objective | NIST SP 800-53 Rev. 5 | CIS Controls v8.1 | ISO/IEC 27001:2022 Annex A | ATT&CK / D3FEND |
|---|---|---|---|---|
| Restrict USB ports and device types | SC-41 Port and I/O Device Access; CM-7 Least Functionality | Safeguard 10.3 (removable media handling) | A.8.1 User endpoint devices | M1034 Limit Hardware Installation |
| Inventory approved peripherals | CM-8 System Component Inventory | Safeguard 1.1 asset inventory | A.8.1 User endpoint devices | D3-HCI Hardware Component Inventory |
| Lock unattended sessions | AC-11 Device Lock | Safeguard 4.3 automatic session locking | A.8.1 User endpoint devices | Reduces T1200 impact |
| Log and analyse device events | AU-12 Audit Record Generation; SI-4 System Monitoring | Control 8 audit log management | A.8.15 Logging; A.8.16 Monitoring activities | D3-IDA Input Device Analysis |
Physical protection of exposed ports on kiosks and reception machines sits under NIST SP 800-53 PE-3 Physical Access Control and ISO/IEC 27001:2022 A.7.2 Physical entry. Mapping by NHI Mgmt Group.
Practitioner checklist
- Enable Audit PNP Activity on all Windows hosts and forward 6416 and 6423 events to the SIEM.
- Block new Keyboard and HIDClass device installs on servers and locked kiosks.
- Allowlist privileged workstations by device instance ID, port or descriptor hash, not VID:PID alone.
- Deploy USBGuard on Linux endpoints with an implicit block target and interface rules for composite devices.
- Set Mac laptops with Apple silicon to Ask for new accessories or stricter, never Always allow.
- Add FIDO2 security keys, docks and receivers to every allowlist before enforcing.
- Correlate new HID devices with shell or script launches within seconds and alert.
- Remove standing local admin rights and keep screen lock timeouts short on privileged hosts.
- Issue approved cables and forbid unknown ones on sensitive workstations.
Standards and references
- Microsoft Learn: Manage device installation with Group Policy
- Microsoft Learn: System-defined device setup classes available to vendors
- Microsoft Learn: DeviceInstallation Policy CSP
- Microsoft Learn: Event 6416, a new external device was recognized by the System
- USBGuard: usbguard-rules.conf rule language
- Linux kernel documentation: Authorizing (or not) your USB devices to connect to the system
- Apple Support: Allow accessories to connect to a Mac
- Google: USB Keystroke Injection Protection (UKIP)
Related NHI Mgmt Group resources: Removable Media and USB Control Guide · USB Drop Attacks Guide · MITRE ATT&CK T1200 Hardware Additions · Endpoint Security Guide · Kiosk and Shared Device Security Guide