PL EN
WB-C6415DB4TLP:CLEARWazuh 4

Wazuh 4 training, part 7: file integrity monitoring

Wazuh 4 training part 7: FIM modes (scheduled, realtime, whodata), central syscheck configuration, audit fields in alerts and a maintenance window via groups.

Part seven of the Wazuh 4 training moves from the rules in part 6 to the events those rules classify in the FIM module (File Integrity Monitoring, configured in the syscheck section). Three exercises cover the three directory monitoring modes, configuration delivery through an agent group, and silencing noise during a maintenance window. The basics of the module are in the post on FIM and syscheck, and agent groups are covered in the post on agent groups.

How the FIM module works

The module runs periodic scans of specified paths and watches selected directories in real time. Paths are set in the agent or manager configuration. FIM stores checksums and file attributes in a local database and compares the current state with the stored one during a scan. A discrepancy ends in an alert.

The data goes to two databases. The first is a local SQLite database on the monitored endpoint, in /var/ossec/queue/fim/db on Linux, /Library/Ossec/queue/fim/db on macOS and C:\Program Files (x86)\ossec-agent\queue\fim\db on Windows. The second is the agent database on the Wazuh server, created and maintained by the wazuh-db daemon, one per agent ID, in /var/ossec/queue/db. The module synchronizes the two, and only information about changed checksums and attributes goes to the server. An up-to-date server database serves FIM-related API queries.

The default configuration is enabled on both the agent and the manager, but the documentation recommends reviewing the paths for your own environment. The default value of the directories option is /etc,/usr/bin,/usr/sbin,/bin,/sbin (the default configuration block in the documentation for Linux also adds /boot), and on macOS /private/etc,/usr/bin,/usr/sbin,/bin,/sbin, because /etc is a symbolic link there. Since version 4.13.0 the module on Windows monitors local file systems only. UNC paths and mapped drives are ignored in all modes and generate no events.

Monitoring modes

The attributes of the directories element choose the mode for each directory separately. The table below collects the ones the exercises use.

AttributeDefaultMeaning
realtimenocontinuous monitoring (inotify on Linux), directories only, not single files
whodatanocontinuous monitoring with information about who made the change, Linux and Windows
report_changesnoreports the content of the change, text files only
diff_size_limit50MBa file larger than the limit reports no diff
check_allyessets all attributes with the check_ prefix
tagsnonelist of tags added to alerts from the directory, separated by commas
recursion_level512maximum depth, an integer from 0 to 512
restrictnonelimits checks to files whose name contains the given string (sregex)
follow_symbolic_linknofollows a link given directly in the configuration, Unix

A directory without realtime and whodata is checked only in periodic scans. A scan starts every frequency seconds, by default 43200, which is every 12 hours. The alternative is scan_time (for example 10pm) together with scan_day (for example saturday), which moves scans outside working hours. The documentation notes that scan_time may delay the initialization of real-time scans. Periodic scans protect against alert floods for files that change often, such as logs.

Two notes on realtime mode. Real-time detection is paused for the duration of a periodic scan and resumes right after it finishes. The directory must exist before the agent restarts, otherwise the module skips it until the next restart. Likewise, files or directories created after the first scan are picked up only in the next periodic scan.

If several options affect the same attribute, the last one applies. The notation check_all="no" check_mtime="yes" keeps the modification time check, while the reversed order check_mtime="yes" check_all="no" disables all attributes including it. The second rule concerns wildcards: if a block with * or ? conflicts with a block without them, the block without wildcards wins for that specific path. Wildcards are re-evaluated at every periodic scan.

What gets recorded

The check_* attributes decide which metadata goes into the database. check_sum covers three hashes at once (MD5, SHA-1, SHA-256), while check_md5sum, check_sha1sum and check_sha256sum control each one separately. The rest are check_size, check_owner, check_group, check_perm, check_attrs (Windows), check_mtime and check_inode (Unix). On Windows uid and gid are always 0, and check_perm works only on NTFS partitions and records a list of permissions for users and groups.

Whodata mode

Whodata adds to the event information about who changed the file: the user and the process name. On Linux the documentation describes two providers, audit and ebpf, selected with the provider element in the whodata block. When provider is not set, audit runs. Audit mode is an extension of realtime mode with data from the Linux Audit subsystem.

