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
| Word | What it means | Example |
|---|---|---|
| Hash | a short fingerprint of a file that changes if one byte changes | |
| Baseline | a record of files when they were known to be good | |
| Integrity | being unchanged by anybody without the right to change it | |
| AIDE | a free tool that records and checks files' hashes, modes and owners | |
| Attribute | a fact about a file other than its content | its mode |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsA 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
sudo mkdir -p /srv/siteShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a page in it
echo '<h1>Shop</h1>' | sudo tee /srv/site/index.htmlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Take a hash baseline
sha256sum /srv/site/index.html | sudo tee /root/site.sha256Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Play the intruder: add one line
echo '<script src="/x.js"></script>' | sudo tee -a /srv/site/index.htmlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check against the baseline
sudo sha256sum -c /root/site.sha256Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAIDE 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
sudo apt-get install -y -qq --no-install-recommends aideShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a small configuration
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.confShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the configuration
| Line | What it means |
|---|---|
| database_in=file:/var/lib/aide-lab/aide.db | The baseline AIDE checks against |
| database_out=file:/var/lib/aide-lab/aide.db.new | Where a new baseline is written |
| /srv/site p+u+g+s+sha256 | Watch this folder: mode, owner, group, size and hash |
The cells students fill in are left blank in this preview.
Make the database folder
sudo mkdir -p /var/lib/aide-labShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the baseline
sudo aide --config=aide-lab.conf --initShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the new database the baseline
sudo mv /var/lib/aide-lab/aide.db.new /var/lib/aide-lab/aide.dbShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check with nothing changed
sudo aide --config=aide-lab.conf --checkShown here, not run. Press Start lab to type it into your own lab’s terminal.
Intruder: let everybody write the page
sudo chmod 666 /srv/site/index.htmlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Intruder: drop in a new file
echo 'test' | sudo tee /srv/site/shell.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check again
sudo aide --config=aide-lab.conf --checkShown 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
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.
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