Syntax
Resources
Overview
The linux audit framework is a collection of tools used to log events the administrator deems important; typically used to collect security-relevant information. It is not a form of protection against attacks, but simply a means of logging information to analyze after the fact.
Linux audit framework can be used in larger security pipelines, feeding information to scripts and dashboards to catch any potential weaknesses in a systems security.
It does this by listening to events reported by the kernel and logging them in a file.
[!tip]
The log file is typically found here:/var/log/auditd.log
.
Linux audit framework is broken down into a few parts:
- Audit kernel module - included in most linux kernels (some custom kernels may require additional steps to include).
- Auditd - A configurable daemon responsible for writing messages to the
log file. Configuration is done in the
/etc/audit/auditd.conf
file. - Command-line tools - various command line tools to interface with the
audit system. examples:
auditctl
: Interacting with the daemons configuration on the fly.ausearch
: Searching for specific events.aureport
: Generating reports.autrace
: Tracing processes.- There are many more tools that can be used to interact with the audit system.
- Audit rules - A collection of
auditctl
command that are run at system boot time. Configured in the file:/etc/audit/audit.rules
.
Installation
Although the kernel module is likely included with your distribution; you may need to install the relevant packages to interface with it. For this example I will be showing how to enable the linux audit system on NixOS.
nixpkgs
includes a module that be used to enable the linux audit system:
# configuration.nix
{
security.audit = {
enable = true;
};
}
You will also want to enable the audit daemon:
# configuration.nix
{
security = {
audit = {
enable = true;
};
auditd.enable = true;
};
}
Sources for these modules:
[!info]
Use the kernel parameteraudit=1
to allow the audit system to audit processes that are run before the audit daemon starts.This is set by default if you enable the NixOS module.
Configuration
The auditctl
command can be used to set and retrieve configuration settings,
changes made this way are ephemeral and will removed when the system restarts.
For permanent changes, configuration is done in the /etc/audit/auditd.conf
file. Configuration options are structured as such:
{conf}keyword=value
Example configuration option:
/var/log/auditd.log
In most cases you can leave the default configuration.
For a list of all available configuration options, refer to the auditd.conf(5) man page.
Rules
[!warning]
The output from the audit system can be very verbose; filling up the log file very quickly. Make sure to test all rules before deployment.
The auditctl
command can also be used for setting rules; these rules are
definitions for what events we want to log & configuration for the kernel module
itself. Configuring the audit system with the auditctl
command is typically
used for ad hoc changes, as these modifications are not automatically saved to a
permanent configuration file. Changes made with auditctl
are only active for
the current session and will be lost upon system restart.
For persistent rules, we use the /etc/audit/audit.rules
file or files in the
/etc/audit/rules.d/
directory.
Rules in auditd are broken up into 3 varieties:
Control
These are commands that are used to configure the audit system (kernel module) directly.
For a full list of control rules see auditctl(8) man page.
Some available persistent options:
Failure mode
-f
:
This is used for defining what action to take when the a critical error
(failure mode) is detected. the available options are:
0
- silent.1
- printk (print a failure message).2
- panic (halt the system).
Example:-f 1
print message when a critical error occurs.
Buffer Size
-b
:
Set the maximum number of audit system buffers in the kernel.
Example: -b 8192
Sets the maximum number of buffers to 8192, exceeding this
number will trigger a critical error.
Enable flag
-e
:
Set the enable flag. Available options:
0
- Disables auditing.1
- Enables Auditing.2
- Locks the configuration file preventing any further changes.
Example:-e 2
Enables the auditing and locks the configuration file.
Rate
-r
:
Set the message/sec limit, if set to 0
, disable rate limiting. If the rate is
exceeded a critical error will be triggered.
Example: -r 60
sets the rate limit to 60 messages/sec.
Delete
-D
:
Deletes all rules and watches.
File System
Otherwise known as watches, the -w
flag can be used to audit access to files
and directories.
Example: -w path/to/file -p permissions -k keyname
Paths
Paths can either be a file or a directory. If a directory is defined, then the rule is used recursively down the directory tree excluding any directories that may be mount points. Keep this in mind as auditing a large tree may be resource intensive. Limiting the scope of your rules is key to optimizing performance.
Permissions
The -p
option is for defining what permissions access type will trigger on.
Available permissions:
r
- read of the filew
- write to the filex
- execute the filea
- change in the file's attribute
These options can be combined e.g: -p rw
or -p rwa
.
Key
The -k
option is used to set a string as an identifier (key) for the rule.
This string is limited to 31 bytes long.
Typically used to group related rules to then be searched for with ausearch
.
Example
# /etc/audit/auditd.rules
-w /etc/localtime -p wa -k system_changes
-w /etc/passwd -p x -k password_changes
-w /usr/bin/passwd -p x -k password_changes
In this example we set a few rules:
-w /etc/localtime -p wa -k system_changes
here we define a rule that tracks when thelocaltime
file is either written to (w
), or had an attribute changed (a
). We then assign it thesystem_changes
key as a unique identifier.-w /etc/passwd -p x -k password_changes
&-w /usr/bin/passwd -p x -k password_changes
here we are tracking when either of these files are executed (x
), and assigning them both thepassword_changes
key.
System Calls
System call rules are for tracking kernel syscalls. It does this by loading rules into a matching engine that checks every syscall that all programs make on a system.
Example structure:
-a action,list -S syscall -F field=value -k keyname