bash
# 使用fail2ban
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
禁用不必要的方法:
# Apache配置示例
<Location "/">
LimitExcept GET POST HEAD {
Deny from all
}
</Location>
# Nginx配置示例
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
隐藏服务器信息:
ServerTokens Prod
ServerSignature Off
server_tokens off;
# Nginx速率限制示例
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
location /login {
limit_req zone=login burst=3 nodelay;
# 其他配置...
}
# 使用iptables阻止IP
iptables -A INPUT -s 恶意IP -j DROP
# 使用ipset管理大量IP
ipset create blacklist hash:ip
iptables -I INPUT -m set --match-set blacklist src -j DROP
ModSecurity:
sudo apt install libapache2-mod-security2
sudo mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
Naxsi (Nginx插件):
load_module modules/ngx_http_naxsi_module.so;
# 使用logwatch分析日志
sudo apt install logwatch
sudo logwatch --output mail --format html --range yesterday
# 或使用ELK Stack进行高级分析
# 使用netstat监控异常连接
watch -n 1 'netstat -antup | grep ESTABLISHED'
# 使用iftop监控网络流量
sudo apt install iftop
sudo iftop -nNP
# 快速封禁攻击IP
sudo iptables -A INPUT -s 攻击IP -j DROP
# 分析攻击模式
sudo grep "攻击IP" /var/log/apache2/access.log | awk '{print $7}' | sort | uniq -c | sort -nr
#!/bin/bash
# 自动封禁频繁尝试登录的IP
ATTACKERS=$(grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | awk '$1 > 10 {print $2}')
for ip in $ATTACKERS; do
iptables -A INPUT -s $ip -j DROP
echo "$(date) - Banned $ip for too many login attempts" >> /var/log/security.log
done
定期安全审计:
sudo lynis audit system
保持系统更新:
sudo apt update && sudo apt upgrade -y
最小化服务原则:关闭所有不必要的服务和端口
定期备份关键数据并测试恢复流程
实施安全开发实践:对Web应用进行代码审计,防止注入等漏洞
通过综合应用这些策略,可以显著提高Linux服务器Web接口的安全性,有效防御暴力破解攻击。