2016-09-22 12:43:57 +00:00
---
title: About the iptables Resource
2018-02-16 00:28:15 +00:00
platform: linux
2016-09-22 12:43:57 +00:00
---
# iptables
Use the `iptables` InSpec audit resource to test rules that are defined in `iptables`, which maintains tables of IP packet filtering rules. There may be more than one table. Each table contains one (or more) chains (both built-in and custom). A chain is a list of rules that match packets. When the rule matches, the rule defines what target to assign to the packet.
2017-10-03 21:35:10 +00:00
<br>
2016-09-27 19:03:23 +00:00
## Syntax
2016-09-22 12:43:57 +00:00
A `iptables` resource block declares tests for rules in IP tables:
describe iptables(rule:'name', table:'name', chain: 'name') do
it { should have_rule('RULE') }
end
where
* `iptables()` may specify any combination of `rule`, `table`, or `chain`
* `rule:'name'` is the name of a rule that matches a set of packets
* `table:'name'` is the packet matching table against which the test is run
* `chain: 'name'` is the name of a user-defined chain or one of `ACCEPT`, `DROP`, `QUEUE`, or `RETURN`
2017-06-28 10:17:24 +00:00
* `have_rule('RULE')` tests that rule in the iptables list. This must match the entire line taken from `iptables -S CHAIN`.
2016-09-22 12:43:57 +00:00
2017-10-03 21:35:10 +00:00
<br>
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
## Examples
2016-09-22 12:43:57 +00:00
The following examples show how to use this InSpec audit resource.
2017-06-28 10:17:24 +00:00
### Test if the INPUT chain is in default ACCEPT mode
2016-09-22 12:43:57 +00:00
describe iptables do
it { should have_rule('-P INPUT ACCEPT') }
end
2017-06-28 10:17:24 +00:00
### Test if the INPUT chain from the mangle table is in ACCEPT mode
2016-09-22 12:43:57 +00:00
2017-06-28 10:17:24 +00:00
describe iptables(table:'mangle', chain: 'INPUT') do
2016-09-22 12:43:57 +00:00
it { should have_rule('-P INPUT ACCEPT') }
end
2017-06-28 10:17:24 +00:00
### Test if there is a rule allowing Postgres (5432/TCP) traffic
describe iptables do
it { should have_rule('-A INPUT -p tcp -m tcp -m multiport --dports 5432 -m comment --comment "postgres" -j ACCEPT') }
end
Note that the rule specification must exactly match what's in the output of `iptables -S INPUT`, which will depend on how you've built your rules.
2017-10-03 21:35:10 +00:00
<br>
## Matchers
2018-02-16 00:28:15 +00:00
For a full list of available matchers please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
2017-10-03 21:35:10 +00:00
### have_rule
The `have_rule` matcher tests the named rule against the information in the `iptables` file:
it { should have_rule('RULE') }