L Harden Server PDF
L Harden Server PDF
L Harden Server PDF
17 Dec 2008
Objectives
In this tutorial, you learn about basic concepts in security administration, including
how to secure Secure Shell (SSH) remote logins, create firewall rules, and watch
logs for possible attacks.
Prerequisites
This tutorial is written for the beginning GNU/Linux user. You should have some
familiarity with operating system installations and the command line. To fully
understand the concepts in this tutorial, you should have gone through the
companion tutorial, "Hardening the Linux desktop."
System requirements
To run the examples in this tutorial, you need to install Ubuntu Server Edition on a
computer or a virtual machine, such as Sun VirtualBox. You also need an Internet
connection to download specific software packages used in the tutorial.
Section 2. Introduction
To understand the basics of hardening a server running GNU/Linux as the operating
system, you need to be aware that although many core concepts of security apply to
both the desktop operating system and the server operating system, the ways
they're secured are completely different.
Securing a server is much different than securing a desktop computer for a variety of
reasons. By default, a desktop operating system is installed to provide the user with
an environment that can be run out of the box. Desktop operating systems are sold
on the premise that they require minimal configuration and come loaded with as
many applications as possible to get the user up and running. Conversely, a server's
operating system should abide by the Principle of Least Privilege, which states that it
should have only the services, software, and permissions necessary to perform the
tasks it's responsible for.
First, the following law applies to general security practices: Security only works if
the secure way also happens to be the easy way. This is the most important law for
any system administrator. If a security policy is so tight that people can't perform
their job tasks, they're going to find ways to circumvent the security put in place,
sometimes creating a greater vulnerability than the policy was put in place to
prevent. The best example relates to passwords. Strong passwords should be part
of any security policy, but sometimes policies go too far. Requiring users to
remember a password that is 15 characters long and that consists of uppercase
letters, lowercase letters, numbers, and symbols is asking for a high percentage of
users to write their password on a post-it note and attach it to their monitor.
Four of Culp's laws apply directly to the material covered in this tutorial:
• If you don't keep up with security fixes, your network won't be yours
for long. Attackers find vulnerabilities every day. As a system
administrator. you need to make sure your system is updated. But this
brings you to a difference between hardening a desktop and hardening a
server. Generally, updates to the GNU/Linux desktop should be installed
when they're published. When you're dealing with the server, you should
test it in a research or development server environment before applying
the fix to your production server, to make sure the patch doesn't interfere
with the operations of the server or the users.
• Eternal vigilance is the price of security. In an effort to make sure your
GNU/Linux server is secured, you must constantly check logs, apply
use that server for something other than its intended use. Not only does
using the server for other tasks take resources away from it performing its
main task, it exposes the server to threats it would not likely see without
the software installed on it.
Among other things, you must decide whether to install a graphical user interface.
For years, GNU/Linux admins have held a certain pride in being able to completely
administer their networks and servers from a command-line interface. But in recent
years, some system administrators have begun administering their GNU/Linux
servers through a GUI. The choice to install a GUI such as the X Window System
has sparked debate on various forums. On one hand, defenders of the
command-line interface bring up the fact that the GUI can tax a system's resources
and, because it's an extra service that isn't necessary, provide attackers with
additional vulnerabilities. This side also points out that commands can be entered
quickly through the command line without the need to search through menus and
folders when performing a task.
On the other side of the debate, those who support a GUI environment argue that
the GUI process can be killed when no longer in use to save resources and prevent
any vulnerabilities from being exploited. They also argue that the GUI makes certain
tasks, such as working with a database, much easier for the administrator.
GUI login
Some people who rely on a GUI like Gnome or KDE may be
inclined to install a graphical login such as GDM. This isn't
necessary because you can log in from the command-line interface
just as easily as you would through a GUI-based login screen. The
only difference is that you have to use the sudo startx command
if you need to administer your server through a GUI.
1. Once logged into your system, you should be at the command prompt. To
install the Gnome core, type the following: sudo aptitude install
x-window-system-core gnome-core
2. Press Enter. You're asked for the sudo password. Type it, and then press
Enter again. You're informed about what is being installed.
3. To continue with the installation, type Y and then press Enter. Doing so
installs a scaled-down version of Gnome that keeps the features of the
desktop environment to a minimum and saves system resources. To
install the full-featured version of Gnome, enter sudo aptitude
install x-window-system-core gnome
4. After you press Enter, you're asked to go through the same process as
earlier. Follow along until Gnome is installed on your system.
Security by obscurity
One of the most common methods for hardening SSH is to change the port number
that is used to access it. The theory is that an attacker using the default port or TCP
22 to establish a connection will be denied access because the service is running on
a secure port.
This method of securing SSH is the center of multiple forum debates. Changing the
port number won't prevent the SSH port from being found by an attacker with a port
scanner who takes the time to scan all of the ports on your server; and for this
reason, many system administrators don't bother changing the port. But this
approach does prevent script kiddies from attacking SSH with automated tools
dedicated to finding open TCP 22 ports, and impatient attackers may grow weary of
scanning your server if they don't find SSH running in the first range of ports they
scan.
To change the SSH port address, you need to first install SSH on your server. Type
Press Enter and type your password. This command installs openssh to use for
remote logins to your server.
Once you have an SSH file to configure, you should copy the file in case something
happens when configuring. You can always revert back to the original. Follow these
steps:
2. Press Enter and provide your password to complete the backup of this
file.
Install emacs
To install emacs, use sudo aptitude install emacs Now,
you need to locate the portion of the file where the port number is
set. Once you've found this (the default is port 22), you can change
it to an arbitrary number. There are more than 65,000 ports; choose
something at the upper end of the scale, but a number you'll
remember. Remember, skilled attackers know how people think.
Changing the port number to 22222 or 22022 is a common
mistake—choose a number that isn't easily guessed.
Now, you need to change the permissions for the sshd_config file so you can
change it:
2. Press Enter. Now you can use a text editor like emacs or vi to change the
file: emacs /etc/ssh/sshd_config
Whitelist users
Another step you can take to harden SSH on your server is to allow only certain
users to use this service. This process is known as whitelisting. To create a whitelist,
you first need the usernames of the people who will be allowed to use SSH to
remotely access the server. Then, follow these steps
2. Save your configuration file, and exit your editor. You need to restart SSH
in order for the changes to take effect. You don't need to shut down your
computer -- just type sudo service ssh restart
3. Press Enter and provide your password. The service restarts and tells
you [OK].
There are many other ways to further secure SSH that are for more advanced users.
When you've had more experience working with GNU/Linux and SSH, you should
consider taking these steps.
To get started with UFW, you need to install it. Follow these steps:
2. Press Enter and enter your password. Press Enter again to install the
package.
4. Press Enter. You see the message Firewall started and enabled
on system startup. Now you can create rules for your firewall.
Remember how you changed the port for SSH earlier? To open the port through
UFW by creating a rule, type the following at the command line:
That command allows access over port 65000 and lets SSH traffic into your server.
To allow or deny traffic specifically on TCP port 65000, use the following command:
You can also allow or deny traffic according by the protocol it uses. For instance, to
block all HTTP traffic, you can use this command:
You can create more complicated rules to deny or allow a service based on its IP
address. For instance, if your desktop had the IP address 192.168.1.30 and your
server had an IP address of 192.168.1.5, you could allow only your computer's IP
address the ability to establish an SSH connection:
sudo ufw allow proto tcp from 192.168.1.30 to 192.1681.5 port 65000
You're presented with a list of rules you've written for your firewall. If you see a rule
that you wish to delete, type
The following sections walk you through the steps of installing and configuring two
programs that help to detect intrusions. Tripwire alerts you to unauthorized activity
that takes place with system files on your server, and Logwatch is a tool that can be
used to create reports for you to analyze.
Tripwire
Tripwire is a program that sets up a baseline of normal system binaries for your
computer. It then reports any anomalies against this baseline through an e-mail alert
or through a log. Essentially, if the system binaries have changed, you'll know about
it. If a legitimate installation causes such a change, no problem. But if the binaries
are altered as a result of a Trojan horse or rootkit being installed, you have a starting
point from which to research the attack and fix the problems.
To install and configure Tripwire through the command line, follow these steps:
2. Press Enter and type your password, and Tripwire downloads and
installs.
4. The next screen asks if you wish to create your passphrase during
installation. Select Yes, and press Enter.
5. The next screen informs you about how Tripwire works. The program
creates a text file that stores an encrypted database of the systems
configuration. This text file is the baseline. If any changes are made to the
system configuration, Tripwire sees the change and creates an alert. In
order for you to make legitimate changes to the system, you create a
passphrase. Select Yes and press Enter to begin building the
configuration file.
6. The following screen explains the same thing, but this time you're building
the Tripwire policy file. Again, select Yes and press Enter.
7. Once the files are built, you're prompted to enter the site-key passphrase.
You need to remember this passphrase. Select OK, and then press
Enter. You're prompted to enter your passphrase again on the next
screen.
9. Now that Tripwire has been installed, you're told the location of the
database and the binaries. With OK selected, press Enter again to
complete the configuration process.
Press Enter. You're asked to provide the local passphrase you created during the
Tripwire installation. Provide the passphrase and again press Enter. Now, Tripwire
has created the baseline snapshot of your file system. This baseline will be used to
check for changes to critical files. If such a change is detected, an alert will be sent.
You can check run an integrity check at any time by following these steps:
1. Type
2. Press Enter. You're provided with a report that is saved in the reports
directory. To view this report, use the twprint command:
3. Press Enter, and type the sudo password. You're given a different type of
prompt that looks like this:
>
At this prompt, type the location and filename of the report you wish to
print:
If you don't know the exact time you ran your report, navigate to the
directory /var/lib/tripwire/reports to see the complete filename.
As your skills advance, you can look to twadmin to further fine-tune the capabilities
of Tripwire. You can also set a cron job to e-mail you a copy of this report each day
or configure Tripwire to e-mail you if an anomaly is reported.
Logwatch
Logwatch is a great tool for monitoring your system's log files. This program requires
a working mail server on your network to e-mail the logs to you. If you wish to
change the .conf file, you need to open
/usr/share/logwatch/default.conf/logwatch.conf and look for the line that reads
MailTo. Change user.name.domain.tld to your e-mail address.
Pressing Enter sends a copy of the report to the e-mail address specified. If you
aren't running a mail server on your network but would still like to see a Logwatch
report, the following command provides it on your screen:
The output spans several screens; press Shift-Page Up to move to the beginning of
the report.
• File permissions -- Read (r), write (w), and execute (x). Each of these
permissions is also given a number: read = 4, write = 2, and execute = 1.
• Directory-level permissions -- Enter, which gives permission to enter
the directory; show, which gives permission to see the contents of the
directory; and write, which gives permission to create a new file or
subdirectory.
• How permissions are assigned -- Permissions are assigned in three
ways: by user level, group level, and other level. The user level defines
the user who created the file or directory, the group level defines the
group the user is in, and the other level is for any user outside of the
user's group.
The user permissions are granted first: for example, r/w/x means the user can read,
write, and execute the file or files in the folder. You can apply the number value to
each permission. Thus if a user can read, write, and execute, you add the
corresponding numbers 4, 2, and 1, for a total of 7. Next come the group
permissions. For instance, the other members of the user's group may be able to
read and execute, but not write. Adding up the corresponding values gives you 5.
Those in the others category can only read the files, so their numerical value is 4.
Thus, the permissions for the file or folder are 754.
When permissions are set to 777, everyone is given the ability to read write and
execute. The chmod command changes permissions for files and directories. If you
wish to change ownership of a user, use the chown command. To change group
ownership of a file or directory, use the chgrp command.
Section 9. Encryption
Encryption is the process of taking data stored on a computer and scrambling it in a
manner that makes it unreadable to anyone who doesn't possess the key to
re-create the data in its original form. Data that has been encrypted can be stored on
the local computer, stored on a network share, or transmitted to other users and
computers.
It's possible to encrypt an entire hard disk or the partitions of the disk. This should be
done at installation. You can also secure data through encryption by creating a
directory and encrypting it. For example, if you've set up a file server, you may want
to encrypt a directory that holds sensitive information.
Before you go forward with protecting your data, you need to install eCryptfs from
the Ubuntu repositories by typing
Encrypt a directory
The next step is to create a directory to encrypt. The example uses a directory called
secure, but you can name it anything you wish. Follow these steps:
mkdir ~/secure
5. Once you've answered all the questions, your directory is ready to add
files and other subdirectories to. When you're ready to secure your
directory, unmount it with
Updates
A production server should never have updates and patches installed unless they
were first tested on a test, or development, server. Because a GUI may not be
installed on your server, you have to download any updates and patches through the
terminal. When you're ready to install updates, enter the command sudo apt-get
update and then sudo apt-get dist-upgrade. In some cases, you need to
restart your server.
Malware
Many system administrators find installing antivirus software on a server running
GNU/Linux to be a waste of resources because no viruses in the wild can attack the
GNU/Linux operating system. But any GNU/Linux administrator who is running
SAMBA to share Windows files should definitely make sure an antivirus scanner like
ClamAV is installed to make sure infected files don't spread throughout your system.
Although viruses don't pose as much of a threat to the GNU/Linux server, rootkits
can cause you a headache. Rootkits are tools that attackers use to gain root-level
permissions to a system, capture passwords, intercept traffic, and create other
vulnerabilities. To combat this threat, you should install tools such as RKHunter and
chkrootkit on the server (see instructions in the "Hardening the Linux desktop"
tutorial).
If you're planning to run backup and recovery software from the Ubuntu repositories,
Sbackup is an excellent choice because it can be run from either the command line
or a GUI. When backing up server data on a corporate network, it's important that
your backup files be stored outside the server. Portable storage devices provide
large amounts of storage space at extremely reasonable prices, and they're
excellent options for storing backed-up files and directories.
Passwords
As the system administrator, you're required to set passwords for your server's root
account and possibly other sensitive accounts in your organization such as MySQL
databases or FTP connections. You can't force strong passwords for your users with
Ubuntu Server, but you can be sure you train users on how to create a strong
password.
Make sure your users' passwords contain at least three of the following: an
uppercase letter, a lowercase letter, a number, or a symbol. To further strengthen
the password, make it a policy that all passwords are at least eight characters long.
One way to teach users to use strong passwords but keep them from writing down
complex passwords on sticky notes is to have them use passphrases. Something
like Myf@voritecolorisBlue! is much easier to remember than M$iuR78$, and both
meet minimal complexity standards.
solid knowledge base on the topic of system security. Keep in mind that these
tutorials are aimed at beginners, to provide a foundation for learning more about
GNU/Linux security.
Resources
Learn
• Also by Jeffrey Orloff, "Hardening the Linux desktop" (developerWorks,
November 2008) is a step-by-step guide to securing a GNU/Linux desktop
computer.
• Scott Culp's 10 Immutable Laws of Security boils down the important facts of
security for users, and his follow-on article, 10 Immutable Laws of Security
Administration gives similar guidance for administrators.
• In the developerWorks Linux zone, find more resources for Linux developers
(including developers who are new to Linux), and scan our most popular articles
and tutorials.
• See all Linux tips and Linux tutorials on developerWorks.
• Stay current with developerWorks technical events and Webcasts.
Get products and technologies
• Download Ubuntu Server Edition to follow along with the lessons in this tutorial.
• Download Sun VirtualBox to create a virtual machine so that you can practice
with the lessons in this tutorial.
• With IBM trial software, available for download directly from developerWorks,
build your next development project on Linux.
Discuss
• Get involved in the developerWorks community through blogs, forums,
podcasts, and spaces.
Trademarks
IBM, the IBM logo, ibm.com, DB2, developerWorks, Lotus, Rational, Tivoli, and
WebSphere are trademarks or registered trademarks of International Business
Machines Corporation in the United States, other countries, or both. These and other
IBM trademarked terms are marked on their first occurrence in this information with
the appropriate symbol (® or ™), indicating US registered or common law
trademarks owned by IBM at the time this information was published. Such
trademarks may also be registered or common law trademarks in other countries.
See the current list of IBM trademarks.
Linux is a trademark of Linus Torvalds in the United States, other countries, or both.
Windows is a trademark of Microsoft Corporation in the United States, other
countries, or both.