# 定期更新系统
sudo apt update && sudo apt upgrade -y # Debian/Ubuntu
sudo yum update -y # CentOS/RHEL
# 安装并配置UFW
sudo apt install ufw -y
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
# 在/etc/nginx/nginx.conf的http块中添加
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
# 在具体server配置中添加
server {
...
limit_req zone=one burst=20 nodelay;
limit_conn addr 10;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
# XSS防护
add_header X-XSS-Protection "1; mode=block";
# 禁用内容类型嗅探
add_header X-Content-Type-Options "nosniff";
# CSP策略
add_header Content-Security-Policy "default-src 'self';";
}
# 在/etc/apache2/apache2.conf或虚拟主机配置中添加
<IfModule mod_security2.c>
SecRuleEngine On
SecRequestBodyLimit 8388608
SecRequestBodyNoFilesLimit 131072
SecRequestBodyInMemoryLimit 131072
</IfModule>
# 添加安全头
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set X-Content-Type-Options "nosniff"
Header always set Content-Security-Policy "default-src 'self'"
# 安装Fail2Ban
sudo apt install fail2ban -y
# 创建自定义jail配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
编辑/etc/fail2ban/jail.local
添加Web防护规则:
[nginx-botsearch]
enabled = true
port = http,https
filter = nginx-botsearch
logpath = /var/log/nginx/access.log
maxretry = 5
findtime = 600
bantime = 86400
# 对于Debian/Ubuntu
sudo apt install libapache2-mod-security2 -y
sudo mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
# 启用OWASP核心规则集
sudo git clone https://github.com/coreruleset/coreruleset /etc/modsecurity/crs/
sudo cp /etc/modsecurity/crs/crs-setup.conf.example /etc/modsecurity/crs/crs-setup.conf
# 使用iptables进行连接限制
sudo iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP
sudo iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 50 -j DROP
# 安装并配置GoAccess进行实时日志分析
sudo apt install goaccess -y
goaccess /var/log/nginx/access.log --log-format=COMBINED --real-time-html --output=/var/www/html/report.html
# 使用logwatch进行日志监控
sudo apt install logwatch -y
sudo logwatch --output mail --mailto admin@example.com --detail high
# 使用Lynis进行安全审计
sudo apt install lynis -y
sudo lynis audit system
通过以上综合措施,可以显著提高Linux服务器上Web接口对Botnet攻击的防护能力。