Audit mode requirements

The auditd daemon is required. With Audit 3.1.1 and later you additionally install the audispd plugin (the audispd-plugins packages) and restart the service. The second trap: many systems have the rule -a never,task in the audit rules, which disables event processing and prevents who-data reporting. You check it with auditctl -l | grep task. If the entry exists, remove it from /etc/audit/rules.d/audit.rules, then restart auditd and the agent.

After the agent restarts, an audit rule for the monitored directory appears, for example -w /etc -p wa -k wazuh_fim. The agent removes it when it stops. The restart_audit option (default yes) enables restarting auditd after the plugin installation, and audit_key includes in the database events that audit already collects under its own key. queue_size in the whodata block sets the event queue, and increasing it raises the memory use of the agent.

eBPF mode

The ebpf provider extracts events from eBPF programs and does not need auditd. It requires a kernel with the BPF ring buffer and BTF, that is upstream from 5.8 or an older one with a backport. At startup the agent runs a health check, and when the check fails the module falls back to audit mode. The documentation notes one more difference: in alerts from eBPF mode the fields audit.process.name and audit.process.parent_name contain the short task name (for example bash, at most 15 characters) instead of an absolute path. Rules and searches that match a path (/usr/bin/bash) will not match alerts from this mode.

Fields in the alert

An alert with who-data contains the following data in the syscheck.audit section.

FieldContent
user.id, user.namewho started the process that changed the file
login_user.id, login_user.namethe user who opened the session, inherited by processes even when the identity changes
effective_user.id, effective_user.namethe effective user, after sudo it is root with ID 0
group.id, group.namethe group of the user who started the process
process.id, process.name, process.ppidID, name and parent ID of the process

In the example alert from the documentation, login_user is the user who logged in and used sudo, while user and effective_user are root. To identify the actor, read the login_user field, because user shows root. The same alert contains syscheck.mode equal to whodata, syscheck.event equal to modified, the changed_attributes list and the diff field with the content of the change.

Event classification

FIM events are classified by rules from the file 0015-ossec_rules.xml. Three of them handle files.

RuleLevelDescriptionDecoder
5507Integrity checksum changed.syscheck_integrity_changed
5537File deleted.syscheck_deleted
5545File added to the system.syscheck_new_entry

All belong to the groups syscheck and syscheck_file, and each has an extra group that depends on the event: syscheck_entry_modified, syscheck_entry_deleted or syscheck_entry_added. On Windows, registry keys are handled by rules 594, 597 and 598, all at level 5. You build custom rules on the syscheck group through if_group, as in part 6.

Central configuration through agent.conf

You can set syscheck locally in ossec.conf or remotely in agent.conf. The agent.conf file lives in the group directory on the server, for example /var/ossec/etc/shared/group1/agent.conf. The manager sends the agent the files from its group directories, and the agent stores them in /var/ossec/etc/shared. Every agent belongs to the default group unless set otherwise, and it can belong to several groups. With several groups the files are merged into one, and the last group on the list has the highest priority.

The agent_config block accepts the filters name, os and profile (sregex expressions, case insensitive and unanchored unless the pattern uses ^ or $). They let one group file hold separate settings for Linux and Windows.

The documentation describes a safe procedure for changing the file:

# Edit under a name that starts with a dot, the manager does not distribute such files
touch /var/ossec/etc/shared/example-linux/.agent.conf.tmp
chown wazuh:wazuh /var/ossec/etc/shared/example-linux/.agent.conf.tmp
chmod 660 /var/ossec/etc/shared/example-linux/.agent.conf.tmp

# Validate, then rename to activate
/var/ossec/bin/verify-agent-conf -f /var/ossec/etc/shared/example-linux/.agent.conf.tmp
mv /var/ossec/etc/shared/example-linux/.agent.conf.tmp /var/ossec/etc/shared/example-linux/agent.conf

The manager detects the new file on its own and distributes it to the group. With every keepalive (notify_time, by default 20 seconds) the agent sends the checksum of its merged.mg, and when the checksum differs the manager sends the newer file. The agent applies the configuration right away, with no manual restart. On Linux wazuh-agentd keeps its connection while the other daemons restart. On Windows the whole service restarts, so the agent looks disconnected for a few seconds.

