01_pwnbox/tools-helper.sh
#!/usr/bin/env bash

COLOR_CYAN_BOLD="\e[1;36m"
COLOR_GREEN_BOLD="\e[1;32m"
COLOR_YELLOW_BOLD="\e[1;33m"
COLOR_RED_BOLD="\e[1;31m"
COLOR_RST="\e[0m"

SIGN_INFO="[*]"
SIGN_SUCCESS="[+]" # \xE2\x9C\x94 (Unicode tick)
SIGN_WARNING="[!]" # \xE2\x9A\xA0 (Unicode warning sign)
SIGN_ERROR="[-]" # "\xE2\x9C\x97 (Unicode cross)

TOOL_IMPACKET_SECRETSDUMP="impacket-secretsdump"
TOOL_MASSCAN="masscan"
TOOL_NMAP="nmap"
TOOL_PYPYKATZ="pypykatz"

FOLDER_NAME_NMAP="nmap"
FOLDER_NAME_RECON="recon"
FOLDER_NAME_LOOT="loot"

function print_info() {
    echo -e "${COLOR_CYAN_BOLD}${SIGN_INFO}${COLOR_RST} $1"
}

function print_success() {
    echo -e "${COLOR_GREEN_BOLD}${SIGN_SUCCESS}${COLOR_RST} $1"
}

function print_warning() {
    echo -e "${COLOR_YELLOW_BOLD}${SIGN_WARNING}${COLOR_RST} $1" >&2
}

function print_error() {
    if [ $# == 1 ]
    then
        echo -e "${COLOR_RED_BOLD}${SIGN_ERROR}${COLOR_RST} $1" >&2
    elif [ $# == 2 ]
    then
        echo -e "${COLOR_RED_BOLD}${SIGN_ERROR}${COLOR_RST} '${1}'@'${BASH_SOURCE[0]}' failed with error: ${2}" >&2
    fi
}

function check_argc() {
    if [ $1 -lt $2 ];
    then
        print_error ${FUNCNAME[0]} "Invalid number of arguments (${1}). Expected ${2} argument(s)."
        return 1
    else
        return 0
    fi
}

function confirm() {
    check_argc $# 2 || return
    arg_msg=$1
    arg_def=$2
    case "${arg_def}" in
        yes)
            loc_def_yes=1
            loc_prompt="[Y/n]"
            ;;
        no)
            loc_def_yes=0
            loc_prompt="[y/N]"
            ;;
        *)
        print_error ${FUNCNAME[0]} "Incorrect parameter: ${2}"
        return 1
    esac

    loc_prompt="${arg_msg} ${loc_prompt} "
    while true
    do
        read -p "${loc_prompt}" loc_resp
        case $loc_resp in
            [yY][eE][sS]|[yY])
                # The answer is 'yes' or 'y', set value accordingly.
                ret_val=0
                break
                ;;
            [nN][oO]|[nN])
                # The answer is 'no' or 'n', set value accordingly.
                ret_val=1
                break
                ;;
            "")
                # The answer is empty, determine value based on default.
                if [ $loc_def_yes == 1 ]
                then
                    ret_val=0
                else
                    ret_val=1
                fi # if [ $loc_def_yes == 1 ]
                break
                ;;
        esac # case $loc_resp in
    done # while true
    return $ret_val
}

function get_timestamp() {
    date "+%F_%H-%M-%S"
}

function test_directory_exists() {
    if [ -d "${1}" ]
    then
        return 0
    else
        print_error "Directory does not exist: ${1}"
        return 1
    fi
}

function test_file_exists() {
    if [ -f "${1}" ]
    then
        return 0
    else
        print_error "File does not exist: ${1}"
        return 1
    fi
}

function test_file_not_exists() {
    if [ -f "${1}" ]
    then
        print_error "File exists: ${1}"
        return 1
    else
        return 0
    fi
}