This post explains how to generate verification codes via the command line in Linux and MacOS to log into websites that use Microsoft mutli-factor authentication (MFA).
It avoids the need to use a MFA smartphone apps like Microsoft Authenticator, bypassing 2FA fatigue. Our phones are not always nearby or may not have an internet connection.
First, you need to install the OATH toolkit: https://www.nongnu.org/oath-toolkit/download.html.
The steps are as follows, explained in more detail in sections below.
Use oathtool
to register your terminal as a new sign-in app.
Store and encrypt your Microsoft-generated secret key.
Use oathtool
to generate a verification code each time you wish to log into
the website that uses Microsoft MFA.
Use the generated verification code in the website's login screen.
Click "Add sign-in method".
Click "Microsoft Authenticator".
Click "I want to use a different authenticator app" then "Next" then "Can't scan image?".
Copy the key next to "Secret key" then click "Next". Note you will need this secret key in a later step, so keep a copy of it.
Run:
oathtool --base32 --totp <INSERT SECRET KEY>
Copy the 6 digit code into the box beneath "Enter the 6-digit code shown in the Authenticator app." on the Microsoft security info web page.
You should now see "Authenticator app was successfully registered" in the top right corner:
Now to automate verification code generation as a command in a shell script. First, store your secret key in an encrypted file.
Create a plain text file somewhere:
touch 2fa-passwd
then use a text editor to edit that to 2fa-passwd
file, pasting the secret key from earlier.
Encrypt it:
gpg --output 2fa-passwd.gpg --symmetric 2fa-passwd
GPG may ask you to input a password to encrypt this file. Whatever you type in will become the password you'll need to enter to decrypt your secret key (to generate MFA verification codes), so make a note of this GPG password.
Delete the plain text file:
rm 2fa-passwd
oathtool
for future loginsNow create a shell command that decrypts your secret key and passes it to
oathtool
to generate verification codes.
Create the shell script file:
touch 2fa-code
Then make it executable:
chmod 700 2fa-code
Optionally, create this file in a directory in your $PATH
so you can run
2fa-code
without having to specify its full path.
Populate that file with this (modifying the path to your 2fa-passwd.gpg
file):
#!/bin/sh
gpg -q -d /path/to/2fa-passwd.gpg | oathtool --base32 --totp -
When logging into the website that uses Microsoft MFA authentication:
Enter your username.
Enter your password.
Click "I can't use my Microsoft Authenticator app right now"
Click "Use a verification code":
Generate the verification code by running your shell script file:
2fa-code
GPG may ask you for your GPG password to decrypt your 2fa-passwd.gpg
. Type
it in.
A six-digit code will be printed to standard output. Type this into the website:
Then click "Verify".
You should now be logged into the website.
In future, you only need to follow this final step of "Using the verification
code", i.e. run the 2fa-code
file to obtain a new six-digit code each time
you are logging into the Microsoft-authenticated website.
These instructions are based on those shared by my colleague Swaraj Dash. I've just written them up for wider dissemination.