PL EN
WB-C34158FBTLP:CLEARWazuh 4

Wazuh 4 training, part 2: agent enrollment

Wazuh 4 training part 2: groups before enrollment, MSI deployment variables, agent-auth, the enrollment block, agent status checks and enrollment errors.

The second part of the Wazuh 4 training joins three agents to the manager from part 1 using three different methods: an MSI package with deployment variables on Windows, the agent-auth tool, and the <enrollment> block in ossec.conf on Linux. Each method uses the enrollment password set in part 1, and the options and defaults come from the Wazuh 4.14 documentation.

Exercise 1: groups and ports before the first agent

I create the groups before enrollment because an agent can be assigned to them during enrollment. The documentation is explicit here: enrollment with a group parameter fails if the group does not exist on the manager. Without an assignment the agent lands in the default group, whose files live in /var/ossec/etc/shared/default/. Every group has its own /var/ossec/etc/shared/<GROUP_NAME>/agent.conf file on the manager, which is empty after creation. I fill it in part 3.

A group can be created in three ways, all with the same result:

MethodHow
dashboardAgents management > Groups > Add new group, enter the name and Save new group
tool on the manager/var/ossec/bin/agent_groups -a -g GROUP_ID -q
APIPOST /groups with the body {"group_id": "GROUP_ID"}

The -q flag suppresses the confirmation prompt. A group name may contain uppercase and lowercase letters, digits, dots, underscores and hyphens. For the two lab groups the tool is enough:

# Create the two lab groups on the manager (run as root)
/var/ossec/bin/agent_groups -a -g example-linux -q
/var/ossec/bin/agent_groups -a -g example-windows -q
# List all groups and the number of agents in each
/var/ossec/bin/agent_groups -l

The output of -l starts with the header Groups (N): and ends with the line Unassigned agents: 0.. The same result comes from a POST /groups request in Server management > Dev Tools; the response Group 'example-linux' created. confirms the creation. A full description of groups, the order of multiple groups and shared files is in the post on agent groups.

Before installing the first agent I check from its host that the manager answers on the three ports from the documentation: 1514/TCP (agent communication), 1515/TCP (enrollment through the agent configuration) and 55000/TCP (enrollment through the server API):

# Linux and macOS, netcat installed: test the three manager ports
nc -zv manager.example.com 1514 1515 55000
# Windows PowerShell: no output means the connection succeeded
(new-object Net.Sockets.TcpClient).Connect("manager.example.com", 1514)
(new-object Net.Sockets.TcpClient).Connect("manager.example.com", 1515)
(new-object Net.Sockets.TcpClient).Connect("manager.example.com", 55000)

On Linux, success looks like Connection to ... port 1514 [tcp] succeeded!. On Windows, a failure is the message A connection attempt failed because the connected party did not properly respond after a period of time. Port 1515 serves enrollment and 1514 serves the communication of a running agent.

Exercise 2: a Windows agent with deployment variables

The MSI package accepts deployment variables, which the installer turns into the agent configuration and a one-time request to the enrollment service. The list from the documentation is shared between Windows and Linux:

VariableMeaning according to the documentationTarget option
WAZUH_MANAGERIP address or FQDN of the manager, several addresses separated by commasaddress
WAZUH_MANAGER_PORTport for the connection to the managerport
WAZUH_PROTOCOLtcp or udp, TCP by defaultprotocol
WAZUH_REGISTRATION_SERVERenrollment server; when empty, the value of WAZUH_MANAGER is usedmanager_address
WAZUH_REGISTRATION_PORTport of the enrollment serverport in enrollment
WAZUH_REGISTRATION_PASSWORDenrollment password, saved in the authd.pass fileauthorization_pass_path
WAZUH_KEEP_ALIVE_INTERVALtime between connection checks with the managernotify_time
WAZUH_TIME_RECONNECTtime before reconnecting after a connection losstime-reconnect
WAZUH_REGISTRATION_CApath to the certificate authority used to verify the managerserver_ca_path
WAZUH_REGISTRATION_CERTIFICATEpath to the agent certificateagent_certificate_path
WAZUH_REGISTRATION_KEYpath to the agent keyagent_key_path
WAZUH_AGENT_NAMEagent name, the computer name by defaultagent_name
WAZUH_AGENT_GROUPone or more existing groups, separated by commasgroups
ENROLLMENT_DELAYtime agentd waits after a successful enrollmentdelay_after_enrollment

