Hi guys this is just a basic linux based server security setup that can help you out somewhat.
Ok so basically first thing is first
If you have cpanel installed but not CSF
log into your ssh the port is usually 22 if you have not changed it yet
in ssh type in this
NOTE* This is for free CSF
Code:
rm -fv csf.tgz
wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
Basically what it is doing is downloading the file
csf.tgz to your webserver and taking on the extension .tgz using the
command and then changes to the directory to csf
and then installs it
now we are going to see if you are running the iptables
type this in your ssh
Code:
perl /etc/csf/csftest.pl
this is basically stating for it to run the script csftest.pl script
and inside that script
for future notice you should not use any other
iptables firewall configuration script
so there we go we installed csf now if you want to edit it
you can via ssh by using the pico command pico is user friendly or via cpanel whm
csf will work with
DirectAdmin , cpanel/ preconfigured wise
now we need to test if klogd us running most vps server disable it
go here
and make sure that any klogd lines are not commented out. Also after done editing restart klogd
Webmin Module Installation/Upgrade
Install csf as above
Install the csf webmin module in:
Webmin > Webmin Configuration > Webmin Modules >
how to uninstall it
Code:
On cPanel servers:
cd /etc/csf
sh uninstall.sh
On DirectAdmin servers:
cd /etc/csf
sh uninstall.directadmin.sh
On generic linux servers:
cd /etc/csf
now that we got csf installed head on over to your whm
scroll all the way down and click config server security
after that click check server security it will display a whole lot of red shit
these are errors we aer going to need to fix now bare in mind some of these you usually dont have to fix but that depends on you
Seeing that i aleady dont this i dont really see the errors anymore so ill try to remember them
Basically most of them you will have to edit through tweak settings and of course mysql , easyapache and such
also for more ease on editing the files in the server download winscp
WinSCP :: Download
Login to root and there you can edit all of your files through there instead of editing through ssh.
__________________________________________________ __________________________________________________ ____________________________________
PHP SECURITY
For the php disabling functions i reccomend this but do realize these functions being disabled can cause errors for your clients or such
usual php location
Code:
/usr/local/lib/php.ini
Code:
apache_child_terminate, apache_setenv, define_syslog_variables,
escapeshellarg, escapeshellcmd, eval, exec, fp, fput, ftp_connect,
ftp_exec, ftp_get, ftp_login, ftp_nb_fput, ftp_put, ftp_raw,
ftp_rawlist, highlight_file, ini_alter, ini_get_all, ini_restore,
inject_code, mysql_pconnect, openlog, passthru, php_uname,
phpAds_remoteInfo, phpAds_XmlRpc, phpAds_xmlrpcDecode,
phpAds_xmlrpcEncode, popen, posix_getpwuid, posix_kill, posix_mkfifo,
posix_setpgid, posix_setsid, posix_setuid, posix_setuid, posix_uname,
proc_close, proc_get_status, proc_nice, proc_open, proc_terminate,
xmlrpc_entity_decode,shell_exec, syslog,
After that
Asuming you have php harden installed aka susohin lets move on
I dont have it turned on but its better safe than sorry so if you want to enable php safe_mode
Safe mode is an easy way to lock down the security and functions you can use. PHP.net explains php safe_mode as, "The PHP safe mode is an
attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP
level, but since the alternatives at the web server and OS levels aren't very realistic, many people, especially ISP's, use safe mode for
now."
Now that you have enabled php safe mode lets customize your safe mode to work on websites.
When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files
(e.g. httpd.conf) and .htaccess files.
how to turn on safe mode?
Ctrl + W and type in: safe_mode
you will see
make it look like this
Now save the file by ctrl + x then y
Restart the Apache web server by
Code:
/etc/init.d/httpd restart
Now how to make it work with sites some scripts require safe mode to be turned off but its not so lets fix that
SSH to your server and login as root.
Then find the httpd.conf, normally it’s in /etc/httpd/conf/ or /usr/local/apache/conf/
If it’s not in either of those places try search for it: locate httpd.conf
Then find the site you wish to edit.
Ctrl+W and type in the domain name
Code:
<VirtualHost ***.***.***.***>
ServerAlias www.domain.net domain.net
ServerAdmin webmaster@domain.net
DocumentRoot /home/domain/public_html
BytesLog domlogs/domain.net-bytes_log
ServerName www.domain.net
User domain
Group domain
CustomLog domlogs/domain.net combined
ScriptAlias /cgi-bin/ /home/domain/public_html/cgi-bin/
</VirtualHost>
Now add this line:
Code:
php_admin_flag safe_mode Off
if that code does not work type this
Code:
php_admin_value safe_mode 0
now you have to choose one it can not be both and in the end it should look like this
Code:
<VirtualHost ***.***.***.***>
ServerAlias www.domain.net domain.net
ServerAdmin webmaster@domain.net
DocumentRoot /home/domain/public_html
php_admin_flag safe_mode Off
BytesLog domlogs/domain.net-bytes_log
ServerName www.domain.net
User domain
Group domain
CustomLog domlogs/domain.net combined
ScriptAlias /cgi-bin/ /home/domain/public_html/cgi-bin/
</VirtualHost>
Now save the changes.
Ctrl + X then Y
Restart the Apache web server by
Code:
/etc/init.d/httpd restart
If you run into trouble after editing httpd.conf you can run the apachectl configtest
in shell.
Help?
PHP customzing help here
http://php.us.themoes.org/manual/en/...on.changes.php
now The biggest problem with PHP is that on cPanel servers is that PHP will run as nobody. When someone sets a script to 777 access that means the
nobody user has write access to that file. So if someone on the same shared server wrote a script to search the system for 777 files they
could inject anything they wanted, compromising the unsuspecting users account.
PHPsuexec makes PHP run as the user so 777 permissions are not allowed. There are a few downfalls to PHPsuexec but
I think it's required on a shared environment for the security of everyone. Safe_mode doesn't prevent you from compromising other users
files. This is where PHPsuexec comes in, it stops the user from being able to read another users files. It also makes it easier for you, the
administrator, to track PHP mail function spamming and lots of other issues caused by PHP scripts because now you can easily track it ot the
users account responsible.
For this you will need to recompile PHP with suexec. On cPanel /scripts/easyapach has this build in
Stop PHP nobody Spammers
First of all there is 2 ways to do this
in ssh type this
Code:
find / -name "[Ff]orm[mM]ai*
find / -name "[Cc]giemai*"
chmod a-rwx /path/to/filename
or you can do this
Turn off exim while we do this so it doesn't freak out.
[code]
/etc/init.d/exim stop
[code]
backup your original /usr/sbin/sendmail file. On systems using Exim MTA, the sendmail file is just basically a pointer to Exim itself.
Code:
mv /usr/sbin/sendmail /usr/sbin/sendmail.hidden
Create the spam monitoring script for the new sendmail
Code:
pico /usr/sbin/sendmail
or just use winscp
Paste in the following:
Code:
#!/usr/local/bin/perl # use strict;
use Env;
my $date = `date`;
chomp $date;
open (INFO, ">>/var/log/spam_log") || die "Failed to open file ::$!";
my $uid = $>;
my @info = getpwuid($uid);
if($REMOTE_ADDR) {
print INFO "$date - $REMOTE_ADDR ran $SCRIPT_NAME at $SERVER_NAME n";
}
else {
print INFO "$date - $PWD - @infon";
}
my $mailprog = '/usr/sbin/sendmail.hidden';
foreach (@ARGV) {
$arg="$arg" . " $_";
}
open (MAIL,"|$mailprog $arg") || die "cannot open $mailprog: $!n";
while (<STDIN> ) {
print MAIL;
}
close (INFO);
close (MAIL);
Change the new sendmail permissions
Code:
chmod +x /usr/sbin/sendmail
Create a new log file to keep a history of all mail going out of the server using web scripts
Code:
touch /var/log/spam_log
chmod 0777 /var/log/spam_log
Start Exim up again
Code:
/etc/init.d/exim start
Monitor your spam_log file for spam, try using any formmail or script that uses a mail function - a message board, a contact script.
Code:
tail - f /var/log/spam_log
log rotation:
Your spam_log file isn't set to be rotated so it might get to be very large quickly. Keep an eye on it and consider adding it to your logrotation.
Code:
pico /etc/logrotate.conf
FIND:
Code:
# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
ADD BELOW:
Code:
# SPAM LOG rotation
/var/log/spam_log {
monthly
create 0777 root root
rotate 1
}
You may also want to chattr + i /usr/sbin/sendmail so it doesn't get overwritten.
__________________________________________________ __________________________________________________ _______________
Disable Open DNS Recursion
If you are running bind DNS server, then you might want to check your dns server statistics with dnstools.com. You dont want to allow recursive lookups to performed on your server other than local IP. It can also slowdown your server.
nano /etc/named.conf
Under Options { place a line
Code:
Options {
recursion no;
.....
Then restart the bind
Code:
service named restart
You will also need to restrict zone transfers and notifications if you are running Bind 9. Refer to: dns server hardening
__________________________________________________ __________________________________________________ _______________
Install Mod_Security
ModSecurity is a free open source web application firewall which can help you to guard against LFI (local file inclusion attacks) and SQL injection vulnerabilities.
CPanel Installation:
Just go to Cpanel WHM > Plugins > Enable Mod_Security > Save
Source Installation:
That should install mod security in your cpanel. Under apache it should show under installed modules if you run test.php with phpinfo() in it. Try adding some mod security rules. Installing mod_security could be sometimes complicated. Dont use apxs for compiling mod_security as it causes number of problems.
Note: Mod_security needs libxml2 and http-devel libraries before it can be installed. It also requires mod_unique_id enabled in apache modules. To install mod_unique_id, you have to place
LoadModule unique_id_module modules/mod_unique_id.so
in your httpd.conf file.
Code:
yum install libxml2 libxml2-devel httpd-devel
Download the latest version of mod_security for apache2 from
ModSecurity: Open Source Web Application Firewall
Code:
wget http://www.modsecurity.org/download/modsecurity-apache_2.1.7.tar.gz
tar zxf modsecurity-apache_2.5.4.tar.gz
cd modsecurity-apache_2.5.4
cd apache2
Then
If you cannot find ./configure then you will need to edit Makefile and make change to top_dir = /usr/lib/httpd (for centos)
Next, copy the rule files depending on which you want (you can also select minimal rules file which comes with source). Make a directory named modsecurity under /etc/httpd/conf and copy all the modsecurity rules there. Finally include those files in the httpd.conf file
# /etc/httpd/conf/httpd.conf
Code:
LoadModule unique_id_module modules/mod_unique_id.so
LoadFile /usr/lib/libxml2.so
LoadModule security2_module modules/mod_security2.so
Include conf/modsecurity/*.conf
Then
Code:
/etc/init.d/httpd restart
Log Files
Watch for log files to detect any errors or intrusion activity
/var/log/httpd/modsec_audit
/var/log/httpd/error_log
If you get any errors, i have compiled a list of errors while compiling. see here
__________________________________________________ ________________________________
Install Mod_Evasive
ModEvasive module for apache offers protection against DDOS (denial of service attacks) in your server.
Code:
wget http://www.zdziarski.com/projects/mod_evasive/mod_evasive_1.10.1.tar.gz
tar zxf mode_evasive-1.10.1.tar.gz
cd mod_evasive
then run the following command for apache2..
Code:
> /usr/sbin/apxs -cia mod_evasive20.c
Once mod evasive is installed, place the following lines in your /etc/httpd/conf/httpd.conf
Code:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
</IfModule>
Follow the instructions in the README for more tuning of mod_evasive. This will compile, install and activate the module in your server.