python-nmap

import nmap
nm = nmap.PortScanner()
nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389') 

python扫描网站并且获取端口信息

#coding=utf-8

import nmap
import optparse
import threading
import sys
import re
import socket

#----------------------------------------------------------------------
def get_ip(url):
  """"""
  try:
    ip = socket.gethostbyname(url)
    return ip
  except Exception,e:
    return False
  

def portscanner(target_url,target_port):
  try:
    if get_ip(target_url):
      target_host = get_ip(target_url)
      try:
        scanner = nmap.PortScanner()
        results = scanner.scan(hosts=target_host,ports=target_port,arguments='-T4 -A -v -Pn ')  #禁ping的快速扫描
        #print('扫描语句是:',results['nmap']['command_line'])
        #print('[*]主机' + target_host + '的' + str(target_port) + '端口状态为:' + results['scan'][target_host]['tcp'][int(target_port)]['state'])
        ip_banner = results['scan'][target_host]['tcp'][int(target_port)]['name']
        with open('res.txt', 'a+') as rr:
          rr.write(target_url + '-----' + target_host + '-----' + target_port + '-----' + ip_banner + '\n')
      except Exception,e:
        pass
  except Exception,e:
    pass

def main(target_host):

  target_port = [20, 21, 22, 23, 25, 53, 80, 81, 135, 161, 443, 445, 1433, 1521, 2601, 3306, 3389, 4440, 5601, 8080, 8081, 8088, 8089, 9043, 9200, 27017]
  
  for port in target_port:
    #portscanner(target_host,str(port))
    t = threading.Thread(target=portscanner,args=(target_host,str(port)))
    t.start()

if __name__ == '__main__':
  with open('domain_target.txt', 'a+') as f:
    list_target = f.readlines()
    count = 1
    for i in list_target:
      print '[+] Start ' + i + '第   ' + str(count) + '   个'
      
      main(i.strip())
      
      count += 1
    ```

检测ip存活性再进行nmap扫描存入的均为open状态

#coding=utf-8

import nmap
import optparse
import threading
import sys
import re
import socket
import datetime
from func_timeout import func_set_timeout
import time
import datetime
import func_timeout


from pymongo import MongoClient
import re
class MongoDB(object):
  """"""
  #----------------------------------------------------------------------
  def __init__(self, host, port, database):
    """Constructor"""
    self.host = host
    self.port = port
    self.database = database
    self.conn = MongoClient(self.host, self.port)
    self.coll = self.conn[self.database]


Mongo = MongoDB('192.168.200.244', 27017, 'kuxuan')


def portscanner(target_url, url, url_name):

  try:
    scanner = nmap.PortScanner()
    try:
      results = scanner.scan(hosts=target_url, arguments='sS host-timeout 10 ')  #禁ping的快速扫描
    except Exception,e:
      pass
    #print('扫描语句是:',results['nmap']['command_line'])
    #print('[*]主机' + target_host + '的' + str(target_port) + '端口状态为:' + results['scan'][target_host]['tcp'][int(target_port)]['state'])
    ip_banner = results['scan']
    if len(ip_banner) != 0:
      for i in ip_banner:
        #print i 
        ip = str(i)
        scan_info = results['scan'][ip]['tcp']
        for i in scan_info:
          if len(scan_info) < 50:
            if scan_info[i]['state'] == 'open':  
              Mongo.coll['res'].update({'url':url},
                                         {"$set":{'ip': target_url, 'title': url_name,
                                                  'port.'+str(i):scan_info[i]['name']
                                                  }},upsert = True)
              print(url + '-----' + ip + '-----' + str(i) + '-----' + scan_info[i]['name'] + scan_info[i]['state'] +'\n')
    else:
      with open('error.txt', 'a+') as ee:
        ee.write(target_url + '\n')
  except Exception,e:
    print str(e)
    pass


import os
if __name__ == '__main__':
  starttime = datetime.datetime.now()
  with open('0816.txt', 'a+') as f:
    list_target = f.readlines()
    count = 1
    for i in list_target:
      s = i.strip().split(' ')
      ret = os.system("ping  -w 1 %s"%s[2].strip())
      if ret == 0:
        try:
          portscanner(s[2].strip(), s[1].strip(),s[0].strip())
        except Exception,e:
          continue

      count += 1
  endtime = datetime.datetime.now()
  print (endtime - starttime).seconds

你可能感兴趣的:(python-nmap)