What can I do to protect my Linux laptop from BadUSB attacks as described by ArsTechnica here?

Perhaps writing an appropriate AppArmor profile would help?

  • 2
    Since nobody answered about AppArmor, no it would not help. AA adds access control for userland processes, not for firmware code which AA is not even aware of. Commented Oct 8, 2014 at 14:56
  • 2
    Thought I'd at least mention USBGuard: github.com/dkopecek/usbguard Commented Dec 4, 2016 at 2:09

7 Answers 7


The BadUSB attack is based on the fact that computers allow and enable HID devices on all usb ports. Faked network adapters are no real danger. My answer tries to describe how to use udev to temporarily disable the addition of new HID devices. I'm no udev expert, but I've tested my approach, and it works for me.

For preparation, create a file /etc/udev/rules.d/10-usbblock.rules with the content:

#ACTION=="add", ATTR{bInterfaceClass}=="03" RUN+="/bin/sh -c 'echo 0 >/sys$DEVPATH/../authorized'"

If you want to block other classes too, then look up the class number, and copy the line, and change the class.

Now you can block all new HID devices using the command

sed -i 's/#//' /etc/udev/rules.d/10-usbblock.rules; udevadm control --reload-rules

and unblock with:

sed -i 's/^/#/' /etc/udev/rules.d/10-usbblock.rules; udevadm control --reload-rules

Before you shut down, always unblock, as the setting is persistent, and your "good" HID devices would be rejected on reboot.

I don't know whether you can edit the temporary rules directory, but if changes there affect the behaviour, you should edit that instead, as then you don't need to unblock before shutdown.

  • 1
    Can you give some details on the time based approach?
    – student
    Commented Aug 3, 2014 at 13:37
  • @user53541 tried to comment but can't due to reputation limit so he had to resort to editing to talk to us. Rejected edit, but adding comment for him: "It can't work to authorize device using interface class number, the interface device will not be created if the device is not authorized."
    – Luc
    Commented Aug 9, 2014 at 7:51
  • 3
    @user53541 He's right, I've now tested a new approach: authorized per default, and if there is an interface with class 3 added, the device gets de-authorized. I'll edit my answer accordingly. Hey user53541, do you want to answer it yourself? Its obvious that I'm new to udev, so perhaps you have a better solution in mind.
    – user10008
    Commented Aug 9, 2014 at 18:41
  • Really nice answer, but now my question is this: what happens if your keyboard or mouse, your supposedly-good HID devices, are able to run BadUSB as well? How would you handle it in that situation? Most BadUSB attacks that I've personally seen were in the keyboard and mouse. Commented Nov 5, 2015 at 16:34
  • 1
    Some stuff you have to trust. You can't have no trust anchor. You can prevent getting attacked by BadUSB from devices you don't use for HUD input, but devices you do use for HUD you have to trust.
    – user10008
    Commented Nov 11, 2015 at 1:07

The idea behind BadUSB is that a malicious agent re-flashes a device's USB controller chip to do something nasty. This is an interesting possiblity, but there are some serious assumptions here that people tend to gloss over:

1: The USB controller chip has to allow firmware flashing over the USB connection

This is a security vulnerability for sure if it's possible. If this is allowed, then any host that the device is plugged in to can permanently alter the characteristics of the device. Generally this requires special equipment and direct access to the chip's physical pins, but if a manufacturer decided to expose the functionality over the USB protocol, then that is cause for alarm in itself, and should be reported as a vulnerability in that product. It is not, however, a flaw in the protocol itself.

The fact that 3 of the 4 scheduled demos involve chips from Phison Electronics suggest that the researcher discovered just such a vulnerability in a specific product.

2: The device has to be physically capable of the activity you're attempting

By flashing your device's firmware, you can get a thumb drive to report itself as a network adapter. But that doesn't make it actually a network adapter, it just means that the computer will talk to it as if it was one. So now your computer starts talking to your Verbatim Store-n-Go using the driver for the D-Link DUB-E100. But unless the Store-n-Go has the corresponding hardware interface found in the D-Link, all you have is broken USB stick.

If the USB stick has a relatively powerful microcontroller on board, you might be able to re-program it. But "powerful" and "USB peripheral" don't usually go together.

3: The computer has to be willing to play along

One of the examples cited is teaching a device to act like a network adapter, and then assuming that all traffic will be looped through it on any computer you plug it in to. That's... a stretch. To make that happen, your computer has to be already configured to set any newly connected network adapter as the new default gateway. I'm not sure if Windows is that eager for change, but if you've ever configured networking on a Linux computer, you know that it's never that simple.

The Take-Away

