#!/bin/bash
source pakuri.conf
source $MODULES_PATH/misc_module.sh
WINDOW_NAME="${modules[2]}"
MSF=0
function passcrack_menu()
{
    local KEY
    local BRUTE

    box_1 "Password Crack"
    echo -e "-------- Select Action ---------"
    if [ $MSF = 1 ];then
        echo -e "${RED_b}Metasploit is running! ->${NC}"
        read -p "Press Enter to continue..."
    else
        echo -e "Do you want to do it?" 
        yes-no-help
        read -n 1 -s KEY
        if [ $KEY = 1 ];then
            cd $WDIR
            if [[ ! -d $WDIR/brutespray ]]; then
                mkdir $WDIR/brutespray
            else
                rm $WDIR/brutespray/*.txt
            fi

            BRUTE="$WDIR/brutespray/brute"
            tmux send-keys -t $WINDOW_NAME.1 "nmap -T4 -p- --max-retries 1 --max-scan-delay 20 --defeat-rst-ratelimit --open -oX $BRUTE-target.xml -iL $TARGETS ; \
            brutespray --file $BRUTE-target.xml --output $WDIR/brutespray/ --threads 5 --hosts 5 ; cat $WDIR/brutespray/*.txt" C-m
            tmux select-pane -t $WINDOW_NAME.0
            read -p "Press Enter to continue..."
        elif [ $KEY -eq 3 ];then
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/exploit_help_module.sh 21" C-m 
            tmux select-pane -t $WINDOW_NAME.0
        fi
    fi
}

function metasploit_menu()
{
    local KEY
    local Ans

    box_2 "Metasploit"
    echo -e "------- Metasploit Menu --------"
    if [ $MSF = 0 ];then
        select_3 "Setup" "Start Metasploit" "help"
    else
        select_3 "Setup" "Stop Metasploit" "help"
    fi
    read -n 1 -s KEY
    exploit_banner
    box_2 "Metasploit"
    echo -e "------- Metasploit Menu --------"
    case $KEY in
        1 )
            if [ $MSF = 1 ];then
                echo -e "${RED_b}Metasploit is running! ->${NC}"
                read -p "Press Enter to continue..."
            else
                msf_setup_menu
            fi ;;
        2)
            if [ $MSF = 0 ];then
                box_2 "Start Matasploit"
                tmux send-keys -t $WINDOW_NAME.1 'msfconsole' C-m
                MSF=1
                echo -e "Please wait a moment for the metasploit to start up."
                sleep 10
                read -p "Press Enter to continue..."
            else
                box_2 "Stop Matasploit"
                echo -e "Are you sure you want to stop Metasploit?"
                yes-no
                read -n 1 -s Ans
                if [ $Ans -eq 1 ];then
                    tmux send-keys -t $WINDOW_NAME.1 'exit -y' C-m
                    MSF=0
                    read -p "Press Enter to continue..."
                fi
            fi ;;
        3)
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/exploit_help_module.sh 22" C-m 
            tmux select-pane -t $WINDOW_NAME.0
            ;;
        9)
            break;;
        *)
            ;;
    esac
}

function msf_setup_menu()
{
    local KEY
    local Ans

    box_1 "Setup"
    echo -e "-------- Select Action ---------"
    select_3 "Create DB" "Delete DB" "Help"
    read -n 1 -s KEY
    case $KEY in
        1)
            box_1 "Create DB"
            tmux send-keys -t $WINDOW_NAME.1 'msfdb init' C-m
            read -p "Press Enter to continue..."
            ;;
        2)
            box_2 "Delete DB"
            echo -e "Are you sure you want to delete it?"
            yes-no
            read -n 1 -s Ans
            if [ $Ans -eq 1 ];then
                tmux send-keys -t $WINDOW_NAME.1 'msfdb delete' C-m
                read -p "Press Enter to continue..."
            fi
            ;;
        3)
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/exploit_help_module.sh 221" C-m 
            tmux select-pane -t $WINDOW_NAME.0
            ;;
        9)
            break;;
        *)
            ;;
    esac
}

function searchsploit_menu()
{
    box_3 "Searchsploit"
    echo -e "-------- Select Action ---------"
    echo -e "Search the Exploit Database for Keywords."
    echo -e "   e.g. CVE-XXXX-XXXX, MSXX-XXX, apache ..."
    echo -e ""
    read -p "Imput keyword: " keyword
    tmux send-keys -t $WINDOW_NAME.1 "searchsploit $keyword" C-m
    echo -e "Select it with the mouse cursor and press enter to copy it to the clipboard."
    echo -e ""
    read -p "Press Enter to continue..."
}

function exploit_manage()
{
    local KEY
    local Ans
    local flg_pc
    
    while :
    do
        exploit_banner
        if ps -ef|grep brutespray|grep -v "grep" >/dev/null;then
            flg_pc=1
        else
            flg_pc=0
        fi

        select_4 "Password Crack" "Metasploit" "Searchsploit" "Help"
        read -n 1 -t 25 -s KEY
        exploit_banner
        if [ $flg_pc = "1" ];then
            echo -e "Password Crack process is Running!"
            read -p "Press Enter to continue..."
        else
            case "$KEY" in
                1 )
                    passcrack_menu ;;
                2 )
                    metasploit_menu ;;
                3 )
                    searchsploit_menu ;;
                4 )
                    tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/exploit_help_module.sh main" C-m 
                    tmux select-pane -t $WINDOW_NAME.0 ;;
                9 )
                    tmux select-window -t "${modules[0]}" ;;
                * )
                    ;;
            esac
        fi
    done
}
exploit_manage