Now that your checking is complete, it s time to put the machine back on the network. Plug the Ethernet cable(s) back into your network card(s). If you used the method of shutting down the interface manually with the
ifconfig
command, your machine should have come back on the network after rebooting in step 5. If the interface is not back up, use a tool like
The quickest way to test whether you are back on the network is to ping the gateway. If you are not sure what your gateway IP is, type ip route and take the IP address that is on the default line. You may need to add a -b to the ping line; the command will tell you if it is necessary. Type ping gatewayIP , as in ping 192.168.1.1 . If you are back on the network, you should get messages showing the time it takes the packets to reach your gateway.
Congratulations, you are done hardening your network services. Go on to Chapter 3 and harden the security of your network interface.
Many network managers have an
This chapter steps you through the following
Identify firewall rules that may be present before you make changes
Understand the difference between firewall rules and network filters
Identify the level and type of protection necessary for your system
Install minimum protective measures to ensure the system s safety
Before moving on, take stock:
The Linux operating system has been installed and is operational.
Nonessential services have been turned off or disabled. A service that has been turned off will restart the
There are many areas of vulnerability to be addressed immediately ”you must get ready to take the steps that are outlined in the chapters that follow this one.
During installation of the Linux operating system, firewall rules may already have been created. Those new to Linux may simply accept defaults that are
The Linux operating system kernel supports the loading of application modules that allow you to add kernel-level processes as they are required. This design helps to minimize the
Immediately after the firewall filtering module has been loaded into kernel memory, it is autoconfigured to accept all network traffic as the default setting. Each module includes a
Firewall rules are normally set as early as possible to minimize the opportunity for potential system intrusion during system startup.
The firewall rules may be examined using the following command:
iptables L v
The command for ipchains -based facilities is
ipchains L v
To discover what rules are in effect on your system, follow these steps:
Start your Linux server.
Log in as root.
For Red Hat Enterprise Linux AS 3.0, select Red Hat System Tools Terminal.
For SLES8/9, click the Terminal button on the toolbar.
When the terminal shell has opened up, execute iptables -L -v .
The output you obtain will show whether firewall settings have been implemented. Figure 3-1 shows output from a system that is in its default state where rules have not been applied.
Figure 3-1:
Red Hat Linux, no firewall rules present
The firewall rules have a policy to accept all network traffic as the setting. In other words, there are no protective rules.
When firewall rules have been applied during system startup, the output of this command will be similar to that shown in Figure 3-2.
Figure 3-2:
Red Hat Linux, firewall rules enabled
In this example, the settings the user implemented offer little protection as the policy settings on the input rules permit traffic to be accepted.
The steps you have just completed have positively determined whether firewall rules are present. The point of this chapter is that you need appropriate rules to ensure that the Linux system is
The firewall rules can be easily enabled on a SLES 8 system using the YaST tool that will be described later in this chapter. The resulting firewall rules are complex and lengthy. You can gain
| Heads Up |
A simple firewall rule set may not offer the level of protection necessary, while undue complexity is a maintenance liability. Every firewall configuration must be rigorously
|
If you find that a firewall configuration has already been implemented, it is necessary to determine its suitability. Obviously, if the rules presently in effect are sufficient they should not be tampered with. On the other hand, if inadequate or inappropriate the rules must be replaced with the correct solution.
If a firewall is found on a freshly installed Linux system, it can be removed or reconfigured using standard operating system tools. If a firewall script is found on a Linux system that has been installed for some time, your best recourse is to find the person who performed the installation to identify how the firewall rules were implemented.
It is important to realize that a Linux system that has been within network reach, and has been inadequately protected, may already have been compromised. In many situations like this the most time-effective way to deal with the problem is to reinstall the system from an installation source that is of known integrity. Correction of damage done to a compromised system will almost
Commercial Linux servers like Red Hat Enterprise Linux AS 3.0 and SLES8 implement a facility known as tcp_wrappers , a technique that was designed to protect services that are implemented using the internetworking superdaemon ( inetd or xinetd ).
The tcp_wrappers are implemented using two files, one controlling what is
Many programs have the capability to support tcp_wrappers, including FTP and telnet, as well as other popular protocols except for HTTP and the X Windows System, among the most notable exceptions that use their own logging and access control systems.
Some open source applications have built-in support for tcp_wrappers. The best known example of this is Samba, where control of tcp_wrappers is effected using control statements inside Samba s smb.conf file. The two parameters that effect tcp_wrappers are hosts allow and hosts deny . You should refer to the online manual page for Samba s smb.conf file for specific guidance regarding these parameters.
For example, to find the manual entry for hosts allow in the smb.conf file, execute:
[root@sandpiper root]# man smb.conf
The
tcp_wrappers
facility can log connections as they come in, so your service sessions that weren t
daemon_list : client_list [: options ]
The daemon_list and client_list entries are mandatory. The options shown in brackets are optional. The most common option is to specify a shell script that can be executed when a rule is invoked as a result of the matching of an allow or deny rule with an incoming connection packet.
The default setting does not include rules and therefore allows all communications to take place. The default behavior of tcp_wrappers is to deny access to services defined in hosts.deny unless there is a specific rule in hosts.allow to allow it (/etc/hosts.allow takes precedence over /etc/hosts.deny). Each rule is evaluated from the top down. Rules are evaluated starting at the top of the hosts.deny and the hosts.allow files so that a later rule can override an earlier rule. The ordering of rules within each file is significant.
| Heads Up |
Careless and incorrect ordering of rules within the hosts.allow and hosts.deny files may result in incorrect tcp_wrappers operation and may result in many hours lost trying to debug a tcp_wrappers rules file. Always build the rules file one step at a time and test each entry as soon as it is added. |
SLES8/9 are provided with Web (HTML) formatted man pages. Many sites object to providing external access to man pages. The default SUSE
tcp_wrappers
configuration disables external access to the remote man pages. This is effected using an entry in the /etc/hosts.deny file as
http-rman: ALL EXCEPT LOCAL
| Heads Up |
Most documentation available on the Internet will tell you to put
ALL : ALL
in /etc/hosts.deny and then define services allowed in /etc/hosts.allowed. This is fine in most cases, but doesn't achieve what the user thinks it does, as someone with proper privileges can add a service to /etc/hosts.allow or
|
The client list in both hosts.allow and hosts.deny can be set up to show the types of entries shown in Table 3-1.
|
Client List Entry Types |
Example |
|---|---|
|
IP address |
192.168.1.100 matches single IP 192.168.1. matches entire class C network of 192.168.1.0 (if a portion of the entry is left out, everything to the right of the entry are treated as zero) |
|
Hostname |
fake.domain would match any
.fake.domain would match any requests from anything within the fake.domain |
|
Network/Netmask |
192.168.0.0/255.255.255.0 would match everything in the class C 192.168.0 network |
|
NIS (YP)
|
Anything with @ as the leading character is treated as an NIS netgroup |
Note that when using domain
|
Wildcard |
Description |
|---|---|
|
ALL |
Always matches |
|
KNOWN |
Matches a host whose name and address are known (via
|
|
LOCAL |
Any name that does not contain a dot (linux2 would match, linux2.domain would not) |
|
|
Matches a host whose name does not match the address (
|
|
UNKNOWN |
When a hostname or address is unknown because it cannot be resolved using the gethostbyXXXX() system calls that implement name resolution services |
tcp_wrappers
also can use the
EXCEPT
operator to allow you to exclude certain
So if your server, which is linux1 (192.168.1.1), needed to allow SSH connections from linux2 (192.168.1.100) and deny connections from other machines, you could set up your /etc/hosts.allow to show:
[root@sandpiper root]# cat /etc/hosts.allow # # hosts.allow This file describes the names of the hosts which are # allowed to use the local INET services, as decided # by the '/usr/sbin/tcpd' server. # sshd: 192.168.1.100 ALL: ALL: DENY
In the following hosts.allow file you can see additional entries that
Listing 3-1: A typical hosts.allow configuration file
|
|
# # hosts.allow This file describes the names of the hosts which are # allowed to use the local INET services, as described # by the '/usr/sbin/tcpd' server. # sshd: .domain EXCEPT bad.domain in.telnetd: 192.168.1.6, 10.0.0.0/255.0.0.0 EXCEPT 10.0.0.5 portmap: 192.168.1.100 ALL: ALL : DENY
|
|
In this example, you are allowing all machines on the .domain network to connect to your server using SSH except for bad.domain, which is
Another interesting capability of tcp_wrappers is that it can run commands when a condition is matched. This should be used sparingly, but for advanced customization it can provide some important functionality. For instance, you can place the following line in your /etc/hosts.deny file to have an e-mail be sent to jdoe@my.domain whenever a connection fails:
ALL:ALL : spawn (/bin/echo Security Alert from %a on %d on 'date' \ tee -a /var/log/security_Alert mail jdoe@linux1.domain)
You are having anything that doesn t match any of the rules in /etc/hosts.allow and that matches the ALL:ALL condition in /etc/hosts.deny write
Security Alert from hostname on daemon_process on date
and sending that to /var/log/security_alert as well as
|
Expansion |
Description |
|---|---|
|
%a |
Client's host address |
|
%A |
Server's host address |
|
%c |
As much client information as available |
|
%d |
Daemon process information |
|
%h |
Client hostname or address if no hostname is available |
|
%H |
Server hostname or address if no hostname is available |
|
%n |
Client hostname (or unknown or paranoid) |
|
%N |
Server hostname (or unknown or paranoid) |
|
%p |
Daemon process ID |
|
%s |
As much server information as available |
|
%u |
Client username (or unknown) |
|
%% |
Expands to a single % |
tcp_wrappers can provide an extra layer of security even when using iptables or other firewalling software, and can provide some interesting alerting mechanisms with the spawn option.
tcp_wrappers support is supplied in an unconfigured state by default on commercial Linux systems. It is left to the discretion of the administrator to configure this facility.
Network managers are constantly bombarded by companies that sell Internet security devices. Many
The information provided in this section is designed to help you, the administrator, understand the basics of firewalls and network filters.
The
Every NIC has a unique 48-bit address that is by convention
All physical network communications involve the use of MAC addresses. The MAC address contains two
Figure 3-3:
The anatomy of a MAC address
A NIC may be assigned a primary IP address, plus additional addresses (known as IP aliases). The relationship between the elements of the TCP/IP protocols is shown in Figure 3-4.
Figure 3-4:
The addressing
Network cards respond to two MAC addresses: their own unique address and the all ones address (ff:ff:ff:ff:ff:ff).
The Address Resolution Protocol (ARP), as well as the Reverse Address Resolution Protocol (RARP), makes use of the fact that the NIC will respond to the
all ones
MAC address. The protocols that make use of this behavior include ARP, RARP, ICMP, and UDP (broadcast). Excessive use of such protocols will interrupt the smooth operation of network devices and under extreme load may deny
TCP and UDP make use of port addresses. For example, the Hypertext Transport Protocol (HTTP) used for web services uses TCP port 80. Network traffic follows standards that document the structure of network messages or packets. It is from the contents of the network packet (the sequence of information that
A network packet may contain data that continues a current communication session. It may contain information that will attempt to start up a new session or close a session. Normal network traffic also includes packets from each end of a TCP session which
Any element of network communications can
A network client or server may have one network interface (called a single-homed host) that is attached to an internal (private) network, or may be attached to a public network (called an external host). A network client or server that has more than one NIC is said to be
The default configuration of the Linux kernel has ip_forwarding between interfaces disabled. Most Linux
Firewall configuration involves creation and application of policies that specify default handling of incoming packets and outbound packets, as well as forwarding of packets from the receiving interface to the destination address.
Packets that are destined for the machine itself must be routed to the loopback interface (IP address 127.0.0.1). All packets that are received from a network interface that are not destined for the machine itself must be routed to the destination or dropped. A dropped packet is effectively one that is ignored and disposed of.
Rules may be applied either to accept a packet, reject (deny) the packet, or drop it. A packet that is rejected will result in a message sent to the source address
|
Protocol |
Inside Interfaces |
External Interfaces |
|---|---|---|
|
HTTP |
Yes |
Yes |
|
HTTPS |
Yes |
Yes |
|
SMTP |
Yes |
Yes |
|
POP/IMAP |
Yes |
No |
|
DNS |
Yes |
Yes (lookup only) |
|
DHCP |
Yes |
No |
|
NBNS |
Yes |
No |
|
NBSS |
Yes |
No |
|
FTP |
Yes |
Yes (read only) |
|
Proxy |
Yes |
No |
|
Print (515, 631) |
Yes |
No |
|
High order ports (ports >1024) |
Yes |
Yes (only for existing connections) |
Rules specified may cause the firewall to examine any part of the incoming packet header information and determine how the packet will be
Figure 3-5 illustrates the points of control in a dual-homed host. Firewall rules may be based on conditions at each point. The implementation of the rules is based on inbound, outbound, or forwarding requirements. As a result, rules that affect points A and E are collated under the rules table for outgoing packets, for B and D as part of the rules table for incoming packets, and forwarding between the NICs and the host itself is specified in the rules table for forwarding of packets.
Figure 3-5:
Firewall rule bounds in a dual-homed host
A firewall may additionally be configured to hide the identity of hosts (IP addresses) that are present on a private (internal) network from the world at large. The most common modes of protection make use of IP masquerading or of network address translation (NAT).
IP Masquerade, also called IPMASQ or MASQ, allows one or more computers in a network without assigned IP addresses to communicate with the Internet using the Linux server s assigned IP address. The IPMASQ server acts as a gateway, and the other devices are invisible behind it, so to other machines on the Internet the outgoing traffic appears to be coming from the IPMASQ server and not the internal PCs.
It is much easier to guard a single point of entry than it is to guard many potential points of entry. The principle behind the Internet firewall is a machine that divides the network into the inside and the outside , with all traffic passing through the firewall. By protecting the single network firewall, the entire internal network can be protected. Masquerading allows machines on the inside to get out, without allowing foreign machine access to systems inside the private network. Masquerading rewrites the IP headers of outbound internal packets, making it appear that they all came from the firewall. Reply packets are treated by the reverse of the outgoing IP header translation, and are then forwarded to the appropriate internal machine. Thus, internal machines are allowed to connect to the outside world. However, external machines cannot find internal machines, since they are aware of only the IP address of the firewall external interface. The result is that they cannot attack the internal machines directly.
Network address translation is a more general process whereby IP addresses in one address space (perhaps a private network) will be translated into another address space (say, a public address range) by a router or equivalent device. Unlike IP masquerading, NAT may map each address that originates from an internal network to its own external address. Additionally, an internal IP address that has been translated to an external address may well be capable of being reached by a foreign system simply by connecting to the unique external address that the internal address has been mapped to. This makes possible the use of NAT for a very wide range of applications, including:
Load distribution routing
Clustering of network servers and services
Virtual server
NAT was invented in the early 1990s. It is specified by RFC 1631 and was
The Linux kernel version 2.4 and more recent supports a wide array of IP masquerading and NAT techniques. Additionally, Red Hat Enterprise Linux AS 3.0 and SLES8 both provide the Linux Virtual Server (LVS) application, which makes it possible to implement load distribution routing as well as load distribution NAT.
NAT may be used to implement IP masquerading in the Linux operating system kernel, therefore it is important to be aware what NAT entails. It is beyond the scope of this book to address the details of IP masquerading and NAT technologies, other than to
Now that you know what a firewall is, there is one important factor that
A stateful firewall
The Linux kernel
iptables
facility can track connection state and