Skip to content

Instantly share code, notes, and snippets.

@r4ulcl
Last active March 25, 2018 10:18
Show Gist options
  • Save r4ulcl/15417b1314941bb3d6bcce855232b256 to your computer and use it in GitHub Desktop.
Save r4ulcl/15417b1314941bb3d6bcce855232b256 to your computer and use it in GitHub Desktop.
Script que escanea una red para descubrir hosts y servicios.
#!/bin/bash
### BEGIN INIT INFO
# Provides: Raul Calvo Laorden
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Script that scans the network to discover hosts and services on a computer network.
# Description: Script that scans the network to discover hosts and services on a computer network.
#
### END INIT INFO
#author : Raul Calvo Laorden (raulcalvolaorden@gmail.com)
#description : Script that scans the network to discover hosts and services on a computer network.
#date : 2018-02-26
#usage : sudo bash escanerRed.sh [options]
#-----------------------------------------------------------------------------------------------------------
function help(){
echo -e "\nOptions: "
echo -e "\t --arp-only\t\t: arp-only (without nmap)"
echo -e "\t -c [mail@domain.com]\t: send mail to mail@domain.com"
echo -e "\t -d [mail@domain.com]\t: mail and file"
echo -e "\t -f\t\t\t: full nmap (nmap -sS -vv -sV -O)"
echo -e "\t -h\t\t\t: help"
echo -e "\t -i [interface]\t\t: interface"
echo -e "\t -n [num]\t\t: num loop [default 1]"
echo -e "\t -q\t\t\t: enable quiet mode (without bash output)"
echo -e "\t -s\t\t\t: silence mode [default] (nmap -F -sV --version-intensity 0)"
echo -e "\t -t [sec]\t\t: time between scans [default 300sec]"
echo -e "\t -v\t\t\t: verbose"
}
#variables para cambiar de color del echo
RED=`tput setaf 1`
GREEN=`tput setaf 2`
NC=`tput sgr0`
#mostramos la intefaz
echo -e "author : Raul Calvo Laorden (raulcalvolaorden@gmail.com)"
echo -e "description : Script that scans the network to discover hosts and services on a computer network."
echo -e "usage : sudo bash escanerRed.sh [options]\n"
# only root can run the script
if [[ $EUID -ne 0 ]]; then
echo "${RED}This script must be run as root${NC}" 1>&2
exit 1
fi
MYARRAY=( "$@" ) #array con los parametros que le pasamos al script
SENDMAIL=0 #boolean si enviar mail o no, por defecto no
MOSTRARECHO=1 #booleano para mostrar datos por pantalla o no -q, por defecto si
COMANDONMAP="nmap -F -sV --version-intensity 0 " #comando nmap por defecto
NUMERO=1 #NUMERO de veces que se hara el escaner, por defecto 1
ARPONLY=0 #boolean para saber si solo hay que hacer el arp
DUAL=0 #boolean correo y fichero -d
TIEMPO=300 #TIEMPO de espera entre bucles, por defecto 300 seg
INTERFAZ="*" #INTERFAZ para el arp-scan, * para todas
CONTADOR=0 #CONTADOR para el bucle
VERBOSE=0 #false por defecto
#comprobamos los parametros introducidos
for BUCLE in "$@"
do
case $BUCLE in
-n) NUMERO=${MYARRAY[($BUCLE)+1]};; #si es -n guardamos la siguiente en NUMERO
-c) SENDMAIL=1 ; MAIL=${MYARRAY[($BUCLE)+1]};; #si es -c guardamos en MAIL el siguiente parametro
-q) MOSTRARECHO=0;; #Ponemos MOSTRARECHO a 0 (false)
-f) COMANDONMAP="nmap -sS -vv -sV -O ";; #modificamos el comando de nmap para el completo
--arp-only) ARPONLY=1 ;; #ponemos ARPONLY a 1 (true)
-d) DUAL=1; MAIL=${MYARRAY[($BUCLE)+1]};; #Ponmeos DUAL a true y guardamos el siguiente parametro en MAIL
-t) TIEMPO=${MYARRAY[($BUCLE)+1]};; #guardamos en TIEMPO el siguiente parametro
-i) INTERFAZ=${MYARRAY[($BUCLE)+1]};; #guardamos en INTERFAZ el siguiente parametro, por defecto *
-h) help ; exit 0;; #mostramos el menu principal con las opciones y salimos
-v) VERBOSE=1;; #modo verbose a true
#*) echo "${MYARRAY[$BUCLE]} no reconocido" #si no le conocemos no hacemos nada
esac
done
echo -e "------------------------------------------------------------------------------------------------------\n"
#comprobamos EMAIL y NUMERO y TIEMPO
#si se envia correo con DUAL o SENDMAIL y MAIL y no tiene @ es erroneo, por lo que devolvemos error 1 y cerramos
if [[ ( "$DUAL" = 1 || "$SENDMAIL" = 1 ) && $MAIL != *@* ]]
then
echo "${RED}Direccion email incorrecta [mail@domain.com]${NC}"; exit 1
fi
#Si TIEMPO no es un NUMERO error 2 y cerramos
case $TIEMPO in
''|*[!0-9]*) echo "${RED}Parametro TIEMPO incorrecto${NC}" ; exit 2 ;;
esac
#si NUMERO no es un NUMERO error 3 y cerramos
case $NUMERO in
''|*[!0-9]*) echo "${RED}Parametro NUMERO incorrecto${NC}" ; exit 3;;
esac
#INICIAMOS EL ESCANER
while [ $CONTADOR -lt $NUMERO ] #mientras el CONTADOR sea menor que NUMERO
do
date >> auxEscanerRed.txt #guardamos fecha y hora actual
rm auxEscanerRed.txt #borramos el fichero anterior
CONTADOR=$((CONTADOR+ 1)) #++ al CONTADOR
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "${GREEN}Empezamos bucle $CONTADOR${NC}"
fi
echo -e "Empezamos bucle $CONTADOR" >> auxEscanerRed.txt
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "${GREEN}Obtenemos ifconfig${NC}"
fi
echo -e "Obtenemos ifconfig" >> auxEscanerRed.txt
ifconfig >> auxEscanerRed.txt
IP=$(wget -qO- ipinfo.io/ip)
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "Public ip $IP "
fi
echo -e "Public ip $IP" >> auxEscanerRed.txt
RX='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' #expresion reg para ip valida
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "${GREEN}Escaneando la red en busca de dispositivos${NC}"
fi
echo -e "Escaneando la red en busca de dispositivos" >> auxEscanerRed.txt
if [ "$INTERFAZ" = "*" ] ; then
ARP=$(arp-scan --retry=4 --ignoredups --localnet --random)
echo -e "arp-scan --retry=4 --ignoredups --localnet --random \n" >> auxEscanerRed.txt
else
ARP=$(arp-scan --retry=4 --ignoredups -I $INTERFAZ --localnet --random)
echo -e "arp-scan --retry=4 --ignoredups -I $INTERFAZ --localnet --random \n" >> auxEscanerRed.txt
fi
if [ "$VERBOSE" = 1 ] ; then
echo -e "$ARP" >> auxEscanerRed.txt
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "$ARP"
fi
else
echo -e "$ARP" | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' >> auxEscanerRed.txt
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "$ARP" | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
fi
fi
LINEAS=$( echo -e "$ARP" | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | wc -l)
CONTADOR=0
if [ "$ARPONLY" = 0 ] ; then #si no arp-only
for IP in $ARP; do
if [[ $IP =~ ^$RX\.$RX\.$RX\.$RX ]]; then #si es una ip valida
CONTADOR=$((CONTADOR + 1))
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "${GREEN}Escaneando $CONTADOR de $LINEAS con ip $IP${NC}"
fi
echo -e "Escaneando $CONTADOR de $LINEAS con ip $IP" >> auxEscanerRed.txt
echo "$COMANDONMAP$IP" >> auxEscanerRed.txt
NMAP=$($COMANDONMAP$IP) # nmap -sS -sV -O -v $IP
if [ "$VERBOSE" = 1 ] ; then
echo -e "$NMAP" >> auxEscanerRed.txt
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "$NMAP"
fi
else
echo -e "$NMAP" | grep -E "/tcp|/udp" >> auxEscanerRed.txt
if [ "$MOSTRARECHO" = 1 ] ; then
echo -e "$NMAP" | grep -E "/tcp|/udp"
fi
fi
fi
done
date >> auxEscanerRed.txt
fi
DATE=`date +%Y-%m-%d:%H:%M:%S`
if [ "$DUAL" = 1 ] ; then #correo y fichero
ENVIO=$(mpack -s EscanerRed auxEscanerRed.txt $MAIL)
mv auxEscanerRed.txt salidaEscanerRed-$DATE.txt
elif [ "$SENDMAIL" = 1 ] ; then #solo correo
ENVIO=$(mpack -s EscanerRed auxEscanerRed.txt $MAIL)
else #solo fichero
mv auxEscanerRed.txt salidaEscanerRed-$DATE.txt
fi
echo -e "$ENVIO" >> auxEscanerRed.txt
if [ $CONTADOR -lt $NUMERO ] ; then
sleep $TIEMPO
else
exit
fi
done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment