Exemplo n.º 1
0
def printProgress():
    msg = '%s success | %s remaining | %s scanned in %.2f seconds' % (
        out.count(), KB["task_queue"].qsize(), KB["finished"],
        time.time() - KB['start_time'])

    _ = '\r' + ' ' * (KB['console_width'][0] - len(msg)) + msg
    Share.dataToStdout(_)
Exemplo n.º 2
0
 def log(self, msg):
     # Share.dataToStdout(value + '\n')
     width = KB["console_width"][0]
     while len(msg) > width:
         _ = msg[:width]
         Share.dataToStdout('\r' + _ + '\n\r')
         msg = msg[width:]
     Share.dataToStdout('\r' + msg + '\n\r')
     printProgress()
Exemplo n.º 3
0
def run_threads(num_threads, thread_function, args: tuple = ()):
    threads = []
    KB["continue"] = True
    KB["console_width"] = getTerminalSize()
    KB['start_time'] = time.time()
    KB['finished'] = 0
    KB["lock"] = threading.Lock()
    KB["result"] = 0
    KB["running"] = 0

    try:
        info_msg = "Staring {0} threads".format(num_threads)
        logger.info(info_msg)

        # Start the threads
        for num_threads in range(num_threads):
            thread = threading.Thread(target=exception_handled_function,
                                      name=str(num_threads),
                                      args=(thread_function, args))
            thread.setDaemon(True)
            try:
                thread.start()
            except Exception as ex:
                err_msg = "error occurred while starting new thread ('{0}')".format(
                    str(ex))
                logger.critical(err_msg)
                break

            threads.append(thread)

        # And wait for them to all finish
        alive = True
        while alive:
            alive = False
            for thread in threads:
                if thread.isAlive():
                    alive = True
                    time.sleep(0.1)

    except KeyboardInterrupt as ex:
        KB['continue'] = False
        if num_threads > 1:
            logger.info("waiting for threads to finish{0}".format(
                " (Ctrl+C was pressed)" if isinstance(ex, KeyboardInterrupt
                                                      ) else ""))
        try:
            while threading.activeCount() > 1:
                pass
        except KeyboardInterrupt:
            raise

    except Exception as ex:
        logger.error("thread {0}: {1}".format(
            threading.currentThread().getName(), str(ex)))
        traceback.print_exc()
    finally:
        Share.dataToStdout('\n')
Exemplo n.º 4
0
    def execute(self, request: Request, response: Response):
        self.target = ''
        self.requests = request
        self.response = response
        output = None
        try:
            output = self.audit()
        except NotImplementedError:
            msg = 'Plugin: {0} not defined "{1} mode'.format(self.name, 'audit')
            Share.dataToStdout(Share.dataToStdout('\r' + msg + '\n\r'))

        except (ConnectTimeout, requests.exceptions.ReadTimeout, urllib3.exceptions.ReadTimeoutError, socket.timeout):
            retry = RETRY
            while retry > 0:
                msg = 'Plugin: {0} timeout, start it over.'.format(self.name)
                # Share.dataToStdout('\r' + msg + '\n\r')
                try:
                    output = self.audit()
                    break
                except (
                        ConnectTimeout, requests.exceptions.ReadTimeout, urllib3.exceptions.ReadTimeoutError,
                        socket.timeout):
                    # msg = 'Plugin: {0} time-out retry failed!'.format(self.name)
                    # Share.dataToStdout('\r' + msg + '\n\r')
                    retry -= 1
            else:
                msg = "connect target '{0}' failed!".format(self.target)
                # Share.dataToStdout('\r' + msg + '\n\r')

        except HTTPError as e:
            msg = 'Plugin: {0} HTTPError occurs, start it over.'.format(self.name)
            # Share.dataToStdout('\r' + msg + '\n\r')

        except ConnectionError:
            msg = "connect target '{0}' failed!".format(self.target)
            # Share.dataToStdout('\r' + msg + '\n\r')
        except TooManyRedirects as e:
            # Share.dataToStdout('\r' + str(e) + '\n\r')
            pass

        except RemoteDisconnected as e:
            pass

        except NewConnectionError as ex:
            pass

        except PoolError as ex:
            pass

        except Exception as e:
            if DEBUG:
                Share.dataToStdout('\r' + "[x]{} report:".format(self.name) + str(e) + '\n\r')
                traceback.print_exc()

        return output
Exemplo n.º 5
0
 def log(self, msg):
     width = KB["console_width"][0]
     outputs = []
     msgs = msg.split('\n')
     for i in msgs:
         line = i
         while len(line) >= width:
             _ = line[:width]
             outputs.append(_)
             # Share.dataToStdout('\r' + _ + ' ' * (width - len(msg)) + '\n\r')
             line = line[width:]
         outputs.append(line)
     for i in outputs:
         Share.dataToStdout('\r' + i + ' ' * (width - len(i)) + '\n\r')
Exemplo n.º 6
0
 def log(self, msg):
     # Share.dataToStdout(value + '\n')
     Share.dataToStdout('\r' + msg + '\n\r')
     printProgress()
Exemplo n.º 7
0
    def execute(self, request: Request, response: Response):
        self.target = ''
        self.requests = request
        self.response = response
        output = None
        try:
            output = self.audit()
        except NotImplementedError:
            msg = 'Plugin: {0} not defined "{1} mode'.format(
                self.name, 'audit')
            Share.dataToStdout(Share.dataToStdout('\r' + msg + '\n\r'))

        except ConnectTimeout:
            retry = RETRY
            while retry > 0:
                msg = 'Plugin: {0} timeout, start it over.'.format(self.name)
                Share.dataToStdout('\r' + msg + '\n\r')
                try:
                    output = self.audit()
                    break
                except ConnectTimeout:
                    msg = 'POC: {0} time-out retry failed!'.format(self.name)
                    Share.dataToStdout('\r' + msg + '\n\r')
                retry -= 1
            else:
                msg = "connect target '{0}' failed!".format(self.target)
                Share.dataToStdout('\r' + msg + '\n\r')

        except HTTPError as e:
            msg = 'Plugin: {0} HTTPError occurs, start it over.'.format(
                self.name)
            Share.dataToStdout('\r' + msg + '\n\r')

        except ConnectionError as e:
            msg = "connect target '{0}' failed!".format(self.target)
            Share.dataToStdout('\r' + msg + '\n\r')

        except TooManyRedirects as e:
            if e:
                Share.dataToStdout('\r' + str(e) + '\n\r')

        except Exception as e:
            if e:
                Share.dataToStdout('\r' + str(e) + '\n\r')

        return output