Last active 7 months ago

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

Revision 6cacaf2dadd63df3fd1090979ece243ca67f72f8

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
allow_loopback_traffic.sh Raw
1iptables -A INPUT -i lo -d 127.0.0.0/8 -j ACCEPT
2iptables -A OUTPUT -o lo -s 127.0.0.0/8 -j ACCEPT
3
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
setup_basic_and_ddos_rules.sh Raw
1# 定義基本規則
2setup_basic_rules() {
3 iptables -P INPUT DROP
4 iptables -P OUTPUT DROP
5 iptables -P FORWARD DROP
6 # ... 其他基本規則
7}
8
9# 定義 DDoS 防護規則
10setup_ddos_protection() {
11 iptables -N DDoS_PROTECTION
12 iptables -A INPUT -j DDoS_PROTECTION
13 # ... 其他 DDoS 防護規則
14}
15
16# 調用各個函數
17setup_basic_rules
18setup_ddos_protection
19