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
| Word | What it means | Example |
|---|---|---|
| auditd | the program that writes the kernel's audit records to a log | |
| Watch | an audit rule on one file or folder | -w /etc/sudoers -p wa |
| Key | a label on a rule, used to find its records | sudoers-change |
| auid | the audit user ID: the account that logged in, kept through sudo | |
| ausearch | the 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 questionsLogs 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
sudo apt-get install -y -qq auditdShown here, not run. Press Start lab to type it into your own lab’s terminal.
Is it running?
systemctl is-active auditdShown here, not run. Press Start lab to type it into your own lab’s terminal.
Which rules are loaded?
sudo auditctl -lShown 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
sudo auditctl -w /etc/sudoers -p wa -k sudoers-changeShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the rules again
sudo auditctl -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Change the file's time stamp
sudo touch /etc/sudoersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Find the records by key
sudo ausearch -k sudoers-change --format textShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.rulesShown here, not run. Press Start lab to type it into your own lab’s terminal.
Load every rules file now
sudo augenrules --loadShown here, not run. Press Start lab to type it into your own lab’s terminal.
Both rules are loaded
sudo auditctl -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: rules worth having
| Rule | What it records | Why |
|---|---|---|
| -w /etc/sudoers -p wa -k sudoers-change | Changes to the sudo rules | Who gained admin rights |
| -w /etc/passwd -p wa -k identity | Accounts made or changed | A new account is a common foothold |
| -w /etc/ssh/sshd_config -p wa -k sshd | Changes to SSH settings | A weakened door |
| -w /var/log/audit -p wa -k audit-log | Changes to the audit log | Somebody covering their tracks |
The cells students fill in are left blank in this preview.
Make a throwaway account
sudo useradd -M -s /usr/sbin/nologin auditdemoShown here, not run. Press Start lab to type it into your own lab’s terminal.
What did the identity rule see?
sudo ausearch -k identity --format textShown here, not run. Press Start lab to type it into your own lab’s terminal.
Remove the throwaway account
sudo userdel auditdemoShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read one raw record in 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 questionsAssignment: 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.
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