-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_monitor.sh
107 lines (90 loc) · 2.96 KB
/
file_monitor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# Configuration
WATCH_DIRS="/etc /bin /sbin /usr/bin /usr/sbin" # Directories to monitor
HASH_FILE="/var/log/file_hashes.db" # Database of file hashes
LOG_FILE="/var/log/file_monitor.log" # Log file for changes
EMAIL="[email protected]" # Email for notifications
# Ensure running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Create directories if they don't exist
mkdir -p "$(dirname "$HASH_FILE")"
mkdir -p "$(dirname "$LOG_FILE")"
# Function to generate initial hash database
initialize_hashes() {
echo "Initializing hash database..."
> "$HASH_FILE"
for dir in $WATCH_DIRS; do
if [ -d "$dir" ]; then
find "$dir" -type f -exec sha256sum {} \; >> "$HASH_FILE"
fi
done
echo "Hash database initialized at $(date)" >> "$LOG_FILE"
}
# Function to check for file changes
check_files() {
local changes_detected=0
local temp_file=$(mktemp)
for dir in $WATCH_DIRS; do
if [ -d "$dir" ]; then
find "$dir" -type f -exec sha256sum {} \; >> "$temp_file"
fi
done
# Compare current hashes with stored hashes
while read -r line; do
if ! grep -q "^$line$" "$HASH_FILE"; then
changes_detected=1
file_path=$(echo "$line" | cut -d' ' -f2-)
echo "[$(date)] Changed file detected: $file_path" >> "$LOG_FILE"
# Get file details
permissions=$(ls -l "$file_path" | awk '{print $1}')
owner=$(ls -l "$file_path" | awk '{print $3}')
group=$(ls -l "$file_path" | awk '{print $4}')
# Log detailed information
{
echo "File: $file_path"
echo "Permissions: $permissions"
echo "Owner: $owner"
echo "Group: $group"
echo "Last modified: $(stat -c %y "$file_path")"
echo "---"
} >> "$LOG_FILE"
fi
done < "$temp_file"
rm "$temp_file"
if [ $changes_detected -eq 1 ]; then
send_alert
fi
}
# Function to send alerts
send_alert() {
local alert_message="File changes detected on $(hostname) at $(date)"
# Send email alert if mail is configured
if command -v mail &> /dev/null; then
tail -n 20 "$LOG_FILE" | mail -s "$alert_message" "$EMAIL"
fi
# Send desktop notification if running in GUI environment
if command -v notify-send &> /dev/null; then
notify-send -u critical "Security Alert" "$alert_message"
fi
# Log to system journal
logger -p auth.alert -t file_monitor "$alert_message"
}
# Main script logic
case "$1" in
"init")
initialize_hashes
;;
"check")
check_files
;;
*)
echo "Usage: $0 {init|check}"
echo " init - Initialize hash database"
echo " check - Check for file changes"
exit 1
;;
esac
exit 0