Defining file check policies: what, and how, to monitor

Samhain
Prev | Chapter 5. Configuring samhain, the file monitor | Next

# 5.4. Defining file check policies: what, and how, to monitor

This section explains how to specify in the configuration file, which files or directories should be monitored, and which monitoring policy should be used.

## 5.4.1. Monitoring policies

samhain offers several pre-defined monitoring policies. Each of these policies has its own section in the configuration file. Placing a file in one of these sections will select the respective policy for that file.

The available policies (section headings) are:

ReadOnly

  All modifications except access times will be reported for these files.

  Checked: owner, group, permissions, file type, device number, hardlinks, links, inode, checksum, size, mtime, ctime.

LogFiles

  Modifications of timestamps, file size, and signature will be ignored.

  Checked: owner, group, permissions, file type, device number, hardlinks, links, inode.

GrowingLogFiles

  Modifications of timestamps, and signature will be ignored. Modification of the file size will only be ignored if the file size has increased.

  Checked: owner, group, permissions, file type, device number, hardlinks, links, inode, size >= previous_size, checksum(file start up tp previous size) equals previous checksum.

Attributes

  Only modifications of ownership, access permissions, and device number will be checked.

  Checked: owner, group, permissions, file type, device number.

IgnoreAll

  No modifications will be reported. However, the existence of the specified file or directory will still be checked.

IgnoreNone

  All modifications, including access time, but excluding ctime, will be reported - checking atime and ctime would require to play with the system clock.

  Checked: owner, group, permissions, file type, device number, hardlinks, links, inode, checksum, size, mtime, atime.

User0

  Initialized to: report all modifications.

User1

  Initialized to: report all modifications.

User2

  Initialized to: report all modifications.

User3

  Initialized to: report all modifications.

User4

  Initialized to: report all modifications.

Prelink

  Modifications of timestamps, size, and inode will be ignored Checksums will be verified by calling /usr/sbin/prelink --verify. This policy is intended for verification of prelinked executables/libraries and/or directories containing such files. For details and further configuration options see Section 5.4.8>.

  Checked: owner, group, permissions, file type, device number, hardlinks, links, checksum.

| Note
| Each policy can be modified in the config file section Misc with entries like RedefReadOnly=`+XXX[,...]` or RedefReadOnly=`-XXX[,...]` to add (+XXX) or remove (-XXX) a (a comma-separated list of) tests XXX, where XXX can be any of CHK (checksum), LNK (link), HLN (hardlink), INO (inode), USR (user), GRP (group), MTM (mtime), ATM (atime), CTM (ctime), SIZ (size), RDEV (device numbers), MOD (file mode), PRE (prelinked binary) and/or SGROW (file size is allowed to grow). This must come before any file policies are used in the config file.

## 5.4.2. File/directory specification

Entries for files have the following syntax:

file=`/full/path/to/the/file`

Entries for directories have the following syntax:

dir=`[recursion depth]/full/path/to/the/dir`

The specification of a (numerical) recursion depth is optional (see Section 5.4.5>). (Do not put the recursion depth in brackets -- they just indicate that this is an optional argument ...).

Wildcard patterns ('*', '?', '[...]') as in shell globbing are supported for paths. The leading '/' is mandatory.

| Note on directories
| A directory is (a) a collection of files, with (b) a directory special file where a listing of all files in the directories is kept. This directory special file will be modified in case of a file addition, removal, or renaming. Depending on the chosen policy, samhain will report on such modifications of the directory special file. The addition and/or deletion of files from a directory modifies the directory special file (mtime/ctime). The addition/deletion of subdirectories will also modify the number of hardlinks of the directory special file. A modification of a file may modify a directory special file (mtime/ctime), if this modification is done by first creating a temporary file, followed by renaming this temporary file to the original one.

### 5.4.2.1. Rules

1. For the file check, samhain does not follow symlinks. If the argument for a file=... directive is a symlink, then the symlink itself is checked, not the location it points to.

