#!/usr/bin/env bash # pe — pentest-env dispatcher. `pe [args]`. # Lives at $PE_CONFIG_DIR/bin/pe; usually invoked via a ~/.local/bin/pe symlink. # (No `set -u`: optional positionals like `pe status` must fall through to a # friendly usage message, not an "unbound variable" abort.) Pipe failures do # need to propagate: SQLCipher is intentionally driven through stdin pipelines. set -o pipefail umask 077 # --- locate ourselves (resolve symlinks) so we can find config.sh + lib/ ----- pe__realpath() { _p=$1 while [ -L "$_p" ]; do _l=$(readlink "$_p") case $_l in /*) _p=$_l ;; *) _p=$(dirname "$_p")/$_l ;; esac done cd "$(dirname "$_p")" 2>/dev/null && printf '%s/%s' "$(pwd -P)" "$(basename "$_p")" } PE_SELF=$(pe__realpath "$0") PE_BIN_DIR=$(dirname "$PE_SELF") : "${PE_CONFIG_DIR:=$(dirname "$PE_BIN_DIR")}" export PE_CONFIG_DIR # --- load foundation (always needed) ---------------------------------------- . "$PE_CONFIG_DIR/config.sh" . "$PE_LIB_DIR/ui.sh" . "$PE_LIB_DIR/db.sh" . "$PE_LIB_DIR/target.sh" # `pe` also works from launchers and tmux where interactive shell startup has # not yet added the isolated Go/Rust/Python tool locations. PATH="$HOME/go/bin:$HOME/.cargo/bin:$HOME/.local/bin:$PATH" export PATH # pe__lib NAME -> source lib/NAME.sh once (idempotent). pe__lib() { eval "case \${PE__LOADED_$1:-} in 1) return 0 ;; esac" . "$PE_LIB_DIR/$1.sh" eval "PE__LOADED_$1=1" } pe_usage() { cat <<'USAGE' pe — pentest-env toolkit pe init create + key the encrypted database pe status show the active target (box | ip | flags) pe doctor audit tools, VPN, wordlists and permissions pe vpn manage private HTB OpenVPN profiles pe session [target] open the deterministic HTB tmux workspace pe target | t pe flag | f pe cred | c pe host | h pe note | n pe backup snapshot the database (gzip, retained) pe restore restore from a backup (fzf picker) pe reset wipe all rows, keep the schema pe destroy delete the database file entirely pe cheat [query] browse/search cheatsheets pe guide [page|list|path] open the offline HTML handbook pe help | -h | --help this text USAGE } pe_init() { pe_need "$PE_SQLCIPHER" pe_dirs_ensure # no key available yet -> generate a GPG-wrapped one (unless using env key) if [ -z "${PENTEST_DB_KEY:-}" ] && [ ! -f "$PE_KEYFILE" ]; then pe_info "no key found — generating a GPG-wrapped SQLCipher key" db_genkey fi db_init pe_info "next: pe target add " } main() { _cmd=${1:-help}; [ $# -gt 0 ] && shift case $_cmd in init) pe_init "$@" ;; doctor) pe__lib doctor; doctor_main "$@" ;; vpn) pe__lib vpn; vpn_main "$@" ;; session) exec "$PE_BIN_DIR/htb-session" "$@" ;; status) db_require; target_status "$@" ;; target|t) db_require; target_main "$@" ;; flag|f) db_require; pe__lib flag; flag_main "$@" ;; cred|c) db_require; pe__lib cred; cred_main "$@" ;; host|h) db_require; pe__lib host; host_main "$@" ;; note|n) db_require; pe__lib note; note_main "$@" ;; backup) db_require; pe__lib backup; backup_create "$@" ;; restore) pe__lib backup; backup_restore "$@" ;; reset) db_require; pe__lib backup; backup_reset "$@" ;; destroy) pe__lib backup; backup_destroy "$@" ;; cheat) pe__lib cheat; cheat_main "$@" ;; guide) pe__lib guide; guide_main "$@" ;; version|-v|--version) printf 'pe (pentest-env) 1.1\n' ;; help|-h|--help) pe_usage ;; *) pe_err "unknown command: $_cmd"; pe_usage; exit 2 ;; esac } main "$@"