Reducing iptables configuration complexity using chains

44

description

Configuring a Linux firewall can get rather complex. This presentations shows some ideas to keep the iptables/netfilter configuration rules maintainable, better to read and run faster. Intended audience : anyone interested in configuring a linux firewall, with basic knowledge of network concepts like protocols, ports, packets and IP addresses. These slides were presented on May 8th 2014 at LinuxTag 2014 in Berlin.

Transcript of Reducing iptables configuration complexity using chains

Page 1: Reducing iptables configuration complexity using chains

Reducing iptables con�gurationcomplexity using chains

Dieter Adriaenssens

@dcadriaenssens - Ghent University, Belgium

LinuxTag - BerlinMay 8th, 2014

This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 License.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 1 / 30

Page 2: Reducing iptables configuration complexity using chains

Overview

Brief introduction to iptables/net�lter

Optimizing con�guration with a tutorial use case

Conclusion

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 2 / 30

Page 3: Reducing iptables configuration complexity using chains

What is iptables/net�lter?

Iptables is a tool for creating the rulesets for net�lter, apacket �ltering framework which was introduced in thelinux 2.4 kernel

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 3 / 30

Page 4: Reducing iptables configuration complexity using chains

Rules

When an IP packet comes in, it is checked against a set ofrules.

Exampleiptables -A INPUT -m tcp -p tcp --dport ssh -j ACCEPTiptables -A INPUT -m tcp -p tcp --dport http -j ACCEPTiptables -A INPUT -m tcp -p tcp --dport https -j ACCEPTiptables -A INPUT -j DROP

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 4 / 30

Page 5: Reducing iptables configuration complexity using chains

Chains

Prede�ned chains :

INPUT

OUTPUT

FORWARD

PREROUTING

POSTROUTING

Example-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT

Custom chains can be de�ned

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 5 / 30

Page 6: Reducing iptables configuration complexity using chains

Match

Filter on packet parameters :

protocol (tcp, udp, icmp, . . . )

destination/source port

destination/source IP address

in/outgoing interface (eth0, . . . )

. . .

Example-A INPUT -m tcp -p tcp --dport ssh -s 10.0.0.0/8 -j ACCEPT

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 6 / 30

Page 7: Reducing iptables configuration complexity using chains

Targets

What to do if a packet matches a rule :

ACCEPT

DROP

QUEUE → userspace

RETURN → leave current chain

LOG

jump to a custom chain

Example-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 7 / 30

Page 8: Reducing iptables configuration complexity using chains

Putting it all together

All rules are checked one by one, and if one matches, thetarget is executed :

Example-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A INPUT -j DROP

The last rule matches everything, so if a packet didn'tmatch a previous rule, it will be rejected.

Remark: the order of the rules is important

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 8 / 30

Page 9: Reducing iptables configuration complexity using chains

Putting it all together

All rules are checked one by one, and if one matches, thetarget is executed :

Example-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A INPUT -j DROP

The last rule matches everything, so if a packet didn'tmatch a previous rule, it will be rejected.Remark: the order of the rules is important

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 8 / 30

Page 10: Reducing iptables configuration complexity using chains

Default target DROP

A default target can be set for the prede�ned chains:

Example-P INPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A INPUT -j DROP

If none of the rules match, the default target is executed.In this example, a packet is dropped.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 9 / 30

Page 11: Reducing iptables configuration complexity using chains

Default target ACCEPT

Exercise:

What happens if the default target is ACCEPT?

Example-P INPUT ACCEPT-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

All packets are accepted, even if no rule matches them.Always de�ne a default target or use an

all-matching rule at the end!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 10 / 30

Page 12: Reducing iptables configuration complexity using chains

Default target ACCEPT

Exercise:

What happens if the default target is ACCEPT?

Example-P INPUT ACCEPT-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

All packets are accepted, even if no rule matches them.

Always de�ne a default target or use an

all-matching rule at the end!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 10 / 30

Page 13: Reducing iptables configuration complexity using chains

Default target ACCEPT

Exercise:

What happens if the default target is ACCEPT?

