Exemplo n.º 1
0
def test_create_message(expected_exception, command, arguments, custom):
    """Checks message returned is correct

    Checks if message returned by create_message(...) contains the command, arguments and '!' symbol
    when it is needed.

    Parameters
    ----------
    expected_exception : str
        Exception code expected when calling create_message.
    command : str
        Command to be introduced in the message.
    arguments : list
        Arguments for the command/script.
    custom : boolean
        True if command is a script.
    """
    if expected_exception:
        with pytest.raises(WazuhError, match=f'.* {expected_exception} .*'):
            active_response.create_message(command=command,
                                           arguments=arguments,
                                           custom=custom)
    else:
        ret = active_response.create_message(command=command,
                                             arguments=arguments,
                                             custom=custom)
        assert command in ret, f'Command not being returned'
        if arguments:
            assert (arg in ret
                    for arg in arguments), f'Arguments not being added'
        if custom:
            assert '!' in ret, f'! symbol not being added when custom command'
Exemplo n.º 2
0
def run_command(agent_list=None, command=None, arguments=None, custom=False):
    """Run AR command in a specific agent

    :param agent_list: Run AR command in the agent.
    :param command: Command running in the agent. If this value starts by !, then it refers to a script name instead of
    a command name
    :param custom: Whether the specified command is a custom command or not
    :param arguments: Command arguments
    :return: AffectedItemsWazuhResult.
    """
    msg_queue = active_response.create_message(command=command,
                                               arguments=arguments,
                                               custom=custom)
    oq = OssecQueue(common.ARQUEUE)
    result = AffectedItemsWazuhResult(
        none_msg='Could not send command to any agent',
        some_msg='Could not send command to some agents',
        all_msg='Command sent to all agents')
    for agent_id in agent_list:
        try:
            active_response.send_command(msg_queue, oq, agent_id)
            result.affected_items.append(agent_id)
            result.total_affected_items += 1
        except WazuhException as e:
            result.add_failed_item(id_=agent_id, error=e)
    oq.close()

    return result