认证与授权漏洞
注入攻击
跨站脚本(XSS)
跨站请求伪造(CSRF)
不安全的配置
# OWASP ZAP
docker run -v $(pwd):/zap/wrk/:rw -t owasp/zap2docker-stable zap-baseline.py \
-t https://your-web-interface/ -g gen.conf -r testreport.html
# Nikto
nikto -h https://your-web-interface/ -output nikto_scan.html
# Nmap NSE脚本
nmap --script http-vuln* -p 80,443 your-server-ip
# OWASP Amass
amass enum -active -d yourdomain.com
# Postman/Newman安全测试
newman run security_test_collection.json
# Nginx示例安全配置
server {
# 禁用不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
# 安全头部
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Content-Security-Policy "default-src 'self'";
add_header Referrer-Policy "strict-origin-when-cross-origin";
# 禁用目录列表
autoindex off;
# 限制文件上传大小
client_max_body_size 1M;
}
# 安装ModSecurity WAF
sudo apt install libapache2-mod-security2
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
# 配置OWASP核心规则集
git clone https://github.com/coreruleset/coreruleset /etc/modsecurity/crs
#!/bin/bash
# 定期安全检查脚本
# 检查异常进程
ps aux | grep -E '(wget|curl|nc|ncat|netcat|perl|python|ruby|bash|sh)'
# 检查异常连接
netstat -tulnp | grep -E '(4444|5555|6666|7777|8888|9999)'
# 检查文件完整性
find /var/www/html -type f -exec md5sum {} \; > /tmp/web_checksum.txt
diff /tmp/web_checksum.txt /opt/security/web_checksum.baseline
# 检查日志中的攻击尝试
grep -E '(union.*select|%27|%22|%3Cscript|etc/passwd)' /var/log/nginx/access.log
实时监控
# 使用Fail2Ban防止暴力破解
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 配置监控规则
[nginx-badbots]
enabled = true
port = http,https
filter = nginx-badbots
logpath = /var/log/nginx/access.log
maxretry = 2
日志分析
# 使用GoAccess分析访问日志
goaccess /var/log/nginx/access.log --log-format=COMBINED -o report.html
应急响应
# 隔离受影响系统
iptables -A INPUT -s <attacker_ip> -j DROP
# 取证收集
tar czvf /tmp/incident_$(date +%F).tar.gz /var/log/ /var/www/html/
# 快速修补
git -C /var/www/html reset --hard HEAD
chown -R www-data:www-data /var/www/html
定期更新
# 自动化安全更新
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
渗透测试
# 使用Metasploit框架测试
msfconsole
> use auxiliary/scanner/http/http_version
> set RHOSTS your-server-ip
> run
安全编码实践
通过以上综合措施,您可以显著提高Linux服务器上Web接口的安全性,减少潜在攻击面,并建立有效的监控和响应机制。