Ejemplo n.º 1
0
def process(conf):
    """
    """

    remote_addr = conf['host']['addr']
    remote_port = conf['host']['port']
    username = conf['host']['username']
    password = conf['host']['password']
    lime_module = conf['host']['module']
    filename = conf['host']['filename']
    key = conf['host']['key']
    bucket = conf['aws']['bucket']
    progressbar = conf['host']['progressbar']
    tunnel_addr = '127.0.0.1'
    tunnel_port = random.randint(10000, 30000)
    remote_module_path = '/tmp/lime.ko'

    repository_enabled = conf['repository']['enabled']
    repository_url = conf['repository']['url']

    queue_handler = QueueHandler(log_queue)
    logger = logging.getLogger('margaritashotgun')
    logger.addHandler(queue_handler)

    if bucket is not None:
        dest = OutputDestinations.s3
    else:
        dest = OutputDestinations.local

    if filename is None:
        tm = int(time.time())
        dt = datetime.utcfromtimestamp(tm).isoformat()
        filename = "{0}-{1}-mem.lime".format(remote_addr, dt)

    try:
        host = Host()
        host.connect(username, password, key, remote_addr, remote_port)
        host.start_tunnel(tunnel_port, tunnel_addr, tunnel_port)
        if lime_module is None:
            kernel_version = host.kernel_version()
            if repository_enabled:
                repo = Repository(repository_url)
                match = repo.search_modules(kernel_version)
                if match is not None:
                    lime_module = repo.fetch_module(match)
                    host.upload_module(lime_module)
                else:
                    raise KernelModuleNotFoundError(kernel_version, repo.url)
            else:
                # TODO: prompt user to search repository when running interactively
                raise KernelModuleNotProvidedError(kernel_version)
        else:
            host.upload_module(lime_module, remote_module_path)

        host.load_lime(remote_module_path, tunnel_port)
        lime_loaded = host.wait_for_lime(tunnel_port)

        if lime_loaded:
            result = host.capture_memory(dest, filename, bucket, progressbar)
        else:
            result = False

        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()

        return (remote_addr, result)
    except KeyboardInterrupt:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)
    except Exception as ex:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        logger.critical(ex)
        return (remote_addr, False)
