2025-04-22 16:50:42 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Path to Snort's alert log (snort.alert.fast)
|
|
|
|
SNORT_LOG="/var/log/snort/snort.alert.fast"
|
|
|
|
|
2025-04-23 16:02:27 -04:00
|
|
|
# Database connection details
|
2025-04-26 08:10:36 -04:00
|
|
|
DB_HOST="38.102.127.174"
|
2025-04-23 16:02:27 -04:00
|
|
|
DB_USER="ipblocks_user"
|
|
|
|
DB_PASS="rusty2281"
|
|
|
|
DB_NAME="ipblocks"
|
2025-04-22 16:50:42 -04:00
|
|
|
|
2025-04-23 16:02:27 -04:00
|
|
|
# Function to insert blocked IP into the PostgreSQL database
|
2025-04-22 16:50:42 -04:00
|
|
|
block_ip() {
|
|
|
|
local ip=$1
|
|
|
|
|
2025-04-23 16:02:27 -04:00
|
|
|
# Remove port if included in the IP
|
|
|
|
ip=${ip%%:*}
|
2025-04-22 16:50:42 -04:00
|
|
|
|
2025-04-23 16:02:27 -04:00
|
|
|
# Insert the blocked IP into the PostgreSQL database (into the blocked_ip_log table)
|
|
|
|
PGPASSWORD="$DB_PASS" psql -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME" -c "INSERT INTO blocked_ip_log (ip_address) VALUES ('$ip');"
|
|
|
|
|
|
|
|
# Optionally print to confirm the insertion
|
|
|
|
echo "Blocked IP $ip inserted into the database log."
|
2025-04-22 16:50:42 -04:00
|
|
|
}
|
|
|
|
|
2025-04-23 16:02:27 -04:00
|
|
|
# Ensure the log file exists and is readable
|
|
|
|
if [ ! -f "$SNORT_LOG" ]; then
|
|
|
|
echo "Snort log file not found!"
|
|
|
|
exit 1
|
2025-04-22 16:50:42 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Monitor the snort.alert.fast file for new malicious IPs
|
|
|
|
tail -F "$SNORT_LOG" | while read line; do
|
|
|
|
# Debug: Output the full line from Snort log
|
|
|
|
echo "Processing: $line"
|
|
|
|
|
|
|
|
# Extract source and destination IP addresses from Snort logs
|
|
|
|
if echo "$line" | grep -q "ICMP PING NMAP"; then
|
|
|
|
# Extract source IP (before "->")
|
2025-04-23 16:02:27 -04:00
|
|
|
ip=$(echo "$line" | awk -F' -> ' '{print $1}' | awk '{print $NF}' | cut -d':' -f1)
|
2025-04-22 16:50:42 -04:00
|
|
|
echo "Found Source IP: $ip" # Debug: Show the IP being extracted
|
|
|
|
block_ip "$ip"
|
|
|
|
elif echo "$line" | grep -q "EXPLOIT"; then
|
|
|
|
# Extract source IP (before "->")
|
2025-04-23 16:02:27 -04:00
|
|
|
ip=$(echo "$line" | awk -F' -> ' '{print $1}' | awk '{print $NF}' | cut -d':' -f1)
|
2025-04-22 16:50:42 -04:00
|
|
|
echo "Found Source IP: $ip" # Debug: Show the IP being extracted
|
|
|
|
block_ip "$ip"
|
|
|
|
fi
|
|
|
|
done
|