On Windows there are two more installer switches: /l installer.log writes the installation log, and /l*v installer.log does the same with verbose messages. A relative path in WAZUH_REGISTRATION_CA is searched for in the agent installation directory. The agent certificate and key must be given together so that the server can verify the agent identity.

I run the installation in PowerShell as administrator. I recommend matching the MSI version to the manager version; the 4.14 documentation examples use 4.14.11:

# Download the agent installer
Invoke-WebRequest -Uri https://packages.wazuh.com/4.x/windows/wazuh-agent-4.14.11-1.msi -OutFile wazuh-agent-4.14.11-1.msi
# Install and enroll in one step, with name, group and password
msiexec.exe /i wazuh-agent-4.14.11-1.msi /q WAZUH_MANAGER="manager.example.com" WAZUH_REGISTRATION_SERVER="manager.example.com" WAZUH_REGISTRATION_PASSWORD="<REGISTRATION_PASSWORD>" WAZUH_AGENT_NAME="example-win-01" WAZUH_AGENT_GROUP="example-windows"

If a variable value contains spaces, PowerShell needs it in triple quotes, for example WAZUH_REGISTRATION_PASSWORD="""TOP SECRET""". The documentation has you start the service as a separate step:

# Start the agent service
Start-Service wazuhsvc

The CMD equivalent is NET START WazuhSvc. The agent is a 32-bit application, so on 64-bit Windows the files are in C:\Program Files (x86)\ossec-agent, and on 32-bit Windows in C:\Program Files\ossec-agent. That directory holds ossec.conf, client.keys, ossec.log and the state file wazuh-agent.state. Checking the state:

# Read the connection status from the agent state file
Select-String -Path 'C:\Program Files (x86)\ossec-agent\wazuh-agent.state' -Pattern "^status"

After a few seconds status='connected' should appear, and the agent shows up in Agents management > Summary. If the installation ran without deployment variables, the agent has no key and has to be enrolled with one of the methods from exercises 3 and 4. The installer also ships a graphical “Windows agent manager” that shows the service state and opens the log and the configuration file, but every exercise can be done without it.

Windows versions older than Server 2008 and Windows 7 need a compatibility mode: either start /var/ossec/bin/wazuh-authd with the -a flag, or set <ssl_auto_negotiate> to yes in the manager <auth> section. I skip that in a lab with current systems.

Exercise 3: a Linux agent through agent-auth

The second Linux host has an agent installed without deployment variables, so it has no key. First I point it at the manager in /var/ossec/etc/ossec.conf, in the <client> section:

<client>
  <server>
    <address>manager.example.com</address>
    ...
  </server>
</client>

The default values of port (1514) and protocol (tcp) stay. Then, as root, I run agent-auth, the client of the wazuh-authd service on the manager. The options from the documentation:

OptionMeaningDefault
-m MANAGER_IPmanager address-
-p PORTport of the wazuh-authd service1515
-A AGENT_NAMEagent namehostname
-G GROUPexisting groups, separated by commas-
-P PASSWORDpassword instead of the authd.pass file-
-ithe agent IP address is set by the connection to the manager-
-Isets the agent IP address-
-v PATHCA used to verify the server-
-x PATHagent certificate-
-k PATHagent key-
-aautomatic choice of a common SSL/TLS versionTLS 1.2 only, if the server supports it
-c CIPHERSlist of SSL ciphersHIGH:!ADH:!EXP:!MD5:!RC4:!3DES:!CAMELLIA:@STRENGTH
-Dinstallation directory/var/ossec
-tconfiguration test-
-ddebug mode, repeating it increases verbosity-

The remaining options are -g (run as a group), -n (network interface for IPv6 link-local addresses), -h and -V. The command from the exercise:

# Enroll this host with a name, a group and the registration password
/var/ossec/bin/agent-auth -m manager.example.com -A agent-02 -G example-linux -P <REGISTRATION_PASSWORD>

The documentation notes that by default this transaction does not require authentication and recommends running wazuh-authd only while a new agent is being added. The -P option replaces the lookup of the password in authd.pass; when the password is in neither the file nor the command line, the client connects without a password, that is, in the unsecured mode. A manager with use_password set to yes then rejects the request. A password given through -P ends up in the shell history and the process list, so for real deployments I recommend the authd.pass file instead of -P.

A correct run looks like this:

agent-auth: INFO: Started (pid: 4321).
agent-auth: INFO: Requesting a key from server: manager.example.com
agent-auth: INFO: Using agent name as: agent-02
agent-auth: INFO: Waiting for server reply
agent-auth: INFO: Valid key received

The message Valid key received means the manager issued a key. The connection still needs a restart of the agent service:

