LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 04-23-2014, 04:16 PM   #1
PGn
LQ Newbie
 
Registered: Apr 2014
Location: USA
Distribution: Debian 7.4 64bit & 32bit
Posts: 6

Rep: Reputation: Disabled
Most efficient way to block multiple IP addresses?


Hello,

I have a public server running Deb 7.4 amd64 multiarch where I host multiple public game servers. Current security includes iptables and fail2ban, among other things. Because this is a public server, I have to ban IP address for a variety of reasons including habitual break-in attempts and DoS or other types of server attacks. Most of the offending IPs are not from a contiguous range or from the same subnet, so they must be banned individually.

Because low latency is so very important for game servers I am always concerned about the server and network running as efficiently and as fast as possible. As such, I do not want to create any bottlenecks with excessive filtering of incoming or outgoing traffic.

Currently I block individual IPs using an iptables rule. However, the list of blocked IPs is getting rather long.

Q: Are individual "DROP" rules inserted into iptables the most efficient way to manage numerous IP bans or is there a better way?

Thank you for your help,

PGn

Last edited by PGn; 04-23-2014 at 04:39 PM.
 
Old 04-24-2014, 12:48 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PGn View Post
among other things.
Specifically which other measures?


Quote:
Originally Posted by PGn View Post
Are individual "DROP" rules inserted into iptables the most efficient way to manage numerous IP bans or is there a better way?
Sure: use ipset, preferably in the mangle table. BTW I ran a little test a while ago.
 
Old 04-24-2014, 02:06 AM   #3
yzT!
Member
 
Registered: Jan 2013
Distribution: Debian
Posts: 168

Rep: Reputation: 2
http://daemonkeeper.net/781/mass-blo...es-with-ipset/
 
Old 04-24-2014, 01:44 PM   #4
PGn
LQ Newbie
 
Registered: Apr 2014
Location: USA
Distribution: Debian 7.4 64bit & 32bit
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
Specifically which other measures?
Other measures include disabling SSH password authentication and root login in favor of RSA key pair authentication. Fail2ban is configured to permanently ban IPs that attempt brute-force SSH break-in. Is it possible to have SSH control (ssh restricted to a single ip only) handled by iptables and not use fail2ban? Alternatively, can IPs banned by fail2ban be automatically added to the ipset blacklist instead of staying with f2b?

Quote:
Originally Posted by unSpawn View Post
Sure: use ipset, preferably in the mangle table. BTW I ran a little test a while ago.
I've not yet used the mangle table, so all of the rules I've created must be in the filter table.

Code:
:~$ sudo iptables -t mangle --list
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
Can you help me understand why the ipset should be in the mangle table?

Than you,

PGn
 
Old 04-24-2014, 01:47 PM   #5
PGn
LQ Newbie
 
Registered: Apr 2014
Location: USA
Distribution: Debian 7.4 64bit & 32bit
Posts: 6

Original Poster
Rep: Reputation: Disabled
This is a great article, especially for someone like me who is not familiar with ipset. So essentially, I can remove all of the individual IP DROP rules from iptables and move all of the "bad" source IPs to a black list in ipset, correct?

Thank you,

PGn
 
Old 04-24-2014, 01:48 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would /etc/hosts.deny help here ?
 
Old 04-24-2014, 03:59 PM   #7
PGn
LQ Newbie
 
Registered: Apr 2014
Location: USA
Distribution: Debian 7.4 64bit & 32bit
Posts: 6

Original Poster
Rep: Reputation: Disabled
Public forums like LQ with helpful people are so valuable. From the replies to my first post on LQ, I've already learned a number of new and very useful techniques.

Having read the ipset article referenced by yzT!, I did a test install of ipset on my dev server, created the new set (ip_blacklist) and loaded several hundred "bad" ips with a quick for-loop. The article suggests this iptables rule to reference the new set:
Code:
iptables -I INPUT  -m set --match-set ip_blacklist src -p TCP \
     --destination-port 80 -j REJECT
Two points here: 1. since my server is not a web server I want all traffic from the blacklisted IPs stopped, not just tcp and not just to port 80. 2. Why use REJECT rather than DROP? I think the iptables rule I need is:
Code:
iptables -I INPUT  -m set --match-set ip_blacklist src -j DROP
Is this the right rule to use?

Before moving this onto the production server I am also wondering if null routing the IPs at the kernel would be even more efficient than using ipset?
Code:
route add xxx.xxx.xxx.xxx reject
Thank you again for your help,

PGn
 
Old 04-24-2014, 04:03 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by schneidz View Post
would /etc/hosts.deny help here ?
No, please see Denyhosts vs Fail2ban aka tcp_wrappers vs iptables.
 
Old 04-24-2014, 04:31 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PGn View Post
Is it possible to have SSH control (ssh restricted to a single ip only) handled by iptables and not use fail2ban?
Given you've followed the SSH Best practices and since SSH access is already restricted to one IP address only (a white listing scenario) there is absolutely no need to bother with blocking IP addresses trying to access TCP/22.


Quote:
Originally Posted by PGn View Post
Alternatively, can IPs banned by fail2ban be automatically added to the ipset blacklist instead of staying with f2b?
Yes, fail2ban can use ipset for some time now.


