This repository contains a custom solution for monitoring USB drive activity on macOS systems with integration into Wazuh for enhanced security monitoring.
A Swift script-based tool that monitors and logs USB connection and disconnection events on macOS. The logs are formatted in JSON and can be integrated with Wazuh, a powerful open-source security monitoring tool, to provide real-time alerts and monitoring.
- A Wazuh Manager server running the latest version of Wazuh.
- A macOS endpoint with
USBMonitorexecutable andusb.idsfile in the same directory. - Wazuh Agent installed and configured on the macOS endpoint.
- Xcode or Xcode Command Line Tools installed on macOS.
Download the usb.ids file from the USB ID Repository and ensure it is placed in the same directory as the USBMonitor executable.
Set the necessary permissions using the following commands:
chmod +x /path/to/USBMonitor
chmod 644 /path/to/usb.ids
touch /var/log/usb_monitor.log
chmod 640 /var/log/usb_monitor.logIt's important to ensure that the usb.ids file is using UTF-8 encoding. Run the following command in the terminal:
iconv -f iso-8859-1 -t utf-8 usb.ids > usb-utf8.ids && mv usb-utf8.ids usb.idsThis command converts the encoding of the usb.ids file from ISO-8859-1 to UTF-8 and then replaces the original file with the converted one.
Download the USBMonitor.swift file from this repository. Update the /path/to/usb.ids in the script to the actual location of your usb.ids file before compiling.
xcode-select --install
swiftc USBMonitor.swift -o USBMonitorExecute the USBMonitor to start monitoring USB events:
/path/to/USBMonitorThe script logs events to /var/log/usb_monitor.log. Ensure the path to the usb.ids file is correct in the Swift script before executing the monitor.
To integrate the USB monitoring solution with Wazuh, follow these steps:
-
Install the Wazuh Agent on the macOS system if it's not already installed.
-
Configure the agent by editing the configuration file located at
/Library/Ossec/etc/ossec.conf. Add the following block to the configuration:
<localfile>
<log_format>json</log_format>
<location>/var/log/usb_monitor.log</location>
</localfile>This will direct the Wazuh Agent to monitor the log file generated by the USBMonitor.
On the Wazuh Manager server, update the /var/ossec/etc/rules/local_rules.xml file to include rules for processing macOS USB event logs. Add a new group for macOS USB-related rules:
<group name="macos,usb,">
<rule id="100010" level="7">
<decoded_as>json</decoded_as>
<field name="eventType">^USBConnected$</field>
<description>macOS: USB device connected</description>
<options>no_full_log</options>
</rule>
<rule id="100011" level="7">
<decoded_as>json</decoded_as>
<field name="eventType">^USBDisconnected$</field>
<description>macOS: USB device disconnected</description>
<options>no_full_log</options>
</rule>
</group>Replace the id attribute values with the appropriate rule IDs as per your Wazuh Manager configuration. For example, if you are already using these ids, then choose different ones.
After updating the configurations, restart both the Wazuh Agent and Manager services for the changes to take effect.
For the macOS endpoint, run:
sudo /Library/Ossec/bin/wazuh-control restartFor the Wazuh Manager:
sudo systemctl restart wazuh-managerTo test the integration, monitor the usb_monitor.log for new entries and check the Wazuh Manager dashboard for alerts corresponding to the USB device events.
tail -f /var/log/usb_monitor.log
When a USB device is connected or disconnected, you should see JSON-formatted log entries in the usb_monitor.log file and corresponding alerts in the Wazuh Manager. This real-time monitoring allows for quick detection and response to USB device activities on macOS systems.
To ensure the USBMonitor script runs automatically at every startup of your macOS machine, follow these steps to create a startup script:
-
Create a Launch Daemon
.plistfile. This file will instruct macOS to run theUSBMonitorscript at startup. -
Use the provided
com.user.usbmonitor.plistfile as a template by downloading it and placing it in your/Library/LaunchDaemonsfolder.Edit the file and replace
/path/to/USBMonitorwith the actual file path of yourUSBMonitorexecutable.
-
Save the
.plistfile to/Library/LaunchDaemons/com.user.usbmonitor.plist. -
Set the correct ownership and permissions for the file:
sudo chown root:wheel /Library/LaunchDaemons/com.user.usbmonitor.plist sudo chmod 644 /Library/LaunchDaemons/com.user.usbmonitor.plist
-
Load the daemon to register it with the system:
sudo launchctl load /Library/LaunchDaemons/com.user.usbmonitor.plist
After setting up the launch daemon, reboot your system. Once macOS starts up, check if the USBMonitor script is running and logging events as expected:
tail -f /var/log/usb_monitor.log
You should see log entries corresponding to USB events if any USB devices are connected or disconnected after the reboot.
If you're interested in contributing to this project, please fork the repository and submit a pull request. For substantial changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
This guide is for educational purposes only. It is recommended to review and test the code thoroughly before deploying it in a production environment.