30.2 Intrusion Detection Systems (IDS)
mtree generates directory checksum specifications using a seed value, which can be compared afterwards to identify file changes. This section covers the complete workflow of specification creation, simulated tampering, and verification.
Intrusion Detection System (IDS)
Verifying the integrity of system files and binaries is an important means for system administrators and security teams to stay informed of changes. Any software capable of monitoring system changes is collectively referred to as an Intrusion Detection System (IDS).
FreeBSD natively includes a basic IDS — mtree. However, while nightly security emails report changes, the information is stored locally, and a malicious user may still have the opportunity to tamper with records to cover their actions. Therefore, it is advisable to create a separate set of binary signatures and place them in a read-only directory owned by root; storing them on a removable USB disk or a remote server is even better.
It is recommended to run freebsd-update IDS after each update. Currently, this command is incompatible with PkgBase, producing the following error:
freebsd-update is incompatible with the use of packaged base. Please see
https://wiki.freebsd.org/PkgBase for more information.Generating a Specification File
The built-in mtree tool can generate a specification of directory contents. Specification generation relies on a seed value (a numeric constant), which is also required for subsequent checks, to determine whether files or binaries have been modified. Without knowledge of the seed value, it is extremely difficult or even infeasible for an attacker to forge or match checksums.
It is recommended to generate specifications for directories containing binaries and configuration files, as well as all directories containing sensitive data. Common targets include /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /etc, and /usr/local/etc.
The following example generates a set of SHA-512 hash values for each system binary under /bin and stores them in a hidden file /home/ykla/.bin_chksum_mtree in the user's home directory:
# mtree -s 123456789 -c -K cksum,sha512 -p /bin > /home/ykla/.bin_chksum_mtreeTip
The
123456789and/home/yklain the above example are placeholders and must be replaced with actual values.
123456789 represents the seed value, which should be chosen randomly. The seed value should be memorized and must not be disclosed.
The output should resemble the following:
At the same time, care should be taken to prevent malicious users from obtaining the seed value and checksum output.
Specification File Structure
The mtree format describes a collection of file system objects, typically used to create or verify directory hierarchies.
An mtree file consists of a series of lines, each describing one file system object (mtree always ignores leading whitespace).
FreeBSD system specifications are stored in the /etc/mtree directory.
The specification file created earlier can illustrate its format and content:
Content explanation:
①: The
/setspecial command defines partial settings extracted from the analysis file.②: References the parsed directory, indicating its type, mode, hard link count, and UNIX-format modification time, among other information.
③: References a file, showing its size, time, and a series of integrity checksum hash values.
Verifying the Specification File
To verify whether binaries have changed, compare the current directory contents against the previous specification and write the results to a file.
This command requires the same seed value used to generate the original specification:
This command should output the same /bin checksum as when the specification was created.
If no binaries in the directory have changed, /home/ykla/.bin_chksum_output will be an empty file.
To simulate a change, first use touch to modify the timestamp of /bin/cat, then run the verification command:
Run the verification command again:
Then check the output content of the /home/ykla/.bin_chksum_output file:
The output should resemble the following:
The above is an example of command output showing the situation after metadata modification.
Exercises
Use
mtreeto create an integrity baseline for the /etc directory, then manually modify /etc/motd and add a user, and runmtreeagain to detect changes. Map each detection output to the corresponding modification operation and explain the meaning of each detection record.Consult the documentation for AIDE (Advanced Intrusion Detection Environment), compare the differences in features between
mtreeand AIDE, and explain why AIDE is typically chosen over the system's built-inmtreein production environments.Design an experiment: install a rootkit demonstration tool (such as
chkrootkit), run it and read its detection logic, and analyze based on IDS principles why integrity checking tools can or cannot detect kernel-level rootkits.
Last updated