Files
pentest.intro/session1/content/lib/cmd_kali.sh
T
quoije dcb64ac65e 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.
2026-10-24 14:22:39 -04:00

219 lines
6.9 KiB
Bash

# shellcheck shell=bash
# cmd_kali — optional attacker VM on the qemu platform.
#
# Uses the official Kali QEMU image (full toolset, kali/kali). It is kept as a
# read-only base plus a throwaway overlay, exactly like the Windows guests.
# Two NICs: br-lab (lab network, DHCP from DC01) and QEMU user networking so
# apt still works while the lab itself stays isolated.
_kali_base() { printf '%s/images/base/kali-base.qcow2' "$LAB_ROOT"; }
_kali_disk() { printf '%s/images/overlays/kali.qcow2' "$LAB_ROOT"; }
_kali_pid() { printf '%s/run/kali.pid' "$LAB_ROOT"; }
_kali_log() { printf '%s/run/kali.log' "$LAB_ROOT"; }
_kali_installed() { [[ -f $(_kali_base) ]]; }
_kali_tools_iso() { printf '%s/seeds/kali-tools.iso' "$LAB_ROOT"; }
# The stock Kali image ships without the lab wordlist. A small ISO carries it
# into the guest, so a fresh redeploy is reproducible without network access.
_kali_build_tools() {
local iso dir
iso=$(_kali_tools_iso)
dir=$(mktemp -d)
mkdir -p "$(dirname "$iso")"
cp "$CONTENT_DIR/scripts/wordlists/norbourg.txt" "$dir/"
cat > "$dir/README.txt" <<'EOF'
Outils de l'atelier Norbourg
============================
norbourg.txt : liste de mots ciblee (1716 entrees), dimensionnee pour que le
cassage prenne quelques secondes.
Installation dans Kali :
sudo mkdir -p /opt/norbourg/wordlists
sudo cp /media/cdrom/norbourg.txt /opt/norbourg/wordlists/
sudo chmod 644 /opt/norbourg/wordlists/norbourg.txt
EOF
if have_cmd genisoimage; then
genisoimage -quiet -volid NORBOURG_TOOLS -J -R -o "$iso" "$dir"
else
xorriso -as mkisofs -quiet -V NORBOURG_TOOLS -J -R -o "$iso" "$dir"
fi
rm -rf "$dir"
ok "tool ISO: $iso"
}
_kali_ensure_overlay() {
local base overlay
base=$(_kali_base)
overlay=$(_kali_disk)
_kali_installed || return 1
mkdir -p "$(dirname "$overlay")"
[[ -f $overlay ]] && return 0
qemu-img create -q -f qcow2 -F qcow2 -b "../base/kali-base.qcow2" "$overlay"
}
cmd_kali() {
local action=${1:-up}
case $action in
install) _kali_install ;;
up) _kali_up ;;
down) _kali_down ;;
reset) _kali_reset ;;
snapshot) _kali_snapshot ;;
status) _kali_status ;;
*) info "usage: ./lab.sh kali install|up|down|reset|snapshot|status"; return 2 ;;
esac
}
_kali_install() {
local archive="$LAB_ISO_CACHE/kali-qemu.7z"
local base tmp found
have_cmd 7z || die "7z is required to extract the Kali image"
base=$(_kali_base)
if _kali_installed; then
ok "Kali base image already present ($(du -h "$base" | cut -f1))"
return 0
fi
mkdir -p "$LAB_ISO_CACHE" "$(dirname "$base")"
if [[ ! -f $archive ]]; then
step "download Kali QEMU image (~4 GB)"
curl -L --fail --retry 3 -C - -o "$archive.part" "$KALI_ARCHIVE_URL" -sS \
|| { rm -f "$archive.part"; die "download failed"; }
mv -f "$archive.part" "$archive"
fi
ok "archive: $(du -h "$archive" | cut -f1)"
step "extract"
tmp=$(mktemp -d "$LAB_ROOT/run/kali-extract.XXXXXX")
( cd "$tmp" && 7z x -y "$archive" >/dev/null ) || { rm -rf "$tmp"; die "extraction failed"; }
found=$(find "$tmp" -maxdepth 2 -name '*.qcow2' -print -quit)
[[ -n $found ]] || { rm -rf "$tmp"; die "no qcow2 inside the archive"; }
mv "$found" "$base"
rm -rf "$tmp"
if [[ ${KALI_DISK_GB:-40} -gt 0 ]]; then
qemu-img resize -q "$base" "${KALI_DISK_GB}G" >/dev/null 2>&1 || true
fi
ok "base image: $(du -h "$base" | cut -f1) (${KALI_DISK_GB:-40}G nominal)"
_kali_ensure_overlay
_kali_build_tools
printf '\n'
ok "Kali installed — ./lab.sh kali up"
info " default credentials: kali / kali"
}
_kali_up() {
local base overlay pid vnc
_kali_installed || die "Kali is not installed — ./lab.sh kali install"
_kali_ensure_overlay || die "could not create the Kali overlay"
if _kali_status >/dev/null 2>&1; then
info "Kali already running"
return 0
fi
base=$(_kali_base)
overlay=$(_kali_disk)
vnc=${KALI_VNC_DISPLAY:-9}
mkdir -p "$LAB_ROOT/run"
[[ -f $(_kali_tools_iso) ]] || _kali_build_tools
# net0: the lab bridge (DHCP from DC01). net1: user networking so apt works
# without punching a hole through the lab isolation policy.
local cd_args=()
if [[ -f $(_kali_tools_iso) ]]; then
cd_args=( -drive "if=none,id=cd1,media=cdrom,file=$(_kali_tools_iso)" -device "ide-cd,drive=cd1,bus=ide.1" )
fi
nohup qemu-system-x86_64 \
-name norbourg-kali \
-machine q35,accel=kvm -cpu host \
-smp "${KALI_VCPU:-2},sockets=1,cores=${KALI_VCPU:-2}" \
-m "${KALI_RAM:-4096}" \
-rtc base=localtime,clock=host \
-drive "if=none,id=disk,file=$overlay,format=qcow2,cache=none,aio=native" \
-device ide-hd,drive=disk,bus=ide.0 \
-netdev "bridge,id=net0,br=$LAB_BRIDGE" \
-device "e1000,netdev=net0,mac=$(qemu_mac_for "${KALI_IP:-10.10.10.101}")" \
-netdev "user,id=net1,hostfwd=tcp:127.0.0.1:${KALI_SSH_PORT:-2222}-:22" \
-device "e1000,netdev=net1" \
-monitor "unix:$LAB_ROOT/run/kali.monitor,server,nowait" \
"${cd_args[@]}" \
-display none -vnc "127.0.0.1:$vnc" \
>>"$(_kali_log)" 2>&1 &
printf '%s\n' "$!" > "$(_kali_pid)"
sleep 2
if kill -0 "$(<"$(_kali_pid)")" 2>/dev/null; then
ok "Kali started · vnc 127.0.0.1:$(( 5900 + vnc )) · ssh 127.0.0.1:${KALI_SSH_PORT:-2222}"
info " login kali/kali · bridge address via DHCP (${KALI_IP:-10.10.10.101} reserved)"
info " wordlist on the tool CD: mount /dev/sr0 /mnt && cp /mnt/norbourg.txt /opt/norbourg/wordlists/"
else
fail "Kali failed to start — see $(_kali_log)"
return 1
fi
}
_kali_down() {
local pid sock
_kali_status >/dev/null 2>&1 || { info "Kali already stopped"; return 0; }
pid=$(<"$(_kali_pid)")
sock="$LAB_ROOT/run/kali.monitor"
if [[ -S $sock ]]; then
printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true
fi
for _ in $(seq 1 30); do kill -0 "$pid" 2>/dev/null || break; sleep 1; done
kill -0 "$pid" 2>/dev/null && { kill "$pid" 2>/dev/null; sleep 2; }
rm -f "$(_kali_pid)"
ok "Kali stopped"
}
# Fold the current overlay into the base image: wordlist, DNS, packages and
# any tooling you installed survive a reset from then on.
_kali_snapshot() {
local overlay
_kali_installed || die "Kali is not installed"
overlay=$(_kali_disk)
_kali_down >/dev/null 2>&1 || true
if [[ ! -f $overlay ]]; then
ok "nothing to commit — no working overlay"
return 0
fi
step "commit the overlay into ${KALI_NAME:-kali}'s base image"
qemu-img commit -q "$overlay" || die "qemu-img commit failed"
rm -f "$overlay"
_kali_ensure_overlay
ok "baseline updated: ./lab.sh kali reset now returns to this state"
}
_kali_reset() {
local overlay
_kali_installed || die "Kali is not installed"
_kali_down >/dev/null 2>&1 || true
overlay=$(_kali_disk)
rm -f "$overlay"
_kali_ensure_overlay
ok "Kali reverted to the clean image"
}
_kali_status() {
local pid
_kali_installed || return 1
[[ -r $(_kali_pid) ]] || return 1
pid=$(<"$(_kali_pid)")
[[ -n $pid ]] && kill -0 "$pid" 2>/dev/null
}