Automated CSF Blacklist Management with Alerts and Whitelisting for Production Servers

CSF (ConfigServer Security & Firewall) is widely used for Linux server security. While CSF automatically blocks suspicious IPs, managing blacklists manually in production environments can be tedious and error-prone.

An automated CSF blacklist management system provides:

This guide walks you through creating a production-ready, automated CSF monitoring and management system.


Real-Life Scenario

Scenario:

Solution:


Step 1: Prerequisites

Verify CSF:

csf -v
systemctl status csf

Step 2: Create a Whitelist File for Trusted IPs

Create a simple file to store pre-approved IPs:

mkdir -p /etc/csf/whitelist
nano /etc/csf/whitelist/trusted_ips.txt

Example content:

198.51.100.10   # Monitoring Server
203.0.113.25    # Corporate VPN

Set permissions:

chmod 600 /etc/csf/whitelist/trusted_ips.txt
chown root:root /etc/csf/whitelist/trusted_ips.txt

Step 3: Create Monitoring Script

Create a Bash script /usr/local/bin/csf-monitor.sh:

#!/bin/bash

# File with trusted IPs
WHITELIST="/etc/csf/whitelist/trusted_ips.txt"

# Get recently blocked IPs
BLOCKED_IPS=$(csf -g | grep "found" | awk '{print $1}')

for IP in $BLOCKED_IPS; do
    # Skip if IP is trusted
    if grep -q "$IP" $WHITELIST; then
        echo "Trusted IP $IP is blocked. Removing..."
        csf -dr $IP
        csf -r
        echo "$(date): Auto-whitelisted $IP" >> /var/log/csf-auto-whitelist.log
        # Optional: send alert email
        echo "Trusted IP $IP was auto-whitelisted" | mail -s "CSF Auto-Whitelist Alert" [email protected]
    else
        # Send alert for unknown IP
        echo "$(date): $IP is blocked by CSF" >> /var/log/csf-blocked.log
        echo "$IP is blocked by CSF" | mail -s "CSF Block Alert" [email protected]
    fi
done

Make it executable:

chmod +x /usr/local/bin/csf-monitor.sh

Step 4: Automate Script via Cron

Schedule the script to run every 5 minutes:

crontab -e

Add:

*/5 * * * * /usr/local/bin/csf-monitor.sh

Why: Ensures real-time monitoring without manual intervention.


Step 5: Real-Life Example

This keeps production services running smoothly while maintaining security.


Step 6: Add Slack Notifications (Optional)

  1. Create Slack webhook URL

  2. Add function to send alerts:

send_slack_alert() {
    WEBHOOK="https://hooks.slack.com/services/XXXX/XXXX/XXXX"
    MESSAGE="$1"
    curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" $WEBHOOK
}

Call this function inside the loop:

send_slack_alert "Trusted IP $IP was auto-whitelisted"
send_slack_alert "$IP is blocked by CSF"

Step 7: Monitoring & Logging

cat /var/log/csf-auto-whitelist.log
cat /var/log/csf-blocked.log

Step 8: Security & Best Practices


Step 9: Benefits


💡 Pro Tip: Combine this with fail2ban integration to proactively manage repeated failed login attempts across services like SSH, FTP, and email servers, and auto-whitelist critical systems.