http://www.seleads.com/webmastering/how-to-harden-an-ubuntu-linux-installation/
http://www.andrewault.net/2010/05/17/securing-an-ubuntu-server/
https://www.owasp.org/index.php/Category:OWASP_ModSecurity_Core_Rule_Set_Project
Submitted by The Fan Club on Sun, 2012-04-29 23:33
Last Update : August 2013
This guide is based on various community forum posts, and hours of frustration.
This guide is intended as a relatively easy step by step guide to:
Requirements:
sudo apt-get install libxml2 libxml2-dev libxml2-utils
sudo apt-get install libaprutil1 libaprutil1-dev
ln -s /usr/lib/x86_64-linux-gnu/libxml2.so.2 /usr/lib/libxml2.so.2
sudo apt-get install libapache-mod-security
sudo mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sudo vi /etc/modsecurity/modsecurity.conf
SecRuleEngine On
SecRequestBodyLimit 16384000SecRequestBodyInMemoryLimit 16384000
cd /tmp
sudo wget -O SpiderLabs-owasp-modsecurity-crs.tar.gz http://github.com/SpiderLabs/owasp-modsecurity-crs/tarball/master
sudo tar -zxvf SpiderLabs-owasp-modsecurity-crs.tar.gz
sudo cp -R SpiderLabs-owasp-modsecurity-crs-*/* /etc/modsecurity/
sudo rm SpiderLabs-owasp-modsecurity-crs.tar.gz
sudo rm -R SpiderLabs-owasp-modsecurity-crs-*
sudo mv /etc/modsecurity/modsecurity_crs_10_setup.conf.example /etc/modsecurity/modsecurity_crs_10_setup.conf
cd /etc/modsecurity/base_rulesfor f in * ; do sudo ln -s /etc/modsecurity/base_rules/$f /etc/modsecurity/activated_rules/$f ; donecd /etc/modsecurity/optional_rulesfor f in * ; do sudo ln -s /etc/modsecurity/optional_rules/$f /etc/modsecurity/activated_rules/$f ; done
sudo vi /etc/apache2/mods-available/mod-security.conf
Include "/etc/modsecurity/activated_rules/*.conf"
sudo a2enmod headerssudo a2enmod mod-security
sudo /etc/init.d apache2 restart
service apache2 restart
sudo apt-get install libapache2-mod-evasive
sudo mkdir /var/log/mod_evasive
sudo chown www-data:www-data /var/log/mod_evasive/
sudo vi /etc/apache2/mods-available/mod-evasive.conf
<ifmodule mod_evasive20.c> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10 DOSLogDir /var/log/mod_evasive DOSEmailNotify EMAIL@DOMAIN.com DOSWhitelist 127.0.0.1</ifmodule>
sudo ln -s /etc/alternatives/mail /bin/mail/
sudo a2enmod mod-evasive
sudo /etc/init.d/apache2 restart
service apache2 restart
This guide is based on various community forum posts and webpages. Special thanks to all. All comments and improvements are very welcome as this is purely a personal experimental project at this point and must be considered a work in progress.
This guide is intended as a relatively easy step by step guide to:
Harden the security on an Ubuntu 12.04 LTS server by installing and configuring the following:
If you are looking for a GUI script to install and configure all the steps explained here automatically,
visit How to secure an Ubuntu 12.04 LTS server – Part 2 The GUI Installer script
Requirements:
sudo apt-get install ufwsudo ufw enable
sudo ufw status verbose
sudo ufw allow sshsudo ufw allow http
sudo vi /etc/fstab
tmpfs /dev/shm tmpfs defaults,noexec,nosuid 0 0
sudo vi /etc/ssh/sshd_config
Port <ENTER YOUR PORT>Protocol 2PermitRootLogin noDebianBanner no
sudo /etc/init.d/ssh restart
sudo groupadd adminsudo usermod -a -G admin <YOUR ADMIN USERNAME>sudo dpkg-statoverride --update --add root admin 4750 /bin/su
sudo vi /etc/sysctl.conf
# IP Spoofing protectionnet.ipv4.conf.all.rp_filter = 1net.ipv4.conf.default.rp_filter = 1# Ignore ICMP broadcast requestsnet.ipv4.icmp_echo_ignore_broadcasts = 1# Disable source packet routingnet.ipv4.conf.all.accept_source_route = 0net.ipv6.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0net.ipv6.conf.default.accept_source_route = 0# Ignore send redirectsnet.ipv4.conf.all.send_redirects = 0net.ipv4.conf.default.send_redirects = 0# Block SYN attacksnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_syn_backlog = 2048net.ipv4.tcp_synack_retries = 2net.ipv4.tcp_syn_retries = 5# Log Martiansnet.ipv4.conf.all.log_martians = 1net.ipv4.icmp_ignore_bogus_error_responses = 1# Ignore ICMP redirectsnet.ipv4.conf.all.accept_redirects = 0net.ipv6.conf.all.accept_redirects = 0net.ipv4.conf.default.accept_redirects = 0 net.ipv6.conf.default.accept_redirects = 0# Ignore Directed pingsnet.ipv4.icmp_echo_ignore_all = 1
sudo sysctl -p
sudo vi /etc/bind/named.conf.options
recursion no;version "Not Disclosed";
sudo /etc/init.d/bind9 restart
sudo vi /etc/host.conf
order bind,hostsnospoof on
sudo vi /etc/php5/apache2/php.ini
disable_functions = exec,system,shell_exec,passthruregister_globals = Offexpose_php = Offdisplay_errors = Offtrack_errors = Offhtml_errors = Offmagic_quotes_gpc = Off
sudo /etc/init.d/apache2 restart
sudo vi /etc/apache2/conf.d/security
ServerTokens ProdServerSignature OffTraceEnable OffHeader unset ETagFileETag None
sudo /etc/init.d/apache2 restart
sudo apt-get install denyhosts
sudo vi /etc/denyhosts.conf
ADMIN_EMAIL = root@localhostSMTP_HOST = localhostSMTP_PORT = 25#SMTP_USERNAME=foo#SMTP_PASSWORD=barSMTP_FROM = DenyHosts nobody@localhost#SYSLOG_REPORT=YES
sudo apt-get install fail2ban
sudo vi /etc/fail2ban/jail.conf
[ssh]enabled = trueport = sshfilter = sshdlogpath = /var/log/auth.logmaxretry = 3
[ssh]enabled = trueport = <ENTER YOUR SSH PORT NUMBER HERE>filter = sshdlogpath = /var/log/auth.logmaxretry = 3
destemail = root@localhost
action = %(action_)s
action = %(action_mwl)s
sudo vi /etc/fail2ban/jail.local
sudo /etc/init.d/fail2ban restart
sudo fail2ban-client status
sudo apt-get install psad
sudo apt-get install rkhunter chkrootkit
sudo chkrootkit
sudo rkhunter --updatesudo rkhunter --propupdsudo rkhunter --check
sudo apt-get install nmap
nmap -v -sT localhost
sudo nmap -v -sS localhost
sudo apt-get install logwatch libdate-manip-perl
sudo logwatch | less
sudo logwatch --mailto mail@domain.com --output mail --format html --range 'between -7 days and today'
sudo apt-get install apparmor apparmor-profiles
sudo apparmor_status
sudo apt-get install tiger
sudo tiger
sudo less /var/log/tiger/security.report.*
in /etc/init.d – sudo nano /etc/init.d/StartVM
.
Copy Past the following in the file and replace "My VM Name" for your vm name:
#! /bin/sh
# /etc/init.d/StartVM
#
#Edit these variables!
VMUSER=spode
VMNAME="My VM Name"
case "$1" in
start)
echo "Starting VirtualBox VM..."
sudo -H -b -u $VMUSER /usr/bin/VBoxVRDP -s "$VMNAME"
;;
stop)
echo "Saving state of Virtualbox VM..."
sudo -H -u $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
;;
*)
echo "Usage: /etc/init.d/StartVM {start|stop}"
exit 1
;;
esac
exit 0
with sudo chmod +x /etc/init.d/StartVM
.
tell the script be the first to shutdown and the last to startup.
sudo update-rc.d StartVM defaults 99 01
Reference: https://help.ubuntu.com/community/AutomaticSecurityUpdates
To apply only security patches:
First, see what patches are needed:
sudo aptitude
Install unattened upgrades:
sudo apt-get install unattended-upgrades
Execute upgrades:
sudo unattended-upgrades
See what was applied:
sudo aptitude
https://tools.percona.com/dashboard wife has no money password.
This is not perfected…..
rebuild server with the following…
Dest Machine:
cd /tmp/
rm -rf ./www/
Source Machine:
backup —> scp -r /srv/www/Java.Qbytesworld.com/ davidq@apache-999:/tmp/www/
restore —> rsync -raltWv –progress –exclude=*.rsync.log –log-file=/root/logs/www.rsync.log /mnt/nfs/fs-01.sdb1/backup/apache-01/srv/www/ /tmp/www/
Dest Machine:
/etc/init.d/tomcat7 stop
mv /tmp/www/Java.Qbytesworld.com/ /srv/www/
chown -R apache:apache /srv/www/
/etc/init.d/tomcat7 start
May be needed?
chmod -R 775 /srv/www/
http://httpd.apache.org/docs/2.2/rewrite/vhosts.html
This document supplements the mod_rewrite
reference documentation. It describes how you can use mod_rewrite
to create dynamically configured virtual hosts.
mod_rewrite
We want to automatically create a virtual host for every hostname which resolves in our domain, without having to create new VirtualHost sections.
In this recipe, we assume that we'll be using the hostname www.SITE.example.com
for each user, and serve their content out of /home/SITE/www
.
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*) /home/%1/www$1
The internal tolower
RewriteMap directive is used to ensure that the hostnames being used are all lowercase, so that there is no ambiguity in the directory structure which must be created.
Parentheses used in a RewriteCond
are captured into the backreferences %1
, %2
, etc, while parentheses used in RewriteRule
are captured into the backreferences $1
, $2
, etc.
As with many techniques discussed in this document, mod_rewrite really isn't the best way to accomplish this task. You should, instead, consider using mod_vhost_alias
instead, as it will much more gracefully handle anything beyond serving static files, such as any dynamic content, and Alias resolution.
mod_rewrite
This extract from httpd.conf
does the same thing as the first example. The first half is very similar to the corresponding part above, except for some changes, required for backward compatibility and to make the mod_rewrite
part work properly; the second half configures mod_rewrite
to do the actual work.
Because mod_rewrite
runs before other URI translation modules (e.g., mod_alias
), mod_rewrite
must be told to explicitly ignore any URLs that would have been handled by those modules. And, because these rules would otherwise bypass any ScriptAlias
directives, we must have mod_rewrite
explicitly enact those mappings.
# get the server name from the Host: header
UseCanonicalName Off
# splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
<Directory /www/hosts>
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
</Directory>
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower
## deal with normal documents first:
# allow Alias /icons/ to work - repeat for other aliases
RewriteCond %{REQUEST_URI} !^/icons/
# allow CGIs to work
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# do the magic
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
## and now deal with CGIs - we have to force a handler
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]
This arrangement uses more advanced mod_rewrite
features to work out the translation from virtual host to document root, from a separate configuration file. This provides more flexibility, but requires more complicated configuration.
The vhost.map
file should look something like this:
customer-1.example.com /www/customers/1
customer-2.example.com /www/customers/2
# ...
customer-N.example.com /www/customers/N
The httpd.conf
should contain the following:
RewriteEngine on
RewriteMap lowercase int:tolower
# define the map file
RewriteMap vhost txt:/www/conf/vhost.map
# deal with aliases as above
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
# this does the file-based remap
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/docs/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]
Available Languages: en
You will also need to add two files:
1) /etc/tomcat7inst/jmxremote.password
#monitorRole tomcat
#controlRole tomcat
user password
2) /etc/tomcat7inst/jmxremote.access
#monitorRole readonly
#controlRole readwrite
user readwrite
4)
chmod 600 /etc/tomcat7inst/jmxremote.*
chown tomcat7:tomcat7 /etc/tomcat7inst/jmxremote.*
5) Edit /etc/tomcat7inst/8xx/bin/startup.sh
Add to existing options:
export JAVA_OPTS="-Dcom.qbw.instance=811
-Xms1g -Xmx1g -XX:MaxPermSize=128m
-Djava.rmi.server.hostname=192.168.1.xx
-Dcom.sun.management.jmxremote.port=xxy9
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.password.file=/etc/tomcat7inst/jmxremote.password
-Dcom.sun.management.jmxremote.access.file=/etc/tomcat7inst/jmxremote.access
"
6)
/etc/tomcat7inst/8xx/bin/shutdown.sh
/etc/tomcat7inst/8xx/bin/startup.sh
apt-get install tomcat7
apt-get install tomcat7 tomcat7-docs tomcat7-examples tomcat7-admin tomcat7-user
Created for you:
Creating config file /etc/default/tomcat7 with new version
Adding system user `tomcat7' (UID 107) …
Adding new user `tomcat7' (UID 107) with group `tomcat7' …
apt-get install tomcat7-docs tomcat7-examples tomcat7-admin tomcat7-user
# find / -name "tomcat-users.xml"
There will be 2 tomcat-users.xml
• /etc/tomcat7/tomcat-users.xml is for the site created on port 8080
• /usr/share/tomcat7/skel/conf/tomcat-users.xml is the default used for create instance.
vi /etc/tomcat7/tomcat-users.xml (be sure to change user and password)
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="user" password="password" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>
Restart tomcat:
/etc/init.d/tomcat7 restart
CD to the directory where you want to put the new instance.
mkdir /etc/tomcat7inst
cd /etc/tomcat7inst
tomcat7-instance-create -p 8100 -c 8105 810
You are about to create a Tomcat instance in directory '810'
* New Tomcat instance created in 810
* You might want to edit default configuration in 810/conf
* Run 810/bin/startup.sh to start your Tomcat instance
Modify/verify
/etc/tomcat7inst/810/conf/server.xml
<Server port="8105" shutdown="SHUTDOWN">
<Connector port="8100" protocol="HTTP/1.1"
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
Modify/verify
/etc/tomcat7inst/810/conf/ tomcat-users.xml (same as above).
To get ROOT app
cp -r /var/lib/tomcat7/webapps/ROOT/ /etc/tomcat7inst/810/webapps/
Restart tomcat:
./810/bin/shutdown.sh
./810/bin/startup.sh
/etc/init.d/tomcat7 restart
./810/bin/shutdown.sh
./810/bin/startup.sh
/etc/init.d/tomcat7 restart
When doing a deploy, be sure to cleanup…
rm -rf /etc/tomcat7inst/810/work/Catalina/
rm –rf /etc/tomcat7inst/810/temp/
=======================================================
Make Tomcat auto-start on boot (optional)
Now you should create an INIT-File that makes it possible to start, stop and restart your Tomcat Server. This file must be located in your “/etc/init.d/” directory. You can use the following command to create a file called “tomcat” and open up that file in an editor (I used nano).
vi /etc/init.d/tomcat8x0 (example here tomcat8x0 (519.00 bytes)). For a better script look at http://wiki.debian.org/LSBInitScripts or and example /etc/init.d/skeleton
Make sure you set the right paths for the startup.sh and shutdown.sh scripts. They reside in the /bin directory of your tomcat path (use the path to which you moved the tomcat files
chmod 755 /etc/init.d/tomcat8x0
update-rc.d tomcat8x0 defaults (you must cd to /etc/init.d/ for this to work)
You can remove the autostart with the following command:
update-rc.d -f tomcat8x0 remove (you must cd to /etc/init.d/ for this to work)
Now the Tomcat Server starts automatically at system bootup.
If you wish to start/stop Tomcat Server manually like this:
sudo /etc/init.d/tomcat8x0 start
sudo /etc/init.d/tomcat8x0 stop