2. The argument for a dir=... directive must be a directory. Using a symlink to a directory as argument is incorrect.

3. Precedence is given to the most specific location in the filesystem regardless of the order listed in the config file. I.e.,

   - a policy for a specific file overrides the policy for its directory

   - a policy for a subdirectory overrides the policy for its parent directory

   - if a directory or file path are explicitly listed twice in two different policy sections, Samhain will print a warning and honor only the first stanza processed. "First matching rule wins." Note however that it is perfectly ok to list a directory both as file=/path and dir=/path (see next rules).

4. Checking a directory with dir=... will check both the content of the directory as well as the directory special file itself, honoring a local and global recursion depth, giving local preference.

5. Using a directory as argument for both a file=... and a dir=... directive will have the effect that

   - the file=... directive will override the dir=... directive for the directory special file itself,

   - while the dir=... directive remains in effect for the directory content.

6. The presence of a file=`/parent/subdir`, which is more specific of a path entry than that of the parent directory in another policy section with a "deeper" recursion depth as dir=`N/parent` will not prevent Samhain from descending into `/parent/subdir` and applying the higher level directory with the "deeper" recursion policy to the contents of `/parent/subdir` The more-specific rule will only apply to the directory special file and does not "truncate" the higher level policy in any way.

7. To determine if you config file syntax is working as expected, increase the verbosity of debugging when running samhain with "-t init" using "-p info" or even "-p debug".

Example 1: If you only want to check files in a directory, but not the directory inode itself, use:

    [ReadOnly]
    dir = /u01/oracle/archive00
    [IgnoreAll]
    file = /u01/oracle/archive00

    # Note: /u01/oracle/archive00/archive01.dbf -> archive99.dbf *should* be
    # mounted in the DB as a read-only tablespace and should never be
    # changed, however, the DBA thinks he's God and does not need to consult
    # with the Admin, so he may be adding new, deleting, or renaming the
    # DBFs using SQLPlus without consulting with the admin, so tell me about
    # changes to the files inside that we know about at Samhain INIT but
    # such as when he adds a file.

Example 2: If you want to monitor a directory, but not the dynamic contents inside it:

    [Attributes]
    file = /var/spool/mqueue
    file = /tmp
    [IgnoreAll]
    dir=-1/var/spool/mqueue
    dir=-1/tmp

Example 3: If you want to monitor a directory special file, while ensuring no files within are removed but not the actual attributes of those files:

    [Attributes]
    file = /root
    [IgnoreAll]
    dir=0/root

Thanks to Brian A. Seklecki for his effort to clarify these rules and provide examples.

## 5.4.3. Suppress messages about new/deleted files