Example-P INPUT ACCEPT-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

All packets are accepted, even if no rule matches them.Always de�ne a default target or use an

all-matching rule at the end!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 10 / 30

Page 14: Reducing iptables configuration complexity using chains

Example : �rewall for your server

Filter both incoming and outgoing tra�c :

Example-P INPUT DROP-P OUTPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

Problem: incoming packets are accepted, but replies aredropped!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 11 / 30

Page 15: Reducing iptables configuration complexity using chains

Example : �rewall for your server

Filter both incoming and outgoing tra�c :

Example-P INPUT DROP-P OUTPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

Problem: incoming packets are accepted, but replies aredropped!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 11 / 30

Page 16: Reducing iptables configuration complexity using chains

Example : �rewall for your server

Also add rules for outgoing tra�c :

Example-P INPUT DROP-P OUTPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A OUTPUT -m tcp -p tcp --sport ssh -j ACCEPT-A OUTPUT -m tcp -p tcp --sport 80 -j ACCEPT-A OUTPUT -m tcp -p tcp --sport https -j ACCEPT

This can get complicated if more rules are added.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 12 / 30

Page 17: Reducing iptables configuration complexity using chains

Example : �rewall for your server

Also add rules for outgoing tra�c :

Example-P INPUT DROP-P OUTPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A OUTPUT -m tcp -p tcp --sport ssh -j ACCEPT-A OUTPUT -m tcp -p tcp --sport 80 -j ACCEPT-A OUTPUT -m tcp -p tcp --sport https -j ACCEPT

This can get complicated if more rules are added.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 12 / 30

Page 18: Reducing iptables configuration complexity using chains

Established state

Solution: check for established state

Example-P INPUT DROP-P OUTPUT DROP-A INPUT -m tcp -p tcp --dport ssh -j ACCEPT-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT-A OUTPUT -m state --state ESTABLISHED,RELATED -jACCEPT

This makes the rules more manageable, especially whenoutput rules are also de�ned.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 13 / 30

Page 19: Reducing iptables configuration complexity using chains

Intermezzo : �rewall on your PC

Protect your own PC, block all incoming requests :

Example-P INPUT DROP-P OUTPUT ACCEPT

-A INPUT -i lo -j ACCEPT # allow local tra�c-A INPUT -p icmp -j ACCEPT # allow ping, etc-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Add ESTABLISHED, to allow replies of outgoingconnections you intiated!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 14 / 30

Page 20: Reducing iptables configuration complexity using chains

Intermezzo : �rewall on your PC

Protect your own PC, block all incoming requests :

Example-P INPUT DROP-P OUTPUT ACCEPT-A INPUT -i lo -j ACCEPT # allow local tra�c-A INPUT -p icmp -j ACCEPT # allow ping, etc-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Add ESTABLISHED, to allow replies of outgoingconnections you intiated!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 14 / 30

Page 21: Reducing iptables configuration complexity using chains

Tutorial use case : setup

Tcp port 80 (http) and 443 (https) is available for all.

SSH (port 22) and a webbased admin tool (port10000), is limited to admin PCs.

A SMB service is limited to admin and webmasterPCs.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 15 / 30

Page 22: Reducing iptables configuration complexity using chains

Tutorial use case : webserver

Webservice, tcp port 80 (http) and 443 (https) is availablefor all :

Example-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m tcp -p tcp --dport https -j ACCEPT

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 16 / 30

Page 23: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Sysadmins are allowed to access SSH (tcp port 22) and awebbased admin tool (tcp port 10000).The IP addresses of their PCs :

10.100.2.3

10.100.2.4

10.100.2.7

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 17 / 30

Page 24: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Example-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 10000 -j ACCEPT

The same IP addresses are repeated. If there is a changein the IP addresses list, several rules have to be updated.This can be done more e�cient!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 18 / 30

Page 25: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Example-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 10000 -j ACCEPT

The same IP addresses are repeated. If there is a changein the IP addresses list, several rules have to be updated.

This can be done more e�cient!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 18 / 30