Ejemplo n.º 2
0
def process(conf):
    """
    """
    jump_host = conf['host']['jump_host']
    remote_addr = conf['host']['addr']
    remote_port = conf['host']['port']
    username = conf['host']['username']
    password = conf['host']['password']
    lime_module = conf['host']['module']
    filename = conf['host']['filename']
    key = conf['host']['key']
    bucket = conf['aws']['bucket']
    azure_blob_config = conf['azure_blob']
    progressbar = conf['host']['progressbar']
    tunnel_addr = '127.0.0.1'
    tunnel_port = random.randint(10000, 30000)
    remote_module_path = '/tmp/lime.ko'

    repository_enabled = conf['repository']['enabled']
    repository_url = conf['repository']['url']
    repository_manifest = conf['repository']['manifest']
    repository_gpg_verify = conf['repository']['gpg_verify']

    queue_handler = QueueHandler(log_queue)
    logger = logging.getLogger('margaritashotgun')
    logger.addHandler(queue_handler)

    if bucket is not None:
        dest = OutputDestinations.s3
    elif any([ v is not None for v in azure_blob_config.values() ]):
        dest = OutputDestinations.azure_blob
    else:
        dest = OutputDestinations.local

    if filename is None:
        tm = int(time.time())
        dt = datetime.utcfromtimestamp(tm).isoformat()
        filename = "{0}-{1}-mem.lime".format(remote_addr, dt)

    try:
        host = Host()
        host.connect(username, password, key, remote_addr, remote_port,
                     jump_host)
        host.start_tunnel(tunnel_port, tunnel_addr, tunnel_port)
        if lime_module is None:
            kernel_version = host.kernel_version()
            if repository_enabled:
                repo = Repository(repository_url, repository_gpg_verify)
                repo.init_gpg()
                lime_module = repo.fetch(kernel_version, repository_manifest)
                host.upload_module(lime_module)
            else:
                raise KernelModuleNotProvidedError(kernel_version)
        else:
            host.upload_module(lime_module, remote_module_path)

        host.load_lime(remote_module_path, tunnel_port)
        lime_loaded = host.wait_for_lime(tunnel_port)

        if lime_loaded:
            result = host.capture_memory(dest, filename, bucket, azure_blob_config, progressbar)
        else:
            logger.debug("lime failed to load on {0}".format(remote_addr))
            result = False

        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()

        return (remote_addr, result)
    except SSHConnectionError as ex:
        logger.error(ex)
        logger.removeHandler(queue_handler)
        queue_handler.close()
        return (remote_addr, False)
    except KeyboardInterrupt as ex:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)
    except (SSHCommandError, Exception) as ex:
        logger.error(ex)
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)
Ejemplo n.º 3
0
def process(conf):
    """
    """

    remote_addr = conf['host']['addr']
    remote_port = conf['host']['port']
    username = conf['host']['username']
    password = conf['host']['password']
    lime_module = conf['host']['module']
    filename = conf['host']['filename']
    key = conf['host']['key']
    bucket = conf['aws']['bucket']
    progressbar = conf['host']['progressbar']
    tunnel_addr = '127.0.0.1'
    tunnel_port = random.randint(10000, 30000)
    remote_module_path = '/tmp/lime.ko'

    repository_enabled = conf['repository']['enabled']
    repository_url = conf['repository']['url']

    queue_handler = QueueHandler(log_queue)
    logger = logging.getLogger('margaritashotgun')
    logger.addHandler(queue_handler)

    if bucket is not None:
        dest = OutputDestinations.s3
    else:
        dest = OutputDestinations.local

    if filename is None:
        filename = "{0}-mem.lime".format(remote_addr)

    try:
        host = Host()
        host.connect(username, password, key, remote_addr, remote_port)
        host.start_tunnel(tunnel_port, tunnel_addr, tunnel_port)
        if lime_module is None:
            kernel_version = host.kernel_version()
            if repository_enabled:
                repo = Repository(repository_url)
                match = repo.search_modules(kernel_version)
                if match is not None:
                    lime_module = repo.fetch_module(match)
                    host.upload_module(lime_module)
                else:
                    raise KernelModuleNotFoundError(kernel_version, repo.url)
            else:
                # TODO: prompt user to search repository when running interactively
                raise KernelModuleNotProvidedError(kernel_version)
        else:
            host.upload_module(lime_module, remote_module_path)

        host.load_lime(remote_module_path, tunnel_port)
        lime_loaded = host.wait_for_lime(tunnel_port)

        if lime_loaded:
            result = host.capture_memory(dest, filename, bucket, progressbar)
        else:
            result = False

        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()

        return (remote_addr, result)
    except KeyboardInterrupt:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)
    except Exception as ex:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        logger.critical(ex)
        return (remote_addr, False)
