Home > Articles

  • Print
  • + Share This
This chapter is from the book

Section 3: Advanced Server-Hardening Techniques

Depending on your level of threat, you may want to add some additional hardening techniques to each of your servers. The advanced server-hardening techniques we cover include server disk encryption, secure NTP alternatives, and two-factor authentication with SSH.

Server Disk Encryption

Like many more advanced security-hardening techniques, disk encryption is one of those security practices that many administrators skip unless they are required to engage it by regulations, by the sensitivity of the data they store, or by the presence of an overall high-security environment. After all, it requires extra work to set up, it can lower your overall disk performance, and it can require that you manually intervene to enter a passphrase to unlock a disk whenever you boot. As you consider whether you should encrypt your disks, it is important to recognize what security it provides and what security it can’t provide.

  • Encryption protects data at rest. Disk encryption will encrypt data as it is written to disk but provides the data in unencrypted form while the file system is mounted. When the disk is unmounted (or the server is powered off), the data is encrypted and can’t be read unless you know the passphrase.

  • Encryption does not protect the live file system. If an attacker compromises a server while the encrypted disk is mounted (which would usually be the case for most running servers), he will be able to read the data as though it were any other unencrypted file system. In addition, if the attacker has root privileges, he can also retrieve the decryption key from RAM.

  • The encryption is only as strong as your passphrase. If you pick a weak password for your disk encryption, then an attacker will eventually guess it.

Root Disk Encryption

The examples I give next are to encrypt non-root disks. For servers, it’s simpler to segregate your sensitive data to an encrypted disk and leave the OS root partition unencrypted. That way you could set up your system to be able to boot to a command prompt and be accessible over the network in the event of a reboot without prompting you for a passphrase. That said, if your environment is sensitive enough that even the root disk must be encrypted, the simplest way to set it up is via your Linux distribution’s installer, either manually in the partitioning section of your installation disk, or through an automated installation tool like kickstart or preseed.

Non-root Disk Encryption

I’m assuming that if you chose to encrypt your root disk, you probably encrypted all the remaining disks on your server during installation. However, if you haven’t chosen to encrypt everything, you likely have a disk or partition you intend to use for sensitive information. In the examples that follow, we use the Linux Unified Key Setup (LUKS) disk encryption tools and, in particular, we use the cryptsetup script that simplifies the process of creating a new LUKS volume. If cryptsetup isn’t already installed on your server, the package of the same name should be available for your distribution.

In the following example, we will set up an encrypted volume on the /dev/sdb disk but you could also select a partition on the disk instead. All commands will require root permissions. In the end, we will have a disk device at /dev/mapper/crypt1 we can format, mount, and treat like any other disk.

The first step is to use the cryptsetup tool to create the initial encrypted drive with your chosen passphrase and format it with random data before you use it:

$ sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb
WARNING!
========
This will overwrite data on /dev/sdb irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
Command successful.

At this point, you have a LUKS encrypted disk on /dev/sdb, but before you can use it you need to open the device (which will prompt you for the passphrase) and map it to a device under /dev/mapper/ that you can mount:

$ sudo cryptsetup luksOpen /dev/sdb crypt1
Enter passphrase for /dev/sdb:

The syntax for this command is to pass cryptsetup the luksOpen command followed by the LUKS device you want to access, and finally the label you want to assign to this device. The label will be the name that shows up under /dev/mapper, so in the preceding example, after the command completes, I will have a device under /dev/mapper/crypt1.

Once /dev/mapper/crypt1 exists, I can format it with a file system and mount it like any other drive:

$ sudo mkfs -t ext4 /dev/mapper/crypt1
$ sudo mount /dev/mapper/crypt1 /mnt

You will likely want to set this up so that the device shows up in the same way after every boot. Like with the /etc/fstab file you use to map devices to mount point at boot, there is an /etc/crypttab file you can use to map a particular device to the label you want to assign to it. Like with modern /etc/fstab files, it’s recommended that you reference the UUID assigned to the device. Use the blkid utility to retrieve the UUID:

$ sudo blkid /dev/sdb
/dev/sdb: UUID="0456899f-429f-43c7-a6e3-bb577458f92e" TYPE="crypto_LUKS"

Then update /etc/cryptab by specifying the label you want to assign the volume (crypt1 in our example), the full path to the disk, then “none” for the key file field, and then “luks” as the final option. The result in our case would look like this:

$ cat /etc/crypttab
# <target name> <source device>         <key file>      <options>
crypt1 /dev/disk/by-uuid/0456899f-429f-43c7-a6e3-bb577458f92e none luks

If you do set up /etc/crypttab, you will be prompted at boot for a passphrase. Note in our example we did not set up a key file. This was intentional because a key file on the unencrypted root file system would probably be available to an attacker who had access to the powered-off server, and then she would be able to decrypt the disk.

Secure NTP Alternatives

Accurate time is important on servers, not just as a way to synchronize log output between hosts, but because most clustering software relies on cluster members having an accurate clock. Most hosts use a service called Network Time Protocol (NTP) to query a remote NTP server for accurate time. You need root permissions to set the time on a server, so typically the NTP daemon (ntpd) ends up running in the background on your system as root.

I would imagine most administrators don’t think about NTP when they think about security. It is one of those protocols you take for granted; however, most administrators ultimately rely on an external accurate time source (like nist.gov) for NTP. Because NTP uses the UDP protocol, it might be possible for an attacker to send a malicious, spoofed NTP reply before the legitimate server. This reply could simply send the server an incorrect time, which could cause instability, or given that ntpd runs as root, if it didn’t validate the NTP reply in a secure way, there is potential for a man-in-the-middle attacker to send a malicious reply that could execute code as root.

One alternative to NTP is tlsdate, an open-source project that takes advantage of the fact that the TLS handshake contains time information. With tlsdate, you can start a TLS connection over TCP with a remote server that you trust and pull down its time. While the timestamps in TLS are not as precise as with NTP, they should be accurate enough for normal use. Since tlsdate uses TCP and uses TLS to validate the remote server, it is much more difficult for an attacker to send malicious replies back.

The tlsdate project is hosted at https://github.com/ioerror/tlsdate, and the general-purpose installation instructions can be found at https://github.com/ioerror/tlsdate/blob/master/INSTALL. That said, tlsdate is already packaged for a number of popular Linux distributions, so first use your standard package tool to search for the tlsdate package. If it doesn’t exist, you can always download the source code from the aforementioned site and perform the standard compilation process:

./autogen.sh
./configure
make
make install

The tlsdate project includes a systemd or init script (depending on your distribution) that you can start with service tlsdated start. Once running, the script will notice network changes and will periodically keep the clock in sync in the background. If you want to test tlsdate manually, you can set the clock with the following command:

$ sudo tlsdate -V
Sat Jul 11 10:45:37 PDT 2015

By default, tlsdate uses google.com as a trusted server. On the command line, you can use the -H option to specify a different host:

$ sudo tlsdate -V -H myserver.com

If you want to change the default, edit /etc/tlsdate/tlsdated.conf and locate the source section:

# Host configuration.
source
        host google.com
        port 443
        proxy none
end

Change the host to whichever host you would like to use. For instance, you may want to pick a couple of hosts in your network that poll an external source for time and have the rest of your servers use those internal trusted hosts for time. Those internal trusted servers simply need to serve some kind of TLS service (such as HTTPS).

Two-Factor Authentication with SSH

Disabling password authentication on SSH and relying strictly on keys is a great first step to hardening SSH, but it still is not without its risks. First, while you may follow my advice and protect your SSH keys with a password, you can’t guarantee that every user on the system does the same. This means if an attacker were able to access a computer for a short time, he could copy and use the keys to log in to your system. If you want to protect against this kind of attack, one approach is to require two-factor authentication for SSH.

