Link Exchange

Live Traffic Info

How to Harden your Linux Server

Is Linux secure? The question is much less useful, than 'Is Linux "securable"?' The answer to the latter is a definite yes.

Being securable means that Linux can be made more secure (to whatever degree necessary) by applying a clearly defined sequence

of steps that always produces the same result, and that can be automated and applied to systems that have been in operation for

a long time. It would be ideal to be able to make securing systems understandable by regular system administrators who don’t

have formal security training. However, the last requirement might be pushing it a bit, since security will likely always

require expertise.

Linux can be made more secure by hardening the system.

=================

| 1) Intruduction |===================

I wrote a step by step paper how to secure linux server with cP/WHM andApache installed. By default, linux is not secured enough but you haveto understand there is no such thing as "totally secured server/system".The purpose of this paper is to understand how to at least provide somekind of security to the server. I prefer lsws web-server without anyControl Panel at all but for this paper I have used CentOS 5 with cP/WHMand Apache web-server installed since a lot of hosting companies andindividuals out there are using it.

Let's start :)

So, you bought the server with CentOS 5 installed. If you ordered cP/WHM together with the server you can skip 2.1 step

cP/WHM should be installed now. You should be able to access cP viahttp://serverip:2082(SSL-2083) or http://serverip/cpanel and WHM viahttp://serverip:2086(SSL-2087) or http://serverip/whm. Let's configureit now.

WHM - MySQL:------------- Set some MySQL password(Don't set the same password like for the root access)-If you didn't set MySQL password someone will be able to login into the DB withusername "root" without password and delete/edit/download any db on the server.

WHM - Service Configuration - Apache Configuration - PHP and SuExec Configuration-------------------- Enable suEXEC - suEXEC = OnWhen PHP runs as an Apache Module it executes as the user/group of thewebserver which is usually "nobody" or "apache". suEXEC changes this soscripts are run as a CGI. Than means scripts are executed as the userthat created them. With suEXEC script permissions can't be set to777(read/write/execute at user/group/world level)

3.1) Keep all services and scripts up to date and make sure that you running the latest secured version.On CentOS type this into SSH to upgrade/update services on the server.[[email protected] ~]# yum upgradeor[[email protected] ~]# yum update

Configure the source. If you want to use the same config as you used forthe last php build it's not a problem but you will have to add:enable-suhosin to old config. To get an old config type this into SSH:[email protected] [~]# php -i | grep ./configure

Note: If you get an error like make: command not found or patch: Commandnot found, you will have to install "make" and "patch". It can be doneeasly. Just type this into SSH:[email protected] [~]# yum install make[email protected] [~]# yum install patch

Now check is everything as you want. Upload php script like this on the server:<?phpphpinfo();?>And open it via your browser and you will see your PHP configuration there.

It means you have to edit /usr/local/lib/php.iniType into SHH:[email protected] [~]# nano /usr/local/lib/php.iniIf you get an error, nano: Command not found, then:[email protected] [~]# yum install nano

Description : grsecurity is an innovative approach to security utilizinga multi-layered detection, prevention, and containment model. It islicensed under the GPL. It offers among many other features: -An intelligent and robust Role-Based Access Control (RBAC) system that can generate least privilege policies for your entire system with no configuration -Change root (chroot) hardening -/tmp race prevention -Extensive auditing -Prevention of arbitrary code execution, regardless of the technique used (stack smashing, heap corruption, etc) -Prevention of arbitrary code execution in the kernel -Randomization of the stack, library, and heap bases -Kernel stack base randomization -Protection against exploitable null-pointer dereference bugs in the kernel -Reduction of the risk of sensitive information being leaked by arbitrary-read kernel bugs -A restriction that allows a user to only view his/her processes -Security alerts and audits that contain the IP address of the person causing the alert

There is a lot of script kiddiez with brute forcers and they will try to crack our ssh pass because they know username is root, port is 22But we were smarter, we have changed SSH port :)Also, their "brute forcing" can increase server load, which means our sites(hosted on that server) will be slower.

SSH Legal Messageedit /etc/motd, write in motd something like this:"ALERT! That is a secured area. Your IP is logged. Administrator has been notified"

When someone logins into SSH he will see that message:ALERT! That is a secured area. Your IP is logged. Administrator has been notified

