Sophisticated iptables firewall using `recent` triggering and ipset
In this article are shown example instructions for a simple firewall disallowing new connections if the remote host initiates a connection too often. This is especially the case with SSH bruteforce attacks. Most administrators know the feeling of annoyance when they look at the system security logs and notice the enormous amounts of failed SSH logins.
What makes the described solution 'sophisticated' is that there is no need to install and configure a separate log watcher daemon – just bring in some firewall rules. The setup might be most useful in the context of security-sensitive hosts where additional security measures would be justified, even more so if, for some reason, strong (e.g. key based) authentication can't be used.
This article describes deploying IPv4 address bans based on how often a remote client tries to connect to a specific port, using netfilter's
Matching lists of addresses or networks by using just
iptables is indeed messy because
iptables as itself does not support matching multiple separate addresses or networks in one rule. This means that every checked address or network would need their own rule in the ruleset. Such complicated and long rulesets bring administration and performance concerns.
ipset a list of addresses (or networks, etc.) can be matched from one rule. Performance considerations such as indexing the address set make matching and lookups a lot more efficient.
The example is as on CentOS 7. Similar should be achievable on other systems too.
First, the needed tools should be installed:
# yum install iptables iptables-services ipset ipset-service
All offending IPv4 addresses are going to be saved, so edit
/etc/sysconfig/ipset-config and set
IPSET_SAVE_ON_STOP="yes". If wanted, the
ipset set can be created with the timeout parameter which makes the set's entries expire. In this example the entries will last until removed by hand.
# ipset create sshin_bans hash:ip # service ipset save # systemctl start ipset # systemctl enable ipset
Insert desired ruleset into
/etc/sysconfig/iptables. Following is a simple example ruleset:
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :SSHIN - [0:0] # 1 -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -s 10.23.45.67/28 -m conntrack --ctstate NEW -j ACCEPT # 2 -A INPUT -m set --match-set sshin_bans src -j DROP # 3-4 -A INPUT -p icmp -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j SSHIN # 5 Block direct SSH bruteforce -A SSHIN -m recent --set --name bruteforce -A SSHIN -m recent --update --seconds 3600 --hitcount 5 --name bruteforce -j LOG --log-level info --log-prefix "SSH blocked: " -A SSHIN -m recent --update --seconds 3600 --hitcount 5 --name bruteforce -j SET --add-set sshin_bans src -A SSHIN -m recent --update --seconds 3600 --hitcount 5 --name bruteforce -j DROP -A SSHIN -j ACCEPT # 6 if you want to filter ports: #-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT #-A INPUT -j REJECT --reject-with icmp-host-prohibited #-A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
Explanation of the above ruleset:
- Established, localhost, and new trusted-net SSH connections are allowed right away.
- The sshin_bans address list (set) is checked. If the source address is found from the set the connection is immediately dropped (i.e. no connections at all, to any port, are allowed from that specific address).
- ICMP is allowed.
- New SSH (port 22) connections are directed to SSHIN chain for further evaluation.
- SSH connection counting and processing magic:
- an iptables
bruteforceis checked/updated for the packet source address.
recenttable is inspected:
- if during 3600 seconds (1 hour) there are 5 or more hits to the rule, then log, add address to sshin_bans ipset, and drop the new connection;
- otherwise, accept the connection.
- an iptables
- Optional filter rules.
Then enabling the ruleset:
# systemctl start iptables # iptables-restore /etc/sysconfig/iptables # service iptables save # systemctl enable iptables
As noted before, the ban list is persistent because the entries don't expire and the set is saved upon shutdown.
ipset del sshin_bans 172.16.6.10 removes an address from the set and allows that host to connect again, provided it does not trigger a new ban from
iptables's recent match.
With ipset and the SET target it's easy to catch also those hosts which try to connect to a port with no daemon listening on it. If there supposedly was nothing listening on port 25, just by having a rule such as
-A INPUT -p tcp --dport 25 -j SET --add-set honeypot_victims src adds the remote host to the
honeypot_victims set when the offending host matches the chain – i.e. the remote host tried to connect to port 25.
It should be clear anyway that one got to know what they are doing when using this kind of setup. There might be innocent errors which make clients connect to wrong host or port and denying access on the basis of so primitive heuristic might open up the chance for denial of service.
The access control method shown in this article is lighter and more streamlined because it does not need a separate log watcher daemon (e.g. fail2ban). Another good thing is that netfilter is usually readily available and in use anyway, so accommodating a few more rules to the ruleset does not require much extra effort.
This is not, however, a universal solution. The real downside is that netfilter counts only incoming connections, not failed logins, and therefore it could be more easy to accidentally lock oneself out or accidentally cause a small denial of service.
Still, the principal solution for denying access of abusive hosts as described in this article has been tried and tested by the author for a considerable length of time. Even on a small-scale multiuser system there has only been a tiny amount of bans for legitimate users over several years (one or two cases) but obviously this is not a large-scale multiuser solution.
If the presented example solution wasn't suitable, a better and more intelligent solution might be sshguard which has the ability to catch offenders during a longer timespan. sshguard can feed IP addresses into an
ipset set too.