With two-factor authentication, a user must provide both an SSH key and a separate token to log into the server. Time-based tokens are the most common, and in the past they required you to carry around an expensive device on your keychain that would update itself with a new token every 30 seconds. These days, there are a number of two-factor authentication solutions that work in software and can use your cell phone instead of a hardware token.

There are several different phone-based two-factor authentication libraries out there for ssh. Some work by the SSH client ForceCommand config option, some with a system-wide pluggable authentication modules (PAM) module. Some approaches are time-based, so they work even if your device is disconnected from a network, while others use SMS or phone calls to transfer the code. For this section, I’ve chosen the Google Authenticator library for a couple of reasons:

  • It’s been around for a number of years and is already packaged for a number of Linux distributions.

  • The Google Authenticator client is available for multiple phone platforms.

  • It uses PAM, so you can easily enable it system-wide without having to edit ssh config files for each user.

  • It provides the user with backup codes she can write down in case her phone is ever stolen.

Install Google Authenticator

The Google Authenticator library is packaged for different platforms so, for instance, on Debian-based systems you can install it with

$ sudo apt-get install libpam-google-authenticator

If it isn’t already packaged for your distribution, go to https://github.com/google/google-authenticator/ and follow the directions to download the software, build, and install it.

Configure User Accounts

Before we make PAM or SSH changes that might lock you out, you will want to at least configure Google Authenticator for your administrators. First, install the Google Authenticator application on your smartphone. It should be available via the same methods you use to install other applications.

Once the application is installed, the next step is to create a new Google Authenticator account on your phone from your server. To do this, log in to your account as your user and then run google-authenticator. It will walk you through a series of questions, and it’s safe to answer yes to every question, although since you should have already set up tlsdate on your server so its time is accurate, I recommend sticking with the default 90-second window instead of increasing it to 4 minutes. The output looks something like this:

kyle@debian:~$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y
[URL for TOTP goes here]
[QR code goes here]
Your new secret key is: NONIJIZMPDJJC9VM
Your verification code is 781502
Your emergency scratch codes are:
  60140990
  16195496
  49259747
  24264864
  37385449
Do you want me to update your "/home/kyle/.google_authenticator" file (y/n) y

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) y

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) n

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) y

If you have libqrencode installed, this application will not only output a URL you can visit to add this account to your phone, it will also display a QR code on the console (I removed it from the preceding output). You can either scan that QR code with your phone, or enter the secret key that follows “Your new secret key is:” in the output.

The emergency scratch codes are one-time use codes that you can use if you ever lose or wipe your phone. Write them down and store them in a secure location apart from your phone.

Configure PAM and SSH

Once you have configured one or more administrators on the server, the next step is to configure PAM and SSH to use Google Authenticator. Open your SSH PAM configuration file (often at /etc/pam.d/sshd) and at the top of the file add

auth required pam_google_authenticator.so

On my systems, I noticed that once I enabled Google Authenticator and ChallengeResponseAuthentication in my SSH configuration file, logins would also prompt me for a password after I entered my two-factor authentication code. I was able to disable this by commenting out

@include common-auth

in /etc/pam.d/sshd, although if you aren’t on a Debian-based system, your PAM configuration may be a bit different.

Once the PAM file was updated, the final step was to update my SSH settings. Open /etc/ssh/sshd_config and locate the ChallengeResponseAuthentication setting. Make sure it’s set to yes, or if it’s not, in your sshd_config file add it:

ChallengeResponseAuthentication yes

Also, since we have disabled password authentication before, and are using key-based authentication, we will need to add an additional setting to the file, otherwise SSH will accept our key and never prompt us for our two-factor authentication code. Add the following line to the config file, as well:

AuthenticationMethods publickey,keyboard-interactive

Now you can restart ssh with one of the following commands:

$ sudo service ssh restart
$ sudo service sshd restart

Once SSH is restarted, the next time you log in you should get an additional prompt to enter the two-factor authentication code from your Google Authenticator app:

$ ssh kyle@web1.example.com
Authenticated with partial success.
Verification code:

From this point on, you will need to provide your two-factor token each time you log in.

  • + Share This
  • 🔖 Save To Your Account