wazuh / wazuh-qa

Wazuh - Quality Assurance
GNU General Public License v2.0
60 stars 30 forks source link

DTT1 - Test Module - Implement Windows tests #5230

Closed fcaffieri closed 2 days ago

fcaffieri commented 2 weeks ago

Description

The objective of the issue is to make the necessary adaptations to perform agent tests on Windows systems.

Tasks

fcaffieri commented 2 weeks ago

https://github.com/diyan/pywinrm

Connection to Windows

 telnet ec2-54-84-55-248.compute-1.amazonaws.com 3389
Trying 54.84.55.248...
Connected to ec2-54-84-55-248.compute-1.amazonaws.com.
Escape character is '^]'.
from winrm.protocol import Protocol

# Definir los detalles de conexión
host = '54.84.55.248'
port = 3389  # El puerto predeterminado para WinRM
username = 'Administrator'
password = 'vNP7gjyvMI8'

if port == 5986:
    protocol = 'https'
else:
    protocol = 'http'

endpoint_url = f'{protocol}://{host}:{port}'

# Crear la instancia del protocolo WinRM
p = Protocol(
    endpoint=endpoint_url,
    transport='ntlm',
    username=username,
    password=password, server_cert_validation='ignore'
)

shell_id = ""
try:
    # Conectar al servidor Windows
    shell_id = p.open_shell()
    command_id = p.run_command('ipconfig')

    # Leer la salida del comando
    stdout, stderr, status_code = p.get_command_output(command_id)

    if status_code == 0:
        print("Comando ejecutado exitosamente:")
        print(stdout.decode())
    else:
        print("Hubo un error al ejecutar el comando:")
        print(stderr.decode())
except Exception as e:
    print("Error al conectar o ejecutar el comando:", str(e))
finally:
    # Cerrar la conexión
    p.close_shell(shell_id)

Error:

    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='ec2-54-84-55-248.compute-1.amazonaws.com', port=3389): Read timed out. (read timeout=30)

Implementation

from winrm.protocol import Protocol
import winrm

# Definir los detalles de conexión
host = '54.84.55.248'
port = 3389  # El puerto predeterminado para WinRM
username = 'Administrator'
password = 'vNP7gjyvMI8'

if port == 5986:
    protocol = 'https'
else:
    protocol = 'http'

endpoint_url = f'{protocol}://{host}:{port}'

try:
    session = winrm.Session(endpoint_url, auth=(username, password),transport='ntlm', server_cert_validation='ignore')
    cmd = session.run_cmd('ipconfig')
    if cmd.status_code == 0:
        print("WinRM connection successful.")
        stdout, stderr, status_code = winrm.get_command_output(cmd)
        print(stdout)
    else:
        print(f'WinRM connection failed. Check the credentials in the inventory file.')
except Exception as e:
    print(f'Error on attempt {e}')

Error:

$ python3 /home/fcaffieri/wazuh-env/wazuh-qa/deployability/modules/testing/tests/test_agent/test.py
Error on attempt HTTPConnectionPool(host='ec2-54-84-55-248.compute-1.amazonaws.com', port=3389): Read timed out. (read timeout=30)
fcaffieri commented 2 weeks ago

Update

The connection via winrm to Windows has been resolved. Implementation.

@staticmethod
     def execute_windows_command(inventory_path, command) -> str:

         with open(inventory_path, 'r') as yaml_file:
             inventory_data = yaml.safe_load(yaml_file)

         windows_host = inventory_data.get('ansible_host')
         windows_port = inventory_data.get('ansible_port')
         windows_password = inventory_data.get('ansible_password')
         windows_username = inventory_data.get('ansible_user')

         if windows_port == 5986:
             windows_protocol = 'https'
         else:
             windows_protocol = 'http'

         endpoint_url = f'{windows_protocol}://{windows_host}:{windows_port}'

         try:
             session = winrm.Session(endpoint_url, auth=(windows_username, windows_password),transport='ntlm', server_cert_validation='ignore')
             ret = session.run_cmd(command)
             if ret.status_code == 0:
                 return ret.std_out
             else:
                 return Exception(f'Error executing command: {command}')
         except Exception as e:
             raiseException(f'Error executing command: {command}')

Tasks:


A bug was found in the installation of the agents in the tests when the repository was live:

image

This URL is made:

https://packages.wazuh.com/4.7/yum/wazuh-agent-4.7.1-1.x86_64.rpm

Should be:

https://packages.wazuh.com/4.x/yum/wazuh-agent-4.7.1-1.x86_64.rpm
fcaffieri commented 2 weeks ago

Update

All tests, helpers and everything necessary to perform tests on Windows agents were added. All that remains is to validate with @pro-akim a method of helpers/generic.py which I need to validate the implementation. After that change, the tests begin.

Changes done in:

https://github.com/wazuh/wazuh-qa/commit/40df79bed587da139332f43dd75d800d815bd907 https://github.com/wazuh/wazuh-qa/commit/6ac8f3e15a2e06e9d52439c34e7f1e654e0c18fc https://github.com/wazuh/wazuh-qa/commit/0d377069b5a9c62737471ef47faeea5aeb8bc35f

fcaffieri commented 1 week ago

Update

The implementation was added to perform checkfile tests both in the installation and uninstall of the agent. Performing comprehensive testing due to a large number of changes

fcaffieri commented 1 week ago

Test Windows :green_circle:

Test Windows2022 :green_circle:

test-agent-windows-complete.yaml.txt

windows2022.log.txt

Test Windows2019 :green_circle:

test-agent-windows-complete.yaml.txt

windows2019.log.txt

Test Windows2016 :green_circle:

test-agent-windows-complete.yaml.txt

windows2016.log.txt

Test Windows 10 :green_circle:

test-agent-windows-complete.yaml.txt

windows10.log.txt

Test Windows2012 :green_circle:

test-agent-windows-complete.yaml.txt

windows2012.log.txt

Test With linux and Windows agents :green_circle:

Test five Windows and five Linux:green_circle:

test-agent-windows-complete.yaml.txt

complete_test.log.txt

Test five Windows and five Linux with check-files :green_circle:

test-agent-windows-complete.yaml.txt

complete_test_checkfiles.log.txt

rauldpm commented 1 week ago

Moved the ETA to 26/04/2024 as Fede is OOO and we need to finish other tasks before reviewing this

pro-akim commented 1 week ago

Update

Moved to in progress

pro-akim commented 5 days ago

Update

Integration macOs and Windows were integrated in https://github.com/wazuh/wazuh-qa/pull/5310/

pro-akim commented 3 days ago

Update

ETA was updated to 03/05/2024 considering all the fixes to be done.

Requested changes done

pro-akim commented 2 days ago

Update

Requested fixes done.

rauldpm commented 2 days ago

LGTM, good job team!