Security

IPTables & SELinux

IPTables

iptables is the userspace command line program used to configure the Linux 2.4.x and later packet filtering ruleset. It is targeted towards system administrators. -- netfilter.org

IPTables / Netfilter

IPTables Concepts

Chains

_images/iptables.png
INPUT
All packets destined for the host computer
OUTPUT
All packets originating from the host computer
FORWARD
All packets neither destined for nor originating from the host computer, but passing through (routed by) the host computer. This chain is used if you are using your computer as a router.

IPTables Rules workflow

  1. Rules are added in a list to each chain
  2. A packet is checked against each rule in turn, starting at the top
  3. If it matches a rule, then an action is taken (ACCEPT or DROP)
  4. Once a rule has been matched and an action taken, then the packet is processed and isn't processed by further rules in the chain.
  5. If a packet passes down through all the rules in the chain and reaches the bottom without being matched against any rule, then the default action for that chain is taken.
  6. This is referred to as the default policy and may be set to either ACCEPT or DROP the packet.

Default firewall policies

DROP all packets by default
Add rules to specifically allow packets that may be from trusted IP addresses or ports.
ACCEPT all packages by default
Block packets from specific IP addresses or ports.

Hands on IPTables

$ lsmod | grep ip_tables
ip_tables              27240  1 iptable_filter
$ iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
$ iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

IPTables on CentOS 7

$ yum install iptables-services
$ systemctl start iptables
$ iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source     destination
ACCEPT     all  --  anywhere   anywhere     state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere   anywhere
ACCEPT     all  --  anywhere   anywhere
ACCEPT     tcp  --  anywhere   anywhere     state NEW tcp dpt:ssh
REJECT     all  --  anywhere   anywhere     reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source     destination
REJECT     all  --  anywhere   anywhere     reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source     destination
$ iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited

Simple Rule Set

systemctl stop iptables
iptables -P INPUT ACCEPT
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -L -v

Simple Rule Set in detail

iptables -P INPUT ACCEPT
If connecting remotely we must first temporarily set the default policy on the INPUT chain to ACCEPT otherwise once we flush the current rules we will be locked out of our server.
iptables -F
We used the -F switch to flush all existing rules so we start with a clean state from which to add new rules.

Simple Rule Set in detail

iptables -A INPUT -i lo -j ACCEPT

Simple Rule Set in detail

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Simple Rule Set in detail

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Simple Rule Set in detail

iptables -P INPUT DROP

Simple Rule Set in detail

iptables -P FORWARD DROP
Similarly, here we've set the default policy on the FORWARD chain to DROP as we're not using our computer as a router so there should not be any packets passing through our computer.
iptables -P OUTPUT ACCEPT
And finally, we've set the default policy on the OUTPUT chain to ACCEPT as we want to allow all outgoing traffic (as we trust our users).
iptables -L -v
Finally, we can list (-L) the rules we've just added to check they've been loaded correctly.

Saving rules

$ service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
$ cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Mon Mar  7 20:49:54 2016
*filter
:INPUT DROP [37:12136]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [57:5712]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Mon Mar  7 20:49:54 2016

# Manually saving or restoring
$ iptables-save > /etc/sysconfig/iptables
$ iptables-restore < /etc/sysconfig/iptables

SELinux

Security-Enhanced Linux (SELinux) is an implementation of a mandatory access control mechanism in the Linux kernel, checking for allowed operations after standard discretionary access controls are checked. SELinux can enforce rules on files and processes in a Linux system, and on their actions, based on defined policies. -- redhat.com

SELinux History

SELinux Introduction

SELinux Introduction

SELinux Modes

Enforcing
The default mode which will enable and enforce the SELinux security policy on the system, denying access and logging actions
Permissive
In Permissive mode, SELinux is enabled but will not enforce the security policy, only warn and log actions. Permissive mode is useful for troubleshooting SELinux issues
Disabled
SELinux is turned off

SELinux Modes

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28
$ getenforce
Permissive
$ setenforce 1
$ getenforce
Enforcing
$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

SELinux Access Control

Type Enforcement (TE)
Type Enforcement is the primary mechanism of access control used in the targeted policy
Role-Based Access Control (RBAC)
Based around SELinux users (not necessarily the same as the Linux user), but not used in the default targeted policy
Multi-Level Security (MLS)
Not commonly used and often hidden in the default targeted policy. Refers to a security scheme that enforces the Bell-La Padula Mandatory Access Model.

SELinux Security Context

$ yum install -y httpd && touch /var/www/html/index.html
$ ls -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html

SELinux Security Context

Now consider the SELinux security context of the Apache web server process: httpd

$ systemctl start httpd
$ ps axZ | grep httpd
system_u:system_r:httpd_t:s0     2744 ?        Ss     0:00 /usr/sbin/httpd -DFOREGROUND

SELinux Security Context

Finally, let's look at the SELinux security context of a file in our home directory:

$ sudo -u centos touch /home/centos/foo
$ ls -Z /home/centos/foo
-rw-r--r--. centos centos unconfined_u:object_r:user_home_t:s0 /home/centos/foo

SELinux Security Context

Resources