recon-scan.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env bash
  2. # Rate-limited recon scan wrapper for the web-pentest skill.
  3. # Wraps nmap + whatweb + curl headers; enforces scope.txt.
  4. #
  5. # Usage: recon-scan.sh <engagement-dir> <target-url>
  6. #
  7. # Example:
  8. # recon-scan.sh engagement-20260525-031415 http://127.0.0.1:9119
  9. set -euo pipefail
  10. ENGAGEMENT_DIR="${1:-}"
  11. TARGET_URL="${2:-}"
  12. if [[ -z "$ENGAGEMENT_DIR" || -z "$TARGET_URL" ]]; then
  13. echo "usage: $0 <engagement-dir> <target-url>" >&2
  14. exit 2
  15. fi
  16. if [[ ! -d "$ENGAGEMENT_DIR" ]]; then
  17. echo "Engagement directory $ENGAGEMENT_DIR does not exist." >&2
  18. echo "Run Phase 0 (engagement setup) first." >&2
  19. exit 2
  20. fi
  21. SCOPE_FILE="$ENGAGEMENT_DIR/scope.txt"
  22. AUTH_FILE="$ENGAGEMENT_DIR/authorization.md"
  23. EVIDENCE_DIR="$ENGAGEMENT_DIR/evidence"
  24. LOG_FILE="$ENGAGEMENT_DIR/request-log.jsonl"
  25. if [[ ! -f "$AUTH_FILE" ]]; then
  26. echo "Missing $AUTH_FILE — no engagement authorization on file." >&2
  27. echo "Fill out templates/authorization.md before running." >&2
  28. exit 3
  29. fi
  30. if [[ ! -f "$SCOPE_FILE" ]]; then
  31. echo "Missing $SCOPE_FILE — no scope allowlist on file." >&2
  32. exit 3
  33. fi
  34. mkdir -p "$EVIDENCE_DIR"
  35. # Extract host from URL.
  36. HOST="$(python3 -c "import sys, urllib.parse as u; print(u.urlparse(sys.argv[1]).hostname or '')" "$TARGET_URL")"
  37. if [[ -z "$HOST" ]]; then
  38. echo "Could not parse host from URL: $TARGET_URL" >&2
  39. exit 4
  40. fi
  41. # Scope check: hostname must appear literally in scope.txt, OR the
  42. # resolved IP must fall inside a CIDR listed there.
  43. in_scope() {
  44. local host="$1"
  45. while IFS= read -r line; do
  46. # strip comments + whitespace
  47. local entry
  48. entry="$(printf '%s' "$line" | sed 's/#.*//' | tr -d '[:space:]')"
  49. [[ -z "$entry" ]] && continue
  50. if [[ "$entry" == "$host" ]]; then
  51. return 0
  52. fi
  53. # If entry is CIDR, check via python
  54. if [[ "$entry" == */* ]]; then
  55. python3 - "$host" "$entry" <<'PY' && return 0
  56. import sys, socket, ipaddress
  57. host, cidr = sys.argv[1], sys.argv[2]
  58. try:
  59. ip = socket.gethostbyname(host)
  60. if ipaddress.ip_address(ip) in ipaddress.ip_network(cidr, strict=False):
  61. sys.exit(0)
  62. except Exception:
  63. pass
  64. sys.exit(1)
  65. PY
  66. fi
  67. done < "$SCOPE_FILE"
  68. return 1
  69. }
  70. if ! in_scope "$HOST"; then
  71. echo "Host '$HOST' is NOT in $SCOPE_FILE. Refusing to scan." >&2
  72. echo "Add it to scope.txt only if it is genuinely authorized." >&2
  73. exit 5
  74. fi
  75. # Resolve URL for logging
  76. TS="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  77. echo "[recon-scan] target=$TARGET_URL host=$HOST ts=$TS"
  78. # --- headers ---
  79. echo "[recon-scan] fetching headers..."
  80. HEADERS_FILE="$EVIDENCE_DIR/headers.txt"
  81. curl -sSIk --max-time 15 -A "hermes-pentest/recon" "$TARGET_URL" > "$HEADERS_FILE" || true
  82. sleep 0.2
  83. # --- whatweb ---
  84. if command -v whatweb >/dev/null 2>&1; then
  85. echo "[recon-scan] running whatweb..."
  86. whatweb -v --no-errors "$TARGET_URL" > "$EVIDENCE_DIR/whatweb.txt" 2>&1 || true
  87. sleep 0.2
  88. else
  89. echo "[recon-scan] whatweb not installed — skipping. Install with: apt install whatweb"
  90. fi
  91. # --- robots / sitemap / .well-known ---
  92. echo "[recon-scan] checking robots/sitemap/.well-known..."
  93. for path in robots.txt sitemap.xml .well-known/security.txt; do
  94. outfile="$EVIDENCE_DIR/$(echo "$path" | tr / _).txt"
  95. curl -sSk --max-time 10 -A "hermes-pentest/recon" -o "$outfile" -w "%{http_code}\n" "$TARGET_URL/$path" \
  96. > "$outfile.status" || true
  97. sleep 0.2
  98. done
  99. # --- nmap (top 100 ports, default scripts off, scope-bounded) ---
  100. if command -v nmap >/dev/null 2>&1; then
  101. echo "[recon-scan] running nmap (top 100 ports, T3, no NSE)..."
  102. nmap -sT -T3 --top-ports 100 -Pn -oN "$EVIDENCE_DIR/nmap.txt" "$HOST" >/dev/null 2>&1 || true
  103. else
  104. echo "[recon-scan] nmap not installed — skipping. Install with: apt install nmap"
  105. fi
  106. # Log entry
  107. printf '{"ts":"%s","phase":"recon","url":"%s","host":"%s","in_scope":true,"evidence_ref":"evidence/"}\n' \
  108. "$TS" "$TARGET_URL" "$HOST" >> "$LOG_FILE"
  109. echo "[recon-scan] done. Evidence in $EVIDENCE_DIR/"