{"id":383,"date":"2012-02-11T04:34:12","date_gmt":"2012-02-11T04:34:12","guid":{"rendered":""},"modified":"2013-02-07T12:10:28","modified_gmt":"2013-02-07T04:10:28","slug":"how-to-secure-linux-servers","status":"publish","type":"post","link":"https:\/\/ybzx.vip\/wp\/?p=383","title":{"rendered":"How to Secure Linux Servers"},"content":{"rendered":"<h1>How to Secure Linux Servers<\/h1>\n<p><\/p>\n<h2>Basic Linux Server Security <\/h2>\n<p><\/p>\n<p>Install Firewall (APF or CSF Firewall with BFD)<\/p>\n<p>&nbsp;&nbsp;ModSecurity (Web application firewall)<\/p>\n<p>&nbsp;&nbsp;ModEvasive (Prevent DDOS attacks)<\/p>\n<p>&nbsp;&nbsp;Harden SSH server<\/p>\n<p>&nbsp;&nbsp;Fix Open DNS Recursion<\/p>\n<p>&nbsp;&nbsp;Install RKhunter<\/p>\n<p>&nbsp;&nbsp;Install ClamAV (Antivirus)<\/p>\n<p>&nbsp;&nbsp;XInet Servers Hardening (Disable Telnet\/Finger or unwanted services)<\/p>\n<p>&nbsp;&nbsp;Securing PHP <\/p>\n<p>&nbsp;&nbsp;PortsEntry (tool to detect portscans)<\/p>\n<p>&nbsp;&nbsp;Harden host.conf (against IP spoofing)<\/p>\n<p>&nbsp;&nbsp;Check User Uploaded files <\/p>\n<p>&nbsp;&nbsp;Secure \/tmp Folders (noexec, nosuid)<\/p>\n<p><!--more--><\/p>\n<p>This tutorial guide covers only <strong>basic<\/strong> linux server security <br \/>&nbsp;&nbsp;tips intended for linux learners. I am writing this guide assuming that you <br \/>&nbsp;&nbsp;are running Centos 5 or later versions.<\/p>\n<p><\/p>\n<h1>Install Firewall<\/h1>\n<p><\/p>\n<p>The very first first step on securing a server is installing a firewall (atleast <br \/>&nbsp;&nbsp;IP tables based) to close all unused or unwanted ports. Once the firewall is <br \/>&nbsp;&nbsp;installed it is often considered 50% of work done. You can install CSF firewall <br \/>&nbsp;&nbsp;or APF firewall. Often BFD (brute force detection) utilities comes with firewall. <\/p>\n<p><\/p>\n<p>We will install CSF (Config security firewall) as it is easy to install with <br \/>&nbsp;&nbsp;plenty of features and easily integrated to CPanel (if you are running)<\/p>\n<p><\/p>\n<p class=\"box\">wget http:\/\/www.configserver.com\/free\/csf.tgz<\/p>\n<p>&nbsp;&nbsp;tar zxf csf.tar.gz<\/p>\n<p>&nbsp;&nbsp;sh \/csf\/install.sh <\/p>\n<p><\/p>\n<p>Follow the installer and once installed, you can start the firewall.<\/p>\n<p><\/p>\n<p class=\"box\">csf -s<\/p>\n<p>&nbsp;&nbsp;\/\/ start the firewall<\/p>\n<p>&nbsp;&nbsp;csf -r<\/p>\n<p>&nbsp;&nbsp;\/\/ restart the firewall<\/p>\n<p>&nbsp;&nbsp;csf -f<\/p>\n<p>&nbsp;&nbsp;\/\/ flush the rules or stop the firewall.<\/p>\n<p><\/p>\n<p>You can see the full installing tutorial <a href=\"csf-firewall.htm\"><strong>here<\/strong><\/a><\/p>\n<p><\/p>\n<h1>Harden SSH server<\/h1>\n<p><\/p>\n<p>Very often you will see SSH attacks from various bots trying to get access <br \/>&nbsp;&nbsp;to your server by connected to port 22 with unlimited number of login attempts <br \/>&nbsp;&nbsp;to break in to your system. Imagine attacks coming from different IPs can put <br \/>&nbsp;&nbsp;lot of load in you server. You can trace those failed attempts by checking your <br \/>&nbsp;&nbsp;log file<\/p>\n<p><\/p>\n<p class=\"box\">cat \/var\/log\/secure<\/p>\n<p>&nbsp;&nbsp;cat \/var\/log\/messages<\/p>\n<p><\/p>\n<p>To harden your SSH server, <\/p>\n<p><\/p>\n<ul>&nbsp;&nbsp;<\/p>\n<li>Run SSH on other port rather than default port 22<\/li>\n<p>&nbsp;&nbsp;<\/p>\n<li><strong> Disable Root login<\/strong><\/li>\n<p>&nbsp;&nbsp;<\/p>\n<li> Use only protocol 2<\/li>\n<p>&nbsp;&nbsp;<\/p>\n<li> Enable Public key authentication.<\/li>\n<p><\/ul>\n<p><\/p>\n<p>You can see the full SSH hardening tutorial <a href=\"ssh-attacks.htm\"><strong>here<\/strong><\/a><\/p>\n<p><\/p>\n<h1>Disable Telnet &amp; Other Unused Services<\/h1>\n<p><\/p>\n<p>You may want to disable services like telnet, finger and other unwanted services <br \/>&nbsp;&nbsp;running on your server with <strong>xinet<\/strong>.<\/p>\n<p><\/p>\n<p class=\"box\">nano \/etc\/xinetd.d\/telnet <\/p>\n<p>&nbsp;&nbsp;\/\/ OR<\/p>\n<p>&nbsp;&nbsp;nano \/etc\/xinetd.d\/krb5-telnet<\/p>\n<p><\/p>\n<p>look for lines disable=no and change to <strong>disable=yes<\/strong><\/p>\n<p><\/p>\n<p class=\"box\">chkconfig telnet off<\/p>\n<p><\/p>\n<h1>Hardening PHP for Security<\/h1>\n<p>\u63a8\u8350<\/p>\n<p>PHP is the most popular scripting language for apache and mysql. You will need <br \/>&nbsp;&nbsp;to disable system level functions in the php configuration file.<\/p>\n<p><\/p>\n<p class=\"box\">nano \/usr\/local\/lib\/php.ini<\/p>\n<p><\/p>\n<p>Look for the lines and make sure you have the lines as below..<\/p>\n<p><\/p>\n<p class=\"box\">disable_functions = <strong>exec,system,shell_exec,passthru<\/strong><\/p>\n<p>&nbsp;&nbsp;register_globals = <strong>Off<\/strong><\/p>\n<p>&nbsp;&nbsp;expose_php = <strong>Off<\/strong><\/p>\n<p>&nbsp;&nbsp;magic_quotes_gpc = <strong>On<\/strong><\/p>\n<p><\/p>\n<p> It is best to keep magic_quotes to on as otherwise you forms using <br \/>&nbsp;&nbsp;POST may be used for SQL injection attacks.<\/p>\n<p><\/p>\n<h1>Disable Open DNS Recursion (DNS Server) <\/h1>\n<p><\/p>\n<p>If you are running bind DNS server, then you might want to check your dns server <br \/>&nbsp;&nbsp;statistics with dnstools.com. You dont want to allow recursive lookups to performed <br \/>&nbsp;&nbsp;on your server other than local IP. It can also slowdown your server.<\/p>\n<p><\/p>\n<p class=\"box\">nano \/etc\/named.conf<\/p>\n<p><\/p>\n<p>Under <strong>Options &#123;<\/strong> place a line<\/p>\n<p><\/p>\n<p class=\"box\">Options &#123;<\/p>\n<p>&nbsp;&nbsp;<strong>recursion no;<\/strong><\/p>\n<p>&nbsp;&nbsp;&#8230;..<\/p>\n<p><\/p>\n<p>Then restart the bind<\/p>\n<p><\/p>\n<p class=\"box\">service named restart<\/p>\n<p><\/p>\n<p>You will also need to <strong>restrict zone transfers and notifications<\/strong> <br \/>&nbsp;&nbsp;if you are running Bind 9. Refer to: <a href=\"http:\/\/corpocrat.com\/2009\/02\/21\/how-to-secure-your-dns-server\/\">dns <br \/>&nbsp;&nbsp;server hardening<\/a><\/p>\n<p><\/p>\n<h1>Install Mod_Security<\/h1>\n<p>\u63a8\u8350<\/p>\n<p> ModSecurity is a free open source web application firewall which can help <br \/>&nbsp;&nbsp;you to guard against LFI (local file inclusion attacks) and SQL injection vulnerabilities. <\/p>\n<p><\/p>\n<p><strong> CPanel Installation:<\/strong><\/p>\n<p><\/p>\n<p>Just go to <strong>Cpanel WHM &gt; Plugins &gt; Enable Mod_Security &gt; Save<\/strong><\/p>\n<p><\/p>\n<p><strong>Source Installation:<\/strong><\/p>\n<p><\/p>\n<p>That should install mod security in your cpanel. Under apache it should show <br \/>&nbsp;&nbsp;under installed modules if you run test.php with phpinfo() in it. Try adding <br \/>&nbsp;&nbsp;some mod security rules. Installing mod_security could be sometimes complicated. <br \/>&nbsp;&nbsp;Dont use <strong>apxs<\/strong> for compiling mod_security as it causes number <br \/>&nbsp;&nbsp;of problems. <\/p>\n<p><\/p>\n<p><strong>Note: <\/strong>Mod_security needs<strong> libxml2<\/strong> and <strong>http-devel<\/strong> <br \/>&nbsp;&nbsp;libraries before it can be installed. It also requires <strong>mod_unique_id<\/strong> <br \/>&nbsp;&nbsp;enabled in apache modules. To install mod_unique_id, you have to place<\/p>\n<p><\/p>\n<p><strong>LoadModule unique_id_module modules\/mod_unique_id.so<\/strong> <\/p>\n<p><\/p>\n<p>in your httpd.conf file.<\/p>\n<p><\/p>\n<p class=\"box\">yum install libxml2 libxml2-devel httpd-devel<\/p>\n<p><\/p>\n<p>Download the latest version of mod_security for apache2 from <a href=\"http:\/\/www.modsecurity.org\">http:\/\/www.modsecurity.org<\/a><\/p>\n<p><\/p>\n<p class=\"box\">wget http:\/\/www.modsecurity.org\/download\/modsecurity-apache_2.1.7.tar.gz<\/p>\n<p>&nbsp;&nbsp;tar zxf modsecurity-apache_2.5.4.tar.gz<\/p>\n<p>&nbsp;&nbsp;cd modsecurity-apache_2.5.4<\/p>\n<p>&nbsp;&nbsp;cd apache2<\/p>\n<p><\/p>\n<p>Then <\/p>\n<p><\/p>\n<p>If you cannot find .\/configure then you will need to edit <strong>Makefile <br \/>&nbsp;&nbsp;<\/strong>and make change to <strong>top_dir = \/usr\/lib\/httpd<\/strong> (for centos)<\/p>\n<p><\/p>\n<p class=\"box\">make <\/p>\n<p>&nbsp;&nbsp;make install<\/p>\n<p><\/p>\n<p>Next, copy the rule files depending on which you want (you can also select <br \/>&nbsp;&nbsp;minimal rules file which comes with source). Make a directory named modsecurity <br \/>&nbsp;&nbsp;under <strong>\/etc\/httpd\/conf <\/strong>and copy all the modsecurity rules there. <br \/>&nbsp;&nbsp;Finally include those files in the httpd.conf file<\/p>\n<p><\/p>\n<p><strong># \/etc\/httpd\/conf\/httpd.conf<\/strong><\/p>\n<p><\/p>\n<p class=\"box\">LoadModule unique_id_module modules\/mod_unique_id.so<\/p>\n<p>&nbsp;&nbsp;LoadFile \/usr\/lib\/libxml2.so<\/p>\n<p>&nbsp;&nbsp;LoadModule security2_module modules\/mod_security2.so<\/p>\n<p>&nbsp;&nbsp;Include conf\/modsecurity\/*.conf<\/p>\n<p><\/p>\n<p>Then<\/p>\n<p><\/p>\n<p class=\"box\">\/etc\/init.d\/httpd restart<\/p>\n<p><\/p>\n<p><strong>Log Files<\/strong><\/p>\n<p><\/p>\n<p> Watch for log files to detect any errors or intrusion activity<\/p>\n<p><\/p>\n<p><strong>\/var\/log\/httpd\/modsec_audit<\/p>\n<p>&nbsp;&nbsp;\/var\/log\/httpd\/error_log<\/strong><\/p>\n<p><\/p>\n<p>If you get any errors, i have compiled a list of errors while compiling. <a href=\"http:\/\/www.webmasterpals.com\/showthread.php?t=408\" target=\"_blank\">see <br \/>&nbsp;&nbsp;here<\/a> <\/p>\n<p><\/p>\n<h1>Install Mod_Evasive<\/h1>\n<p><\/p>\n<p><a href=\"http:\/\/www.zdziarski.com\/projects\/mod_evasive\/\">ModEvasive<\/a> module <br \/>&nbsp;&nbsp;for apache offers protection against DDOS (denial of service attacks) in your <br \/>&nbsp;&nbsp;server. <\/p>\n<p><\/p>\n<p class=\"box\">wget http:\/\/www.zdziarski.com\/projects\/mod_evasive\/mod_evasive_1.10.1.tar.gz<\/p>\n<p>&nbsp;&nbsp;tar zxf mode_evasive-1.10.1.tar.gz<\/p>\n<p>&nbsp;&nbsp;cd mod_evasive<\/p>\n<p><\/p>\n<p>then run the following command for apache2&#8230;<\/p>\n<p><\/p>\n<p class=\"box\">&gt; \/usr\/sbin\/apxs -cia mod_evasive20.c<\/p>\n<p><\/p>\n<p>Once mod evasive is installed, place the following lines in your <strong>\/etc\/httpd\/conf\/httpd.conf<\/strong><\/p>\n<p><\/p>\n<p class=\"box\">&lt;IfModule mod_evasive20.c&gt;<\/p>\n<p>&nbsp;&nbsp;DOSHashTableSize 3097<\/p>\n<p>&nbsp;&nbsp;DOSPageCount 2<\/p>\n<p>&nbsp;&nbsp;DOSSiteCount 50<\/p>\n<p>&nbsp;&nbsp;DOSPageInterval 1<\/p>\n<p>&nbsp;&nbsp;DOSSiteInterval 1<\/p>\n<p>&nbsp;&nbsp;DOSBlockingPeriod 10<\/p>\n<p>&nbsp;&nbsp;&lt;\/IfModule&gt;<\/p>\n<p><\/p>\n<p>Follow the instructions in the README for more tuning of mod_evasive. This <br \/>&nbsp;&nbsp;will compile, install and activate the module in your server.<\/p>\n<p><\/p>\n<h1>Install RkHunter (Rootkit)<\/h1>\n<p> \u63a8\u8350<\/p>\n<p>RkHunter is a rootkit scanner scans for vulnerabilities, insecure files, backdoors <br \/>&nbsp;&nbsp;in your system and reports it so that you can further harden the server. Installing <br \/>&nbsp;&nbsp;RkHunter is very easy!<\/p>\n<p><\/p>\n<p class=\"box\">yum install rkhunter<\/p>\n<p><\/p>\n<p>To run checks in your system<\/p>\n<p><\/p>\n<p class=\"box\">rkhunter &#8211;checkall<\/p>\n<p>&nbsp;&nbsp;OR<\/p>\n<p>&nbsp;&nbsp;rkhunter -c<\/p>\n<p><\/p>\n<p>You can find what command options are available under rkhunter by issuing this <br \/>&nbsp;&nbsp;help command<\/p>\n<p><\/p>\n<p class=\"box\">&gt; rkhunter &#8211;help<\/p>\n<p><\/p>\n<h1>Install PortsEntry<\/h1>\n<p><\/p>\n<p>Portsentry is a tool to detect port scans and log it. Download the sorce package <br \/>&nbsp;&nbsp;of portsentry from <a href=\"http:\/\/sourceforge.net\/projects\/sentrytools\/\" target=\"_blank\">sourceforge.net<\/a><\/p>\n<p><\/p>\n<p class=\"box\">wget http:\/\/path\/to\/portsentry-1.2.tar.gz <\/p>\n<p>&nbsp;&nbsp;tar zxf portsentry-1.2.tar.gz <\/p>\n<p>&nbsp;&nbsp;make linux<\/p>\n<p>&nbsp;&nbsp;make install<\/p>\n<p><\/p>\n<p>If you get errors like while compiling<\/p>\n<p><\/p>\n<p class=\"error\">make linux<\/p>\n<p>&nbsp;&nbsp;SYSTYPE=linux<\/p>\n<p>&nbsp;&nbsp;Making<\/p>\n<p>&nbsp;&nbsp;gcc -O -Wall -DLINUX -DSUPPORT_STEALTH -o .\/portsentry .\/portsentry.c &#92;<\/p>\n<p>&nbsp;&nbsp;.\/portsentry_io.c .\/portsentry_util.c<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c: In function &#8216;PortSentryModeTCP&#8217;:<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1187: warning: pointer targets in passing argument 3 of &#8216;accept&#8217; <br \/>&nbsp;&nbsp;differ in signedness<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c: In function &#8216;PortSentryModeUDP&#8217;:<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1384: warning: pointer targets in passing argument 6 of &#8216;recvfrom&#8217; <br \/>&nbsp;&nbsp;diffe r in signedness<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c: In function &#8216;Usage&#8217;:<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1584: error: missing terminating &quot; character<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: &#8216;sourceforget&#8217; undeclared (first use in this function)<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: (Each undeclared identifier is reported only once<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: for each function it appears in.)<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: expected &#8216;)&#8217; before &#8216;dot&#8217;<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: stray &#8216;&#92;&#8217; in program<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1585: error: missing terminating &quot; character<\/p>\n<p>&nbsp;&nbsp;.\/portsentry.c:1595: error: expected &#8216;;&#8217; before &#8216;&#125;&#8217; token<\/p>\n<p>&nbsp;&nbsp;make: *** [linux] Error 1<\/p>\n<p><\/p>\n<p><strong>To fix:<\/strong><\/p>\n<p><\/p>\n<p>Open <strong>portsentry.c<\/strong> and look for the following line. There will <br \/>&nbsp;&nbsp;be a extra carriage return breaking the line and you have to <strong>delete <br \/>&nbsp;&nbsp;the carriage return<\/strong> and make single line. It should look like below.<\/p>\n<p><\/p>\n<p class=\"error\">printf (&quot;Copyright 1997-2003 Craig H. Rowland &lt;craigrowland <br \/>&nbsp;&nbsp;at users dot sourceforget dot net&gt;&#92;n&quot;);<\/p>\n<p><\/p>\n<p>Then run make and make install. That should fix it!<\/p>\n<p><\/p>\n<p>To launch portsentry<\/p>\n<p><\/p>\n<p class=\"box\">\/usr\/local\/psionic\/portsentry\/portsentry -stcp<\/p>\n<p>&nbsp;&nbsp;\/usr\/local\/psionic\/portsentry\/portsentry -sudp<\/p>\n<p><\/p>\n<p>check the log files <strong>\/var\/log\/secure<\/strong> on what portsentry is <br \/>&nbsp;&nbsp;active or not.<\/p>\n<p><\/p>\n<h1>Prevent IP Spoofing<\/h1>\n<p><\/p>\n<p>IP spoofing is a security exploit and can be prevented from placing <strong>nospoof <br \/>&nbsp;&nbsp;on<\/strong> in host.conf file. Edit the host.conf file and place the following <br \/>&nbsp;&nbsp;lines. If you run dns bind, give it preference.<\/p>\n<p><\/p>\n<p class=\"box\">order bind,hosts<\/p>\n<p>&nbsp;&nbsp;nospoof on<\/p>\n<p><\/p>\n<h1>Install ClamAV<\/h1>\n<p><\/p>\n<p>Antivirus protection is the last thing you need for your security to protect <br \/>&nbsp;&nbsp;against worms and trojans invading your mailbox and files! Just install clamav <br \/>&nbsp;&nbsp;(a free open source antivirus software for linux). More information can be found <br \/>&nbsp;&nbsp;on <a href=\"http:\/\/www.clamav.net\">clamav<\/a> website<\/p>\n<p><\/p>\n<p class=\"box\">yum install clamav<\/p>\n<p><\/p>\n<p>Once you have installed clamav in your centos&#8230;here are some of the basic <br \/>&nbsp;&nbsp;commands using the software..<\/p>\n<p><\/p>\n<p><strong>1. To update the antivirus database<\/strong><\/p>\n<p><\/p>\n<p class=\"box\">&gt; freshclam<\/p>\n<p><\/p>\n<p><strong>2. To run antivirus<\/strong><\/p>\n<p><\/p>\n<p class=\"box\">clamav -r \/home<\/p>\n<p><\/p>\n<p><strong>3. Running as Cron Daily Job<\/strong><\/p>\n<p><\/p>\n<p>To run antivirus as a cron job (automatically scan daily) just run crontab <br \/>&nbsp;&nbsp;-e from your command line. Then add the following line and save the file.<\/p>\n<p><\/p>\n<p class=\"box\">02 1 * * * root clamscan -R \/var\/www<\/p>\n<p><\/p>\n<p>This will run the cron job daily @ 1.02 AM by scanning the public html. You <br \/>&nbsp;&nbsp;can change the folder to whatever you want for mail etc.<\/p>\n<p><\/p>\n<p>Thats it! Always keep an eye for log files for any attacks or error messages!<\/p>\n<p>Source: http:\/\/www.mysql-apache-php.com\/basic-linux-security.htm<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Secure Linux Servers Basic Linux Server Security &hellip; <a href=\"https:\/\/ybzx.vip\/wp\/?p=383\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">How to Secure Linux Servers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[78,100],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-network","tag-linux","tag-security"],"_links":{"self":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=383"}],"version-history":[{"count":0,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"wp:attachment":[{"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ybzx.vip\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}