You check the synchronization status in the API or with the tool:

/var/ossec/bin/agent_groups -S -i 001

A correct result is Agent '001' is synchronized., and in the API the group_config_status field has the value synced.

The order rule: ossec.conf is read before agent.conf, and the last value of any setting overrides the previous one. If the same file path appears in both files, both end up in the final configuration. You can cut an agent off from remote configuration by adding agent.remote_conf=0 to /var/ossec/etc/local_internal_options.conf.

Exercise 1: three modes in one group

Goal: the group example-linux has three directories, each in a different mode. /srv/example/apple runs in whodata mode with content reporting, /srv/example/orange in realtime mode, and /srv/example/pear only in the periodic scan. The file private.txt in the apple directory is an exception to content reporting.

Preparing the Linux agent. Audit mode needs the auditd daemon, so I install it if the agent lacks it:

apt -y install auditd
systemctl start auditd
auditctl -l | grep task
mkdir -p /srv/example/apple /srv/example/orange /srv/example/pear

The output of auditctl -l | grep task should be empty. If -a never,task appears, remove the entry as described above. Then on the manager I create the group and assign the agent:

/var/ossec/bin/agent_groups -a -g example-linux
/var/ossec/bin/agent_groups -a -i 001 -g example-linux

The group agent.conf content:

<agent_config>
  <syscheck>
    <disabled>no</disabled>
    <frequency>300</frequency>
    <directories check_all="yes" whodata="yes" report_changes="yes" tags="example,apple">/srv/example/apple</directories>
    <directories check_all="yes" realtime="yes">/srv/example/orange</directories>
    <directories check_all="yes">/srv/example/pear</directories>
    <nodiff>/srv/example/apple/private.txt</nodiff>
  </syscheck>
</agent_config>

The file goes through the procedure from the previous section (temporary file, verify-agent-conf, rename). After synchronization, auditctl -l | grep wazuh_fim shows the rule for the apple directory, in the form -w /srv/example/apple -p wa -k wazuh_fim.

Change test:

echo "one" > /srv/example/apple/date.txt
echo "one" > /srv/example/orange/date.txt
echo "one" > /srv/example/pear/date.txt
echo "two" >> /srv/example/apple/date.txt
rm /srv/example/orange/date.txt

The expected behavior follows from the modes. Events from the apple and orange directories appear immediately, as rule 554 for a new file, 550 for a change and 553 for a deletion. Events from the pear directory appear only after a scan, that is within 300 seconds. The alert from apple shows syscheck.mode equal to whodata, the syscheck.audit.* fields and syscheck.diff with the content of the change. Only whodata adds the syscheck.audit.* fields. The tag example,apple appears in alerts from the apple directory. In the dashboard the events are under Endpoint security > File Integrity Monitoring > Events.

The directory for realtime mode must exist before the agent restarts, which is why I run mkdir before distributing the file.

Exercise 2: change content and exclusions

