Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

One-time codes and MFA

After this lesson: I can explain how a one-time code is made and judge which second factor to use.

60 min lesson3 commandsPassword lab: John the Ripper, MFA and passkeys

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
MFAsigning in with two or more kinds of proof
Shared secreta key both the site and the app keep
HMACa hash keyed with a secret
HOTPa one-time code from a counter
TOTPa one-time code from the timea new code every 30 seconds
Phishing-resistantcannot be passed on by a fake site

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
docker exec john python3 steps/totp.py rfc

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: time steps

Code: python
# 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

Command
docker exec john python3 steps/totp.py now

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Sign amira in with password and code

Command
docker exec john python3 steps/mfalogin.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what mfalogin.py printed

AttemptThe site said
Password onlyneed-code
Password and the code nowok
The same code againwrong-code
A code from two minutes agowrong-code

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Not 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

FactorStops stuffingStops a fake pageWeak point
SMS codeYesNoSIM swap, interception
App code (TOTP)YesNoRelayed by a fake page
Push with number matchingYesPartlyA determined relay
Passkey or security keyYesYesLosing 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.