Module SEC 101 · Autumn term · 12 lectures
Keeping a staff portal secure
This portal began as university coursework in 2021: a login for staff and one for administrators, with secure pages behind them. Here is everything that protects it now, set out as a syllabus, with what the 2021 version did for comparison.
-
Lecture I
Passwords are hashed, never stored
2021Passwords were saved as plain text in db.txt, inside the website folder. They were also trimmed and HTML-escaped first, which quietly changed some passwords.
NowOnly a bcrypt hash is stored, made by password_hash() with a work factor of 12. Signing in compares with password_verify(), and hashes are upgraded automatically if the work factor is raised.
password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]); -
Lecture II
Rules that actually help
2021Any password was accepted, even an empty one.
NowAt least 12 characters, not on a list of common leaked passwords, and not containing the person’s name or username (the NIST SP 800-63B guidance). No “one symbol and one capital” rules. At most 72 bytes, because bcrypt ignores anything longer. A meter rates the password as you type.
if (in_array(strtolower($pw), COMMON_PASSWORDS, true)) { /* refuse */ } -
Lecture III
Slowing down guessing
2021Unlimited attempts: a script could try thousands of passwords a minute.
NowFive wrong passwords for one username from one address, or twenty from one address in total, pause sign-in for 15 minutes. Accounts themselves are never locked, so nobody can lock real staff out by typing their username wrongly on purpose.
if (throttle_wait($username) > 0) { /* “try again in 12 minutes” */ } -
Lecture IV
Telling attackers nothing
2021Different messages for unknown usernames and wrong passwords would reveal who has an account.
NowThe same message for both. When a username doesn’t exist, the password is still checked against a dummy bcrypt hash, so both mistakes take the same time and the delay gives nothing away.
password_verify($password, $user['password_hash'] ?? DUMMY_HASH); -
Lecture V
Sessions that can’t be borrowed
2021The session ID stayed the same after signing in, and sessions never expired.
NowA new session ID at sign-in (stopping session fixation). The cookie is HttpOnly, Secure and SameSite=Lax, limited to this folder, in strict mode. Sessions end after 20 idle minutes or 8 hours. Changing a password or suspending an account signs out every other session.
session_regenerate_id(true); -
Lecture VI
Forged forms (CSRF)
2021Any website could have sent a form to the portal on a signed-in teacher’s behalf.
NowEvery form carries a random token from the session, compared in constant time with hash_equals(), and the browser’s Origin header must be this site. Signing out is a form too, so a link on another site can’t sign anyone out.
hash_equals($_SESSION['csrf'], $_POST['_token']); -
Lecture VII
Who may open what
2021The secure pages checked a session value, but the admin page relied on one shared password.
NowPersonal accounts with roles (teacher or administrator). Every protected page checks the role on the server, on every request. Refusals return “403 Forbidden” and are written to the audit log. After signing in, ?next= only goes to the portal’s own pages (no open redirect).
if ($user['role'] !== 'admin') { error_page(403, …); } -
Lecture VIII
Checking what people type
2021“Steve” and “steve” could both be registered, and fields weren’t checked.
NowThe server checks every field: titles and roles from fixed lists, usernames by pattern, email addresses with filter_var(), names by letters. Usernames and email addresses are unique whatever their capitals. The browser gives hints, but only the server decides.
filter_var($email, FILTER_VALIDATE_EMAIL) -
Lecture IX
Escaping output, and a Content Security Policy
2021Some values were echoed into pages without escaping.
NowEverything goes through htmlspecialchars() on its way into a page. The Content Security Policy only allows this site’s own scripts, so even injected HTML couldn’t run any. Other sites can’t frame the portal, and browsers aren’t allowed to guess file types.
header("Content-Security-Policy: default-src 'none'; script-src 'self'; …"); -
Lecture X
SQL injection
2021The text file was read line by line; a real database was the next step.
NowSQLite through PDO, with prepared statements for every query: what people type is sent separately from the SQL and can never change it.
q('SELECT * FROM users WHERE username = ?', [$username]); -
Lecture XI
Keeping data out of reach
2021db.txt could be downloaded by anyone who guessed its address. After signing out, the Back button still showed the secure pages.
NowThe database lives outside the website’s folder, and the app’s own folders refuse all visitors. PHP errors are logged, never shown. Every page is sent with “Cache-Control: no-store”, so nothing is left in the browser after signing out.
header('Cache-Control: no-store, max-age=0'); -
Lecture XII
Watching the watchmen
2021Nothing was recorded.
NowAn audit log records sign-ins, failures, pauses, refusals and every administrator action, with IP addresses masked (81.2.69.x) for privacy. Administrators can reset a password but never see one, and a reset password must be replaced at the next sign-in.
audit('password_reset', $admin, '', 'reset the password of ' . $target['username']);
Coursework
Exercises to try
- Sign in as
adawith a wrong password five times. What happens on the sixth try? - Sign in as Ada (a teacher), then type
admin.phpinto the address bar. - Sign in as
babbage, register a member of staff with a temporary password, then sign in as them. - Sign out, then press your browser’s Back button.
- As Babbage, open the audit log and find everything you just did.