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

Free lesson

Backups an attacker cannot delete

After this lesson: I can keep a backup that the account that made it cannot delete.

65 min lesson16 commandsIncident response and forensics lab: investigate a compromised server 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

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

WordWhat it means
Immutablecannot be changed or deleted
Object locka store refusing deletes until a date
Retentionhow long a copy must be kept
Append-onlynew data may be added, never removed
Snapshota 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 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

The 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

Command
cd ~/rr && ls mkshare.py manifest.py drill.py compare.py

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

Build a fresh share and a full backup

Command
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 backups

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

Run the drill on share and backups

Command
python3 drill.py share backups

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

See what is left of the backups

Command
ls backups

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

An 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

Command
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.py

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

Write the vault, part 2

Command
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.py

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

Write the vault, part 3

Command
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.py

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

Give the vault its own account and folder

Command
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.py

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

Start the vault as its own account

Command
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

Command
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 backups

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

Send the backup and manifest to the vault

Command
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

Command
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 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 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

Command
python3 drill.py share backups

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

Try to delete the backup from the vault

Command
curl -s -X DELETE http://127.0.0.1:8730/share-day1-full.tar.gz

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

Try the vault's folder, then read its log

Command
ls /srv/vault; tail -n 4 /tmp/vault.log

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

Fetch the backup and check it

Command
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/share

Shown 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?

Code: python
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.

Code: python
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

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.