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

Free lesson

Spotting changed files with an integrity baseline

After this lesson: I can build a file integrity baseline and find what changed since.

60 min lesson14 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

One extra line in a web page

File integrity monitoring means recording what important files look like, then checking them later to find anything changed, added or removed. It cannot stop a change, but it tells you one happened.

A shop's web page gains one hidden line that sends card numbers away. It looks the same to every visitor. A baseline taken last week shows the change in seconds. In this lesson you play the intruder on your own lab copy only.

By the end of this lesson

Note

I can build a file integrity baseline and find what changed since.

Key words

WordWhat it meansExample
Hasha short fingerprint of a file that changes if one byte changes
Baselinea record of files when they were known to be good
Integritybeing unchanged by anybody without the right to change it
AIDEa free tool that records and checks files' hashes, modes and owners
Attributea fact about a file other than its contentits mode

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

A hash shows that content changed

A hash such as SHA-256 turns a file into a short fingerprint. Change one byte and the fingerprint is completely different.

So a list of hashes taken when the files were good is a baseline. Hash them again later and compare. Different means changed.

Make a practice web folder

Command
sudo mkdir -p /srv/site

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

Write a page in it

Command
echo '<h1>Shop</h1>' | sudo tee /srv/site/index.html

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

Take a hash baseline

Command
sha256sum /srv/site/index.html | sudo tee /root/site.sha256

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

Play the intruder: add one line

Command
echo '<script src="/x.js"></script>' | sudo tee -a /srv/site/index.html

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

Check against the baseline

Command
sudo sha256sum -c /root/site.sha256

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

AIDE checks more than content

A hash list misses two things: a new file it never listed, and a change of mode or owner. AIDE records the hash and every attribute you ask for, and reports files added, removed and changed.

You give it a configuration: where its database is, and which folders to watch with which attributes. Ours watches only /srv/site, so it runs in a moment.

Install AIDE without the mail extras

Command
sudo apt-get install -y -qq --no-install-recommends aide

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

Write a small configuration

Command
printf 'database_in=file:/var/lib/aide-lab/aide.db\ndatabase_out=file:/var/lib/aide-lab/aide.db.new\n/srv/site p+u+g+s+sha256\n' > aide-lab.conf

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

Worked example: the configuration

LineWhat it means
database_in=file:/var/lib/aide-lab/aide.dbThe baseline AIDE checks against
database_out=file:/var/lib/aide-lab/aide.db.newWhere a new baseline is written
/srv/site p+u+g+s+sha256Watch this folder: mode, owner, group, size and hash

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

Make the database folder

Command
sudo mkdir -p /var/lib/aide-lab

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

Build the baseline

Command
sudo aide --config=aide-lab.conf --init

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

Make the new database the baseline

Command
sudo mv /var/lib/aide-lab/aide.db.new /var/lib/aide-lab/aide.db

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

Check with nothing changed

Command
sudo aide --config=aide-lab.conf --check

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

Intruder: let everybody write the page

Command
sudo chmod 666 /srv/site/index.html

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

Intruder: drop in a new file

Command
echo 'test' | sudo tee /srv/site/shell.php

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

Check again

Command
sudo aide --config=aide-lab.conf --check

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

A common mistake

Warning

Many people think integrity monitoring stops changes. It only finds them, after they happen. And an intruder with root can rewrite the baseline too, so keep a copy where the server cannot write, such as another machine.

Compare two baselines in Python

Code: python
old = {"/srv/site/index.html": "a1f3", "/srv/site/app.js": "77c0"}
new = {"/srv/site/index.html": "9be2", "/srv/site/app.js": "77c0", "/srv/site/shell.php": "5d41"}

for path in new:
    if path not in old:
        print("added", path)
    elif new[path] != old[path]:
        print("changed", path)
for path in old:
    if path not in new:
        print("removed", path)
# Delete app.js from new and press Run again.

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

Assignment: compare two baselines

A baseline is a dictionary from a path to a list [mode, owner, hash], such as "/srv/site/index.html": ["644", "root", "a1f3"]. Write changed_parts(before, after), which takes two such lists and returns the names of what differs, in the order "mode", "owner", "hash". Then write report(old, new, ignore). It returns lines, sorted alphabetically: "added <path>" for a path only in new, "removed <path>" for one only in old, and "changed <path>: <parts>" with the parts joined by ", ". Skip every path that starts with one of the prefixes in ignore.

Code: python
def changed_parts(before, after):
    # Compare the mode, then the owner, then the hash.
    return []


def report(old, new, ignore):
    # Leave out paths under an ignored prefix.
    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.