Labs / Identity & Access / Tickets & trust

Lab 2 of 4 · Tickets & trust

Two-step login: a one-time code after the key

Add SSH 2FA, a one-time code from a phone, and see what it stops.

Start this lab 6 steps

The idea

Larkspur Hosting reaches its customers' servers through one jump host, guarded by SSH keys alone. Add a one-time code from a phone on top, the kind banks ask for, and find out what it stops and what it doesn't.

One check, and the laptop passes it
One check, and the laptop passes it
One secret, kept at both ends
One secret, kept at both ends
A code is the secret and the clock
A code is the secret and the clock
Two checks, in order
Two checks, in order
The second login is refused
The second login is refused
Each emergency code works once
Each emergency code works once

Steps

  1. One key, one factor

    Larkspur Hosting runs websites for a few hundred small businesses. Its team reaches the customers' servers through one machine, jump: the only way in, so it's the one to guard.

    Nobody types a password on jump. Each person has an SSH key: a secret half on the team's laptop, in ~/.ssh/, and a matching half in their home on jump. A login proves the laptop holds the secret half, and that's all it proves.

    Proof of who you are comes in kinds, called factors:

    • something you know: a password, a PIN;
    • something you have: a phone, a key file, a badge;
    • something you are: a fingerprint, a face.

    An SSH key is something you have. Whoever has the laptop has every key on it.

    Mara leads the team. Log in to jump as her, then read its SSH settings.

    laptop:~# ssh mara@jump.lab hostname
    jump:~# cat /etc/ssh/sshd_config.d/jump.conf
  2. Set up an authenticator

    The second proof here is a one-time code from an authenticator app on Mara's phone. TOTP, time-based one-time passwords, the method behind it, starts with one secret, kept at both ends.

    google-authenticator sets it up. It makes a random secret, saves it in ~/.google_authenticator on jump, and shows it to you, normally as a QR code for the phone to scan. From then on the server and the phone each hold the secret, and each turns it into codes on its own.

    Run it on jump as Mara, through her login, with its questions answered up front:

    • -t: codes that change with time;
    • -d: each code works once;
    • -w 3: also accept the code before and the code after this one, for a phone whose time is a little off;
    • -u: no cap on login attempts, so the lab's back-to-back logins don't trip it (real servers keep one);
    • -f: write the file without asking.

    It then asks for a code from the app, to prove the scan worked, and the run answers -1 to skip that.

    laptop:~# ssh mara@jump.lab google-authenticator -t -d -w 3 -u -f
    laptop:~# ssh mara@jump.lab cat .google_authenticator
  3. Read a code

    A code is the secret and the clock, mixed together. Count the 30-second time steps since 1 January 1970: that's the counter. Run the counter and the secret through HMAC, a hash that takes a key, and boil the result down to six digits.

    The phone does that on its own, with no network: all it needs is the secret and the time. jump does the same sum when Mara logs in, and compares. Each code is good for the 30 seconds of its time step, and -w 3 lets jump take the code from the time step before and the one after as well.

    oathtool is an authenticator for the command line. On laptop it plays Mara's phone: every second it makes the code for that moment and puts it on the phone's screen, the file /root/phone/screen.

    Run it yourself, to see the sum:

    • --totp makes codes from the time;
    • -b says the secret is in base32, the alphabet of capital letters and digits the file uses;
    • @ reads the secret from a file, /root/phone/mara, where the phone keeps it;
    • -v shows the sum.
    laptop:~# oathtool --totp -v -b @/root/phone/mara
    laptop:~# cat /root/phone/screen
    laptop:~# oathtool --totp -w 2 -b @/root/phone/mara
  4. Ask for the code

    Two edits on jump put the code on top of the key.

    First, PAM, the library Linux programs ask to check a login. /etc/pam.d/sshd lists what an SSH login must pass, and its auth line sends it to base-auth, the usual password check.

    Swap that for pam_google_authenticator.so, which asks for a code and checks it against ~/.google_authenticator. required means a login has to pass it.

    Then SSH itself. AuthenticationMethods lists the methods a login must pass, in order, split by commas. keyboard-interactive is the one where the server asks questions and the person types answers.

    With UsePAM yes, PAM picks the questions. So publickey,keyboard-interactive means the key first, then PAM's question.

    From then on, the laptop has to answer that question. ssh reads answers from a person at a keyboard only, so sshpass stands in for one. PAM's question is Verification code:, so -P code waits for a question with code in it, and -f types the first line of a file as the answer.

    laptop:~# ssh mara@jump.lab hostname
    laptop:~# sshpass -P code -f /root/phone/screen ssh mara@jump.lab hostname
  5. The same code twice

    A code shows on a screen and gets typed by hand, so it can be seen: over a shoulder, in a screen share, by malware on the laptop. Sending a captured code again is a replay. For the minute or so jump still takes a code (its own time step, and the next one, thanks to -w 3), someone with the laptop and that code is as good as Mara.

    -d closes that gap. jump writes down the time step of each code it accepts, on the DISALLOW_REUSE line of Mara's file, and refuses a code from a time step it has seen. Each code works once, even while it's still good.

    PAM on jump writes what it checks to /var/log/messages, tagged pam_google_authenticator.

    Before you start, the lab waited, out of sight, for a fresh time step, one whose code Mara hasn't used yet.

    Now play the one looking over her shoulder. Write down the code on her screen: cp copies it to /root/noted, and -v says what it copied. Then Mara logs in with that code, and right after her, the watcher types the same one.

    laptop:~# cp -v /root/phone/screen /root/noted
    laptop:~# sshpass -P code -f /root/noted ssh mara@jump.lab hostname
    laptop:~# sshpass -P code -f /root/noted ssh mara@jump.lab hostname
    jump:~# grep pam_google_authenticator /var/log/messages
    jump:~# grep DISALLOW_REUSE /home/mara/.google_authenticator
  6. A lost phone

    Mara's phone went over the side of the ferry this morning. No phone, no codes, and jump won't let her in without one.

    That's what the emergency codes are for: the eight-digit lines at the end of her file. Each one works once in place of a code, with no phone and no clock.

    People print them, or keep them in a password manager: anywhere except the phone, or the laptop that holds the key. Mara printed hers, and here a file on the laptop, /root/emergency-codes, stands in for that printout.

    The lost phone is a problem of its own. Whoever finds it, and gets past its lock screen, has the secret and makes Mara's codes. That alone won't get them into jump, which wants her key first, but the secret is out.

    Running google-authenticator again makes a new secret and new emergency codes, and the old phone's codes stop working.

    First, get Mara in. Read her codes, log in with the first one, then try that one again.

    laptop:~# cat /root/emergency-codes
    laptop:~# sshpass -P code -f /root/emergency-codes ssh mara@jump.lab hostname
    laptop:~# sshpass -P code -f /root/emergency-codes ssh mara@jump.lab hostname
    jump:~# cat /home/mara/.google_authenticator

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab