在 CentOS + Apache 环境下,防攻击通常需要从 系统层、Apache 配置层、应用层、网络层 多维度来做。下面按“实用优先”的方式给你一套常见方案。
yum update -y
systemctl list-unit-files --type=service
systemctl disable telnet
systemctl start firewalld
systemctl enable firewalld
# 只开放必要端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
vi /etc/ssh/sshd_config
Port 2222
PermitRootLogin no
PasswordAuthentication no
vi /etc/httpd/conf/httpd.conf
ServerTokens Prod
ServerSignature Off
重载:
systemctl reload httpd
LimitRequestBody 10485760 # 10MB
<IfModule mpm_prefork_module>
MaxClients 150
ServerLimit 150
</IfModule>
或使用 mod_reqtimeout:
RequestReadTimeout header=10-20,MinRate=500
Options -Indexes
yum install mod_security -y
systemctl restart httpd
启用规则:
yum install mod_security_crs -y
编辑:
vi /etc/httpd/conf.d/mod_security.conf
SecRuleEngine On
yum install mod_evasive -y
配置示例:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 60
</IfModule>
yum install epel-release -y
yum install fail2ban -y
示例(Apache 登录暴力):
vi /etc/fail2ban/jail.d/httpd.conf
[httpd-auth]
enabled = true
filter = httpd-auth
logpath = /var/log/httpd/error_log
maxretry = 5
bantime = 3600
启动:
systemctl enable fail2ban
systemctl start fail2ban
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=1.2.3.4 reject'
firewall-cmd --reload
yum install certbot python2-certbot-apache -y
certbot --apache
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "DENY"
Header always set X-XSS-Protection "1; mode=block"
<Directory "/var/www/html/uploads">
php_admin_flag engine off
</Directory>
tail -f /var/log/httpd/access_log
grep "404" /var/log/httpd/access_log | wc -l
可配合:
goaccessatopnetdata✅ 防火墙
✅ 隐藏版本
✅ mod_security
✅ mod_evasive
✅ fail2ban
✅ HTTPS
如果你愿意,可以告诉我:
我可以给你针对性的配置模板。