#!/bin/bash

source staticValues.env

# ==== REQUIREMENTS CHECK ===================================
if msfconsole --version &> /dev/null;
then
    echo -e "${GREEN}[SUCCESS]${WHITE} Metasploit is already installed."
else
    echo "" && echo -e "${YELLOW}[WARNING]${WHITE} Installing Metasploit." && echo ""
    curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb > msfinstall && chmod 755 msfinstall && ./msfinstall
    echo "yes" | msfconsole -q -x "exit"
    echo -e "${GREEN}[SUCCESS]${WHITE} Metasploit installed successfully." 
fi

# ==== LIST SERVICES ========================================
grep -E "^[0-9]+/tcp" ./Output/$target-portScan.txt | awk '{
  match($0, /([a-zA-Z0-9._-]+) ([0-9]+\.[0-9]+)/, arr);
  if (arr[0] != "")
    print arr[1], arr[2]
}' > serviceVersionsBrief.txt

# ==== VULNERABILITY SCAN ===================================
while read -r service;
do 
  echo "=====================================================" >> $target-vulnerabilityScan.txt
  echo "SERVICE: $service" >> $target-vulnerabilityScan.txt
  msfconsole -q -x "search $service; exit" >> $target-vulnerabilityScan.txt
  echo "" >> $target-vulnerabilityScan.txt
done < serviceVersionsBrief.txt

rm serviceVersionsBrief.txt 
mv $target-vulnerabilityScan.txt ./Output/
