#!/bin/bash
####################################################################
# Elog is a depedency of Eternal Scanner (Denveloped by peterpt)   #
#          https://gitlab.com/peterpt/eternal_scanner              #
####################################################################
#setup colors
green='\033[92m'
yellow='\e[0;33m'
orange='\e[38;5;166m'
cyan='\e[0;36m'

sleep 3
if [ "$EUID" -ne 0 ]
  then 
  root="n"
  else
  root="y"
  fi

# Set Default Data directory
if [[ "$root" == "y" ]]
then
defdir="/usr/local/share/Eternal_Scanner"
else
echo "This tools requires root previleges to run"
exit 0
fi

# Default file that defines which program is running
masstat="$defdir/stat"
if [[ ! -f "$masstat" ]]
then
echo -e "$green""Elog is a dependency from Eternal Scanner"
echo -e "Run escan instead"
exit 1
fi
stat=$(sed -n '1p' < $masstat)

echo ""

# When CTRL+C is pressed
trap ctrl_c INT
function ctrl_c() {
if [ "$stat" == "1" ] 
then
pkill -f masscan* >/dev/null 2>&1
rm -rf "$defdir/pid.tmp" >/dev/null 2>&1
elif [ "$stat" == "2" ] 
then
kill "$PID" >/dev/null 2>&1
rm -rf "$defdir/pid.tmp" >/dev/null 2>&1
fi
exit 1
}

# Read Process ID generated by escan
PID=$(cat $defdir/pid.tmp)

function start(){

# Define where it should be msflog file
mlog="$defdir/msflog.log"

# Check if msfconsole pid is running
ps --pid "$PID" &>/dev/null
pd="$?"

# Start routine to grab the value "100% from msflog file"
# In case metasploit already exited then
if [ "$pd" -eq "1" ]
then

# Check if msflog file exists
if [ -f  "$mlog" ] 
then

# open the log and search in last lines the value (Scanned)"
gmsf=$(sed -n -e 's/^.*Scanned //p' $mlog | tail -1 | sed 's/ *$//')
if [[ -z "$gmsf" ]]
then
gmsft="Verified: "
else
gmsft="Verified: $gmsf"
fi
# Compare output with current value of variable (a)
# At this point variable (a) should be at < (Scanned 90%) from msflog
if [ "$gmsft" != "$a" ]
then 

# In case the values are different then display that line (100%) and exit elog
echo "$gmsft"
exit 0
fi
fi
else

# In case msflog does not exists yet then start a timing process to wait for it .
# Some machines may take a while to load metasploit , and this is why this timming exists
if [ ! -f  "$mlog" ]
then

# Increase tm value + 1 from its current value
tm=$((tm+1))

# until 60s if msflog is not created then elog will exit
if [ "$tm" == "60" ]
then
echo -e "$yellow" "Metasploit Not Detected"
exit 0
else

# Timmer is not yet at 60 , pause 1 second and loop again
sleep 1
start
fi
else

# At this point msfconsole PID is running and msflog was generated to be consulted 

# gmsft will grab the last value (scanned) from msflog
gmsf=$(sed -n -e 's/^.*Scanned //p' $mlog | tail -1 | sed 's/ *$//')
if [[ -z "$gmsf" ]]
then
gmsft="Verified: "
else
gmsft="Verified: $gmsf"
fi
# In case gmsft is equal to variable (a) then set variable a with same value and wait 1 second
if [ "$gmsft" == "$a" ]
then
a="$gmsft"
sleep 1

# timer value sets to 59 of 60 seconds , this way when elog ends its job it will reach 60s in 1 second and will exit itself so escan can continue its job
tm="59"

#start loop
start
else

# In case gmsft is different then variable (a) then setup a with same value
# this process avoids repeating the same percentage on screen from msflog
echo "$gmsft"
a="$gmsft"
tm="59"
sleep 1

# start loop
start
fi
fi
fi
}
# output splash screen when elog will check metasploit
msfc(){
echo -e "$orange" "+-----------------------------------------------------+"
echo -e "$orange" "|$green Realtime Eternal Scan Metasploit 6 results Checkout$orange |"
echo -e "$orange" "+-----------------------------------------------------+"
echo -e "$orange" "|$yellow       Please wait for percentage output            $orange |"
echo -e "$orange" "+-----------------------------------------------------+$cyan"

# Timer start value to wait for msflog file generated by metasploit 
tm="0"

# variable used to check in msflog for percentage
a="Verified: "

}

mass() {
# Define where it should be masscan file
massl="$defdir/masscan"

# Check if masscan pid is running
ps --pid "$PID" &>/dev/null
pd="$?"
if [ "$pd" -eq "1" ]
then

if [ -f  "$massl" ] 
then
gmsft=$(grep "open" $massl | wc -l)

if [ "$gmsft" != "$a" ]
then 

echo -ne "$yellow $gmsft$cyan Ips To Check Ahead\033[0K\r"
exit 1
fi
fi
else

if [ ! -f  "$massl" ]
then

tm=$((tm+1))

# until 10s if masscan file output is not created then elog will exit
if [ "$tm" == "10" ]
then
echo -e "$yellow" "Masscan file was not found"
rm -rf "$defdir/pid.tmp" >/dev/null 2>&1
exit 1
else

# Timmer is not yet at 10 , pause 1 second and loop again
sleep 1
mass
fi
else

# At this point masscan PID is running and masscan ip output was generated to be consulted 

# gmsft will grab the last value (scanned) from masscan
gmsft=$(grep "open" $massl | wc -l)

# In case gmsft is equal to variable (a) then set variable a with same value and wait 1 second
if [ "$gmsft" == "$a" ]
then
a="$gmsft"
sleep 1

# timer value sets to 9 of 10 seconds , this way when elog ends its job it will reach 15s in 1 second and will exit itself so escan can continue its job
tm="9"

#start loop
mass
else

# In case gmsft is different then variable (a) then setup a with same value
# this process avoids repeating the same percentage on screen from scanner output

echo -ne "$green  Ips detected with port$yellow 445$green opened so far$yellow :$cyan $gmsft\033[0K\r"
a="$gmsft"
tm="9"
sleep 1

# start loop
mass
fi
fi
fi
}
masplsh () {
echo -e "$orange -------------------------------------------"
echo -e "$orange |$green  Scanning for Ips with port$yellow 445$green opened $orange |"
echo -e "$orange -------------------------------------------"
echo ""
}
# If stat file is 1 then it means that masscan is running
if [ "$stat" == "1" ] 
then
masplsh
mass
fi
# If stat file is 2 then it means that metasploit is running
if [ "$stat" == "2" ] 
then
msfc
start
fi
