devdiscover.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/python3
  2. import os, sys
  3. import colorama
  4. from colorama import Fore, Style
  5. def printUsage():
  6. print('Usage: ./devdiscover.py --ip-start=[ip] --ip-end=[ip] --wait-response=[secs] --iface=[interface] \
  7. [--verbose] [--show-names] [--show-macs]')
  8. print('./devdiscover.py --help for additional help')
  9. def help():
  10. print()
  11. print(Fore.LIGHTBLUE_EX)
  12. print('''devdiscover is a simple utility that can discover all LAN/WLAN devices in specified IP Range.
  13. Usage: ./devdiscover.py --ip-start=[ip] --ip-end=[ip] --wait-response=[secs] --iface=[interface]
  14. [--verbose] [--show-names] [--show-macs]
  15. --ip-start=[ip] Start bound of IP range (*)
  16. --ip-end=[ip] End bound of IP range (*)
  17. --show-macs Show MAC addresses?
  18. --show-names Show names of devices?
  19. --verbose Show all scanned IP addresses
  20. --wait-response=[seconds] How much time should I wait for response? (*)
  21. --iface=[interface] Which interface should I use to scan IP addresses? (*)
  22. ''')
  23. print(Style.RESET_ALL)
  24. def check(myArgs):
  25. foundArgs = 0
  26. startIP,endIP,waitInterval,iface,verbose,macs,names = '','',0,'',False,False,False
  27. for argument in myArgs:
  28. # --ip-start, --ip-end, --wait-response, --iface // it exists? //
  29. if argument.find('--ip-start=') != -1 or argument.find('--ip-end=') != -1:
  30. if len(argument[argument.index('=')+1:].split('.')) == 4: # all is ok
  31. foundArgs += 1
  32. if argument.find('--ip-start=') != -1:
  33. _startIP = argument[argument.index('=')+1:]
  34. startIP = _startIP
  35. else:
  36. _endIP = argument[argument.index('=')+1:]
  37. endIP = _endIP
  38. else: # we've got --ip-start / --ip-end, but specified IP is not IP
  39. print(Fore.RED + 'Incorrect usage of argument --ip-start / --ip-end')
  40. print(Style.RESET_ALL)
  41. printUsage()
  42. exit()
  43. elif argument.find('--wait-response=') != -1:
  44. if len(argument[argument.index('=')+1:]) > 0:
  45. foundArgs += 1
  46. waitInterval = argument[argument.index('=')+1:]
  47. else:
  48. print(Fore.RED + 'Incorrect usage of argument --wait-response')
  49. print(Style.RESET_ALL)
  50. printUsage()
  51. exit()
  52. elif argument.find('--iface=') != -1:
  53. _ifaces = os.popen('ls /sys/class/net/')
  54. ifaces = _ifaces.read().split('\n')
  55. specifiedIface = argument[argument.index('=')+1:]
  56. if specifiedIface in ifaces:
  57. foundArgs += 1
  58. iface = specifiedIface
  59. else:
  60. print(Fore.RED + 'Specified network interface does not exist at the moment')
  61. print(Style.RESET_ALL)
  62. printUsage()
  63. exit()
  64. elif argument == '--verbose' or argument == '--show-macs' or argument == '--show-names':
  65. if argument == '--verbose':
  66. verbose = True
  67. elif argument == '--show-macs':
  68. macs = True
  69. elif argument == '--show-names':
  70. names = True
  71. foundArgs += 1
  72. if foundArgs >= 4:
  73. if '.'.join(_startIP.split('.')[0:3]) == '.'.join(_endIP.split('.')[0:3]):
  74. if int(max(_startIP.split('.'))) <= 255 and int(max(_endIP.split('.'))) <= 255:
  75. if int(min(_startIP.split('.'))) >= 0 and int(min(_endIP.split('.'))) >= 0:
  76. if int(_startIP.split('.')[-1]) < int(_endIP.split('.')[-1]):
  77. return (startIP,endIP,waitInterval,iface,verbose,macs,names)
  78. else: # start bound is < end bound
  79. print(Fore.RED + 'Incorrect usage of argument --ip-start / --ip-end')
  80. print(Style.RESET_ALL)
  81. printUsage()
  82. exit()
  83. else: # one of IP blocks is < 0
  84. print(Fore.RED + 'Incorrect usage of argument --ip-start / --ip-end')
  85. print(Style.RESET_ALL)
  86. printUsage()
  87. exit()
  88. else: # one of IP blocks is > 255
  89. print(Fore.RED + 'Incorrect usage of argument --ip-start / --ip-end')
  90. print(Style.RESET_ALL)
  91. printUsage()
  92. exit()
  93. else: # xxx.xxx.xxx.??? != xxx.xxx.xxx.???
  94. print(Fore.RED + 'Incorrect usage of argument --ip-start / --ip-end')
  95. print(Style.RESET_ALL)
  96. printUsage()
  97. exit()
  98. else:
  99. print(Fore.RED + 'Not all necessary arguments were found.')
  100. print(Style.RESET_ALL)
  101. printUsage()
  102. exit()
  103. def work(startIP, endIP, waitInterval, iface, verbose, macs, names):
  104. avaliableHosts = []
  105. for last_block in range(int(startIP.split('.')[-1]), int(endIP.split('.')[-1])):
  106. ipaddr = '.'.join(startIP.split('.')[0:3]) + '.' + str(last_block)
  107. ping_stream = os.popen('ping ' + ipaddr + ' -c 1 -W ' + str(waitInterval) + ' -I ' + iface)
  108. ping_data = ping_stream.read()
  109. if 'bytes from' in ping_data:
  110. avaliableHosts.append(ipaddr)
  111. if verbose:
  112. print(Fore.GREEN + '[+] ' + ipaddr + ': FOUND' + Style.RESET_ALL)
  113. else:
  114. if verbose:
  115. print(Fore.RED + '[-] ' + ipaddr + ': no such host' + Style.RESET_ALL)
  116. print(Fore.LIGHTBLUE_EX + '[i] Getting summary information...')
  117. for host in avaliableHosts:
  118. print('IP: ' + host, end=' ')
  119. if names:
  120. nslookup_stream = os.popen('nslookup ' + host)
  121. nslookup_data = nslookup_stream.read()
  122. try:
  123. print('Name: ' + nslookup_data[nslookup_data.index('=')+2:-3], end=' ')
  124. except:
  125. print('Name: UNKNOWN (report this!)', end=' ')
  126. if macs:
  127. arp_stream = os.popen('arp -a ' + host)
  128. arp_data = arp_stream.read()
  129. try:
  130. print('MAC: ' + arp_data[arp_data.index('at')+3:arp_data.index('[')-1], end=' ')
  131. except:
  132. print('MAC: UNKNOWN (report this!)', end=' ')
  133. print()
  134. print(Fore.GREEN + '[+] Job done!' + Style.RESET_ALL)
  135. def main():
  136. print(Fore.YELLOW)
  137. os.system('figlet devdiscover')
  138. print('v. 0.1')
  139. print('thm, 2021')
  140. print('https://github.com/thm-unix/devdiscover/')
  141. #print(Style.RESET_ALL)
  142. myArgs = sys.argv
  143. if '--help' in myArgs:
  144. help()
  145. exit()
  146. # checking if arguments are typed correctly and setting them
  147. startIP,endIP,waitInterval,iface,verbose,macs,names = check(myArgs)
  148. print(Fore.LIGHTBLUE_EX)
  149. print('[i] IP Start Bound: ' + startIP)
  150. print('[i] IP End Bound: ' + endIP)
  151. print('[i] Wait Response Interval: ' + str(waitInterval) + ' secs')
  152. print('[i] Interface: ' + iface)
  153. print('[i] Verbose: ' + str(verbose))
  154. print('[i] Show MAC addresses: ' + str(macs))
  155. print('[i] Show names of devices: ' + str(names))
  156. print(Fore.GREEN + '\nStarting...')
  157. print(Fore.RED + 'Please be patient, it can take a couple of minutes.' + Style.RESET_ALL)
  158. work(startIP, endIP, waitInterval, iface, verbose, macs, names)
  159. main()