Linux two step authentication with PAM and Google Authenticator


Recommended Posts

We can already use two step authentication in GMail with the Google Authenticator Android app. The idea is creating a secret key shared between the service and the Android app, so every 30 seconds we get a randomly generated token on Android that must be provided to login in addition to the password. That token is only valid in that 30s time frame.

brm_4Z4HD6ei29Fnai7OhIUP9DwpBTHW1C7hb05LukZU-lbP8ALtvvq_dQ5lIc-_YAlS

Since this provides a nice second security layer to our logins, why don't take advantage of it also in our Linux box?

We'll need two things to get started:

  • Install Google Authenticator on our Android, iOS or Blackberry phone (AFAIK it's not availabe for WP7, at least not yet).
  • Install the PAM on our Linux box

The first step is trivial, so we'll go for the second one.

If you are using Ubuntu you can install it straight from the repos:

sudo apt-get install libpam-google-authenticator[/CODE]

For other distros it might be also available on the repos, but if it isn't you can also get the PAM from the Google Code site.

Once we have that installed we will run this command with the user we want to use two step authentication with. If we want to use it for several users we will have to run it once with each of them, since it generates a unique secret key each time.

[CODE]google-authenticator[/CODE]

First thing we will see is a big QR code. Open your Google Authenticator app on the phone, hit the menu button and select "Scan barcode". Point the camera to the QR code on the screen and you'll get a new item on the Google Authenticator main screen with an ID for the user and computer and the generated token below, along with a counter showing how much time is left for the code to expire.

Right below the QR code we'll see something like this:

[CODE]Your new secret key is: [secret_key]
Your verification code is [verification_code]
Your emergency scratch codes are:
[code1]
[code2]
[code3]
[code4][/CODE]

Copy all this somewhere. The four "emergency scratch codes" will come handy if for whatever reason we lose or break our phone. They are one time codes that can be used instead of the tokens generated by Google Authenticator.

Next you'll be asked a few questions to configure some authentication details:

[CODE]Do you want me to update your "~/.google_authenticator" file (y/n)[/CODE]

Answer "y" here to get it configured.

[CODE]Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n)[/CODE]

If you'll need to login more than once every 30 seconds answer "n", else it's OK to answer "y".

[CODE]By default, tokens are good for 30 seconds and in order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. If you experience problems with poor time synchronization, you can increase the window from its default size of 1:30min to about 4min. Do you want to do so (y/n)[/CODE]

Unless your phone and PC clocks are severely desynchronized, it should be OK to answer "n" here.

[CODE]If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n)[/CODE]

It's a good idea to answer "y" here to prevent brute force attacks.

Up to this point we've just configured the tools we'll need to implement the two step authentication, without actually modifying anything in our login settings yet.

  Quote

The next steps in this howto will add the PAM to your login. Bear in mind that if you do anything wrong you might find yourself locked out of your box, and if your home folder is encrypted you'll be unable to access your data.

Even if that happens you can still fix it booting from a livecd and removing the PAM from the login, but just be aware of the potential problems.

We can add the PAM to several different login methods, we'll just see the two most interesting options:

[b]1.- The most interesting one: SSH logi[/b][b]n[/b]

Edit [i]/etc/pam.d/sshd [/i]and add this line at the bottom:

[CODE]auth required pam_google_authenticator.so[/CODE]

Edit [i]/etc/ssh/sshd_config [/i]and either modify (if it already exists) or add this line:

[CODE]ChallengeResponseAuthentication yes[/CODE]

Restart the sshd service (this works for Ubuntu, other distros might have a different way of handling services):

[CODE]sudo service ssh restart[/CODE]

Next time you ssh to the box you'll be prompted for both your password and your validation key.

This will only work if you aren't using certificates, else you'll be automatically logged in as usual.

[b]2.- A less interesting option: your local login[/b]

