repositories

voidwalker.py

Penetration testing arsenal builder and workspace manager for macOS and Linux

clonegit clone https://git.daemon-sec.xyz/voidwalker.py.git
target.shShell228 lines9.7 KiBraw download
# shellcheck shell=sh
# lib/target.sh — target (engagement) CRUD + the active-target context that
# every other manager, the zsh prompt, and the tmux status bar read.
# Depends on: config.sh, lib/ui.sh, lib/db.sh.

# --- active-target pointer --------------------------------------------------
target_active_id() {                       # prints id, or returns 1 if unset
    [ -f "$PE_ACTIVE_TARGET_FILE" ] || return 1
    _id=$(cat "$PE_ACTIVE_TARGET_FILE" 2>/dev/null)
    case $_id in ''|*[!0-9]*) return 1 ;; esac
    # guard against a pointer to a deleted target
    _found=$(db_query "SELECT id FROM targets WHERE id=$_id;") || return 2
    [ "$_found" = "$_id" ] || return 1
    printf '%s' "$_id"
}

target_active_name() {
    _id=$(target_active_id)
    _active_status=$?
    [ "$_active_status" = 0 ] || return "$_active_status"
    db_query "SELECT name FROM targets WHERE id=$_id;" || return 2
}

# target_resolve <name|id> -> prints matching id (empty + return 1 if none).
target_resolve() {
    case $1 in
        ''|*[!0-9]*) _q="name=$(db_quote "$1")" ;;
        *)           _q="id=$1" ;;
    esac
    _id=$(db_query "SELECT id FROM targets WHERE $_q LIMIT 1;") || return 2
    [ -n "$_id" ] || return 1
    printf '%s' "$_id"
}

# pe_target_scope [explicit] -> resolve a target id for scoping a sub-command:
# explicit name/id if given, else the active target, else die.
pe_target_scope() {
    if [ -n "$1" ]; then
        target_resolve "$1"
        _resolve_status=$?
        case $_resolve_status in
            0) : ;;
            1) pe_die "no such target: $1" ;;
            *) return "$_resolve_status" ;;
        esac
    else
        target_active_id
        _resolve_status=$?
        case $_resolve_status in
            0) : ;;
            1) pe_die "no active target — run 'pe target use <name>' or pass --target" ;;
            *) return "$_resolve_status" ;;
        esac
    fi
}

# --- CRUD -------------------------------------------------------------------
target_add() {
    _name=""; _plat=$PE_DEFAULT_PLATFORM; _ip=""; _host=""; _os=""; _diff=""
    while [ $# -gt 0 ]; do
        case $1 in
            --platform|-p) pe_require_value "$@"; _plat=$2; shift 2 ;;
            --ip)          pe_require_value "$@"; _ip=$2;   shift 2 ;;
            --hostname)    pe_require_value "$@"; _host=$2; shift 2 ;;
            --os)          pe_require_value "$@"; _os=$2;   shift 2 ;;
            --difficulty)  pe_require_value "$@"; _diff=$2; shift 2 ;;
            -*)            pe_die "target add: unknown option $1" ;;
            *)             [ -z "$_name" ] && _name=$1 || pe_die "target add: extra arg $1"; shift ;;
        esac
    done
    [ -n "$_name" ] || pe_die "usage: pe target add <name> [--platform p] [--ip x] [--hostname h] [--os o] [--difficulty d]"
    target_resolve "$_name" >/dev/null
    _resolve_status=$?
    case $_resolve_status in
        0) pe_die "target already exists: $_name" ;;
        1) : ;;
        *) return "$_resolve_status" ;;
    esac
    _id=$(db_insert "INSERT INTO targets(name,platform,ip,hostname,os,difficulty)
                     VALUES($(db_quote "$_name"),$(db_quote "$_plat"),$(db_quote "$_ip"),
                            $(db_quote "$_host"),$(db_quote "$_os"),$(db_quote "$_diff"));") || return 1
    pe_ok "added target #$_id: $_name"
    target_use "$_id"                       # newly added target becomes active
}

