|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | +IFS=$'\n\t' |
| 4 | + |
| 5 | +# 1. Extract Docker DNS info BEFORE any flushing |
| 6 | +DOCKER_DNS_RULES=$(iptables-save -t nat | grep "127\.0\.0\.11" || true) |
| 7 | + |
| 8 | +# Flush existing rules and delete existing ipsets |
| 9 | +iptables -F |
| 10 | +iptables -X |
| 11 | +iptables -t nat -F |
| 12 | +iptables -t nat -X |
| 13 | +iptables -t mangle -F |
| 14 | +iptables -t mangle -X |
| 15 | +ipset destroy allowed-domains 2>/dev/null || true |
| 16 | + |
| 17 | +# 2. Selectively restore ONLY internal Docker DNS resolution |
| 18 | +if [ -n "$DOCKER_DNS_RULES" ]; then |
| 19 | + echo "Restoring Docker DNS rules..." |
| 20 | + iptables -t nat -N DOCKER_OUTPUT 2>/dev/null || true |
| 21 | + iptables -t nat -N DOCKER_POSTROUTING 2>/dev/null || true |
| 22 | + echo "$DOCKER_DNS_RULES" | xargs -L 1 iptables -t nat |
| 23 | +else |
| 24 | + echo "No Docker DNS rules to restore" |
| 25 | +fi |
| 26 | + |
| 27 | +# Allow DNS, SSH, and loopback before any restrictions |
| 28 | +iptables -A OUTPUT -p udp --dport 53 -j ACCEPT |
| 29 | +iptables -A INPUT -p udp --sport 53 -j ACCEPT |
| 30 | +iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT |
| 31 | +iptables -A INPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT |
| 32 | +iptables -A INPUT -i lo -j ACCEPT |
| 33 | +iptables -A OUTPUT -o lo -j ACCEPT |
| 34 | + |
| 35 | +# Create ipset with CIDR support |
| 36 | +ipset create allowed-domains hash:net |
| 37 | + |
| 38 | +# Fetch GitHub meta and add their IP ranges |
| 39 | +echo "Fetching GitHub IP ranges..." |
| 40 | +gh_ranges=$(curl -s https://api.github.com/meta) |
| 41 | +if [ -z "$gh_ranges" ]; then |
| 42 | + echo "ERROR: Failed to fetch GitHub IP ranges" |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | +if ! echo "$gh_ranges" | jq -e '.web and .api and .git' >/dev/null; then |
| 46 | + echo "ERROR: GitHub API response missing required fields" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | +echo "Processing GitHub IPs..." |
| 50 | +while read -r cidr; do |
| 51 | + if [[ ! "$cidr" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$ ]]; then |
| 52 | + echo "ERROR: Invalid CIDR range from GitHub meta: $cidr" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + echo "Adding GitHub range $cidr" |
| 56 | + ipset add allowed-domains "$cidr" |
| 57 | +done < <(echo "$gh_ranges" | jq -r '(.web + .api + .git)[]' | aggregate -q) |
| 58 | + |
| 59 | +# Resolve and add allowed domains |
| 60 | +for domain in \ |
| 61 | + "api.anthropic.com" \ |
| 62 | + "sentry.io" \ |
| 63 | + "statsig.anthropic.com" \ |
| 64 | + "statsig.com" \ |
| 65 | + "registry.npmjs.org" \ |
| 66 | + "pypi.org" \ |
| 67 | + "files.pythonhosted.org" \ |
| 68 | + "storage.googleapis.com" \ |
| 69 | + "oauth2.googleapis.com" \ |
| 70 | + "accounts.google.com" \ |
| 71 | + "dl.google.com"; do |
| 72 | + echo "Resolving $domain..." |
| 73 | + ips=$(dig +noall +answer A "$domain" | awk '$4 == "A" {print $5}') |
| 74 | + if [ -z "$ips" ]; then |
| 75 | + echo "ERROR: Failed to resolve $domain" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + while read -r ip; do |
| 79 | + if [[ ! "$ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then |
| 80 | + echo "ERROR: Invalid IP from DNS for $domain: $ip" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + echo "Adding $ip for $domain" |
| 84 | + ipset add allowed-domains "$ip" 2>/dev/null || true # ignore duplicates |
| 85 | + done < <(echo "$ips") |
| 86 | +done |
| 87 | + |
| 88 | +# Allow host network |
| 89 | +HOST_IP=$(ip route | grep default | cut -d" " -f3) |
| 90 | +if [ -z "$HOST_IP" ]; then |
| 91 | + echo "ERROR: Failed to detect host IP" |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | +HOST_NETWORK=$(echo "$HOST_IP" | sed "s/\.[0-9]*$/.0\/24/") |
| 95 | +echo "Host network detected as: $HOST_NETWORK" |
| 96 | +iptables -A INPUT -s "$HOST_NETWORK" -j ACCEPT |
| 97 | +iptables -A OUTPUT -d "$HOST_NETWORK" -j ACCEPT |
| 98 | + |
| 99 | +# Set default DROP policies |
| 100 | +iptables -P INPUT DROP |
| 101 | +iptables -P FORWARD DROP |
| 102 | +iptables -P OUTPUT DROP |
| 103 | + |
| 104 | +# Allow established/related connections |
| 105 | +iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 106 | +iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| 107 | + |
| 108 | +# Allow outbound to whitelisted IPs |
| 109 | +iptables -A OUTPUT -m set --match-set allowed-domains dst -j ACCEPT |
| 110 | + |
| 111 | +# Reject everything else with feedback |
| 112 | +iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited |
| 113 | + |
| 114 | +echo "Firewall configuration complete" |
| 115 | + |
| 116 | +# Verify: blocked |
| 117 | +if curl --connect-timeout 5 https://example.com >/dev/null 2>&1; then |
| 118 | + echo "ERROR: Firewall verification failed - reached https://example.com" |
| 119 | + exit 1 |
| 120 | +else |
| 121 | + echo "Firewall verification passed - example.com is blocked" |
| 122 | +fi |
| 123 | + |
| 124 | +# Verify: allowed |
| 125 | +if ! curl --connect-timeout 5 https://api.github.com/zen >/dev/null 2>&1; then |
| 126 | + echo "ERROR: Firewall verification failed - cannot reach https://api.github.com" |
| 127 | + exit 1 |
| 128 | +else |
| 129 | + echo "Firewall verification passed - api.github.com is reachable" |
| 130 | +fi |
0 commit comments