You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
455 lines
14 KiB
455 lines
14 KiB
= Iptables !
|
|
Apostolos rootApostolos@swarmlab.io
|
|
// Metadata:
|
|
:description: Intro and Install
|
|
:keywords: sec, tcpdump
|
|
:data-uri:
|
|
:toc: right
|
|
:toc-title: Πίνακας περιεχομένων
|
|
:toclevels: 4
|
|
:source-highlighter: highlight
|
|
:icons: font
|
|
:sectnums:
|
|
|
|
include::header.adoc[]
|
|
|
|
|
|
{empty} +
|
|
|
|
|
|
[[cheat-Docker]]
|
|
== Install swarmlab-sec (Home PC)
|
|
|
|
HowTo: See http://docs.swarmlab.io/lab/sec/sec.adoc.html
|
|
|
|
|
|
.NOTE
|
|
[NOTE]
|
|
====
|
|
Assuming you're already logged in
|
|
====
|
|
|
|
|
|
|
|
== iptables
|
|
|
|
|
|
**iptables** is a command line utility for configuring Linux kernel **firewall** implemented within the https://en.wikipedia.org/wiki/Netfilter[Netfilter] project. The term ''iptables'' is also commonly used to refer to this kernel-level firewall. It can be configured directly with iptables, or by using one of the many
|
|
|
|
https://en.wikipedia.org/wiki/Iptables[More: wikipedia]
|
|
|
|
- Console tools
|
|
|
|
and
|
|
|
|
- Graphical front-ends.
|
|
|
|
**iptables** is used for https://en.wikipedia.org/wiki/IPv4[IPv4] and ''ip6tables'' is used for ihttps://en.wikipedia.org/wiki/IPv6[IPv6]. Both ''iptables'' and ''ip6tables'' have the same syntax, but some options are specific to either IPv4 or IPv6.
|
|
|
|
|
|
=== Installation
|
|
|
|
The Swarmlab.io kernel is compiled with iptables support.
|
|
|
|
|
|
=== Front-ends
|
|
|
|
==== Console
|
|
|
|
- Shorewall, High-level tool for configuring Netfilter.
|
|
|
|
You describe your firewall/gateway requirements using entries in a set of configuration files.
|
|
|
|
http://www.shorewall.net/[shorewall]
|
|
|
|
- Arno's Secure firewall for both single and multi-homed machines.
|
|
|
|
Very easy to configure, handy to manage and highly customizable. Supports: NAT and SNAT, port forwarding, ADSL ethernet modems with both static and dynamically assigned IPs, MAC address filtering, stealth port scan detection, DMZ and DMZ-2-LAN forwarding, protection against SYN/ICMP flooding, extensive user definable logging with rate limiting to prevent log flooding, all IP protocols and VPNs such as IPsec, plugin support to add extra features.|
|
|
|
|
http://rocky.eld.leidenuniv.nl/[arno-iptables-firewall]
|
|
|
|
- FireHOL Language to express firewalling rules, not just a script that produces some kind of a firewall. It makes building even sophisticated firewalls easy - the way you want it.
|
|
|
|
http://firehol.sourceforge.net
|
|
|
|
- firewalld (firewall-cmd) Daemon and console interface for configuring network and firewall zones as well as setting up and configuring firewall rules.
|
|
|
|
https://firewalld.org[firewalld]
|
|
|
|
==== Graphical
|
|
|
|
- Firewall Builder
|
|
|
|
firewall configuration and management tool that supports iptables (netfilter), ipfilter, pf, ipfw, Cisco PIX (FWSM, ASA) and Cisco routers extended access lists. The program runs on Linux, FreeBSD, OpenBSD, Windows and macOS and can manage both local and remote firewalls.
|
|
|
|
http://fwbuilder.sourceforge.net[fwbuilder]
|
|
|
|
- firewalld
|
|
|
|
(firewall-config) Daemon and graphical interface for configuring network and firewall zones as well as setting up and configuring firewall rules.
|
|
|
|
https://firewalld.org[firewalld]
|
|
|
|
- FireStarter
|
|
|
|
High-level GUI Iptables firewall for Linux systems
|
|
|
|
http://www.fs-security.com[firestarter]
|
|
|
|
== Basic concepts
|
|
|
|
iptables is used to inspect, modify, forward, redirect, and/or drop IP packets.
|
|
|
|
- The code for filtering IP packets is already built into the kernel and is organized into a collection of **tables**, each with a specific purpose.
|
|
- The tables are made up of a set of predefined **chains**, and the chains contain **rules** which are traversed in order.
|
|
- Each rule consists of a predicate of potential matches and a corresponding action (called a **target**) which is executed if the predicate is true; i.e. the conditions are matched.
|
|
- If the IP packet reaches the end of a built-in chain, including an empty chain, then the chain's **policy** target determines the final destination of the IP packet.
|
|
|
|
iptables is the user utility which allows you to work with these chains/rules.
|
|
|
|
|
|
.Understanding how iptables works
|
|
[NOTE]
|
|
====
|
|
The key to understanding how iptables works is http://docs.swarmlab.io/lab/sec/tables_traverse.jpg[this chart].
|
|
|
|
The lowercase word on top is the **table** and the upper case word below is the **chain**.
|
|
|
|
- Every IP packet that comes in **on any network interface** passes through this flow chart from top to bottom.
|
|
|
|
|
|
**All interfaces are handled the same way; it's up to you to define rules that treat them differently.**
|
|
|
|
Some packets
|
|
|
|
- are intended for local processes, hence come in from the top of the chart and stop at **Local Proces**,
|
|
|
|
- while other packets are generated by local processes; hence start at **Local Process** and proceed downward through the flowchart.
|
|
|
|
A detailed explanation [https://www.frozentux.net/iptables-tutorial/iptables-tutorial.html#TRAVERSINGOFTABLES here].
|
|
====
|
|
|
|
In the vast majority of use cases you won't need to use the **raw**, **mangle**, or **security** tables at all.
|
|
|
|
Consequently, the following chart depicts a simplified network packet flow through **iptables**:
|
|
|
|
[source,bash]
|
|
----
|
|
|
|
XXXXXXXXXXXXXXXXXX
|
|
XXX Network XXX
|
|
XXXXXXXXXXXXXXXXXX
|
|
+
|
|
|
|
|
v
|
|
+-------------+ +------------------+
|
|
|table: filter| <---+ | table: nat |
|
|
|chain: INPUT | | | chain: PREROUTING|
|
|
+-----+-------+ | +--------+---------+
|
|
| | |
|
|
v | v
|
|
[local process] | **************** +--------------+
|
|
| +---------+ Routing decision +------> |table: filter |
|
|
v **************** |chain: FORWARD|
|
|
**************** +------+-------+
|
|
Routing decision |
|
|
**************** |
|
|
| |
|
|
v **************** |
|
|
+-------------+ +------> Routing decision <---------------+
|
|
|table: nat | | ****************
|
|
|chain: OUTPUT| | +
|
|
+-----+-------+ | |
|
|
| | v
|
|
v | +-------------------+
|
|
+--------------+ | | table: nat |
|
|
|table: filter | +----+ | chain: POSTROUTING|
|
|
|chain: OUTPUT | +--------+----------+
|
|
+--------------+ |
|
|
v
|
|
XXXXXXXXXXXXXXXXXX
|
|
XXX Network XXX
|
|
XXXXXXXXXXXXXXXXXX
|
|
----
|
|
|
|
=== Table
|
|
|
|
iptables contains five tables:
|
|
|
|
.Chains
|
|
[NOTE]
|
|
====
|
|
Tables consist of **chains**, which are lists of rules which are followed in order.
|
|
====
|
|
|
|
==== Filter
|
|
|
|
This is the default table.
|
|
|
|
.Its built-in chains are:
|
|
[source,bash]
|
|
----
|
|
Input: packets going to local sockets
|
|
Forward: packets routed through the server
|
|
Output: locally generated packets
|
|
----
|
|
|
|
==== Nat
|
|
|
|
When a packet creates a new connection, this table is used.
|
|
|
|
|
|
.Its built-in chains are:
|
|
[source,bash]
|
|
----
|
|
Prerouting: designating packets when they come in
|
|
Output: locally generated packets before routing takes place
|
|
Postrouting: altering packets on the way out
|
|
----
|
|
|
|
==== Mangle
|
|
|
|
Used for special altering of packets.
|
|
|
|
.Its built-in chains are:
|
|
[source,bash]
|
|
----
|
|
Prerouting: incoming packets
|
|
Postrouting: outgoing packets
|
|
Output: locally generated packets that are being altered
|
|
Input: packets coming directly into the server
|
|
Forward: packets being routed through the server
|
|
----
|
|
|
|
==== Raw
|
|
|
|
Primarily used for configuring exemptions from connection tracking.
|
|
|
|
.Its built-in chains are:
|
|
[source,bash]
|
|
----
|
|
Prerouting: packets that arrive by the network interface
|
|
Output: processes that are locally generated
|
|
----
|
|
|
|
==== Security
|
|
|
|
Used for Mandatory Access Control (MAC) rules. After the filter table, the security table is accessed next.
|
|
|
|
.Its built-in chains are:
|
|
[source,bash]
|
|
----
|
|
Input: packets entering the server
|
|
Output: locally generated packets
|
|
Forward: packets passing through the server
|
|
----
|
|
|
|
|
|
|
|
[NOTE]
|
|
====
|
|
In most common use cases you will only use two of these: **filter** and **nat**.
|
|
====
|
|
|
|
|
|
=== Rules
|
|
|
|
Packet filtering is based on **rules**, which are specified by multiple **matches** (conditions the packet must satisfy so that the rule can be applied), and one **target** (action taken when the packet matches all conditions).
|
|
|
|
The typical things a rule might match on are
|
|
|
|
- what interface the packet came in on (e.g eth0 or eth1),
|
|
- what type of packet it is (ICMP, TCP, or UDP),
|
|
- or the destination port of the packet.
|
|
|
|
Targets are specified using the **-j** or **--jump** option.
|
|
|
|
Targets can be either
|
|
- user-defined chains (i.e. if these conditions are matched, jump to the following user-defined chain and continue processing there), one of the special built-in targets,
|
|
- or a target extension.
|
|
|
|
[NOTE]
|
|
====
|
|
- Built-in targets are **ACCEPT**, **DROP**, **QUEUE** and **RETURN**
|
|
- target extensions are, for example, **REJECT** and **LOG**.
|
|
====
|
|
|
|
- If the target is a built-in target, the fate of the packet is decided immediately and processing of the packet in current table is stopped.
|
|
|
|
- If the target is a user-defined chain and the fate of the packet is not decided by this second chain, it will be filtered against the remaining rules of the original chain.
|
|
|
|
Target extensions can be either **terminating** (as built-in targets) or **non-terminating** (as user-defined chains)
|
|
|
|
=== Traversing Chains
|
|
|
|
A network packet received on any interface traverses the traffic control chains of tables in the order shown in the http://docs.swarmlab.io/lab/sec/tables_traverse.jpg[this chart]
|
|
|
|
- The first routing decision involves deciding if the final destination of the packet is the local machine (in which case the packet traverses through the **INPUT chains**
|
|
|
|
- or elsewhere (in which case the packet traverses through the **FORWARD chains**.
|
|
|
|
- Subsequent routing decisions involve deciding what interface to assign to an outgoing packet.
|
|
|
|
At each chain in the path, every rule in that chain is evaluated in order and whenever a rule matches, the corresponding target/jump action is executed.
|
|
|
|
The 3 most commonly used targets are **ACCEPT**, **DROP**, and **jump** to a user-defined chain.
|
|
|
|
[NOTE]
|
|
====
|
|
While built-in chains can have default policies, user-defined chains can not.
|
|
====
|
|
|
|
- If every rule in a chain that you jumped fails to provide a complete match, the packet is dropped back into the calling chain as illustrated
|
|
http://docs.swarmlab.io/lab/sec/images/table_subtraverse.jpg[here].
|
|
|
|
- If at any time a complete match is achieved for a rule with a **DROP** target, the packet is dropped and no further processing is done.
|
|
- If a packet is **ACCEPT**ed within a chain, it will be **ACCEPT**ed in all superset chains also and it will not traverse any of the superset chains any further.
|
|
|
|
However, be aware that the packet will continue to traverse all other chains in other tables in the normal fashion.
|
|
|
|
|
|
|
|
== Usage
|
|
|
|
=== Showing the current rules
|
|
|
|
[source,bash]
|
|
----
|
|
# iptables -nvL
|
|
|
|
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
|
|
pkts bytes target prot opt in out source destination
|
|
|
|
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
|
|
pkts bytes target prot opt in out source destination
|
|
|
|
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
|
|
pkts bytes target prot opt in out source destination
|
|
----
|
|
|
|
If the output looks like the above, then there are no rules (i.e. nothing is blocked) in the default filter table
|
|
|
|
|
|
=== Resetting rules
|
|
|
|
You can flush and reset iptables to default using these commands:
|
|
|
|
[source,bash]
|
|
----
|
|
# iptables -F
|
|
# iptables -X
|
|
# iptables -t nat -F
|
|
# iptables -t nat -X
|
|
# iptables -t mangle -F
|
|
# iptables -t mangle -X
|
|
# iptables -t raw -F
|
|
# iptables -t raw -X
|
|
# iptables -t security -F
|
|
# iptables -t security -X
|
|
# iptables -P INPUT ACCEPT
|
|
# iptables -P FORWARD ACCEPT
|
|
# iptables -P OUTPUT ACCEPT
|
|
----
|
|
|
|
The -F command with no arguments flushes all the chains in its current table. Similarly, -X deletes all empty non-default chains in a table.
|
|
|
|
Individual chains may be flushed or deleted by following -F and -X with a [chain] argument.
|
|
|
|
=== Editing rules
|
|
|
|
Rules can be edited by
|
|
|
|
- appending -A a rule to a chain,
|
|
|
|
- inserting -I it at a specific position on the chain,
|
|
|
|
- replacing -R an existing rule,
|
|
|
|
- or deleting -D it.
|
|
|
|
The first three commands are exemplified in the following.
|
|
|
|
First of all, our computer is not a router (unless, of course, it is a router). We want to change the default policy on the FORWARD chain from ACCEPT to DROP.
|
|
|
|
[source,bash]
|
|
----
|
|
# iptables -P FORWARD DROP
|
|
----
|
|
|
|
|
|
|
|
=== Examples
|
|
|
|
We going to use Shorewall as an iptables configuration tool. See Appendix.
|
|
|
|
Here some examples of "raw" iptables command lines.
|
|
|
|
|
|
==== Block Traffic by PortPermalink
|
|
|
|
You may use a port to block all traffic coming in on a specific interface. For example:
|
|
|
|
iptables -A INPUT -j DROP -p tcp --destination-port 110 -i eth0
|
|
|
|
Let’s examine what each part of this command does:
|
|
|
|
-A will add or append the rule to the end of the chain.
|
|
INPUT will add the rule to the table.
|
|
DROP means the packets are discarded.
|
|
-p tcp means the rule will only drop TCP packets.
|
|
--destination-port 110 filters packets targeted to port 110.
|
|
-i eth0 means this rule will impact only packets arriving on the eth0 interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[appendix]
|
|
== How to use iptables
|
|
|
|
|
|
|
|
Shorewall is not the easiest to use of the available iptables configuration tools but I believe that it is the most flexible and powerful.
|
|
|
|
It can handle complex and fast changing network environments.
|
|
|
|
It needs multiple configuration files, even for simple setups.
|
|
|
|
Suitable for powerusers! - Most likely there are a lot of these among our Students :-)
|
|
|
|
Shorewall is very popular!
|
|
|
|
|
|
|
|
|
|
|
|
https://wiki.archlinux.org[Origin]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:hardbreaks:
|
|
|
|
{empty} +
|
|
{empty} +
|
|
{empty}
|
|
|
|
:!hardbreaks:
|
|
|
|
'''
|
|
|
|
.Reminder
|
|
[NOTE]
|
|
====
|
|
:hardbreaks:
|
|
Caminante, no hay camino,
|
|
se hace camino al andar.
|
|
|
|
Wanderer, there is no path,
|
|
the path is made by walking.
|
|
|
|
*Antonio Machado* Campos de Castilla
|
|
====
|
|
|