def remove_modules(): print_output("Removing modules..") config = get_config() modules_path = os.path.join(config["FACTION_PATH"], "modules/") command = "sudo su -c 'rm -rf {0}*'".format(modules_path) log.debug("Running: '{0}' from {1}".format(command, os.path.join(config["FACTION_PATH"]))) call(command, cwd=os.path.join(config["FACTION_PATH"]), shell=True)
def remove_builds(): print_output("Removing agent build files..") config = get_config() build_path = os.path.join(config["FACTION_PATH"], "agents/build") command = "sudo su -c 'rm -rf {0}*'".format(build_path) log.debug("Running: '{0}' from {1}".format(command, os.path.join(config["FACTION_PATH"]))) call(command, cwd=os.path.join(config["FACTION_PATH"]), shell=True)
def stop_faction(): print_output("Stopping Faction..") config = get_config() install_path = os.path.join(config["FACTION_PATH"], "install/") log.debug("Running: 'docker-compose -p faction stop' from {0}".format( install_path)) call("docker-compose -p faction stop", cwd=install_path, shell=True)
def get_config(): log.debug("Loading Config..") config = dict() if os.path.exists(config_file_path): with open(config_file_path) as f: config = json.load(f) return config
def get_container(container_name): log.debug("Searching for container named: {0}".format(container_name)) containers = client.containers.list() for container in containers: if container.attrs['Name'] == "/{0}".format(container_name): return container log.debug("Could not find container named: {0}".format(container_name)) return None
def remove_database_files(): print_output("Removing database files..") config = get_config() install_path = os.path.join(config["FACTION_PATH"], "install/") data_path = os.path.join(config["FACTION_PATH"], "data/") command = "sudo su -c 'rm -rf {0}*'".format(data_path) log.debug("Running: '{0}' from {1}".format(command, install_path)) call(command, cwd=install_path, shell=True)
def remove_container(container): log.debug("Stopping container: {0}".format(container.attrs["Name"])) if isinstance(container, Container): if container.status == 'running': container.stop() else: log.debug("Container {0} is not running. No need to stop it") else: error_out("{0} is not a container object".format(container))
def get_container_ip_address(container_name, network_name='faction_default'): log.debug("Getting IP for container named {0} on network {1}".format( container_name, network_name)) container = get_container(container_name) if container: return container.attrs["NetworkSettings"]["Networks"][network_name][ 'IPAddress'] else: return None
def get_user_id(username): faction_db = FactionDB() log.debug("Getting ID for User: {0}".format(username)) user = faction_db.session.query( faction_db.User).filter_by(Username=username.lower()).first() if user: log.debug("Got User ID: {0}".format(user.Id)) return user.Id else: error_out("Could not find user named: {0}".format(username))
def clean_faction(): print_output("Removing Faction Docker Containers and Images..") config = get_config() install_path = os.path.join(config["FACTION_PATH"], "install/") log.debug( "Running: 'docker-compose -p faction down --remove-orphans --rmi all' from {0}" .format(install_path)) call("docker-compose -p faction down --remove-orphans --rmi all", cwd=install_path, shell=True)
def get_role_id(name): faction_db = FactionDB() log.debug("Getting ID for role: {0}".format(name)) role = faction_db.session.query( faction_db.UserRole).filter_by(Name=name.lower()).first() if role: log.debug("Got UserRole ID: {0}".format(role.Id)) return role.Id else: error_out("Could not find role named: {0}".format(name))
def execute_container_command(container, command): log.debug("Executing {0} against container: {1}".format( command, container.attrs["Name"])) if isinstance(container, Container): if container.status == 'running': return container.exec_run(command) else: error_out( "Container {0} is not running. Can not execute commands against it" ) error_out("{0} is not a container object".format(container))
def remove_uploads(): print_output("Removing uploaded files..") config = get_config() paths = [] uploads_path = os.path.join(config["FACTION_PATH"], "uploads/") paths.append(os.path.join(uploads_path, "files/")) paths.append(os.path.join(uploads_path, "payloads/")) for path in paths: command = "sudo su -c 'rm -rf {0}*'".format(path) log.debug("Running: '{0}' from {1}".format(command, uploads_path)) call(command, cwd=uploads_path, shell=True)
def create_user(username, password, role_id): faction_db = FactionDB() print_output("Creating User: {0}".format(username)) log.debug("Password: {0}".format(password)) log.debug("User Role ID: {0}".format(role_id)) faction_db.session.add( faction_db.User(Username=username, Password=bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()), Created=datetime.utcnow(), RoleId=role_id, Enabled=True, Visible=True)) faction_db.session.commit()
def get_logs(container_name=None, follow=False): config = get_config() install_path = os.path.join(config["FACTION_PATH"], "install/") if follow == True: compose_command = 'docker-compose -p faction logs --follow {0}'.format( container_name) else: compose_command = 'docker-compose -p faction logs {0}'.format( container_name) log.debug(compose_command=" from {0}".format(install_path)) call("docker-compose -p faction logs", cwd=install_path, shell=True)
def build_faction(): print_output("Building Faction containers..") try: config = get_config() install_path = os.path.join(config["FACTION_PATH"], "install/") log.debug( "Running: 'docker-compose -p faction up -d --force-recreate --build' from {0}" .format(install_path)) ret = call("docker-compose -p faction up -d --force-recreate --build", cwd=install_path, shell=True) if ret == 0: print_output("Faction has been built") else: error_out("Failed to build Faction.") except Exception as e: error_out("Building Faction failed. Error: {0}".format(str(e)))
def get_passwords(): config = get_config() log.debug("Got Config, pulling passwords") admin_creds = ("Admin", config["ADMIN_USERNAME"], config["ADMIN_PASSWORD"]) log.debug("ADMIN: {}".format(admin_creds)) postgres_creds = ("PostgreSQL", config["POSTGRES_USERNAME"], config["POSTGRES_PASSWORD"]) log.debug("POSTGRES: {}".format(postgres_creds)) rabbit_creds = ("RabbitMQ", config["RABBIT_USERNAME"], config["RABBIT_PASSWORD"]) log.debug("RABBIT: {}".format(rabbit_creds)) return admin_creds, postgres_creds, rabbit_creds
def create_transport(name, transport_type, guid, api_key_id, configuration): faction_db = FactionDB() print_output("Creating Transport: {0}".format(name)) log.debug("Transport Type: {0}".format(transport_type)) log.debug("Guid: {0}".format(guid)) log.debug("API Key ID: {0}".format(api_key_id)) log.debug("Configuration: {0}".format(configuration)) faction_db.session.add( faction_db.Transport(Name=name, TransportType=transport_type, Guid=guid, ApiKeyId=api_key_id, Configuration=configuration, Enabled=True, Visible=True)) faction_db.session.commit()
def download_github_repo(repo_name, output_dir, access_token=None): print_output("Downloading module: {0}".format(repo_name)) log.debug("Access Token: {0}".format(access_token)) url = "https://api.github.com/repos/{0}/zipball".format(repo_name) temporary_zip = tempfile.NamedTemporaryFile(suffix=".zip") log.debug("Temporary File: {0}".format(temporary_zip.name)) if access_token: log.debug("Adding Github PAT Header") headers = {'Authorization': 'token {0}'.format(access_token)} log.debug("Downloading Zip") r = requests.get(url, headers=headers, allow_redirects=True) else: print_output("Downloading Zip") r = requests.get(url, allow_redirects=True) log.debug("Writing Zip") temporary_zip.write(r.content) log.debug("Opening Zip") zip_ref = zipfile.ZipFile(temporary_zip.name, 'r') log.debug("Extracting Zip") temporary_dir = tempfile.mkdtemp() log.debug("Creating Temporary Dir: {0}".format(temporary_dir)) zip_ref.extractall(temporary_dir) log.debug("Buildling source folder") repo_path = repo_name.replace('/', '-') source_path = os.path.join( temporary_dir, "{0}-{1}".format(repo_path, zip_ref.comment.decode())) log.debug("Checking for source path: {0}".format(source_path)) if not os.path.exists(source_path): source_path = temporary_dir log.debug("Source Path: {0}".format(source_path)) log.debug("Creating output directory: {0}".format(output_dir)) path = Path(output_dir) if path.exists(): log.debug("Cleaning out {0}".format(output_dir)) shutil.rmtree(output_dir, ignore_errors=True) path.mkdir(parents=True, exist_ok=True) module_files = os.listdir(source_path) print_output("Moving files to {0}".format(output_dir)) for module_file in module_files: module_file_path = os.path.join(source_path, module_file) log.debug("Moving {0}".format(module_file_path)) shutil.move(module_file_path, output_dir) zip_ref.close() temporary_zip.close() shutil.rmtree(temporary_dir, ignore_errors=True)