Case File: The Password Rules
The company wants a password policy that people can follow and attackers cannot guess around. Write the rules as code and test them on a list of invented passwords: length, common passwords, the user's own name, and mixed character classes, with one reason reported for each failure, and estimate the entropy in bits of a password to see why length beats cleverness. The accounts and the policy are generated for you alone.
Sabi Logistics · Security 1
The Password Rules
The insurer wants a written password policy, and the one on the intranet is eight characters and a number
Halima CISO · Phone call
Halima. The cyber insurer has read our password policy, which says “at least eight characters, one capital, one number”, and asked me whether we have ever checked it against reality. We have not. I suspect it lets through exactly the passwords that attackers try first, and rejects some good ones.
I want you to write the policy as code and run it on a list of passwords from a test system. They are invented, so no real person is exposed. The rules I want: a minimum length, not on the list of common passwords, not containing the person’s own user name, and at least three kinds of character. I want to know how many pass, and for those that fail, why, counting only the first rule that fails so that the numbers add up.
And I want one more number. Somebody in a meeting said that a short password with symbols is stronger than a long one without them. Show me, in bits, who is right.
The brief
What you need to know first.
- A policy is a set of rules, and rules can be code. Write each rule as a small test on a password, and one function that returns the first rule that fails (or
Nonewhen the password passes). The order of the checks matters when more than one fails, so decide it and write it down: common, short, contains the user name, too few kinds of character. - Reading the file.
csv.DictReader(open("passwords.csv"))gives a dict for each row:row["user"],row["password"]. Read the common passwords withset(open("common.txt").read().split())(a set makes “is it in the list?” instant). - Character classes. Lower case, upper case, digits, and everything else.
any(c.islower() for c in p)is true if at least one character is lower case;c.isupper(),c.isdigit(), andnot c.isalnum()(a symbol) do the rest. Add the fourTrue/Falsevalues withsum([...]). - Case-insensitive matches. Compare the password in lower case with the common list (
p.lower() in common) and look for the user name in any case (user.lower() in p.lower()). - Counting reasons. A dictionary you add to:
reasons[why] = reasons.get(why, 0) + 1..get(why, 0)returns 0 the first time. - Entropy measures how many guesses an attacker needs if the password were chosen at random:
bits = length × log2(pool). The pool is the number of different characters you could have used: 26 for lower case, plus 26 for upper case, plus 10 for digits, plus about 32 for symbols, counting only the kinds that appear.math.log2(x)gives the logarithm. Every extra bit doubles the work for the attacker. - What entropy does not tell you. It assumes the password was picked at random. A human-chosen password like
Summer2026!is short on real entropy because it is from a short list of patterns. That is why the common-password check comes first. - Starter variables.
ok_users,n_ok,reasons,entropy_bitsandbest_userare checked after your code runs. Round the entropy to one decimal.
The lab gives you accounts, the policy numbers and a password to measure, generated for you alone. All passwords are invented.
Preparing your data…
On a real system
The lab modelled the rules with a script. These are the commands that you would type on the real thing.
| You want to | Type | What you get |
|---|---|---|
| see the password ageing rules of a Linux account | chage -l username | last change, expiry and warning days |
| see the password policy of a Windows computer or domain | net accounts | minimum length, maximum age and lockout settings |
| read the password policy of an Active Directory domain | Get-ADDefaultDomainPasswordPolicy | complexity, length, history and lockout of the domain policy |
Only run commands on systems that you own or have written permission to test.
Halima: “So the policy I wrote on the intranet would have let Welcome1 through, and refused a perfectly good passphrase because it had no digit. What should the policy say instead? And what do we do about the people who use the same password everywhere, which this list cannot show us?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Halima: “So the policy should ask for length and for a check against known-bad passwords, and the real protection is a second factor.”
What a senior security engineer would do next. Follow current guidance (for example NIST SP 800-63B): long (at least 12, ideally allow 64), allow spaces and passphrases, check against a list of breached and common passwords, do not force periodic changes or odd composition rules, and require a second factor for anything important. Offer a password manager so that every account has a different password. Store only salted, slow hashes (the next case), rate-limit logins and lock out after repeated failures. Measure real strength with a password-guessing tool against your own hashes, with permission, instead of trusting a formula. And write the policy in plain language with the reason for each rule, because people follow rules they understand.
The trap. Believing that complexity rules produce strong passwords. They produce Password1!. Length, uniqueness and a second factor produce security.
Write the policy rationale
Write the short rationale for the new password policy: the rules you tested and why each one is there, what the numbers showed about the old policy (how many passed, and which rule caught most), what entropy is and why length matters more than clever symbols, why the common-password check comes first, and what else the company needs (a second factor, a password manager, slow salted hashes) because a password rule alone is not security.
🧪 Want to practise this on your own computer? See how to set up your lab →