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
- 2006-06-28
- Server side | Advanced
-
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:
- Allow outgoing connections initiated from the host
- Allow inbound ssh connections on port 2
- Allow inbound http connections on port 80
- Allow inbound https connections on port 443
- Block outbound ssh connections
- Block everything else
Write a full post in response to this!
Similar articles
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.
- Login or register to post comments
- 154011 reads
- Printer friendly version (unavailable!)




Looking for Linux hosting, reviews, coupons, etc.? See out user-voted list
Best voted contents
-
Microsoft's Secret Weapon isn't FUD, it's Inertia
Gary Richmond, 2009-06-18 -
2009: software installation in GNU/Linux is still broken -- and a path to fixing it
Tony Mobily, 2009-06-23 -
A second order virtual machine with Falcon
Giancarlo Niccolai, 2009-07-03 -
The Bizarre Cathedral - 46
Ryan Cartwright, 2009-06-22
Two fantastic free software companies that make Free Software Magazine possible:
Buzz authors
Free Software news
- Guess Who Owns unXis.de?
- Transcript of SCO's March 30, 2009 Bankruptcy Hearing
- Este tema me interesa mucho: Freesoftware Ecosystem: Communities and Colaboration http://ur1.ca/718s
- @pobice the answer to that one depends mostly on whether you prefer #freesoftware or #fauxpensource :)
- DylanJonesRT @ctrlclickblog WATCH:BigBuckBunny BRILLIANT! animation done using freesoftware!link2 download,watch+learn
Similar entries
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 60 days
-
2009: software installation in GNU/Linux is still broken -- and a path to fixing it
Tony Mobily, 2009-06-23 -
The Bizarre Cathedral - 44
Ryan Cartwright, 2009-06-08 -
Free Software Magazine caught in the 3fn shutdown crossfire
Tony Mobily, 2009-06-05 -
Is Android the key to the GNU/Linux desktop? Really?
Tony Mobily, 2009-06-12 -
The Bizarre Cathedral - 45
Ryan Cartwright, 2009-06-15
Thanks
Submitted by Anonymous visitor on Thu, 2007-04-05 12:29.
Vote!This is a great artical very down to earth practial stuff that eveyone with a web applacation should be checking for.
Thanks,
Frank
construction
Submitted by Raymond Itabor (not verified) on Fri, 2007-05-18 03:38.
Vote!I will be happy to hear from you thank's.
Thanks for putting this
Submitted by Anonymous visitor (not verified) on Tue, 2007-07-10 20:34.
Vote!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.
/tmp on virtual server
Submitted by cthings (not verified) on Sun, 2007-09-30 15:31.
Vote!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
visit
Submitted by Anonymous visitor (not verified) on Fri, 2007-10-26 08:15.
Vote!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/
Sorry to ask, but...
Submitted by Tony Mobily on Fri, 2007-10-26 17:56.
Vote!Hi,
Hummm sorry to ask, but... is this link actually legal?
Merc.