Hey,
A while ago I was working on a guide and script to automate installing and securing servers on Centos6. Since Centos6 does not cut it anymore for me and Centos7 is not supported, I want to help people who still use Centos6 or want to have a simple guide how to secure their server.
Hint: I use several e-mail addresses in my production. Depending of the content of the automated e-mails I send them to a monitoring mailbox, alarm mailbox, or other. This way, I'm always up-to-date with my systems through mail.
Software to install: Lynis (a hardening check tool. This tool will tell you where possible security risks are). NCDU (a handy tool to check how big directories are, comparable with treesize for windows) RKHunter (Rootkithunter looks for file integrity and possible rootkits on your system) LMD (Linux Mallware Detect looks for malicious softwar eon your system. Very useful for webservers!) Fail2Ban (Bans brute force attempts for various systems) tcpdump (a tool like wireshark, to review network traffic) csf & lfd (scripts that block and ban intrusions)
Here we go:
Step1 Install repositories:
mkdir /tmp/epel cd /tmp/epel wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm
Step 2: Install some basic security tools and update the system:
yum update -y yum install -y fail2ban
vim /etc/fail2ban/jail.local
enter following:
[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space separator. ignoreip = 127.0.0.1 # "bantime" is the number of seconds that a host is banned. bantime = 3600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 3 [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com] logpath = /var/log/secure maxretry = 5
Do not forget to alter the destination and the sender.
Start fail2ban automatically:
chkconfig --level 23 fail2ban on
service fail2ban start
You can also add other jails for other systems (mail, apache, ...). To learn more, there's a lot on google.
Setup rkhunter:
yum install -y rkhunter rkhunter --update rkhunter --propupd vim /etc/cron.daily/rkhunter.sh
Enter the following:
#!/bin/sh ( rkhunter --versioncheck rkhunter --update rkhunter --cronjob --report-warnings-only ) | /bin/mail -s 'rkhunter Daily Run (SERVERNAME)' destinationmail@example.com
Don't forget to set the e-mail address.
Change the chmod.
chmod 755 /etc/cron.daily/rkhunter.sh
Install NCDU, lynis and tcpdump
yum install -y ncdu lynis tcpdump
Install LMD:
mkdir /tmp/lmd cd /tmp/lmd wget http://www.rfxn.com/downloads/maldetect-current.tar.gz tar xfz maldetect-current.tar.gz cd maldetect-* ./install.sh vim /usr/local/maldetect/conf.maldet Enable e-mail alertuser enable quar_hits
You can change some rules in this document to search deeper and harder, but watch out not to set this to strong. It has a tendency to detect virtualmin files as malicious (backup scripts).
Add a user for yourself:
useradd username passwd username vim /etc/sudoers
Somewhere in the code of the sudoers file, you will find the following root user defined:
## Allow root to run any commands anywhere root ALL=(ALL) ALL
Add under it a line like:
username ALL=(ALL) ALL
Configure SSH:
vim /etc/ssh/sshd_config
Change or add the following:
Protocol 2 Port 6000 LoginGraceTime 30 MaxAuthTries 3 PermitRootLogin no AllowUsers username
Restart sshd
service sshd restart
At this point you should change your root password to a LONG string and save it somewhere in your password safe. In normal circumstances you will not need it anymore. You will from now on login with your username. If you need to access as root, you can use the command "sudo su" followed by your username's password.
Logout completely and login again over ssh. Mind the port you set to 6000.
After logging in, change user to root:
sudo su
Let's set-up some IP tables:
vim ~/firewall.sh
Enter the following rules. Please read the comments to see if you need the rule.
#!/bin/bash service iptables start iptables -F iptables -A INPUT -p tcp --dport 6000 -j ACCEPT #SSH if this port is closed, no ssh is possible. iptables -A INPUT -s xx.xx.xx.xx -j ACCEPT #Home Set your home IP in here if it's a fixed IP. This will allow you to always connect to your server. iptables -A INPUT -p icmp --icmp-type 8 -s xx.xx.xx.xx -j ACCEPT #Ping is by default not allowed. If your datacenter (like ovh) forces you to open the port for monitoring reasons, you can allow it here. iptables -A INPUT -p tcp --dport 10055 -j ACCEPT #zabbix agent (monitoring server) iptables -A INPUT -p tcp --dport 10051 -j ACCEPT #zabbix agent(monitoring server) iptables -A INPUT -p tcp --dport 10050 -j ACCEPT #zabbix agent(monitoring server) iptables -A INPUT -p tcp --dport 443 -j ACCEPT #SSL iptables -A INPUT -p tcp --dport 80 -j ACCEPT #http iptables -A INPUT -p tcp --dport 161 -j ACCEPT #snmp iptables -A INPUT -p tcp --dport 995 -j ACCEPT #POP3 SSL iptables -A INPUT -p tcp --dport 30050 -j ACCEPT #virtualmin (I change the default virtualmin port to something not so default to avoid brute force. iptables -A INPUT -p tcp --dport 20000 -j ACCEPT #webmin iptables -A INPUT -p tcp --dport 10000 -j ACCEPT #usermin iptables -A INPUT -p tcp --dport 10050 -j ACCEPT #usermin iptables -A INPUT -p tcp --dport 465 -j ACCEPT #SMTP SSL iptables -P INPUT DROP #all other ports will be dropped. iptables -P FORWARD DROP # we do not allow forwarding iptables -P OUTPUT ACCEPT # we allow all outgoing connections. You can tighten this. iptables -A INPUT -i lo -j ACCEPT #Accept the loopback interface iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #accept all connections that are already established or related /sbin/service iptables save #this line will save the iptables iptables -L -v #this line will show the iptables after saving
The iptables will work for all new connections, so it will not drop your SSH if you made a mistake. Change rights and run:
chmod 755 ~/firewall.sh ~/firewall.sh
At this point you should check if your ssh connection will still work for new connections. DO NOT CLOSE your current session, but open up a new one and try to connect to the server. If you get a connection refused, you did something wrong in the IP tables. the "iptables -F" command will remove all current iptables.
Hardening
vim /etc/sysconfig/init change: SINGLE=/sbin/sulogin vim /etc/inittab add: ~:S:wait:/sbin/sulogin vim /etc/named.conf Set version to “none” in options
Install logwatch
yum install –y logwatch vim /usr/share/logwatch/default.conf/logwatch.conf change: • Mailto = yourlog@mail.com • Mailfrom = logwatch@SERVER.domain.com • Detail = Med • Disable Service = “-*” (remove all disabled services)
Final approach:
Run lynis
./lynis --check-all -Q
To check:
grep Warning /var/log/lynis.log grep Suggestion /var/log/lynis.log
Update the rkhunter database of current files on the system, run rkhunter and let maldet scan your system (this will take a while)
rkhunter --propupd rkhunter -c maldet --scan-all /
A final step: CSF and LFD
http://configserver.com/cp/csf.html
Install the script. There is a GUI in webmin available for this.
In case of problems. To check recently changed files:
find /var/www/ -type f -exec stat --format '%Y :%y %n' {} \; | sort -nr | cut -d: -f2- | head -100
After all this, you can start to install virtualmin on a secure server. I hope this small guide will help some people establish safer servers.
As a follow-up, here is the script I made.
IT IS NOT COMPLETE, IT DOES NOT work as it should. This is only for interested people looking to fix it, improve it and know what they are doing.
Howdy,
That's a nice guide you have there! I just wanted to mention regarding your comment "CentOS 7 is not supported" -- Virtualmin does support CentOS 7 actually. You are welcome to use Virtualmin on that distro/version.
-Eric
Great! But since most commands are different, this guide cannot be blindly used.
Now I have to consider reinstalling my new server before it goes live...
My previous reply has a partly automated script inside, but it does not want to show. Did I do something wrong?
Thanks! Although it is an old post, i would like to thank you for it :)