This whole concept isn't all bunk. If a device allows re-flashing by any connected host, that's an issue. I can safely state with 100% certainty that it won't lead to the calamities pushed by the associated breathless news articles. But it's worth attention.

And more importantly, USB is powerful, and powerful means potentially dangerous. Connecting a device over USB necessarily means altering the way your computer behaves, and very, very dangerous things are possible. We've known this since the 90's. Use all due caution when attaching things to your computer. But this new discovery changes very little; the new attacks possible here aren't nearly as powerful as what already exists, the new danger is that it blurs the line between "trusted" and "untrusted" devices.

  • 3
    Another very real use case for pretending to be a different device is to exploit a vulnerability in a driver for that device. The potential for such a bug is big, as you can send data that the driver was never designed to handle. This is especially tempting as exploiting a driver can give you an express route to privilege escalation. If you find such a bug in a driver that's included with the target operating system, you will also have good coverage.
    – nitro2k01
    Commented Oct 3, 2014 at 3:58
  • 2
    Unfortunately, it's not uncommon to find USB mass storage drives with reprogrammable controllers. I actually have a bunch with Alcor controllers right here. But there are many different controllers, and your modified firmware would have to be different for each controller - making self-propagating attacks infeasible at best. And if you don't need that then you might as well just use special hardware physically disguised as a flash drive.
    – Bob
    Commented Oct 6, 2014 at 13:29
  • @tylerl, Re "this new discovery changes very little"; You sort of missed the point of the article. Before the discovery, you can buy a USB device and be confident that its safe to plug into your computer. Now you can't.
    – Pacerier
    Commented Mar 6, 2018 at 8:24
  • @Pacerier quite the opposite. My point was that you never could be confident that a usb device was not malicious, regardless of the source or packaging. This vuln in phison devices is interesting, but it's no more likely an everyday threat than, say, supply-chain attacks.
    – tylerl
    Commented Mar 6, 2018 at 8:44
  • @tylerl, Before the publication, you can take a new USB from a trusted supplier, reformat it, and be sufficiently safe. Now you can't even after the reformat isn't it?
    – Pacerier
    Commented Mar 9, 2018 at 12:54

BadUSB isn't an attack. It's a tool used when designing attacks.

When you plug a USB device into a computer, the device tells the computer what sort of thing it is, so the computer can select the appropriate driver. For example, a thumb drive declares itself as a "USB Mass Storage" device, while a keyboard is a "Human Interface Device".

BadUSB is a technique for re-writing the firmware of a plugged-in USB device from the computer. For example, it could make a thumb drive identify itself as a mouse and cause the pointer to jump around at random. Or it could make the thumb drive identify as a USB hub with connected keyboard and mass storage, that when plugged in types a sequence of keystrokes that causes a program on the thumb drive to be run.

The novel thing here isn't making USB devices that aren't what they look like -- intelligence agencies, cybercriminals, and others have been doing this for years. The novel thing is that this can be done to commodity hardware over the USB connection, opening the door to self-propagating systems, such as a virus that writes itself to any thumb drive that's been plugged into the machine, then modifies the drive's firmware to try to run the virus any time the drive gets plugged in.

This is a hard thing to defend against, since it operates on such a low level and is so flexible in what an attacker can do with it. On a Linux system, one thing you can do is ensure that all removable media are mounted as "noexec" -- this prevents programs on them from being run, and makes attacks harder. You can also check the USB device tree any time you plug something in, to see if anything unexpected has shown up.

On a non-technical level, avoid using any USB device that's been plugged into a possibly-infected system,

  • 8
    noexec would not help much. There is not much difference for an attacker to emulate keystrokes which execute a program from the stick to emulate keystrokes which first copy the program and change the permissions and then execute it. Commented Aug 2, 2014 at 20:29
  • Re "mounted as "noexec""; What's the command you're referring to?
    – Pacerier
    Commented Mar 6, 2018 at 8:30
  • @SteffenUllrich, But you can see it on screen while it does so right? There's just no way to do it so quickly that it wouldn't have any windows "flashing".
    – Pacerier
    Commented Mar 6, 2018 at 8:32
  • @Pacerier: not necessarily and depends on the environment. But you should see that something is happening if you know where to look, i.e. the attack is not fully stealth. As for noexec - a simple search leads for example to What does NOEXEC flag mean .... Commented Mar 6, 2018 at 9:03
  • @SteffenUllrich, Is there any way the attack can prevent the detection system from detecting it?
    – Pacerier
    Commented Mar 9, 2018 at 12:51

I've recently started to work on USBGuard which implements a USB device whitelist/blacklist with the help of UDev and the Linux kernel USB authorization feature. It's a user-space daemon that listens to UDev events and authorizes or deauthorizes USB devices based on a set of rules written in a custom rule language.