# write the plaintext "name | ip" cache read by the prompt + tmux bar
target__write_display() {
    _display_tmp="$PE_ACTIVE_DISPLAY_FILE.tmp.$$"
    db_query "SELECT name||' | '||COALESCE(NULLIF(ip,''),'no-ip') FROM targets WHERE id=$1;" \
        > "$_display_tmp" || { rm -f "$_display_tmp"; return 1; }
    chmod 600 "$_display_tmp" 2>/dev/null || true
    mv -f "$_display_tmp" "$PE_ACTIVE_DISPLAY_FILE"
}

target_use() {
    [ -n "$1" ] || pe_die "usage: pe target use <name|id>"
    _id=$(target_resolve "$1")
    _resolve_status=$?
    case $_resolve_status in
        0) : ;;
        1) pe_die "no such target: $1" ;;
        *) return "$_resolve_status" ;;
    esac
    pe_dirs_ensure || pe_die "could not prepare pentest-env directories"
    target__write_display "$_id" || return 1
    printf '%s' "$_id" > "$PE_ACTIVE_TARGET_FILE" || return 1
    pe_ok "active target -> $(cat "$PE_ACTIVE_DISPLAY_FILE")"
}

target_ls() {
    _rows=$(db_query "SELECT id,name,platform,COALESCE(ip,'-'),status,flags,creds,hosts
                      FROM v_target_summary ORDER BY id;") || return 1
    [ -n "$_rows" ] || { pe_empty "no targets yet — 'pe target add <name>'"; return 0; }
    _act=$(target_active_id 2>/dev/null || echo "")
    printf '%s\n' "$_rows" | awk -F'|' -v a="$_act" 'BEGIN{OFS="|"}{$1=($1==a?"* "$1:"  "$1);print}' \
        | pe_table "ID|NAME|PLATFORM|IP|STATUS|FLAGS|CREDS|HOSTS"
}

target_show() {
    _id=$(pe_target_scope "$1") || return 1
    db_query "SELECT 'Name      '||name  FROM targets WHERE id=$_id;" || return 1
    db_query "SELECT 'Platform  '||platform||'   Difficulty '||COALESCE(difficulty,'-')||'   Status '||status FROM targets WHERE id=$_id;" || return 1
    db_query "SELECT 'IP        '||COALESCE(ip,'-')||'   Host '||COALESCE(hostname,'-')||'   OS '||COALESCE(os,'-') FROM targets WHERE id=$_id;" || return 1
    db_query "SELECT 'Created   '||created_at FROM targets WHERE id=$_id;" || return 1
    printf '\n'
    printf '%sflags%s\n' "$(pe_fg "$PE_RP_GOLD")" "$(pe_reset)"
    db_query "SELECT '  ['||type||'] '||value FROM flags WHERE target_id=$_id ORDER BY captured_at;" || return 1
    printf '%scredentials%s\n' "$(pe_fg "$PE_RP_FOAM")" "$(pe_reset)"
    db_query "SELECT '  '||COALESCE(username,'?')||':'||COALESCE(secret,'?')||'  ('||COALESCE(service,'-')||')' FROM credentials WHERE target_id=$_id;" || return 1
    printf '%shosts%s\n' "$(pe_fg "$PE_RP_IRIS")" "$(pe_reset)"
    db_query "SELECT '  '||COALESCE(ip,'-')||'  '||COALESCE(hostname,'')||'  '||COALESCE(label,'') FROM hosts WHERE target_id=$_id;" || return 1
}

# target_status [--tmux]  -> current box line. Empty state exits 0; database
# failures remain non-zero.
# --tmux: read the plaintext cache only (NEVER decrypt — runs every few seconds
# in the status bar and must never trigger a key/gpg prompt).
target_status() {
    if [ "${1:-}" = --tmux ]; then
        [ -f "$PE_ACTIVE_DISPLAY_FILE" ] && cat "$PE_ACTIVE_DISPLAY_FILE" || printf 'no target'
        return 0
    fi
    _id=$(target_active_id)
    _active_status=$?
    case $_active_status in
        0) : ;;
        1) pe_empty "no active target"; return 0 ;;
        *) return "$_active_status" ;;
    esac
    db_query "SELECT name||' | '||COALESCE(NULLIF(ip,''),'no-ip')||' | flags '||
              (SELECT COUNT(*) FROM flags WHERE target_id=$_id)
              FROM targets WHERE id=$_id;" || return 1
}

