Zuletzt aktiv 10 months ago

這個 iptables 防火牆腳本 用於 強化伺服器的網路安全,透過 允許合法流量(如 SSH、HTTP/HTTPS)並封鎖未授權連線,同時防禦各種攻擊(如 暴力破解、SYN Flood、埠掃描、DoS)。此外,它會 記錄異常流量 以便後續分析,並將規則儲存至 /etc/iptables/rules.v4,確保設定在重啟後仍生效。適用於 企業伺服器、內部網路防護及個人伺服器安全強化。

Änderung 4209ab5aead561f6024059a9d0e1c132a4362869

iptables_rules.sh Originalformat
1#!/bin/bash
2
3# === Basic Settings ===
4
5# Clear all existing rules
6iptables -F
7iptables -X
8iptables -Z
9
10# Set default policies: drop all incoming and forwarding traffic, allow outgoing
11iptables -P INPUT DROP
12iptables -P FORWARD DROP
13iptables -P OUTPUT ACCEPT
14
15# === Variables ===
16TAIWAN_IPSET="taiwan_ips" # Name of the IP set for Taiwan
17ALLOWED_SSH_IP="192.168.1.0/24" # Allowed SSH subnet
18
19# === Basic Allow Rules ===
20
21# Allow established and related connections
22iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow established connections"
23
24# Allow loopback (local) traffic
25iptables -A INPUT -i lo -j ACCEPT -m comment --comment "Allow local traffic"
26
27# === SSH Rules ===
28
29# Allow SSH from Taiwan IP set
30iptables -A INPUT -p tcp --dport 22 -m set --match-set $TAIWAN_IPSET src -j ACCEPT -m comment --comment "Allow SSH from Taiwan IPs"
31
32# Allow SSH from allowed internal network
33iptables -A INPUT -p tcp --dport 22 -s $ALLOWED_SSH_IP -j ACCEPT -m comment --comment "Allow SSH from internal network"
34
35# Limit SSH attempts to prevent brute force attacks
36iptables -A INPUT -p tcp --dport 22 -m recent --name ssh_bruteforce --set
37iptables -A INPUT -p tcp --dport 22 -m recent --name ssh_bruteforce --update --seconds 60 --hitcount 5 -j DROP -m comment --comment "Limit SSH attempts"
38
39# Drop SSH from other sources
40iptables -A INPUT -p tcp --dport 22 -j DROP -m comment --comment "Drop unauthorized SSH traffic"
41
42# === HTTP/HTTPS Traffic ===
43
44# Allow HTTP (port 80)
45iptables -A INPUT -p tcp --dport 80 -j ACCEPT -m comment --comment "Allow HTTP traffic"
46
47# Allow HTTPS (port 443)
48iptables -A INPUT -p tcp --dport 443 -j ACCEPT -m comment --comment "Allow HTTPS traffic"
49
50# === Attack Prevention ===
51
52# Protect against SYN Flood
53iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 4 -j ACCEPT -m comment --comment "Protect from SYN Flood"
54
55# Detect and drop port scans
56iptables -A INPUT -p tcp --syn -m recent --name portscan --set -m comment --comment "Detect port scans"
57iptables -A INPUT -p tcp --syn -m recent --name portscan --update --seconds 60 --hitcount 10 -j DROP -m comment --comment "Drop port scans"
58
59# Limit TCP, UDP, and ICMP traffic to prevent DoS attacks
60iptables -A INPUT -p tcp -m limit --limit 10/s --limit-burst 20 -j ACCEPT -m comment --comment "Limit TCP traffic for DoS"
61iptables -A INPUT -p udp -m limit --limit 10/s --limit-burst 20 -j ACCEPT -m comment --comment "Limit UDP traffic for DoS"
62iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 3 -j ACCEPT -m comment --comment "Limit ICMP requests for DoS"
63
64# Drop large ICMP packets to prevent Ping of Death
65iptables -A INPUT -p icmp --icmp-type echo-request -m length --length 0:1024 -j ACCEPT -m comment --comment "Prevent Ping of Death"
66
67# Drop packets with the same source and destination IP
68iptables -A INPUT -s 192.168.1.1 -d 192.168.1.1 -j DROP -m comment --comment "Drop same source and destination"
69
70# Drop invalid packets
71iptables -A INPUT -m state --state INVALID -j DROP -m comment --comment "Drop invalid packets"
72
73# === Logging ===
74
75# Log dropped packets
76iptables -A INPUT -m limit --limit 2/min -j LOG --log-prefix "Dropped: " --log-level 4 -m comment --comment "Log dropped packets"
77
78# === Save Rules ===
79
80# Save rules to /etc/iptables/rules.v4
81iptables-save > /etc/iptables/rules.v4
82