If you want to recieve an email every time when someone logins into SSH as root, edit .bash_profile(It's located in /root directory) and put this at the end of file:echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | awk '{print $6}'`" [email protected]

In order to install csf your server needs to have some ipt modulesenabled. csftest is a perl script and it comes with csf. You can checkthose mudules with it.[email protected] [~/csf]# ./csftest.plThe output should be like this:

You will have to edit csf.conf file. It's located here:/etc/csf/csf.conf

You need to edit it like this:Testing = "0"

And you need to configure open ports in csf.conf or you won't be able toaccess these ports. In most cases it should be configured like this ifyou are using cP/WHM. If you are running something on some other portyou will have to enable it here. If you changed SSH port you will haveto add a new port here:# Allow incoming TCP portsTCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2077,2078,2082,2083,2086,2087,2095,2096"# Allow outgoing TCP portsTCP_OUT = "20,21,22,25,37,43,53,80,110,113,443,587,873,2087,2089,2703"

6.2) CSF Connection LimitThere is in csf.conf CT option, configure it like thisCT_LIMIT = "200"It means every IP with more than 200 connections is going to be blocked.CT_PERMANENT = "1"IP will blocked permanentyCT_BLOCK_TIME = "1800"IP will be blocked 1800 secs(1800 secs = 30 mins)CT_INTERVAL = "60"Set this to the the number of seconds between connection tracking scans.

After csf.conf editing you need to restart csf[email protected] [~# service csf restart

6.3) SYN CookiesEdit the /etc/sysctl.conf file and add the following line in order to enable SYN cookies protection:-----------------------------------# Enable TCP SYN Cookie Protectionnet.ipv4.tcp_syncookies = 1-----------------------------------

6.4) CSF as security testing toolCSF has an option "Server Security Check". Go to WHM - Plugins - CSF -Test Server Security. You will see additional steps how to secure theserver even more. I'm writing only about most important things here andI covered most of them in the paper but if you want you can follow stepsprovided by CSF to get the server even more secured.

6.5) Mod_EvasiveModEvasive module for apache offers protection against DDoS (denial of service attacks) on your server.

-See how many IP addresses are connected to the server and how many connections has each of them.netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

===================| 7) Mod_Security |===================

Mod_Security is a web application firewall and he can help us to secure our sites against RFI, LFI, XSS, SQL Injection etc

If you use cP/WHM you can easly enable Mod_security in WHM - Plugins - Enable Mod_Security and save

Now I will explain how to install Mod_security from source.You can't install Mod_Security if you don't have libxml2 and http-devel libraries. Also, you need to enable mod_unique_id in apache modules, but don't worry, I will explain how to do it :)

You need to find a good rules for Mod_Security. You can find them atofficial Mod_Security site. Also, give a try to gotroot.com rules. Whenyou find a good rules, just put them in /etc/httpd/conf/modsecurity.conf

You need AV protection to protect the server against worms and trojansinvading your mailbox and files! Just install clamav (a free open sourceantivirus software for linux). More information can be found on clamav.website - http://www.clamav.net

Running as Cron Daily JobTo run antivirus as a cron job (automatically scan daily) just runcrontab -e from your command line. Then add the following line and savethe file.@daily root clamscan -R /home

It means clamav will be scanning /home directory every day. You can change the folder to whatever you want to scan.

==============| 9) Rootkit |==============

Rootkit scanner is scanning tool to ensure you for about 99.9%* you're clean of nasty tools. This tool scans for rootkits, backdoors and local exploits by running tests like: -MD5 hash compare -Look for default files used by rootkits -Wrong file permissions for binaries -Look for suspected strings in LKM and KLD modules -Look for hidden files -Optional scan within plaintext and binary files

10.2) PasswordsDon't use the same password you are using for the server on some other places.When the Datacenter contacts you via e-mail or phone, always requestmore informations. Remember, someone alse could contact you to get someinformation or even root passwords.

10.3) Random thoughtsNo matter what you need to secure the server, don't think you are safeonly because you are not personally involved in any shits with"hackers". When you are hosting hacking/warez related sites you are thetarget. There is no such thing as totally secured server. Most importantthings are backups, make sure you will always have an "up-to-date"offsite backups ^^

Anyhow, this is the end of my paper, I hope it will help you to get somekind of security to your server.