Commit f0fce106 by fwkz

Using custom threads in scanner

parent 8631b17b
import threading import time
from routersploit import ( from routersploit import (
exploits, exploits,
print_error, print_error,
...@@ -6,7 +7,7 @@ from routersploit import ( ...@@ -6,7 +7,7 @@ from routersploit import (
print_status, print_status,
print_info, print_info,
utils, utils,
LockedIterator, threads,
) )
...@@ -33,9 +34,31 @@ class Exploit(exploits.Exploit): ...@@ -33,9 +34,31 @@ class Exploit(exploits.Exploit):
threads = exploits.Option(8, "Number of threads") threads = exploits.Option(8, "Number of threads")
def run(self): def run(self):
data = LockedIterator(utils.iter_modules(utils.EXPLOITS_DIR))
self.vulnerabilities = [] self.vulnerabilities = []
self.run_threads(self.threads, self.target_function, data) data_producer = threads.DataProducerThread(utils.iter_modules(utils.EXPLOITS_DIR))
data_producer.start()
time.sleep(1)
workers = []
for worker_id in xrange(int(self.threads)):
worker = threads.WorkerThread(
target=self.target_function,
name='worker-{}'.format(worker_id),
)
workers.append(worker)
worker.start()
try:
while worker.isAlive():
worker.join(1)
except KeyboardInterrupt:
print_info()
print_status("Waiting for already scheduled jobs to finish...")
data_producer.stop()
for worker in workers:
worker.join()
else:
data_producer.join_queue()
if self.vulnerabilities: if self.vulnerabilities:
print_info() print_info()
...@@ -48,26 +71,17 @@ class Exploit(exploits.Exploit): ...@@ -48,26 +71,17 @@ class Exploit(exploits.Exploit):
def check(self): def check(self):
raise NotImplementedError("Check method is not available") raise NotImplementedError("Check method is not available")
def target_function(self, running, data): def target_function(self, exploit):
name = threading.current_thread().name exploit = exploit()
print_status(name, 'process is starting...') exploit.target = self.target
exploit.port = self.port
while running.is_set():
try:
exploit = data.next()
except StopIteration:
break
else:
exploit = exploit()
exploit.target = self.target
exploit.port = self.port
response = exploit.check() response = exploit.check()
if response is True: if response is True:
print_success("{} {} is vulnerable".format(name, exploit)) print_success("{} is vulnerable".format(exploit))
self.vulnerabilities.append(exploit) self.vulnerabilities.append(exploit)
elif response is False: elif response is False:
print_error("{} {} is not vulnerable".format(name, exploit)) print_error("{} is not vulnerable".format(exploit))
else: else:
print_status("{} {} could not be verified".format(name, exploit)) print_status("{} could not be verified".format(exploit))
from __future__ import print_function from __future__ import print_function
from __future__ import absolute_import
import threading import threading
from weakref import WeakKeyDictionary from weakref import WeakKeyDictionary
...@@ -25,19 +24,26 @@ class DataProducerThread(threading.Thread): ...@@ -25,19 +24,26 @@ class DataProducerThread(threading.Thread):
for record in self.data: for record in self.data:
data_queue.put(record) data_queue.put(record)
def stop(self):
data_queue.queue.clear()
def join_queue(self):
data_queue.join()
class WorkerThread(threading.Thread): class WorkerThread(threading.Thread):
def __init__(self): def __init__(self, target, name):
super(WorkerThread, self).__init__() super(WorkerThread, self).__init__(target=target, name=name)
self.target = target
self.name = name
def run(self): def run(self):
while not data_queue.empty(): while not data_queue.empty():
record = data_queue.get() record = data_queue.get()
self.target(record) try:
data_queue.task_done() self.target(record)
finally:
def target(self, record): data_queue.task_done()
pass
class PrinterThread(threading.Thread): class PrinterThread(threading.Thread):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment