Add optional Kali attacker VM and DHCP for the lab bridge
lab.sh kali install|up|down|reset|status extracts the official Kali QEMU image once, keeps it as a read-only base with a throwaway overlay, and boots two NICs: the lab bridge (for the exercise) and user networking (so apt works without opening the isolation policy). DC01 now serves DHCP on 10.10.10.100-200 with DNS and domain options but no router: devices on the bridge configure themselves while the lab stays isolated. A reservation pins 10.10.10.101 to the Kali MAC. Also fixed backend_stop, which still called the pre-refactor WinRM helper and fell back to killing the VMs.
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
# 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_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 ;;
|
||||
status) _kali_status ;;
|
||||
*) info "usage: ./lab.sh kali install|up|down|reset|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
|
||||
|
||||
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"
|
||||
|
||||
# net0: the lab bridge (DHCP from DC01). net1: user networking so apt works
|
||||
# without punching a hole through the lab isolation policy.
|
||||
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" \
|
||||
-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)"
|
||||
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"
|
||||
}
|
||||
|
||||
_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
|
||||
}
|
||||
Reference in New Issue
Block a user