LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-07-2022, 11:47 AM   #1
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Rep: Reputation: 0
iptables is ABSOLUTE TRASH !!


Hello, I've been wrestling with this problem for a while now.

I have a list of ~50 bad IPs that I want iptables to log if matched and then drop, in both INPUT AND FORWARD.

For example IPs like this:
Code:
-s 10.0.0.0/8 -j DROP
-s 172.16.0.0/12 -j DROP
-s 192.168.0.0/16 -j DROP
-s 169.254.0.0/16 -j DROP
-s 176.0.0.0/8 -j DROP
After some extensive google research I found two ways to log and drop packets that were matched against a rule.

Option a.) Adding a LOG and DROP rule, separately for each IP:

Example in my case:
Code:
-N droplist
-A INPUT -j droplist
-A FORWARD -j droplist

-A droplist -s 10.0.0.0/8 -j LOG --log-level 4
-A droplist -s 10.0.0.0/8 -j DROP
-A droplist -s 172.16.0.0/12 -j LOG --log-level 4
-A droplist -s 172.16.0.0/12 -j DROP
-A droplist -s 192.168.0.0/16 -j LOG --log-level 4
-A droplist -s 192.168.0.0/16 -j DROP
-A droplist -s 169.254.0.0/16 -j LOG --log-level 4
-A droplist -s 169.254.0.0/16 -j DROP
-A droplist -s 176.0.0.0/8 -j LOG --log-level 4
-A droplist -s 176.0.0.0/8 -j DROP
Option b.) Creating LOG chain that logs/drops and jumping there from each rule:
Code:
-N droplist
-N logging
-A INPUT -j droplist
-A FORWARD -j droplist

-A droplist -s 10.0.0.0/8 -j logging
-A droplist -s 172.16.0.0/12 -j logging
-A droplist -s 192.168.0.0/16 -j logging
-A droplist -s 169.254.0.0/16 -j logging
-A droplist -s 176.0.0.0/8 -j logging

-A logging -j LOG --log-level 4
-A logging -j DROP
Option b.) is apparently unsafe because the packets aren't dropped, immediately.

There MUST be a better and more elegant way to do this, right?

Last edited by amateur_intermediate; 01-07-2022 at 02:46 PM.
 
Old 01-07-2022, 11:51 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by amateur_intermediate View Post
There MUST be a better and more elegant way to do this, right?
Yes. Development on IPTables has slowed or almost stopped. The much easier way to work with packet filtering on GNU/Linux systems is NFTables:

https://wiki.nftables.org/

https://wiki.nftables.org/wiki-nftab..._in_10_minutes

Remove IPTables and install NFTables, and then give it a try with your task.
 
Old 01-07-2022, 11:58 AM   #3
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Yes. Development on IPTables has slowed or almost stopped.

Remove IPTables and install NFTables, and then give it a try with your task.
Hi Turbo, thanks for your reply!

So... while I do hate on iptables, I've spent a long time carefully reading all kinds of documentation and finally feel comfortable with it now. If possible I'd like to avoid having to learn something new lol.

Can you think of a better way to achieve my goal within iptables?
 
Old 01-07-2022, 12:00 PM   #4
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Quote:
Originally Posted by Turbocapitalist View Post
Yes. Development on IPTables has slowed or almost stopped. The much easier way to work with packet filtering on GNU/Linux systems is NFTables:

https://wiki.nftables.org/

https://wiki.nftables.org/wiki-nftab..._in_10_minutes

Remove IPTables and install NFTables, and then give it a try with your task.
thanks for that insight, i have a one question : does it support NFQUEUE ? i am using suricata in IPS mode.

i just found this from linux mint forums : https://forums.linuxmint.com/viewtopic.php?t=288764

looks like i'll have to rebuild my suricata setup
 
Old 01-07-2022, 12:08 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Sunk cost fallacy, it's also what keeps too many on M$ Windows ... but be that as it may, if you wish to throw good money^Wtime after bad, then take a look at this part of your script:

Code:
...
-A INPUT -j droplist
-A FORWARD -j droplist
...
The -A puts the test at the very end of everything else. That's almost certainly not what you want, since some other rule will probably let the packet through first. Instead the -I option can take a number as a parameter in order to place the new rule at a certain position among the existing rules.