Ejemplo n.º 4
0
def installer(
    qt_archive: QtPackage,
    base_dir: str,
    command: Optional[str],
    queue: multiprocessing.Queue,
    keep: bool = False,
    response_timeout: Optional[int] = None,
):
    """
    Installer function to download archive files and extract it.
    It is called through multiprocessing.Pool()
    """
    name = qt_archive.name
    url = qt_archive.archive_url
    hashurl = qt_archive.hashurl
    archive = qt_archive.archive
    start_time = time.perf_counter()
    # set defaults
    Settings.load_settings()
    # set logging
    setup_logging()  # XXX: why need to load again?
    qh = QueueHandler(queue)
    logger = getLogger()
    for handler in logger.handlers:
        handler.close()
        logger.removeHandler(handler)
    logger.addHandler(qh)
    #
    logger.debug("Download URL: {}".format(url))
    if response_timeout is None:
        timeout = (Settings.connection_timeout, Settings.response_timeout)
    else:
        timeout = (Settings.connection_timeout, response_timeout)
    hash = binascii.unhexlify(getUrl(hashurl, timeout))
    retry_on_errors(
        action=lambda: downloadBinaryFile(url, archive, "sha1", hash, timeout),
        acceptable_errors=(ArchiveChecksumError, ),
        num_retries=Settings.max_retries_on_checksum_error,
        name=f"Downloading {name}",
    )
    if command is None:
        with py7zr.SevenZipFile(archive, "r") as szf:
            szf.extractall(path=base_dir)
    else:
        if base_dir is not None:
            command_args = [
                command,
                "x",
                "-aoa",
                "-bd",
                "-y",
                "-o{}".format(base_dir),
                archive,
            ]
        else:
            command_args = [command, "x", "-aoa", "-bd", "-y", archive]
        try:
            proc = subprocess.run(command_args,
                                  stdout=subprocess.PIPE,
                                  check=True)
            logger.debug(proc.stdout)
        except subprocess.CalledProcessError as cpe:
            msg = "\n".join(
                filter(None, [
                    f"Extraction error: {cpe.returncode}", cpe.stdout,
                    cpe.stderr
                ]))
            raise ArchiveExtractionError(msg) from cpe
    if not keep:
        os.unlink(archive)
    logger.info("Finished installation of {} in {:.8f}".format(
        archive,
        time.perf_counter() - start_time))
    qh.flush()
    qh.close()
    logger.removeHandler(qh)
Ejemplo n.º 5
0
def process(conf):
    """
    """
    jump_host = conf['host']['jump_host']
    remote_addr = conf['host']['addr']
    remote_port = conf['host']['port']
    username = conf['host']['username']
    password = conf['host']['password']
    lime_module = conf['host']['module']
    filename = conf['host']['filename']
    key = conf['host']['key']
    bucket = conf['aws']['bucket']
    progressbar = conf['host']['progressbar']
    tunnel_addr = '127.0.0.1'
    tunnel_port = random.randint(10000, 30000)
    remote_module_path = '/tmp/lime.ko'

    repository_enabled = conf['repository']['enabled']
    repository_url = conf['repository']['url']
    repository_manifest = conf['repository']['manifest']
    repository_gpg_verify = conf['repository']['gpg_verify']

    queue_handler = QueueHandler(log_queue)
    logger = logging.getLogger('margaritashotgun')
    logger.addHandler(queue_handler)

    if bucket is not None:
        dest = OutputDestinations.s3
    else:
        dest = OutputDestinations.local

    if filename is None:
        tm = int(time.time())
        dt = datetime.utcfromtimestamp(tm).isoformat()
        filename = "{0}-{1}-mem.lime".format(remote_addr, dt)

    try:
        host = Host()
        host.connect(username, password, key, remote_addr, remote_port,
                     jump_host)
        host.start_tunnel(tunnel_port, tunnel_addr, tunnel_port)
        if lime_module is None:
            kernel_version = host.kernel_version()
            if repository_enabled:
                repo = Repository(repository_url, repository_gpg_verify)
                repo.init_gpg()
                lime_module = repo.fetch(kernel_version, repository_manifest)
                host.upload_module(lime_module)
            else:
                raise KernelModuleNotProvidedError(kernel_version)
        else:
            host.upload_module(lime_module, remote_module_path)

        host.load_lime(remote_module_path, tunnel_port)
        lime_loaded = host.wait_for_lime(tunnel_port)

        if lime_loaded:
            result = host.capture_memory(dest, filename, bucket, progressbar)
        else:
            logger.debug("lime failed to load on {0}".format(remote_addr))
            result = False

        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()

        return (remote_addr, result)
    except SSHConnectionError as ex:
        logger.error(ex)
        logger.removeHandler(queue_handler)
        queue_handler.close()
        return (remote_addr, False)
    except KeyboardInterrupt as ex:
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)
    except (SSHCommandError, Exception) as ex:
        logger.error(ex)
        logger.removeHandler(queue_handler)
        queue_handler.close()
        host.cleanup()
        return (remote_addr, False)