fix(egress): address review on the allowlist enforcement

- Flush artex_allow at startup: a host removed from the lists no longer stays
  reachable until the ipset timeout.
- Fail-closed setup (enforce): install a blanket subnet-egress deny before the
  allowlist is built and remove it only after setup completes; an ERR trap
  (set -E) leaves it in place and exits non-zero on failure, with a FATAL log.
- IPv6: document the gap, warn when unfiltered, and add an ip6tables deny when
  ARTEX_SUBNET6 is set.
- Gateway/host-proxy bypass: document it (container->gateway goes through host
  INPUT, not DOCKER-USER) and add an opt-in EGRESS_FILTER_GATEWAY INPUT filter.
- README: make clear the allowlist only acts in enforce; monitor is observe-only.
- Add egress-guard/selftest.sh and confirm in a real container (own netns):
  flush, rules, enforce REJECT, DNS redirect, and fail-closed all pass (10/10).
This commit is contained in:
Luis
2026-11-21 12:28:46 -05:00
parent e91870a2ae
commit 91a6a86332
3 changed files with 162 additions and 14 deletions
+21 -3
View File
@@ -22,9 +22,16 @@ edits the host firewall, so read this before enabling it.
## Modes (`EGRESS_MODE` in `.env`)
| Mode | Effect |
|---|---|
| `monitor` (default) | only logs what would be blocked |
| `enforce` | blocks |
| `off` | no rules; DNS only |
| `monitor` (default) | **observe only** — logs what would be blocked, blocks nothing |
| `enforce` | the allowlist actually blocks |
| `off` | no firewall rules; DNS redirect only |
**The allowlist only restricts egress in `enforce`.** The default `monitor` is a
dry run for calibration — it protects nothing. Set `EGRESS_MODE=enforce` (and
bring the service up again) for the guard to have any effect. In `enforce`,
setup is **fail-closed**: a blanket deny is installed before the allowlist is
built, so if setup fails the ARTEX container loses outbound connectivity (except
DNS) until the guard restarts cleanly.
## Operation
- New engagement: edit `lists/scope-domains.txt` and `lists/scope-cidrs.txt`, then
@@ -36,6 +43,17 @@ edits the host firewall, so read this before enabling it.
(fail-closed).
## Known gaps
- **IPv6 is not filtered.** The allowlist is IPv4-only (ipset `hash:ip`/`hash:net`).
Keep the ARTEX network IPv4-only (do not set `enable_ipv6` on it) — then the
container has no IPv6 address and the gap is latent. If you do enable IPv6, set
`ARTEX_SUBNET6` so `enforce` installs an `ip6tables` deny for it; the guard logs
a warning when IPv6 is left unfiltered.
- **Traffic to the host/gateway bypasses the filter.** Container→gateway traffic
goes through the host `INPUT` chain, not `DOCKER-USER` (`FORWARD`), so a proxy
bound on the gateway IP is reachable. Don't run a forward proxy on the gateway,
or set `EGRESS_FILTER_GATEWAY=1` to have `enforce` add an `INPUT` rule that drops
everything from the subnet to the gateway except DNS (off by default — it edits
the host `INPUT` chain).
- Exfiltration through an allowed destination (a GitHub gist, OAST servers, an
in-scope host the attacker controls).
- Data hidden in DNS queries (slow; visible in `logs/dnsmasq.log`).
+88 -11
View File
@@ -1,15 +1,27 @@
#!/usr/bin/env bash
# egress-guard entrypoint.
# EGRESS_MODE monitor (default: log only) | enforce (log + reject) | off (no rules, DNS only)
# ARTEX_SUBNET the ARTEX docker network (must match docker-compose.yml)
# ARTEX_GATEWAY bridge gateway IP; dnsmasq listens here
# UPSTREAM_DNS comma-separated upstream resolvers; empty = host /etc/resolv.conf
# IPSET_TIMEOUT seconds a resolved IP stays allowed after its last lookup (default 21600)
set -euo pipefail
# EGRESS_MODE monitor (default: log only) | enforce (log + reject) | off (DNS only)
# ARTEX_SUBNET the ARTEX docker network (must match docker-compose.yml)
# ARTEX_GATEWAY bridge gateway IP; dnsmasq listens here
# ARTEX_SUBNET6 optional IPv6 subnet; when set, enforce also drops its egress
# EGRESS_FILTER_GATEWAY 1 = in enforce, also drop non-DNS traffic from the subnet to the
# host/gateway (closes the host-proxy bypass). Off by default because
# it edits the host INPUT chain. See README "Known gaps".
# UPSTREAM_DNS comma-separated upstream resolvers; empty = host /etc/resolv.conf
# IPSET_TIMEOUT seconds a resolved IP stays allowed after its last lookup (default 21600)
#
# Fail-closed (enforce): a blanket deny for the subnet's egress is installed BEFORE the allowlist
# is built and removed only after setup completes. If any setup step fails, the ERR trap leaves
# that deny in place and exits non-zero, so Docker restarts the guard while traffic stays blocked.
# The operator then sees the ARTEX container lose outbound connectivity (except DNS) until the
# guard comes up cleanly. In monitor/off mode a setup failure is non-fatal by design (log only).
set -Eeuo pipefail # -E so the ERR trap fires for failures inside functions (apply_rules)
MODE="${EGRESS_MODE:-monitor}"
SUBNET="${ARTEX_SUBNET:?ARTEX_SUBNET required}"
GW="${ARTEX_GATEWAY:?ARTEX_GATEWAY required}"
SUBNET6="${ARTEX_SUBNET6:-}"
FILTER_GW="${EGRESS_FILTER_GATEWAY:-0}"
TIMEOUT="${IPSET_TIMEOUT:-21600}"
LISTS=/etc/egress-guard
LOGDIR=/var/log/egress
@@ -17,6 +29,11 @@ CONF=/run/egress-guard/dnsmasq.conf
IPT=iptables
command -v iptables-nft >/dev/null 2>&1 && IPT=iptables-nft
[ -n "${IPTABLES_BIN:-}" ] && IPT="$IPTABLES_BIN"
IP6T=ip6tables
command -v ip6tables-nft >/dev/null 2>&1 && IP6T=ip6tables-nft
[ -n "${IP6TABLES_BIN:-}" ] && IP6T="$IP6TABLES_BIN"
BOOT_CMT="artex-egress-bootstrap-deny"
log() { echo "[egress-guard] $*"; }
@@ -25,13 +42,54 @@ case "$MODE" in monitor|enforce|off) ;; *) log "invalid EGRESS_MODE=$MODE"; exit
# Read a list file: strip comments/blank lines, lowercase, drop leading dots/wildcards.
read_list() { [ -f "$1" ] || return 0; sed -e 's/#.*//' -e 's/[[:space:]]//g' "$1" | tr 'A-Z' 'a-z' | sed -e 's/^\*\.//' -e 's/^\.//' | grep -v '^$' || true; }
ensure_docker_user() {
# DOCKER-USER exists whenever dockerd runs; create it only for standalone tests.
$IPT -L DOCKER-USER -n >/dev/null 2>&1 || { $IPT -N DOCKER-USER; $IPT -I FORWARD -j DOCKER-USER; }
if [ -n "$SUBNET6" ] && command -v "$IP6T" >/dev/null 2>&1; then
$IP6T -L DOCKER-USER -n >/dev/null 2>&1 || { $IP6T -N DOCKER-USER; $IP6T -I FORWARD -j DOCKER-USER; }
fi
}
bootstrap_deny() {
# enforce only: block the subnet's egress up-front so a partial setup fails closed.
[ "$MODE" = enforce ] || return 0
$IPT -I DOCKER-USER 1 -s "$SUBNET" ! -d "$SUBNET" -m comment --comment "$BOOT_CMT" -j DROP
if [ -n "$SUBNET6" ] && command -v "$IP6T" >/dev/null 2>&1; then
$IP6T -I DOCKER-USER 1 -s "$SUBNET6" -m comment --comment "$BOOT_CMT" -j DROP 2>/dev/null \
|| log "WARN: ip6tables unavailable; IPv6 egress NOT filtered"
elif command -v "$IP6T" >/dev/null 2>&1; then
log "WARN: ARTEX_SUBNET6 unset; IPv6 egress is NOT filtered (keep the ARTEX network IPv4-only)"
fi
}
remove_bootstrap_deny() {
[ "$MODE" = enforce ] || return 0
while $IPT -C DOCKER-USER -s "$SUBNET" ! -d "$SUBNET" -m comment --comment "$BOOT_CMT" -j DROP 2>/dev/null; do
$IPT -D DOCKER-USER -s "$SUBNET" ! -d "$SUBNET" -m comment --comment "$BOOT_CMT" -j DROP
done
[ -n "$SUBNET6" ] && command -v "$IP6T" >/dev/null 2>&1 && \
while $IP6T -C DOCKER-USER -s "$SUBNET6" -m comment --comment "$BOOT_CMT" -j DROP 2>/dev/null; do
$IP6T -D DOCKER-USER -s "$SUBNET6" -m comment --comment "$BOOT_CMT" -j DROP
done
return 0
}
cleanup_rules() {
# Leave any bootstrap deny untouched here: cleanup runs on stop (on_term), where removing it
# is correct, and on startup before setup, where re-adding happens in bootstrap_deny.
remove_bootstrap_deny
# filter table: detach and delete our chain
while $IPT -C DOCKER-USER -s "$SUBNET" ! -d "$SUBNET" -j ARTEX-EGRESS 2>/dev/null; do
$IPT -D DOCKER-USER -s "$SUBNET" ! -d "$SUBNET" -j ARTEX-EGRESS
done
$IPT -F ARTEX-EGRESS 2>/dev/null || true
$IPT -X ARTEX-EGRESS 2>/dev/null || true
# optional host INPUT filter for the gateway
while $IPT -C INPUT -s "$SUBNET" -d "$GW" -j ARTEX-EGRESS-HOST 2>/dev/null; do
$IPT -D INPUT -s "$SUBNET" -d "$GW" -j ARTEX-EGRESS-HOST
done
$IPT -F ARTEX-EGRESS-HOST 2>/dev/null || true
$IPT -X ARTEX-EGRESS-HOST 2>/dev/null || true
# nat table: DNS redirect
while $IPT -t nat -C PREROUTING -s "$SUBNET" -j ARTEX-DNS 2>/dev/null; do
$IPT -t nat -D PREROUTING -s "$SUBNET" -j ARTEX-DNS
@@ -42,6 +100,8 @@ cleanup_rules() {
apply_rules() {
ipset create artex_allow hash:ip timeout "$TIMEOUT" -exist
ipset flush artex_allow # drop IPs allowed in a previous run: a host removed from the
# lists must not stay reachable until the ipset timeout expires.
ipset create artex_scope hash:net -exist
ipset flush artex_scope
local n=0
@@ -50,11 +110,6 @@ apply_rules() {
[ "$MODE" = off ] && { log "mode off: no firewall rules"; return; }
# DOCKER-USER exists whenever dockerd runs; create it only for standalone tests.
if ! $IPT -L DOCKER-USER -n >/dev/null 2>&1; then
$IPT -N DOCKER-USER; $IPT -I FORWARD -j DOCKER-USER
fi
# Redirect every DNS query leaving the ARTEX network to dnsmasq, so tools with
# hard-coded resolvers (dnsx, bbot, dig @8.8.8.8) still populate the allowlist.
$IPT -t nat -N ARTEX-DNS
@@ -75,6 +130,19 @@ apply_rules() {
$IPT -A ARTEX-EGRESS -j RETURN
fi
$IPT -I DOCKER-USER 1 -s "$SUBNET" ! -d "$SUBNET" -j ARTEX-EGRESS
# Optional: close the host-proxy bypass. Traffic from the container to the host/gateway goes
# through the host INPUT chain, not DOCKER-USER (FORWARD), so a proxy bound on the gateway is
# otherwise reachable. Off by default because it edits the host INPUT chain.
if [ "$MODE" = enforce ] && [ "$FILTER_GW" = 1 ]; then
$IPT -N ARTEX-EGRESS-HOST 2>/dev/null || $IPT -F ARTEX-EGRESS-HOST
$IPT -A ARTEX-EGRESS-HOST -p udp --dport 53 -j RETURN
$IPT -A ARTEX-EGRESS-HOST -p tcp --dport 53 -j RETURN
$IPT -A ARTEX-EGRESS-HOST -j REJECT --reject-with icmp-port-unreachable
$IPT -I INPUT 1 -s "$SUBNET" -d "$GW" -j ARTEX-EGRESS-HOST
log "gateway filter on: only DNS to $GW allowed from the subnet"
fi
log "firewall rules applied (mode=$MODE, subnet=$SUBNET)"
}
@@ -102,10 +170,19 @@ write_dnsmasq_conf() {
on_term() { log "stopping"; [ -n "${DNSMASQ_PID:-}" ] && kill "$DNSMASQ_PID" 2>/dev/null || true; cleanup_rules; exit 0; }
trap on_term TERM INT
# Setup is fail-closed in enforce: install the blanket deny first, and keep it if anything below
# throws. The ERR trap fires under `set -e` before the shell exits.
on_setup_err() { local rc=$?; log "FATAL: setup failed (rc=$rc); fail-closed deny left in place, exiting for restart"; exit "$rc"; }
trap on_setup_err ERR
cleanup_rules
ensure_docker_user
bootstrap_deny
apply_rules
remove_bootstrap_deny
write_dnsmasq_conf
dnsmasq --test -C "$CONF"
trap - ERR # setup done; a later dnsmasq exit is handled below, not as a fatal setup error.
dnsmasq -k -C "$CONF" &
DNSMASQ_PID=$!
log "dnsmasq listening on $GW:53 (pid $DNSMASQ_PID)"
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# egress-guard self-test. Builds nothing; run it inside the image, in the container's
# OWN network namespace (NOT network_mode host), so it exercises the real ipset + iptables
# without touching the host firewall:
#
# docker build -t artex-egress-guard:local ./egress-guard
# docker run --rm --cap-add NET_ADMIN --cap-add NET_RAW \
# -v "$PWD/egress-guard/selftest.sh:/selftest.sh:ro" \
# --entrypoint bash artex-egress-guard:local /selftest.sh
#
# Checks: startup flush of a stale allow IP (#1), rules applied, enforce REJECT (#5),
# DNS redirect, and fail-closed on a setup error (#2: deny left in place, FATAL, exit 1).
set -u
IPT=iptables; command -v iptables-nft >/dev/null 2>&1 && IPT=iptables-nft
pass=0; fail=0
ok(){ echo "PASS: $1"; pass=$((pass+1)); }
no(){ echo "FAIL: $1"; fail=$((fail+1)); }
# dummy iface so dnsmasq can bind the gateway IP
ip link add dummy0 type dummy 2>/dev/null
ip addr add 10.123.0.1/24 dev dummy0 2>/dev/null
ip link set dummy0 up 2>/dev/null
echo "######## TEST 1: happy path (enforce) ########"
# seed a STALE allowed IP to prove flush at startup
ipset create artex_allow hash:ip timeout 21600 -exist
ipset add artex_allow 203.0.113.99 -exist
export EGRESS_MODE=enforce ARTEX_SUBNET=10.123.0.0/24 ARTEX_GATEWAY=10.123.0.1 UPSTREAM_DNS=1.1.1.1
/usr/local/bin/egress-guard >/tmp/g.log 2>&1 &
GP=$!
sleep 3
if ipset list artex_allow 2>/dev/null | grep -q 203.0.113.99; then no "stale IP flushed at startup"; else ok "stale IP flushed at startup (#1)"; fi
$IPT -S DOCKER-USER 2>/dev/null | grep -q 'ARTEX-EGRESS' && ok "DOCKER-USER jumps to ARTEX-EGRESS" || no "DOCKER-USER jump present"
$IPT -S DOCKER-USER 2>/dev/null | grep -q 'bootstrap-deny' && no "bootstrap-deny removed after clean setup" || ok "bootstrap-deny removed after clean setup (#2)"
$IPT -S ARTEX-EGRESS 2>/dev/null | grep -q 'match-set artex_allow dst' && ok "allow ipset rule present" || no "allow ipset rule present"
$IPT -S ARTEX-EGRESS 2>/dev/null | grep -qi 'REJECT' && ok "enforce REJECT present (#5)" || no "enforce REJECT present"
$IPT -t nat -S ARTEX-DNS 2>/dev/null | grep -q 'DNAT' && ok "DNS redirect (DNAT) present" || no "DNS redirect present"
kill -TERM $GP 2>/dev/null; sleep 1
$IPT -S ARTEX-EGRESS 2>/dev/null | grep -q . && no "chain removed on stop" || ok "chain removed on stop (cleanup)"
echo "######## TEST 2: fail-closed (setup error after bootstrap) ########"
# invalid IPSET_TIMEOUT makes apply_rules fail right after bootstrap_deny is installed
export IPSET_TIMEOUT=notanumber
/usr/local/bin/egress-guard >/tmp/g2.log 2>&1
rc=$?
[ "$rc" -ne 0 ] && ok "setup failure is fatal (exit $rc, #2 fail-closed)" || no "setup failure fatal"
$IPT -S DOCKER-USER 2>/dev/null | grep -q 'bootstrap-deny.*DROP' && ok "fail-closed DROP left in place (#2)" || no "fail-closed DROP present"
grep -q 'FATAL: setup failed' /tmp/g2.log && ok "operator sees FATAL message (#2 documented)" || no "FATAL message logged"
# cleanup
unset IPSET_TIMEOUT
$IPT -F DOCKER-USER 2>/dev/null; $IPT -X ARTEX-EGRESS 2>/dev/null
echo "=== guard log test1 ==="; cat /tmp/g.log; echo "=== guard log test2 ==="; cat /tmp/g2.log
echo "######## RESULT: pass=$pass fail=$fail ########"