A robust Python-based file integrity monitoring system designed to protect web servers from defacement attacks. This tool uses SHA-256 hashing to detect unauthorized changes to critical web server files and alerts administrators via syslog.
- SHA-256 Hash-Based Detection: Cryptographically secure file integrity verification
- Comprehensive Monitoring: Monitors web content, Apache/Nginx configs, and .htaccess files
- Syslog Integration: Real-time alerts logged to system syslog
- Automated Scanning: Runs every 5 minutes via systemd timer or cron
- Security Hardened: Root-owned with immutable bit set to prevent tampering
- Baseline Management: Easy baseline creation and updates
- Detailed Reporting: Detects modified, new, and deleted files
/var/www/html- Web content directory/etc/apache2/*.conf- Apache configuration files/etc/apache2/sites-available/*- Apache virtual host configs/etc/apache2/sites-enabled/*- Active Apache virtual hosts/var/www/**/.htaccess- htaccess files/etc/nginx/*.conf- Nginx configuration files/etc/nginx/sites-available/*- Nginx virtual host configs/etc/nginx/sites-enabled/*- Active Nginx virtual hosts
- Operating System: Ubuntu 20.04 LTS or later
- Python: Python 3.6+
- Privileges: Root access required
- Dependencies: Standard Python libraries only (no external packages)
- Clone or download the repository:
cd /opt
sudo git clone <repository-url> web-integrity-monitor
cd web-integrity-monitor- Run the installation script:
sudo chmod +x install.sh
sudo ./install.shThe installation script will:
- Copy the script to
/usr/local/bin/web-integrity-monitor - Set ownership to root:root
- Set permissions to 700 (rwx------)
- Set the immutable bit using
chattr +i - Create baseline directory at
/var/lib/web-integrity - Install systemd service and timer
- Create initial baseline of monitored files
If you prefer manual installation:
- Copy the script:
sudo cp web_integrity_monitor.py /usr/local/bin/web-integrity-monitor- Set ownership and permissions:
sudo chown root:root /usr/local/bin/web-integrity-monitor
sudo chmod 700 /usr/local/bin/web-integrity-monitor- Set immutable bit:
sudo chattr +i /usr/local/bin/web-integrity-monitor- Create baseline directory:
sudo mkdir -p /var/lib/web-integrity
sudo chown root:root /var/lib/web-integrity
sudo chmod 700 /var/lib/web-integrity- Create initial baseline:
sudo /usr/local/bin/web-integrity-monitor --baselineAfter installation or after making legitimate changes to your website:
sudo web-integrity-monitor --baselineThis scans all monitored files and creates a baseline hash database.
Run a one-time integrity check with detailed output:
sudo web-integrity-monitor --monitor --verboseSilent mode (for automation):
sudo web-integrity-monitor --monitorTo monitor additional directories:
sudo chattr -i /usr/local/bin/web-integrity-monitor
sudo web-integrity-monitor --add-path /opt/custom-webapp --baseline
sudo chattr +i /usr/local/bin/web-integrity-monitorEnable and start the monitoring timer:
sudo systemctl enable web-integrity-monitor.timer
sudo systemctl start web-integrity-monitor.timerCheck timer status:
sudo systemctl status web-integrity-monitor.timer
sudo systemctl list-timers web-integrity-monitor.timerView service logs:
sudo journalctl -u web-integrity-monitor -fIf you prefer cron over systemd:
sudo chmod +x setup-cron.sh
sudo ./setup-cron.shThis creates a cron job at /etc/cron.d/web-integrity-monitor that runs every 5 minutes.
Real-time monitoring:
sudo tail -f /var/log/syslog | grep web-integritySearch for alerts:
sudo grep "web-integrity-monitor" /var/log/syslogsudo journalctl -t web-integrity-monitor -fRecent entries:
sudo journalctl -t web-integrity-monitor -n 50The system logs different severity levels:
- LOG_INFO: Baseline created, normal operations
- LOG_WARNING: Summary of detected changes
- LOG_ALERT: Specific file modifications, additions, or deletions
- LOG_ERR: Errors during operation
-rwx------ 1 root root /usr/local/bin/web-integrity-monitorOnly root can read, write, or execute the script.
The immutable bit prevents modification or deletion:
sudo lsattr /usr/local/bin/web-integrity-monitor
----i--------e------- /usr/local/bin/web-integrity-monitorTo modify the script (e.g., for updates):
# Remove immutable bit
sudo chattr -i /usr/local/bin/web-integrity-monitor
# Make changes or updates
# ...
# Restore immutable bit
sudo chattr +i /usr/local/bin/web-integrity-monitorThe baseline database is stored in /var/lib/web-integrity/ with:
- Root ownership
- 700 permissions
- Protected from unauthorized access
The systemd service includes:
PrivateTmp=yes- Isolated /tmp directoryNoNewPrivileges=yes- Prevents privilege escalationProtectSystem=strict- Read-only /usr, /boot, /efiProtectHome=yes- Home directories inaccessible
When you make authorized changes to your website:
# Remove immutable bit temporarily
sudo chattr -i /usr/local/bin/web-integrity-monitor
# Recreate baseline
sudo web-integrity-monitor --baseline
# Restore immutable bit
sudo chattr +i /usr/local/bin/web-integrity-monitor# Run manual check
sudo web-integrity-monitor --monitor --verbose
# Check if timer is active
sudo systemctl is-active web-integrity-monitor.timer
# View next run time
sudo systemctl list-timers web-integrity-monitor.timerTemporarily disable:
sudo systemctl stop web-integrity-monitor.timerPermanently disable:
sudo systemctl disable web-integrity-monitor.timer
sudo systemctl stop web-integrity-monitor.timerFor cron:
sudo rm /etc/cron.d/web-integrity-monitorEnsure you're running as root:
sudo web-integrity-monitor --monitor --verboseCheck if the default paths exist on your system:
ls -la /var/www/html
ls -la /etc/apache2
ls -la /etc/nginxIf using custom paths, add them to the monitoring list.
Create a baseline first:
sudo web-integrity-monitor --baselineCheck timer status:
sudo systemctl status web-integrity-monitor.timerReload systemd and restart:
sudo systemctl daemon-reload
sudo systemctl restart web-integrity-monitor.timerSome files may change legitimately (e.g., log files, cache files). Consider:
- Excluding specific directories
- Recreating baseline after legitimate changes
- Reviewing monitored paths in the script
Forward syslog to your SIEM:
# Configure rsyslog to forward web-integrity-monitor logs
sudo vim /etc/rsyslog.d/50-web-integrity.confAdd:
:syslogtag, isequal, "web-integrity-monitor:" @@your-siem-server:514
Create a wrapper script that emails on changes:
#!/bin/bash
OUTPUT=$(/usr/local/bin/web-integrity-monitor --monitor --verbose)
if [ $? -ne 0 ]; then
echo "$OUTPUT" | mail -s "Web Integrity Alert" admin@example.com
fiUse a webhook to send alerts to Slack or Teams when changes are detected.
.
├── web_integrity_monitor.py # Main Python script
├── install.sh # Automated installation script
├── setup-cron.sh # Cron setup script (alternative)
├── web-integrity-monitor.service # Systemd service file
├── web-integrity-monitor.timer # Systemd timer file
└── README.md # This file
-
Baseline Creation:
- Scans all files in monitored paths
- Calculates SHA-256 hash for each file
- Stores hashes in
/var/lib/web-integrity/baseline.json
-
Monitoring:
- Runs every 5 minutes (via timer/cron)
- Scans current files and calculates hashes
- Compares against baseline
- Detects:
- Modified files (hash changed)
- New files (not in baseline)
- Deleted files (in baseline but missing)
-
Alerting:
- Logs all changes to syslog with LOG_ALERT priority
- Logs summary with LOG_WARNING priority
- Sends to systemd journal
- Can be forwarded to SIEM, email, or other tools
- Regular Baseline Updates: Update baseline after deploying legitimate changes
- Monitor the Monitors: Ensure the timer/cron is running regularly
- Review Alerts Promptly: Investigate all alerts immediately
- Test Recovery: Practice responding to alerts
- Backup Baseline: Keep a backup of your baseline file
- Secure Logs: Ensure syslog is protected and backed up
- Document Changes: Keep a change log for your website
- Script runs as root - ensure it's properly secured
- Baseline file contains file paths - protect it appropriately
- Logs may contain sensitive path information
- Immutable bit prevents tampering but can be removed by root
- Consider running on a separate monitoring server for critical environments
This script is provided for defensive security purposes only.
For issues, questions, or contributions:
- Review the troubleshooting section
- Check systemd/syslog for detailed error messages
- Ensure all prerequisites are met
- Initial release
- SHA-256 hash-based integrity checking
- Syslog integration
- Systemd timer and cron support
- Immutable bit security
- Comprehensive monitoring of web server files