Hardening Linux Web Servers

Comprehensive security spans several disciplines, learn how to secure a system, to host securely coded PHP and Java web services

Download the whole article as PDF

Short URL: http://fsmsh.com/1255

Write a full post in response to this!


Now that I know what services are listening on which ports, I can go about securing them. In some cases, the solution will be disabling the unwanted service via inetd; in others, I will use iptables rules to block external access to that port.

In the context of a web server, I would recommended disabling all services managed by inetd (if they aren’t already).

/etc/xinetd.conf (Red Hat): this file usually has some minimalistic configuration of the logging software and then an include statement for all the files under /etc/xinetd.d, which are configuration files for each service run through the super server.

/etc/inetd.conf (Debian): Debian has a much simpler configuration layout—one simple file /etc/inetd.conf containing one line for each service managed by inetd.

iptables

The venerable iptables has been the standard Linux firewall since the 2.4 kernel. The kernels that come with Red Hat and Debian have the proper modules enabled; however, on Debian systems you may need to install the iptables user land tools. Configuring iptables is fairly simple: iptables has chains, rules and targets. iptables has three built in chains: FORWARD, INPUT, and OUTPUT. To create an effective firewall I will append rules to chains that will be matched by connection type, source or destination address or state. In more advanced configurations, it is favorable to create custom chains and then reference them in the default chains; but, to demonstrate the basic principles, I am just going to append rules to the three default chains. When a connection is being matched against the configured rules, each rule is checked. If it matches, it is executed, if not, the next rule is tested. As such, the rules allowing traffic should be appended first, and the very last line in any chain should be a deny rule. This is the most secure firewall configuration, where everything is dropped except the explicitly allowed connections.

If you use Debian, run:

  $apt-get install iptables ( to install iptables )
  $apt-cache search iptables ( to search for packages related to iptables)
  

To get started with iptables I will list the current rule set using the following command:

  $iptables --list 

(Note: Output has been modified due to formatting constraints.)

   Chain INPUT (policy ACCEPT)
   target     prot   opt     source   destination
   ACCEPT     all       anywhere  anywhere \ 
            state RELATED,ESTABLISHED

   Chain FORWARD (policy ACCEPT)
   target     prot   opt     source   destination
   ACCEPT     all       anywhere anywhere   \
                        state RELATED,ESTABLISHED

   Chain OUTPUT (policy ACCEPT)
   target     prot   opt     source   destination
   DROP       tcp       anywhere anywhere  \
                                       tcp dpt:ssh

The partial listing above shows rules that allow incoming traffic that isn’t new; that is to say: the connection has been established from inside the network. IP forwarding follows the same rule, and using ssh to connect out to other hosts is blocked.

The flush command with no options will flush all rules; if a chain is passed, all rules in that chain will be flushed. I’ll flush all rules and begin configuring the firewall.

  $iptables -F 
    or 
  $iptables -F INPUT 
  $iptables -F FORWARD
  $iptables -F OUTPUT
  

Next, I am going to append the rules to the appropriate chain. A high level overview of the firewall will be the following:

  1. Allow outgoing connections initiated from the host
  2. Allow inbound ssh connections on port 2
  3. Allow inbound http connections on port 80
  4. Allow inbound https connections on port 443
  5. Block outbound ssh connections
  6. Block everything else
Don't miss out on the other pages!
« first‹ previous123456789next ›last »

Write a full post in response to this!

0

Do you like this post?
Vote for it!

Copyright information

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is available at http://www.gnu.org/copyleft/fdl.html.

Biography

Yousef Ourabi: Yousef Ourabi is a developer in the San Francisco bay area. He is currently working at the startup he recently founded, Zero-Analog. Zero-Analog is currently developing an enterprise application, however, one of its stated goals is "to increase the rate of open source adoption in companies of all sizes, across all industries". Zero-Analog also offers consulting services, all based around open source tools, frameworks and applications.

Anonymous visitor's picture

Thanks

Submitted by Anonymous visitor on Thu, 2007-04-05 12:29.

Vote!
0

This is a great artical very down to earth practial stuff that eveyone with a web applacation should be checking for.

Thanks,

Frank

Raymond Itabor's picture

construction

Submitted by Raymond Itabor (not verified) on Fri, 2007-05-18 03:38.

Vote!
0

I will be happy to hear from you thank's.

Anonymous visitor's picture

Thanks for putting this

Submitted by Anonymous visitor (not verified) on Tue, 2007-07-10 20:34.

Vote!
0

Thanks for putting this together. Though the docs for Apache stuff are pretty good, they didn't get into SQL injection. At least now I know what to look for and can research this for more in depth knowledge.

cthings's picture

/tmp on virtual server

Submitted by cthings (not verified) on Sun, 2007-09-30 15:31.

Vote!
0

Hi,

just a quick comment:
If you're on a virtual server with no control over the fysical filesystems, but with the ability to mount filesystems, you can use a piece of system ram to create a ramdisk and use that as a place to put the session stuff from either apache, java or php:

/bin/mount -t tmpfs tmpfs -o size=16M,nodev,nosuid,noexec /mnt/ramdisk/
mount --bind /mnt/ramdisk /chroot/apache2/tmp

The above will give you a 16Mb ramdisk, which is mounted nodev,nosuid,noexec
The second mount command will add this as "tmp" to the chroot'ed apache jail created with mod_security.

Keep in mind to clear out sessions periodically!

regards,
Niels

Anonymous visitor's picture

visit

Submitted by Anonymous visitor (not verified) on Fri, 2007-10-26 08:15.

Vote!
0

Hi,
MCGRAW_HILL-Osborne_Hardening_Linux
http://www.soyo123.com/HardeningLinux/
This book is dedicated to the army of skilled people who have a vision for a world in which ideas may be freely communicated and where the application of those ideas can benefit all of society. The Linux operating system platform is one of the fruits of the exchange of such ideas, their implementation and ultimately their use the world over.This book can not cover everything that is to be known about securing Linux, but without input from many generous folks who gave their time and who continue to take great care and have pride in their efforts this book could not be a powerful tool in helping you to secure your Linux servers.
John Terpstra

http://www.soyo123.com/HardeningLinux/

Tony Mobily's picture

Sorry to ask, but...

Submitted by Tony Mobily on Fri, 2007-10-26 17:56.

Vote!
0

Hi,

Hummm sorry to ask, but... is this link actually legal?

Merc.



CariNet: Cloud computing is a reality.

Other sites

Odiogo

Free Software Magazine uses Apollo, project management and CRM for its everyday activities!