UNtested, but something like this is closer:

Code:
...
-I INPUT 3 -j droplist
-I FORWARD 3 -j droplist
-I OUTPUT 3 -j droplist
...
That of course depends on what rules 1 and 2 are. You definitely want to give the highest priority to processing the loopback and established or related connections.

The details are determined by how the network will be used, what kind of system is this for? Desktop? Router? Server? Other?

Last edited by Turbocapitalist; 01-07-2022 at 12:14 PM. Reason: swap sequence
 
Old 01-07-2022, 12:11 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Oops. Race condition. Disregard the previous post then.

With NFTables, take a look at sets for dealing with pools of addresses:

https://wiki.nftables.org/wiki-nftables/index.php/Sets
 
Old 01-07-2022, 12:28 PM   #7
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
...
-A INPUT -j droplist
-A FORWARD -j droplist
...


The -A puts the test at the very end of everything else. That's almost certainly not what you want, since some other rule will probably let the packet through first. Instead the -I option can take a number as a parameter in order to place the new rule at a certain position among the existing rules.
Sorry, I should've posted the entire table for clarity.
Code:
-P INPUT DROP
-P FORWARD DROP
-P OUTPUT ACCEPT

-N droplist
-N logging
-A INPUT -j droplist
-A FORWARD -j droplist

-A droplist -s 10.0.0.0/8 -j logging
-A droplist -s 172.16.0.0/12 -j logging
-A droplist -s 192.168.0.0/16 -j logging
-A droplist -s 169.254.0.0/16 -j logging
-A droplist -s 176.0.0.0/8 -j logging

-A logging -j LOG --log-level 4
-A logging -j DROP

-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
So that's the actual begging - end.

As far as I understand, because
Code:
-A INPUT -j droplist
-A FORWARD -j droplist
are placed at the very beginning, before any other rules, both chains will immediately jump to droplist and traverse the rules in exactly that order and before anything else. If no rule in droplist matches, it will jump back to the beginning of INPUT or FORWARD chain. I got that from here: https://www.lammertbies.nl/nl/comm/info/iptables

I read in the docs that depending where you jump to a custom chain from one of the main chains, iptables will traverse the custom chain and if no rule matches, it jumps back to the original chain one rule below the jump. Is that incorrect? (getting really confused now)

Last edited by amateur_intermediate; 01-07-2022 at 12:32 PM.
 
Old 01-07-2022, 12:32 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I don't know if you are referring to this web page but building a script to load rules would be a more elegant way. In the end the rules will still be the same.

https://www.cyberciti.biz/faq/iptabl...rom-text-file/

Rules are operated basically in sequential order so option b is not necessarily unsafe but adds an extra step to complicate things a bit.
 
1 members found this post helpful.
Old 01-07-2022, 01:08 PM   #9
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
I don't know if you are referring to this web page but building a script to load rules would be a more elegant way. In the end the rules will still be the same.

https://www.cyberciti.biz/faq/iptabl...rom-text-file/

Rules are operated basically in sequential order so option b is not necessarily unsafe but adds an extra step to complicate things a bit.
Thank you, that's perfect. I only have two questions, if you don't mind.

1.) if no interface is defined, would the rules be applied to all interfaces? Or do I have to name each separately, i.e for eth0, eth1 etc.?

So this for all interfaces:
Code:
$IPT -A droplist -s $ip -j LOG --log-prefix " myBad IP BlockList  "
$IPT -A droplist -s $ip -j DROP
2.) Is it possible to add a custom log prefix for each type of packet?

Last edited by amateur_intermediate; 01-07-2022 at 01:34 PM.
 
Old 01-07-2022, 01:19 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
1. All interfaces.
2. Yes --log-prefix "what ever"

Depending on distribution once the script is run there is iptables-save or similar command to save the rules so they are automatically loaded at boot up.

Last edited by michaelk; 01-07-2022 at 01:31 PM.
 
1 members found this post helpful.
Old 01-07-2022, 01:33 PM   #11
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
1. All interfaces.
2. Yes --log-prefix "what ever"
Awesome, thanks! Trying to find a +rep or thanks button.

One very last thing, sorry.

