repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
htbscan.shShell121 lines4.3 KiBraw download
# 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]"; exit 0 ;;
  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; exit 2 ;;
    esac
    shift 2
    ;;
esac

target=${1:-}
if [ -z "$target" ] && [ -s "$STATE/target" ]; then target=$(cat "$STATE/target"); fi
target=${target:-${TARGET:-}}
if [ -z "$target" ]; then
  echo "htbscan: no target — htbscan <ip>, or htbbox new / htbbox use <box> first" >&2
  exit 2
fi

box=""
if [ -s "$STATE/box" ] && dir=$(htbbox path 2>/dev/null); then
  box=$(cat "$STATE/box")
  out="$dir/recon"
else
  out="$PWD/recon"
fi
mkdir -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() {
  local speed="$1"; shift
  local -a rs=(-a "$target" -g -u 5000)
  if [ "$speed" = slow ]; then rs=(-a "$target" -g -u 2000 -b 500 -t 2000 --tries 2); fi
  echo "» 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) || true
    if [ -z "$open" ]; then
      echo "htbscan: rustscan found nothing fast — retrying slow (firewall?) + nmap -Pn" >&2
      open=$(rustports slow) || true
      pn="-Pn"
    fi
    if [ -z "$open" ]; then
      echo "htbscan: no open TCP ports even on the slow pass. Host may be down or fully filtered." >&2
      echo "         try manually: rustscan -a $target -u 2000 -b 300 -t 3000 --tries 3 -- -Pn" >&2
      exit 1
    fi
    echo "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, -) || true
    if [ -z "$open" ]; then
      echo "htbscan: no open TCP ports found (host down? try -Pn: nmap -Pn -p- $target)" >&2
      exit 1
    fi
    echo "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