After creating the rule with
auditctl -w /home/me/mysterious_file -k watch_for_mysterious_file
I use this script to get desktop notifications via notify-send:
#!/bin/bash
# /usr/local/bin/audit-monitor.sh
user_to_notify="me"
# Audit key that ausearch uses
audit_key="watch_for_mysterious_file"
# File to store the time after which ausearch should look for events.
# On exiting this script, we'll update the file with the current date and time
last_run_file="/tmp/last_audit_check"
# Initialize the last run time if it doesn't exist,
# we can use ausearch's special time values here such as
# "yesterday", "this-year", "recent", etc.
if [ ! -f "$last_run_file" ]; then
echo yesterday > "$last_run_file"
fi
# Search the audit log for entries with the specified key after the last run time.
# We need to pass the line from last_run_file without quotes so --start gets two parameters: the date and the time separated
new_events=$(ausearch -k "$audit_key" --start $(cat "$last_run_file"))
# If there are new events send a notification to the specified user
if [[ -n "$new_events" ]]; then
# Get the executable names that caused the new events, separated by commas
exe_names=$(grep -oP 'exe="\K[^"]+' <<< "$new_events" | sort | uniq | sed -z 's/\n/, /g; s/, $/\n/')
id_of_user_to_notify=$(id -u "$user_to_notify")
sudo -u "$user_to_notify" DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$id_of_user_to_notify"/bus notify-send "Audit Event Triggered" "New event detected for key: $audit_key. Executable names: $exe_names"
echo "New event detected for key: $audit_key"
fi
# Update the last run time, see "man ausearch" for the right date and time format
date '+%x %H:%M:%S' > "$last_run_file"
Then, I created a systemd service that calls the script:
# /etc/systemd/system/audit-monitor.service
[Unit]
Description=Audit Event Monitoring Service
After=auditd.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/audit-monitor.sh
The timer runs every minute:
# /etc/systemd/system/audit-monitor.timer
[Unit]
Description=Run Audit Event Monitoring Script Periodically
[Timer]
OnCalendar=*:0/1
[Install]
WantedBy=timers.target
Enable and start the timer with
systemctl daemon-reload && systemctl enable --now audit-monitor.timer
If auditd is not yet enabled and running:
systemctl enable --now auditd.service