Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending of commands to the M_API #187

Merged
merged 3 commits into from
Dec 18, 2024

Conversation

AlexRuiz7
Copy link
Member

@AlexRuiz7 AlexRuiz7 commented Dec 17, 2024

Description

This PR fixes the sending of command to the Wazuh Server due to SSL handshake exceptions caused by self-signed certificates.

Following examples from OpenSearch and Apache HttpClient, the HttpRestClient initialization has been modified to allow the loading and use of custom certificates to properly establish the connection with the Wazuh Server.

Once the connection could be established, it could be seen that the plugin was unable to log in to the Wazuh Server. This problem was also fixed, and the Imposter's configuration has been updated accordingly to better mock the Wazuh Server authentication and security layer. Now, imposter won't respond with a 200 response no matter what. Proper authentication headers must be provided, but for the / endpoint, which returns basic information about the Wazuh Server. The remainings endpoints are protected with a JWT, and the /security/user/authenticate endpoint needs basic authorization headers (user and password). Instructions to run imposter using SSL / TSL are also provided (to be done).

More and better logging has been included. Most verbose logs have been moved to the DEBUG level, while some existing log calls that were using non-string parameters have been fixed.

Lots of missing JavaDocs have been including, reducing the DocLint warnings from 100 to 54.

Dead code has been removed.

Although everything seems to work fine, it seems that the use of custom certificates makes OpenSearch raise some errors in the logs. See the file attached.

wazuh-cluster.log

Testing evidence

server

curl -X PUT https://127.0.0.1:55000/agents/restart -k -H "Authorization: Bearer $TOKEN"
{"data": {"affected_items": [], "total_affected_items": 1, "total_failed_items": 1, "failed_items": [{"error": {"code": 1762, "message": "Error sending command to the commands manager: OK"}, "id": ["d5b250c4-dfa1-4d94-827f-9f99210dbe6c"]}]}, "message": "Restart command was not sent to some agents", "error": 2}

indexer

