Files
pentest.intro/session1/content/lib/cmd_doctor.sh
T
quoije 59e2594c1e dc01: make the delegated ACL the only way out, and stabilise the state
Three shortcuts and one race made the lab either easier than documented or
non-deterministic:

- Serveurs-Admins was nested in BUILTIN\Administrators on the DC, so
  m.legault was a de-facto domain-controller admin (C$, NTDS.dit) and the
  whole ACL lesson could be skipped. It also made SDProp treat her as
  protected: an hour after provisioning, the intended GenericAll ACE was
  wiped. Removed the nesting; adminCount is explicitly reset.
- the t.smith decoy ACE gave a working path to Domain Admin until SDProp
  ran (up to an hour after a fresh build). The role now grants it and
  revokes it immediately, so the state is deterministic.
- the replication-rights grant stacked duplicate ACEs on every re-run; it
  now removes its own ACEs first.
- up/reset/save returned as soon as WinRM answered, while the directory
  was still starting; doctor right after could report a missing ACE and
  missing replication rights on a healthy lab. They now wait for
  Get-ADDomain on dc01.

doctor: a decoy ACE that is still present is now a failure, not a success.
The deck's wording matches the new behaviour.
2026-10-25 14:05:55 -04:00

163 lines
7.0 KiB
Bash

