Problem overview
Unauthorized or unaccounted-for USB drives can pose significant security threats, from data theft to malware injection. Monitoring for USB drive insertions is essential for organizations that want to safeguard their systems and maintain compliance with data protection policies.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-monitor-usb-drive
# Look for USB Drives
USBDrivePresent=$(lsblk -o NAME,TRAN | grep "usb" | awk '{print $1}')
if [ -n "$USBDrivePresent" ]; then
# If USB drive is present, send console message for Level to alert on
echo "ALERT"
fi This script quickly checks whether any USB storage devices are currently connected to the system. If it detects a USB drive, it returns a simple "ALERT" message, which you can capture with a script-based monitor in Level to trigger a real-time notification and log the event.
You can also build an Automation in Level with a schedule trigger to regularly check for USB drives and act on any findings—this can include disabling the port or generating a security ticket for further investigation.
Use cases
- Monitoring secure environments for unauthorized USB attachments
- Enforcing data protection policies in regulated industries
- Logging removable media usage on remote or distributed workstations
- Real-time alerting of potential data exfiltration attempts
Recommendations
- Test the script in a safe environment before deploying widely
- Use a script-based monitor in Level to instantly detect drive insertions
- Consider scheduling automated checks via Level’s Automation features
- Verify that lsblk is available and functioning correctly on your Linux distribution
- Plan immediate or follow-up actions for any alerts—lockdown, logging, or user notifications