Depending on your distro you might be using a different login manager. Pick and edit the correct file among these (you'll likely only have one of them):

  • /etc/pam.d/gdm
  • /etc/pam.d/lightdm
  • /etc/pam.d/kdm

Add this line at the bottom:

[CODE]auth required pam_google_authenticator.so[/CODE]

That's it. Next time you login you'll be prompted for both your password first and then your verification key:

IMG_20120228_024150_s.jpg

Now, as a side note, why is this local login a "less interesting option"?

The two step authentication will keep users out of you box as long as they don't also have access to your phone, but you shouldn't forget that there's no way to really secure a box if the attacker has physical access to it.

The secret key is stored in your home folder. The attacker could boot your box from a livecd, get the key and generate his own tokens.

Then again same thing holds true for you user password so that's not to say that two step authentication is not secure, it's just that is has the same problems as any other login method when it comes with physically accessible machines.

This topic is now closed to further replies.
  • Posts

    • Limassol, Cyprus. Just south of Turkey. NOT Russia.
    • Hello, Given the reports of Chinese Mini PCs shipping with malware, I would recommend wiping the machine and performing a clean install of Windows on it before use.  From what I can infer from the reports, the Mini PCs that shipped with malware were not the result of targeted purposeful action on the part of the device manufacturers (which is something that has happened with low-cost Android smartphones and TV boxes from China) but rather due to lax security in the manufacturing process.   Getting back to the subject at hand, there are a few steps you will want to go through before wiping the Mini PC: You can start preparing even before the Mini PC arrives.  Once you have ordered it and know the brand and model, go to the manufacturer's website and download all of the latest device drivers, BIOS (UEFI) firmware updates, machine-specific software (if any), and manuals.  Many Mini PC manufacturers do not do a lot of customization of their device drivers, just shipping whatever device drivers the the silicon vendors provide.  I still recommend downloading them, though, just in case there are some customizations or for initial install since those are the drivers you know the manufacturer validated for the Mini PC.  Store these in a safe place, so you have them ready when the Mini PC arrives. Use Microsoft's Windows Media Creation Tool to create an installation USB.  You can also create a directory on installation USB--like C:\DRIVERS\ or whatnot--and store the extracted device drivers there in case you need them while or after installing Windows. Once the Mini PC arrives, and you have your Windows installation USB available, you can proceed with wiping the PC and doing the clean install.  Here's how you do that, step-by-step: Check the computer and make sure you know how to boot it from a USB flash drive (may be a specific key you have to press when the computer is powered on, or a change to the BIOS (UEFI) firmware settings.  The PC may tell you what key combination you need to press to boot from another drive, or the manual for the PC may it. Plug the USB flash drive into the computer and power it up using the means to have it boot from the Windows install USB. Once the computer finishes booting, it should be at a Windows installation screen. Do not agree to any prompts, copyright licenses, or click on any buttons. Press the Shift + F10 keys together to open a Command Prompt. Run DISKPART to start the command-line disk partitioning utility. The command line prompt will change to DISKPART>. At the DISKPART> prompt, type LIST DISK to get the numbers of all drives installed in the system. Make a note of what number is assigned to what drive (if the Mini PC has more than one drive).  At the DISKPART> prompt, type SEL DISK n  where n is the number of the drive containing Windows. At the DISKPART> prompt, type CLEAN and this will erase the GPT/MBR code from the beginning of the drive. *WARNING:* After performing the clean operation, the drive now be blank/erased, and everything on it will be gone (all files, etc.).  You can exit DiskPart and just continue with the Windows installation as you normally would.  If needed, you can install the device drivers you put on the Windows install media to get your network connection up and running, and from there run Windows Update to get the operating system and device drivers up to date Regards, Aryeh Goretsky
    • Why? Amazon has some great shows and Fallout was near perfect.
    • Both a game adaptation and it's on Amazon I'll set my hopes low
  • Recent Achievements

    • Week One Done
      cac1lll earned a badge
      Week One Done
    • One Month Later
      Falcon.ai earned a badge
      One Month Later
    • Week One Done
      Falcon.ai earned a badge
      Week One Done
    • Dedicated
      EYEREX earned a badge
      Dedicated
    • First Post
      Electronic Person earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      238
    3. 3
      Xenon
      166
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!