Cameron Securing Your Linux Servers
Cameron Securing Your Linux Servers
Outline
Introduction
Vectors of attack
External – network services
Internal – trusted and non-trusted users
Installation time – trim your package list
Pre-production – port scan, process scan &
harden your server
Firewalls (iptables)
tcp_wrappers
Outline
xinetd
Security Enhanced Linux (SELinux)
Production
Periodically port scan and check for unknown
processes against baseline
Introduction
Welcome!
Today we will only be talking about hardening a
single Linux host – 90 minutes is not enough
time to go too terribly deep into security
For security training, consider courses like:
RHS333, Red Hat Enterprise Security: Network
Services
RHS429, Red Hat Enterprise SELinux Policy
Administration
For additional security information, see
https://www.redhat.com/security/updates/
Vectors of Attack
External
Most commonly considered attack vector.
Includes buffer overflows, dictionary/brute
force attacks, malformed URLs and the like
Internal
Attacks from within areas considered to be
secure. More and more sensitive data is lost
to internal attacks than external. Can be
trusted staff who abuse privileges or non-
trusted staff who exploit weaknesses of interal
networks. Also consider social engineering -
combination internal/external.
Installation Time
Install only the minimum necessary software to
do the job for which the system is designed.
A web server probably doesn't need gcc and friends
installed.
You can get a good handle on what software
groups to install by looking at the various
comps*.xml files
egrep "<name>|packagereq" /path/to/comps.xml
Note that each channel will have its own comps
file
Server, Cluster, ClusterStorage, VT
Installation Time
Once you've determined the package list,
generate a kickstart file.
Automated installs increase security by
decreasing variance.
Automated installs decrease recovery time if
compromised. If you know exactly how all
your systems are configured, you know exactly
what to fix on all of them.
Installation Time
You can define the following in %packages:
Individual packages (package)
Package groups (@Group Name)
Packages to skip (-package)
Installation Time
Use %post to:
Turn off unnecessary services (better you
should not install any)
Fix settings like ipv6 or authentication
Add any user accounts (use /sbin/grub-md5-
crypt to generate passwords)
Run any shell code needed to tighten things up
Install any packages outside the Red Hat
install tree
Installation Time
install
url --url http://ml110.redhat.com/ty/gWwGX8y4
key 014fce2d4495830d
lang en_US.UTF-8
keyboard us
skipx
network --device eth0 --bootproto dhcp
rootpw --iscrypted $1$agkorjqJ$KeeFnItYAS.l.uUcjxrxR1
firewall --disabled
authconfig --enablemd5 --enableshadow
selinux --disabled
timezone America/Chicago
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
clearpart --all
part /boot --fstype ext3 --size=100
part swap --size=1024
part / --fstype ext3 --size=1000 --grow
%packages
@base
@Web Server
-tux
%post
/sbin/chkconfig foo off
/bin/rpm -Uvh http://ml110.redhat.com/pub/packages/mypackage-1.0-1.i386.rpm
/bin/echo install ipv6 /bin/true > /etc/modprobe.conf
/usr/sbin/groupadd admins
/usr/sbin/useradd -G admins -p '$1$uWyP9$RRM.DRsYpNcAUF6/KD/WV/' -c “Thomas Cameron” tcameron
Pre-production
Before your system goes into production:
port scan
process scan
harden your server
Pre-production
Port scan your server
nmap, nmapfe
Scan the host from another machine or
machines
Pre-production
nmap, nmapfe
Command line or GUI driven tools to do
portscans
Don't portscan servers unless you know it is
OK to do so!
• Your systems
• You have documented permission to scan them
Pre-production
Determine which services are supposed to
be listening and make sure that those ports
are open. If anything else is open,
determine the service which is running and
remove it.
lsof -i :[port] can help you determine which
service is listening on what port
Record what is listening – capture a
baseline reading
Pre-production
Process Scanning
Do a process scan to see what is running and
who is running it.
• ps -aux
• ps -ef
Pre-production
In the previous output, does it make sense
to have hpiod running on, say, a web
server?
Pre-production
Probably not. Remove it or at least disable
it.
/sbin/chkconfig hplip off
/sbin/service hplip stop
yum remove hplip
Note that hplip is not necessarily a
dangerous service to have running. It's
just that it is not needed, and as such,
should be removed.
Pre-production
As with external ports, create a baseline of
known acceptable running processes
Don't rely on your memory. Document
listening ports and running processes.
Easy for you to reference months or years later
Easy for other staff to reference should you not
be available
Pre-production
Harden Your Systems
iptables
xinetd
pluggable authentication modules (PAM)
Security Enhanced Linux (SELinux)
Application Specific
• sendmail
• NFS
iptables (netfilter)
Kernel-based traffic filter/firewall toolkit
Traffic is managed through rules, called
“chains:”
INPUT
FORWARD
OUTPUT
Those packets can be sent to a target,
including
ACCEPT
DROP
REJECT
Thomas Cameron: Securing Your Linux Servers Slide 27
Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Red Hat
iptables (netfilter)
Additionally, packets can be modified using
Network Address Translation
-t nat
• DNAT (PREROUTING)
• SNAT (POSTROUTING)
• MASQUERADE (POSTROUTING)
iptables (netfilter)
Some benefits of iptables:
Works at layers 2 and 3 – the application is
never even aware that the traffic has hit the
box. Lessens the chance of a buffer overflow
or other malformed traffic attack.
Very low performance impact
iptables examples
First allow traffic to and from localhost:
iptables -A INPUT -i lo -j ACCEPT
Next allow icmp (ping and friends):
iptables -A INPUT -p icmp -j ACCEPT
To allow ssh from
station101.example.com:
iptables -A INPUT -s station101 -p tcp \
--dport 22 -j ACCEPT
iptables examples
To allow traffic from the world to the web
server:
iptables -A INPUT -p tcp --dport http -j ACCEPT
iptables -A INPUT -p tcp --dport https -j ACCEPT
To allow all traffic from a trusted subnet:
iptables-I INPUT -s 192.168.1.0/24 -j ACCEPT
Now log any other traffic:
iptables -A INPUT -m state --state NEW -j LOG \
--log-prefix="Blocked Traffic "
iptables examples
Finally, block any other traffic:
iptables -A INPUT -m state --state NEW -j DROP
iptables examples
Note that REJECT sends back an icmp port
unreachable message, where DROP silently
discards (and slows down the attacker)
iptables examples
Don't forget to make your changes permanent!
tcp_wrappers
Applications can be compiled against
libwrap
That application is going to check
connections against /etc/hosts.allow first
and then and /etc/hosts.deny
If a connection is allowed in hosts.allow then
the connection will succeed. BE AWARE OF
THIS!!!
These files are configured in this format:
<daemon list>: <client list> [: <option>: <option>: ...]
tcp_wrappers
Where daemon is something like
vsftpd
sshd
in.telnetd
And client list is something like:
192.168.1. or 192.168.1.0/24
ALL
*.cracker.lan
tcp_wrappers
And option is something like:
allow
deny
twist
spawn
except
options can be added
sshd: 192.168.2. : spawn /usr/bin/logger
“Access denied on $(/bin/date)”: deny
vsftpd : .example.com : twist /bin/echo "421
This domain has been black-listed. Access
denied!"
Thomas Cameron: Securing Your Linux Servers Slide 37
Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Red Hat
tcp_wrappers
Example of hosts.deny with a spawn
directive to log:
tcp_wrappers
Example of hosts.deny with a twist:
xinetd
xinetd is the internet “super-server,” a
replacement for the old inetd service.
A single service which listens on several ports.
When a connection is made to a port, xinetd
decides what service to start in response to
that connection
Rules are stored in /etc/xinetd.d/[service]
xinetd
Some security features for the service files
only_from = 192.168.0.0/24
no_access = 192.168.1.10
access_times = 09:00-18:00
xinetd
Logging options:
ATTEMPT — Logs the fact that a failed attempt was
made (log_on_failure).
DURATION — Logs the length of time the service is
used by a remote system (log_on_success).
EXIT — Logs the exit status or termination signal of
the service (log_on_success).
HOST — Logs the remote host's IP address
(log_on_failure and log_on_success).
PID — Logs the process ID of the server receiving the
request (log_on_success).
USERID — Logs the remote user using the method
defined in RFC 1413 for all multi-threaded stream
services (log_on_failure and log_on_success).
xinetd
Example configuration file for the kerberos-
enabled telnet service, /etc/xinetd.d/krb5-telnet:
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/kerberos/sbin/telnetd
log_on_failure += USERID
disable = yes
}
xinetd
Using the SENSOR attribute with xinetd-
managed services
SENSOR is a sort of “honeypot” tool
• Once an unauthorized system attempts to connect,
xinetd locks the entire system down for that system
xinetd
Using SENSOR:
service telnet
{
flags = SENSOR
deny_time = 1
socket_type = stream
wait = no
user = root
server = /usr/kerberos/sbin/telnetd
log_on_failure += USERID
disable = no
}
xinetd
Using SENSOR:
Sep 25 01:13:50 station100 xinetd[2437]: START: login pid=2442
from=192.168.1.101
Sep 25 01:13:54 station100 xinetd[2437]: EXIT: login status=0
pid=2442 duration=4(sec)
Sep 25 01:13:58 station100 xinetd[2437]: 2437 {process_sensor}
Adding 192.168.1.101 to the global_no_access list for 1 minutes
Sep 25 01:13:58 station100 xinetd[2437]: FAIL: telnet address
from=192.168.1.101
Sep 25 01:14:02 station100 xinetd[2471]: FAIL: login address
from=192.168.1.101
xinetd
Another important feature of xinetd is its ability
to control the amount of resources which services
under its control can utilize.
It does this by way of the following directives:
cps = <number_of_connections> <wait_period> —
Dictates the connections allowed to the service per
second. This directive accepts only integer values.
instances = <number_of_connections> — Dictates the
total number of connections allowed to a service. This
directive accepts either an integer value or
UNLIMITED.
xinetd
It does this by way of the following
directives (Continued):
per_source = <number_of_connections> —
Dictates the connections allowed to a service
by each host. This directive accepts either an
integer value or UNLIMITED.
rlimit_as = <number[K|M]> — Dictates the
amount of memory address space the service
can occupy in kilobytes or megabytes. This
directive accepts either an integer value or
UNLIMITED.
xinetd
It does this by way of the following
directives (Continued):
rlimit_cpu = <number_of_seconds> —
Dictates the amount of time in seconds that a
service may occupy the CPU. This directive
accepts either an integer value or UNLIMITED.
Process contexts:
Current context: user_u:system_r:unconfined_t:s0
Init context: system_u:system_r:init_t:s0
/sbin/mingetty system_u:system_r:getty_t:s0
Production
Once the server is in place, use utilities such as
aide to check system integrity.
rpm -Va is another useful utility, but since the RPM
database is stored on the local filesystem, it is not
reliable
Periodically check for any open ports or
processes which were not there during the build
and hardening process
Set up remote logging - see the
/etc/sysconfig/syslog file for details, it's very well
documented.
Use logwatch to monitor your logs, it's included
and active by default
Thomas Cameron: Securing Your Linux Servers Slide 79
Colorado Software Summit: October 21 – 26, 2007 © Copyright 2007, Red Hat
Questions?