Wazuh 4 training, part 10: Osquery, Sysmon and Docker
Part 10 of the Wazuh 4 training: the osquery module with query packs, Sysmon via eventchannel and the Docker listener, with ossec.conf options and 4.14 rules.
The last part of the Wazuh 4 training adds three more data sources to agents: Osquery on Linux and Windows hosts, Sysmon on Windows and Docker events. The previous part, CDB lists and Active Response, ended with a reaction to an alert. Here I go back to data collection, this time for data the agent does not get from log files.
Each of the three sources works differently. Osquery is started by an agent module as a subprocess and reports the results of SQL queries. Sysmon writes to the Windows Event Log channel, which the agent reads like any other channel. Docker events are collected by a listener running in the agent.
Osquery: how the module connects to the agent
Osquery exposes the operating system as a relational database, so host data is fetched with SQL queries. The Wazuh documentation gives three examples that show the range well:
-- all local users
SELECT * FROM users;
-- processes listening on all interfaces
SELECT DISTINCT processes.name, listening_ports.port, processes.pid
FROM listening_ports JOIN processes USING (pid)
WHERE listening_ports.address = '0.0.0.0';
-- processes whose executable was deleted from disk
SELECT * FROM processes WHERE on_disk = 0;
The osquery module in the agent manages the Osquery configuration and collects the results it generates, then passes them to the manager, where they can become alerts. Osquery is not installed together with the agent. It is separate software that you have to install yourself on every host.
Exercise 1: Osquery on a Linux agent
Installing and configuring Osquery
On Red Hat family distributions the documentation tells you to add the GPG key and the repository first, then install the package:
# Red Hat, CentOS and Fedora (yum-utils may be required first)
curl -L https://pkg.osquery.io/rpm/GPG | tee /etc/pki/rpm-gpg/RPM-GPG-KEY-osquery
yum-config-manager --add-repo https://pkg.osquery.io/rpm/osquery-s3-rpm.repo
yum-config-manager --enable osquery-s3-rpm-repo
yum install osquery
On Debian and Ubuntu the key goes to /etc/apt/keyrings and the repository to a separate file:
# Debian and Ubuntu based distributions
mkdir -p /etc/apt/keyrings
curl -L https://pkg.osquery.io/deb/pubkey.gpg | tee /etc/apt/keyrings/osquery.asc
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/osquery.asc] https://pkg.osquery.io/deb deb main" | tee /etc/apt/sources.list.d/osquery.list
apt-get update
apt-get install osquery
Osquery needs the file /etc/osquery/osquery.conf. You can copy the example shipped with the package (/opt/osquery/share/osquery/osquery.example.conf) or use the configuration from the Wazuh documentation. The version below contains three scheduled queries and six packs:
{
"options": {
"config_plugin": "filesystem",
"logger_plugin": "filesystem",
"utc": "true"
},
"schedule": {
"system_info": {
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
"interval": 3600
},
"high_load_average": {
"query": "SELECT period, average, '70%' AS 'threshold' FROM load_average WHERE period = '15m' AND average > '0.7';",
"interval": 900,
"description": "Report if load charge is over 70 percent."
},
"low_free_memory": {
"query": "SELECT memory_total, memory_free, CAST(memory_free AS real) / memory_total AS memory_free_perc, '10%' AS threshold FROM memory_info WHERE memory_free_perc < 0.1;",
"interval": 1800,
"description": "Free RAM is under 10%."
}
},
"packs": {
"osquery-monitoring": "/opt/osquery/share/osquery/packs/osquery-monitoring.conf",
"incident-response": "/opt/osquery/share/osquery/packs/incident-response.conf",
"it-compliance": "/opt/osquery/share/osquery/packs/it-compliance.conf",
"vuln-management": "/opt/osquery/share/osquery/packs/vuln-management.conf",
"hardware-monitoring": "/opt/osquery/share/osquery/packs/hardware-monitoring.conf",
"ossec-rootkit": "/opt/osquery/share/osquery/packs/ossec-rootkit.conf"
}
}
The interval key is given in seconds: system_info runs every hour (3600), high_load_average every 15 minutes (900), low_free_memory every 30 minutes (1800). You add your own query to the schedule section as another object with the query and interval fields, and you register your own packs in the packs section or with the module’s pack option.
Then I start the Osquery daemon:
systemctl enable osqueryd
systemctl start osqueryd
Enabling the module in the agent
I enable the module in the agent’s /var/ossec/etc/ossec.conf or through central configuration (groups and agent.conf are covered in part 3 and in the post on agent groups). The shortest form is an empty element:
<wodle name="osquery"/>
The full section with every value given explicitly looks like this:
<wodle name="osquery">
<disabled>no</disabled>
<run_daemon>yes</run_daemon>
<bin_path>/usr/bin</bin_path>
<log_path>/var/log/osquery/osqueryd.results.log</log_path>
<config_path>/etc/osquery/osquery.conf</config_path>
<add_labels>no</add_labels>
<pack name="custom_pack">/path/to/custom_pack.conf</pack>
</wodle>
Module options:
| Option | Values | Default | Meaning |
|---|---|---|---|
disabled | yes, no | no | disables the module |
run_daemon | yes, no | yes | yes: the module starts osqueryd as a subprocess; no: the module only reads the results log and does not start Osquery |
bin_path | any path | Linux: empty, Windows: C:\Program Files\osquery\osqueryd | full path to the directory with the osqueryd executable |
log_path | any path | Linux: /var/log/osquery/osqueryd.results.log, Windows: C:\Program Files\osquery\log\osqueryd.results.log | results log written by Osquery |
config_path | any path | Linux: /etc/osquery/osquery.conf, Windows: C:\Program Files\osquery\osquery.conf | Osquery configuration file, can be relative to the agent directory |
add_labels | yes, no | yes | adds the agent labels defined as decorators |
pack | path to a pack file | empty | adds a query pack, can be repeated, the name attribute sets the name |
The run_daemon value decides who is responsible for what. With yes, Wazuh starts and supervises Osquery, so a separate osqueryd service is not needed. With no, Osquery runs independently, for example as a service managed outside Wazuh, and the agent only reads the file from log_path.
Alerts from query results
Results reach the manager as events with the location osquery and are decoded by the json decoder. The rules in 0545-osquery_rules.xml create three levels of messages and one rule for results:
| Rule | Level | Condition |
|---|---|---|
| 24000 | 3 | location ends with osquery, generic message |
| 24001 | 5 | message starts with E and four digits, Osquery error |
| 24002 | 3 | message starts with W and four digits, warning |
| 24003 | 2 | message starts with I and four digits, information |
| 24010 | 3 | decoded JSON, description osquery: $(osquery.name) query result |
Rule 24010 is the parent of all result rules. The scheduled queries from the configuration above have their own child rules (level 4, filtering on the osquery.name field): high_load_average is 24011, low_free_memory is 24012, low_disk_space is 24013 and disk_time_busy is 24014. The description of rule 24011 in the file talks about memory, like 24012, even though it covers load. When reading an alert, I look at the query name and ignore the description text.
Pack results are recognized by rules that filter on the osquery.pack field. For the osquery-monitoring pack this is 24020, for incident-response 24030, and the file also covers, among others, it-compliance, vuln-management, hardware-monitoring, ossec-rootkit, windows-hardening, windows-attacks and unwanted-chrome-extensions.
The documentation includes an example alert in JSON format. The most important fields I use when searching:
| Alert field | Content |
|---|---|
rule.id | 24010 |
location | osquery |
decoder.name | json |
data.name | scheduled query name (rules refer to it as osquery.name) |
data.hostIdentifier | host identifier assigned by Osquery |
data.columns.* | columns returned by the query |
data.action | added in the example |
The documentation adds one caveat: if more reports with the same content arrive, an alert is created only the first time and the rest are dropped. A query with a short interval therefore does not flood the dashboard with repeats, but it also will not remind you about a state that has not changed.
To quickly check what Osquery wrote locally before the result reaches the manager, jq on the file from log_path is enough. The filter below prints the time, the action and the columns of the query named users_list:
# Read the local results log of the osquery module (one JSON object per line)
jq -r 'select(.name == "users_list") | [.calendarTime, .action, (.columns | tostring)] | @tsv' \
/var/log/osquery/osqueryd.results.log
I finish the exercise by searching the dashboard for rule.id:24010. If the schedule contains a users_list query with SELECT * FROM users;, after the first run I will see alerts with rows holding the account list. In the example alert from the documentation the result has action equal to added.
Exercise 2: Osquery on a Windows agent
On Windows the same module has different default paths. Installing Osquery from an MSI package is not part of the Wazuh documentation, so I assume osqueryd is already on the host in the default directory. If it lives elsewhere, I set bin_path, log_path and config_path explicitly:
<wodle name="osquery">
<disabled>no</disabled>
<run_daemon>yes</run_daemon>
<bin_path>C:\Program Files\osquery\osqueryd</bin_path>
<log_path>C:\Program Files\osquery\log\osqueryd.results.log</log_path>
<config_path>C:\Program Files\osquery\osquery.conf</config_path>
<add_labels>yes</add_labels>
</wodle>
The osquery.conf file for Windows has the same structure (options, schedule, packs) as the Linux version, and the tables used in queries must exist on Windows. The Wazuh rules file has separate rules for the windows-hardening and windows-attacks packs, so I put those first in the packs section. The documentation does not give pack paths on Windows.
I keep the configuration of Windows agents in a group whose agent.conf contains the wodle section. One change then reaches all hosts in the group.
Exercise 3: Sysmon on Windows
What Sysmon is
Sysmon (System Monitor) is a Windows service and driver. Once installed, it stays on the host across reboots and writes system activity to the Windows event log: process creation with the full command line, network connections, driver and library loads, file creation time changes. According to Microsoft, Sysmon does not analyze the events it generates and does not hide from an attacker, so analysis is the job of the SIEM. Events go to the channel Applications and Services Logs/Microsoft/Windows/Sysmon/Operational, and timestamps are in UTC.
Sysmon commands:
| Command | Action |
|---|---|
sysmon64 -i [file] | installs the service and driver, optionally with a configuration file |
sysmon64 -c [file] | changes the configuration of a running Sysmon, prints the current one with no argument |
sysmon64 -c -- | restores default settings |
sysmon64 -s | prints the configuration schema |
sysmon64 -u [force] | uninstalls the service and driver |
-accepteula | accepts the license without prompting |
Neither installation nor uninstallation requires a system restart.
Configuration file
An installation without a configuration file does not enable network monitoring. Events 3 (network connection) and 7 (image loaded) are disabled by default and have to be enabled in the configuration. The XML file has a Sysmon root with a schemaversion attribute, configuration entries and an EventFiltering section:
<Sysmon schemaversion="4.82">
<HashAlgorithms>*</HashAlgorithms>
<EventFiltering>
<DriverLoad onmatch="exclude">
<Signature condition="contains">microsoft</Signature>
<Signature condition="contains">windows</Signature>
</DriverLoad>
<NetworkConnect onmatch="include">
<DestinationPort>443</DestinationPort>
<DestinationPort>80</DestinationPort>
</NetworkConnect>
<NetworkConnect onmatch="exclude">
<Image condition="end with">iexplore.exe</Image>
</NetworkConnect>
</EventFiltering>
</Sysmon>
The schema version is independent of the executable version, and the current one is shown by -? config. The filtering rules are as follows:
onmatch="include"logs only matching events,onmatch="exclude"logs all events except the matching ones.- For one event you can give both sets, and exclusions take precedence.
- Rules for the same field combine with OR, and rules for different fields with AND. A
RuleGroupelement with agroupRelationattribute sets this explicitly toandoror. - Events 4 (service state change) and 16 (configuration change) cannot be filtered.
The condition is given in the condition attribute. Available values (case-insensitive): is (default), is any, is not, contains, contains any, contains all, excludes, excludes any, excludes all, begin with, end with, not begin with, not end with, less than, more than and image, which matches either the full image path or just the file name. The name attribute on a rule makes Sysmon record which rule triggered the event.
Configuration entries include HashAlgorithms (MD5, SHA1, SHA256, IMPHASH or *), CheckRevocation (default True), DnsLookup (default True), CopyOnDeletePE (default False) and ArchiveDirectory (default Sysmon).
Installation and collection in the agent
The Wazuh documentation shows installing Sysmon with a sysmonconfig.xml file placed next to the executable, in PowerShell with administrator rights:
.\Sysmon64.exe -accepteula -i .\sysmonconfig.xml
Then in C:\Program Files (x86)\ossec-agent\ossec.conf, inside <ossec_config>, I add the channel collection in the eventchannel format:
<!-- Configure Wazuh agent to receive events from Sysmon -->
<localfile>
<location>Microsoft-Windows-Sysmon/Operational</location>
<log_format>eventchannel</log_format>
</localfile>
Restart the agent with Restart-Service -Name wazuh. This block can also go into the group’s agent.conf instead of the local file. You change the configuration of a running Sysmon with sysmon64 -c <file>, which updates the settings of the active driver.
Sysmon rules in Wazuh
The ID range 61600-62099 belongs to the file 0595-win-sysmon_rules.xml, and the rules are in the groups windows,sysmon. Rules 61600-61602 are children of channel rule 60004, and the event rules are children of 61600:
| Rule | Level | Meaning |
|---|---|---|
| 61600 | 0 | informational event (severityValue equal to INFORMATION) |
| 61601 | 0 | warning event |
| 61602 | 5 | error event |
| 61603 | 0 | event 1, process creation, group sysmon_event1 |
| 61605 | 0 | event 3, network connection, group sysmon_event3 |
| 61615 | 0 | event 13, registry value set |
| 61655 | 0 | event 255, Sysmon error |
Rules for individual events have level 0, so they do not create alerts on their own. They act as parents: detection rules from files such as 0800-sysmon_id_1.xml (range 92000-92100) attach to them through if_group on the sysmon_event1 group. The detailed rule chain and the win.eventdata fields are described in the post on Sysmon in Wazuh, and agent-side channel filtering in the post on EventChannel filtering.
A custom rule based on PsExec
The documentation shows a child rule of event 13 (rule 61615) that detects creation of the PSEXESVC service. A version with an ID from the custom rule range, for /var/ossec/etc/rules/local_rules.xml:
<group name="windows,sysmon,privilege-escalation">
<rule id="100100" level="10">
<if_sid>61615</if_sid>
<field name="win.eventdata.targetObject" type="pcre2">HKLM\\\\System\\\\CurrentControlSet\\\\Services\\\\PSEXESVC</field>
<field name="win.eventdata.eventType" type="pcre2">^SetValue$</field>
<field name="win.eventdata.user" type="pcre2">NT AUTHORITY\\\\SYSTEM</field>
<options>no_full_log</options>
<description>PsExec service running as $(win.eventdata.user) has been created on $(win.system.computer).</description>
<mitre>
<id>T1543.003</id>
</mitre>
</rule>
</group>
After adding the rule I restart the manager with sudo systemctl restart wazuh-manager.service. The alert from this rule has the location EventChannel, the decoder windows_eventchannel and data.win.system.eventID equal to 13.
The certutil exercise
A typical detection test is downloading a file with the built-in certutil.exe tool and the -urlcache option. Rule 92075 (level 12, technique T1105) requires three things at once: event 1, the original file name certutil.exe and a command line matching the expression (?i)\-urlcache.+\.(dll|exe). The pattern ends in .dll or .exe, so downloading an .exe file triggers it and downloading a file with any other extension does not. A separate level 4 rule for -decode alone (92072) exists and does not cover -urlcache.
After the test I search the dashboard for events from the sysmon group and check which rule fired. An event that matches only a level 0 rule does not create an alert.
Exercise 4: Docker monitoring
Requirements
Docker monitoring has two steps: the agent on the Docker server watches the host, and the Docker listener collects container events and passes them to the manager. The listener requires Python 3 (compatibility declared for versions 3.8 to 3.13) and the docker library at version 7.1.0. The manager has all dependencies installed, so the steps below apply only to the agent:
# Python 3.8-3.10
pip3 install docker==7.1.0 urllib3==1.26.20 requests==2.32.2
# Python 3.11-3.12 (externally managed environment, see PEP 668)
pip3 install docker==7.1.0 urllib3==1.26.20 requests==2.32.2 --break-system-packages
The --break-system-packages flag overrides the default externally managed Python environment. The alternative is a virtual environment: in the script /var/ossec/wodles/docker/DockerListener you then have to change the shebang to the interpreter from that environment, for example #!<path-to-venv>/bin/python3.
Enabling the listener
In the agent’s /var/ossec/etc/ossec.conf on the Docker server I add:
<wodle name="docker-listener">
<disabled>no</disabled>
</wodle>
Then systemctl restart wazuh-agent. By default the listener starts together with the agent. Options:
| Option | Default | Meaning |
|---|---|---|
disabled | no | disables the listener (yes, no) |
attempts | 5 | number of attempts to start the listener after a failure |
run_on_start | yes | starts immediately after the agent starts |
interval | 1m | wait time before restarting after a failure, with the suffix s, m, h, d or M |
day | none | day of the month 1-31, requires interval to be a multiple of months |
wday | none | day of the week (sunday/sun to saturday/sat), incompatible with day, interval in weeks |
time | none | time in hh:mm format, interval in days or weeks |
The documentation example sets ten-minute intervals and five attempts, without starting on agent start:
<wodle name="docker-listener">
<interval>10m</interval>
<attempts>5</attempts>
<run_on_start>no</run_on_start>
<disabled>no</disabled>
</wodle>
Here interval serves a recovery purpose: it describes the restart after an error and does not set a polling frequency.
Docker rules
Events are decoded by the json decoder, and base rule 87900 (level 0, group docker) matches the integration field equal to docker. Below it are rules keyed on the docker.status or docker.Action field. The most important ones from 0560-docker_integration_rules.xml:
| Rule | Level | Condition | Description |
|---|---|---|---|
| 87901 | 3 | status create | container created |
| 87902 | 5 | status destroy | container destroyed, technique T1561.001 |
| 87903 | 3 | status start | container started |
| 87904 | 3 | status stop | container stopped |
| 87905 | 3 | status pause | container paused |
| 87906 | 3 | status unpause | container resumed |
| 87907 | 3 | status starts with exec_start: | command run in a container |
| 87908 | 5 | child of 87907, status exec_start: and bash, /bin/bash, sh, dash or /bin/dash | shell session in a container |
| 87921 | 7 | status delete | container deleted |
| 87924 | 7 | status kill or die | container received a kill or die action |
| 87932 | 3 | status pull | image pulled |
Rules for the types volume, network, config, secret, plugin, node and service have a level 0 parent and child rules on docker.Action, for example 87914 (level 7) for volume removal. Rule descriptions are built from the docker.Actor.Attributes.name fields, so the container name is visible in the alert. The status patterns in rule 87908 have a space before the end of the text, for example exec_start: /bin/bash $.
Exercise
On the host with the listener I run a container lifecycle and check which alerts appear:
docker pull nginx
docker run -d --name example-web nginx
docker pause example-web
docker unpause example-web
docker exec -it example-web /bin/bash
# leave the shell with exit
docker stop example-web
docker rm example-web
In the dashboard I filter on data.integration:docker. I expect alerts from the rules matching the commands: pull (87932), create and start (87901, 87903), pause and unpause (87905, 87906), the shell (87908) and finally stop and destroy (87904, 87902). The Wazuh documentation does not list which events Docker emits for a given command, so I treat additional alerts, for example from rule 87924 on stop, as part of the run.
I treat level 5 of rule 87908 as the notification threshold. Custom child rules (IDs from 100000) can raise it for selected containers by filtering on docker.Actor.Attributes.name.
Checklist after part 10
- Osquery is installed on the Linux agent,
osquerydis running and/etc/osquery/osquery.confcontains thescheduleandpackssections. - The
<wodle name="osquery">section is enabled inossec.confor in the group’sagent.conf, andrun_daemonhas a deliberately chosen value. - In the dashboard
rule.id:24010returns results and they show the scheduled query name. - On the Windows agent the
bin_path,log_pathandconfig_pathvalues point to existing files. - Sysmon is installed with
-accepteula -iand a configuration file, and I apply configuration changes with-c. - The
localfileblock withMicrosoft-Windows-Sysmon/Operationaland theeventchannelformat is in the Windows agent configuration. - I know which rules in the 92000-92100 range match the test command and why a file with an extension other than
.dllor.exedoes not trigger rule 92075. - On the Docker server Python 3 runs with the
docker7.1.0 library, the listener is enabled anddata.integration:dockeralerts are visible.
This is the last part of the series.
What the documentation does not specify
- Osquery installation on Windows, including the MSI package and the paths to query packs on that system.
- How Osquery reports differences between consecutive results. The Wazuh documentation shows only an example with
"action":"added"and a note about dropping identical reports. - Field names in the example Osquery alert: the example shows
data.name, while the rules filter onosquery.name. - Descriptions of rules 24011 and 24012: in the 4.14.0 file both talk about memory, although 24011 covers CPU load.
- The source and contents of
sysmonconfig.xml. The Wazuh documentation gives only the file name, and the Sysmon documentation describes the syntax, not a ready configuration. - The list of events Docker emits for individual CLI commands. The documentation only states which
docker.statusanddocker.Actionvalues match the rules. - A rule for
certutil.exewith-urlcacheand a file other than.dllor.exe: the 4.14.0 rules file has no such rule.
Sources
- https://documentation.wazuh.com/4.14/user-manual/capabilities/system-inventory/osquery.html
- https://documentation.wazuh.com/4.14/user-manual/reference/ossec-conf/wodle-osquery.html
- https://documentation.wazuh.com/4.14/user-manual/capabilities/container-security/monitoring-docker.html
- https://documentation.wazuh.com/4.14/user-manual/reference/ossec-conf/wodle-docker.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/mitre.html
- https://learn.microsoft.com/en-us/sysinternals/downloads/sysmon
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0545-osquery_rules.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0560-docker_integration_rules.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0595-win-sysmon_rules.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0800-sysmon_id_1.xml