SecurityTube

Multi-Threaded SYN Scanner using Scapy, Threading and Queue

Jul 26th, 2012
2,513
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import threading
  4. import Queue
  5. import time
  6. from scapy.all import *
  7.  
  8. class WorkerThread(threading.Thread) :
  9.  
  10.     def __init__(self, queue, tid) :
  11.         threading.Thread.__init__(self)
  12.         self.queue = queue
  13.         self.tid = tid
  14.         print "Worker %d Reporting for Service Sir!" %self.tid
  15.  
  16.     def run(self) :
  17.         total_ports = 0
  18.         while True :
  19.             port = 0
  20.             try :
  21.                 port = self.queue.get(timeout=1)
  22.             except  Queue.Empty :
  23.                 print "Worker %d exiting. Scanned %d ports ..." % (self.tid, total_ports)
  24.                 return
  25.             # port scanning to begin
  26.             # we rely on scapy to do this
  27.             ip = sys.argv[1]
  28.             response = sr1(IP(dst=ip)/TCP(dport=port, flags="S"), verbose=False, timeout=.2)
  29.             # only checking for SYN-ACK == flags = 18
  30.             # filtererd ports etc. is another story altogether
  31.             if response :
  32.                 if response[TCP].flags == 18 :
  33.                    
  34.                     print "ThreadId %d: Received port number %d Status: OPEN" %(self.tid, port)
  35.             self.queue.task_done()
  36.             total_ports += 1
  37.  
  38.  
  39. queue = Queue.Queue()
  40.  
  41. threads = []
  42. for i in range(1, 10) :
  43.     print "Creating WorkerThread : %d"%i
  44.     worker = WorkerThread(queue, i)
  45.     worker.setDaemon(True)
  46.     worker.start()
  47.     threads.append(worker)
  48.     print "WorkerThread %d Created!"%i
  49.    
  50. for j in range (1,1000) :
  51.     queue.put(j)
  52.  
  53. queue.join()
  54.  
  55. # wait for all threads to exit
  56.  
  57. for item in threads :
  58.     item.join()
  59.  
  60. print "Scanning Complete!"
RAW Paste Data