|
2 | 2 | # firewall-status.sh - 防火牆 vs 實際端口 對比檢查 |
3 | 3 | # curl -fsSL https://raw.githubusercontent.com/ExpTechTW/API/refs/heads/main/scripts/firewall-status.sh | sudo bash |
4 | 4 |
|
5 | | -VERSION="1.1.0" |
| 5 | +VERSION="1.1.1" |
6 | 6 |
|
7 | 7 | set -e |
8 | 8 |
|
@@ -38,16 +38,46 @@ if [ -n "$TRUSTED" ]; then |
38 | 38 | done |
39 | 39 | fi |
40 | 40 |
|
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) |
42 | 42 | INET_RULES=$(nft list table inet filter 2>/dev/null || true) |
43 | 43 | if [ -z "$INET_RULES" ]; then |
44 | | - # no inet filter table, try ip filter (iptables-nft) excluding Docker chains |
45 | 44 | INET_RULES=$(nft list table ip filter 2>/dev/null | grep -v "DOCKER" || true) |
46 | 45 | 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 |
50 | 79 | fi |
| 80 | + |
51 | 81 | PUBLIC_RULES=$(echo "$INET_RULES" | grep "dport" | grep -v "saddr" || true) |
52 | 82 | PRIVATE_RULES=$(echo "$INET_RULES" | grep "dport" | grep "saddr" || true) |
53 | 83 |
|
|
0 commit comments