#!/bin/bash

echo "
/..---..---..---..---..----..---. .---.
||  -||  -||'-/ /| |  | || || |-< | |- 
--'---''---' '-' '---''----''-''-''---'
Hacker Attack Simulation von 1337core
-> Leetcore Cybersecurity GmbH
-> www.hackerattack.de

Save your API key in >> api.key
"

APIKEY=$(<api.key)
if [[ -z "$APIKEY" ]]; then
    echo "Enter APIKey (from https://www.hackerattack.de/settings.html):"
    read -p "API Key: " APIKEY
fi

echo "Enter Domain (e.g. domain.com):"
read -p "Target: " TARGET

# add to flatrate_queue
output_start_scan=$(curl --silent -f -b "api_key=$APIKEY" "https://api.hackerattack.de/security/flatrate_queue/$TARGET")
echo $output_start_scan

# save filename from queue
output_filename=$(echo $output_start_scan | grep -o 'scan_.*json')
echo $output_filename

# wait for scan to finish -> show json
if [[ -n "$output_filename" ]]; then
    output_finished_scan=$(curl --silent --retry 1440 --retry-all-errors --retry-delay 60 -f -b "api_key=$APIKEY" "https://api.hackerattack.de/security/download/$output_filename")
    echo $output_finished_scan |
    grep -Eo '"[^"]*" *(: *([0-9]*|"[^"]*")[^{}\["]*|,)?|[^"\]\[\}\{]*|\{|\},?|\[|\],?|[0-9 ]*,?' |
    awk '{if ($0 ~ /^[}\]]/ ) offset-=4; printf "%*c%s\n", offset, " ", $0; if ($0 ~ /^[{\[]/) offset+=4}'
fi

# download json
curl --silent -b "api_key=$APIKEY" "https://api.hackerattack.de/security/download/$output_filename" -o "$output_filename"