report_changes copies every monitored file to a private location, in /var/ossec/queue/diff/* on Linux, so it uses disk space. That is why I turn off content reporting where a file may hold sensitive data. The nodiff option from exercise 1 makes a change to private.txt generate an alert without the content of the change. The documentation states that this protects against data leaking through alert content.

Check: append a line to /srv/example/apple/private.txt and to /srv/example/apple/date.txt. The alert for date.txt has the syscheck.diff field, the alert for private.txt does not.

There are two ways to silence a path. The first is the ignore element, which takes a path or an sregex expression:

<ignore>/etc/mtab</ignore>
<ignore type="sregex">.log$|.swp$</ignore>

The documentation stresses that ignored files are still scanned, but the results are not reported. The second way works on the server: a level 0 rule in the syscheck group. Level 0 alerts are silent and the server does not report them. An example for the file cache.tmp in the pear directory, saved on the manager in /var/ossec/etc/rules/fim_ignore.xml:

<group name="syscheck">
  <rule id="100210" level="0">
    <if_group>syscheck</if_group>
    <field name="file">/srv/example/pear/cache.tmp</field>
    <description>Ignore changes to $(file)</description>
  </rule>
</group>

After saving the file I restart the manager:

systemctl restart wazuh-manager

The practical difference: ignore works on the agent, so the event never reaches the server. The level 0 rule works on the server and silences a change only for chosen conditions, such as a path, without changing the agent configuration. The ID 100210 comes from the custom rule range 100000 to 120000 in part 6.

Exercise 3: maintenance window

Maintenance work changes monitored files, which produces alerts with no value. There are two complementary strategies, both based on groups.

The first is a fim-disabled group whose agent.conf turns the module off:

<agent_config>
  <syscheck>
    <disabled>yes</disabled>
  </syscheck>
</agent_config>

Before the maintenance window I assign the agent to the group, and afterwards I remove it from it:

/var/ossec/bin/agent_groups -a -g fim-disabled
/var/ossec/bin/agent_groups -a -i 001 -g fim-disabled
/var/ossec/bin/agent_groups -r -i 001 -g fim-disabled

The command -a -i 001 -g adds the group to the agent’s group list. In the documentation example, after successive additions the list reads default, group1, group2, so I assume fim-disabled lands at the end. The last group has the highest priority in the merged configuration, so its disabled setting wins over the settings from the other groups. I check the order with agent_groups -s -i 001. The command -a -f -i agent -g group would replace the whole list, so I do not use it in this exercise.

The second strategy is a maintenance group with a label:

<agent_config>
  <labels>
    <label key="state">maintenance</label>
  </labels>
</agent_config>

The label goes into the alerts of agents that belong to the group. The documentation describes the labels section as additional information about the agent included in alerts, also in e-mail messages, and keys separated by a period are nested in JSON alerts. The attribute hidden="yes" hides a label by default, and a key cannot start with an underscore, because such keys are reserved for system labels. After assigning the agent to the group I open its alert and check under which path the label field sits. Then a dashboard query excludes events from that period, for example not agent.labels.state: maintenance, provided the path in the alert is the same.

The strategies complement each other. The first disables the FIM module for the window, the second leaves the events in the index and the view filters them out. If you need a trace of changes from the maintenance window, choose the second.

Limits and constraints

  • file_limit limits the number of monitored files, by default 100000 entries. Files added after the limit is reached are ignored.
  • max_eps limits event throughput, by default 50, and 0 disables the limit.
  • max_files_per_second limits scan speed, by default 0, which means no limit.
  • process_priority sets the nice value of the syscheck process, by default 10, from -20 to 19.
  • auto_ignore works on the manager side and, with the default values, stops alerting on a file that has changed 10 times within 3600 seconds. Alerts for that file resume after 3600 seconds. The option is off by default and has no effect in the agent configuration.
  • skip_nfs, skip_dev, skip_proc and skip_sys by default skip network file systems (NFS and CIFS), /dev, /proc and /sys respectively.
  • scan_on_start is accepted but has no effect. Syscheck always runs a scan at startup to create the baseline, whatever the value.

Checklist after part 7

  • Directories used in realtime mode exist before the agent restarts.
  • auditctl -l | grep task returns no -a never,task entry.
  • The example-linux group has an agent.conf verified with verify-agent-conf and synchronized (agent_groups -S -i 001).
  • The alert from the apple directory has syscheck.mode equal to whodata and the syscheck.audit.* fields.
  • Events from the pear directory appear only after a periodic scan.
  • A change to private.txt has no syscheck.diff field.
  • Rule 100210 silences cache.tmp, and the file fim_ignore.xml passed xmllint.
  • After removal from the fim-disabled group the agent returns to its previous FIM configuration.

Part 8 moves on to the system inventory, vulnerability detection and rootkit detection.

What the documentation does not specify

  • The documentation does not state explicitly whether directories elements from agent.conf are added to the list from ossec.conf or replace it. The order rule speaks of overriding the last value of a setting and of merging file paths, but it does not name FIM directories.
  • The labels page does not give the name of the field under which a label appears in an alert. You have to read the path from a real alert.
  • The documentation does not describe what exactly happens to the agent FIM database after the agent is removed from a group with disabled set to yes and the module is enabled again.

Sources

↑↓ select Enter open Esc close