Script for scanning on nmap Home › Forums › Bash › Script for scanning on nmap Tagged: bash, commented, nmap, script This topic has 1 reply, 1 voice, and was last updated 6 years, 10 months ago by dreadpixel. Viewing 1 reply thread Author Posts August 8, 2016 at 3:46 am #132 dreadpixelParticipant This is the code, just run it with ssh command `#!/bin/bash SCANS_DIR="scans" TMP_DIR="temp" echo "" echo -e "\033[1mNmap Scanning Script\033[0m" echo "" # Usernames and mail addresses to send tarball with scan results shownames () { echo "USERS:" echo "" echo "JD - John Doe" echo "" } listusers () { while [ -z "$email" ] do if [ "$username" == "empty" ]; then echo "Enter your initials:" echo "" shownames echo -n "Initials? " read username fi case "$username" in JD|jd) email="john.doe@example.com";; *) username="empty";; esac done } usage () { echo "Usage: ${0##*/} [-b] -f {source_file} {scan_name} [initials]" echo " ${0##*/} [-b] -i {ip_range} {scan_name} [initials]" echo " ${0##*/} -h" echo " ${0##*/} -u" echo "" echo " -h shows this help" echo " -u shows user list" echo " -b scan only high ports (optional)" echo " -f take a file containing IP addresses" echo " -i IP range to scan (notation: 10.10.10.0-255 o 10.10.10.0/24." echo " {scan_name} it cannot contain whitespaces" echo " [initials] user initials" echo "" } # Check input parameters if [ "$1" == "-u" ]; then shownames exit 0 fi highonly="n" usefile="n" if [[ "$1" == "-h" || -z "$1" ]]; then usage exit 0 fi if [[ "$1" != "-h" && "$1" != "-b" && "$1" != "-f" && "$1" != "-i" ]]; then usage echo "Invalid parameter: $1" exit 1 fi if [ "$1" == "-b" ]; then highonly="y" shift fi if [[ -z "$2" || -z "$3" ]]; then usage echo "Invalid number of parameters" exit 2 fi case "$1" in "-f") iplist="$2" usefile="y";; "-i") ip_addrs="$2";; *) echo "Internal error...?" exit 99;; esac scan_name="$3" if [ -z "$iplist" ]; then ip_addrs2="$ip_addrs" else ip_addrs2="-iL $iplist" ip_addrs="$iplist" fi if [ "$4" ]; then username="$4" else username="empty" fi # Check if file exists and if it is valid if [ "$usefile" == "y" ]; then if [[ ! -e "$ip_addrs" || ! -f "$ip_addrs" || ! -s "$ip_addrs" || ! -r "$ip_addrs" ]]; then echo "Invalid file. Aborting..." exit 3 fi ip_addrs="${ip_addrs##*/}" else net="${ip_addrs2%%/*}" mask="${ip_addrs2##*/}" if [ "$net" != "$mask" ]; then ip_addrs="$net..$mask" fi fi # Which user runs the script listusers echo "" echo -e "\033[01;36mScan launched successfully!\033[0m" echo "" # Scan profiles definition directory="$scan_name.$username.nmapscan<code>date +%H%M-%d%m%y</code>" mkdir "$TMP_DIR/$directory" script="$TMP_DIR/nmapscan-$scan_name-$ip_addrs" name[0]="SWEEP" dst[0]="nmap-$scan_name-$ip_addrs-${name[0]}" arg[0]=" -sP -PS80 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[0]} $ip_addrs2 " name[1]="WEB" dst[1]="nmap-$scan_name-$ip_addrs-${name[1]}" arg[1]=" -sT -P0 -p80,443 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[1]} $ip_addrs2 " name[2]="COMMON" dst[2]="nmap-$scan_name-$ip_addrs-${name[2]}" arg[2]=" -sT -P0 -p21,22,23,25,110,113,143,389 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[2]} $ip_addrs2 " name[3]="UDP" dst[3]="nmap-$scan_name-$ip_addrs-${name[3]}" arg[3]=" -sU -P0 -p7,13,19,53,69,123,137,161,500,1434,1645,1812,2483,2484,3306 --stylesheet c:/nmap.xsl -oA $TMP_DIR/${dst[3]} $ip_addrs2 " name[4]="FAST" dst[4]="nmap-$scan_name-$ip_addrs-${name[4]}" arg[4]=" -vv -sV -P0 -F -O --stylesheet nmap.xsl -oA $TMP_DIR/${dst[4]} $ip_addrs2 " name[5]="HIGH-LIVE" dst[5]="nmap-$scan_name-$ip_addrs-${name[5]}" arg[5]=" -sT -PS80 -n -p- --stylesheet nmap.xsl -oA $TMP_DIR/${dst[5]} $ip_addrs2 " name[6]="HIGHPORTS" dst[6]="nmap-$scan_name-$ip_addrs-${name[6]}" arg[6]=" -sS -n -P0 -p- --stylesheet nmap.xsl -oA $TMP_DIR/${dst[6]} $ip_addrs2 " # Creating the scan script echo "#!/bin/bash" > $script i=0 while [ "$i" -lt "${#name[@]}" ] do if [[ "$highonly" == "y" && "${name[$i]}" != "HIGHPORTS" ]]; then let "i+=1" continue fi echo "" >> $script echo "echo nmap ${arg[$i]}" >> $script echo "nmap ${arg[$i]}" >> $script echo "mv $TMP_DIR/${dst[$i]}.nmap $TMP_DIR/$directory" >> $script echo "mv $TMP_DIR/${dst[$i]}.gnmap $TMP_DIR/$directory" >> $script echo "mv $TMP_DIR/${dst[$i]}.xml $TMP_DIR/$directory" >> $script let "i+=1" done # Create tarball and send it by email to user echo "" >> $script echo "tar cfz $TMP_DIR/$directory.tar.gz -C $TMP_DIR/ $directory" >> $script echo "directory2=\"$scan_name<code>date +%H%M-%d%m%y</code>\"" >> $script echo "mv $TMP_DIR/$directory.tar.gz $SCANS_DIR/$directory2" >> $script echo "mail -s \"Nmap Complete - Scan ${name[$i]} para $scan_name por $username\" $email" >> $script # Delete temporary files echo "rm -rf $TMP_DIR/*" >> $script # Run the scan script chmod a+x $script nohup $script >& /dev/null & exit 0 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 `#!/bin/bash SCANS_DIR="scans"TMP_DIR="temp" echo ""echo -e "\033[1mNmap Scanning Script\033[0m"echo "" # Usernames and mail addresses to send tarball with scan resultsshownames (){echo "USERS:"echo ""echo "JD - John Doe"echo ""} listusers (){while [ -z "$email" ]doif [ "$username" == "empty" ]; thenecho "Enter your initials:"echo ""shownamesecho -n "Initials? "read usernameficase "$username" inJD|jd) email="[email protected]";;*) username="empty";;esacdone} usage (){echo "Usage: ${0##*/} [-b] -f {source_file} {scan_name} [initials]"echo " ${0##*/} [-b] -i {ip_range} {scan_name} [initials]"echo " ${0##*/} -h"echo " ${0##*/} -u"echo ""echo " -h shows this help"echo " -u shows user list"echo " -b scan only high ports (optional)"echo " -f take a file containing IP addresses"echo " -i IP range to scan (notation: 10.10.10.0-255 o 10.10.10.0/24."echo " {scan_name} it cannot contain whitespaces"echo " [initials] user initials"echo ""} # Check input parametersif [ "$1" == "-u" ]; thenshownamesexit 0fihighonly="n"usefile="n"if [[ "$1" == "-h" || -z "$1" ]]; thenusageexit 0fiif [[ "$1" != "-h" && "$1" != "-b" && "$1" != "-f" && "$1" != "-i" ]]; thenusageecho "Invalid parameter: $1"exit 1fiif [ "$1" == "-b" ]; thenhighonly="y"shiftfiif [[ -z "$2" || -z "$3" ]]; thenusageecho "Invalid number of parameters"exit 2ficase "$1" in"-f") iplist="$2"usefile="y";;"-i") ip_addrs="$2";;*) echo "Internal error...?"exit 99;;esacscan_name="$3"if [ -z "$iplist" ]; thenip_addrs2="$ip_addrs"elseip_addrs2="-iL $iplist"ip_addrs="$iplist"fiif [ "$4" ]; thenusername="$4"elseusername="empty"fi # Check if file exists and if it is validif [ "$usefile" == "y" ]; thenif [[ ! -e "$ip_addrs" || ! -f "$ip_addrs" || ! -s "$ip_addrs" || ! -r "$ip_addrs" ]]; thenecho "Invalid file. Aborting..."exit 3fiip_addrs="${ip_addrs##*/}"elsenet="${ip_addrs2%%/*}"mask="${ip_addrs2##*/}"if [ "$net" != "$mask" ]; thenip_addrs="$net..$mask"fifi # Which user runs the scriptlistusers echo ""echo -e "\033[01;36mScan launched successfully!\033[0m"echo "" # Scan profiles definitiondirectory="$scan_name.$username.nmapscan<code>date +%H%M-%d%m%y</code>"mkdir "$TMP_DIR/$directory"script="$TMP_DIR/nmapscan-$scan_name-$ip_addrs" name[0]="SWEEP"dst[0]="nmap-$scan_name-$ip_addrs-${name[0]}"arg[0]=" -sP -PS80 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[0]} $ip_addrs2 " name[1]="WEB"dst[1]="nmap-$scan_name-$ip_addrs-${name[1]}"arg[1]=" -sT -P0 -p80,443 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[1]} $ip_addrs2 " name[2]="COMMON"dst[2]="nmap-$scan_name-$ip_addrs-${name[2]}"arg[2]=" -sT -P0 -p21,22,23,25,110,113,143,389 --stylesheet nmap.xsl -oA $TMP_DIR/${dst[2]} $ip_addrs2 " name[3]="UDP"dst[3]="nmap-$scan_name-$ip_addrs-${name[3]}"arg[3]=" -sU -P0 -p7,13,19,53,69,123,137,161,500,1434,1645,1812,2483,2484,3306 --stylesheet c:/nmap.xsl -oA $TMP_DIR/${dst[3]} $ip_addrs2 " name[4]="FAST"dst[4]="nmap-$scan_name-$ip_addrs-${name[4]}"arg[4]=" -vv -sV -P0 -F -O --stylesheet nmap.xsl -oA $TMP_DIR/${dst[4]} $ip_addrs2 " name[5]="HIGH-LIVE"dst[5]="nmap-$scan_name-$ip_addrs-${name[5]}"arg[5]=" -sT -PS80 -n -p- --stylesheet nmap.xsl -oA $TMP_DIR/${dst[5]} $ip_addrs2 " name[6]="HIGHPORTS"dst[6]="nmap-$scan_name-$ip_addrs-${name[6]}"arg[6]=" -sS -n -P0 -p- --stylesheet nmap.xsl -oA $TMP_DIR/${dst[6]} $ip_addrs2 " # Creating the scan scriptecho "#!/bin/bash" > $scripti=0while [ "$i" -lt "${#name[@]}" ]doif [[ "$highonly" == "y" && "${name[$i]}" != "HIGHPORTS" ]]; thenlet "i+=1"continuefiecho "" >> $scriptecho "echo nmap ${arg[$i]}" >> $scriptecho "nmap ${arg[$i]}" >> $scriptecho "mv $TMP_DIR/${dst[$i]}.nmap $TMP_DIR/$directory" >> $scriptecho "mv $TMP_DIR/${dst[$i]}.gnmap $TMP_DIR/$directory" >> $scriptecho "mv $TMP_DIR/${dst[$i]}.xml $TMP_DIR/$directory" >> $scriptlet "i+=1"done # Create tarball and send it by email to userecho "" >> $scriptecho "tar cfz $TMP_DIR/$directory.tar.gz -C $TMP_DIR/ $directory" >> $scriptecho "directory2=\"$scan_name<code>date +%H%M-%d%m%y</code>\"" >> $scriptecho "mv $TMP_DIR/$directory.tar.gz $SCANS_DIR/$directory2" >> $scriptecho "mail -s \"Nmap Complete - Scan ${name[$i]} para $scan_name por $username\" $email" >> $script # Delete temporary filesecho "rm -rf $TMP_DIR/*" >> $script # Run the scan scriptchmod a+x $scriptnohup $script >& /dev/null &exit 0 August 9, 2016 at 8:00 am #136 dreadpixelParticipant Using pre tags its working for adding the code, we are reviewing if we can do nested answers. Author Posts Viewing 1 reply thread You must be logged in to reply to this topic. Log In Username: Password: Keep me signed in Log In