I think it makes it much harder for an attacker to reprogram a device in a way that it will bypass a good whitelist (i.e. as specific as it gets). Of course, USBGuard cannot protect you from every attack and if an attacker has physical access to your USB devices, then he has access to all the values needed to create a copy of such a device, for example a keyboard. Keyboards are especially bad cases as they usually don't export any iSerial value which would distinguish two keyboards of the same type.

However, using a whitelist of known devices is good against hidden/unexpected interfaces on a USB device. The notorious example being a keyboard or a network card added (by reprogramming on a different/malware infected machine). If you know that your USB flash disk should have only one mass storage interface and suddenly it has something more, then you know you shouldn't let it interact with your system -- and that's the main use case of USBGuard.

The same level can be implemented just using UDev rules and some helpers to parse USB descriptors, but USBGuard makes it more convenient.

  • 1
    Hey Daniel Kopeček I had to hunt your answer down on a place that I had enough reputation to up-vote and comment that you've made a sweet tool. Ya may want to update your readme file to reflect additional packaging support because it looks like Kali Sana has added packaged support for your tool; even for ARMhf CPUs, RPi or Android with chroot. I'll be trying it out very soon and thank you for building and advertising your project.
    – S0AndS0
    Commented Nov 27, 2016 at 19:24

Accept the first USB HID Keyboard/Mouse, prompt on screen to accept more USB HID Keyboards/Mice. Probably the simplest way, I don't know of any OS or software which has implemented something like that yet.

Alternatively, display something onscreen that the user has to enter from the keyboard so that you know its not a rogue device. Like, a short sequence of numbers or letters. (Because how would a rogue device know what's onscreen?)

  • Just accepting the first device leaves scope to have an input box on screen & no functioning keyboard - either if the mouse comes up 1st, or the badusb device does...
    – user57212
    Commented Oct 8, 2014 at 9:55
  • Magstripe and barcode readers are usually implemented as HID keyboards. If the OS enumerates one of them first, you could find yourself with no way to input arbitrary values.
    – Mark
    Commented Oct 9, 2014 at 4:35
  • @linagee, Re "how would a rogue device know what's onscreen"; Is there any way for the USB to advertise itself as a "screen" too? Or is that just physically impossible?
    – Pacerier
    Commented Mar 6, 2018 at 8:37
  • No need to "accept the first USB HID Keyboard/Mouse"... just prompt always and include "or tap the power button" as a fallback option for accepting a new device. The power button has effectively been a one-key keyboard with a default global hotkey bound to it for decades. (ATX+ACPI) It even shows up in places like the xinput tool's input device listing on Linux. (I disabled its default behaviour and bound it to "put screens to sleep") It makes all the sense in the world to use it as the hardware equivalent to how Windows NT uses Ctrl+Alt+Delete as its "secure attention key".
    – ssokolow
    Commented May 9, 2023 at 20:30

The easiest way is probably to use a secure kernel, I mean the grsecurity patched kernel:


Grsec is providing 2 options to disable usb after boot :

the soft one :


User can still unlock usb using a sysctl ( kernel.grsecurity.deny_new_usb )

the hard one :


No usb at all after boot

Grsec is a set of source patches enhancing the security level of the linux kernel, and will protect you of many more security problems ( buffer overflow, stack exec and many more ), see https://grsecurity.net/features.php

You can build your own grsecurity patched linux kernel on any linux distro, but the distros supporting it are :




  • This only helps if you're worried about someone else putting a flash drive in against your wishes; that doesn't help if, for instance, you plug a flash drive into an infected computer (unknowingly) and then plug it into your computer. The main problem is that you might want to connect a USB device, just not a keyboard (for instance); a restriction would really have to be based on device class to be effective against that (then a device can only be malicious within the class of devices you want it to be in).
    – cpast
    Commented Dec 18, 2014 at 2:33
  • you can still reboot with your ( infected or not ) usb device to get it recognized.
    – neofutur
    Commented Dec 19, 2014 at 5:15
  • That's precisely my point. The threat posed by BadUSB is not limited to attackers plugging an infected USB into your computer; one of the main threats is a user unintentionally infecting themselves. Among the biggest issues with BadUSB, compared to out-of-band USB reprogramming that's always been possible, is that an infected computer can infect a device connected to it without the user knowing. If a user's work computer is infected, it can infect their flash drive, and doing this won't stop that flash drive from infecting their home computer.
    – cpast
    Commented Dec 19, 2014 at 6:16

There are projects that do exactly what you want:

I have tested, on ubuntu 18.04, usbdeath and usbcut. The difference is that usbdeath turns off the PC when inserting an unauthorized device. Instead blackusb blocks the device unauthorized (without turning off the PC)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .