Son aktivite 10 months ago

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

Revizyon f73c21bc661e172e26508e3306fa1086f6164adc

iptables_rules.sh Ham
1#!/bin/bash
2
3# Flush all existing rules
4iptables -F
5
6# Set default policies to drop
7iptables -P INPUT DROP
8iptables -P FORWARD DROP
9iptables -P OUTPUT ACCEPT
10
11# Allow established connections
12iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow established connections"
13
14# Allow loopback interface traffic
15iptables -A INPUT -i lo -j ACCEPT -m comment --comment "Allow loopback interface"
16
17# Allow SSH traffic (port 22)
18# iptables -A INPUT -p tcp --dport 22 -j ACCEPT -m comment --comment "Allow SSH"
19
20# Allow SSH traffic only from IPs in the taiwan_ips ipset collection
21iptables -A INPUT -p tcp --dport 22 -m set --match-set taiwan_ips src -j ACCEPT -m comment --comment "Allow SSH from Taiwan IPs"
22
23# Drop SSH traffic from other IPs
24iptables -A INPUT -p tcp --dport 22 -j DROP -m comment --comment "Drop SSH from other IPs"
25
26
27# Allow HTTP traffic (port 80)
28# iptables -A INPUT -p tcp --dport 80 -j ACCEPT -m comment --comment "Allow HTTP"
29
30# Allow HTTPS traffic (port 443)
31# iptables -A INPUT -p tcp --dport 443 -j ACCEPT -m comment --comment "Allow HTTPS"
32
33# Allow ping traffic
34iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT -m comment --comment "Allow ping"
35
36# Block SYN Flood attacks
37iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 4 -j ACCEPT -m comment --comment "Protect against SYN Flood"
38
39# Block Port Scanning
40iptables -A INPUT -p tcp --syn -m recent --name scan --set -m comment --comment "Detect port scans"
41iptables -A INPUT -p tcp --syn -m recent --name scan --update --seconds 60 --hitcount 10 -j DROP -m comment --comment "Drop port scans"
42
43# Block DoS attacks
44iptables -A INPUT -p tcp -m limit --limit 10/s --limit-burst 20 -j ACCEPT -m comment --comment "Limit incoming connections to prevent DoS"
45iptables -A INPUT -p udp -m limit --limit 10/s --limit-burst 20 -j ACCEPT -m comment --comment "Limit UDP connections to prevent DoS"
46iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 3 -j ACCEPT -m comment --comment "Limit ICMP requests to prevent DoS"
47
48# Limit UDP traffic to prevent UDP Flood attack
49iptables -A INPUT -p udp -m limit --limit 10/s --limit-burst 20 -j ACCEPT -m comment --comment "Limit UDP traffic to prevent UDP Flood"
50
51# Drop oversized ICMP packets to prevent Ping of Death attack
52iptables -A INPUT -p icmp --icmp-type echo-request -m length --length 0:1024 -j ACCEPT -m comment --comment "Drop oversized ICMP packets"
53
54# Drop packets with a source address equal to the broadcast address
55iptables -A INPUT -p icmp --icmp-type echo-request -s 255.255.255.255 -j DROP -m comment --comment "Drop ICMP packets with broadcast source address"
56
57# Drop packets with the same source and destination address
58iptables -A INPUT -s 192.168.1.1 -d 192.168.1.1 -j DROP -m comment --comment "Drop packets with same source and destination address"
59
60# Drop invalid packets
61iptables -A INPUT -m state --state INVALID -j DROP -m comment --comment "Drop invalid packets"
62
63# Drop suspicious port scanning attempts
64iptables -A INPUT -p tcp --syn -m recent --name portscan --set -m comment --comment "Track port scanning attempts"
65iptables -A INPUT -p tcp --syn -m recent --name portscan --update --seconds 60 --hitcount 10 -j DROP -m comment --comment "Drop excessive port scan attempts"
66
67# Rate limit incoming connections to mitigate DDoS
68iptables -A INPUT -p tcp -m limit --limit 20/s --limit-burst 100 -j ACCEPT -m comment --comment "Rate limit TCP connections to mitigate DDoS"
69iptables -A INPUT -p udp -m limit --limit 20/s --limit-burst 100 -j ACCEPT -m comment --comment "Rate limit UDP connections to mitigate DDoS"
70
71
72# Set NAT forwarding rules (if needed)
73# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE -m comment --comment "NAT outbound traffic"
74
75# Save rules
76# iptables-save > /etc/iptables/rules.v4