Quote:
Originally Posted by PGn View Post
Can you help me understand why the ipset should be in the mangle table?
The Frozentux Iptables-tutorial shows you table traversal, mangle comes before input, and since you want to just drop traffic and not do anything with it it's advised to drop that traffic as early on as possible. Also, since you'll be using the filter table for traffic that actually matters it keeps the rule set much cleaner which makes it easier to maintain.


Quote:
Originally Posted by PGn View Post
So essentially, I can remove all of the individual IP DROP rules from iptables and move all of the "bad" source IPs to a black list in ipset, correct?
Correct.


Quote:
Originally Posted by PGn View Post
since my server is not a web server I want all traffic from the blacklisted IPs stopped, not just tcp and not just to port 80.
Jst adjust the rule then.


Quote:
Originally Posted by PGn View Post
Why use REJECT rather than DROP?
That's a matter of preference (or courtesy): as 'man iptables' tells you the REJECT target sends back an ICMP message while DROP just drops the packet in the bit bucket.


Quote:
Originally Posted by PGn View Post
I think the iptables rule I need is:
Code:
iptables -I INPUT  -m set --match-set ip_blacklist src -j DROP
Is this the right rule to use?
These commands makes it the first rule in the raw (disabling connection tracking) and mangle (drop) tables prerouting chains:
Code:
iptables -t raw -I PREROUTING 1 -m set --match-set ip_blacklist src -j CT --notrack;
iptables -t mangle -I PREROUTING 1 -m set --match-set ip_blacklist src -j DROP

Quote:
Originally Posted by PGn View Post
Before moving this onto the production server I am also wondering if null routing the IPs at the kernel would be even more efficient than using ipset?
Where does that "even more" come from? What have you read that makes you say that? Null routing, just like iptables drop rules, denies any remote system to establish (SYN) a connection. The difference is that with null routing traffic is still(!) received: your system just can't send anything (SYN,ACK) back, while iptables is more fine grained and explicitly drops that traffic.
 
Old 04-27-2014, 04:11 PM   #10
PGn
LQ Newbie
 
Registered: Apr 2014
Location: USA
Distribution: Debian 7.4 64bit & 32bit
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
Given you've followed the SSH Best practices and since SSH access is already restricted to one IP address only (a white listing scenario) there is absolutely no need to bother with blocking IP addresses trying to access TCP/22.
Understood. However, the reason for asking is because the server has on multiple occasions been flooded with brute-force or other types of unauthorized break-in attempts to the point that I can't even login over SSH because the server is overloaded and unresponsive. Therefore I was looking for a way to block or drop all unauthorized connection requests before they have to be individually processed as invalid.

Quote:
Originally Posted by unSpawn View Post
These commands makes it the first rule in the raw (disabling connection tracking) and mangle (drop) tables prerouting chains:
Code:
iptables -t raw -I PREROUTING 1 -m set --match-set ip_blacklist src -j CT --notrack;
iptables -t mangle -I PREROUTING 1 -m set --match-set ip_blacklist src -j DROP
Thank you! These are exactly what I needed and are very instructive too.

Quote:
Originally Posted by unSpawn View Post
Where does that "even more" come from? What have you read that makes you say that? Null routing, just like iptables drop rules, denies any remote system to establish (SYN) a connection. The difference is that with null routing traffic is still(!) received: your system just can't send anything (SYN,ACK) back, while iptables is more fine grained and explicitly drops that traffic.
Thank you for the explanation. "even more" was just a question based on the desire to learn "best practices". Being relatively new to Linux administration there are still so many areas where I have only scratched the surface...

Thank you again for all of you help,

PGn
 
Old 04-28-2014, 01:29 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PGn View Post
Understood. However, the reason for asking is because the server has on multiple occasions been flooded with brute-force or other types of unauthorized break-in attempts to the point that I can't even login over SSH because the server is overloaded and unresponsive. Therefore I was looking for a way to block or drop all unauthorized connection requests before they have to be individually processed as invalid.
Within a server all services use the same resource pool. So if for example your machine is being (ab)used to send spam because you forgot to update WordPress or one of its plugins that will affect the web server and consequently any other service vying for resources. Apart from using a cap on resources there's also iptables modules that limit the amount of connections a single IP address (or range!) is allowed to initiate. That's something you want to implement efficiently at the network layer and not higher up in the application layer.


Quote:
Originally Posted by PGn View Post
Being relatively new to Linux administration there are still so many areas where I have only scratched the surface...
It's a good thing to know where you stand and as long as you show you've done some research, as long as you're willing to question responses (not all replies or web log posts are actually good answers), learn from others and practice yourself (doesn't make perfect but it does help ;-p) you can only grow.

Good luck!
 
  


Reply



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
[SOLVED] Trying to block all IP addresses except US and CA. Dafydd Programming 5 04-03-2013 01:03 PM
How to block the ip addresses using iptables onlymahendra7 Linux - Networking 5 05-27-2012 10:57 AM
Block ALL IP addresses only allow 3 IP addresses on port 80/443 yelluc Linux - Security 8 03-28-2012 04:20 AM
scan a block of addresses fentonc2003 Linux - Networking 1 11-21-2006 04:33 AM
block specific ip addresses paperdiesel Linux - Security 3 07-21-2004 11:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:24 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