插件窝 干货文章 CentOS上搭建web服务器的安全审计与事件日志管理

CentOS上搭建web服务器的安全审计与事件日志管理

log Nginx 配置 var 870    来源:    2025-04-13

CentOS Web服务器安全审计与事件日志管理指南

一、基础安全配置

1. 系统加固

  • 更新系统:yum update -y && yum upgrade -y
  • 安装必要安全工具: bash yum install -y fail2ban rkhunter chkrootkit aide

2. 防火墙配置

systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

3. SELinux配置

setenforce 1
sed -i 's/SELINUX=permissive/SELINUX=enforcing/g' /etc/selinux/config

二、Web服务器日志配置

1. Apache日志配置

/etc/httpd/conf/httpd.conf中:

ErrorLog "|/usr/bin/rotatelogs /var/log/httpd/error_log.%Y%m%d 86400"
CustomLog "|/usr/bin/rotatelogs /var/log/httpd/access_log.%Y%m%d 86400" combined

2. Nginx日志配置

/etc/nginx/nginx.conf中:

access_log /var/log/nginx/access.log main buffer=32k;
error_log /var/log/nginx/error.log warn;

三、日志集中管理与轮转

1. Logrotate配置

创建/etc/logrotate.d/web_server

/var/log/httpd/*.log /var/log/nginx/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /usr/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
        /usr/bin/systemctl reload nginx.service > /dev/null 2>/dev/null || true
    endscript
}

2. 集中日志服务器配置(可选)

安装rsyslog:

yum install -y rsyslog

配置/etc/rsyslog.conf

module(load="imfile" PollingInterval="10")

# Apache日志
input(type="imfile"
      File="/var/log/httpd/access_log"
      Tag="apache-access"
      Severity="info"
      Facility="local6")

input(type="imfile"
      File="/var/log/httpd/error_log"
      Tag="apache-error"
      Severity="error"
      Facility="local6")

# Nginx日志
input(type="imfile"
      File="/var/log/nginx/access.log"
      Tag="nginx-access"
      Severity="info"
      Facility="local7")

input(type="imfile"
      File="/var/log/nginx/error.log"
      Tag="nginx-error"
      Severity="error"
      Facility="local7")

# 发送到远程日志服务器
*.* @192.168.1.100:514

四、安全审计工具

1. 安装并配置auditd

yum install -y audit
systemctl start auditd
systemctl enable auditd

配置/etc/audit/audit.rules

# 监控web目录
-w /var/www/html -p wa -k web_content
-w /etc/httpd/ -p wa -k apache_config
-w /etc/nginx/ -p wa -k nginx_config

# 监控关键系统文件
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity

# 监控sudo使用
-w /etc/sudoers -p wa -k sudoers
-w /var/log/sudo.log -p wa -k sudoers

2. 使用aIDE进行文件完整性检查

初始化数据库:

aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz

每日检查:

aide --check

五、实时监控与告警

1. 使用Fail2Ban防护

配置/etc/fail2ban/jail.local

[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/httpd/error_log
maxretry = 3
bantime = 3600

[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
bantime = 3600

2. 自定义告警规则

创建监控脚本/usr/local/bin/web_monitor.sh

#!/bin/bash

# 检查异常登录
ALERT_EMAIL="admin@example.com"
LOG_FILE="/var/log/web_monitor.log"

# 检查root登录
grep "Accepted password for root" /var/log/secure | tail -1 | while read line
do
    echo "$(date) - 警告:检测到root登录 - $line" >> $LOG_FILE
    echo "$line" | mail -s "服务器root登录告警" $ALERT_EMAIL
done

# 检查web目录文件变更
find /var/www/html -type f -mtime -1 -exec ls -la {} \; | while read line
do
    echo "$(date) - 警告:web目录文件变更 - $line" >> $LOG_FILE
done

添加到cron:

echo "*/5 * * * * root /usr/local/bin/web_monitor.sh" > /etc/cron.d/web_monitor

六、日志分析工具

1. 安装GoAccess(实时日志分析)

yum install -y goaccess

生成HTML报告:

goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html

2. 使用ELK Stack(可选)

对于大规模部署,建议使用Elasticsearch + Logstash + Kibana进行日志分析。

七、定期审计任务

  1. 每周安全检查

    rkhunter --check --sk
    chkrootkit
    aide --check
    
  2. 每月安全报告

    ausearch -k web_content | aureport -f -i > /var/log/web_audit_$(date +%Y%m).log
    
  3. 季度渗透测试:使用工具如Nessus或OpenVAS进行漏洞扫描。

通过以上配置,您的CentOS Web服务器将具备完善的安全审计和日志管理能力,能够及时发现并响应安全事件。