小豆浆 基于python的socket扫描(threading、argparse、Queue、socket、sys、time) www.xdoujiang.com

  • A+
所属分类:python实战
  1. 小豆浆 基于python的socket扫描(threadingargparseQueuesocketsystime) www.xdoujiang.com
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4. #--------------------------------------------------
  5. #Author:gongzheng
  6. #Email:85646830@163.com
  7. #FileName:socketscan.py
  8. #Function: 
  9. #Version:1.0 
  10. #Created:2017-01-05
  11. #--------------------------------------------------
  12. import threading
  13. import argparse
  14. from Queue import Queue
  15. import socket
  16. import sys
  17. from time import ctime
  18. def scan(host,port,show):
  19.     s = socket.socket()
  20.     #protocolname = 'tcp'
  21.     s.settimeout(0.1)
  22.     if s.connect_ex((host, port)) == 0:
  23.         try:
  24.             #print "%s  open => service name: %s" %(port,socket.getservbyport(port,protocolname))
  25.             print "%s open" %(port,socket.getservbyport(port,))
  26.         except:
  27.             #print port, 'open => service name: No Found'  
  28.             print port, 'open'
  29.     else:
  30.         if show:
  31.             print port ,'Close'
  32.     s.close()
  33. def writeQ(queue,start,end):
  34.     for i in range(start,end):
  35.         queue.put(i,1)
  36. def readQ(queue,host,start,end,show,thread):
  37.     for i in range((end-start)/thread):
  38.         num = queue.get(1)
  39.         scan(host,num,show)
  40. def thread_demo(host,port_start,port_end,show,thread):
  41.     funcs = [writeQ,readQ]
  42.     nfunc = range(len(funcs))
  43.     q = Queue(65535)
  44.     threads = []
  45.     t = threading.Thread(target=funcs[0],args=(q,port_start,port_end))
  46.     threads.append(t)
  47.     for i in range(thread):
  48.         t = threading.Thread(target=funcs[1],args=(q,host,port_start,port_end,show,thread))
  49.         threads.append(t)
  50.     for i in range(thread+1):
  51.         threads[i].start()
  52.     for i in range(thread+1):
  53.         threads[i].join()
  54. if __name__ == '__main__':
  55.     #print "all start at: ",ctime()
  56.     parser = argparse.ArgumentParser(description="input your host and port")
  57.     parser.add_argument("-o","--on",help="show close",action="store_true")
  58.     parser.add_argument("--host",help="chose host",action="store",default='127.0.0.1',dest="host")
  59.     parser.add_argument("--host_start",help="chose host_start",action="store",default='127.0.0.1',dest="host_start")
  60.     parser.add_argument("--host_end",help="chose host_end",action="store",default='127.0.0.1',dest="host_end")
  61.     parser.add_argument("--port",help="chose port",action="store",default=80,type=int,dest="port")
  62.     parser.add_argument("--port_start",help="chose port port_start",action="store",type=int,default=0,dest="port_start")
  63.     parser.add_argument("--port_end",help="chose port port_end",action="store",type=int,default=512,dest="port_end")
  64.     parser.add_argument("--thread",help="how much thread",action="store",type=int,default=4,dest="thread")
  65.     args = parser.parse_args()
  66.     host = args.host
  67.     host_start = args.host_start
  68.     host_end   = args.host_end
  69.     port = args.port
  70.     port_start = args.port_start
  71.     port_end   = args.port_end
  72.     thread = args.thread
  73.     show = args.on
  74.     if host == "127.0.0.1":
  75.         for hosts in range(int(host_start.split(".")[-1]),int(host_end.split(".")[-1])+1):
  76.             hosts = host_start.split(".")[0]+"."+host_start.split(".")[1]+"."+host_start.split(".")[2]+"."+str(hosts)
  77.             print "----------"+hosts+"----------"
  78.             if host_start != host_end and port_start == 0 and port_end == 512:
  79.                 scan(hosts,port,show)
  80.             elif host_start != host_end and port_start != 0 or port_end != 512:
  81.                 thread_demo(hosts,port_start,port_end+1,show,thread)
  82.             elif host_start == host_end and port == 80:
  83.                 thread_demo(hosts,port_start,port_end+1,show,thread)
  84.             elif host_start == host_end and port != 80:
  85.                 scan(hosts,port,show)
  86.             else:
  87.                 print "En... Your Input Is Wrong"
  88.     else:
  89.         print "----------"+host+"----------"
  90.         if port != 80:
  91.             scan(host,port,show)
  92.         else:
  93.             thread_demo(host,port_start,port_end+1,show,thread)
  94. #print "all end   at: ",ctime()
  95. 使用:
  96. python socketscan.py --host 172.16.1.131 --port_start 1 --port_end 65535 --thread 8
    python socketscan.py --host_start 172.16.1.100 --host_end 172.16.1.127 --port_start 1 --port_end 65535 --thread 8
    python socketscan.py --host_start 172.16.1.139 --host_end 172.16.1.143 --port_start 1 --port_end 65535 --thread 8
  97. 日志:
  98. root@192.168.1.118:socketscanlog# cat 20170918.scanlog
    ----------172.16.0.131----------
    21 open
  99. 22 open
    80 open
    443 open
    ----------172.16.0.100----------
    21 open
  100. 22 open
    80 open
    443 open
    10050 open
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: