Iam ussing RaspberryPis a lot as network engineer, i have at least 8 PIs around house and at the office. I use them for console servers, remote debuging and analyzing network. Network traffic capture etc. They are very convient, they can be used with the power-bank even for 24h or more, depends on the need.

Every time when we where taking over some customers network, we always done a lot of things manualy (some still do, no other way). But for smaller customers this script is really usefull, and you can just save all the output as a part of current network configuration too.

What i wanted to achieve with this script is, that i can give the small RaspberryPI even to not so skilled technican and i dont even need to go to the customer site to get a lot of useful informations about the customers network.

First i wrote down the things that we always check about the customer network, and choose the things that are worth the effort to automate, so the final list was:

-LAN segment RaspberryPI is connected to, RaspberryPI DHCP address
-Customers public IP address
-Network devices that are connected to the same LAN, their IP and MAC addresses
-Vendors of the MAC addresses for some quick look into the “customer arsenal”
-WiFi SSID-s near the RaspberryPI
-Internet speed on the location
-Traceroute to the internet, gateway, our data-center
-Latency to gateway, our data-center and internet
-Advance nmap scan for open ports on devices in the same LAN as RaspberryPI

So the script does gather all the above info sends it to email and upload to some SCP server, to sure get the info. The RaspberryPI even OpenVPN connects back to headquarters as soon as it gets the IP address over DHCP. So i have root access to the linux box inside customer network for some manual work if needed. It uses RGB led for scan progress:


RED blinking: waiting for DHCP server
BLUE blinking: doing fast scan, 10-20 seconds
BLUE led on: doing advance NMAP scan
GREEN blinking: done

#!/bin/bash
#Simple script to scan network

ip=$(ip addr show eth0 | grep "inet\b"|awk '{print $2}')
while [ "$ip" =  "" ]; do
   echo "Waiting for ip address drom DHCP!"
   sudo python3 /home/pi/led/red-blink.py
   ip=$(ip addr show eth0 | grep "inet\b"|awk '{print $2}')
