allow_essential_output_traffic.sh
· 200 B · Bash
Brut
# 僅允許必要的 OUTPUT 流量
iptables -A OUTPUT -o eth0 -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT
| 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 |
allow_loopback_traffic.sh
· 99 B · Bash
Brut
iptables -A INPUT -i lo -d 127.0.0.0/8 -j ACCEPT
iptables -A OUTPUT -o lo -s 127.0.0.0/8 -j ACCEPT
| 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 |
| 3 |
bypass_dns_forwarding_for_specific_ip.sh
· 266 B · Bash
Brut
# 允許特定 IP 繞過 DNS 轉發
iptables -t nat -A PREROUTING -i eth1 -s 192.168.6.10 -p udp --dport 53 -j ACCEPT
# 其他設備的 DNS 流量轉發到指定伺服器
iptables -t nat -A PREROUTING -i eth1 -p udp --dport 53 -j DNAT --to-destination 192.168.88.1
| 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 |
| 6 |
check_network_interface_and_iptables.sh
· 266 B · Bash
Brut
# 檢查網卡是否存在
if ! ip link show $LAN_IFACE > /dev/null 2>&1; then
echo "錯誤:內網網卡 $LAN_IFACE 不存在!"
exit 1
fi
# 檢查 iptables 命令是否成功
iptables -P INPUT DROP || { echo "設置 INPUT 鏈策略失敗!"; exit 1; }
| 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; } |
| 9 |
limit_syn_flood_and_ssh_rate.sh
· 523 B · Bash
Brut
# 限制 SYN 封包速率,防止 SYN Flood 攻擊
iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT
iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -j DROP
# 使用 recent 模組限制單一 IP 的 SSH 連線速率
iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --set --name SSH_LIMIT --mask 255.255.255.255
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
| 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 |
| 8 |
restrict_ssh_access_by_subnet.sh
· 149 B · Bash
Brut
# 限制 SSH 訪問僅允許特定子網
iptables -A INPUT -p tcp --dport 22 -s 192.168.6.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP
| 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 |
| 4 |
setup_basic_and_ddos_rules.sh
· 384 B · Bash
Brut
# 定義基本規則
setup_basic_rules() {
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
# ... 其他基本規則
}
# 定義 DDoS 防護規則
setup_ddos_protection() {
iptables -N DDoS_PROTECTION
iptables -A INPUT -j DDoS_PROTECTION
# ... 其他 DDoS 防護規則
}
# 調用各個函數
setup_basic_rules
setup_ddos_protection
| 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 |
| 19 |