Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import nmap
  3. from socket import *
  4.  
  5. nm = nmap.PortScanner()
  6.  
  7.  
  8. def scanNet(networkAddress, networkBits, numberPortsToScan):
  9.     target = ('{0}/{1}').format(networkAddress, networkBits)  
  10.     settings = ('-T4 --top-ports {0}').format(numberPortsToScan)
  11.     nm.scan(hosts=target, arguments=settings)
  12.  
  13.  
  14.  
  15. def getLiveHosts(index=None):
  16.     hostList = []
  17.     for host in nm.all_hosts():
  18.         hostList.append({'ip': host, 'hostname': nm[host].hostname()})
  19.     if isinstance(index, int):
  20.         return hostList[index]
  21.     else:
  22.         return hostList
  23.  
  24.  
  25.  
  26. def getHostPorts(host):
  27.     openPorts = []
  28.     for proto in nm[host].all_protocols():
  29.         for openPort in nm[host][proto]:
  30.             if (nm[host][proto][openPort]['state'] == 'open'):
  31.                openPorts.append(openPort)
  32.     return openPorts
  33.  
  34.  
  35.  
  36.  
  37. def captureBanner(host, port)
  38.     conn = socket(AF_INET, SOCK_STREAM)
  39.     conn.connect((target, port))
  40.    
  41.     if port == 80 || port == 8080 || port == 8000:
  42.         conn.send('GET HTTP/1.1 \r\n')
  43.     else:
  44.         conn.send('Hello, is it me you\'re looking for? \r\n')
  45.    
  46.     banner = conn.recv(1024)
  47.     return str(banner)
  48.  
  49.  
  50. def printHostInfo(hostId):
  51.     host = getLiveHosts(hostId):
  52.     print("{0} | {1}".format(liveHost['ip'], liveHost['hostname']))
  53.     for port in getHostPorts(liveHost['ip']):
  54.         print("\tOpen Port: " + port)
  55.         print("\t\t" + captureBanner(liveHost['ip'], port))
  56.         print("---   ---   ---   ---   ---   ---")
  57.  
  58.  
  59.  
  60.  
  61. def printHostsInfo(hostIndex = None):
  62.     if isinstance(hostIndex, int):
  63.         hostList =getLiveHosts(hostIndex);
  64.     else:
  65.         hostList = getLiveHosts()
  66.     for host in hostList:
  67.         print("{0} | {1}".format(host['ip'], host['hostname']))
  68.         for port in getHostPorts(host['ip']):
  69.             print("\tOpen Port: " + port)
  70.             print("\t\t" + captureBanner(host['ip'], port))
  71.             print("---   ---   ---   ---   ---   ---")
  72.         print("---------------------------\n\n")
  73.  
  74.  
  75.  
  76.  
  77. scanNet('192.168.1.200', 32, 5)
  78. printHostsInfo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement