# htbscan — the usual recon passes, written where htbbox looks for them.## htbscan [target] DEFAULT (rush): rustscan all ports → nmap -A on# the open set. If rustscan finds nothing it# retries slow + firewall-friendly and runs nmap# with -Pn. → recon/rustscan, recon/scan# htbscan quick [target] -sC -sV, top 1000 ports only → recon/quick# htbscan full [target] nmap -p- sweep, then -sC -sV on open# → recon/full, recon/targeted# htbscan ports 22,80 [target] -sC -sV on just these → recon/targeted# htbscan udp [target] top 100 UDP (sudo) → recon/udp## Target: the argument, else the current box's $TARGET (htbbox). Output: $BOXDIR/recon when# a box is current (htbbox use), else ./recon. Every pass is -oA, and when a# box is current the result is imported with `htbbox ports`, so box.json and# `htbbox info` show the ports without another step.## The default pass is rustscan (fast full-range connect sweep) handing its open# ports to a single `nmap -A` run; a filtered/slow host triggers an automatic# slow+`-Pn` retry. The IceBreaker nmap-init/-allports/-targeted trio survives# as quick/full/ports.STATE="${XDG_STATE_HOME:-$HOME/.local/state}/htb"mode=rush
case"${1:-}"in -h | --help)echo"usage: htbscan [rush|quick|full|udp|ports <list>] [target]"; exit0 ;;
rush | quick | full | udp)mode=$1; shift ;;
ports)mode=ports
plist=${2:-}case"$plist"in'' | *[!0-9,-]*)echo"htbscan: ports wants a list like 22,80,445" >&2; exit2 ;;
esacshift2 ;;
esactarget=${1:-}if[ -z "$target"]&&[ -s "$STATE/target"]; thentarget=$(cat "$STATE/target"); fitarget=${target:-${TARGET:-}}if[ -z "$target"]; thenecho"htbscan: no target — htbscan <ip>, or htbbox new / htbbox use <box> first" >&2exit2fibox=""if[ -s "$STATE/box"]&&dir=$(htbbox path 2>/dev/null); thenbox=$(cat "$STATE/box")out="$dir/recon"elseout="$PWD/recon"fimkdir -p "$out"run(){echo"» nmap $*" >&2"$@"}# rustscan all 65535 ports, greppable, and print the open set as "22,80,443".# $1 = mode: "fast" (big batch) or "slow" (small batch, long timeout, retries —# for a rate-limiting firewall). Greppable output is teed to recon/rustscan.txt.rustports(){localspeed="$1"; shiftlocal -a rs=(-a "$target" -g -u 5000)if["$speed"= slow ]; thenrs=(-a "$target" -g -u 2000 -b 500 -t 2000 --tries 2); fiecho"» rustscan ${rs[*]}" >&2 rustscan "${rs[@]}"2>/dev/null | tee "$out/rustscan.txt"\ | sed -n 's/.*-> \[\(.*\)\].*/\1/p' | head -n1
}ingest(){if[ -n "$box"]; then htbbox ports "$1.xml"|| true; fi}case"$mode"in rush)pn=""open=$(rustports fast)||trueif[ -z "$open"]; thenecho"htbscan: rustscan found nothing fast — retrying slow (firewall?) + nmap -Pn" >&2open=$(rustports slow)||truepn="-Pn"fiif[ -z "$open"]; thenecho"htbscan: no open TCP ports even on the slow pass. Host may be down or fully filtered." >&2echo" try manually: rustscan -a $target -u 2000 -b 300 -t 3000 --tries 3 -- -Pn" >&2exit1fiecho"open: $open" >&2# shellcheck disable=SC2086 # $pn is a single optional flag, must word-split run nmap -A $pn -p"$open" -oA "$out/scan""$target" ingest "$out/scan" ;;
quick) run nmap -sC -sV -oA "$out/quick""$target" ingest "$out/quick" ;;
full) run nmap -p- --min-rate 5000 -T4 -oA "$out/full""$target"open=$(grep -oE '[0-9]+/open/tcp'"$out/full.gnmap" | cut -d/ -f1 | sort -un | paste -sd, -)||trueif[ -z "$open"]; thenecho"htbscan: no open TCP ports found (host down? try -Pn: nmap -Pn -p- $target)" >&2exit1fiecho"open: $open" >&2 run nmap -sC -sV -p"$open" -oA "$out/targeted""$target" ingest "$out/targeted" ;;
ports) run nmap -sC -sV -p"$plist" -oA "$out/targeted""$target" ingest "$out/targeted" ;;
udp)# Root output in the user's box tree: hand the files back afterwards. run sudo "$(command -v nmap)" -sU --top-ports 100 -oA "$out/udp""$target" sudo chown "$(id -u):$(id -g)""$out"/udp.*
ingest "$out/udp" ;;
esac