Skip to content
Tutorials Center
My learning

Topics

Lesson 70 of 80 ยท Projects

Project: Password Audit Tool

Build a password audit tool in Python: find weak, reused and breached passwords in a fake credential list, store scrypt hashes and report with pandas.

Imagine you have just joined a small company that is retiring an old internal app. Its user table stores passwords in plain text, which is exactly the situation Password Storage Done Right warns about. Your job has two parts: audit the existing passwords so you know how much risk you are carrying, and migrate the accounts to properly salted, slow hashes so the plain-text column can be deleted.

In this project you will build a small audit tool in three steps. It checks every password for weakness, reuse and presence in a breach list, writes a report that never prints a password, and stores the survivors as salted scrypt hashes. Everything uses invented users and invented passwords. Never run a tool like this against real credentials you are not authorized to handle, and in a real migration do the audit once, inside the secure environment where the data already lives, then destroy the plain text.

The ideas come from Password Strength and Policies and Hash Functions and Integrity, and the reporting uses pandas.

The plan

flowchart LR
    A["Fake credential list"] --> B["Audit checks<br/>weak, reused, breached"]
    B --> C["Hash with salted scrypt"]
    C --> D["Report in pandas<br/>no passwords shown"]
    C --> E["Delete the plain-text column"]

Audit first, then hash, then report on findings only; the plain text is gone at the end.

A few design rules keep the tool itself safe:

  • The report must never contain passwords. Show the length, the user and the problems, nothing else.
  • Audit against an offline list. Sending passwords (or even hash prefixes of real users) to an online service from an ad hoc script is a risk; a local breach list avoids it. Real services based on k-anonymity exist for applications, but for a one-time migration an offline list is simpler.
  • Reject, do not repair. Users with weak passwords should be forced to choose new ones at next login. You never choose passwords for people.

Step 1: the audit checks

Each check answers one question about a password. We keep them as small functions so they are easy to test and extend.

Check Rule used in this project
short fewer than 12 characters
breached appears in the breach list, even after undoing common letter-to-symbol swaps such as @ for a
contains username the account name appears inside the password
repetitive fewer than 5 distinct characters
sequence contains a keyboard run or a counting run of 5 or more characters, such as qwerty or 12345
reused the identical password is used by another account

The thresholds are policy choices for this audit. Current NIST guidance requires at least 8 characters and recommends allowing much longer passphrases, while forbidding composition rules and requiring a blocklist check; the 12-character bar here is simply a stricter local policy.

Python Audit a fake credential list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at what the report contains: the user, the length, the problems and a score. A weak password such as Welcome1 looks fine to a naive complexity rule (it has a capital letter and a digit) and still fails the breach check, which matches what you learned about composition rules in the strength lesson. Meanwhile violet-lantern-orbit-seven sails through with no complexity symbols at all, because length does the work.

Step 2: storing salted scrypt hashes

Once you know which accounts are risky, you hash every password with a salted, slow, memory-hard function. As in the earlier password lessons, hashlib.scrypt is not available in the browser Python used on this site, so the demo below contains a pure-Python scrypt written from RFC 7914. It is slow and for learning only, and it is checked against the RFC's published test vector so you can see it computes the real function. In your own projects call hashlib.scrypt or a library such as argon2-cffi.

The three scrypt parameters are N (CPU and memory cost, a power of two), r (block size) and p (parallelism). The memory needed is about 128 * N * r bytes. We use the tiny N = 128, r = 1 here so the demo runs in a second or two; real systems use settings such as N = 2**15 or more with r = 8, tuned to take around 100 to 500 ms on production hardware.

Python Pure-Python scrypt and a password vault

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two accounts with the same password now produce two unrelated stored strings, and each record carries the algorithm and its parameters so they can be raised later. If a plain-text password ever shows up in that table after this step, the migration is not finished.

Step 3: the complete tool

Now we combine both halves into one run: audit every account, hash every password, and produce a report that is safe to share. Two decisions are worth noting:

  • Accounts with high risk are marked must_reset. They get a hash (so the old password cannot be recovered from the table any more) but are forced to choose a new password at next login. A password found in a breach list should never be accepted again.
  • The final table has no password column at all. The tool works on the plain text in memory and the result keeps only hashes and findings.
Python The complete password audit tool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two users who shared sunshine are a good illustration of why the tool counts reuse separately: a single breach of either account's password elsewhere would let an attacker open both.

Ideas for extending the project

  • Replace the tiny BREACHED set with a real offline list of the most common passwords, loaded line by line, and compare the speed of a set lookup with a list scan.
  • Add a hash upgrade path: verify_password could also return whether the stored cost is below your target, as shown in the password storage lesson, so that logins quietly raise it.
  • Add a rate-limited login simulation that uses verify_password and counts failures per account (see Spotting Attacks in Logs).
  • Produce a chart of risk levels with matplotlib for a management summary.
  • Write unit tests for each check using the approach from Security Code Review, SAST and Testing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the audit report show the password length and issues but never the password itself?

    Reports travel through email, tickets and chats. Keeping plain-text passwords out of them avoids creating a second copy of the exact secret you are trying to protect.
  2. 2. Why does the tool check passwords against a breach list even after replacing symbols such as @ and 0?

    P@ssw0rd is a predictable variation of a very common password, so it appears in cracking rule sets. Undoing common swaps catches it.
  3. 3. What does the stored string 'scrypt$128$1$1$salt$hash' allow the system to do later?

    Storing the algorithm and parameters inside the record makes migrations possible. The hash is still one-way, so the password cannot be recovered.
  4. 4. What should happen to an account whose password is on the breach list?

    A known-breached password is guessable no matter how well it is hashed. Hash it so the plain text is gone, and force a reset.

Summary

  • A migration audit has two jobs: measure the risk in existing passwords and replace plain text with salted, slow hashes.
  • Useful checks are length, breach-list membership (including symbol swaps), username inclusion, repetition, simple sequences and reuse across accounts.
  • The report keeps users, lengths and findings, never the passwords.
  • Store scrypt (or Argon2id) hashes with a unique salt and the parameters inside the record, and compare in constant time.
  • Force a reset for accounts whose passwords are breached or reused, and delete the plain-text column once the migration is verified.

Next: Project: Security Log Analyzer, where you will turn raw web-server logs into alerts and charts.