Restructure the repository per session
session1/ now holds the whole live lab (content, platforms, docs, answer key, README); session2/ keeps the design for the second workshop. The root README indexes both and states that session2 modifies session1 rather than duplicating it. build-bundle.sh builds from session1 and still produces a flat archive, so the documented paths inside a deployment (platforms/<p>/lab.sh) are unchanged. .gitignore follows the move.
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
# shellcheck shell=bash
|
||||
# QEMU/KVM backend — local development and dry runs.
|
||||
#
|
||||
# Hardware model matches the Proxmox target on purpose: q35 + AHCI (SATA) and
|
||||
# an e1000 NIC, so the unattended file never needs virtio drivers.
|
||||
|
||||
backend_check() {
|
||||
if qemu-system-x86_64 -M help 2>/dev/null | grep -q '^q35'; then
|
||||
_c_ok "qemu: q35 machine type available"
|
||||
else
|
||||
_c_fail "qemu: q35 machine type unavailable"
|
||||
fi
|
||||
|
||||
if qemu-system-x86_64 -device help 2>/dev/null | grep -q 'name "e1000"'; then
|
||||
_c_ok "qemu: e1000 NIC model available"
|
||||
else
|
||||
_c_warn "qemu: e1000 NIC model not listed"
|
||||
fi
|
||||
|
||||
if qemu-system-x86_64 -device help 2>/dev/null | grep -q 'name "ich9-ahci"'; then
|
||||
_c_ok "qemu: ich9-ahci controller available"
|
||||
else
|
||||
_c_warn "qemu: ich9-ahci controller not listed"
|
||||
fi
|
||||
|
||||
# `-netdev help` omits bridge even when it is compiled in, so probe it instead:
|
||||
# an unsupported type answers "Invalid netdev type", a supported one complains
|
||||
# about the missing bridge device (br0).
|
||||
if qemu-system-x86_64 -netdev bridge,id=probe 2>&1 | grep -qi 'invalid netdev'; then
|
||||
_c_fail "qemu: bridge netdev not supported"
|
||||
else
|
||||
_c_ok "qemu: bridge netdev supported"
|
||||
fi
|
||||
|
||||
if [[ -x /usr/libexec/qemu-bridge-helper ]]; then
|
||||
_c_ok "qemu-bridge-helper present"
|
||||
else
|
||||
_c_warn "qemu-bridge-helper not found: unprivileged bridge attach will fail"
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ layout
|
||||
qemu_base() { printf '%s/images/base/%s-base.qcow2' "$LAB_ROOT" "$1"; }
|
||||
qemu_overlay(){ printf '%s/images/overlays/%s.qcow2' "$LAB_ROOT" "$1"; }
|
||||
|
||||
# Boot the overlay when a baseline exists, the golden disk otherwise.
|
||||
qemu_disk() {
|
||||
local overlay
|
||||
overlay=$(qemu_overlay "$1")
|
||||
if [[ -f $overlay ]]; then printf '%s' "$overlay"; else printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; fi
|
||||
}
|
||||
|
||||
# The disk created by `build`, before it is frozen into a base image.
|
||||
qemu_golden() { printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; }
|
||||
qemu_pidfile(){ printf '%s/run/%s.pid' "$LAB_ROOT" "$1"; }
|
||||
qemu_log() { printf '%s/run/%s.log' "$LAB_ROOT" "$1"; }
|
||||
qemu_monitor(){ printf '%s/run/%s.monitor' "$LAB_ROOT" "$1"; }
|
||||
|
||||
backend_running() {
|
||||
local pidfile pid
|
||||
pidfile=$(qemu_pidfile "$1")
|
||||
[[ -r $pidfile ]] || return 1
|
||||
pid=$(<"$pidfile")
|
||||
[[ -n $pid ]] && kill -0 "$pid" 2>/dev/null
|
||||
}
|
||||
|
||||
backend_create_disk() {
|
||||
local name=$1 dry=${2:-0}
|
||||
local disk i size
|
||||
disk=$(qemu_golden "$name")
|
||||
i=$(vm_index "$name") || die "unknown vm: $name"
|
||||
size=${VM_DISK[$i]}
|
||||
|
||||
if [[ -f $disk ]]; then
|
||||
ok "$(printf '%-6s disk present (%s)' "$name" "$(du -h "$disk" | cut -f1)")"
|
||||
return
|
||||
fi
|
||||
mkdir -p "$LAB_ROOT/images"
|
||||
if (( dry )); then
|
||||
info " + qemu-img create -f qcow2 $disk ${size}G"
|
||||
return
|
||||
fi
|
||||
qemu-img create -q -f qcow2 "$disk" "${size}G"
|
||||
ok "$(printf '%-6s disk created (%sG)' "$name" "$size")"
|
||||
}
|
||||
|
||||
backend_launch() {
|
||||
local name=$1 mode=${2:-run} dry=${3:-0}
|
||||
local i ip ram vcpu disk seed win vnc
|
||||
local boot_iso="" args=() cd=1
|
||||
|
||||
i=$(vm_index "$name") || die "unknown vm: $name"
|
||||
ip=${VM_IPS[$i]}; ram=${VM_RAM[$i]}; vcpu=${VM_VCPU[$i]}
|
||||
disk=$(qemu_disk "$name")
|
||||
seed="$LAB_ROOT/seeds/$name-seed.iso"
|
||||
win=$WINDOWS_ISO
|
||||
vnc=$(( i + 1 ))
|
||||
|
||||
[[ -f $seed ]] || (( dry )) || die "missing seed ISO for $name (run ./lab.sh seeds)"
|
||||
[[ -f $disk ]] || (( dry )) || die "missing disk for $name (run ./lab.sh build)"
|
||||
|
||||
if [[ $mode == install ]] && ! (( dry )); then
|
||||
[[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)"
|
||||
fi
|
||||
|
||||
args=(
|
||||
-name "norbourg-$name"
|
||||
-machine q35,accel=kvm
|
||||
-cpu host
|
||||
-smp "${vcpu},sockets=1,cores=${vcpu}"
|
||||
-m "$ram"
|
||||
-rtc base=localtime,clock=host
|
||||
-drive "if=none,id=disk,file=$disk,format=qcow2,cache=none,aio=native,discard=unmap"
|
||||
-device ide-hd,drive=disk,bus=ide.0
|
||||
-netdev "bridge,id=net0,br=$BRIDGE"
|
||||
-device "e1000,netdev=net0,mac=$(qemu_mac_for "$ip")"
|
||||
-monitor "unix:$(qemu_monitor "$name"),server,nowait"
|
||||
-display none
|
||||
-vnc "127.0.0.1:${vnc}"
|
||||
)
|
||||
|
||||
if [[ $mode == install ]]; then
|
||||
(( dry )) || [[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)"
|
||||
args+=( -drive "if=none,id=cd-install,media=cdrom,file=$win"
|
||||
-device "ide-cd,drive=cd-install,bus=ide.${cd}" )
|
||||
cd=$(( cd + 1 ))
|
||||
fi
|
||||
args+=( -drive "if=none,id=cd-seed,media=cdrom,file=$seed"
|
||||
-device "ide-cd,drive=cd-seed,bus=ide.${cd}" )
|
||||
|
||||
if [[ $mode == install ]]; then
|
||||
# once=d: boot the install media this time only, then always from disk.
|
||||
# Without `once`, Setup would re-enter itself after its own reboot.
|
||||
args+=( -boot order=c,once=d )
|
||||
else
|
||||
args+=( -boot order=c )
|
||||
fi
|
||||
|
||||
if (( dry )); then
|
||||
info " + qemu-system-x86_64 ${args[*]}"
|
||||
return
|
||||
fi
|
||||
|
||||
mkdir -p "$LAB_ROOT/run"
|
||||
nohup qemu-system-x86_64 "${args[@]}" >>"$(qemu_log "$name")" 2>&1 &
|
||||
printf '%s\n' "$!" >"$(qemu_pidfile "$name")"
|
||||
sleep 1
|
||||
|
||||
if backend_running "$name"; then
|
||||
ok "$(printf '%-6s started · vnc 127.0.0.1:%d · pid %s' "$name" "$(( 5900 + vnc ))" "$(<"$(qemu_pidfile "$name")")")"
|
||||
else
|
||||
fail "$name failed to start — see $(qemu_log "$name")"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
backend_stop() {
|
||||
local name=$1 sock pid ip i
|
||||
|
||||
backend_running "$name" || { info " $name already stopped"; return 0; }
|
||||
|
||||
sock=$(qemu_monitor "$name")
|
||||
pid=$(<"$(qemu_pidfile "$name")")
|
||||
ip=${VM_IPS[$(vm_index "$name")]}
|
||||
|
||||
# ACPI powerdown is best-effort; Server 2022 often ignores it. Asking the
|
||||
# guest to shut itself down over WinRM is what actually works.
|
||||
if backend_ps "$name" "shutdown.exe /s /t 3 /f" >/dev/null 2>&1; then
|
||||
detail "shutdown requested over WinRM"
|
||||
elif [[ -S $sock ]]; then
|
||||
printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true
|
||||
fi
|
||||
|
||||
for i in $(seq 1 90); do
|
||||
kill -0 "$pid" 2>/dev/null || break
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if kill -0 "$pid" 2>/dev/null; then
|
||||
warn "$name did not shut down cleanly, sending TERM"
|
||||
kill "$pid" 2>/dev/null || true
|
||||
sleep 3
|
||||
kill -9 "$pid" 2>/dev/null || true
|
||||
fi
|
||||
rm -f "$(qemu_pidfile "$name")"
|
||||
ok "$name stopped"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ baseline
|
||||
backend_snapshot() {
|
||||
local name=$1
|
||||
local golden base overlay
|
||||
golden=$(qemu_golden "$name")
|
||||
base=$(qemu_base "$name")
|
||||
overlay=$(qemu_overlay "$name")
|
||||
|
||||
if backend_running "$name"; then
|
||||
fail "$name is still running"
|
||||
return 1
|
||||
fi
|
||||
if [[ -f $base ]]; then
|
||||
ok "$(printf '%-6s baseline already frozen' "$name")"
|
||||
return 0
|
||||
fi
|
||||
if [[ ! -f $golden ]]; then
|
||||
fail "$name has no disk to freeze"
|
||||
return 1
|
||||
fi
|
||||
|
||||
mkdir -p "$(dirname "$base")" "$(dirname "$overlay")"
|
||||
mv "$golden" "$base"
|
||||
# Relative backing path: the tree can move without breaking the chain.
|
||||
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
||||
ok "$(printf '%-6s baseline frozen (%s)' "$name" "$(du -h "$base" | cut -f1)")"
|
||||
}
|
||||
|
||||
backend_has_baseline() { [[ -f $(qemu_base "$1") ]]; }
|
||||
|
||||
backend_reset() {
|
||||
local name=$1
|
||||
local base overlay
|
||||
base=$(qemu_base "$name")
|
||||
overlay=$(qemu_overlay "$name")
|
||||
|
||||
if backend_running "$name"; then
|
||||
backend_stop "$name" >/dev/null 2>&1 || true
|
||||
fi
|
||||
if [[ ! -f $base ]]; then
|
||||
fail "$name has no baseline — run ./lab.sh snapshot first"
|
||||
return 1
|
||||
fi
|
||||
|
||||
rm -f "$overlay"
|
||||
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
||||
ok "$(printf '%-6s reverted' "$name")"
|
||||
}
|
||||
|
||||
# Fold the current overlay into the base, then start a fresh overlay.
|
||||
# Used after a deliberate change to the lab, so `reset` keeps the change.
|
||||
backend_commit() {
|
||||
local name=$1
|
||||
local base overlay
|
||||
base=$(qemu_base "$name")
|
||||
overlay=$(qemu_overlay "$name")
|
||||
|
||||
if backend_running "$name"; then
|
||||
fail "$name is still running"
|
||||
return 1
|
||||
fi
|
||||
if [[ ! -f $base || ! -f $overlay ]]; then
|
||||
fail "$name has no base/overlay pair"
|
||||
return 1
|
||||
fi
|
||||
|
||||
qemu-img commit -q "$overlay" || { fail "commit failed for $name"; return 1; }
|
||||
rm -f "$overlay"
|
||||
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
||||
ok "$(printf '%-6s overlay committed into the baseline' "$name")"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ transport
|
||||
# WinRM over the lab network. The guest scripts are already on disk, so this
|
||||
# layer only runs commands and waits for markers.
|
||||
_labwinrm() {
|
||||
local name=$1; shift
|
||||
local ip=${VM_IPS[$(vm_index "$name")]}
|
||||
LAB_PASSWORD=$LAB_ADMIN_PASSWORD python3 "$CONTENT_DIR/scripts/labwinrm.py" "$ip" "$@" \
|
||||
--user "$LAB_USER" --password "$LAB_ADMIN_PASSWORD"
|
||||
}
|
||||
|
||||
backend_ps() {
|
||||
local name=$1 script=$2
|
||||
_labwinrm "$name" ps "$script"
|
||||
}
|
||||
|
||||
backend_ready() {
|
||||
local name=$1 timeout=${2:-300}
|
||||
local deadline=$(( SECONDS + timeout ))
|
||||
while (( SECONDS < deadline )); do
|
||||
_labwinrm "$name" ps 'exit 0' >/dev/null 2>&1 && return 0
|
||||
sleep 10
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
backend_start() {
|
||||
local name=$1 remote=$2
|
||||
_labwinrm "$name" start "$remote"
|
||||
}
|
||||
|
||||
backend_wait() {
|
||||
local name=$1 remote=$2 timeout=${3:-1800}
|
||||
_labwinrm "$name" wait "$remote" "$timeout"
|
||||
}
|
||||
|
||||
backend_destroy() {
|
||||
local name=$1
|
||||
local golden base overlay disk
|
||||
golden=$(qemu_golden "$name")
|
||||
base=$(qemu_base "$name")
|
||||
overlay=$(qemu_overlay "$name")
|
||||
|
||||
backend_running "$name" && backend_stop "$name" >/dev/null 2>&1 || true
|
||||
for disk in "$overlay" "$base" "$golden"; do
|
||||
[[ -f $disk ]] && rm -f "$disk"
|
||||
done
|
||||
rm -f "$(qemu_pidfile "$name")" "$(qemu_monitor "$name")" "$(qemu_log "$name")"
|
||||
ok "$name removed"
|
||||
}
|
||||
|
||||
|
||||
# Nothing to do: the boot order was set to "once=d" at launch time.
|
||||
backend_finalize() { :; }
|
||||
@@ -0,0 +1,67 @@
|
||||
# lab.conf — QEMU/KVM platform (generic Linux host)
|
||||
# Sourced as bash by lab.sh. Keep it KEY=value, no functions.
|
||||
# Override anything in lab.conf.local (gitignored) instead of editing this file.
|
||||
|
||||
# ---------------------------------------------------------------- identity
|
||||
BACKEND="qemu"
|
||||
LAB_NAME="norbourg"
|
||||
DOMAIN="norbourg.lab"
|
||||
NETBIOS="NORBOURG"
|
||||
FLAG_PREFIX="NORBOURG"
|
||||
|
||||
# ---------------------------------------------------------------- network
|
||||
# Lab segment. Nothing here is ever bridged to the office LAN.
|
||||
LAB_SUBNET="10.10.10.0/24"
|
||||
LAB_NETMASK="24"
|
||||
LAB_PREFIX="10.10.10"
|
||||
LAB_HOST_IP="10.10.10.230" # this host, on the lab bridge
|
||||
LAB_DC_IP="10.10.10.10" # DC01, also the lab DNS server
|
||||
LAB_GATEWAY="10.10.10.230" # default gateway handed to the VMs
|
||||
BRIDGE="br-lab"
|
||||
LAB_BRIDGE="$BRIDGE"
|
||||
|
||||
# Interface that faces the participants. Leave empty to auto-detect the
|
||||
# default-route interface at runtime.
|
||||
LAN_IFACE=""
|
||||
|
||||
# The office network the lab must never be able to reach.
|
||||
OFFICE_SUBNET="192.168.10.0/24"
|
||||
|
||||
# ---------------------------------------------------------------- storage
|
||||
# Disks must live on a real POSIX filesystem; btrfs gets chattr +C (no CoW).
|
||||
# Defaults to ~/lab-vms so a fresh clone works anywhere.
|
||||
LAB_ROOT="${LAB_ROOT:-$HOME/lab-vms/pentest-lab}"
|
||||
LAB_ISO_CACHE="${LAB_ISO_CACHE:-$HOME/lab-vms/iso}"
|
||||
MIN_FREE_GB=20
|
||||
|
||||
WINDOWS_ISO="$LAB_ISO_CACHE/SERVER_EVAL_x64FRE_en-us.iso"
|
||||
VIRTIO_ISO="$LAB_ISO_CACHE/virtio-win.iso"
|
||||
|
||||
# ---------------------------------------------------------------- vms
|
||||
# name ip ram_mb disk_gb vcpu role (= windows/roles/<role>.ps1)
|
||||
VMS=$(cat <<'EOF'
|
||||
dc01 10.10.10.10 4096 36 2 dc01
|
||||
fs01 10.10.10.20 4096 36 2 fs01
|
||||
ws01 10.10.10.31 4096 36 2 ws01
|
||||
EOF
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------- windows
|
||||
LAB_ADMIN_PASSWORD="${LAB_ADMIN_PASSWORD:-Norbourg!2026}"
|
||||
LAB_SAFE_MODE_PASSWORD="${LAB_SAFE_MODE_PASSWORD:-NorbourgDsrm!2026}"
|
||||
WINDOWS_IMAGE_INDEX=2 # 2 = Standard, Desktop Experience
|
||||
|
||||
# ---------------------------------------------------------------- kali
|
||||
# Optional attacker VM (official Kali QEMU image, full toolset).
|
||||
KALI_ARCHIVE_URL="https://cdimage.kali.org/kali-2026.3/kali-linux-2026.3-qemu-amd64.7z"
|
||||
KALI_IP="10.10.10.101"
|
||||
KALI_RAM=4096
|
||||
KALI_VCPU=2
|
||||
KALI_DISK_GB=40
|
||||
KALI_SSH_PORT=2222
|
||||
KALI_VNC_DISPLAY=9
|
||||
|
||||
# ---------------------------------------------------------------- tuning
|
||||
DISK_FILL_RATIO=30 # percent of the nominal disk actually used (measured ~26%)
|
||||
ISO_BUDGET_GB=8
|
||||
BUILD_TIMEOUT=2700 # seconds to wait for unattended installs
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
# lab.sh — Norbourg lab on a generic Linux host (QEMU/KVM).
|
||||
set -euo pipefail
|
||||
|
||||
LAB_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd -P)
|
||||
ROOT_DIR=$(cd -- "$LAB_DIR/../.." && pwd -P)
|
||||
CONTENT_DIR="$ROOT_DIR/content"
|
||||
PLATFORM=qemu
|
||||
export LAB_DIR ROOT_DIR CONTENT_DIR PLATFORM
|
||||
|
||||
source "$CONTENT_DIR/lib/common.sh"
|
||||
for _lib in check cmd_init cmd_isos cmd_seeds cmd_build cmd_provision cmd_web \
|
||||
cmd_snapshot cmd_doctor cmd_misc cmd_kali; do
|
||||
# shellcheck disable=SC1090
|
||||
source "$CONTENT_DIR/lib/${_lib}.sh"
|
||||
done
|
||||
unset _lib
|
||||
|
||||
# shellcheck source=../../content/lib/dispatch.sh
|
||||
source "$CONTENT_DIR/lib/dispatch.sh"
|
||||
Executable
+234
@@ -0,0 +1,234 @@
|
||||
#!/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 ./setup-network.sh apply (idempotent)
|
||||
# sudo ./setup-network.sh --verify check the current state
|
||||
# sudo ./setup-network.sh --remove undo everything
|
||||
# ./setup-network.sh --dry-run print commands, change nothing
|
||||
set -euo pipefail
|
||||
|
||||
LAB_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd -P)
|
||||
ROOT_DIR=$(cd -- "$LAB_DIR/../.." && pwd -P)
|
||||
CONTENT_DIR="$ROOT_DIR/content"
|
||||
export LAB_DIR ROOT_DIR CONTENT_DIR
|
||||
|
||||
# shellcheck source=../../content/lib/common.sh
|
||||
source "$CONTENT_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 "<kali-conn>" 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 <<EOF
|
||||
Usage: sudo $0 [--dry-run|--verify|--remove|--status]
|
||||
|
||||
(no flag) apply the isolated lab network
|
||||
--dry-run print the commands without running them (no root needed)
|
||||
--verify check the current state
|
||||
--remove undo every change
|
||||
EOF
|
||||
}
|
||||
|
||||
case ${1:-apply} in
|
||||
apply) need_root "$@"; apply ;;
|
||||
--dry-run|dry-run)
|
||||
DRY_RUN=1; apply ;;
|
||||
--verify|verify)
|
||||
need_root "$@"; verify ;;
|
||||
--remove|remove)
|
||||
need_root "$@"; remove ;;
|
||||
-h|--help|help) usage ;;
|
||||
*) usage; exit 2 ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user