#!/bin/sh
################################################################################
#                ____                     _ __                                 #
#     ___  __ __/ / /__ ___ ______ ______(_) /___ __                           #
#    / _ \/ // / / (_-</ -_) __/ // / __/ / __/ // /                           #
#   /_//_/\_,_/_/_/___/\__/\__/\_,_/_/ /_/\__/\_, /                            #
#                                            /___/ team                        #
#                                                                              #
# ripdc.sh - reverse ip domain checker                                         #
#                                                                              #
# FILE                                                                         #
# ripdc.sh                                                                     #
#                                                                              #
# DATE                                                                         #
# 2013-06-05                                                                   #
#                                                                              #
# DESCRIPTION                                                                  #
# A script which maps domains related to an given ip address or domainname.    #
#                                                                              #
# AUTHOR                                                                       #
# noptrix                                                                      #
#                                                                              #
################################################################################


VERSION="ripdc.sh v0.4"

FALSE=0
TRUE=1

SUCCESS=1337
FAILURE=31337

VERBOSE="/dev/null"

NORM="$(printf '\033[0m')"
BOLD="$(printf '\033[1;37;10m')"
RED="$(printf '\033[1;31;10m')"
GREEN="$(printf '\033[1;32;10m')"
YELLOW="$(printf '\033[1;33;10m')"
BLUE="$(printf '\033[1;34;10m')"

IPREFIX="${BOLD}${BLUE}[+]${NORM}"
GPREFIX="${BOLD}${GREEN}[*]${NORM}"
WPREFIX="${BOLD}${YELLOW}[!]${NORM}"
EPREFIX="${BOLD}${RED}[-]${NORM}"
VPREFIX="${BOLD}  >${NORM}"

BANNER="${BLUE}         _           __
   _____(_)___  ____/ /____
  / ___/ / __ \/ __  / ___/
 / /  / / /_/ / /_/ / /__
/_/  /_/ .___/\__,_/\___/
      /_/
${NORM}
  --== [ by nullsecurity.net ] ==--
"

HELP="${BOLD}usage${NORM}

  ripdc.sh -t <arg> [opts] | <misc>

${BOLD}options${NORM}

  -t <target>   - ip-address or domain name to reverse map
  -v            - verbose mode (default: off)

${BOLD}misc options${NORM}

  -V      - print version and exit
  -H      - print this help message

${BOLD}examples${NORM}

  # reverse map an ip address
  \$ ripdc.sh -t 1.2.3.4

  # reverse map a domain name
  \$ ripdc.sh -t example.com

  # verbose mode
  \$ ripdc.sh -t 1.2.3.4 -v
"


reverse_map()
{
  url="https://api.hackertarget.com/reverseiplookup/?q=${target}"

  printf "%s reverse mapping %s\n" "$IPREFIX" "$target"

  domains="$(curl -s "$url" 2>$VERBOSE)"

  if echo "$domains" | grep -qE "^error|No records found|API count exceeded"; then
    warn "no domains were found (or API limit hit)"
  elif [ -n "${domains}" ]; then
    printf "%s listing found domains\n" "$GPREFIX"
    for domain in ${domains}; do
      printf "%s\n" "$domain"
    done
  else
    warn "no domains were found"
  fi

  return $SUCCESS
}


warn()
{
  printf "%s %s\n" "$WPREFIX" "${@}"

  return $SUCCESS
}


err()
{
  printf "%s %s\n" "$EPREFIX" "${@}"
  exit $FAILURE

  return $SUCCESS
}


usage()
{
  printf "%s\n" "$HELP"
  exit $SUCCESS

  return $SUCCESS
}


banner()
{
  printf "%s\n" "$BANNER"

  return $SUCCESS
}


check_argc()
{
  if [ ${#} -lt 1 ]; then
    err "-H for help and usage"
  fi

  return $SUCCESS
}


check_args()
{
  printf "%s checking arguments\n" "$IPREFIX" > $VERBOSE 2>&1
  if [ -z "$target" ]; then
    err "WTF?! mount /dev/brain"
  fi

  return $SUCCESS
}


get_opts()
{
  while getopts t:vVH flags; do
    case ${flags} in
      t)
        target="${OPTARG}"
        ;;
      v)
        VERBOSE="/dev/stdout"
        ;;
      V)
        printf "%s\n" "$VERSION"
        exit $SUCCESS
        ;;
      H)
        usage
        ;;
      *)
        err "WTF?! mount /dev/brain"
        ;;
    esac
  done

  return $SUCCESS
}


main()
{
  banner
  check_argc "${@}"
  get_opts "${@}"
  check_args "${@}"
  reverse_map

  printf "%s game over\n" "$IPREFIX"

  return $SUCCESS
}


main "${@}"