# shellcheck shell=bash
# cmd_doctor — end-to-end health check of the running laboratory.
_d_ok() { ok "$*"; }
_d_warn() { warn "$*"; DOCTOR_WARNS=$(( DOCTOR_WARNS + 1 )); }
_d_fail() { fail "$*"; DOCTOR_FAILS=$(( DOCTOR_FAILS + 1 )); }
cmd_doctor() {
DOCTOR_FAILS=0
DOCTOR_WARNS=0
local i name ip
step "host"
if ip link show "$LAB_BRIDGE" >/dev/null 2>&1; then
_d_ok "bridge ${LAB_BRIDGE} present"
else
_d_fail "bridge ${LAB_BRIDGE} missing — run sudo ./platforms/qemu/setup-network.sh"
fi
step "machines"
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}
ip=${VM_IPS[$i]}
if ! backend_running "$name"; then
_d_fail "$name is not running — ./lab.sh up"
continue
fi
if tcp_open "$ip" 445; then
_d_ok "$(printf '%-6s %-14s smb up' "$name" "$ip")"
else
_d_warn "$(printf '%-6s %-14s smb not answering' "$name" "$ip")"
fi
if [[ $name == dc01 || $name == ws01 || $name == fs01 ]]; then
if tcp_open "$ip" 5985; then
_d_ok "$(printf '%-6s %-14s winrm up' "$name" "$ip")"
else
_d_warn "$(printf '%-6s %-14s winrm not answering' "$name" "$ip")"
fi
fi
done
step "domain"
if have_cmd dig; then
local www
www=$(dig +short +time=2 +tries=1 "@$LAB_DC_IP" www.norbourg.lab 2>/dev/null | head -1)
if [[ $www == 10.10.10.20 ]]; then
_d_ok "dns: www.norbourg.lab -> $www"
else
_d_fail "dns: www.norbourg.lab did not resolve via ${LAB_DC_IP} (got '${www:-nothing}')"
fi
local srv
srv=$(dig +short +time=2 +tries=1 "@$LAB_DC_IP" SRV _ldap._tcp.dc._msdcs.norbourg.lab 2>/dev/null | head -1)
if [[ -n $srv ]]; then
_d_ok "dns: ldap SRV record present"
else
_d_fail "dns: ldap SRV record missing — AD discovery would fail"
fi
else
_d_warn "dig not installed, DNS checks skipped"
fi
step "lab state"
_lab_state
step "web"
local code
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "http://10.10.10.20/" 2>/dev/null || true)
if [[ $code == 200 ]]; then
_d_ok "site answers on 10.10.10.20"
else
_d_fail "site not answering (${code:-timeout}) — ./lab.sh web --sync"
fi
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "http://10.10.10.20/public/IT-inventaire.txt" 2>/dev/null || true)
if [[ $code == 200 ]]; then
_d_ok "entry point reachable: /public/IT-inventaire.txt"
else
_d_fail "entry point missing (${code:-timeout})"
fi
step "share"
if have_cmd smbclient; then
if smbclient //10.10.10.20/Public -U 'NORBOURG\helpdesk1%Welcome2024!' -c 'ls' 2>/dev/null | grep -q 'IT-inventaire'; then
_d_ok "Public share reachable with the leaked credential"
else
_d_warn "Public share not readable as helpdesk1 (domain channel or DNS?)"
fi
else
_d_warn "smbclient not installed, share check skipped"
fi
printf '\n'
if (( DOCTOR_FAILS )); then
fail "${DOCTOR_FAILS} problem(s), ${DOCTOR_WARNS} warning(s)"
return 1
fi
ok "laboratory healthy · ${DOCTOR_WARNS} warning(s)"
}
# The puzzle has invariants that services do not reveal: a delegated right, an
# SPN, disabled pre-authentication, a password policy, replication rights.
# SDProp quietly reverts ACEs on protected objects, so the target must stay
# outside protected groups — worth checking every time.
_lab_state() {
local out key value
out=$(backend_ps dc01 '
Import-Module ActiveDirectory,DnsServer
$dom = Get-ADDomain
$base = $dom.DistinguishedName
$acl = Get-Acl ("AD:\" + (Get-ADUser t.smith).DistinguishedName)
"ace=" + @($acl.Access | Where-Object { $_.IdentityReference.Value -like "*svc_backup*" }).Count
$plainAcl = Get-Acl ("AD:\" + (Get-ADUser m.legault).DistinguishedName)
"ace_plain=" + @($plainAcl.Access | Where-Object { $_.IdentityReference.Value -like "*svc_backup*" }).Count
"plain_enabled=" + (Get-ADUser m.legault).Enabled
"mcount=" + (Get-ADUser m.legault -Properties adminCount).adminCount
"repl=" + @((dsacls.exe $base | Select-String "Sauvegarde-AD")).Count
"share=" + ((Get-SmbShare -Name "Sauvegarde$" -ErrorAction SilentlyContinue) -ne $null)
"root=" + (Test-Path C:\Sauvegarde\root.txt)
"spn=" + @((Get-ADUser svc_backup -Properties ServicePrincipalNames).ServicePrincipalNames).Count
"preauth=" + (Get-ADUser j.contractor -Properties DoesNotRequirePreAuth).DoesNotRequirePreAuth
"minage=" + (Get-ADDefaultDomainPasswordPolicy).MinPasswordAge.TotalHours
"txt=" + ((Get-DnsServerResourceRecord -ZoneName norbourg.lab -Name yourlove -EA SilentlyContinue) -ne $null)
"dhcp=" + ((Get-DhcpServerv4Scope -EA SilentlyContinue) -ne $null)
"kali_res=" + @((Get-DhcpServerv4Reservation -ScopeId 10.10.10.0 -EA SilentlyContinue) | Where-Object { $_.IPAddress -eq "10.10.10.101" }).Count
' 2>/dev/null) || { _d_warn "cannot query DC01 for lab state"; return; }
while IFS='=' read -r key value; do
value=${value//$'\r'/}
case $key in
ace) if [[ $value -ge 1 ]]; then _d_fail "decoy ACE still on t.smith — a Domain Admin shortcut until SDProp runs"
else _d_ok "decoy ACE absent on t.smith (protected object, as designed)"; fi ;;
ace_plain) if [[ $value -ge 1 ]]; then _d_ok "stable path: svc_backup has an ACE on m.legault"
else _d_fail "stable path missing (ACE on m.legault)"; fi ;;
plain_enabled) if [[ $value == True ]]; then _d_ok "m.legault account enabled"
else _d_fail "m.legault is disabled — the path is dead"; fi ;;
mcount) if [[ -z $value || $value == 0 ]]; then _d_ok "m.legault outside protected groups"
else _d_fail "adminCount=$value: m.legault is protected, SDProp will wipe the ACE"; fi ;;
repl) if [[ $value -ge 2 ]]; then _d_ok "Sauvegarde-AD holds replication rights (DCSync)"
else _d_fail "replication rights missing on Sauvegarde-AD"; fi ;;
share) if [[ $value == True ]]; then _d_ok "backup share with the flag"
else _d_fail "Sauvegarde\$ share missing"; fi ;;
root) if [[ $value == True ]]; then _d_ok "root.txt reachable through the backup share"
else _d_fail "root.txt missing"; fi ;;
spn) if [[ $value -ge 1 ]]; then _d_ok "service account: SPN present"
else _d_fail "svc_backup has no SPN"; fi ;;
preauth) if [[ $value == True ]]; then _d_ok "pre-auth disabled on j.contractor"
else _d_fail "j.contractor still requires pre-auth"; fi ;;
minage) if [[ $value == 0 ]]; then _d_ok "minimum password age: 0"
else _d_fail "minimum password age is $value h"; fi ;;
txt) if [[ $value == True ]]; then _d_ok "easter egg TXT record"
else _d_warn "yourlove TXT record missing"; fi ;;
dhcp) if [[ $value == True ]]; then _d_ok "DHCP scope present"
else _d_fail "DHCP scope missing"; fi ;;
kali_res) if [[ $value -ge 1 ]]; then _d_ok "DHCP reservation 10.10.10.101"
else _d_fail "Kali reservation missing"; fi ;;
esac
done <<< "$out"
}