[2024-12-17T19:37:49,963][INFO ][c.w.c.r.RestPostCommandAction] [node-1] Received POST /_plugins/_command_manager/commands request id [170] from host [127.0.0.1:9200]
[2024-12-17T19:37:49,967][INFO ][c.w.c.i.CommandIndex     ] [node-1] Adding command with id [NRYg1pMBC-j4GAK1bqiN] to the bulk request
[2024-12-17T19:37:49,967][INFO ][c.w.c.i.CommandIndex     ] [node-1] Index template index-template-commands already exists. Skipping creation.
[2024-12-17T19:38:15,507][WARN ][stderr                   ] [node-1] SLF4J(W): No SLF4J providers were found.
[2024-12-17T19:38:15,508][WARN ][stderr                   ] [node-1] SLF4J(W): Defaulting to no-operation (NOP) logger implementation
[2024-12-17T19:38:15,508][WARN ][stderr                   ] [node-1] SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
[2024-12-17T19:38:15,508][WARN ][stderr                   ] [node-1] SLF4J(W): Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
[2024-12-17T19:38:15,508][WARN ][stderr                   ] [node-1] SLF4J(W): Ignoring binding found at [jar:file:/usr/share/wazuh-indexer/plugins/wazuh-indexer-command-manager/log4j-slf4j-impl-2.23.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[2024-12-17T19:38:15,508][WARN ][stderr                   ] [node-1] SLF4J(W): See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.
[2024-12-17T19:38:15,536][INFO ][c.w.c.u.h.AuthHttpRestClient] [node-1] Attempting authentication at [https://127.0.0.1:55000/security/user/authenticate]
[2024-12-17T19:38:15,537][INFO ][c.w.c.u.h.HttpRestClient ] [node-1] Sending payload with id [null] to [https://127.0.0.1:55000/security/user/authenticate]
[2024-12-17T19:38:15,906][INFO ][c.w.c.u.h.AuthHttpRestClient] [node-1] Authentication successful
[2024-12-17T19:38:15,907][INFO ][c.w.c.u.h.HttpRestClient ] [node-1] Sending payload with id [null] to [https://127.0.0.1:55000/orders]
[2024-12-17T19:38:15,934][INFO ][c.w.c.j.SearchThread     ] [node-1] Server replied with 200. Updating orders' status.

server

Dec 17 19:37:49 rhel-server env[3296]: 2024/12/17 19:37:49 INFO: [Management API] wazuh 127.0.0.1 "PUT /agents/restart" with parameters {} and body {} done in 0.099s: 200
Dec 17 19:37:51 rhel-server env[2805]: 2024/12/17 19:37:51 INFO: [Master] [Local integrity] Starting.
Dec 17 19:37:51 rhel-server env[2805]: 2024/12/17 19:37:51 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.
Dec 17 19:37:59 rhel-server env[2805]: 2024/12/17 19:37:59 INFO: [Master] [Local integrity] Starting.
Dec 17 19:37:59 rhel-server env[2805]: 2024/12/17 19:37:59 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.
Dec 17 19:38:07 rhel-server env[2805]: 2024/12/17 19:38:07 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:07 rhel-server env[2805]: 2024/12/17 19:38:07 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.
Dec 17 19:38:15 rhel-server env[2805]: 2024/12/17 19:38:15 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:15 rhel-server env[2805]: 2024/12/17 19:38:15 INFO: [Master] [Local integrity] Finished in 0.001s. Calculated metadata of 2 files.
Dec 17 19:38:15 rhel-server env[3296]: 2024/12/17 19:38:15 INFO: [Management API] wazuh 127.0.0.1 "POST /security/user/authenticate" with parameters {} and body {} done in 0.258s: 200
Dec 17 19:38:15 rhel-server env[3296]: 2024/12/17 19:38:15 DEBUG: [Management API] Decoded token {'username': 'wazuh', 'roles': (1,), 'token_nbf_time': 1734464295, 'run_as': False, 'origin_node_type': 'master'}
Dec 17 19:38:15 rhel-server env[2805]: 2024/12/17 19:38:15 INFO: [Local 120361] [Main] Sending orders to the other nodes
Dec 17 19:38:15 rhel-server env[2805]: 2024/12/17 19:38:15 INFO: [Local 120361] [Main] Sending orders to the Communications API
Dec 17 19:38:15 rhel-server env[3296]: 2024/12/17 19:38:15 INFO: [Management API] wazuh 127.0.0.1 "POST /orders" with parameters {} and body {"orders": [{"action": {"args": [], "name": "restart", "version": "5.0.0"}, "source": "Users/Services", "document_id": "NRYg1pMBC-j4GAK1bqiN", "user": "Management API", "order_id": "NBYg1pMBC-j4GAK1bqiM", "request_id": "MxYg1pMBC-j4GAK1bqiM", "timeout": 100, "target": {"id": "d5b250c4-dfa1-4d94-827f-9f99210dbe6c", "type": "agent"}, "status": "pending"}]} done in 0.022s: 200
Dec 17 19:38:15 rhel-server env[2805]: 2024/12/17 19:38:15 ERROR: [Local 120361] [Main] Error 3041 - Server status check timed out after adding new servers: [Errno 2] No such file or directory
Dec 17 19:38:23 rhel-server env[2805]: 2024/12/17 19:38:23 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:23 rhel-server env[2805]: 2024/12/17 19:38:23 INFO: [Master] [Local integrity] Finished in 0.005s. Calculated metadata of 2 files.
Dec 17 19:38:31 rhel-server env[2805]: 2024/12/17 19:38:31 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:31 rhel-server env[2805]: 2024/12/17 19:38:31 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.
Dec 17 19:38:39 rhel-server env[2805]: 2024/12/17 19:38:39 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:39 rhel-server env[2805]: 2024/12/17 19:38:39 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.
Dec 17 19:38:47 rhel-server env[2805]: 2024/12/17 19:38:47 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:47 rhel-server env[2805]: 2024/12/17 19:38:47 INFO: [Master] [Local integrity] Finished in 0.004s. Calculated metadata of 2 files.
Dec 17 19:38:55 rhel-server env[2805]: 2024/12/17 19:38:55 INFO: [Master] [Local integrity] Starting.
Dec 17 19:38:55 rhel-server env[2805]: 2024/12/17 19:38:55 INFO: [Master] [Local integrity] Finished in 0.003s. Calculated metadata of 2 files.

agent

Dec 13 18:18:29 rhel-agent env[2418]: [2024-12-13 18:18:29.184] [wazuh-agent] [error] [ERROR] [http_client.cpp:405] [PerformHttpRequestInternal] Error: Error connecting to host: Connection refused
Dec 13 18:18:29 rhel-agent env[2418]: [2024-12-13 18:18:29.184] [wazuh-agent] [warning] [WARN] [http_client.cpp:291] [AuthenticateWithUuidAndKey] Error: 500.
Dec 13 18:18:29 rhel-agent env[2418]: [2024-12-13 18:18:29.184] [wazuh-agent] [warning] [WARN] [communicator.cpp:31] [SendAuthenticationRequest] Failed to authenticate with the manager. Retrying in 30 seconds.
Dec 13 18:18:31 rhel-agent env[2418]: [2024-12-13 18:18:31.461] [wazuh-agent] [warning] [WARN] [http_client.cpp:147] [Co_PerformHttpRequest] Failed to send http request to endpoint: /api/v1/commands. Retrying i>
Dec 13 18:18:59 rhel-agent env[2418]: [2024-12-13 18:18:59.185] [wazuh-agent] [error] [ERROR] [http_client.cpp:405] [PerformHttpRequestInternal] Error: Error connecting to host: Connection refused
Dec 13 18:18:59 rhel-agent env[2418]: [2024-12-13 18:18:59.185] [wazuh-agent] [warning] [WARN] [http_client.cpp:291] [AuthenticateWithUuidAndKey] Error: 500.
Dec 13 18:18:59 rhel-agent env[2418]: [2024-12-13 18:18:59.185] [wazuh-agent] [warning] [WARN] [communicator.cpp:31] [SendAuthenticationRequest] Failed to authenticate with the manager. Retrying in 30 seconds.
Dec 13 18:19:01 rhel-agent env[2418]: [2024-12-13 18:19:01.463] [wazuh-agent] [warning] [WARN] [http_client.cpp:147] [Co_PerformHttpRequest] Failed to send http request to endpoint: /api/v1/commands. Retrying i>
Dec 13 18:19:29 rhel-agent env[2418]: [2024-12-13 18:19:29.314] [wazuh-agent] [info] [INFO] [communicator.cpp:27] [SendAuthenticationRequest] Successfully authenticated with the manager.

Issues Resolved

Closes #154

@AlexRuiz7 AlexRuiz7 self-assigned this Dec 17, 2024
@AlexRuiz7 AlexRuiz7 requested a review from a team as a code owner December 17, 2024 18:51
Copy link
Member

@QU3B1M QU3B1M left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AlexRuiz7 AlexRuiz7 merged commit 380da37 into master Dec 18, 2024
1 check passed
@AlexRuiz7 AlexRuiz7 deleted the fix/154-send-commands-to-the-server branch December 18, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The CM plugin fails to send orders back to the Management API
2 participants