đź“„ malicious-usb-device-detector | home

Malicious USB Device Detector for Linux

guest@techdiy:~$

Malicious USB Device Detector

What They Are

Malicious USB charging cables, often called USB attack cables or O.MG cables, look like regular charging cables but contain hidden hardware that can steal data, install malware, or remotely control a device. Once plugged into a computer or smartphone, they can execute harmful commands without the user's knowledge.

These cables resemble ordinary charging/data cables, but contain hidden hardware—typically a tiny implant that can connect via Wi‑Fi to let an attacker remotely access your device.

How They Work

Once plugged in normally, the cable behaves like a regular USB‑A, USB‑C, micro‑USB or Lightning cable. But secretly, it can:

• Act as a keyboard to inject keystroke-based commands using DuckyScript.

• Launch scripts to install malware, steal passwords, or exfiltrate data.

• Function as a hardware keylogger, storing up to hundreds of thousands of keystrokes for later retrieval .

Real-World Impact

• These cables are marketed for red-teaming and penetration testing, but can also be used maliciously, posing serious threats to corporate or personal data security.

• Some variants offer advanced features like geo‑fencing, self-destruct, and encrypted command/control connections.

• Malicious USB charging cables act like BadUSB flash drives, they can emulate a keyboard or mouse to execute harmful commands, but are even more dangerous because they look like regular charging cables. Unlike suspicious-looking USB sticks, these cables blend in with everyday tech, making them much less likely to raise alarms or be noticed during security checks.

How to Stay Safe

Never use cables found unattended or offered by strangers (e.g. charging stations, shared cords, freebies).

Only use cables from trusted manufacturers, ideally original-brand accessories

Train staff and users to avoid cable baiting where attackers leave malicious cables deliberately for unsuspecting users.

Security protocols in professional environments should limit USB usage and regularly inspect any new cables entering the premises.

Here's some basic script for detection: (Linux command line only)

bad-usb-checker.sh:
#!/bin/bash

####################################################
# Script detects USB malicious behaviour  
# ..(especially HID injection or rogue USB devices) 
# ..while you plug in the USB cable
####################################################

echo "🔍 USB Safety Check Starting..."
TMPDIR=$(mktemp -d -t usbmon-XXXX)
trap "rm -rf $TMPDIR" EXIT

# Take snapshots before
echo "[*] Capturing initial state..."
lsusb > "$TMPDIR/lsusb_before"
ls /dev/input/ > "$TMPDIR/input_before"
ls /dev/ | grep tty > "$TMPDIR/tty_before"
lsblk > "$TMPDIR/lsblk_before"

# Check if libinput exists
HAS_LIBINPUT=false
if command -v libinput &> /dev/null; then
    HAS_LIBINPUT=true
    echo "[*] libinput detected, monitoring for HID injection..."
    sudo libinput debug-events > "$TMPDIR/libinput.log" &
    LIBINPUT_PID=$!
else
    echo "[!] libinput not found, skipping HID monitoring"
    echo "[!] You can install 'libinput-tools' for HID injection monitoring!"
fi

# Monitor dmesg in background
sudo dmesg --follow > "$TMPDIR/dmesg.log" &
DMESG_PID=$!

echo ""
echo "➡️  Please plug in the USB cable now. Waiting 30 seconds..."
sleep 30

# Stop background processes
kill $DMESG_PID &> /dev/null
[ "$HAS_LIBINPUT" = true ] && kill $LIBINPUT_PID &> /dev/null

# Take snapshots after
echo "[*] Capturing post-insertion state..."
lsusb > "$TMPDIR/lsusb_after"
ls /dev/input/ > "$TMPDIR/input_after"
ls /dev/ | grep tty > "$TMPDIR/tty_after"
lsblk > "$TMPDIR/lsblk_after"

# Compare and display only differences
echo ""
echo "đź§Ş Changes Detected:"
echo "--------------------"

function compare_and_report() {
    LABEL=$1
    FILE1=$2
    FILE2=$3
    DIFF=$(diff "$FILE1" "$FILE2")
    if [ -n "$DIFF" ]; then
        echo "🔸 $LABEL changed:"
        echo "$DIFF" | sed 's/^/> /'
        echo ""
    else
        echo "âś… $LABEL: No changes"
    fi
}

compare_and_report "USB Devices (lsusb)" "$TMPDIR/lsusb_before" "$TMPDIR/lsusb_after"
compare_and_report "/dev/input devices" "$TMPDIR/input_before" "$TMPDIR/input_after"
compare_and_report "/dev/tty* devices" "$TMPDIR/tty_before" "$TMPDIR/tty_after"
compare_and_report "Block Devices (lsblk)" "$TMPDIR/lsblk_before" "$TMPDIR/lsblk_after"

if [ "$HAS_LIBINPUT" = true ]; then
    echo "🔍 Keystroke/Motion Activity (from libinput):"
    if grep -q . "$TMPDIR/libinput.log"; then
        grep -E 'key|button|motion' "$TMPDIR/libinput.log" | sed 's/^/> /'
    else
        echo "âś… No input activity detected."
    fi
    echo ""
fi

echo "🛡️  Done. No persistent changes made to your system."

	

👣 Make the file executable:

chmod +x bad-usb-checker.sh

👣 Run the script before connecting the USB interface. The script will tell you when it's ready to connect it...


đź’ˇ Notes:

This script is experimental and should not be considered as the complete solution for detecting or protecting against these devices. It may be extended in the future to improve its effectiveness.

⚠️ Disclaimer:

This script is provided as-is, without any warranties or guarantees of any kind. Use of this at your own risk.