Junior Security Analyst

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.

50 min

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 None when 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 with set(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(), and not c.isalnum() (a symbol) do the rest. Add the four True/False values with sum([...]).
  • 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_bits and best_user are 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 toTypeWhat you get
see the password ageing rules of a Linux accountchage -l usernamelast change, expiry and warning days
see the password policy of a Windows computer or domainnet accountsminimum length, maximum age and lockout settings
read the password policy of an Active Directory domainGet-ADDefaultDomainPasswordPolicycomplexity, length, history and lockout of the domain policy

Only run commands on systems that you own or have written permission to test.

🔒 The debrief unlocks when you close the case. Finish the lab above.

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.

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 4

Which password has more entropy against a guessing attack, assuming an attacker knows nothing about the person: 'Tr0ub4dor&3' or 'correct horse battery staple'?

Report a problem

About: /lessons/case-the-password-rules