Harcrack

bash-scan.sh

Nov 4th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.51 KB | None | 0 0
  1. usage="$(basename "$0") [OPTIONS] -- program to retrieve network devices and show IP address paired with the device name
  2. where:
  3.     -h  show this help text
  4.     -i  set the IP interface to check (default: 1) - check available IPs list with [-l] option
  5.     -l  list the available IP addresses
  6.     -a  show all network IPs, even if no computer name is found
  7.     -m  show MAC address
  8.     -b  show devices brand when no other information is available (if nmap installed and if can be found)"
  9.  
  10. myip=1
  11. shownoname=false
  12. showmac=false
  13. showbrand=false
  14. RED='\033[0;31m'
  15. GREEN='\033[0;32m'
  16. ORANGE='\033[0;33m'
  17. NC='\033[0m'
  18.  
  19. while getopts ':halmbi:' option; do
  20.     case "$option" in
  21.         h) echo "$usage"
  22.             exit 0
  23.             ;;
  24.         a) shownoname=true
  25.             ;;
  26.         l) sudo nm-tool | grep -i 'address' | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | nl -n 'ln'
  27.             exit 0
  28.             ;;
  29.         m) showmac=true
  30.             ;;
  31.         b) showbrand=true
  32.             ;;
  33.         i) myip=$OPTARG
  34.             if [ -z $(sudo nm-tool | grep -i 'address' | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sed -n "$myip"p) ]; then
  35.                 echo "there is no such interface, try the [-l] option"
  36.                 exit 1
  37.             fi
  38.             ;;
  39.         :) printf "missing argument for -%s\n" "$OPTARG" >&2
  40.             exit 1
  41.             ;;
  42.         \?) printf "illegal option: -%s\n" "$OPTARG" >&2
  43.             exit 1
  44.             ;;
  45.     esac
  46. done
  47. shift $((OPTIND - 1))
  48.  
  49. # get if nmap is installed
  50. nmapInstalled=$(whereis nmap)
  51. if [ -z "$nmapInstalled" ]; then
  52.     showbrand=false
  53. fi
  54.  
  55. maxwait=0.1;
  56. # get starter IP address
  57. IFS=. read -r i1 i2 i3 i4 <<< $(sudo nm-tool | grep -i 'address' | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sed -n "$myip"p)
  58. IFS=. read -r m1 m2 m3 m4 <<< $(sudo nm-tool | grep -i 'prefix' | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sed -n "$myip"p)
  59. si1=$(($i1 & $m1))
  60. si2=$(($i2 & $m2))
  61. si3=$(($i3 & $m3))
  62. si4=$(($i4 & $m4))
  63. # get my HW address
  64. myhwaddr=$(ifconfig | grep -B 1 "$i1.$i2.$i3.$i4" | grep -oP '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})' | sed -n "$myip"p)
  65. # get number of IPs in network
  66. iprange=$(sudo nm-tool | grep -i 'prefix' | grep -Po '\s[0-9]+' | grep -Po '[0-9]+' | sed -n "$myip"p)
  67. iprange=$(( 2**(32-$iprange) -1 ))
  68. #  cycle through IPs
  69. for((i=1;i<$iprange;i++)); do
  70.     # calulate IP
  71.     ci4=$(($si4 + $i))
  72.     ci3=$(($si3 + ($ci4 / 256) )); ci4=$(($ci4 % 256))
  73.     ci2=$(($si2 + ($ci3 / 256) )); ci3=$(($ci3 % 256))
  74.     ci1=$(($si1 + ($ci2 / 256) )); ci2=$(($ci2 % 256))
  75.     # get computer name
  76.     result=$(timeout $maxwait nmblookup -A "$ci1.$ci2.$ci3.$ci4" | sed -n 2p | grep -Po '\t.+?\s' | xargs)
  77.     hwaddress=$(arp "$ci1.$ci2.$ci3.$ci4" | grep -Po '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})')
  78.     if [ -z "$result" ] && [ ! -z "$hwaddress" ] && [ $shownoname == true ]; then
  79.         result="???"
  80.     fi
  81.     # print if response given
  82.     if [ ! -z "$result" ]; then
  83.         toprint="$ci1.$ci2.$ci3.$ci4"
  84.         if [ $showmac == true ]; then
  85.             if [ -z "$hwaddress" ]; then
  86.                 hwaddress=$myhwaddr
  87.             fi
  88.             toprint="$toprint ( $hwaddress )"
  89.         fi
  90.         myhost=$(grep "$ci1.$ci2.$ci3.$ci4" /etc/hosts | grep -oP '\s.+' | xargs)
  91.         if [ ! -z "$myhost" ]; then
  92.             result="$result ( ${GREEN}$myhost${NC} )"
  93.         fi
  94.         if [ "$ci1.$ci2.$ci3.$ci4" == "$i1.$i2.$i3.$i4" ]; then
  95.             result="$result ( ${RED}THIS DEVICE${NC} )"
  96.         fi
  97.         # if nothing found and nmap installed get device brand
  98.         if [ "$result" == "???" ] && [ $showbrand == true ]; then
  99.             result=$(sudo nmap -sP "$ci1.$ci2.$ci3.$ci4" | grep 'MAC Address' | grep -Po '\(.+?\)')
  100.             if [ "$result" == "(Unknown)" ]; then
  101.                 result="???"
  102.             else
  103.                 result="??? ${ORANGE}$result${NC}"
  104.             fi
  105.         fi
  106.         echo -e "$toprint\t=>\t$result"
  107.     fi
  108. done
Advertisement
Add Comment
Please, Sign In to add comment