Skip to content

reconFTW is a tool designed to perform automated recon on a target domain by running the best set of tools to perform scanning and finding out vulnerabilities

64.5 KB
Raw Blame
#!/bin/bash
# shellcheck disable=SC2154  # Variables defined in reconftw.cfg
# reconFTW - Mode/workflow orchestration module
# Contains: start, end, build_hotlist, passive, osint, all, vulns,
#           multi_osint, recon, multi_recon, multi_custom,
#           subs_menu, webs_menu, zen_menu, help
# This file is sourced by reconftw.sh - do not execute directly
[[ -z "${SCRIPTPATH:-}" ]] && {
    echo "Error: This module must be sourced by reconftw.sh" >&2
    exit 1
}

function start() {

    global_start=$(date +%s)
    _RECON_CLEAN_EXIT=false  # CR-01 fix: gate _cleanup_inprogress so SIGINT/SIGTERM preserve .inprogress_* sentinels (D-02 clean-exit semantics)
    set +m 2>/dev/null || true

    # Validate configuration before starting
    validate_config || exit $?
    apply_performance_profile

    # Check available disk space before starting (require at least 5GB by default)
    local required_space_gb="${MIN_DISK_SPACE_GB:-5}"
    if ! check_disk_space "$required_space_gb" "."; then
        _print_status WARN "Low disk space detected" "Continuing anyway..."
        # Not exiting, just warning - user can set MIN_DISK_SPACE_GB=0 to disable
    fi

    # Raise ulimit for long-running VPS jobs (fail-soft on failure)
    if [[ ${RAISE_ULIMIT:-true} == "true" ]]; then
        ULIMIT_TARGET=${ULIMIT_TARGET:-65535}
        ulimit -n "$ULIMIT_TARGET" 2>>"${LOGFILE:-/dev/null}" >/dev/null || true
    fi

    # Log version and key flags
    {
        printf "[%s] reconFTW version: %s\n" "$(date +'%Y-%m-%d %H:%M:%S')" "$reconftw_version"
        printf "[%s] Flags: OSINT=%s SUBDOMAINS_GENERAL=%s VULNS_GENERAL=%s DEEP=%s\n" \
            "$(date +'%Y-%m-%d %H:%M:%S')" "$OSINT" "$SUBDOMAINS_GENERAL" "$VULNS_GENERAL" "$DEEP"
    } >>"${LOGFILE:-/dev/null}"
    if [[ $upgrade_before_running == true ]]; then
        "${SCRIPTPATH}/install.sh" --tools
    fi

    # Initialize incremental mode if enabled
    incremental_init

    #[[ -n "$domain" ]] && ipcidr_target $domain

    if [[ -z $domain ]]; then
        if [[ -n $list ]]; then
            if [[ -z $domain ]]; then
                domain="Multi"
                dir="${SCRIPTPATH}/Recon/$domain"
                called_fn_dir="$dir"/.called_fn
            fi
            if [[ $list == /* ]]; then
                mkdir -p "$dir/webs"
                cp "$list" "$dir/webs/webs.txt"
            else
                mkdir -p "$dir/webs"
                cp "${SCRIPTPATH}/${list}" "$dir/webs/webs.txt"
            fi
        fi
    else
        dir="${SCRIPTPATH}/Recon/$domain"
        called_fn_dir="$dir"/.called_fn
    fi

    if [[ -z $domain ]]; then
        notification "${bred} No domain or list provided ${reset}\n\n" error
        exit
    fi

    if [[ ! -d $called_fn_dir ]]; then
        mkdir -p "$called_fn_dir"
    fi
    if [[ "${FORCE_RESCAN:-false}" == "true" ]]; then
        rm -f "$called_fn_dir"/.* 2>>"${LOGFILE:-/dev/null}" || true
        # Explicitly clear .inprogress_* orphans so a --force run never inherits
        # a fake resume condition regardless of shell dotglob settings (D-05).
        rm -f "$called_fn_dir"/.inprogress_* 2>>"${LOGFILE:-/dev/null}" || true
    fi
    mkdir -p "$dir"
    cd "$dir" || {
        print_errorf "Failed to cd directory in %s @ line %s" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    if [[ $AXIOM == true ]]; then
        if [[ -n $domain ]]; then
            echo "$domain" | anew -q target.txt
            list="${dir}/target.txt"
        fi
    fi
    mkdir -p {.log,.tmp,webs,hosts,vulns,osint,screenshots,subdomains}
    chmod 700 .tmp 2>/dev/null || true
    # Baseline inputs expected by multiple modules (avoid pipefail errors on missing files).
    touch webs/webs.txt webs/webs_uncommon_ports.txt webs/webs_all.txt \
        subdomains/subdomains.txt .tmp/web_full_info.txt 2>/dev/null || true

    # Load plugins and emit start event
    plugins_load
    plugins_emit start "$domain" "$dir"

    NOW=$(date +"%F")
    NOWT=$(date +"%T")
    LOGFILE="${dir}/.log/${NOW}_${NOWT}.txt"
    touch ".log/${NOW}_${NOWT}.txt"
    DEBUG_LOG="${dir}/debug.log"
    touch "$DEBUG_LOG"
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"${LOGFILE}"
    enable_command_trace

    # Cache resolver selection once per run (auto mode detects NAT/CGNAT and picks dnsx vs puredns).
    init_dns_resolver

    # Rotate old log files
    rotate_logs "${dir}/.log" "${MAX_LOG_FILES:-10}" "${MAX_LOG_AGE_DAYS:-30}"

    # Trap for cleanup on unexpected exit
    trap 'cleanup_on_exit' INT TERM
    trap '_cleanup_inprogress' EXIT  # silent EXIT-only sentinel sweep (D-02)

    # Initialize structured logging if enabled
    log_init

    # Reset incidents for this run (global arrays consumed by lib/common.sh helpers)
    # shellcheck disable=SC2034
    INCIDENTS_LEVELS=()
    # shellcheck disable=SC2034
    INCIDENTS_ITEMS=()

    # Initialize cache for wordlists/resolvers
    cache_init
    cache_clean "${CACHE_MAX_AGE_DAYS:-30}" 2>>"${LOGFILE:-/dev/null}" || true
    if [[ "${CACHE_REFRESH:-false}" == "true" ]]; then
        notification "Cache refresh forced for this run" warn
    fi

    # init time saved estimator
    TIME_SAVED_EST=0

    # Non-fatal error trap: log and continue (plus short explanation for common cases)
    trap 'rc=$?; ts=$(date +"%Y-%m-%d %H:%M:%S"); cmd=${BASH_COMMAND}; loc_fn=${FUNCNAME[0]:-main}; loc_ln=${BASH_LINENO[0]:-0}; msg="[$ts] ERR($rc) @ ${loc_fn}:${loc_ln} :: ${cmd}"; if [[ -n "${LOGFILE:-}" ]]; then echo "$msg" >>"$LOGFILE"; else echo "$msg" >&2; fi; explain_err "$rc" "$cmd" "$loc_fn" "$loc_ln"' ERR

    if declare -F ui_header >/dev/null 2>&1; then
        ui_header
    else
        _print_rule
        printf "%b  Target: %s%b\n" "${bred:-}" "$domain" "${reset:-}"
        if [[ "${OUTPUT_VERBOSITY:-1}" -ge 2 ]]; then
            printf "%b  Mode:   %s | Parallel: %s | Verbosity: %s%b\n" \
                "${bblue:-}" "${opt_mode:-r}" "${PARALLEL_MODE:-true}" "${OUTPUT_VERBOSITY:-1}" "${reset:-}"
        fi
        _print_rule
    fi

    if [[ "${FORCE_RESCAN:-false}" == "true" ]]; then
        _print_msg WARN "Force rescan enabled: ignoring cached module markers"
    fi

    # Resume banner (D-04): detect .inprogress_<fn> sentinels left behind by a
    # prior crashed run and emit exactly ONE WARN line naming the affected
    # functions. Guarded against unset called_fn_dir and skipped under
    # FORCE_RESCAN (Step C already wiped any orphans there).
    if [[ -n "${called_fn_dir:-}" ]] && [[ "${FORCE_RESCAN:-false}" != "true" ]]; then
        local -a _leftover
        mapfile -t _leftover < <(ls -1 "${called_fn_dir}"/.inprogress_* 2>/dev/null)
        if ((${#_leftover[@]} > 0)); then
            local _prefix="${called_fn_dir}/.inprogress_"
            local -a _names=()
            local _f
            for _f in "${_leftover[@]}"; do
                _names+=("${_f#"$_prefix"}")
            done
            local _joined
            _joined=$(IFS=,; printf "%s" "${_names[*]}")
            _print_msg WARN "resume: ${#_leftover[@]} functions re-running after interruption (${_joined})"
            log_json "WARN" "resume" "Inprogress sentinels found" "reason=inprogress_leftover" "funcs=${_joined}"
        fi
    fi

    if [[ "${MONITOR_MODE:-false}" == "true" ]] && [[ "${MONITOR_CYCLE:-1}" -gt 1 ]]; then
        notification "Monitor cycle ${MONITOR_CYCLE}: skipping repeated tools check" info
    else
        tools_installed
    fi

}

# Prepare the web-mode output dir for a clean run. Behavior differs between
# `-l` (shared "Multi" dir, scope redefined per invocation) and `-d`
# (per-domain dir, scope stable across invocations):
#
# The Multi dir is created only by the `-l -w` flow, so nothing in it predates
# a `-w` invocation — the entire contents (other than cross-run tracking) can
# be wiped to guarantee scope isolation between distinct lists.
#
# Per-domain dirs may have been populated by prior `-a`/`-s`/`-p` runs, so
# non-web state must survive.
#
# Always wiped (pure web-pipeline artifacts that anew-accumulate):
#   webs/*, .tmp/*, js/*, fuzzing/*, gf/*, screenshots/*, assets.jsonl
#
# Additionally wiped in `-l` Multi mode (no prior non-web state to protect):
#   vulns/*      — nuclei/cms/swagger/graphql findings
#   subdomains/* — subtakeover, sub_js_extract, tls_ip_pivots artifacts
#   hosts/*      — grpc_reflection.txt and any hosts/ writer in webs_menu
#   osint/*      — defensive wipe (no webs_menu writers today, but prevents
#                   accidental leakage if a module grows an osint/ output path)
#
# Always preserved (cross-run tracking):
#   .log/*, .incremental/*, .called_fn/*, debug.log
#
# For `-d`: vulns/, subdomains/ (except subdomains.txt append), hosts/, osint/
# are preserved so prior recon state survives. webs_menu modules using anew
# will add new findings atop old ones (default incremental-but-untracked
# behavior).
#
# subdomains/subdomains.txt handling:
#   -l Multi:   OVERWRITE with sanitized hostnames from the list (scope reset)
#   -d <dom>:   APPEND the target via anew so enumerated subs from prior
#               `-a`/`-s` remain as probe input
#
# Used by: reconftw.sh -w dispatch, monitor_mode 'w' case (every cycle).
function prepare_web_mode_scope() {
    # Branch on $flist, NOT $list (issue #1045).
    #
    # $flist is the resolved absolute path of the user's -l file and is set ONCE,
    # at startup in reconftw.sh, from whatever $list held then; with -d it is set
    # to '' and stays empty for the run. So $flist is the signal for "the user
    # passed a list", and it is the variable this branch actually operates on
    # (the cp below, and the -not -path guard further down).
    #
    # $list is NOT that signal, because start() reassigns it mid-run: under
    # AXIOM with -d it writes the domain to target.txt and sets
    # list="${dir}/target.txt" (modules/modes.sh, start). Branching on $list
    # therefore sent `reconftw.sh -d example.com -w -v 50` down the -l path with
    # $flist still empty, so the snapshot `cp "" <tmp>` failed and scope prep
    # returned 1 — aborting the run before webs_menu with
    # "Web mode scope preparation failed (tmpfile/cp error)".
    if [[ -n $flist ]]; then
        # Defense-in-depth for the user's input file: treat $flist as strictly
        # read-only. One initial `cp` to a tmp snapshot outside $dir; every
        # subsequent operation uses the snapshot. The original is never written,
        # deleted, or re-referenced — so even if -path matching has edge cases
        # (trailing slash, symlinks, case folding) the original is untouched.
        local _list_snapshot
        if ! _list_snapshot=$(mktemp -t reconftw_web_list.XXXXXX); then
            return 1
        fi
        if ! cp "$flist" "$_list_snapshot" 2>/dev/null; then
            rm -f "$_list_snapshot" 2>/dev/null
            return 1
        fi

        # Sanitize + stage OUTSIDE $dir first. Only wipe $dir after we know we
        # have a non-empty, validated new scope — otherwise a fail-close path
        # would destroy prior output dirs and leave the caller with no scope
        # AND no prior state. `| sort -u > file` hides the while-loop rc, so
        # we capture `$?` of the whole pipeline (which is `sort`'s + the
        # redirection) and also check the staged file is non-empty.
        local _subs_stage
        if ! _subs_stage=$(mktemp -t reconftw_web_subs.XXXXXX); then
            rm -f "$_list_snapshot" 2>/dev/null
            return 1
        fi
        local _t _write_rc
        while IFS= read -r _t <&3 || [[ -n "$_t" ]]; do
            _t=${_t%$'\r'}
            [[ -z "$_t" ]] && continue
            _t=$(_sanitize_list_entry "$_t") || continue
            printf '%s\n' "$_t"
        done 3<"$_list_snapshot" | sort -u >"$_subs_stage"
        _write_rc=$?

        rm -f "$_list_snapshot" 2>/dev/null

        if (( _write_rc != 0 )) || [[ ! -s "$_subs_stage" ]]; then
            # Empty/failed staging → every entry rejected by sanitization, or
            # the write failed. Abort WITHOUT wiping $dir so prior state
            # survives the failed prep.
            rm -f "$_subs_stage" 2>/dev/null
            return 1
        fi

        # Validation passed → safe to wipe and install the new scope atomically.
        # -l Multi: the dir is only ever created/populated by -l -w flow, so no
        # non-web state needs protecting. Wipe every file under $dir except
        # cross-run tracking and $flist (belt-and-suspenders in case the user's
        # list path lives inside $dir).
        find "$dir" -mindepth 1 -type f \
            -not -path "$dir/.log/*" \
            -not -path "$dir/.incremental/*" \
            -not -path "$dir/.called_fn/*" \
            -not -path "$dir/debug.log" \
            -not -path "$flist" \
            -delete 2>/dev/null || true

        mkdir -p "$dir/subdomains" 2>/dev/null
        if ! mv "$_subs_stage" "$dir/subdomains/subdomains.txt"; then
            rm -f "$_subs_stage" 2>/dev/null
            return 1
        fi
        return 0
    else
        # -d <domain>: the per-domain dir may hold prior -a/-s/-p recon state
        # that must survive (hosts/ips.txt for virtualhosts, subdomains enum,
        # vulns/ findings history, osint/, etc.). Wipe only pure web-pipeline
        # artifacts that modules append via anew/>> and would otherwise mix
        # prior-run data with this run's probe output.
        local _p
        for _p in webs .tmp js fuzzing gf screenshots; do
            [[ -d "$dir/$_p" ]] && find "$dir/$_p" -mindepth 1 -type f -delete 2>/dev/null || true
        done
        : >"$dir/assets.jsonl" 2>/dev/null || true

        mkdir -p "$dir/subdomains" 2>/dev/null
        if ! printf '%s\n' "$domain" | anew -q "$dir/subdomains/subdomains.txt"; then
            return 1
        fi
        [[ -s "$dir/subdomains/subdomains.txt" ]] || return 1
        return 0
    fi
}

function end() {

    if [[ $opt_ai ]]; then
        notification "Sending ${domain} data to AI" info
        mkdir -p "${dir}/ai_result" 2>>"${LOGFILE}"
        local ai_script ai_prompts_file ai_venv_python ai_python ai_json_output ai_redact_flag ai_pull_flag ai_strict_flag
        local -a ai_cmd
        ai_script="${tools}/reconftw_ai/reconftw_ai.py"
        ai_prompts_file="${AI_PROMPTS_FILE:-${tools}/reconftw_ai/prompts.json}"
        ai_venv_python="${tools}/reconftw_ai/venv/bin/python3"
        ai_python="${AI_EXECUTABLE:-python3}"
        ai_json_output="${dir}/ai_result/reconftw_analysis.json"
        ai_redact_flag="--redact"
        ai_pull_flag=""
        ai_strict_flag=""

        if [[ "${AI_REDACT:-true}" != "true" ]]; then
            ai_redact_flag="--no-redact"
        fi
        if [[ "${AI_ALLOW_MODEL_PULL:-false}" == "true" ]]; then
            ai_pull_flag="--allow-model-pull"
        fi
        if [[ "${AI_STRICT:-false}" == "true" ]]; then
            ai_strict_flag="--strict"
        fi

        if [[ -x "${ai_venv_python}" ]]; then
            ai_python="${ai_venv_python}"
        elif ! command -v "${ai_python}" >/dev/null 2>&1; then
            notification "AI skipped: Python executable not found (${ai_python})" warn
            ai_python=""
        fi

        if [[ -n "${ai_python}" && -f "${ai_script}" ]]; then
            ai_cmd=(
                "${ai_python}" "${ai_script}"
                --results-dir "${dir}" \
                --output-dir "${dir}/ai_result" \
                --output-json "${ai_json_output}" \
                --model "${AI_MODEL}" \
                --output-format "${AI_REPORT_TYPE}" \
                --report-type "${AI_REPORT_PROFILE}" \
                --prompts-file "${ai_prompts_file}" \
                --max-chars-per-file "${AI_MAX_CHARS_PER_FILE:-50000}" \
                --max-files-per-category "${AI_MAX_FILES_PER_CATEGORY:-200}" \
                "${ai_redact_flag}"
            )
            if [[ -n "${ai_pull_flag}" ]]; then
                ai_cmd+=("${ai_pull_flag}")
            fi
            if [[ -n "${ai_strict_flag}" ]]; then
                ai_cmd+=("${ai_strict_flag}")
            fi
            if ! "${ai_cmd[@]}" 2>>"${LOGFILE}" >/dev/null; then
                notification "AI report failed; check ${DEBUG_LOG} for details" warn
            fi
        elif [[ -n "${ai_python}" ]]; then
            notification "AI skipped: reconftw_ai script not found at ${ai_script}" warn
        fi
    fi

    find "$dir" -type f -empty -print | grep -v '.called_fn' | grep -v '.log' | grep -v '.tmp' | xargs -r rm -f -- 2>>"$LOGFILE" >/dev/null
    find "$dir" -type d -empty -print -delete 2>>"$LOGFILE" >/dev/null

    echo "[$(date +'%Y-%m-%d %H:%M:%S')] End" >>"${LOGFILE}"

    if [[ $PRESERVE != true ]]; then
        find "$dir" -type f -empty | grep -v "called_fn" | xargs -r rm -f -- 2>>"$LOGFILE" >/dev/null
        find "$dir" -type d -empty | grep -v "called_fn" | xargs -r rm -rf -- 2>>"$LOGFILE" >/dev/null
    fi

    if [[ $REMOVETMP == true ]]; then
        rm -rf -- "$dir/.tmp"
    fi

    if [[ $REMOVELOG == true ]]; then
        rm -rf -- "$dir/.log"
    fi

    if [[ $FARADAY == true ]]; then
        if ! faraday-cli status 2>>"$LOGFILE" >/dev/null; then
            _print_error "Faraday server is not running. Skipping Faraday integration"
        else
            if [[ -s ".tmp/tko_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei .tmp/tko_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "hosts/portscan_active.xml" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nmap hosts/portscan_active.xml 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s ".tmp/fuzzparams_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei .tmp/fuzzparams_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "nuclei_output/info_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei nuclei_output/info_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "nuclei_output/low_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei nuclei_output/low_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "nuclei_output/medium_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei nuclei_output/medium_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "nuclei_output/high_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei nuclei_output/high_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            if [[ -s "nuclei_output/critical_json.txt" ]]; then
                run_command faraday-cli tool report -w "$FARADAY_WORKSPACE" --plugin-id nuclei nuclei_output/critical_json.txt 2>>"$LOGFILE" >/dev/null
            fi
            notification "Information sent to Faraday" "good"
        fi
    fi

    if [[ -n $dir_output ]]; then
        output
        finaldir=$dir_output
    else
        finaldir=$dir
    fi
    #Zip the output folder and send it via tg/discord/slack
    if [[ $SENDZIPNOTIFY == true ]]; then
        zipSendOutputFolder
    fi

    # Screenshot diffs (hashing)
    if [[ -d screenshots ]]; then
        if [[ -f screenshots/hashes.txt ]]; then mv screenshots/hashes.txt screenshots/hashes_prev.txt 2>>"$LOGFILE" || true; fi
        # shellcheck disable=SC2016  # $1 is intentionally for sh -c, not bash
        find screenshots -type f -name '*.png' -print0 | xargs -0 -I{} sh -c 'sha256sum "$1" 2>/dev/null || shasum -a 256 "$1"' -- {} | sed 's|  ./||' >screenshots/hashes.txt 2>>"$LOGFILE" || true
        if [[ -f screenshots/hashes_prev.txt ]]; then
            comm -3 <(cut -d' ' -f1,2 screenshots/hashes_prev.txt | sort) <(cut -d' ' -f1,2 screenshots/hashes.txt | sort) | awk '{print $2}' | sed '/^$/d' >screenshots/diff_changed.txt 2>>"$LOGFILE" || true
        fi
    fi

    # Emit end plugin event
    plugins_emit end "$domain" "$dir"

    # Build hotlist (risk summary)
    build_hotlist || true

    # Incremental mode report
    incremental_report

    global_end=$(date +%s)
    getElapsedTime "$global_start" "$global_end"
    if declare -F ui_summary >/dev/null 2>&1; then
        if [[ "${RECON_PARTIAL_RUN:-false}" == "true" ]]; then
            _print_status WARN "Run completed with non-fatal warnings" "osint_parallel=${RECON_OSINT_PARALLEL_FAILURES:-0}"
        fi
        local subs_count webs_count
        local -A vulns_count=( [critical]=0 [high]=0 [medium]=0 [low]=0 [info]=0 )
        subs_count=$(count_lines "${dir}/subdomains/subdomains.txt")
        webs_count=$(count_lines "${dir}/webs/webs_all.txt")
        
        if [[ -d "${dir}/nuclei_output" ]]; then
            for sev in "${!vulns_count[@]}"; do
                if [[ -s "${dir}/nuclei_output/${sev}.txt" ]]; then
                    vulns_count[$sev]=$(count_lines "${dir}/nuclei_output/${sev}.txt")
                fi
            done
        fi
        local mode_label="FULL"
        case "${opt_mode:-r}" in
            n) mode_label="OSINT-ONLY" ;;
            w) mode_label="WEB" ;;
            s) mode_label="SUBDOMAINS" ;;
            p) mode_label="PASSIVE" ;;
            a) mode_label="ALL" ;;
            z) mode_label="ZEN" ;;
        esac
        if [[ "${opt_mode:-r}" == "n" ]]; then
            subs_count="N/A (OSINT-only)"
            webs_count="N/A (OSINT-only)"
        fi
        ui_summary "$domain" "$runtime" "$finaldir" "$mode_label" "${subs_count:-0}" "${webs_count:-0}" \
            "${vulns_count[critical]}" "${vulns_count[high]}" "${vulns_count[medium]}" \
            "${vulns_count[low]}" "${vulns_count[info]}"
        print_incidents "${DEBUG_LOG:-}"
    else
        _print_section "Scan Complete"
        if [[ "${RECON_PARTIAL_RUN:-false}" == "true" ]]; then
            _print_status WARN "Run completed with non-fatal warnings" "osint_parallel=${RECON_OSINT_PARALLEL_FAILURES:-0}"
        fi
        printf "%b  Target:   %s%b\n" "${bgreen:-}" "$domain" "${reset:-}"
        printf "%b  Duration: %s%b\n" "${bgreen:-}" "$runtime" "${reset:-}"
        printf "%b  Output:   %s%b\n" "${bgreen:-}" "$finaldir" "${reset:-}"
        _print_rule
        print_incidents "${DEBUG_LOG:-}"
    fi
    notification "Finished Recon on: ${domain} under ${finaldir} in: ${runtime}" good "$(date +'%Y-%m-%d %H:%M:%S')"
    [ "$SOFT_NOTIFICATION" = true ] && echo "[$(date +'%Y-%m-%d %H:%M:%S')] Finished Recon on: ${domain} under ${finaldir} in: ${runtime}" | notify -silent

    # Print performance timing summary
    print_timing_summary
    write_perf_summary
    if [[ "${NO_REPORT:-false}" != "true" ]]; then
        generate_consolidated_report || true
        export_reports || true
    fi

    # Mark clean traversal so the EXIT trap's _cleanup_inprogress sweeps
    # .inprogress_* sentinels (D-02 clean-exit semantics; CR-01 fix). Any
    # path that bypasses end() — SIGINT/SIGTERM via cleanup_on_exit, an
    # internal abort like _abort_disk_full's exit 1, or an unhandled error
    # — leaves the flag at false and the sentinels survive to drive the
    # next run's resume banner.
    _RECON_CLEAN_EXIT=true
}

function build_hotlist() {
    mkdir -p .tmp
    declare -A score
    # Nuclei high/critical
    for f in nuclei_output/high_json.txt nuclei_output/critical_json.txt; do
        [[ -s $f ]] || continue
        while read -r h; do
            [[ -z $h ]] && continue
            score["$h"]=$((${score["$h"]:-0} + 10))
        done < <(jq -r '.["matched-at"] // .host' "$f" 2>/dev/null)
    done
    # Takeovers
    [[ -s webs/takeover.txt ]] && while read -r h; do score["$h"]=$((${score["$h"]:-0} + 8)); done <webs/takeover.txt
    # Secrets
    for s in js/js_secrets.txt js/js_secrets_jsmap.txt js/js_secrets_jsmap_jsluice.txt; do
        [[ -s $s ]] || continue
        while read -r h; do score["$h"]=$((${score["$h"]:-0} + 6)); done < <(awk '{print $1}' "$s")
    done
    # Favicon technology fingerprints (favirecon)
    if [[ -s webs/favirecon.txt ]]; then
        while read -r line; do
            [[ -z $line ]] && continue
            host=$(printf '%s' "$line" | awk '{print $1}' | awk -F/ '{print $3}' | sed 's/:.*$//')
            [[ -z $host ]] && continue
            score["$host"]=$((${score["$host"]:-0} + 4))
        done <webs/favirecon.txt
    fi
    # New assets bonus
    for p in .tmp/subs_new_only.txt webs/url_extract.txt; do
        [[ -s $p ]] || continue
        while read -r l; do
            host=$(echo "$l" | unfurl -u domains 2>/dev/null)
            [[ -z $host ]] && continue
            score["$host"]=$((${score["$host"]:-0} + 2))
        done <"$p"
    done
    # Emit hotlist sorted
    {
        for k in "${!score[@]}"; do printf "%s %s\n" "${score[$k]}" "$k"; done | sort -nr | head -n "${HOTLIST_TOP:-50}"
    } >hotlist.txt
    [[ -s hotlist.txt ]] && notification "Hotlist ready (top ${HOTLIST_TOP:-50})" info
}

###############################################################################################################
########################################### MODES & MENUS #####################################################
###############################################################################################################

function passive() {
    start

    _print_section "OSINT"

    domain_info
    ip_info
    emails
    google_dorks
    github_dorks
    github_repos
    github_leaks
    metadata
    apileaks
    third_party_misconfigs

    # Save original values before overriding for passive-only mode
    local _saved_SUBNOERROR="${SUBNOERROR:-}" _saved_SUBANALYTICS="${SUBANALYTICS:-}"
    local _saved_SUBBRUTE="${SUBBRUTE:-}" _saved_SUBSCRAPING="${SUBSCRAPING:-}"
    local _saved_SUBPERMUTE="${SUBPERMUTE:-}" _saved_SUBREGEXPERMUTE="${SUBREGEXPERMUTE:-}"
    local _saved_SUB_RECURSIVE_BRUTE="${SUB_RECURSIVE_BRUTE:-}"
    local _saved_PORTSCAN_ACTIVE="${PORTSCAN_ACTIVE:-}"
    local _saved_PTR_SWEEP="${PTR_SWEEP:-}" _saved_NS_DELEGATION="${NS_DELEGATION:-}"
    local _saved_TLS_IP_PIVOTS="${TLS_IP_PIVOTS:-}"

    # shellcheck disable=SC2034  # These globals are consumed by downstream module functions
    SUBNOERROR=false
    # shellcheck disable=SC2034
    SUBANALYTICS=false
    # shellcheck disable=SC2034
    SUBBRUTE=false
    # shellcheck disable=SC2034
    SUBSCRAPING=false
    # shellcheck disable=SC2034
    SUBPERMUTE=false
    # shellcheck disable=SC2034
    SUBREGEXPERMUTE=false
    # shellcheck disable=SC2034
    SUB_RECURSIVE_BRUTE=false
    # shellcheck disable=SC2034
    PTR_SWEEP=false
    # shellcheck disable=SC2034
    NS_DELEGATION=false
    # shellcheck disable=SC2034
    TLS_IP_PIVOTS=false
    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    _print_section "Subdomains"

    run_module_with_axiom_failover subdomains_full
    remove_big_files

    _print_section "Web Detection"

    run_module_with_axiom_failover cdnprovider
    # shellcheck disable=SC2034  # PORTSCAN_ACTIVE controls scan behavior
    PORTSCAN_ACTIVE=false
    run_module_with_axiom_failover portscan
    run_module_with_axiom_failover geo_info

    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi

    # Restore original values so subsequent modes (e.g. monitor) are not affected
    SUBNOERROR="${_saved_SUBNOERROR}"
    SUBANALYTICS="${_saved_SUBANALYTICS}"
    SUBBRUTE="${_saved_SUBBRUTE}"
    SUBSCRAPING="${_saved_SUBSCRAPING}"
    SUBPERMUTE="${_saved_SUBPERMUTE}"
    SUBREGEXPERMUTE="${_saved_SUBREGEXPERMUTE}"
    SUB_RECURSIVE_BRUTE="${_saved_SUB_RECURSIVE_BRUTE}"
    PORTSCAN_ACTIVE="${_saved_PORTSCAN_ACTIVE}"
    PTR_SWEEP="${_saved_PTR_SWEEP}"
    NS_DELEGATION="${_saved_NS_DELEGATION}"
    TLS_IP_PIVOTS="${_saved_TLS_IP_PIVOTS}"

    end
}

function all() {
    start
    recon
    vulns
    end
}

function osint() {
    if [[ "${PARALLEL_MODE:-false}" != "false" ]]; then
        # Group 1: Independent lookups (no shared state)
        parallel_funcs "${PAR_OSINT_GROUP1_SIZE:-4}" \
            domain_info ip_info emails google_dorks github_dorks
        # Group 2: GitHub/API analysis
        parallel_funcs "${PAR_OSINT_GROUP2_SIZE:-3}" \
            github_repos github_leaks github_actions_audit
        # Group 3: Metadata, API leaks, third-party checks
        parallel_funcs "${PAR_OSINT_GROUP3_SIZE:-4}" \
            metadata apileaks third_party_misconfigs zonetransfer
        # Group 4: Mail/DNS/Cloud
        parallel_funcs "${PAR_OSINT_GROUP4_SIZE:-3}" \
            mail_hygiene spoof cloud_enum_scan lunar_exposure
    else
        domain_info
        ip_info
        emails
        google_dorks
        github_dorks
        github_repos
        github_leaks
        github_actions_audit
        metadata
        apileaks
        third_party_misconfigs
        zonetransfer
        mail_hygiene
        spoof
        cloud_enum_scan
        lunar_exposure
    fi
}

run_module_with_axiom_failover() {
    local module_fn="$1"
    shift || true

    if ! declare -F "$module_fn" >/dev/null 2>&1; then
        notification "Failover wrapper: unknown function '${module_fn}'" warn
        return 1
    fi

    local axiom_before=false
    if axiom_runtime_enabled; then
        axiom_before=true
    fi

    "$module_fn" "$@"
    local rc=$?

    if [[ "$axiom_before" == "true" ]] \
        && [[ "${AXIOM_RUNTIME_DISABLED:-false}" == "true" ]] \
        && [[ "${AXIOM_FAILOVER_RETRY_ACTIVE:-false}" != "true" ]]; then
        local prev_diff="${DIFF:-}"
        local had_diff=false
        [[ -v DIFF ]] && had_diff=true
        local prev_retry="${AXIOM_FAILOVER_RETRY_ACTIVE:-false}"
        local fail_reason="${AXIOM_RUNTIME_DISABLE_REASON:-unknown reason}"
        local fail_cmd="${AXIOM_RUNTIME_DISABLE_CMD:-unknown command}"
        AXIOM_FAILOVER_RETRY_ACTIVE=true

        notification "Axiom failover in ${module_fn}; retrying once locally (${fail_reason})" warn
        log_note "retrying module locally after axiom failover: module='${module_fn}' reason='${fail_reason}' cmd='${fail_cmd}'" "${FUNCNAME[0]}" "${LINENO}"

        DIFF=true
        "$module_fn" "$@"
        rc=$?

        if [[ "$had_diff" == "true" ]]; then
            DIFF="$prev_diff"
        else
            unset DIFF
        fi
        AXIOM_FAILOVER_RETRY_ACTIVE="$prev_retry"
    fi

    return "$rc"
}

function vulns() {
    _print_section "Vulnerability Checks"
    if [[ $VULNS_GENERAL == true ]]; then
        if [[ "${PARALLEL_MODE:-true}" == "true" ]] && declare -f parallel_funcs &>/dev/null && ! axiom_runtime_enabled; then
            # Parallel execution - group independent checks
            [[ "${OUTPUT_VERBOSITY:-1}" -ge 2 ]] && printf "%b[*] Running vulnerability checks in parallel mode%b\n" "$bblue" "$reset"
            parallel_funcs "${PAR_VULNS_GROUP1_SIZE:-4}" crlf_checks xss ssrf_checks lfi
            local vulns_g1_rc=$?
            if ((vulns_g1_rc > 0)); then
                if [[ "${CONTINUE_ON_TOOL_ERROR:-true}" == "true" ]]; then
                    RECON_PARTIAL_RUN=true
                    notification "Parallel vulns group 1 completed with ${vulns_g1_rc} warning(s); continuing" warn
                else
                    notification "Parallel vulns batch failed (group 1)" error
                    return 1
                fi
            fi
            parallel_funcs "${PAR_VULNS_GROUP2_SIZE:-4}" ssti sqli command_injection smuggling
            local vulns_g2_rc=$?
            if ((vulns_g2_rc > 0)); then
                if [[ "${CONTINUE_ON_TOOL_ERROR:-true}" == "true" ]]; then
                    RECON_PARTIAL_RUN=true
                    notification "Parallel vulns group 2 completed with ${vulns_g2_rc} warning(s); continuing" warn
                else
                    notification "Parallel vulns batch failed (group 2)" error
                    return 1
                fi
            fi
            parallel_funcs "${PAR_VULNS_GROUP4_SIZE:-3}" webcache spraying brokenLinks fray_checks
            local vulns_g4_rc=$?
            if ((vulns_g4_rc > 0)); then
                if [[ "${CONTINUE_ON_TOOL_ERROR:-true}" == "true" ]]; then
                    RECON_PARTIAL_RUN=true
                    notification "Parallel vulns group 4 completed with ${vulns_g4_rc} warning(s); continuing" warn
                else
                    notification "Parallel vulns batch failed (group 4)" error
                    return 1
                fi
            fi
            fuzzparams
            nuclei_dast
            4xxbypass
            test_ssl
        else
            crlf_checks
            run_module_with_axiom_failover xss
            ssrf_checks
            lfi
            ssti
            sqli
            command_injection
            smuggling
            webcache
            spraying
            run_module_with_axiom_failover brokenLinks
            fray_checks
            run_module_with_axiom_failover fuzzparams
            run_module_with_axiom_failover nuclei_dast
            4xxbypass
            test_ssl
        fi
    else
        _print_msg WARN "Vulnerability module disabled (VULNS_GENERAL=false). Use -a flag or set VULNS_GENERAL=true in config."
    fi
}

function multi_osint() {

    global_start=$(date +%s)

    #[[ -n "$domain" ]] && ipcidr_target $domain

    if [[ -s "$list" ]]; then
        sed_i 's/\r$//' "$list"
    else
        notification "Target list not provided" error
        exit
    fi

    workdir="${SCRIPTPATH}/Recon/${multi}"
    mkdir -p "$workdir" || {
        print_errorf "Failed to create directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    if [[ "${FORCE_RESCAN:-false}" == "true" ]]; then
        rm -f "$workdir"/.called_fn/.* 2>>"${LOGFILE:-/dev/null}" || true
    fi
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    mkdir -p {.log,.tmp,webs,hosts,vulns,osint,screenshots,subdomains}
    touch webs/webs.txt webs/webs_uncommon_ports.txt webs/webs_all.txt \
        subdomains/subdomains.txt .tmp/web_full_info.txt 2>/dev/null || true

    NOW=$(date +"%F")
    NOWT=$(date +"%T")
    LOGFILE="${workdir}/.log/${NOW}_${NOWT}.txt"
    touch ".log/${NOW}_${NOWT}.txt"
    DEBUG_LOG="${workdir}/debug.log"
    touch "$DEBUG_LOG"
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"${LOGFILE}"
    init_dns_resolver
    enable_command_trace

    while IFS= read -r domain || [[ -n "$domain" ]]; do
        [[ -z "$domain" ]] && continue
        dir="$workdir/targets/$domain"
        called_fn_dir="$dir/.called_fn"
        mkdir -p "$dir"
        cd "$dir" || {
            print_errorf "Failed to cd directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }
        mkdir -p {.log,.tmp,webs,hosts,vulns,osint,screenshots,subdomains}
        touch webs/webs.txt webs/webs_uncommon_ports.txt webs/webs_all.txt \
            subdomains/subdomains.txt .tmp/web_full_info.txt 2>/dev/null || true
        NOW=$(date +"%F")
        NOWT=$(date +"%T")
        LOGFILE="${dir}/.log/${NOW}_${NOWT}.txt"
        touch ".log/${NOW}_${NOWT}.txt"
        DEBUG_LOG="${dir}/debug.log"
        touch "$DEBUG_LOG"
        echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"${LOGFILE}"
        domain_info
        ip_info
        emails
        google_dorks
        #github_dorks
        github_repos
        github_leaks
        metadata
        apileaks
        third_party_misconfigs
        zonetransfer
        cloud_enum_scan
    done <"$list"
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    dir=$workdir
    domain=$multi
    end
}

function recon() {
    RECON_PARTIAL_RUN=false
    RECON_OSINT_PARALLEL_FAILURES=0

    # Initialize module-level progress (7 modules: OSINT, Subdomains, Web Detection, Web Analysis, Finalization, + 2 optional)
    local module_total=5
    progress_module_init "$module_total"

    _print_section "OSINT"

    if [[ "${PARALLEL_MODE:-true}" == "true" ]] && declare -f parallel_funcs &>/dev/null; then
        # Group 1: balanced with third_party_misconfigs (slow) alongside fast ones
        parallel_funcs "${PAR_OSINT_GROUP1_SIZE:-5}" domain_info ip_info emails google_dorks third_party_misconfigs
        local osint_g1_rc=$?
        if ((osint_g1_rc > 0)); then
            RECON_PARTIAL_RUN=true
            RECON_OSINT_PARALLEL_FAILURES=$((RECON_OSINT_PARALLEL_FAILURES + osint_g1_rc))
        fi
        # Group 2: remaining OSINT + zonetransfer
        parallel_funcs "${PAR_OSINT_GROUP2_SIZE:-5}" github_repos github_leaks github_actions_audit metadata apileaks zonetransfer
        local osint_g2_rc=$?
        if ((osint_g2_rc > 0)); then
            RECON_PARTIAL_RUN=true
            RECON_OSINT_PARALLEL_FAILURES=$((RECON_OSINT_PARALLEL_FAILURES + osint_g2_rc))
        fi
        if [[ "${S3BUCKETS:-false}" != "true" ]]; then
            cloud_enum_scan
        fi
    else
        domain_info
        ip_info
        emails
        google_dorks
        #github_dorks
        github_repos
        github_leaks
        github_actions_audit
        metadata
        apileaks
        third_party_misconfigs
        zonetransfer
        if [[ "${S3BUCKETS:-false}" != "true" ]]; then
            cloud_enum_scan
        fi
    fi

    ui_module_end "OSINT" "osint/" "subdomains/"
    progress_module "OSINT"

    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    _print_section "Subdomains"

    run_module_with_axiom_failover subdomains_full
    run_module_with_axiom_failover subtakeover
    remove_big_files
    run_module_with_axiom_failover s3buckets

    ui_module_end "Subdomains" "subdomains/" "webs/"
    progress_module "Subdomains"

    _print_section "Web Detection"
    run_module_with_axiom_failover webprobe_full

    if [[ "${PARALLEL_MODE:-true}" == "true" ]] && declare -f parallel_funcs &>/dev/null && ! axiom_runtime_enabled; then
        parallel_funcs "${PAR_WEB_DETECT_GROUP_SIZE:-3}" screenshot cdnprovider portscan favirecon_tech
        local webhost_rc=$?
        if ((webhost_rc > 0)); then
            if [[ "${CONTINUE_ON_TOOL_ERROR:-true}" == "true" ]]; then
                RECON_PARTIAL_RUN=true
            else
                notification "Parallel web/host batch failed" error
                return 1
            fi
        fi
        geo_info
        tls_ip_pivots
        virtualhosts
    else
        run_module_with_axiom_failover screenshot
        run_module_with_axiom_failover cdnprovider
        run_module_with_axiom_failover portscan
        favirecon_tech
        run_module_with_axiom_failover geo_info
        tls_ip_pivots
        virtualhosts
    fi

    ui_module_end "Web Detection" "webs/" "hosts/" "screenshots/"
    progress_module "Web Detection"

    # Quick-rescan gating
    subs_new=$(cat .tmp/subs_new_count 2>/dev/null || echo 1)
    webs_new=$(cat .tmp/webs_new_count 2>/dev/null || echo 1)
    if [[ $QUICK_RESCAN == true && $subs_new -eq 0 && $webs_new -eq 0 ]]; then
        _print_status SKIP "Web Analysis" "(no new subs/webs)"
        TIME_SAVED_EST=$((${TIME_SAVED_EST:-0} + \
            ${TIME_EST_WAF:-0} + ${TIME_EST_NUCLEI:-600} + ${TIME_EST_API:-300} + ${TIME_EST_GQL:-180} + \
            ${TIME_EST_FUZZ:-900} + ${TIME_EST_IIS:-60} + ${TIME_EST_SJ:-120} + ${TIME_EST_URLCHECKS:-300} + ${TIME_EST_JSCHECKS:-300} + \
            ${TIME_EST_PARAM:-240} + ${TIME_EST_GRPC:-120}))
    else
        _print_section "Web Analysis"

        # Heavy scanning functions run sequentially to avoid overwhelming targets
        run_module_with_axiom_failover waf_checks
        run_module_with_axiom_failover nuclei_check
        run_module_with_axiom_failover graphql_scan
        run_module_with_axiom_failover fuzz
        run_module_with_axiom_failover iishortname
        swagger_check
        run_module_with_axiom_failover urlchecks
        run_module_with_axiom_failover jschecks
        sub_js_extract
        well_known_pivots

        # Lightweight functions can run in parallel (minimal target load)
        if [[ "${PARALLEL_MODE:-true}" == "true" ]] && declare -f parallel_funcs &>/dev/null; then
            parallel_funcs "${PAR_WEB_ANALYSIS_LIGHT_SIZE:-3}" websocket_checks grpc_reflection llm_probe
        else
            websocket_checks
            grpc_reflection
            llm_probe
        fi

        # param_discovery runs locally only (arjun not available in axiom images)
        param_discovery

        ui_module_end "Web Analysis" "webs/" "hosts/" "vulns/" "nuclei_output/" "js/" "fuzzing/"
        progress_module "Web Analysis"
    fi

    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi

    _print_section "Finalization"

    cms_scanner
    url_gf
    wordlist_gen
    wordlist_gen_roboxtractor
    password_dict
    url_ext

    ui_module_end "Finalization" "webs/" "gf/" "cms/"
    progress_module "Finalization"
}

function multi_recon() {

    [ "$SOFT_NOTIFICATION" = true ] && echo "$(date +'%Y-%m-%d %H:%M:%S') Recon successfully started on ${multi}" | notify -silent

    global_start=$(date +%s)

    #[[ -n "$domain" ]] && ipcidr_target $domain

    if [[ -s "$list" ]]; then
        sed_i 's/\r$//' "$list"
        mapfile -t targets <"$list"
    else
        notification "Target list not provided" error
        exit
    fi

    workdir="${SCRIPTPATH}/Recon/${multi}"
    mkdir -p "$workdir" || {
        print_errorf "Failed to create directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }

    mkdir -p {.log,.tmp,webs,hosts,vulns,osint,screenshots,subdomains}
    touch webs/webs.txt webs/webs_uncommon_ports.txt webs/webs_all.txt \
        subdomains/subdomains.txt .tmp/web_full_info.txt 2>/dev/null || true
    NOW=$(date +"%F")
    NOWT=$(date +"%T")
    LOGFILE="${workdir}/.log/${NOW}_${NOWT}.txt"
    touch ".log/${NOW}_${NOWT}.txt"
    DEBUG_LOG="${workdir}/debug.log"
    touch "$DEBUG_LOG"
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"${LOGFILE}"
    init_dns_resolver

    [ -n "$flist" ] && LISTTOTAL=$(wc -l <"$flist")

    for domain in "${targets[@]}"; do
        [[ -z "$domain" ]] && continue
        dir="$workdir/targets/$domain"
        called_fn_dir="$dir/.called_fn"

        # Ensure directories exist
        mkdir -p "$dir" || {
            print_errorf "Failed to create directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }
        mkdir -p "$called_fn_dir" || {
            print_errorf "Failed to create directory '%s' in %s @ line %s" "$called_fn_dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }

        cd "$dir" || {
            print_errorf "Failed to cd to directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }

        mkdir -p {.log,.tmp,webs,hosts,vulns,osint,screenshots,subdomains}
        touch webs/webs.txt webs/webs_uncommon_ports.txt webs/webs_all.txt \
            subdomains/subdomains.txt .tmp/web_full_info.txt 2>/dev/null || true

        NOW=$(date +"%F")
        NOWT=$(date +"%T")
        LOGFILE="${dir}/.log/${NOW}_${NOWT}.txt"

        # Ensure the .log directory exists before touching the file
        mkdir -p .log

        touch "$LOGFILE" || {
            print_errorf "Failed to create log file: %s" "$LOGFILE"
            exit 1
        }
        DEBUG_LOG="${dir}/debug.log"
        touch "$DEBUG_LOG"
        echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"$LOGFILE"
        enable_command_trace
        loopstart=$(date +%s)

        domain_info
        ip_info
        emails
        google_dorks
        #github_dorks
        github_repos
        github_leaks
        metadata
        apileaks
        third_party_misconfigs
        zonetransfer
        if [[ "${S3BUCKETS:-false}" != "true" ]]; then
            cloud_enum_scan
        fi
        currently=$(date +"%H:%M:%S")
        loopend=$(date +%s)
        getElapsedTime "$loopstart" "$loopend"
        if [[ -n $flist ]]; then
            POSINLIST=$(grep -nrE "^${domain}$" "$flist" | cut -f1 -d':')
            _print_status OK "${domain} (OSINT ${POSINLIST}/${LISTTOTAL})" "$runtime"
        else
            _print_status OK "${domain} (OSINT)" "$runtime"
        fi
    done
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }

    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    for domain in "${targets[@]}"; do
        [[ -z "$domain" ]] && continue
        loopstart=$(date +%s)
        dir="$workdir/targets/$domain"
        called_fn_dir="$dir/.called_fn"
        cd "$dir" || {
            print_errorf "Failed to cd directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }
        run_module_with_axiom_failover subdomains_full
        run_module_with_axiom_failover webprobe_full
        run_module_with_axiom_failover subtakeover
        remove_big_files
        run_module_with_axiom_failover screenshot
        run_module_with_axiom_failover cdnprovider
        run_module_with_axiom_failover portscan
        run_module_with_axiom_failover geo_info
        tls_ip_pivots
        virtualhosts
        currently=$(date +"%H:%M:%S")
        loopend=$(date +%s)
        getElapsedTime "$loopstart" "$loopend"
        if [[ -n $flist ]]; then
            POSINLIST=$(grep -nrE "^${domain}$" "$flist" | cut -f1 -d':')
            _print_status OK "${domain} (recon ${POSINLIST}/${LISTTOTAL})" "$runtime"
        else
            _print_status OK "${domain} (recon)" "$runtime"
        fi
    done
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }

    _print_section "Total Data"
    NUMOFLINES_pwndb_total=$(find . -type f -name 'passwords.txt' -exec cat {} + | anew osint/passwords.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_subs_total=$(find . -type f -name 'subdomains.txt' -exec cat {} + | anew subdomains/subdomains.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_subtko_total=$(find . -type f -name 'takeover.txt' -exec cat {} + | anew webs/takeover.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_webs_total=$(find . -type f -name 'webs.txt' -exec cat {} + | anew webs/webs.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_webs_total_uncommon=$(find . -type f -name 'webs_uncommon_ports.txt' -exec cat {} + | anew webs/webs_uncommon_ports.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_ips_total=$(find . -type f -name 'ips.txt' -exec cat {} + | anew hosts/ips.txt | sed '/^$/d' | wc -l)
    NUMOFLINES_cloudsprov_total=$(find . -type f -name 'cdn_providers.txt' -exec cat {} + | anew hosts/cdn_providers.txt | sed '/^$/d' | wc -l)
    find . -type f -name 'portscan_active.gnmap' -exec cat {} + | tee hosts/portscan_active.gnmap 2>>"$LOGFILE" >/dev/null
    find . -type f -name 'portscan_passive.txt' -exec cat {} + | tee hosts/portscan_passive.txt 2>&1 >>"$LOGFILE" >/dev/null

    printf "  Creds: %s │ Subs: %s │ Takeovers: %s │ Webs: %s (+%s uncommon) │ IPs: %s │ Cloud: %s\n" \
        "$NUMOFLINES_pwndb_total" "$NUMOFLINES_subs_total" "$NUMOFLINES_subtko_total" \
        "$NUMOFLINES_webs_total" "$NUMOFLINES_webs_total_uncommon" \
        "$NUMOFLINES_ips_total" "$NUMOFLINES_cloudsprov_total"
    run_module_with_axiom_failover s3buckets
    run_module_with_axiom_failover waf_checks
    run_module_with_axiom_failover nuclei_check
    for domain in "${targets[@]}"; do
        [[ -z "$domain" ]] && continue
        loopstart=$(date +%s)
        dir="$workdir/targets/$domain"
        called_fn_dir="$dir/.called_fn"
        cd "$dir" || {
            print_errorf "Failed to cd directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }
        loopstart=$(date +%s)
        run_module_with_axiom_failover fuzz
        run_module_with_axiom_failover iishortname
        swagger_check
        run_module_with_axiom_failover urlchecks
        run_module_with_axiom_failover jschecks
        currently=$(date +"%H:%M:%S")
        loopend=$(date +%s)
        getElapsedTime "$loopstart" "$loopend"
        if [[ -n $flist ]]; then
            POSINLIST=$(grep -nrE "^${domain}$" "$flist" | cut -f1 -d':')
            _print_status OK "${domain} (vulns ${POSINLIST}/${LISTTOTAL})" "$runtime"
        else
            _print_status OK "${domain} (vulns)" "$runtime"
        fi
    done

    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi

    for domain in "${targets[@]}"; do
        [[ -z "$domain" ]] && continue
        loopstart=$(date +%s)
        dir="$workdir/targets/$domain"
        called_fn_dir="$dir/.called_fn"
        cd "$dir" || {
            print_errorf "Failed to cd directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
            exit 1
        }
        cms_scanner
        url_gf
        wordlist_gen
        wordlist_gen_roboxtractor
        password_dict
        url_ext
        currently=$(date +"%H:%M:%S")
        loopend=$(date +%s)
        getElapsedTime "$loopstart" "$loopend"
        if [[ -n $flist ]]; then
            POSINLIST=$(grep -nrE "^${domain}$" "$flist" | cut -f1 -d':')
            _print_status OK "${domain} (final ${POSINLIST}/${LISTTOTAL})" "$runtime"
        else
            _print_status OK "${domain} (final)" "$runtime"
        fi
    done
    cd "$workdir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$workdir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    dir=$workdir
    domain=$multi
    end
    [ "$SOFT_NOTIFICATION" = true ] && echo "$(date +'%Y-%m-%d %H:%M:%S') Finished Recon on: ${multi} in ${runtime}" | notify -silent
}

function multi_custom() {

    global_start=$(date +%s)

    if [[ -s "$list" ]]; then
        sed_i 's/\r$//' "$list"
    else
        notification "Target list not provided" error
        exit
    fi

    dir="${SCRIPTPATH}/Recon/${multi}"
    if [[ -z "$multi" || "$dir" != "${SCRIPTPATH}/Recon/"* ]]; then
        print_errorf "Refusing to remove '%s' -- safety check failed" "$dir"
        return 1
    fi
    if [[ -d "$dir" ]]; then
        if [[ "${FORCE_RESCAN:-false}" == "true" ]]; then
            rm -rf -- "$dir"
        else
            # Preserve existing results; use --force to wipe
            notification "Directory $dir exists; use --force to overwrite" warn
        fi
    fi
    mkdir -p "$dir" || {
        print_errorf "Failed to create directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }
    cd "$dir" || {
        print_errorf "Failed to cd directory '%s' in %s @ line %s" "$dir" "${FUNCNAME[0]}" "${LINENO}"
        exit 1
    }

    mkdir -p {.called_fn,.log}
    called_fn_dir="$dir/.called_fn"
    NOW=$(date +"%F")
    NOWT=$(date +"%T")
    LOGFILE="${dir}/.log/${NOW}_${NOWT}.txt"
    touch ".log/${NOW}_${NOWT}.txt"
    DEBUG_LOG="${dir}/debug.log"
    touch "$DEBUG_LOG"
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] Start ${NOW} ${NOWT}" >"${LOGFILE}"
    enable_command_trace
    init_dns_resolver

    [ -n "$flist" ] && entries=$(wc -l <"$flist")

    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    custom_function_list=$(echo "$custom_function" | tr ',' '\n')
    func_total=$(echo "$custom_function_list" | wc -l)

    # Iterate each target line through _sanitize_list_entry instead of loading
    # the whole file into $domain. Loading the whole file exposed every sink
    # that expands $domain unquoted (urlfinder, EmailHarvester) to CLI option
    # injection and accidental word-splitting across multiple targets.
    func_count=0
    while IFS= read -r raw_domain || [[ -n "$raw_domain" ]]; do
        [[ -z "$raw_domain" ]] && continue
        domain=$(_sanitize_list_entry "$raw_domain") || continue

        for custom_f in $custom_function_list; do
            ((func_count = func_count + 1))

            loopstart=$(date +%s)

            run_module_with_axiom_failover "$custom_f"

            currently=$(date +"%H:%M:%S")
            loopend=$(date +%s)
            local duration=$((loopend - loopstart))
            _print_status OK "$custom_f" "${duration}s (${domain})"
            [[ "${OUTPUT_VERBOSITY:-1}" -ge 2 ]] && \
                print_notice INFO "$custom_f" "entries ${entries} (${func_count}/${func_total}) at ${currently}"
        done
    done <"$flist"

    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi

    end
}

function subs_menu() {
    start

    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    _print_section "Subdomains"

    run_module_with_axiom_failover subdomains_full
    run_module_with_axiom_failover subtakeover
    remove_big_files
    zonetransfer
    run_module_with_axiom_failover s3buckets

    _print_section "Web Detection"
    run_module_with_axiom_failover webprobe_full
    favirecon_tech
    run_module_with_axiom_failover screenshot
    tls_ip_pivots
    virtualhosts

    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi

    end
}

function webs_menu() {
    _print_section "Web Detection"

    run_module_with_axiom_failover webprobe_full
    favirecon_tech
    run_module_with_axiom_failover subtakeover
    remove_big_files
    run_module_with_axiom_failover screenshot
    virtualhosts

    _print_section "Web Analysis"

    run_module_with_axiom_failover waf_checks
    run_module_with_axiom_failover nuclei_check
    run_module_with_axiom_failover graphql_scan
    run_module_with_axiom_failover fuzz
    cms_scanner
    run_module_with_axiom_failover iishortname
    swagger_check
    run_module_with_axiom_failover urlchecks
    param_discovery
    run_module_with_axiom_failover jschecks
    sub_js_extract
    well_known_pivots
    websocket_checks
    url_gf
    wordlist_gen
    wordlist_gen_roboxtractor
    password_dict
    url_ext
    grpc_reflection

    vulns
    end
}

function zen_menu() {
    start
    if [[ $AXIOM == true ]]; then
        axiom_launch
        axiom_selected
    fi

    _print_section "Subdomains"

    run_module_with_axiom_failover subdomains_full
    run_module_with_axiom_failover subtakeover
    remove_big_files
    run_module_with_axiom_failover s3buckets

    _print_section "Web Detection"
    run_module_with_axiom_failover webprobe_full
    favirecon_tech
    run_module_with_axiom_failover screenshot
    tls_ip_pivots
    virtualhosts
    run_module_with_axiom_failover cdnprovider

    _print_section "Web Analysis"

    run_module_with_axiom_failover waf_checks
    run_module_with_axiom_failover nuclei_check
    run_module_with_axiom_failover graphql_scan
    run_module_with_axiom_failover fuzz
    run_module_with_axiom_failover iishortname
    swagger_check
    if [[ $AXIOM == true ]]; then
        axiom_shutdown
    fi
    cms_scanner
    end
}

function monitor_mode() {
    local selected_mode="${1:-r}"

    if [[ -z "${selected_mode}" ]]; then
        selected_mode="r"
    fi

    if [[ -n "${multi:-}" ]]; then
        notification "Monitor mode does not support -m multi-target runs" error
        return 1
    fi
    if [[ "${selected_mode}" != "w" ]] && [[ -n "${list:-}" ]]; then
        notification "Monitor mode supports -l only with web mode (-w)" error
        return 1
    fi

    if [[ -n "${dir_output:-}" ]]; then
        notification "Monitor mode ignores -o output relocation to preserve incremental state" warn
        unset dir_output
    fi

    if [[ ! "${MONITOR_INTERVAL_MIN:-}" =~ ^[0-9]+$ ]] || [[ "${MONITOR_INTERVAL_MIN:-0}" -lt 1 ]]; then
        MONITOR_INTERVAL_MIN=60
    fi
    if [[ ! "${MONITOR_MAX_CYCLES:-}" =~ ^[0-9]+$ ]]; then
        MONITOR_MAX_CYCLES=0
    fi

    # shellcheck disable=SC2034  # Used by downstream execution functions in monitor cycles
    INCREMENTAL_MODE=true
    QUICK_RESCAN=true
    # shellcheck disable=SC2034  # Used by downstream execution functions in monitor cycles
    DIFF=true
    local interval_sec=$((MONITOR_INTERVAL_MIN * 60))
    local cycle=0

    notification "Monitor mode enabled (mode=${selected_mode}, interval=${MONITOR_INTERVAL_MIN}m, max_cycles=${MONITOR_MAX_CYCLES:-0})" info

    while true; do
        cycle=$((cycle + 1))
        MONITOR_CYCLE=$cycle

        notification "Monitor cycle ${cycle} started" info

        case "$selected_mode" in
            'r')
                start
                recon
                end
                ;;
            's')
                subs_menu
                ;;
            'p')
                passive
                ;;
            'a')
                export VULNS_GENERAL=true
                all
                ;;
            'w')
                if [[ -z $list ]]; then
                    notification "Web mode in monitor requires -l list file" error
                    return 1
                fi
                # Same scope-prep as the one-shot -w dispatch: wipe stale module
                # output, preserve cross-run state (.log/.incremental/.called_fn/
                # debug.log), and populate subdomains/subdomains.txt with sanitized
                # hostnames so webprobe_full picks them up. DIFF=true is already
                # set by monitor_mode above. If scope prep fails (mktemp / cp
                # error) skip webs_menu so we don't probe a stale scope, but
                # fall through to the normal post-case (monitor_snapshot,
                # max-cycles check, sleep) — `continue` here would busy-loop and
                # bypass MONITOR_MAX_CYCLES.
                start
                if prepare_web_mode_scope; then
                    webs_menu
                else
                    notification "Web-scope preparation failed this cycle; skipping webs_menu until next interval" warn
                fi
                ;;
            'n')
                PRESERVE=true
                start
                osint
                end
                ;;
            'z')
                zen_menu
                ;;
            *)
                notification "Unsupported mode '${selected_mode}' for monitor. Use one of: r,s,p,a,w,n,z" error
                return 1
                ;;
        esac

        monitor_snapshot || true

        if [[ "${MONITOR_MAX_CYCLES:-0}" -gt 0 ]] && [[ "$cycle" -ge "${MONITOR_MAX_CYCLES}" ]]; then
            notification "Monitor finished after ${cycle} cycle(s)" good
            break
        fi

        notification "Monitor cycle ${cycle} completed, sleeping ${MONITOR_INTERVAL_MIN} minute(s)" info
        sleep "$interval_sec"
    done
}

function report_only_mode() {
    if [[ -z "${domain:-}" ]]; then
        notification "Report-only mode requires -d <domain>" error
        return 1
    fi
    if [[ -n "${list:-}" ]] || [[ -n "${multi:-}" ]]; then
        notification "Report-only mode currently supports single-target only" error
        return 1
    fi

    dir="${SCRIPTPATH}/Recon/$domain"
    called_fn_dir="${dir}/.called_fn"
    if [[ ! -d "$dir" ]]; then
        notification "Target directory not found for report-only mode: $dir" error
        return 1
    fi

    cd "$dir" || {
        notification "Failed to enter target directory: $dir" error
        return 1
    }

    mkdir -p .log report
    NOW=$(date +"%F")
    NOWT=$(date +"%T")
    LOGFILE="${dir}/.log/${NOW}_${NOWT}_report_only.txt"
    touch "$LOGFILE"
    DEBUG_LOG="${dir}/debug.log"
    touch "$DEBUG_LOG"
    runtime="report-only"

    notification "Report-only mode: rebuilding reports from existing artifacts" info
    build_hotlist || true
    write_perf_summary || true
    if [[ "${NO_REPORT:-false}" != "true" ]]; then
        generate_consolidated_report || true
        export_reports || true
    fi
    notification "Report-only rebuild completed for ${domain}" good
}

function help() {
    pt_header "Usage"
    printf "\n Usage: %s [-d domain.tld] [-m name] [-l list.txt] [-x oos.txt] [-i in.txt] " "$0"
    printf "\n           	      [-r] [-s] [-p] [-a] [-w] [-n] [-z] [-c] [-y] [-h] [-f] [--ai] [--deep] [--monitor] [--monitor-interval m] [--monitor-cycles n] [--vps-count n] [--report-only] [--refresh-cache] [--gen-resolvers] [--force] [--export fmt] [-o OUTPUT]\n\n"
    printf " %bTARGET OPTIONS%b\n" "${bblue}" "${reset}"
    printf "   -d domain.tld     Target domain\n"
    printf "   -m company        Target company name\n"
    printf "   -l list.txt       Targets list (One on each line)\n"
    printf "   -x oos.txt        Excludes subdomains list (Out Of Scope)\n"
    printf "   -i in.txt         Includes subdomains list\n"
    printf " \n"
    printf " %bMODE OPTIONS%b\n" "${bblue}" "${reset}"
    printf "   -r, --recon       Recon - Performs full recon process (includes nuclei and fuzzing)\n"
    printf "   -s, --subdomains  Subdomains - Performs Subdomain Enumeration, Web probing and check for sub-tko\n"
    printf "   -p, --passive     Passive - Performs only passive steps\n"
    printf "   -a, --all         All - Performs all checks and active exploitations\n"
    printf "   -w, --web         Web - Performs web checks from list of subdomains\n"
    printf "   -n, --osint       OSINT - Checks for public intel data\n"
    printf "   -z, --zen         Zen - Performs a recon process covering the basics and some vulns\n"
    printf "   -c, --custom      Custom - Launches specific function against target, u need to know the function name first\n"
    printf "   -y, --ai          AI - Analyzes ReconFTW results using a local LLM\n"
    printf "   -h                Help - Show help section\n"
    printf " \n"
    printf " %bGENERAL OPTIONS%b\n" "${bblue}" "${reset}"
    printf "   --deep            Deep scan (Enable some slow options for deeper scan)\n"
    printf "   -f config_file    Alternate reconftw.cfg file\n"
    printf "   -o output/path    Define output folder\n"
    printf "   -v, --vps         Axiom distributed VPS\n"
    printf "   --vps-count n     Override Axiom fleet instances for this run (implies AXIOM)\n"
    printf "   -q                Rate limit in requests per second\n"
    printf "   --check-tools     Exit if one of the tools is missing\n"
    printf "   --health-check    Run system health check and exit\n"
    printf "   --quick-rescan    Skip heavy steps if no new subs/webs this run\n"
    printf "   --incremental     Only scan new findings since last run\n"
    printf "   --adaptive-rate   Automatically adjust rate limits on errors (429/503)\n"
    printf "   --dry-run         Show what would be executed without running commands\n"
    printf "   --quiet           Minimal output (errors and final summary only)\n"
    printf "   --verbose         Extra output (PIDs, debug info)\n"
    printf "   --log-format f    Console format: plain, jsonl, jsonl-strict\n"
    printf "   --parallel-log m  Parallel output mode: summary (default), tail, or full\n"
    printf "   --parallel        Run independent functions in parallel (faster but uses more resources)\n"
    printf "   --no-parallel     Force sequential execution\n"
    printf "   --show-cache      Show cached modules lines (default collapses)\n"
    printf "   --no-banner       Disable ASCII banner\n"
    printf "   --legal           Show full legal block\n"
    printf "   --monitor         Continuous monitoring mode (single target; -w supports -l)\n"
    printf "   --monitor-interval Minutes between cycles (default: 60)\n"
    printf "   --monitor-cycles  Stop after N cycles (0 = infinite)\n"
    printf "   --report-only     Rebuild report artifacts from existing Recon/<target>\n"
    printf "   --no-report       Disable report generation and exports\n"
    printf "   --refresh-cache   Force refresh of cached resolvers/wordlists\n"
    printf "   --gen-resolvers   Generate custom resolvers with dnsvalidator\n"
    printf "   --force           Re-run all modules (ignore cached markers)\n"
    printf "   --export fmt      Export artifacts: json|html|csv|all (default: all)\n"
    printf " \n"
    printf " %bUSAGE EXAMPLES%b\n" "${bblue}" "${reset}"
    printf " %bPerform full recon (without attacks):%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -r\n"
    printf " \n"
    printf " %bPerform subdomain enumeration on multiple targets:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -l targets.txt -s\n"
    printf " \n"
    printf " %bPerform Web based scanning on a subdomains list:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -l targets.txt -w\n"
    printf " \n"
    printf " %bMultidomain recon:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -m company -l domainlist.txt -r\n"
    printf " \n"
    printf " %bPerform full recon (with active attacks) along Out-Of-Scope subdomains list:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -x out.txt -a\n"
    printf " \n"
    printf " %bAnalyze ReconFTW results with AI:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -r --ai\n"
    printf " \n"
    printf " %bRun recon on Axiom with 30 instances:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -r -v 30\n"
    printf " \n"
    printf " %bRun custom function:%b\n" "${byellow}" "${reset}"
    printf " ./reconftw.sh -d example.com -c nuclei_check \n"
}