# Database Forensics

> Use when examining a database file, database dump, or database server data directory during an authorized forensic investigation. Covers SQLite databases and write-ahead logs, deleted and freelist records, PostgreSQL and MySQL data recovery, transaction log and binary log analysis, and proving what rows existed, changed, or were deleted and when. Use when the evidence lives in an application database, when a record was deleted from the live view, or when database logs contradict the current table contents.

- Skill: `mizazhaider-ceh/database-forensics` (Agent Skill)
- Install (CLI): `npx get-skill add mizazhaider-ceh/database-forensics`
- Raw SKILL.md: https://api.skillmd.com/api/skills/mizazhaider-ceh/database-forensics/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: mizazhaider-ceh (https://skillmd.com/u/mizazhaider-ceh)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/mizazhaider-ceh/database-forensics

---


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

1. Inventory the database files: main file, journal or write-ahead log,
   backups, dumps, and server log files.
2. Hash every file and work on copies, keeping the original set sealed.
3. Identify the engine and version from the file header before opening it.
4. Read the live schema and row counts, then check integrity.
5. Recover deleted and historical rows from freelist pages, the
   write-ahead log, and transaction or binary logs.
6. Reconstruct the change history: who or what changed which row, and when.
7. Correlate database timestamps with host, application, and network
   timelines.
8. Report each recovered row with its source page or log offset, so the
   claim can be reproduced.

## 1. SQLite identification and safe opening

```bash
# 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

```bash
# 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

```bash
# 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

```bash
# 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.

