CentOS7 Server Hardening
CentOS7 Server Hardening
1. Lock down and secure the access: As soon as the server is online at the IDC and the passwords
are given to us.
2. Login in to server with the user root and password for it.
3. Disable the default firewall.
To use the iptables and ip6tables services, first check the status of firewalld and disable it by running the
following command as root:
The iptables-services package contains the iptables service and the ip6tables service.
OR
Edit /etc/sysconfig/iptables
1
BNPL – SDD CentOS 7 Hardening Guide
# vim /etc/sysconfig/iptables
2
BNPL – SDD CentOS 7 Hardening Guide
With the last command you will be able to view the Hostname which has been set.
NOTE: In the first command test.balasai.com is only given as an example. Give the actual Hostname of
the server.
# cd /root
# vi .bashrc
Add the following line at the end of the file:
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
auditd.service enabled
avahi-daemon.service enabled
crond.service enabled
dbus-org.freedesktop.Avahi.service enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
[email protected] enabled
iptables.service enabled
irqbalance.service enabled
kdump.service enabled
lvm2-monitor.service enabled
microcode.service enabled
NetworkManager-dispatcher.service enabled
NetworkManager.service enabled
rsyslog.service enabled
3
BNPL – SDD CentOS 7 Hardening Guide
sshd.service enabled
systemd-readahead-collect.service enabled
systemd-readahead-drop.service enabled
systemd-readahead-replay.service enabled
tuned.service enabled
Further tweaking may be required depending on the hardware, and the use of the server.
(1) Add a new user bn_admin (User may get changed depending upon Customer’s Server)
# useradd bn_admin
# passwd bn_admin
Give a strong password which contains alpha-numeric characters.
# vi /etc/ssh/sshd_config
Make sure that below mentioned changes are enabled.
Protocol 2
PermitRootLogin No
StrictModes yes
AllowUsers bn_admin
HostbasedAuthentication no
PermitEmptyPasswords no
X11Forwarding no
UsePrivilegeSeparation sandbox
AllowTcpForwarding yes
(3) Confirm SSH hardening: Start a new putty session to the server while you are already logged on
through existing shell.
Confirm whether you are still able to login from root. If yes then you SSH hardening is not proper.
Recheck.
You should be able to login with bn_admin as first user
Then su - press Enter key and give root password.
If you are successful then it's OK.
You can log out of the first shell.
(4) Change the root password and set a strong password which contains alpha-numeric characters.
4
BNPL – SDD CentOS 7 Hardening Guide
# passwd
Type new password,
Re-type.
# cd /usr/local/src
Package Dependencies:
Ensure openssl and openssl-devel are installed.
# rpm -q openssl
# rpm -q openssl-devel
If they are not installed, install them using:
# yum install openssl openssl-devel
As Webmin is developed in Perl, so install Perl before proceeding with Webmin installation.
# yum install perl
The rest of the install will be done automatically to the directory /usr/libexec/webmin, the
administration Username set to root and the password to your current root password. You should now
be able to login to Webmin at the URL http://<host>:10000/. Or if accessing it remotely, replace
localhost with your system's IP address.
Edit /etc/webmin/miniserv.conf
5
BNPL – SDD CentOS 7 Hardening Guide
Restart Webmin for these changes to take effect: systemctl restart webmin
Enable the below mentioned options by clicking on Yes which is parallel to each:
Include Webmin logins and logouts in actions log?
Log changes made to files by each action?
Record all modified files before actions, for rollbacks?
I] Edit /etc/hosts
# vi /etc/hosts
# vi /etc/resolv.conf
Check the resolvers to be used. The resolvers are nameservers as defined by the data center where the
server is located.
Suggested nameservers :
6
BNPL – SDD CentOS 7 Hardening Guide
# vi /etc/aliases
Press Shift G
You will notice:-
# Person who should get root's mail
#root: marc
run newaliases
# newaliases
(newaliases command will not work until and unless sendmail is not installed, so it is suggested to run
this command only after the sendmail/mail server installation without fail)
NOTE : in cPanel based servers, we get "EXIM" as mail server. There is no need of installing sendmail if
"newaliases" command does not works.
You can simply restart EXIM.
7
BNPL – SDD CentOS 7 Hardening Guide
2. Add hostname.
# vi /etc/localdomains (only if cPanel based.)
# vi /etc/hosts.deny
10. Disble IPv6 : Prevent the kernel module from loading at boot time and disable IPv6 networking.
# touch /etc/modprobe.d/disable-ipv6.conf
# echo "options ipv6 disable=1" >> /etc/modprobe.d/disable-ipv6.conf
# vi /etc/sysconfig/selinux
Set SELINUX=disabled
8
BNPL – SDD CentOS 7 Hardening Guide
Save file.
Now that the server is rebooted in locked down environment, you can do the other tasks with ease. Read
/var/log/messages completely. Make sure there are no errors related to hardware and kernel.
1. SYSCTL
Sysctl config is not a cut and paste job. sysctl configure kernel parameters at run time. Any error in this
config will not boot the system with network access. Only physical console access required to rectify,
hence it is utmost important to do the settings and verify before committing the changes.
The config depends on how the interfaces are connected. Public interface, whether eth0 or eth1 and
private interface needs to be tuned properly.
Now edit the file /etc/sysctl.conf, remove the exiting contents from file if any and paste the below
contents:
# Controls whether core dumps will append the PID to the core filename
# Useful for debugging multi-threaded applications
kernel.core_uses_pid = 1
9
BNPL – SDD CentOS 7 Hardening Guide
# Turn on execshield
kernel.exec-shield=1
kernel.randomize_va_space=1
10
BNPL – SDD CentOS 7 Hardening Guide
Save file.
# sysctl -p
If no errors, we are good to save the file.
# sysctl -w net.ipv4.route.flush=1
In order to disable IPv6 for a particular adapter(suppose if the network card name is eno16777736)
net.ipv6.conf.eno16777736.disable_ipv6 = 1
Save file.
# systemctl restart network.service
Check if any errors while restarting the service.
# sync
# sync
# reboot
2. Secure installers.
3. Install CSF/LFD
# cd /usr/local/src
# wget https://download.configserver.com/csf.tgz
# tar -xzf csf.tgz
11
BNPL – SDD CentOS 7 Hardening Guide
# cd csf
# sh install.sh
Login to Webmin.
Go to Webmin tab >> Webmin Configuration >> Webmin Modules
From local file >> /etc/csf/csfwebmin.tgz >> Install Module
http://intra2.balasai.com/mt-new/opsprocs/2016/07/configserver-security-firewall-tweak-csflfd.html
4. Install RKhunter
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:
- Looks for default files used by rootkits
- Wrong file permissions for binaries
- Looks for suspected strings in LKM and KLD modules
- Looks for hidden files
Optional scan within plaintext and binary files.
# cd /usr/local/src
Download rkhunter from the URL : http://www.rootkit.nl/projects/rootkit_hunter.html
12
BNPL – SDD CentOS 7 Hardening Guide
5. Reboot server.
1. Filesystem
noexec: Binaries are not allowed to be executed (Do not use this option with root filesystem(/))
nosuid: Blocks the operation of suid and sgid bits
nodev: Prevent any user to mount the file system
The XFS file system replaces ext4 as the default file system, and is now supported to a maximum file
system size of 500 TB, and a maximum file offset of 8 EB (sparse files).
The ext4 file system is now supported to a maximum file system size of 50 TB and a maximum file size of
16 TB. Tuning recommendations have been updated accordingly. Additionally, support for the ext2 and
ext3 file systems is now provided by the ext4 driver.
2. Yum Configuration
Install priorities.
# yum install yum-priorities
- Add priorities to repositories by adding the line in repo configs. (cd /etc/yum/repos.d/)
# vi /etc/yum.repos.d/CentOS-Base.repo
priority=N
Third Party Repos. priority=N (where N is > 10 and based on your preference)
13
BNPL – SDD CentOS 7 Hardening Guide
#!/bin/bash
YUM=/usr/bin/yum
$YUM -y -R 10 -e 0 -d 0 update
Save file.
Cpan> q
# vi /etc/webmin/miniserv.conf
Only for BNPL Servers, install wild card SSL for webmin:
http://intra2.balasai.com/mt-new/opsprocs/2015/10/installing-wild-card-ssl-certificate-of-balasaicom-
for-webmin.html
14
BNPL – SDD CentOS 7 Hardening Guide
Only root account have UID 0 with full permissions to access the system. Type the following command
to display all accounts with UID set to 0:
Only root should be displayed. Any other user is displayed then the system may be compromised.
Remove the user, check system for mitigation from a suspect compromise.
- Noowner Files
Files not owned by any user or group can pose a security problem. Just find them with the following
command which do not belong to a valid user and a valid group:
Change the ownership to root or bn_admin, if the default installs of any packages like rkhunter, webmin,
Nagios plugins etc. Have left no owners files or directories.
4. VsFTP
anonymous_enable=NO
chroot_local_user=YES
async_abor_enable=YES
anon_world_readable_only=NO
guest_enable=NO
guest_username=ftp
hide_ids=YES
max_clients=60
max_per_ip=2
session_support=NO
use_localtime=YES
user_config_dir=/etc/vsftpd/user_list
userlist_file=/etc/vsftpd/denied_users
dirlist_enable=YES
anon_umask=0027
>> Make the change for below directive: Change listen to YES
When "listen" directive is enabled, vsftpd runs in standalone mode and
listens on IPv4 sockets. This directive cannot be used in conjunction
with the listen_ipv6 directive.
listen=YES
>> Make the change for below directive: Comment the line listen_ipv6=YES
This directive enables listening on IPv6 sockets. By default, listening
15
BNPL – SDD CentOS 7 Hardening Guide
on the IPv6 "any" address (::) will accept connections from both IPv6
and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
sockets. If you want that (perhaps because you want to listen on specific
addresses) then you must run two copies of vsftpd with two configuration
files.
Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
5. Apache
Install HTTP:
# yum install httpd*
# /var/www/html/
# vi index.html
<html>
<head>
<meta http-equiv="refresh" content="1;url=https://www.balasai.com">
</head>
</html>
For customer servers, can just create a blank index.html page or put the Hostname in index.html page.
a> From:
#ServerName www.example.com:80
To:
ServerName Server_FQDN
Timeout 60
# Maximum size of the request body.
LimitRequestBody 10000
16
BNPL – SDD CentOS 7 Hardening Guide
Run httpd -S to verify changes are correct and syntax is valid. Fix errors/warnings if any.
Start httpd:
# systemctl start httpd.service
Enable httpd service at boot up:
# systemctl enable httpd
<IfModule mod_security.c>
# enable mod security
SecFilterEngine On
17
BNPL – SDD CentOS 7 Hardening Guide
6. Secure PHP
allow_url_fopen = Off
enable_dl = Off
expose_php = Off
From:
mysql.default_host =
To:
mysql.default_host = 127.0.0.1:3306
From:
;open_basedir =
To:
open_basedir = "/var/www/html/"
From:
disable_functions =
To: disable_functions = show_source, system, shell_exec, passthru, exec, popen, proc_open,
allow_url_fopen
More Secure :
To: disable_functions =
fpassthru,crack_check,crack_closedict,crack_getlastmessage,crack_opendict,psockopen,php_ini_scanne
d_files,shell_exec,chown,hell-exec,dl,ctrl_dir,phpini,tmp,safe_mode,systemroot,server_software,
get_current_user,HTTP_HOST,ini_restore,popen,pclose,exec,suExec,passthru,proc_open,proc_nice,proc
_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfif,
posix_setpgid,posix_setsid,posix_setuid,escapeshellcmd,escapeshellarg,posix_ctermid,posix_getcwd,po
six_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,
posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrli
mit,system,posix_getsid,posix_getuid,posix_isatty,posix_setegid,posix_seteuid,posix_setgid,posix_time,
posix_ttyname,posix_uname,posix_access,posix_get_last_error,posix_mknod,posix_strerror,posix_initgr
oups,posix_setsidposix_setuid
From:
;include_path = ".:/php/includes"
To:
include_path = "/usr/local/lib/php"
18
BNPL – SDD CentOS 7 Hardening Guide
display_errors = Off
display_startup_errors = Off
Configure limits:
max_execution_time = 30 ; Max script execution time
max_input_time = 60 ; Max time spent parsing input
memory_limit = 16M ; Max memory used by one script (Can be increased as per the
requirement)
upload_max_filesize = 2M ; Max upload file size (Can be increased as per the requirement)
post_max_size = 8M ; Max post size
7. Sendmail
Install the sendmail-cf, which is required in order to compile a new configuration file:
# yum install sendmail-cf
# systemctl start sendmail.service
# systemctl enable sendmail
Connect:localhost.localdomain RELAY
Connect:localhost RELAY
Connect:127.0.0.1 RELAY
Ensure that the sendmail is sending out mails from the server:
# yum install mailx
# mail –s “Subject” <Recipient Email ID>
Body
Press Ctrl+D
Run the following commands to ensure correct permissions on the mail log:
# chown root:root /var/log/maillog
# chmod 600 /var/log/maillog
PART IV: - Logwatch, Log rotation and Remote monitoring with Nagios.
Prerequisites:
# cd /usr/local/src
# yum install net-snmp net-snmp-devel openssl-devel openssl-perl xinetd
# useradd Nagios
# mkdir /usr/local/nagios
# cd /usr/local/nagios
19
BNPL – SDD CentOS 7 Hardening Guide
-edit /usr/local/nagios/etc/nrpe.cfg
Search for "dont_blame_nrpe=0"
and Change the value to 1 i.e. "dont_blame_nrpe=1"
Search for all external commands at the bottom and comment them with "#"
paste the following external commands at the end of the file:
20
BNPL – SDD CentOS 7 Hardening Guide
Add the following line at the end of file (On cPanel servers):
nagios ALL=(ALL) NOPASSWD: /usr/sbin/exim, /usr/local/nagios/libexec/
# default: on
# description: NRPE
service nrpe
{
flags = REUSE
socket_type = stream
wait = no
user = nagios
server = /usr/local/nagios/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from = 103.228.50.1 115.108.42.134
}
Configure SNMP
21
BNPL – SDD CentOS 7 Hardening Guide
# vi /etc/snmp/snmpd.conf
Remove all existing lines and add following. Edit the server names and location.
# cd /usr/local/src/
# wget RPM download link
# rpm -ivh logwatch-<downloaded>.rpm
The logwatch configuration files are in /etc/logwatch/ and the default configuration is in
/usr/share/logwatch/. This is not to be modified. We will modify the localized copy.
# cd /etc/logwatch/conf
# cp /usr/share/logwatch/default.conf/logwatch.conf logwatch.conf
# vi logwatch.conf
Find and change following parameters:
Output = mail
MailTo = root
MailFrom = root@hostname [Eg. MailFrom = [email protected]]
Detail = Med
Add the following under the option "# You can also disable certain services (when specifying all)"
Service = "-iptables"
Save file.
22
BNPL – SDD CentOS 7 Hardening Guide
3. Logrotation
# vi /etc/logrotate.conf
Change "weekly" to "daily"
Change "rotate 4" to "rotate 31"
Remove the comment for compress
23