Caloogy Article
中文

Hash Chain Audit Logs: Making Logs Impossible to Silently Alter

Date
Sep 8, 2026
Series
Infra
Reading
4 min
Topics
Audit logHash chainTamper evidentCertificate transparency

The one-sentence version

The first thing an intruder does after gaining access is often to delete or modify logs to cover their tracks. Hash chains make this "possible, but guaranteed to be detected": each record contains the hash of the previous record, so changing any single entry causes all subsequent record hashes to mismatch. By periodically saving the latest hash to a location an attacker cannot reach (an "anchor"), even "recalculating the entire chain" will be exposed. Blockchain, Git, and Certificate Transparency logs all use this concept.

This article has three layers:

  1. Layer 1: The Intuition: An interlocking ledger
  2. Layer 2: Real-World Use: Certificate Transparency, Git, Blockchain, Financial Auditing
  3. Layer 3: Principles and Engineering details

Layer 1: The Intuition

1.1 An Interlocking Ledger

Imagine a ledger where the footer of every page contains the "fingerprint of the previous page's content."

To change one page, you must change every subsequent page.

1.2 You Can Only Modify "Your Own Copy"

If you make a copy of the fingerprint of the last page every day and lock it in a drawer at home (a place an attacker cannot reach), then even if an attacker rewrites the entire ledger, the last page's fingerprint will not match the one in your drawer.

This is tamper-evident: it doesn't make tampering impossible, but it ensures that tampering will definitely be detected.


Layer 2: Real-World Use

System How it is used
Certificate Transparency (CT) In 2011, the Dutch CA DigiNotar was breached, and attackers issued a fake certificate for google.com. Subsequently, Google pushed for all certificates issued by CAs to be written to public, append-only logs (RFC 6962); browsers now require certificates to include proof of "inclusion in a log." Any certificate issued secretly by a CA will be exposed.
Git The ID of each commit is a hash of the "content + parent commit ID." Changing any commit in history changes the IDs of all subsequent commits, making it obvious to everyone.
Blockchain Each block header contains the hash of the previous block, plus proof of work or consensus, making the cost of tampering extremely high.
Financial and Compliance Auditing Banking and exchange operation logs require "non-repudiation and immutability"; hash chains combined with external evidence are a common implementation.
Key Transparency WhatsApp and iMessage write user public keys to auditable logs to prevent servers from secretly swapping keys (see "TOFU and Public Key Pinning").

Layer 3: Principles and Engineering

3.1 Construction

The iist record stores:

hi=SHA256(hi−1 ∥ index ∥ timestamp ∥ operator ∥ content),h0=0⋯0.h_i = \mathrm{SHA256}\big(h_{i-1} \,\|\, \text{index} \,\|\, \text{timestamp} \,\|\, \text{operator} \,\|\, \text{content}\big),\qquad h_0 = 0\cdots0.

During verification, you recalculate from the beginning; every entry must match the stored hih_i.

3.2 A worked example

Three records (for brevity, the formula uses hi=SHA256(hi−1 ∥ "|" ∥ event)h_i = \mathrm{SHA256}(h_{i-1} \,\|\, \texttt{"|"} \,\|\, \text{event}), showing only the first 8 digits)

# Event Previous Hash Current Hash
1 Login alice 00000000 a28ed04e
2 Bind address 0xAB.. a28ed04e 98992e28
3 Change notification settings 98992e28 8fe3ea53

An attacker changes the 2nd record to "Bind address 0xCD..":

# Event Previous Hash Recalculated Hash
1 Login alice 00000000 a28ed04e ✓
2 Bind address 0xCD.. a28ed04e 6ea0ec94 ✗ (stored is 98992e28)
3 Change notification settings 6ea0ec94 3ab27dfe ✗ (stored is 8fe3ea53)

Everything from the 2nd record onwards fails to match. Even if the attacker also modifies the stored hashes to the new values, making the last one 3ab27dfe, it will still be exposed because the external anchor records 8fe3ea53. (Verified with Python.)

3.3 External Anchors: The Most Critical Step

Implementing a hash chain within a single database only protects against "uninformed tampering"; it cannot protect against an attacker with full control over the database, who can recalculate all subsequent hashes starting from the point of tampering. Therefore, you must periodically save the latest hash to an independent location:

Anchor Location Description
Another machine / Personal computer An independent program pulls all logs daily, recalculates the hash chain using its own code, and compares it with previously stored anchors.
Emailing yourself Simple and effective; the email service provider is an independent third party.
Writing to a public chain The strongest method, but comes with costs.
Public disclosure The Certificate Transparency approach: logs are public to everyone, allowing anyone to audit them.

Note the phrase "recalculating using its own code": if the verification program also runs on the compromised server, the attacker can modify the verification program itself.

3.4 Database-Level Hardening

3.5 Merkle Trees: The Scalable Upgrade

To verify the nnth record in a hash chain, you must calculate from the 1st to the nnth record. Merkle Trees hash records in pairs, building up layer by layer until a single root hash is obtained. Proving "a specific record is in the log" only requires log⁡2n\log_2 n hash values. Certificate Transparency, transactions in Bitcoin blocks, and Ethereum states all use Merkle Trees.

3.6 Where Security Comes From

For an attacker to modify a record without being detected, they must find another piece of content whose hash exactly matches the original value—meaning they must break the second preimage resistance of the hash function. For SHA-256, this would require approximately 22562^{256} attempts (see "The Birthday Paradox and Hash Collisions").


Summary

Layer Key points
Intuition Record the fingerprint of the previous page on every page; changing one page requires changing all subsequent pages.
Key points External anchors + independent code verification to prevent "full chain recalculation."
Real-world use Certificate Transparency, Git, Blockchain, Audit Logs, Key Transparency.
Engineering details Append-only (triggers prohibit modification/deletion); do not log sensitive data; use Merkle Trees for large scales.