Iptables 8
Add Chains
Logging Multiple Rules
You may want to drop a number of IP addresses or blocks. You may also want to log everything you drop. The way things are so far, you need to have two rules for each, one for logging and the other for dropping.
iptables -A INPUT -s 10.0.0.0/8 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 10.0.0.0/8 -j DROP iptables -A INPUT -s 172.16.0.0/12 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 172.16.0.0/12 -j DROP iptables -A INPUT -s 192.168.0.0/16 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 192.168.0.0/16 -j DROP iptables -A INPUT -s 203.194.0.0/18 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 203.194.0.0/18 -j DROP iptables -A INPUT -s 60.208.0.0/12 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 60.208.0.0/12 -j DROP iptables -A INPUT -s 202.96.0.0/12 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 202.96.0.0/12 -j DROP iptables -A INPUT -s 60.0.0.0/11 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 60.0.0.0/11 -j DROP iptables -A INPUT -s 222.192.0.0/11 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 222.192.0.0/11 -j DROP iptables -A INPUT -s 203.193.128.0/18 -j LOG --log-level 4 -log-prefix 'SourceDrop ' iptables -A INPUT -s 203.193.128.0/18 -j DROP |
Notice how long the list is getting. As you add more rules the list may get much longer.
Adding Chains
You can avoid having each rule twice by adding another chain. Let's call it LOGDROP.
To add this chain, open the Root Terminal and type:
This tells Iptables to create a new chain called LOGDROP. This only needs to be done once, unless you delete it, or reinstall the operating system.
If you type "iptables -L" you will now see INPUT, FORWARD, OUTPUT and LOGDROP chains.
You can use any name for the chain, and add more chains if you want.
Let's add rules to this chain.
iptables -A LOGDROP -j LOG --log-level 4 --log-prefix 'SourceDrop ' iptables -A LOGDROP -j DROP |
These rules need to be added to the Iptables rules.
The rules above can now look like this:
iptables -A INPUT -s 10.0.0.0/8 -j LOGDROP iptables -A INPUT -s 172.16.0.0/12 -j LOGDROP iptables -A INPUT -s 192.168.0.0/16 -j LOGDROP iptables -A INPUT -s 203.194.0.0/18 -j LOGDROP iptables -A INPUT -s 60.208.0.0/12 -j LOGDROP iptables -A INPUT -s 202.96.0.0/12 -j LOGDROP iptables -A INPUT -s 60.0.0.0/11 -j LOGDROP iptables -A INPUT -s 222.192.0.0/11 -j LOGDROP iptables -A INPUT -s 203.193.128.0/18 -j LOGDROP |
Each rule only needs to be written once, which saves a lot of space, particularly as the list gets longer.
Notice the "-j" which has previously been followed by ACCEPT, DROP or LOG, now tells Iptables to "jump" to LOGDROP. LOGDROP tells Iptables to log the information then drop the packet.
If these new rules are added to the earlier rules, you should have:
iptables -F iptables -A INPUT -m state --state INVALID -j LOG --log-level 4 --log-prefix 'InvalidDrop ' iptables -A INPUT -m state --state INVALID -j DROP iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m limit --limit 2/min -j LOG --log-level 4 --log-prefix 'In2/m ' iptables -A INPUT -s 10.0.0.0/8 -j LOGDROP iptables -A INPUT -s 172.16.0.0/12 -j LOGDROP iptables -A INPUT -s 192.168.0.0/16 -j LOGDROP iptables -A INPUT -s 203.194.0.0/18 -j LOGDROP iptables -A INPUT -s 60.208.0.0/12 -j LOGDROP iptables -A INPUT -s 202.96.0.0/12 -j LOGDROP iptables -A INPUT -s 60.0.0.0/11 -j LOGDROP iptables -A INPUT -s 222.192.0.0/11 -j LOGDROP iptables -A INPUT -s 203.193.128.0/18 -j LOGDROP iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p tcp --sport 80 -j ACCEPT iptables -A INPUT -p udp --sport 53 -j ACCEPT iptables -A INPUT -j LOG --log-level 4 --log-prefix 'InDrop ' iptables -A INPUT -j DROP iptables -A OUTPUT -o lo -j ACCEPT iptables -A OUTPUT -m limit -limit 6/hour -j LOG --log-level 4 --log-prefix 'OutAllow6/h ' iptables -A OUTPUT -j ACCEPT iptables -A LOGDROP -j LOG --log-level 4 --log-prefix 'SourceDrop ' iptables -A LOGDROP -j DROP iptables-save > /etc/iptables.rules |
Deleting Chains
Chains may also be deleted. They can only be deleted when they are empty. To delete everything from Iptables type:
To delete the chain LOGDROP, type:
© Copyright Guy Shipard 2008 - 2009
|