#!/bin/bash
#
# Jet-Tools by noam alum
#

## Functions
	function get_user_input {
		var_name=$1
		var_type=$2
		shift 2  # Shift the arguments to access the remaining ones

		# Join the remaining arguments to form the complete prompt
		prompt="$*"

		read -p " - $prompt" $var_name < /dev/tty

		while [ -z "${!var_name}" ]; do
			read -p " - $prompt" $var_name < /dev/tty
		done

		if [ "$var_type" == "INT" ]; then
			while ! [[ ${!var_name} =~ ^[0-9]+$ ]]; do
				echo " - Insert an integer!"
				read -p " - $prompt" $var_name < /dev/tty
			done
		elif [ "$var_type" == "STR" ]; then
			while [[ -n "${!var_name}" && "${!var_name}" =~ ^[0-9]+$ ]]; do
				echo " - Insert a string!"
				read -p " - $prompt" $var_name < /dev/tty
			done
		fi

		export $var_name
	}

	function get_input {
		echo -e "$(for ((i = 0; i < ${#Tools[@]}; i++)); do echo "$(( $i + 1)).${Tools[$i]}" | tr -s " " "_" && [ $(( $i + 1 )) != ${#Tools[@]} ] && echo "|"; done | xargs)\n$1\n$1"
		[ -z $1 ] && read -p "- Which tool do you want to use? : " tool_id < /dev/tty || tool_id="$1"

		while [ -z "$tool_id" ]; do
			get_input
		done

		while [ $tool_id -gt ${#Tools[@]} ] || [ $tool_id -lt 0 ] || ! [[ $tool_id =~ ^[0-9]+$ ]]; do
			echo -e "\033[0;31mYou can't use $tool_id, it does not exist or is not a valid integer!\033[0m"
				get_input
		done
	}

	# 1 - imunify_malicious_list
	function imunify_malicious_list {
		[ -z "$1" ] && get_user_input iml_user STR "Which user where you scanning? : " || iml_user="$1"
		[ -z "$2" ] && get_user_input iml_files INT "How manny infected files found? : " || iml_files="$2"

		imunify360-agent malware malicious list --user $iml_user --limit $iml_files | awk '{print $8}' | tail -n +2 > /home/$iml_user/infected_files_$iml_user.txt

		chown $iml_user.$iml_user /home/$iml_user/infected_files_$iml_user.txt

		echo " - infected files list was created in /home/$iml_user/infected_files_$iml_user.txt"
	}

	# 2 - Alter tables
	function Alter_tables {
		[ -z "$1" ] && get_user_input at_DB_USER STR "What is the user associate with the DataBase in question? : " || at_DB_USER="$1"
		[ -z "$2" ] && get_user_input at_DB_PASS STR "What is the users password? : " || at_DB_PASS="$2"
		[ -z "$3" ] && get_user_input at_DB_NAME STR "What is the DataBase name? : " || at_DB_NAME="$3"

		[ -z "$4" ] && get_user_input at_TYPE STR "Do you want to swap to InnoDB or MyISAM? : " || at_TYPE="$4"

		while [ "$at_TYPE" != "InnoDB" ] && [ "$at_TYPE" != "MyISAM" ];do
			echo -e " - \033[0;31mPlease use InnoDB or MyISAM !\033[0m (case sensitive)"
			get_user_input at_TYPE STR "Do you want to swap to InnoDB or MyISAM? : "
		done

		curl -Ls alum.sh/codes/alter-table|bash -s $at_DB_USER $at_DB_PASS $at_DB_NAME $at_TYPE InnoDB/MyISAM
	}

	# 3 - WordPress related
	function WordPress_related {
		## WC functions
			function WordPress_migration {
				echo "- WordPress migration"
				wget -q https://alum.sh/files/WPM_sZadN1_w.sh && chmod +x WPM_sZadN1_w.sh
				 echo "     run: \"./WPM_sZadN1_w.sh\""
			}

			function Add_user {
				echo "- Add user"
				[ -z "$1" ] && get_user_input wc_user STR "	What is the new users username? : " || wc_user="$1"
				[ -z "$2" ] && get_user_input wc_mail STR "	What is the new users mail? : " || wc_mail="$2"

				wp user create $wc_user $wc_mail --role=administrator --allow-root --skip-plugins
			}

			function Change_password {
				echo "- Change password"
				[ -z "$1" ] && get_user_input wc_user STR "	What is the users username? : " || wc_user="$1"
				[ -z "$2" ] && get_user_input wc_pass STR "	What is the new password? : " || wc_pass="$2"

				wp user update $wc_user --user_pass="$wc_pass" --allow-root --skip-plugins
			}

			function Set_root_priv {
				echo "- Set root priv"
				[ -z "$1" ] && get_user_input wc_user STR "	What is the users username? : " || wc_user="$1"

				wp user update $wc_user --role=administrator --allow-root --skip-plugins
			}

			function wp_recovery {
				echo "- wp recovery"
				curl -Ls 'alum.sh/wp_recovery'|bash
			}
		## WC functions

		## Define Commands
			wc_Commands=("Add user" "Change password" "Set root priv" "WordPress migration" "wp_recovery")

			function wc_get_input {
				echo -e "$(for ((i = 0; i < ${#wc_Commands[@]}; i++)); do echo "$(( $i + 1)).${wc_Commands[$i]}" | tr -s " " "_" && [ $(( $i + 1 )) -ne ${#wc_Commands[@]} ] && echo "|"; done | xargs)\n"
				[ -z "$1" ] && read -p "- Which tool do you want to use? : " wc_Command_id < /dev/tty || wc_Command_id="$1"

				while [ -z "$wc_Command_id" ]; do
					wc_get_input
				done

				while [ $wc_Command_id -gt ${#wc_Commands[@]} ] || [ $wc_Command_id -lt 0 ] || ! [[ $wc_Command_id =~ ^[0-9]+$ ]]; do
					echo -e "\033[0;31mYou can't use $wc_Command_id, it does not exist or is not a valid integer!\033[0m"
						wc_get_input
				done
			}
			wc_get_input "$1"

			wc_Command_id=$(( $wc_Command_id - 1 ))
		## Define commands

		## Call command
			wc_command_function="$(echo "${wc_Commands[$wc_Command_id]}" | tr -s " " "_"})"
			shift 1
			$wc_command_function "$1" "$2" "$3" "$4"
		## Call command
	}

	# 4 - Running proc
	function Running_proc {
		[ -z "$1" ] && get_user_input rp_user STR "What is the user you want to check? : " || rp_user="$1"

		watch -t "ps -u $rp_user | tail -n +2 | cat -n"
	}

	# 5 - litespeed data domains
	function litespeed_data_domains {
		[ -z "$1" ] && get_user_input dom_name STR "What domain would you like to search for? : " || dom_name="$1"

		VH_users=($(grep -rlo $dom_name /usr/local/lsws/conf/ | grep -v '.bak\|httpd_config' | awk -F '/usr/local/lsws/conf/' {'print $2'} | awk -F '.xml' {'print $1'}))

		for ((i=0; i<${#VH_users[@]}; i++))
		do
			VH_roots+=($(cat /usr/local/lsws/conf/httpd_config.xml | sed -n "/<name>${VH_users[$i]}<\/name>/,/<\/vhRoot>/ s/.*<vhRoot>\([^<]*\)<\/vhRoot>.*/\1/p" | sed "s/\$VH_NAME\//${VH_users[$i]}/g"))
			DOC_roots+=($(awk -F'<docRoot>\\$VH_ROOT|</docRoot>' 'NF>1{print $2}' /usr/local/lsws/conf/${VH_users[$i]}.xml))


			echo -e "\n$(( 1 + $i)). User: ${VH_users[$i]}\n   Root directory: ${VH_roots[$i]}${DOC_roots[$i]}\n   Domains: \n$(sed -n "/<vhost>${VH_users[$i]}<\/vhost>/,/<domain>/s/.*<domain>\(.*\)<\/domain>.*/\1/p" /usr/local/lsws/conf/httpd_config.xml | sort -u | xargs -n 1 | cat -n)\n"
		done
	}

	# 6 - download data
	function download_data {
		[ -z "$1" ] && get_user_input download_url STR "What is the download url? : " || download_url="$1"
		[ -z "$2" ] && get_user_input file_name STR "What would you like to name your file? : " || file_name="$2"

		# check status code
		status_code="$(curl -s -I -o /dev/null -w "%{http_code}" "$download_url")"
		if [ "$(echo "$status_code" | cut -c 1)" -eq "3" ] || [ "$(echo "$status_code" | cut -c 1)" -eq "2" ]; then
			wget -O "$file_name" --no-check-certificate --quiet "$download_url"
			if [ "$?" -eq 0 ]; then
				echo -e " - File downloaded at: $(pwd)/$file_name"

                file_type="$(file $file_name | awk -F ':|,' '{print $2}')"
                file_extension="$(echo $file_name | awk -F '.' '{print $2}')"
                detected_file_extension="$(file --extension $file_name | awk {'print $2'})"

				if [ "$file_extension" != "$detected_file_extension" ] && [ "???" != "$detected_file_extension" ];then
					echo -e " - File extension mismatch:\n    + detected extension: $detected_file_extension\n    + inputted extension: $file_extension\n    + File type detected: $file_type"
				elif [ "???" == "$detected_file_extension" ]; then
					echo -e " - File extension could not be detected:\n    + detected extension: $detected_file_extension\n    + inputted extension: $file_extension\n    + File type detected: $file_type"
				fi
			else
				echo " - There was an error while downloading $file_name"
			fi
		else
			echo " - URL is invalid (status code: $status_code), exiting."
			exit 1
		fi
	}

## Functions

## Define tools
	echo "Jet-Tools:"
	Tools=("imunify malicious list" "Alter tables" "WordPress related" "Running proc" "litespeed data domains" "download data")
	get_input $1

	tool_id=$(( $tool_id - 1 ))
## Define tools

## Call tool
	tool_function=$(echo "${Tools[$tool_id]}" | tr -s " " "_"})
	echo -e "\n----\n$tool_function :"
	shift 1
	$tool_function "$1" "$2" "$3" "$4"
## Call tool
Bash