03_ad/ad-secretsdump-machine.sh
#!/usr/bin/env bash

### BEGIN SCRIPT HEADER
SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
SCRIPT_NAME=$(basename "${SCRIPT_PATH}")
DIR_SCRIPT=$(dirname "${SCRIPT_PATH}")
DIR_ROOT=$(dirname $DIR_SCRIPT)
HELPER="${DIR_ROOT}/01_pwnbox/tools-helper.sh"
source "$HELPER" || exit
### END SCRIPT HEADER

### BEGIN COMMON
function print_usage_and_exit() {
    print_info "Usage: ${SCRIPT_NAME} <HOSTNAME>"
    exit
}

check_argc $# 1 || print_usage_and_exit
### END COMMON

test_directory_exists "./${FOLDER_NAME_LOOT}" || exit

MACHINE_NAME=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
FILE_SAM="./${FOLDER_NAME_LOOT}/${MACHINE_NAME}_sam.bin"
FILE_SYSTEM="./${FOLDER_NAME_LOOT}/${MACHINE_NAME}_system.bin"
FILE_SECURITY="./${FOLDER_NAME_LOOT}/${MACHINE_NAME}_security.bin"
FILE_OUTPUT="./${FOLDER_NAME_LOOT}/${MACHINE_NAME}_secrets.txt"

COMMAND="${TOOL_IMPACKET_SECRETSDUMP}"

test_file_not_exists "${FILE_OUTPUT}" || exit
test_file_exists "${FILE_SYSTEM}" || exit

if [ -f $FILE_SAM ]
then
    COMMAND="${COMMAND} -sam '${FILE_SAM}'"
else
    print_warning "SAM file not found: ${FILE_SAM}"
fi

if [ -f $FILE_SECURITY ]
then
    COMMAND="${COMMAND} -security '${FILE_SECURITY}'"
else
    print_warning "SECURITY file not found: ${FILE_SECURITY}"
fi

COMMAND="${COMMAND} LOCAL"

eval ${COMMAND} | tee "${FILE_OUTPUT}"

print_info "Output save to: ${FILE_OUTPUT}"