systemctl restart wazuh-agent

Exercise 4: enrollment from the enrollment block

The third method needs no separate command: an agent without a key reports to the enrollment service on its own as soon as it has the manager address in <client><server><address>. On the agent-01 host I set the password on the agent side. The file must contain the same password that the manager reads from /var/ossec/etc/authd.pass:

# Create the password file on the agent and restrict it
echo "<REGISTRATION_PASSWORD>" > /var/ossec/etc/authd.pass
chmod 640 /var/ossec/etc/authd.pass
chown root:wazuh /var/ossec/etc/authd.pass

The documentation gives the correct permissions: -rw-r----- 1 root wazuh ... /var/ossec/etc/authd.pass. This is the default path, so the agent finds the file without extra configuration. A custom path is set by the authorization_pass_path option in the <enrollment> block. I add that block, together with the agent name and group, just before the closing </client>:

<client>
  <server>
    <address>manager.example.com</address>
    ...
  </server>
  <enrollment>
    <agent_name>agent-01</agent_name>
    <groups>example-linux</groups>
    <authorization_pass_path>/var/ossec/etc/authd.pass</authorization_pass_path>
  </enrollment>
</client>

The <enrollment> options from the <client> reference, with their defaults:

OptionDefaultMeaning
enabledyesenables or disables agent enrollment
manager_addressnonemanager used for enrollment; without a value the agent enrolls with the manager from the server section
port1515port of the enrollment request, must match the manager port
agent_namehostnameonly alphanumeric characters and -, _ and ., at least two characters
groupsnonegroups the agent belongs to
agent_addresssrcforced agent IP address; without it the manager uses the source address of the request
ssl_cipherHIGH:!ADH:!EXP:!MD5:!RC4:!3DES:!CAMELLIA:@STRENGTHlist of SSL ciphers
server_ca_pathnoneCA used to verify the manager; without it the server is not verified
agent_certificate_path, agent_key_pathnonerequired when the manager verifies agents
authorization_pass_path/etc/authd.pass (Unix) or authd.pass (Windows), relative to the installation directoryrequired for enrollment with a password
auto_methodnoyes negotiates the best common SSL/TLS version, no means TLS 1.2 only
delay_after_enrollment20seconds to wait after a successful enrollment
use_source_ipnoforces the IP address to be taken from the agent message

Relative paths are resolved from the agent installation directory. Group assignment works only for groups that exist, and exercise 1 took care of that. After saving the file I restart the agent and look for the enrollment fragment in the log:

systemctl restart wazuh-agent
# Show the enrollment lines in the agent log
grep -A 5 "Requesting a key" /var/ossec/logs/ossec.log

The line Valid key received ends the enrollment. All enrollment options, including certificate verification on both sides, are described in the post on agent enrollment. In a cluster you can add several <server> sections that point to the workers; the documentation describes the failover configuration, and the split of agents between nodes is covered in the post on the load balancer for agents.

Exercise 5: checking the connection four ways

Enrollment and connection are two separate events, so after each agent I check both. The documentation lists four sources of status, and I use all of them because each shows a different side.

SourceWhereWhat it shows
dashboardAgents management > Summarythe statuses Active, Disconnected, Pending, Never connected
agent_controlmanagerthe state of one agent or a list of all agents
APIGET /agents/001/stats/agentkeepalive and ACK times, message counters
state fileagent hostthe status as seen by the agent itself

On the manager:

# Status of one agent
/var/ossec/bin/agent_control -i 001 | grep Status
# List all agents with their status
/var/ossec/bin/agent_control -l

The first command returns the line Status: Active. The list starts with the header Wazuh agent_control. List of available agents:, the first row is the manager itself with ID 000 and the note Active/Local, and an agent that enrolled but has not connected has the status Never connected. The API request returns the fields status, last_keepalive, last_ack, msg_count, msg_sent, msg_buffer and buffer_enabled. With the token and jq from part 1 you can extract the three most important fields:

# Print status, last keepalive and last ACK of agent 001
curl -k -X GET "https://localhost:55000/agents/001/stats/agent" -H "Authorization: Bearer $TOKEN" | jq -r '.data.affected_items[0] | [.status, .last_keepalive, .last_ack] | @tsv'

The jq filter returns one line with three tab-separated values. On the agent host I read the state file. On Linux it is /var/ossec/var/run/wazuh-agentd.state:

# Read the connection status from the agent state file
sudo grep ^status /var/ossec/var/run/wazuh-agentd.state

