Closed fcaffieri closed 2 days 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)
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:
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
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
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
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
test-agent-windows-complete.yaml.txt
Moved the ETA to 26/04/2024 as Fede is OOO and we need to finish other tasks before reviewing this
Moved to in progress
Integration macOs and Windows were integrated in https://github.com/wazuh/wazuh-qa/pull/5310/
ETA was updated to 03/05/2024 considering all the fixes to be done.
Requested changes done
Requested fixes done.
LGTM, good job team!
Description
The objective of the issue is to make the necessary adaptations to perform agent tests on Windows systems.
Tasks
winrm
python library for the tests