Why amira's account held
In the last lesson the stuffing tool had amira's real password, and still could not get in. Her account asked for a six-digit code from an app on her phone, and the attacker did not have the phone.
This lesson opens that app up: where the code comes from, why it changes, and what it does not stop.
By the end of this lesson
Note
I can explain how a one-time code is made and judge which second factor to use.
Key words
| Word | What it means | Example |
|---|---|---|
| MFA | signing in with two or more kinds of proof | |
| Shared secret | a key both the site and the app keep | |
| HMAC | a hash keyed with a secret | |
| HOTP | a one-time code from a counter | |
| TOTP | a one-time code from the time | a new code every 30 seconds |
| Phishing-resistant | cannot be passed on by a fake site |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA code is an HMAC of a counter
When you add an account to an authenticator app, it saves a shared secret, often from a QR code. The site keeps the same secret.
To make a code, both work out an HMAC of the secret and a counter, then cut it down to six digits. HOTP counts presses. TOTP uses the time: the counter is the number of 30-second steps since 1970. Same secret, same step, same code.
Both sides make the same code
flowchart LR
S["Shared secret"] --> H["HMAC-SHA1"]
T["Time step: seconds // 30"] --> H
H --> C["Cut to six digits"]
C --> A["The app shows it"]
C --> B["The site compares it"]Print the published test values
docker exec john python3 steps/totp.py rfcShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsTry it: time steps
# A TOTP code changes every 30 seconds: the counter is the time step.
for seconds in [0, 29, 30, 59, 60, 1000000000]:
step = seconds // 30
print(seconds, "seconds is step", step)
# Add 89 and 90 to the list, then press Run.
Make a free account to save your work and track your progress.
The site takes a code once
amira's account on the lab site has a TOTP secret. With her password alone the site asks for a code. With the right code it lets her in.
The site accepts the step before and after now, for a phone clock that drifts. It also remembers the last step used, so the same code cannot be used twice, and an old one is refused.
Show amira's code now
docker exec john python3 steps/totp.py nowShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign amira in with password and code
docker exec john python3 steps/mfalogin.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what mfalogin.py printed
| Attempt | The site said |
|---|---|
| Password only | need-code |
| Password and the code now | ok |
| The same code again | wrong-code |
| A code from two minutes ago | wrong-code |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsNot every second factor is equal
A code by SMS can be stolen by moving the number to a new SIM, and NIST now restricts it. An app's TOTP code is better, but a fake page can ask for it and pass it on at once.
Push approvals can be spammed until a tired user taps yes, so they should show a number to match. Only cryptographic sign-in, such as a passkey, is phishing-resistant.
Worked example: second factors compared
| Factor | Stops stuffing | Stops a fake page | Weak point |
|---|---|---|---|
| SMS code | Yes | No | SIM swap, interception |
| App code (TOTP) | Yes | No | Relayed by a fake page |
| Push with number matching | Yes | Partly | A determined relay |
| Passkey or security key | Yes | Yes | Losing every device |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think any MFA stops phishing. A fake page can ask for the password and the code, and use both within the 30 seconds. One-time codes stop stuffing and guessing; only a passkey or security key stops a fake page.
Check
Have a go
Check
3 questionsAssignment: HOTP and TOTP
Write one-time codes as RFC 4226 and RFC 6238 define them. hotp(key, counter, digits): work out the HMAC-SHA1 of the 8-byte big-endian counter with the key (bytes). Take the last byte's value modulo 16 as an offset, read the 4 bytes from there as a big-endian number, drop its top bit (modulo 2147483648), keep the last digits digits and return them as a string padded with leading zeros. totp(key, seconds, digits) is hotp with the counter seconds // 30. window_ok(key, seconds, code) returns True when the code matches the step before, the step at or the step after seconds.
import hashlib
import hmac
def hotp(key, counter, digits):
return ""
def totp(key, seconds, digits):
return ""
def window_ok(key, seconds, code):
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go