The Hashes Without Salt

Medium

· Database

Incident

SEV-1

BUG-LI-2012

Priority: Critical · reported by security

Time to fix: 20 min

Failing checks on the shipped build

  • two members with the same password get different hashes

    identical passwords must not produce identical stored values: expected a truthy value but got false

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

LinkedIn · 2012

Modelled on the LinkedIn password leak of June 2012. About 6.5 million LinkedIn password hashes were posted online. They were unsalted SHA-1: the same password always produced the same hash, so one precomputed guess cracked every account that shared it, and a fast hash made guessing cheap. LinkedIn confirmed the leak and moved to salted hashing; years later a far larger dump from the same breach surfaced.

passwords.py stores and checks member passwords. The storage policy in config.py has long said what it should be; the code never followed it.

Rewrite hash_password and verify_password to follow the policy.

Code map

import hashlib
import hmac

config = bug_require("./config.py")


def hash_password(password, rng):
    return hashlib.sha1(password.encode("utf-8")).hexdigest()


def verify_password(password, stored):
    candidate = hashlib.sha1(password.encode("utf-8")).hexdigest()
    return hmac.compare_digest(candidate, stored)

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Hashes Without Salt — Python Bug Hunt

Modelled on the LinkedIn password leak of June 2012. About 6.5 million LinkedIn password hashes were posted online.

  • Language: Python
  • Layer: Database
  • Difficulty: Medium
  • Concepts: Security, Hashing
  • Modelled on: LinkedIn · 2012
  • Visible tests: a stored password verifies and a wrong one does not; two members with the same password get different hashes
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-1 (priority: Critical)
  • Ticket: BUG-LI-2012
  • Reported by: security
  • Time to fix: 20 minutes, the target for a medium incident

Failing checks on the shipped build

  • two members with the same password get different hashes — identical passwords must not produce identical stored values: expected a truthy value but got false

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Modelled on the LinkedIn password leak of June 2012. About 6.5 million LinkedIn password hashes were posted online. They were unsalted SHA-1: the same password always produced the same hash, so one precomputed guess cracked every account that shared it, and a fast hash made guessing cheap. LinkedIn confirmed the leak and moved to salted hashing; years later a far larger dump from the same breach surfaced.

passwords.py stores and checks member passwords. The storage policy in config.py has long said what it should be; the code never followed it.

Rewrite hash_password and verify_password to follow the policy.

Bug report

BUG-LI-2012 · Priority: Critical · Reported by: security

hash_password(password, rng) returns "pbkdf2_sha256$<iterations>$<salt hex>$<hash hex>"

  • salt = rng(config.SALT_BYTES) — a fresh salt per call, from the injected rng
  • hash = hashlib.pbkdf2_hmac("sha256", password UTF-8, salt, config.ITERATIONS, config.KEY_BYTES)
  • hex is lowercase

verify_password(password, stored):

  • recomputes with the stored salt and iteration count, compares in constant time (hmac.compare_digest), returns True/False
  • anything not in the four-part format above (including a legacy bare SHA-1 hex digest) returns False — never raises

Observed: two members with the password "linkedin" have byte-identical stored hashes.

Logs

[audit] users.password_hash: 6.5M rows, 0 salts
[audit] top hash 7c4a8d09ca3762af61e59520943dc26494f8941b shared by many accounts

The code as shipped

src/accounts/passwords.py (editable)

import hashlib
import hmac

config = bug_require("./config.py")


def hash_password(password, rng):
    return hashlib.sha1(password.encode("utf-8")).hexdigest()


def verify_password(password, stored):
    candidate = hashlib.sha1(password.encode("utf-8")).hexdigest()
    return hmac.compare_digest(candidate, stored)

Read-only context: src/accounts/config.py.

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Python bug hunts.