The file accepts three status values. pending means waiting for an acknowledgement from the manager, connected means the acknowledgement arrived, and disconnected means no acknowledgement in the last 60 seconds or a lost connection. The documentation does not give the time after which the connected status stops being current. The causes of disconnections, the manager-side threshold and the dependency on notify_time are covered in the post on disconnected agents. The documentation default for notify_time is 20 seconds, and it should be lower than the time after which the manager considers an agent disconnected.

On the agent side the network connection shows up as an ESTABLISHED session to port 1514 of the manager:

# Linux: show the agent connection to the manager
netstat -vatunp|grep wazuh-agentd
# Windows: show the agent connection to the manager
Get-NetTCPConnection -RemotePort 1514

The last test concerns groups. Since the agent joins a group during enrollment, I check whether it received the group configuration:

# Group synchronization status of agent 001
/var/ossec/bin/agent_groups -S -i 001

The correct answer is Agent '001' is synchronized.. The same comes from GET /agents?agents_list=001&select=group_config_status, with the group_config_status field equal to synced.

If an agent is enrolled again under a different name or ID, it lands in the default group even if it belonged to another one before. The remoted.guess_agent_group=1 option in /var/ossec/etc/local_internal_options.conf turns on automatic restoration of membership; the manager then compares the checksum of the agent merged.mg file with other agents.

Exercise 6: enrollment errors

The first step in every case is the log. The manager writes to /var/ossec/logs/ossec.log, and the agent writes to /var/ossec/logs/ossec.log on Linux or C:\Program Files (x86)\ossec-agent\ossec.log on 64-bit Windows. Six cases from the documentation:

Symptom in the logCauseSolution
ERROR: Invalid password (from manager) preceded by No authentication password providedno password, or one different from the managerthe same password on both sides, authd.pass in /var/ossec/etc/ with permissions 640 and owner root
ERROR: Invalid agent name: NAME (from manager)the name already exists, usually a duplicated hostnamea unique name in the agent_name option or through WAZUH_AGENT_NAME
WARNING: (1404): Authentication error. Wrong key or corrupt payloaddifferent client.keys on the manager and the agenta matching agent key, no copying of client.keys between agents
Unable to read CA certificate fileno CA at the path from the manager ssl_agent_ca option or the agent server_ca_paththe file at the given path
Unable to read private key fileno agent key from the agent_key_path optionthe file at the given path
Unable to read certificate file (not found)no agent certificate from the agent_certificate_path optionthe file at the given path

For the Invalid password error the documentation gives a third solution: if the password is not needed, disable it in the manager <auth> section. In the lab I leave it enabled because exercise 4 relies on the password. The entries Unable to add agent (from manager) accompany the password and name errors. The two certificate errors additionally end with the message Could not set up SSL connection! Check certification configuration., and they apply only to deployments with identity verification, which this training does not enable.

Duplicate keys in client.keys can appear after removing agents with the highest IDs or after copying the file between agents. The thresholds for replacing agents with the same name in the <force> block I described in part 1.

Checklist after part 2

  • The groups example-linux and example-windows are visible in agent_groups -l.
  • Manager ports 1514 and 1515 are reachable from every agent host.
  • The Windows agent is installed with WAZUH_AGENT_NAME, WAZUH_AGENT_GROUP and WAZUH_REGISTRATION_PASSWORD, and the service is running.
  • The Linux agent agent-02 is enrolled through agent-auth with -G and the Valid key received confirmation.
  • The Linux agent agent-01 is enrolled through the <enrollment> block with authorization_pass_path.
  • status='connected' in the state file on every agent.
  • Three agents on the agent_control -l list, each with the status Active.
  • agent_groups -S -i <ID> returns synchronized for agents in groups.

In part 3 I upgrade the agents from the manager and fill in the agent.conf files of the groups created in this part.

What the documentation does not specify

  • The documentation pages use two names for the agent service on Windows: WazuhSvc at startup (NET START WazuhSvc, Start-Service wazuhsvc) and wazuh at restart (Restart-Service -Name wazuh, net stop wazuh). They do not explain whether this is one service under two names.
  • The agent-auth page speaks of existing groups for -G but does not describe what the tool does when the group does not exist. For the <enrollment> block the documentation explicitly announces an error.
  • The 4.14 installation page gives one MSI example (4.14.11) and does not describe the availability of older MSI files or a rule for matching the agent version to the manager version.
  • The time after which the manager changes the status from pending to disconnected is not given on the agent connectivity page.
  • The documentation shows the enrollment password given in plain text in the msiexec command but does not assess the risk of its visibility in the command line.

Sources

↑↓ select Enter open Esc close