Page 26: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Example-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 22 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.3 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.4 --dport 10000 -j ACCEPT-A INPUT -p tcp -m tcp -s 10.100.2.7 --dport 10000 -j ACCEPT

The same IP addresses are repeated. If there is a changein the IP addresses list, several rules have to be updated.This can be done more e�cient!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 18 / 30

Page 27: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Wouldn't it be convenient to :

make a list of IP addresses?

check this list when a packet matches a rule (fe.TCP port 22)?

reuse this list when another rule matches a packet?

This is possible with custom chains!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 19 / 30

Page 28: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Wouldn't it be convenient to :

make a list of IP addresses?

check this list when a packet matches a rule (fe.TCP port 22)?

reuse this list when another rule matches a packet?

This is possible with custom chains!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 19 / 30

Page 29: Reducing iptables configuration complexity using chains

Introducing custom chains

Create the custom chain and add the rules

Example-N admin_IP

-A admin_IP -s 10.100.2.3 -j ACCEPT-A admin_IP -s 10.100.2.4 -j ACCEPT-A admin_IP -s 10.100.2.7 -j ACCEPT-A admin_IP -j DROP

Syntax of adding rules to a custom chain is similar toadding to default targets.

Custom chains don't have a default target, so set atarget for all packets that are not matched.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 20 / 30

Page 30: Reducing iptables configuration complexity using chains

Introducing custom chains

Create the custom chain and add the rules

Example-N admin_IP-A admin_IP -s 10.100.2.3 -j ACCEPT-A admin_IP -s 10.100.2.4 -j ACCEPT-A admin_IP -s 10.100.2.7 -j ACCEPT-A admin_IP -j DROP

Syntax of adding rules to a custom chain is similar toadding to default targets.

Custom chains don't have a default target, so set atarget for all packets that are not matched.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 20 / 30

Page 31: Reducing iptables configuration complexity using chains

Introducing custom chains

Create the custom chain and add the rules

Example-N admin_IP-A admin_IP -s 10.100.2.3 -j ACCEPT-A admin_IP -s 10.100.2.4 -j ACCEPT-A admin_IP -s 10.100.2.7 -j ACCEPT-A admin_IP -j DROP

Syntax of adding rules to a custom chain is similar toadding to default targets.

Custom chains don't have a default target, so set atarget for all packets that are not matched.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 20 / 30

Page 32: Reducing iptables configuration complexity using chains

Introducing custom chains

Create the custom chain and add the rules

Example-N admin_IP-A admin_IP -s 10.100.2.3 -j ACCEPT-A admin_IP -s 10.100.2.4 -j ACCEPT-A admin_IP -s 10.100.2.7 -j ACCEPT-A admin_IP -j DROP

Syntax of adding rules to a custom chain is similar toadding to default targets.

Custom chains don't have a default target, so set atarget for all packets that are not matched.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 20 / 30

Page 33: Reducing iptables configuration complexity using chains

Why you should set a default target

Avoid jumping back to the calling chain :

performance issue

unexpected behaviour, some other rules might match

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 21 / 30

Page 34: Reducing iptables configuration complexity using chains

Tutorial use case : sysadmin PCs

Add the rules for TCP port 22 and 10000 using thecustom chain :

Example-A INPUT -p tcp -m tcp --dport 22 -j admin_IP-A INPUT -p tcp -m tcp --dport 10000 -j admin_IP

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 22 / 30

Page 35: Reducing iptables configuration complexity using chains

Bene�ts

the list of IP addresses in the custom chain is reusedfor both ports, so they have to be de�ned only once

adding/changing/removing an IP address is mucheasier

a better overview of the �rewall rules

better performance : the rules in the custom chainare only checked if that chain is accessed

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 23 / 30

Page 36: Reducing iptables configuration complexity using chains

Tutorial use case : webmaster PCs

Let's do the same for webmasters AND admins havingaccess to SMB.

Create a chain with webmaster IP addresses :