target_edit() {
    [ -n "$1" ] || pe_die "usage: pe target edit <name|id> [--ip x] [--hostname h] [--os o] [--difficulty d] [--status s] [--platform p]"
    _id=$(target_resolve "$1")
    _resolve_status=$?
    case $_resolve_status in
        0) : ;;
        1) pe_die "no such target: $1" ;;
        *) return "$_resolve_status" ;;
    esac
    shift
    _set=""
    while [ $# -gt 0 ]; do
        case $1 in
            --ip)         pe_require_value "$@"; _set="$_set, ip=$(db_quote "$2")";         shift 2 ;;
            --hostname)   pe_require_value "$@"; _set="$_set, hostname=$(db_quote "$2")";   shift 2 ;;
            --os)         pe_require_value "$@"; _set="$_set, os=$(db_quote "$2")";         shift 2 ;;
            --difficulty) pe_require_value "$@"; _set="$_set, difficulty=$(db_quote "$2")"; shift 2 ;;
            --status)     pe_require_value "$@"; _set="$_set, status=$(db_quote "$2")";     shift 2 ;;
            --platform)   pe_require_value "$@"; _set="$_set, platform=$(db_quote "$2")";   shift 2 ;;
            *)            pe_die "target edit: unknown option $1" ;;
        esac
    done
    [ -n "$_set" ] || pe_die "nothing to change"
    db_exec "UPDATE targets SET updated_at=datetime('now')$_set WHERE id=$_id;" || return 1
    _active_id=$(target_active_id 2>/dev/null || true)
    if [ "$_active_id" = "$_id" ]; then
        target__write_display "$_id" || return 1
    fi
    pe_ok "updated target #$_id"
}

target_rm() {
    [ -n "$1" ] || pe_die "usage: pe target rm <name|id>"
    _id=$(target_resolve "$1")
    _resolve_status=$?
    case $_resolve_status in
        0) : ;;
        1) pe_die "no such target: $1" ;;
        *) return "$_resolve_status" ;;
    esac
    _name=$(db_query "SELECT name FROM targets WHERE id=$_id;") || return 1
    pe_confirm_destructive "$_name" \
        "Delete target '$_name' and ALL its flags, creds, hosts and notes (cascade). Irreversible." \
        || { pe_warn "aborted"; return 1; }
    _wasactive=0; [ "$(cat "$PE_ACTIVE_TARGET_FILE" 2>/dev/null)" = "$_id" ] && _wasactive=1
    db_exec "DELETE FROM targets WHERE id=$_id;" || return 1
    [ "$_wasactive" = 1 ] && rm -f "$PE_ACTIVE_TARGET_FILE" "$PE_ACTIVE_DISPLAY_FILE"
    pe_ok "deleted target '$_name'"
}

# dispatch for `pe target <verb>`
target_main() {
    _verb=${1:-ls}; [ $# -gt 0 ] && shift
    case $_verb in
        add)            target_add "$@" ;;
        use|switch)     target_use "$@" ;;
        ls|list)        target_ls ;;
        show)           target_show "$@" ;;
        status)         target_status "$@" ;;
        current)
            target_active_name
            _active_status=$?
            case $_active_status in
                0) : ;;
                1) pe_empty "no active target" ;;
                *) return "$_active_status" ;;
            esac
            ;;
        edit|set)       target_edit "$@" ;;
        rm|del|delete)  target_rm "$@" ;;
        -h|--help|help) printf 'pe target <add|use|ls|show|status|current|edit|rm>\n' ;;
        *)              pe_die "unknown: pe target $_verb (try 'pe target help')" ;;
    esac
}