Forum Moderators: bakedjake
:INPUT ACCEPT [0:0]
(...)
:Enemies - [0:0]
(...)
-A INPUT -j EnemiesThen in another iptables config file, I can add entries to the Enemies table with lines like: -A Enemies -s 99.99.99.99 -j DROPThe nice thing is that I can clear and rebuild the Enemies table without the fear that I delete important entries like the SSH lines. (...)
# my rules
:Ranges - [0:0]
:Dynamics - [0:0]
# my actions
-A INPUT -j Ranges
-A INPUT -j Dynamics
# existing rules
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT :INPUT ACCEPT [4927:1439976]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4896:2452260]
Ranges all -- anywhere anywhere
(...)
Chain Ranges (1 references)
target prot opt source destination sudo iptables-restore < /etc/iptables/rules.v4but it does not show the content of the external tables, only the references to the table itself. (...)
# my rules
:Ranges - [0:0]
:Ranges_80_443 - [0:0]
:Dynamics - [0:0]
# my actions
-A INPUT -j Ranges
-A INPUT -j Ranges_80_443
-A INPUT -j Dynamics
# existing rules
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT -A Ranges -s 20.192.0.0/10 -p tcp -m multiport --dports 443 -j DROP sudo cat /etc/iptables/rules.v4 /etc/iptables/Ranges.v4 /etc/iptables/Ranges_80_443.v4 | iptables-restore
sudo iptables -L -n -v -xYou can also load the rules with three separate commands, but the --noflush parameter has to be used to prevent iptables-restore to delete the contents of the previous loads. sudo iptables-restore < /etc/iptables/rules.v4
sudo iptables-restore --noflush < /etc/iptables/Ranges.v4
sudo iptables-restore --noflush < /etc/iptables/Ranges_80_443.v4
sudo iptables -L -n -v -x
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 ! -i lo -j REJECT --reject-with icmp-port-unreachable
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport (my ssh port) -j ACCEPT
-A INPUT -s (my IP)/32 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -j ACCEPT
-A INPUT -s 111.0.0.0/10 -p tcp --dport 443 -j DROP
-A INPUT -s 166.70.0.0/16 -p tcp --dport 443 -j DROP
-A INPUT -s 58.48.0.0/12 -j DROP
(etc)
-A INPUT -s 13.124.0.0/14 -p tcp --dport 443 -j DROP
-A INPUT -s 81.168.89.142/32 -p tcp --dport 443 -j DROP
COMMIT -A INPUT -j REJECT --reject-with icmp-port-unreachableYou should be safe with the following. It first accepts all localhost traffic and all established connections. This is for performance reasons. There is no need to check if a connection is from a bad source if the TCP handshake sequence was already successful. Then it jumps to the Enemies chain and processes all the DROP rules in the chain. If no enemy was found, processing is continued with scanning for new traffic on port 80, 443 and your SSH port. *filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:Enemies - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 ! -i lo -j REJECT --reject-with icmp-port-unreachable
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -j Enemies
-A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport (my ssh port) -j ACCEPT
-A INPUT -s (my IP)/32 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -j ACCEPT
-A Enemies -s 1.1.0.0/10 -p tcp --dport 443 -j DROP
-A Enemies -s 2.2.0.0/16 -p tcp --dport 443 -j DROP
-A Enemies -s 3.3.0.0/12 -j DROP
(etc)
-A Enemies -s 4.4.0.0/14 -p tcp --dport 443 -j DROP
-A Enemies -s 5.5.5.5/32 -p tcp --dport 443 -j DROP
COMMITAdded bonus with this setup with the Enemies chain is that you can manually add extra IP blocks while the firewall is running with a simple command line call: iptables -A Enemies -s 11.11.11.0/24 -j DROPThis will add an extra rule to the end of the Enemies chain. If you then run iptables-save, that new rule is included in the saved configuration.