Example-N webmaster_IP-A webmaster_IP -s 10.100.2.11 -j ACCEPT-A webmaster_IP -s 10.100.2.17 -j ACCEPT-A webmaster_IP -s 10.100.2.34 -j ACCEPT-A webmaster_IP -j DROP

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 24 / 30

Page 37: Reducing iptables configuration complexity using chains

Tutorial use case : webmaster PCs

Let's do the same for webmasters AND admins havingaccess to SMB.Create a chain with webmaster IP addresses :

Example-N webmaster_IP-A webmaster_IP -s 10.100.2.11 -j ACCEPT-A webmaster_IP -s 10.100.2.17 -j ACCEPT-A webmaster_IP -s 10.100.2.34 -j ACCEPT-A webmaster_IP -j DROP

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 24 / 30

Page 38: Reducing iptables configuration complexity using chains

Tutorial use case : access to SMB

Add the rules for SMB (tcp port 139 and 445) using thecustom chains :

Example-A INPUT -p tcp -m tcp --dport 445 -j admin_IP-A INPUT -p tcp -m tcp --dport 445 -j webmaster_IP-A INPUT -p tcp -m tcp --dport 139 -j admin_IP-A INPUT -p tcp -m tcp --dport 139 -j webmaster_IP

This can be optimized!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 25 / 30

Page 39: Reducing iptables configuration complexity using chains

Tutorial use case : access to SMB

Add the rules for SMB (tcp port 139 and 445) using thecustom chains :

Example-A INPUT -p tcp -m tcp --dport 445 -j admin_IP-A INPUT -p tcp -m tcp --dport 445 -j webmaster_IP-A INPUT -p tcp -m tcp --dport 139 -j admin_IP-A INPUT -p tcp -m tcp --dport 139 -j webmaster_IP

This can be optimized!

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 25 / 30

Page 40: Reducing iptables configuration complexity using chains

Chaining chains

Include the sysadmin chain in the webmaster chain:

Example-N webmaster_IP-A webmaster_IP -s 10.100.2.11 -j ACCEPT-A webmaster_IP -s 10.100.2.17 -j ACCEPT-A webmaster_IP -s 10.100.2.34 -j ACCEPT-A webmaster_IP -j DROP-A webmaster_IP -j admin_IP

Jump to the sysadmin chain at the end of the webmasterchain.Warning : check conditions of ending chains

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 26 / 30

Page 41: Reducing iptables configuration complexity using chains

Tutorial use case : access to SMB

The rules for SMB (tcp port 139 and 445) can be reducedto :

Example-A INPUT -p tcp -m tcp --dport 445 -j admin_IP-A INPUT -p tcp -m tcp --dport 445 -j webmaster_IP-A INPUT -p tcp -m tcp --dport 139 -j admin_IP-A INPUT -p tcp -m tcp --dport 139 -j webmaster_IP

The admin chain is checked as well because the webmasterchain includes it.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 27 / 30

Page 42: Reducing iptables configuration complexity using chains

Conclusion

use ESTABLISHED state to reduce number of rules

using chains makes your rules easier readable andmaintainable

chains can be reused for several rules

chains can be chained together

faster, because it only jumps to a chain when a rulematches

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 28 / 30

Page 43: Reducing iptables configuration complexity using chains

Questions

Thanks for your attention!Questions?

Contact

Blog : http://ruleant.blogspot.com

Twitter : @dcadriaenssens

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 29 / 30

Page 44: Reducing iptables configuration complexity using chains

IPv6

Con�guration is the same for IPv4, use ip6tables :

Exampleip6tables -P INPUT DROPip6tables -P OUTPUT ACCEPTip6tables -A INPUT -i lo -j ACCEPT # allow local tra�cip6tables -A INPUT -p icmp -j ACCEPT # allow ping, etcip6tables -A INPUT -m state --state ESTABLISHED -j ACCEPTip6tables -A INPUT -m state --state RELATED -j ACCEPT

Even if IPv4 rules are set, IPv6 has to be con�guredseperately, otherwise the defaults apply.

Dieter Adriaenssens (UGent) Reduce iptables con�g complexity LinuxTag 8May2014 30 / 30