The backup went first
A school had nightly backups on a server in its own network. The attackers found the backup server on day two and wiped it.
A week later they locked the files. The backups were already gone. A backup is only safe if the attacker cannot change it.
By the end of this lesson
Note
I can keep a backup that the account that made it cannot delete.
Key words
| Word | What it means |
|---|---|
| Immutable | cannot be changed or deleted |
| Object lock | a store refusing deletes until a date |
| Retention | how long a copy must be kept |
| Append-only | new data may be added, never removed |
| Snapshot | a read-only picture of a disk at one moment |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe one rule
Important
The drill runs only in your own lab copy, on sample files and backups it made itself. Never run anything like it on a real machine without written permission. Unauthorised access to a computer, or changing its data, is an offence under the Computer Misuse Act 1990.
What the account can write, the attack deletes
Ransomware runs with the rights of the account it stole. Your backups folder belongs to your account, so the attack can delete it.
This time the drill is given the backups folder too. It deletes the archives there, as real attacks delete the backups they find.
Check lesson 1's scripts are here
cd ~/rr && ls mkshare.py manifest.py drill.py compare.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build a fresh share and a full backup
rm -rf share && python3 mkshare.py share && python3 manifest.py share > before.txt && rm -rf backups && mkdir -p backups && tar --listed-incremental=backups/share.snar -czf backups/share-day1-full.tar.gz share && python3 manifest.py share > backups/share-day1.manifest && ls backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the drill on share and backups
python3 drill.py share backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
See what is left of the backups
ls backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAn immutable copy is held by someone else
An immutable copy cannot be changed or deleted, even by the account that made it. Cloud storage offers object lock: a delete is refused until the retention date.
Other ways are append-only storage, read-only snapshots and tapes kept offline. The vault here is a small append-only store, run by its own account, with a 30-day lock.
Write the vault, part 1
printf '%s\n' 'import datetime, http.server, json, os, sys' 'STORE, DAYS = sys.argv[1], 30' 'LOCKS = os.path.join(STORE, ".locks.json")' 'def locks():' ' return json.load(open(LOCKS)) if os.path.exists(LOCKS) else {}' 'class Vault(http.server.BaseHTTPRequestHandler):' ' def answer(self, code, text, body=None):' ' data = body if body is not None else (text + chr(10)).encode()' ' self.send_response(code)' ' self.send_header("Content-Length", str(len(data)))' ' self.end_headers()' ' self.wfile.write(data)' ' def name(self):' ' name = self.path.strip("/")' ' return name if name and "/" not in name and not name.startswith(".") else None' ' def do_GET(self):' ' name, held = self.name(), locks()' ' if not name:' ' return self.answer(200, chr(10).join(f"{n} locked until {held[n]}" for n in sorted(held)) or "the vault is empty")' ' if name not in held:' > vault.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the vault, part 2
printf '%s\n' ' return self.answer(404, "not found: " + name)' ' self.answer(200, "", open(os.path.join(STORE, name), "rb").read())' ' def do_PUT(self):' ' name, held = self.name(), locks()' ' if not name:' ' return self.answer(400, "refused: give the file a name")' ' if name in held:' ' return self.answer(409, f"refused: {name} exists and is locked until {held[name]}")' ' data = self.rfile.read(int(self.headers["Content-Length"]))' ' open(os.path.join(STORE, name), "wb").write(data)' ' held[name] = str(datetime.date.today() + datetime.timedelta(days=DAYS))' ' json.dump(held, open(LOCKS, "w"))' ' self.answer(201, f"stored {name}, {len(data)} bytes, locked until {held[name]}")' ' def do_DELETE(self):' ' name, held = self.name(), locks()' ' if name not in held:' ' return self.answer(404, "not found: " + str(name))' >> vault.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the vault, part 3
printf '%s\n' ' if str(datetime.date.today()) < held[name]:' ' return self.answer(403, f"refused: {name} is locked until {held[name]}")' ' os.remove(os.path.join(STORE, name))' ' del held[name]' ' json.dump(held, open(LOCKS, "w"))' ' self.answer(200, "deleted " + name)' ' def log_message(self, form, *args):' ' print(self.command, self.path, args[1], flush=True)' 'http.server.HTTPServer(("127.0.0.1", 8730), Vault).serve_forever()' >> vault.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Give the vault its own account and folder
id vault > /dev/null 2>&1 || sudo useradd --system --no-create-home --shell /usr/sbin/nologin vault; sudo install -d -o vault -m 700 /srv/vault && sudo install -D -m 755 vault.py /opt/rr/vault.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start the vault as its own account
pgrep -f /opt/rr/vault.py > /dev/null || (nohup sudo -u vault python3 /opt/rr/vault.py /srv/vault >> /tmp/vault.log 2>&1 &); sleep 1; curl -s http://127.0.0.1:8730/Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the share and a full backup again
rm -rf share && python3 mkshare.py share && python3 manifest.py share > before.txt && rm -rf backups && mkdir -p backups && tar --listed-incremental=backups/share.snar -czf backups/share-day1-full.tar.gz share && python3 manifest.py share > backups/share-day1.manifest && ls backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Send the backup and manifest to the vault
curl -s -T backups/share-day1-full.tar.gz http://127.0.0.1:8730/ && curl -s -T backups/share-day1.manifest http://127.0.0.1:8730/Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to overwrite the backup in the vault
curl -s -T backups/share-day1-full.tar.gz http://127.0.0.1:8730/Shown 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 an immutable copy is safe from everyone. It is safe only from accounts that do not run the store. Root on this VM could stop the vault. So a real locked copy sits with a different provider or system, under admin accounts the attacker never gets.
Run the drill on both again
python3 drill.py share backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to delete the backup from the vault
curl -s -X DELETE http://127.0.0.1:8730/share-day1-full.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try the vault's folder, then read its log
ls /srv/vault; tail -n 4 /tmp/vault.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Fetch the backup and check it
rm -rf check && mkdir check && curl -s -o check/got.tar.gz http://127.0.0.1:8730/share-day1-full.tar.gz && tar -xzf check/got.tar.gz -C check && python3 compare.py before.txt check/shareShown here, not run. Press Start lab to type it into your own lab’s terminal.
The fix and how to detect it
The fix is a locked copy held by another account, ideally on another system and site. Its retention must be longer than an attacker usually hides, often weeks.
Detect attempts to delete backups: the vault's log records every refused DELETE. A delete of backups at night is an alarm in itself.
Try it: when may a backup be deleted?
import datetime
stored = datetime.date(2026, 11, 18)
until = stored + datetime.timedelta(days=30)
for today in [datetime.date(2026, 11, 20), datetime.date(2026, 12, 17), datetime.date(2026, 12, 18)]:
print(today, "delete refused" if today < until else "delete allowed")
# Change days=30 to days=7, then press Run.
Make a free account to save your work and track your progress.
Assignment: apply an object lock
Dates are strings written "YYYY-MM-DD".
lock_until(stored, days) returns the date days after the date stored.
may_delete(until, today) returns True when today is on or after until. An until of None means the copy has no lock, so it may be deleted.
survivors(backups, today) takes a list of dictionaries with "name" and "until". An attacker deletes every copy that may be deleted today. Return the sorted names that are left.
def lock_until(stored, days):
return stored
def may_delete(until, today):
return True
def survivors(backups, today):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go