Last active 7 months ago

快速配置 iptables,確保流量安全,簡單搞定網絡管理。

Revision c859455b28d0e5f5405961f1d967262e38db5022

allow_essential_output_traffic.sh Raw
1# 僅允許必要的 OUTPUT 流量
2iptables -A OUTPUT -o eth0 -p tcp --dport 80 -j ACCEPT
3iptables -A OUTPUT -o eth0 -p tcp --dport 443 -j ACCEPT
4iptables -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT
bypass_dns_forwarding_for_specific_ip.sh Raw
1# 允許特定 IP 繞過 DNS 轉發
2iptables -t nat -A PREROUTING -i eth1 -s 192.168.6.10 -p udp --dport 53 -j ACCEPT
3
4# 其他設備的 DNS 流量轉發到指定伺服器
5iptables -t nat -A PREROUTING -i eth1 -p udp --dport 53 -j DNAT --to-destination 192.168.88.1
6
check_network_interface_and_iptables.sh Raw
1# 檢查網卡是否存在
2if ! ip link show $LAN_IFACE > /dev/null 2>&1; then
3 echo "錯誤:內網網卡 $LAN_IFACE 不存在!"
4 exit 1
5fi
6
7# 檢查 iptables 命令是否成功
8iptables -P INPUT DROP || { echo "設置 INPUT 鏈策略失敗!"; exit 1; }
9
limit_syn_flood_and_ssh_rate.sh Raw
1# 限制 SYN 封包速率,防止 SYN Flood 攻擊
2iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT
3iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -j DROP
4
5# 使用 recent 模組限制單一 IP 的 SSH 連線速率
6iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --set --name SSH_LIMIT --mask 255.255.255.255
7iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --update --seconds 60 --hitcount 4 --name SSH_LIMIT --mask 255.255.255.255 -j DROP
8
restrict_ssh_access_by_subnet.sh Raw
1# 限制 SSH 訪問僅允許特定子網
2iptables -A INPUT -p tcp --dport 22 -s 192.168.6.0/24 -j ACCEPT
3iptables -A INPUT -p tcp --dport 22 -j DROP
4