yum update -y && yum upgrade -y
bash
yum install -y fail2ban rkhunter chkrootkit aide
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
setenforce 1
sed -i 's/SELINUX=permissive/SELINUX=enforcing/g' /etc/selinux/config
在/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
在/etc/nginx/nginx.conf
中:
access_log /var/log/nginx/access.log main buffer=32k;
error_log /var/log/nginx/error.log warn;
创建/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
}
安装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
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
初始化数据库:
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
每日检查:
aide --check
配置/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
创建监控脚本/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
yum install -y goaccess
生成HTML报告:
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html
对于大规模部署,建议使用Elasticsearch + Logstash + Kibana进行日志分析。
每周安全检查:
rkhunter --check --sk
chkrootkit
aide --check
每月安全报告:
ausearch -k web_content | aureport -f -i > /var/log/web_audit_$(date +%Y%m).log
季度渗透测试:使用工具如Nessus或OpenVAS进行漏洞扫描。
通过以上配置,您的CentOS Web服务器将具备完善的安全审计和日志管理能力,能够及时发现并响应安全事件。