done
localip=$(ip addr show eth0 | grep "inet\b"|awk '{print $2}')
sudo python3 /home/pi/led/blue-blink.py &
publicip=$(dig +short myip.opendns.com @resolver1.opendns.com)
#sudo echo "NetscanPI online: "$localip | mutt -s "NetscanPI online: "$publicip -- networkteam@email.com
/bin/echo -e "\e[1;31mCreating folder if it doesnt exist!"
mkdir -p /home/pi/pentest/scans
/bin/echo -e "\e[1;31mProcessing Netmap scan... Please wait!\e[0m"
netscan=$(sudo nmap -sn $(ip addr show eth0 | grep "inet\b"|awk '{print $2}') | awk '/Nmap scan/{printf $5;}/MAC Address:/{printf " => "$3;}/MAC Address:/{print $4 $5 $6 ". "}')
netbiosscan=$(sudo nbtscan $(ip addr show eth0 | grep "inet\b"|awk '{print $2}'))
/bin/echo "$netscan" >> /home/pi/pentest/scans/netmap.txt
activehosts=$(sudo cat /home/pi/pentest/scans/netmap.txt | wc -l)
/bin/echo -e " " >> /home/pi/pentest/scans/netmap.txt
/bin/echo "$netbiosscan" >> /home/pi/pentest/scans/netmap.txt
/bin/echo "Online hosts: ""$activehosts" >> /home/pi/pentest/scans/netmap.txt
/bin/echo -e "\e[1;31mAdding public IP to netmap file,...!\e[0m"
sed -i 1i"LAN Network:" /home/pi/pentest/scans/netmap.txt
sed -i 1i"Public IP: "$publicip /home/pi/pentest/scans/netmap.txt
#/bin/echo -e "\e[1;31mScanning WiFi networks,...!\e[0m"
wifi=$(sudo iw dev wlan0 scan | grep SSID)
sudo echo -e " " >> /home/pi/pentest/scans/netmap.txt
/bin/echo "WiFi-Networks:" >> /home/pi/pentest/scans/netmap.txt
/bin/echo "$wifi" >> /home/pi/pentest/scans/netmap.txt
/bin/echo -e "\e[1;31mNotifing NetADMINS about my online activity!\e[0m"
sudo cat /home/pi/pentest/scans/netmap.txt | mutt -s "Netscan:"$ip" progress notification!" -- networkteam@email.com
sudo pkill -f blue-blink.py
/bin/echo -e "\e[1;31mProcessing advance scan,...!\e[0m"
sudo python3 /home/pi/led/blue-on.py
/bin/echo -e "\e[1;31mSpeedtest in progress please wait,...!\e[0m"
sudo curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python > /home/pi/pentest/scans/speedtest.txt
download=$(cat /home/pi/pentest/scans/speedtest.txt | grep "Download")
upload=$(cat /home/pi/pentest/scans/speedtest.txt | grep "Upload")
#sed -i 2i"Internet speed:" /home/pi/pentest/scans/netmap.txt
sed -i "2i'$download'" /home/pi/pentest/scans/netmap.txt
sed -i "3i'$upload'" /home/pi/pentest/scans/netmap.txt
sed -i 4i"+++++++++++++++++++++++++++++++++++++++++++++++++" /home/pi/pentest/scans/netmap.txt
/bin/echo -e " " >> /home/pi/pentest/scans/netmap.txt
/bin/echo -e "\e[1;31mTraceroute in progress,...!\e[0m"
netrace=$(traceroute 8.8.8.8)
/bin/echo -e " " >> /home/pi/pentest/scans/netmap.txt
/bin/echo "Traceroute:" >> /home/pi/pentest/scans/netmap.txt
/bin/echo "$netrace" >> /home/pi/pentest/scans/netmap.txt
#/bin/echo -e "\e[1;31mScanning WiFi networks,...!\e[0m"
/bin/echo -e " " >> /home/pi/pentest/scans/netmap.txt
#wifi=$(sudo iw dev wlan0 scan | grep SSID)
#/bin/echo "WiFi-Networks:" >> /home/pi/pentest/scans/netmap.txt
#/bin/echo "$wifi" >> /home/pi/pentest/scans/netmap.txt
/bin/echo -e "\e[1;31mProcessing advance NMAP scan... Please wait!\e[0m"
sudo nmap -T4 -oA /home/pi/pentest/scans/customer $(ip addr show eth0 | grep "inet\b"|awk '{print $2}')
/bin/echo -e "\e[1;31mCreating archive file...Please wait!\e[0m"
lastfile=$(ls -last /home/pi/pentest/| grep .zip | awk '/-rw-r/{print $10;}' | cut -d "." -f1| cut -d "n" -f2 | sort -rn | head -n 1)
/bin/echo -e "\e[1;31mLast scan file:scan"$lastfile".zip\e[0m"
nextfile=$((lastfile+1))
sudo xsltproc /home/pi/pentest/scans/customer.xml -o /home/pi/pentest/scans/customer.html
cd /home/pi/pentest/scans/
zip /home/pi/pentest/scan$nextfile netmap.txt speedtest.txt customer.nmap customer.html
/bin/echo -e "\e[1;31mFile created:scan"$nextfile".zip\e[0m"
/bin/echo -e "\e[1;31mUploading scan results:scan"$nextfile".zip"
sshpass -p 'password1234' scp -P 2245 /home/pi/pentest/scan$nextfile.zip root@remote-server.com:/netscans/scan$nextfile.zip
/bin/echo -e "\e[1;31mSending email to Network team\e[0m"
#mpack -s "NetSCAN:"$ip -d ./scans/netmap.txt scan$nextfile.zip networkteam@email.com
sudo cat /home/pi/pentest/scans/netmap.txt | mutt -a /home/pi/pentest/scan$nextfile.zip -s "Netscan:"$ip" complete!" -- user@test.com
sudo rm -r /home/pi/pentest/scans/*.*
sudo python3 /home/pi/led/blue-off.py
#sudo pkill f blue-blink.py
sudo python3 /home/pi/led/green-on.py

pi@raspberrypi:~/pentest $

 

Categories:

Comments are closed