Problem overview
Unregulated or unauthorized USB devices can expose macOS systems to data leaks, malware threats, and policy violations. Manually monitoring for USB connections is time-consuming and prone to oversight, leaving IT Professionals and MSPs vulnerable to unexpected security breaches.
#!/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-macos-monitor-usb-drive
# Look for USB Drives
USB_DRIVE_PRESENT=$(system_profiler SPUSBDataType | grep "Mount Point")
if [[ ! -z "$USB_DRIVE_PRESENT" ]]; then
# If USB drive is present, send console message for Level to alert on
echo "ALERT"
fi This script uses the built-in system_profiler command to detect any mounted USB drives on a macOS endpoint. If it finds an active USB device, it prints “ALERT,” which can be captured and reported by Level. This enables immediate visibility into unauthorized or unexpected removable media usage.
You can configure a script-based monitor in Level to trigger on-demand checks whenever USB-related activity is suspected, or set it to run on a schedule via a Level Automation. By pairing it with a remediation action, you can automatically disable or eject suspicious drives to maintain tighter security.
Use cases
- Monitoring high-security environments for unauthorized removable devices
- Detecting unexpected USB usage in remote work setups
- Enforcing data protection rules where external drives are restricted
- Auditing USB device activity on shared or public systems
Recommendations
- Test the script in a non-production environment to confirm expected output
- Use a script-based monitor in Level to generate immediate alerts on detection
- Schedule periodic checks with Level Automations for continuous oversight
- Consider pairing with a remediation script to disable or eject drives automatically
- Keep detailed logs of alerts to identify patterns or repeated policy violations