Linux 网络安全加固与防护实战
一、网络安全威胁模型与加固原则
常见威胁:
加固原则:
二、防火墙深化配置(iptables / nftables 进阶)
最小化暴露:
# nftables 示例
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iif "lo" accept
ct state established,related accept
tcp dport {22, 80, 443} accept
icmp typeecho-request limit rate 5/second accept
counter log prefix "DROP-INPUT: " drop
}
}
高级防护:
- 限速特定端口:
limit rate 10/minute - 阻止常见扫描:
--tcp-flags ALL NONE 等无效标志
动态封禁:Fail2Ban + iptables 或 nftables。
三、访问控制与 SSH 安全强化
SSH 最佳实践:
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 20
AllowUsers user1@192.168.10.0/24
- 端口改跳(如 2222)+ 结合 WireGuard(第八篇)
其他访问:
四、加密传输与证书管理
- 全站 HTTPS:Let’s Encrypt + Nginx/HAProxy 自动续期
- 内部服务:mTLS(Nginx Stream 或 Envoy)
- VPN 加密:WireGuard / IPsec(第八篇)
证书自动化:
certbot certonly --nginx -d example.com
五、入侵检测与监控
工具推荐:
- OSSEC / Wazuh:主机入侵检测(HIDS)
- Snort / Suricata:网络入侵检测(NIDS)
auditd 规则示例(/etc/audit/rules.d/audit.rules):
-w /etc/ssh/sshd_config -p wa -k sshconfig
-a always,exit -F arch=b64 -S connect -k network_connect
Prometheus + Grafana 安全仪表盘:
六、DDoS 与应用层防护
主机层面:
应用层面:
- WAF(ModSecurity + OWASP 规则)
- Rate Limiting(Nginx limit_req)
云环境:利用厂商 DDoS 清洗能力 + 弹性伸缩。
七、日志审计与取证
集中日志:
- rsyslog / journald → ELK / Loki
- 关键日志保护:immutable 属性 + 远程 syslog
分析技巧:
journalctl -u ssh -xe
grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c
SIEM:集成 Wazuh + Elastic 实现告警关联。
八、内核与系统级安全加固
sysctl 安全参数:
net.ipv4.conf.all.rp_filter = 1 # 反向路径过滤
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.tcp_syncookies = 1
kernel.randomize_va_space = 2 # ASLR
SELinux / AppArmor:
其他:
- 定期
yum update / apt upgrade - 关闭不必要服务:
systemctl disable --now ...
九、生产加固 checklist 与案例
加固清单:
案例:某服务器被挖矿
- 现象:CPU 100%、异常 outbound 流量
- 排查:
netstat/ss 找可疑连接 → lsof -i → 审计日志
另一个案例:SYN Flood 导致服务不可用
- 解决:syncookies + 限速 + 上游清洗