# functions.zsh — fzf widgets + HTB/CTF workflow helpers. [ -n "${ZSH_VERSION:-}" ] || return 0 # Copy a short value using the native Wayland clipboard when available, with # an XWayland fallback. Clipboard failure never hides the requested value. _pe_clipboard() { if [[ -n ${WAYLAND_DISPLAY:-} ]] && command -v wl-copy >/dev/null 2>&1; then wl-copy elif command -v xclip >/dev/null 2>&1; then xclip -selection clipboard -in elif command -v pbcopy >/dev/null 2>&1; then pbcopy else return 1 fi } # --- tip: active target's IP -> stdout (+ clipboard when available) ---------- tip() { [ -f "$PE_ACTIVE_DISPLAY_FILE" ] || { print -u2 "no active target"; return 1; } local ipv; ipv=$(awk -F' \\| ' '{print $2}' "$PE_ACTIVE_DISPLAY_FILE") [ -n "$ipv" ] && printf '%s' "$ipv" | _pe_clipboard 2>/dev/null || true print -- "$ipv" } # useip: export $IP / $RHOST from the active target for use in commands. useip() { export IP="$(tip)"; export RHOST="$IP"; print -- "IP=$IP"; } # newbox [platform]: register + activate a target and make a workdir. newbox() { [ -n "$2" ] || { print -u2 "usage: newbox [platform]"; return 1; } [[ $1 == [[:alnum:]]* && $1 != *[^[:alnum:]_.-]* ]] \ || { print -u2 "name may contain only letters, numbers, dot, dash and underscore"; return 1; } pe target add "$1" --ip "$2" ${3:+--platform "$3"} || return 1 local d="$PE_WORKROOT/$1" (umask 077; mkdir -p "$d"/{nmap,scans,loot,web,exploit,notes}) chmod -R go-rwx "$d" print -- "workdir: $d" cd "$d" } # Refuse scan helpers when the route does not traverse a lab VPN. For another # explicitly authorized environment, opt out per command with PE_ALLOW_NO_VPN=1. _pe_require_lab_route() { local ip=$1 route [[ ${PE_ALLOW_NO_VPN:-0} == 1 ]] && return 0 if command -v ip >/dev/null 2>&1; then route=$(ip route get "$ip" 2>/dev/null) || route="" [[ -n $route ]] && print -r -- "$route" \ | grep -Eq ' dev (tun|tap|wg)[[:alnum:]_.-]*([[:space:]]|$)' \ && return 0 elif command -v route >/dev/null 2>&1; then route=$(route -n get "$ip" 2>/dev/null) || route="" [[ -n $route ]] && print -r -- "$route" \ | grep -Eq 'interface:[[:space:]]*(utun|tun|tap|wg)[[:alnum:]_.-]*' \ && return 0 else route="" fi if [[ -z $route ]]; then print -u2 "cannot resolve a route to $ip; connect the HTB VPN first" return 1 fi print -u2 "refusing to scan: route to $ip is not through tun/tap/utun/wg" print -u2 "for another authorized lab only: PE_ALLOW_NO_VPN=1 " return 1 } # scan : quick recorded nmap (writes into ./nmap). scan() { local ip="${1:-$(tip 2>/dev/null)}" [ -n "$ip" ] || { print -u2 "usage: scan (or set an active target)"; return 1; } _pe_require_lab_route "$ip" || return 1 (umask 077; mkdir -p nmap) (umask 077; nmap -Pn -sCV -oA "nmap/initial-$ip" "$ip") } fullscan() { local ip="${1:-$(tip 2>/dev/null)}" [ -n "$ip" ] || { print -u2 "usage: fullscan "; return 1; } _pe_require_lab_route "$ip" || return 1 (umask 077; mkdir -p nmap) (umask 077; nmap -Pn -p- --min-rate 2000 -T4 -oA "nmap/all-ports-$ip" "$ip") } udpquick() { local ip="${1:-$(tip 2>/dev/null)}" [ -n "$ip" ] || { print -u2 "usage: udpquick "; return 1; } _pe_require_lab_route "$ip" || return 1 (umask 077; mkdir -p nmap) (umask 077; sudo nmap -Pn -sU --top-ports 100 -oA "nmap/udp-top100-$ip" "$ip") } webenum() { local url=$1 wordlist host [ -n "$url" ] || { print -u2 "usage: webenum "; return 1; } host=${url#*://}; host=${host%%/*}; host=${host%%:*} _pe_require_lab_route "$host" || return 1 wordlist="$PE_WORDLIST_ROOT/Discovery/Web-Content/raft-medium-words.txt" [ -r "$wordlist" ] || wordlist="$PE_WORDLIST_ROOT/common.txt" [ -r "$wordlist" ] || { print -u2 "no web wordlist found under $PE_WORDLIST_ROOT"; return 1; } (umask 077; ffuf -ac -c -w "$wordlist" -u "$url") } # NetExec can write hashes, screenshots and logs. A private umask keeps future # artifacts protected even if the upstream defaults are permissive. if command -v nxc >/dev/null 2>&1; then nxc() { (umask 077; command nxc "$@"); } fi if command -v netexec >/dev/null 2>&1; then netexec() { (umask 077; command netexec "$@"); } fi # --- zle widgets (interactive only) ----------------------------------------- if [[ -o interactive ]]; then # Ctrl-X c : pick a credential, insert its SECRET at the cursor. _pe_insert_cred() { command -v fzf >/dev/null 2>&1 || return 0 local sel sel=$(PE_COLOR=0 pe cred ls --all 2>/dev/null | sed '1d' \ | fzf --height=40% --reverse --prompt='cred> ') || return 0 [ -n "$sel" ] && LBUFFER+="$(print -- "$sel" | awk '{print $4}')" zle reset-prompt } zle -N _pe_insert_cred bindkey '^Xc' _pe_insert_cred # Ctrl-X f : pick a flag, insert its VALUE at the cursor. _pe_insert_flag() { command -v fzf >/dev/null 2>&1 || return 0 local sel sel=$(PE_COLOR=0 pe flag ls --all 2>/dev/null | sed '1d' \ | fzf --height=40% --reverse --prompt='flag> ') || return 0 [ -n "$sel" ] && LBUFFER+="$(print -- "$sel" | awk '{print $4}')" zle reset-prompt } zle -N _pe_insert_flag bindkey '^Xf' _pe_insert_flag # Ctrl-X t : fzf-switch the active target. _pe_target_switch() { command -v fzf >/dev/null 2>&1 || return 0 local name name=$(PE_COLOR=0 pe target ls 2>/dev/null | sed '1d' \ | fzf --height=40% --reverse --prompt='target> ' \ | tr -d '*' | awk '{print $2}') [ -n "$name" ] && pe target use "$name" >/dev/null 2>&1 zle reset-prompt } zle -N _pe_target_switch bindkey '^Xt' _pe_target_switch fi