2025-04-25 08:46:09 +02:00

116 lines
3.3 KiB
Bash

#!/bin/bash
# =============================================================================
# Antivirus configuration module
# =============================================================================
# Set script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Source common functions and variables
source "./common.sh"
# Function to install and configure ClamAV
configure_clamav() {
log_message "INFO" "Installing and configuring ClamAV antivirus"
# Install ClamAV if not already installed
if ! is_package_installed "clamav" || ! is_package_installed "clamav-daemon"; then
log_message "INFO" "Installing ClamAV and related packages"
apt-get install -y clamav clamav-daemon clamav-freshclam
if [ $? -ne 0 ]; then
log_message "ERROR" "Failed to install ClamAV"
return 1
fi
else
log_message "INFO" "ClamAV is already installed"
fi
# Configure ClamAV
local freshclam_conf="/etc/clamav/freshclam.conf"
log_message "INFO" "Configuring ClamAV"
backup_file "$freshclam_conf"
# Stop ClamAV services to update configuration
systemctl stop clamav-freshclam
systemctl stop clamav-daemon
# Configure freshclam (virus database updater)
sed -i 's/^Example/#Example/' "$freshclam_conf"
sed -i 's/^Checks.*/Checks 24/' "$freshclam_conf"
sed -i 's/^DatabaseMirror.*/DatabaseMirror db.local.clamav.net/' "$freshclam_conf"
log_message "SUCCESS" "ClamAV freshclam configuration updated"
# Create a daily scan script
local SCAN_SCRIPT="/etc/cron.daily/clamscan"
log_message "INFO" "Creating daily scan script"
cat > "$SCAN_SCRIPT" << 'EOF'
#!/bin/bash
# ClamAV daily scan script
# Generated by security hardening script
# Set log file
LOG_FILE="/var/log/clamav/daily_scan.log"
SCAN_DIR="/"
EXCLUDE_DIRS=("/sys" "/proc" "/dev" "/media" "/mnt" "/run" "/var/lib/clamav")
# Create log directory if it doesn't exist
mkdir -p /var/log/clamav
# Start log
echo "ClamAV daily scan started at $(date)" > "$LOG_FILE"
# Build exclude parameters
EXCLUDES=""
for dir in "${EXCLUDE_DIRS[@]}"; do
EXCLUDES="$EXCLUDES --exclude-dir=$dir"
done
# Run scan
clamscan -r $EXCLUDES --infected --detect-pua=yes --log="$LOG_FILE" --append "$SCAN_DIR"
# Email report if infected files found
INFECTED=$(grep -c "Infected files" "$LOG_FILE")
if [ "$INFECTED" -gt 0 ]; then
echo "Virus detected! See log at $LOG_FILE" | mail -s "ClamAV Virus Alert" root
fi
# End log
echo "ClamAV daily scan completed at $(date)" >> "$LOG_FILE"
EOF
chmod +x "$SCAN_SCRIPT"
log_message "SUCCESS" "ClamAV daily scan script created at $SCAN_SCRIPT"
# Restart ClamAV services
log_message "INFO" "Starting ClamAV services"
service start clamav-freshclam
service start clamav-daemon
if [ $? -eq 0 ]; then
log_message "SUCCESS" "ClamAV services started successfully"
else
log_message "ERROR" "Failed to start ClamAV services"
return 1
fi
# Update virus database
log_message "INFO" "Updating ClamAV virus database"
freshclam
if [ $? -eq 0 ]; then
log_message "SUCCESS" "ClamAV virus database updated successfully"
else
log_message "WARNING" "ClamAV virus database update encountered issues"
fi
}
# Main execution for antivirus
configure_clamav
log_message "SUCCESS" "Antivirus configuration completed"