Databases rarely delete cleanly. A deleted SQLite row often survives in freelist pages or in the write-ahead log until a checkpoint overwrites it, and a changed row often survives in a transaction log, a backup, or an earlier dump. Database forensics is therefore recovery plus chronology: recover what the live database no longer shows, then order inserts, updates, and deletes by the timestamps and log sequence numbers that the database itself recorded. Scope: analyze database files you are authorized to handle, work on copies, and treat database contents as sensitive, because they often hold credentials, messages, and personal data.
Quick Workflow
- Inventory the database files: main file, journal or write-ahead log, backups, dumps, and server log files.
- Hash every file and work on copies, keeping the original set sealed.
- Identify the engine and version from the file header before opening it.
- Read the live schema and row counts, then check integrity.
- Recover deleted and historical rows from freelist pages, the write-ahead log, and transaction or binary logs.
- Reconstruct the change history: who or what changed which row, and when.
- Correlate database timestamps with host, application, and network timelines.
- Report each recovered row with its source page or log offset, so the claim can be reproduced.
1. SQLite identification and safe opening
# Identify the file, hash the full set, and copy it before opening
file /evidence/case01/app.db
sha256sum /evidence/case01/app.db /evidence/case01/app.db-wal /evidence/case01/app.db-shm 2>/dev/null
cp /evidence/case01/app.db /evidence/case01/work.db
cp /evidence/case01/app.db-wal /evidence/case01/work.db-wal 2>/dev/null
cp /evidence/case01/app.db-shm /evidence/case01/work.db-shm 2>/dev/null
# Open the copy read-only so the examination cannot checkpoint the WAL
sqlite3 "file:/evidence/case01/work.db?mode=ro" ".tables"
sqlite3 "file:/evidence/case01/work.db?mode=ro" ".schema messages"
sqlite3 "file:/evidence/case01/work.db?mode=ro" "PRAGMA integrity_check; PRAGMA journal_mode; PRAGMA page_count; PRAGMA freelist_count;"
Opening a SQLite database read-write can checkpoint the write-ahead log into the main file and destroy the very history you came to recover, so the read-only open is not optional. If the WAL file is present but the main file was copied without it, the copy is missing recent transactions, and that gap must be reported.
2. Live rows, deleted rows, and freelist recovery
# Live row counts and the newest and oldest timestamps per table
sqlite3 "file:/evidence/case01/work.db?mode=ro" \
"SELECT COUNT(*), MIN(created_at), MAX(created_at) FROM messages;"
sqlite3 "file:/evidence/case01/work.db?mode=ro" \
"SELECT id, sender, substr(body, 1, 80), created_at FROM messages ORDER BY created_at DESC LIMIT 20;"
# Dump the raw database file and search for strings that are not in live rows
strings -t d /evidence/case01/work.db | grep -i "deleted\|password\|token" | head -40
# Recover freelist and unallocated content with a Python page scan
python3 - <<'PY'
import sqlite3
con = sqlite3.connect("file:/evidence/case01/work.db?mode=ro", uri=True)
cur = con.cursor()
# dbstat is available in standard SQLite builds and reports page use
try:
for row in cur.execute("SELECT name, pageno, pagetype FROM dbstat ORDER BY pageno LIMIT 20"):
print(row)
except Exception as exc:
print("dbstat unavailable", exc)
con.close()
PY
A string found in the raw file is not yet a recovered row. It becomes evidence only when you can place it in a page, a WAL frame, or a log record and explain how it got there. Strings alone also produce false positives from old values, test data, and schema text.
3. Write-ahead log and WAL frame analysis
# WAL header: magic, page size, checkpoint sequence, and salt values
xxd -l 64 /evidence/case01/work.db-wal
# Frame headers show the commit sequence; list frames by offset
python3 - <<'PY'
import struct
with open("/evidence/case01/work.db-wal", "rb") as fh:
header = fh.read(32)
magic, fmt, page_size, ckpt, salt1, salt2, c1, c2 = struct.unpack(">8I", header)
print("page_size", page_size, "checkpoint", ckpt)
offset = 32
frame_no = 0
while True:
frame_header = fh.read(24)
if len(frame_header) < 24:
break
pgno, commit_size = struct.unpack(">2I", frame_header[:8])
fh.seek(page_size, 1)
frame_no += 1
offset += 24 + page_size
if commit_size:
print("frame", frame_no, "page", pgno, "commit_size", commit_size, "offset", offset)
PY
WAL frames are the transaction history in order, and a frame that commits a delete is the proof that the row existed and when it was removed, even after the main file no longer shows it. If the WAL was checkpointed before acquisition, that history is gone from the WAL, and recovery falls back to freelist pages, backups, and server logs.
4. PostgreSQL and MySQL server artifacts
# PostgreSQL: inspect the data directory copy, never the live server
ls /evidence/case01/pgdata/
grep -i "checkpoint\|recovery" /evidence/case01/pgdata/log/postgresql-*.log | tail -20
# Query a restored copy for row history where audit triggers exist
psql "dbname=case01_copy" -c "SELECT table_name, operation, changed_at, changed_by FROM audit_log ORDER BY changed_at DESC LIMIT 40;"
# MySQL binary logs: list events in a copied binlog, with timestamps
mysqlbinlog --base64-output=decode-rows -v /evidence/case01/mysql-bin.000042 | head -120
# MySQL: compare a dump against the live tables to find rows that vanished
mysqldump --no-data case01_copy > /evidence/case01/schema.sql
diff /evidence/case01/schema.sql /evidence/case01/schema_expected.sql | head -40
Server logs and binary logs are evidence in their own right, and they often outlive the rows they describe. A binary log event that deletes a row is stronger proof of the deletion than the absence of the row, which on its own could also mean the row never existed. Always restore server data to a separate copy and never run recovery commands against a live production database.
Edge cases
- The database is encrypted or uses SQLCipher. Without the key, the file is opaque, so pivot to memory artifacts, key material in application configuration, and server logs, and state the encryption limit plainly.
- The WAL file is missing or was already checkpointed. Freelist recovery and backups become the main sources, and recent transactions may be unrecoverable.
- Timestamps are stored as integers, text, or mixed formats in the same column. Normalize each value with its stored type before sorting, or the timeline will be wrong.
- A vacuum or reindex rewrote the file and destroyed freelist content. Check the file modification time and any backup taken before the vacuum.
- Row counts in a dump do not match the live database. Dumps are point-in-time snapshots, so compare dump time against transaction log times before calling the difference a deletion.
Key references
- NIST SP 800-86 (Guide to Integrating Forensic Techniques into Incident Response).
- SQLite file format documentation, including WAL format and freelist pages.
- PostgreSQL documentation on write-ahead logging and point-in-time recovery.
- MySQL documentation on binary logging and mysqlbinlog.
