|
| 1 | +from weakref import WeakKeyDictionary |
| 2 | +from itertools import chain |
| 3 | +import threading |
| 4 | +import time |
| 5 | + |
| 6 | +from routersploit.utils import print_info |
| 7 | + |
| 8 | + |
| 9 | +class Option(object): |
| 10 | + """ Exploit attribute that is set by the end user. """ |
| 11 | + |
| 12 | + def __init__(self, default, description=""): |
| 13 | + self.default = default |
| 14 | + self.description = description |
| 15 | + self.data = WeakKeyDictionary() |
| 16 | + |
| 17 | + def __get__(self, instance, owner): |
| 18 | + return self.data.get(instance, self.default) |
| 19 | + |
| 20 | + def __set__(self, instance, value): |
| 21 | + self.data[instance] = value |
| 22 | + |
| 23 | + |
| 24 | +class ExploitOptionsAggregator(type): |
| 25 | + """ Metaclass for exploit base class. |
| 26 | +
|
| 27 | + Metaclass is aggregating all possible Attributes that user can set |
| 28 | + for tab completion purposes. |
| 29 | + """ |
| 30 | + def __new__(cls, name, bases, attrs): |
| 31 | + try: |
| 32 | + base_exploit_attributes = chain(map(lambda x: x.exploit_attributes, bases)) |
| 33 | + except AttributeError: |
| 34 | + attrs['exploit_attributes'] = {} |
| 35 | + else: |
| 36 | + attrs['exploit_attributes'] = {k: v for d in base_exploit_attributes for k, v in d.iteritems()} |
| 37 | + |
| 38 | + for key, value in attrs.iteritems(): |
| 39 | + if isinstance(value, Option): |
| 40 | + attrs['exploit_attributes'].update({key: value.description}) |
| 41 | + elif key == "__info__": |
| 42 | + attrs["_{}{}".format(name, key)] = value |
| 43 | + del attrs[key] |
| 44 | + elif key in attrs['exploit_attributes']: # Removing exploit_attribute that was overwritten |
| 45 | + del attrs['exploit_attributes'][key] # in the child and is not a Option() instance. |
| 46 | + return super(ExploitOptionsAggregator, cls).__new__(cls, name, bases, attrs) |
| 47 | + |
| 48 | + |
| 49 | +class Exploit(object): |
| 50 | + """ Base class for exploits. """ |
| 51 | + |
| 52 | + __metaclass__ = ExploitOptionsAggregator |
| 53 | + target = Option(default="", description="Target IP address.") |
| 54 | + # port = Option(default="", description="Target port.") |
| 55 | + |
| 56 | + @property |
| 57 | + def options(self): |
| 58 | + """ Returns list of options that user can set. |
| 59 | +
|
| 60 | + Returns list of options aggregated by |
| 61 | + ExploitOptionsAggregator metaclass that user can set. |
| 62 | +
|
| 63 | + :return: list of options that user can set |
| 64 | + """ |
| 65 | + return self.exploit_attributes.keys() |
| 66 | + |
| 67 | + def run(self): |
| 68 | + raise NotImplementedError("You have to define your own 'run' method.") |
| 69 | + |
| 70 | + def check(self): |
| 71 | + raise NotImplementedError("You have to define your own 'check' method.") |
| 72 | + |
| 73 | + def run_threads(self, threads, target, *args, **kwargs): |
| 74 | + workers = [] |
| 75 | + threads_running = threading.Event() |
| 76 | + threads_running.set() |
| 77 | + for worker_id in xrange(int(threads)): |
| 78 | + worker = threading.Thread( |
| 79 | + target=target, |
| 80 | + args=chain((threads_running,), args), |
| 81 | + kwargs=kwargs, |
| 82 | + name='worker-{}'.format(worker_id), |
| 83 | + ) |
| 84 | + workers.append(worker) |
| 85 | + worker.start() |
| 86 | + |
| 87 | + start = time.time() |
| 88 | + try: |
| 89 | + while worker.isAlive(): |
| 90 | + worker.join(1) |
| 91 | + except KeyboardInterrupt: |
| 92 | + threads_running.clear() |
| 93 | + |
| 94 | + for worker in workers: |
| 95 | + worker.join() |
| 96 | + print_info('Elapsed time: ', time.time() - start, 'seconds') |
0 commit comments