From 32b4cebf925cfeb70ccad007c932bd6e2bb0db8e Mon Sep 17 00:00:00 2001 From: Mathieu Moran Date: Wed, 21 Oct 2026 18:35:29 -0400 Subject: [PATCH] Phase 2: isolated br-lab, bridge helper access and firewalld policy One-time root script: NetworkManager bridge portless on 10.10.10.230/24, allow br-lab in /etc/qemu/bridge.conf, ip_forward via sysctl.d, and a firewalld zone + policy that permits participant->lab on AD/web/WinRM ports only. Reverse direction falls through to the default drop. Includes --dry-run, --verify and --remove. --- scripts/setup-network.sh | 233 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100755 scripts/setup-network.sh diff --git a/scripts/setup-network.sh b/scripts/setup-network.sh new file mode 100755 index 0000000..ec1b36c --- /dev/null +++ b/scripts/setup-network.sh @@ -0,0 +1,233 @@ +#!/usr/bin/env bash +# setup-network.sh — one-time root setup for the qemu backend. +# +# Creates the isolated lab bridge, lets unprivileged QEMU attach to it, enables +# IP forwarding, and installs a firewalld zone + policy that allows exactly one +# direction (participant -> lab) and nothing else. +# +# sudo ./scripts/setup-network.sh apply (idempotent) +# sudo ./scripts/setup-network.sh --verify check the current state +# sudo ./scripts/setup-network.sh --remove undo everything +# ./scripts/setup-network.sh --dry-run print commands, change nothing +set -euo pipefail + +REPO_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd -P) +LAB_DIR=$REPO_DIR +export LAB_DIR + +# shellcheck source=../lib/common.sh +source "$REPO_DIR/lib/common.sh" + +load_config + +LAB_ZONE=${LAB_ZONE:-norbourg-lab} +POLICY=${POLICY:-lab-in} +DRY_RUN=0 + +# Ports a participant needs to reach the lab. Everything else stays closed. +TCP_PORTS=(53 80 88 135 139 389 443 445 464 3268 3389 5985) +UDP_PORTS=(53 88 123 137 138 389 464) + +run() { + if (( DRY_RUN )); then + printf ' + %s\n' "$*" + else + "$@" + fi +} + +need_root() { + (( EUID == 0 )) || die "this step needs root: sudo $0 $*" +} + +fw_has_zone() { firewall-cmd --permanent --get-zones | tr ' ' '\n' | grep -qx "$LAB_ZONE"; } +fw_has_policy() { firewall-cmd --permanent --get-policies | tr ' ' '\n' | grep -qx "$POLICY"; } + +# --------------------------------------------------------------------- apply +apply() { + step "prerequisites" + if (( DRY_RUN )); then + warn "dry run — nothing will be changed" + fi + have_cmd nmcli || die "nmcli not found (NetworkManager is required)" + have_cmd firewall-cmd || die "firewall-cmd not found (firewalld is required)" + if ! systemctl is-active --quiet firewalld; then + die "firewalld is not running; start it before running this script" + fi + ok "NetworkManager and firewalld available" + + step "bridge ${BRIDGE} (${LAB_HOST_IP}/${LAB_NETMASK})" + if nmcli -t -f NAME con show | grep -qx "$BRIDGE"; then + run nmcli con mod "$BRIDGE" \ + connection.zone "$LAB_ZONE" \ + ipv4.method manual \ + ipv4.addresses "${LAB_HOST_IP}/${LAB_NETMASK}" \ + ipv4.never-default yes \ + ipv6.method disabled + ok "connection exists, settings refreshed" + else + run nmcli con add type bridge ifname "$BRIDGE" con-name "$BRIDGE" \ + connection.zone "$LAB_ZONE" \ + ipv4.method manual \ + ipv4.addresses "${LAB_HOST_IP}/${LAB_NETMASK}" \ + ipv4.never-default yes \ + ipv6.method disabled + run nmcli con mod "$BRIDGE" bridge.stp no + ok "connection created" + fi + run nmcli con up "$BRIDGE" + + step "unprivileged QEMU bridge access" + if grep -qx "allow ${BRIDGE}" /etc/qemu/bridge.conf 2>/dev/null; then + ok "/etc/qemu/bridge.conf already allows ${BRIDGE}" + else + run mkdir -p /etc/qemu + run sh -c "printf '%s\n' 'allow ${BRIDGE}' >> /etc/qemu/bridge.conf" + run chmod 0644 /etc/qemu/bridge.conf + ok "added 'allow ${BRIDGE}'" + fi + + step "ip_forward" + run sh -c "printf '%s\n' '# Norbourg lab: forward between the office LAN and br-lab' 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-norbourg-lab.conf" + run sysctl -q -p /etc/sysctl.d/99-norbourg-lab.conf + ok "net.ipv4.ip_forward=1" + + step "firewalld zone ${LAB_ZONE}" + if fw_has_zone; then + ok "zone exists" + else + run firewall-cmd --permanent --new-zone="$LAB_ZONE" + run firewall-cmd --permanent --zone="$LAB_ZONE" --set-description="Norbourg lab: isolated 10.10.10.0/24" + ok "zone created" + fi + + step "firewalld policy ${POLICY} (participant -> lab only)" + if fw_has_policy; then + ok "policy exists" + else + run firewall-cmd --permanent --new-policy="$POLICY" + run firewall-cmd --permanent --policy="$POLICY" --add-ingress-zone=public + run firewall-cmd --permanent --policy="$POLICY" --add-egress-zone="$LAB_ZONE" + # CONTINUE keeps the final verdict with the zones, so anything not listed + # below still falls through to the default drop. + run firewall-cmd --permanent --policy="$POLICY" --set-target=CONTINUE + ok "policy created" + fi + + local port + for port in "${TCP_PORTS[@]}"; do + run firewall-cmd --permanent --policy="$POLICY" --add-port="${port}/tcp" + done + for port in "${UDP_PORTS[@]}"; do + run firewall-cmd --permanent --policy="$POLICY" --add-port="${port}/udp" + done + run firewall-cmd --reload + ok "ports opened on ${POLICY}" + + (( DRY_RUN )) || verify + print_next_steps +} + +# -------------------------------------------------------------------- verify +verify() { + local problems=0 + + step "verification" + + if ip -4 -o addr show dev "$BRIDGE" 2>/dev/null | grep -q "inet ${LAB_HOST_IP}/"; then + ok "bridge ${BRIDGE} has ${LAB_HOST_IP}" + else + fail "bridge ${BRIDGE} is missing ${LAB_HOST_IP}" + problems=$(( problems + 1 )) + fi + + if grep -qx "allow ${BRIDGE}" /etc/qemu/bridge.conf 2>/dev/null; then + ok "qemu-bridge-helper allows ${BRIDGE}" + else + fail "/etc/qemu/bridge.conf does not allow ${BRIDGE}" + problems=$(( problems + 1 )) + fi + + if [[ $(sysctl -n net.ipv4.ip_forward 2>/dev/null) == 1 ]]; then + ok "ip_forward enabled" + else + fail "ip_forward disabled" + problems=$(( problems + 1 )) + fi + + if firewall-cmd --zone="$LAB_ZONE" --list-all >/dev/null 2>&1; then + ok "zone ${LAB_ZONE} bound to ${BRIDGE}" + else + fail "zone ${LAB_ZONE} not active" + problems=$(( problems + 1 )) + fi + + if firewall-cmd --policy="$POLICY" --list-all >/dev/null 2>&1; then + ok "policy ${POLICY} active: $(firewall-cmd --policy="$POLICY" --list-ports 2>/dev/null | wc -w) ports" + else + fail "policy ${POLICY} not active" + problems=$(( problems + 1 )) + fi + + printf '\n' + if (( problems )); then + fail "${problems} problem(s)" + return 1 + fi + ok "network ready" +} + +print_next_steps() { + printf '\n%sParticipant prerequisites (Kali):%s\n' "$C_BOLD" "$C_RESET" + printf ' sudo ip route add %s via %s\n' "$LAB_SUBNET" "$LAB_HOST_IP" + printf ' sudo nmcli con mod "" ipv4.dns %s ipv4.ignore-auto-dns yes\n' "$LAB_DC_IP" + printf '\nRemote, from this host:\n' + printf ' ping %s\n' "$LAB_HOST_IP" + printf ' (lab VMs do not exist yet — phase 3)\n' +} + +# -------------------------------------------------------------------- remove +remove() { + step "removing lab network" + run firewall-cmd --permanent --delete-policy="$POLICY" 2>/dev/null || true + run firewall-cmd --permanent --delete-zone="$LAB_ZONE" 2>/dev/null || true + run firewall-cmd --reload || true + ok "firewalld objects removed" + + run sh -c "sed -i '/^allow ${BRIDGE}\$/d' /etc/qemu/bridge.conf 2>/dev/null || true" + ok "bridge.conf cleaned" + + run rm -f /etc/sysctl.d/99-norbourg-lab.conf + run sysctl -q -p /etc/sysctl.d/99-norbourg-lab.conf 2>/dev/null || true + ok "sysctl file removed" + + if nmcli -t -f NAME con show | grep -qx "$BRIDGE"; then + run nmcli con delete "$BRIDGE" || true + ok "bridge connection deleted" + fi + + printf '\nThe lab is detached from the network. Data in %s is untouched.\n' "$LAB_ROOT" +} + +usage() { + cat <