Would it be possible to include rules that don't use an IP in the list or script?

For example stuff like this:

Code:
 -p tcp -m tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
 -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
 -p tcp --tcp-flags ALL ALL -j DROP
 -p tcp --tcp-flags ALL NONE -j DROP

Last edited by amateur_intermediate; 01-07-2022 at 01:46 PM.
 
Old 01-07-2022, 02:09 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Yes if you look at the example script in the posted link it shows additional rules added after the drop list loop.
 
1 members found this post helpful.
Old 01-07-2022, 02:44 PM   #13
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Yes if you look at the example script in the posted link it shows additional rules added after the drop list loop.
Yeah, thanks. I'll just have a second text file with all those rules and a second loop for that.

I tested the script and basically it does Option a.) from the OP, one LOG and DROP rule, for each IP. With 50 IPs this is extremely long.

Are you sure this is better than Option b.), droplist jumping to logging chain?

You said it adds an extra step, which could complicate things, but also that its not necessarily unsafe.

Could you maybe explain in more detail?
 
Old 01-07-2022, 02:55 PM   #14
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As you seem to be on the trail of a working solution provided by the useful posts of others I will not offer a complete alternate example as that might send you down another possibly unnecessary path.

But I will make a few suggestions for your future explorations:

* Although nftables is the new kid on the block, iptables is very mature and useful and is not going anywhere in a hurry! If you have invested time in learning iptables make use of it, there is no need to scrap what you know - use it!

* When you wish to perform actions on large sets of IP addresses consider use of ipsets: Add new addresses to the set from file, manually or dynamically with other iptables rules, then simply test membership in the set to perform the desired actions.

* When you really just want to block a set of IPs put that rule early in the path, such as in the raw table PREROUTING chain. And if all you really want is a count of attempts by IP, avoid the overhead of logging and just add a counter to the set.

* You may also want to learn to use tc which can also perform tests on set membership and drop packets before they are even presented to iptables for processing - very efficient combination!

* Consider changing the title of your threads here to something less perjorative: iptables is certainly not absolute trash and posturing your question under that title might result in some members with helpful knowledge bypassing the thread altogether.

Good luck with iptables and welcome to LQ!

Last edited by astrogeek; 01-07-2022 at 02:59 PM. Reason: tpoys
 
2 members found this post helpful.
Old 01-07-2022, 03:05 PM   #15
amateur_intermediate
LQ Newbie
 
Registered: Jan 2022
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astrogeek View Post
* When you wish to perform actions on large sets of IP addresses consider use of ipsets: Add new addresses to the set from file, manually or dynamically with other iptables rules, then simply test membership in the set to perform the desired actions.

* When you really just want to block a set of IPs put that rule early in the path, such as in the raw table PREROUTING chain. And if all you really want is a count of attempts by IP, avoid the overhead of logging and just add a counter to the set.
Hello and thanks for the warm welcome!

Also thank you for the awesome info, sorry I wrote the title when I was super mad.

I have heard of ipset yes very cool, but I couldn't find out how to LOG each matched rule with a specific prefix and then drop it.

So far the only way I can see that working is with this script and a simple if & grep to change the prefix string according to what was matched.

Code:
IPT=/sbin/iptables
$IPT -N droplist
egrep -v "^#|^$" x | while IFS= read -r ip
do
	$IPT -A droplist -i eth1 -s $ip -j LOG --log-prefix " myBad IP BlockList  "
	$IPT -A droplist -i eth1 -s $ip -j DROP
done < "$_input"
# Drop it 
$IPT -I INPUT -j droplist
$IPT -I OUTPUT -j droplist
$IPT -I FORWARD -j droplist
How do you add a counter? I thought you're just supposed to do this to see a counter?
Code:
iptables -v -L --line-numbers
I read filtering in PREROUTING is not recommended, because it might be ignored.

Last edited by amateur_intermediate; 01-07-2022 at 03:26 PM.
 
  


Reply

Tags
iptables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trash is Full. Empty Trash before moving more files/folders to Trash. wdarledge Slackware 45 12-13-2017 03:53 PM
LXer: Absolute Linux is an absolute winner LXer Syndicated Linux News 0 08-07-2007 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration