コード例 #1
0
def run_playbook(uplink: UplinkConnection, playbook: str, target: str,
                 args: str):
    print(f'###  Manually executing playbook: {playbook} against {target}')
    manual_target = {
        'target_host': target,
        'details': {
            'run_method': 'manual'
        }
    }
    for i in range(0, len(args)):
        manual_target[f'arg{i}'] = args[i]
    try:
        playbook_module = importlib.import_module(
            f'{const.PACKAGE_NAME}.playbooks.{playbook}')
        results = playbook_module.run(manual_target)
        print(results)
    except ModuleNotFoundError as mnfe:
        print(f'!!!  Missing referenced Playbook: {mnfe}')
    except AttributeError as ae:
        print(f'!!!  Malformed Playbook, missing required attribute: {ae}')
    except TypeError as te:
        print(
            f'!!!  Malformed Playbook, the run method must take in the target as a dict: {te}'
        )
    except KeyboardInterrupt:
        print("!!!  Command cancelled by key interrupt")
    uplink.send_data(const.DEFAULT_TARGET_COLLECTION, manual_target)
コード例 #2
0
def main(raw_command: str):
    print('============ EXECUTING COMMAND ============', file=sys.stderr)
    system_command = SystemCommand(raw_command,
                                   additional_meta={"run-mode": "manual"})
    # For each yielded value, print it or use it as a control message
    for output_value in system_command.run():
        if isinstance(output_value, str):
            print(output_value)
        else:  # Is bool = end of command and is the result
            if not output_value:
                print("!!!  Command didn't finish executing", file=sys.stderr)
                exit(1)
            if system_command.command_return_code != 0:
                print(
                    f"!#!  Command returned a non-0 return code ({system_command.command_return_code})"
                )

            # Else it was sucessful and we can just continue
    print('========== PARSING COMMAND OUTPUT =========', file=sys.stderr)
    parser_manager = CommandParserManager()
    command_json = system_command.to_json()
    metadata, targets = parser_manager.parse(
        system_command)  # Conditionally parse command

    print('============ RUNNING PLAYBOOKS ============', file=sys.stderr)
    playbook_manager = PlaybookManager()
    playbook_manager.automate(targets)  # Conditionally run Playbooks

    print('========= ESTABLISHING RADAR UPLINK =======', file=sys.stderr)
    uplink = UplinkConnection()

    print('=============== SYNCING DATA ==============', file=sys.stderr)
    print("> command data... ", end='', file=sys.stderr)
    uplink.send_data(const.DEFAULT_COMMAND_COLLECTION, command_json)
    print("done", file=sys.stderr)
    print("> metadata... ", end='', file=sys.stderr)
    uplink.send_data(const.DEFAULT_METADATA_COLLECTION, metadata)
    print("done", file=sys.stderr)
    print("> target data... ", end='', file=sys.stderr)
    if len(targets) != 0:
        uplink.send_data(const.DEFAULT_TARGET_COLLECTION, targets)
        print("done", file=sys.stderr)
    else:
        print("n/a", file=sys.stderr)

    print('============<({[ COMPLETED ]})>============', file=sys.stderr)