Zuletzt aktiv 10 months ago

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

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

2 files changed, 20 insertions

allow_loopback_traffic.sh(Datei erstellt)

@@ -0,0 +1,2 @@
1 + iptables -A INPUT -i lo -d 127.0.0.0/8 -j ACCEPT
2 + iptables -A OUTPUT -o lo -s 127.0.0.0/8 -j ACCEPT

setup_basic_and_ddos_rules.sh(Datei erstellt)

@@ -0,0 +1,18 @@
1 + # 定義基本規則
2 + setup_basic_rules() {
3 + iptables -P INPUT DROP
4 + iptables -P OUTPUT DROP
5 + iptables -P FORWARD DROP
6 + # ... 其他基本規則
7 + }
8 +
9 + # 定義 DDoS 防護規則
10 + setup_ddos_protection() {
11 + iptables -N DDoS_PROTECTION
12 + iptables -A INPUT -j DDoS_PROTECTION
13 + # ... 其他 DDoS 防護規則
14 + }
15 +
16 + # 調用各個函數
17 + setup_basic_rules
18 + setup_ddos_protection

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

2 files changed, 12 insertions

bypass_dns_forwarding_for_specific_ip.sh(Datei erstellt)

@@ -0,0 +1,5 @@
1 + # 允許特定 IP 繞過 DNS 轉發
2 + iptables -t nat -A PREROUTING -i eth1 -s 192.168.6.10 -p udp --dport 53 -j ACCEPT
3 +
4 + # 其他設備的 DNS 流量轉發到指定伺服器
5 + iptables -t nat -A PREROUTING -i eth1 -p udp --dport 53 -j DNAT --to-destination 192.168.88.1

limit_syn_flood_and_ssh_rate.sh(Datei erstellt)

@@ -0,0 +1,7 @@
1 + # 限制 SYN 封包速率,防止 SYN Flood 攻擊
2 + iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT
3 + iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -j DROP
4 +
5 + # 使用 recent 模組限制單一 IP 的 SSH 連線速率
6 + iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --set --name SSH_LIMIT --mask 255.255.255.255
7 + iptables -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

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

3 files changed, 15 insertions

allow_essential_output_traffic.sh(Datei erstellt)

@@ -0,0 +1,4 @@
1 + # 僅允許必要的 OUTPUT 流量
2 + iptables -A OUTPUT -o eth0 -p tcp --dport 80 -j ACCEPT
3 + iptables -A OUTPUT -o eth0 -p tcp --dport 443 -j ACCEPT
4 + iptables -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT

check_network_interface_and_iptables.sh(Datei erstellt)

@@ -0,0 +1,8 @@
1 + # 檢查網卡是否存在
2 + if ! ip link show $LAN_IFACE > /dev/null 2>&1; then
3 + echo "錯誤:內網網卡 $LAN_IFACE 不存在!"
4 + exit 1
5 + fi
6 +
7 + # 檢查 iptables 命令是否成功
8 + iptables -P INPUT DROP || { echo "設置 INPUT 鏈策略失敗!"; exit 1; }

restrict_ssh_access_by_subnet.sh(Datei erstellt)

@@ -0,0 +1,3 @@
1 + # 限制 SSH 訪問僅允許特定子網
2 + iptables -A INPUT -p tcp --dport 22 -s 192.168.6.0/24 -j ACCEPT
3 + iptables -A INPUT -p tcp --dport 22 -j DROP
Neuer Älter