If you want to suppress messages about the creation of certain files (e.g. rotated log files), you can use the options IgnoreAdded=`/fullpath/with_some_regex_inside` and/or IgnoreMissing=`/fullpath/with_some_regex_inside` (to be placed in the [Misc] section of the configuration files. If you want to add more regular expressions, you can use these options multiple times.

| Note
| The argument to IgnoreAdded and IgnoreMissing must be a regular expression that matches the full path.To test your regex before putting in samhain, you do something like this:
    # This regex matches all files added by logrotate (e.g: messages.1 or messages.2.gz, etc.)
    cd /var/log
    for file in *; do echo $file| egrep "(cron|ksyms|maillog|messages|rpmpkgs|secure|spooler|up2date|wmtp)\.[0-9](\.gz)?$" ; done
Once it's work this way, you can add it to your samhainrc file, but don't forget to add the full path. e.g:
    IgnoreAdded = /var/log/(cron|ksyms|maillog|messages|rpmpkgs|secure|spooler|up2date|wmtp)\.[0-9](\.gz)?$
This tip has been provided by jim at aegis hyphen corp dot org.

| Alternative
| If a directory is added to [Attributes] as a file=/dir, then only the directory special file is monitored for permissions/ownership. The advantage is that additions/removals of files to that subdirectory can happen without recourse, but the integrity of that directory is defended. Assuming the administrator doesn't want to get granular level of detail. Good for such directories as: `/var/mail` `/var/cron/tabs` `/var/tmp` `/tmp` This tip has been provided by Brian A. Seklecki

## 5.4.4. Dynamic database update (modified/disappeared/new files)

samhain reads the file signature database at startup and creates an in-memory copy. This in-memory copy is then dynamically updated to reflect changes in the file system.

I.e. for each modified/disappeared/new file you will receive an alarm, then the in-memory copy of the file signature database is updated, and you will only receive another alarm for that file if it is modified again (or disappears/appears again).

Note that the on-disk file signature database is not updated (if you have signed it, the daemon could not do that anyway). However, as long as the machine is not rebooted, there should be no need to update the on-disk file signature database.

If files disappear after initialization, you will get an error message with the severity specified for file access errors (except if the file is placed under the IgnoreAll policy, in which case a message of SeverityIgnoreAll — see Section 4.1.1> — is generated).

If new files appear in a monitored directory after initialization, you will get an error message with the severity specified for that directory's file policy (except if the file is placed under the IgnoreAll policy, in which case a message of SeverityIgnoreAll — see Section 4.1.1> — is generated).

The special treatment of files under the IgnoreAll policy allows to handle cases where a file might be deleted and/or recreated by the system more or less frequently.

## 5.4.5. Recursion depth(s)

Directories can be monitored up to a maximum recursion depth of 99 (i.e. 99 levels of subdirectories. The recursion depth actually used is defined in the following order of priority:

1. The recursion depth specified for that individual directory (Section 5.4>). As a special case, for directories with the policy IgnoreAll, the recursion depth should be set to 0, if you want to monitor (the existence of) the files within that directory, but to -1, if you do not want samhain to look into that directory.

2. The global default recursion depth specified in the configuration file. This is done in the configuration file section Misc with the entry SetRecursionLevel=`number`

3. The default recursion depth, which is zero.

## 5.4.6. Hardlink check

As of version 1.8.4, samhain will by default compare the number of hardlinks of a directory to the number of its subdirectories (including "." and ".."). Normally, these numbers should be equal. The idea here is that a (kernel) rootkit may hide a directory, but fail to "fix" the parent directory hardlink count (actually, I am not aware of any kernel rootkit that would care to fix the hardlink count of the parent directory). This is an experimental feature; if there are any problems, it can be disabled with the option UseHardlinkCheck=`no` in the [Misc] section of the configuration file.

Errors will be reported at the same severity as directory access errors option SeverityDirs=`severity` in section [EventSeverity]).

| MacOS X
| This feature is not supported on MacOS X (because the resource fork is implemented as an invisible directory, it modifies the parent directory hardlink count.)

### 5.4.6.1. Specify exceptions for the hardlink check

Some filesystems do not always follow the rule mentioned above (directory hardlink equals number of subdirectories). E.g. the root directory of reiserfs partitions generally seems to have two additional hardlinks. To account for such exceptions, you can specify exceptions with the option HardlinkOffset=`N`:`/path` in the [Misc] section of the configuration file. Here, N is the numerical offset (actual - expected hardlinks) for `/path`. For multiple exceptions, use this options multiple times (note that '/path N:/path2' would itself be a valid path, so using the option only once with multiple exceptions on the same line would be ambiguous).

| Note
| Please note that samhain will not check for an exception if the standard rule (offset = 0) is true for a directory. Thus it will not warn if a directory that once was exceptional is not anymore.

## 5.4.7. Check for weird filenames

Samhain checks for weird filenames (containing control/nonprintable characters, newlines or tabs) and warns about them at a severity level that is set with SeverityNames=`severity` in section [EventSeverity]. The rationale is: most of the time, such names are either the result of user errors, buggy scripts, or questionable activity.

If you want to add characters to the set of 'good' ones, you can do so with the option: AddOKChars=`N1, N2, ...` in the [Misc] section of the configuration file. Nn should be the unsigned byte value of the character(s) in hex (leading '0x': 0xNN), octal (leading zero: 0NNN), or decimal.

| UTF-8 filenames
| To specify that filenames are UTF-8 rather than ASCII, use FileNamesAreUTF8=yes. Samhain will check for invalid UTF-8 sequences, and for filenames ending with invisible characters.

| Tip
| This check will not be performed for files under the IgnoreAll policy. To completely disable this check, use AddOKChars=all.

## 5.4.8. Support for prelink

prelink is a tool available on modern Linux systems that can significantly reduce the startup time of applications. It does this by performing some of the work of the dynamic linker in advance. As this changes both executables and shared libraries, file integrity verification will fail unless prelink is supported, in particular as prelinking has to be redone if libraries are updated (so initializing the checksum database after prelinking may not be good enough).

The disadvantage is that prelinking modifies libraries and executables, and may need to be redone (potentially modifying all or many executables again) if a library is updated. This is a major problem for file integrity checkers.

Version 2.0 of samhain and later support prelink. To use this support, you need to place prelinked executables and libraries (or directories holding them) under the [Prelink] policy rather than under the (e.g.) [ReadOnly] policy. For all files under the [Prelink] policy, inode, size, and timestamps will be ignored (prelinking changes them). In addition, for ELF binaries under the [Prelink] policy, /usr/sbin/prelink --verify will be used to compute checksums (i.e. the checksum will be computed on the output of this command). For other files, checksums are computed as usual.

| Speed
| Obviously, invoking prelink results in a significant overhead, and slows down file integrity checking (tests indicate a factor of three - your mileage may vary).

| Verification failures (zero checksum)
| It seems that prelink --verify fails if the dependencies of a prelinked binary have changed. This results in a zero checksum, and can be fixed by re-prelinking the affected binary.

There are two configuration options in the [Misc] section that can are relevant for prelink support:

SetPrelinkPath=`fullpath` sets the path to the prelink executable. The default is `/usr/sbin/prelink`.

SetPrelinkChecksum=`checksum` sets the TIGER192 checksum for the prelink executable. You can compute this with samhain -H /usr/sbin/prelink (remove whitespace from the computed checksum). If the checksum is set, samhain will verify the prelink executable immediately before using it, otherwise prelink will be used without this special precaution.

## 5.4.9. SELinux attributes and Posix ACLs

| Note for users of SQL database logging
| You need to update the database scheme before using this feature, if you are upgrading from a version below 2.3.0. See Section 4.12.1> for details.

As of version 2.3, samhain supports checking and verifying of SELinux attributes and/or Posix ACLs, if the operating system supports these features. SELinux attributes are a Linux-specific feature, while Posix ACLs are supported by multiple oprerating systems.

These features will only get compiled if the required development environment is available on the host where samhain is compiled (e.g. on Debian Linux, packages libattr1-dev and libacl1-dev).

For backward compatibility, these features are disabled by default, even if they are compiled in. To enable them, use the configuration directives:

    [Misc]
    UseACLCheck = yes
    UseSelinuxCheck = yes

## 5.4.10. Codes in messages about reported files

As of version 1.8.2, reports about modified files include a short code in the message field to describe which properties have been modified. The codes are: 'C' for 'checksum', 'L' for (soft) 'link', 'D' for 'device number', 'I' for 'inode', 'H' for (number of) 'hardlinks', 'M' for 'mode', 'U' for 'user' (owner), 'G' for 'group' (owner), 'T' for 'time' (any), and finally 'S' for 'size'.

As an example, 'C--I----TS' would indicate that a file has been replaced by one with different checksum, inode, timestamp, and size, but (e.g.) same mode (type and access permissions) and same ownership.

Prev | Home | Next
File signatures | Up | Excluding files and/or subdirectories (All except …)