Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Recording changes with the Linux audit system

After this lesson: I can write an audit rule that records who changed a sensitive file.

60 min lesson13 commandsLinux administration lab: users, permissions, cron and systemd in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Who changed the sudo rules?

The Linux audit system is a part of the kernel that records chosen events, such as a file changing, and who caused them. The program auditd writes those records to a log.

On Monday the sudo rules on a shop's server are different. Three people have admin rights. Without an audit record nobody can say who changed them. Work only on your own lab copy.

By the end of this lesson

Note

I can write an audit rule that records who changed a sensitive file.

Key words

WordWhat it meansExample
auditdthe program that writes the kernel's audit records to a log
Watchan audit rule on one file or folder-w /etc/sudoers -p wa
Keya label on a rule, used to find its recordssudoers-change
auidthe audit user ID: the account that logged in, kept through sudo
ausearchthe tool that finds audit records by key, user or time

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Logs say what happened, audit says who

Ordinary logs are written by programs, and a program writes what it chooses. The audit system is in the kernel, so it sees every program that touches a watched file.

Each record carries two accounts. uid is who the program ran as, often root. auid is who logged in, and it stays the same through sudo. The auid answers "who did it".

Install auditd from Ubuntu's archive

Command
sudo apt-get install -y -qq auditd

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Is it running?

Command
systemctl is-active auditd

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Which rules are loaded?

Command
sudo auditctl -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A watch: a file, actions, a key

A watch rule has three parts. -w names the file. -p says which actions to record: r read, w write, x run and a a change of attributes, such as the mode. -k gives the records a key, a label you search by.

For the sudo rules, writes and attribute changes matter. Reads would fill the log.

Watch the sudo rules

Command
sudo auditctl -w /etc/sudoers -p wa -k sudoers-change

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

List the rules again

Command
sudo auditctl -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Change the file's time stamp

Command
sudo touch /etc/sudoers

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Find the records by key

Command
sudo ausearch -k sudoers-change --format text

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a rule added with auditctl stays. It lasts only until the machine restarts. A rule that must last goes in a file in /etc/audit/rules.d, which augenrules loads at every boot.

Write the rule in a file

Command
printf -- '-w /etc/sudoers -p wa -k sudoers-change\n-w /etc/passwd -p wa -k identity\n' | sudo tee /etc/audit/rules.d/50-hardening.rules

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Load every rules file now

Command
sudo augenrules --load

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Both rules are loaded

Command
sudo auditctl -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: rules worth having

RuleWhat it recordsWhy
-w /etc/sudoers -p wa -k sudoers-changeChanges to the sudo rulesWho gained admin rights
-w /etc/passwd -p wa -k identityAccounts made or changedA new account is a common foothold
-w /etc/ssh/sshd_config -p wa -k sshdChanges to SSH settingsA weakened door
-w /var/log/audit -p wa -k audit-logChanges to the audit logSomebody covering their tracks

The cells students fill in are left blank in this preview.

Make a throwaway account

Command
sudo useradd -M -s /usr/sbin/nologin auditdemo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What did the identity rule see?

Command
sudo ausearch -k identity --format text

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Remove the throwaway account

Command
sudo userdel auditdemo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read one raw record in Python

Code: python
line = 'type=SYSCALL arch=c000003e syscall=257 success=yes auid=1000 uid=0 comm="touch" key="sudoers-change"'

fields = {}
for part in line.split(" "):
    at = part.index("=")
    fields[part[:at]] = part[at + 1:].strip('"')

print("logged in as", fields["auid"])
print("acting as", fields["uid"])
print("program", fields["comm"], "key", fields["key"])
# Change auid=1000 to auid=1001 and press Run. Which line changes?

Make a free account to save your work and track your progress.

What did the records show?

Have a go

What did the records show?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: who changed what

A raw audit record is one line of name=value pairs split by spaces, such as type=SYSCALL auid=1000 uid=0 comm="touch" key="sudoers-change". Values may have double quotes round them; no value contains a space. Write fields(line), which returns a dictionary of every name and its value with the quotes taken off. Then write who_changed(lines, key, names). Look only at records whose type is SYSCALL and whose key is key. Return a dictionary from each person to how many such records they caused, where a person is names[auid], or the auid itself if it is not in names, or "unset" if the auid is 4294967295.

Code: python
def fields(line):
    # Split on spaces, then on the first = only.
    return {}


def who_changed(lines, key, names):
    # Count SYSCALL records with this key, by the account that logged in.
    return {}

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.