Skip to content

Commit 4d97f45

Browse files
committed
fix: firewall-status.sh
1 parent 8fa31b9 commit 4d97f45

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

scripts/firewall-status.sh

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# firewall-status.sh - 防火牆 vs 實際端口 對比檢查
33
# curl -fsSL https://raw.githubusercontent.com/ExpTechTW/API/refs/heads/main/scripts/firewall-status.sh | sudo bash
44

5-
VERSION="1.1.0"
5+
VERSION="1.1.1"
66

77
set -e
88

@@ -38,16 +38,46 @@ if [ -n "$TRUSTED" ]; then
3838
done
3939
fi
4040

41-
# parse firewall rules (from inet filter table, fallback to ip filter excluding Docker tables)
41+
# parse firewall rules (from inet filter table, fallback to ip filter excluding Docker chains)
4242
INET_RULES=$(nft list table inet filter 2>/dev/null || true)
4343
if [ -z "$INET_RULES" ]; then
44-
# no inet filter table, try ip filter (iptables-nft) excluding Docker chains
4544
INET_RULES=$(nft list table ip filter 2>/dev/null | grep -v "DOCKER" || true)
4645
fi
47-
if [ -z "$INET_RULES" ]; then
48-
# last resort: full ruleset excluding Docker/docker-bridges tables
49-
INET_RULES=$(nft list ruleset 2>/dev/null | sed '/^table.*docker/,/^}/d' | sed '/DOCKER/d' || true)
46+
if [ -z "$(echo "$INET_RULES" | grep "dport" || true)" ]; then
47+
echo ""
48+
echo "!! NO FIREWALL RULES DETECTED !!"
49+
echo "!! All ports are exposed to the internet !!"
50+
echo ""
51+
52+
# show listening ports
53+
declare -A PORT_PROCESS
54+
while IFS= read -r line; do
55+
port=$(echo "$line" | awk '{split($5,a,":"); print a[length(a)]}')
56+
proc=$(echo "$line" | grep -oP 'users:\(\("\K[^"]+' || echo "unknown")
57+
addr=$(echo "$line" | awk '{print $5}')
58+
if [[ "$addr" == 127.0.0.* ]] || [[ "$addr" == "::1:"* ]] || [[ "$addr" == *"%lo:"* ]]; then
59+
continue
60+
fi
61+
PORT_PROCESS[$port]="$proc"
62+
done < <(ss -tulnp | tail -n +2)
63+
64+
LISTEN_PORTS=$(printf '%s\n' "${!PORT_PROCESS[@]}" | sort -un)
65+
TOTAL=$(echo "$LISTEN_PORTS" | grep -c . || true)
66+
67+
echo "[EXPOSED] no firewall"
68+
printf "%-8s %-18s %s\n" "PORT" "PROCESS" "STATUS"
69+
printf "%-8s %-18s %s\n" "----" "-------" "------"
70+
for port in $LISTEN_PORTS; do
71+
printf "%-8s %-18s %s\n" "$port" "${PORT_PROCESS[$port]:-unknown}" "!! EXPOSED"
72+
done
73+
74+
echo ""
75+
echo "======================================"
76+
echo " Total: $TOTAL | Exposed: $TOTAL"
77+
echo "======================================"
78+
exit 1
5079
fi
80+
5181
PUBLIC_RULES=$(echo "$INET_RULES" | grep "dport" | grep -v "saddr" || true)
5282
PRIVATE_RULES=$(echo "$INET_RULES" | grep "dport" | grep "saddr" || true)
5383

0 commit comments

Comments
 (0)