#!/bin/bash

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m'

TEMPLATE="/home/user/nuclei-templates/http/cves/2025/CVE-2025-0133.yaml"

print_help() {
    echo -e "${YELLOW}Usage: $0 <domain-or-file>${NC}\n"
    echo -e "Scan CVE-2025-0133 vulnerabilities using nuclei and shodanx."
    echo -e "If input is a file, scan domains/IPs from the file."
    echo -e "If input is a domain, run shodanx to find related IPs/domains and scan them.\n"
    echo -e "${YELLOW}Options:${NC}"
    echo -e "  -h, --help, help     Show this help message and exit."
}

if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
    print_help
    exit 0
fi

if [ -z "$1" ]; then
    echo -e "${RED}[-] Usage: $0 <domain-or-file>${NC}"
    exit 1
fi

INPUT=$1

# Print Start Time and ASCII banner with spacing
START_TIME=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${BLUE}Scan Start Time: $START_TIME${NC}\n"

echo -e "${BLUE}
▄▖▖▖▄▖  ▄▖▄▖▄▖▄▖  ▄▖▗ ▄▖▄▖
▌ ▌▌▙▖▄▖▄▌▛▌▄▌▙▖▄▖▛▌▜ ▄▌▄▌
▙▖▚▘▙▖  ▙▖█▌▙▖▄▌  █▌▟▖▄▌▄▌
                          
${YELLOW}-ＩＮＴＥＬＥＯＮ４０４${NC}
\n"

# Variables to store outputs
RESULTS_TXT=""
RESULTS_JSON="["

append_output() {
    local output="$1"
    RESULTS_TXT+="$output"$'\n'
    esc_output=$(echo "$output" | sed 's/"/\\"/g' | tr '\n' ' ')
    RESULTS_JSON+="\"$esc_output\","
}

if [ -f "$INPUT" ]; then
    echo -e "${GREEN}[✔] Input is a file: $INPUT — Skipping ShodanX${NC}"
    while read -r line; do
        IP=$(echo "$line" | awk '{print $1}')
        DOMAIN=$(echo "$line" | awk '{print $2}')
        if [[ -n "$DOMAIN" ]]; then
            echo -e "${BLUE}[*] Scanning domain $DOMAIN (IP: $IP)...${NC}"
            result=$(echo "$DOMAIN" | nuclei -t "$TEMPLATE")
            echo "$result"
            append_output "$result"
        else
            echo -e "${BLUE}[*] Scanning domain $IP ...${NC}"
            result=$(echo "$IP" | nuclei -t "$TEMPLATE")
            echo "$result"
            append_output "$result"
        fi
        echo -e "${BLUE}------------------------------------------------------${NC}"
    done < "$INPUT"
else
    echo -e "${GREEN}[✔] Input is a single domain: $INPUT — Running ShodanX first${NC}"
    shodanx custom -cq "http.favicon.hash:\"-631559155\" hostname:$INPUT" | while read -r line; do
        DOMAIN=$(echo "$line" | awk '{print $1}')
        echo -e "${BLUE}[*] Scanning domain $DOMAIN...${NC}"
        result=$(echo "$DOMAIN" | nuclei -t "$TEMPLATE")
        echo "$result"
        append_output "$result"
        echo -e "${BLUE}------------------------------------------------------${NC}"
    done
fi

END_TIME=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${BLUE}Scan End Time: $END_TIME${NC}"

# Close JSON array correctly
RESULTS_JSON=${RESULTS_JSON%,}
RESULTS_JSON+="]"

# Prompt user for saving results
read -p "Do you want to save the scan results? (Y/N): " answer
case "$answer" in
    [Yy]* )
        prefix="scan_result_$(date '+%Y%m%d_%H%M%S')"
        txt_file="${prefix}.txt"
        json_file="${prefix}.json"

        echo -e "Scan Results for $INPUT\nStarted at: $START_TIME\nEnded at: $END_TIME\n\n$RESULTS_TXT" > "$txt_file"
        echo -e "$RESULTS_JSON" > "$json_file"

        echo -e "${GREEN}[✔] Results saved to $txt_file and $json_file${NC}"
        ;;
    * )
        echo -e "${YELLOW}Scan results were not saved.${NC}"
        ;;
esac
