SSH Brute force attacks - Script version 1.0
Pardon my inability to reply to the original post, but I just subscribed. Here is the version of the script I am considering, but have questions about.
iptables -A INPUT -p tcp --dport 22 -s ! $My_Home_Firewall_IP -m state --state NEW -m recent --name SSH --set --rsource -j SSH_BF iptables -A SSH_BF -m recent ! --rcheck --seconds 60 --hitcount 3 --name SSH --rsource -j RETURN
iptables -A SSH_BF -j LOG --log-prefix "SSH Brute Force Attempt: " iptables -A SSH_BF -p tcp -j TARPIT
(1) First of all, why should there be a -p tcp in the TARPIT line? Somewhere in the original thread I think there was mention of nonTCP packets that would get into the script and that such packets shouldn't get dropped, but the rule that jumps to our SSH_BF script only matches TCP packets anyway. Looks like something's wrong if nonTCP packets get to the chain at all.
(2) Next, people interested in implementing this sort of protection might consider changing the duration and number of matches. For example, the current 60:3 settings allow a total of 180 attempts in an hour but only in bursts of three per minute. We can consider two classes of people attempting to connect to the server: brute force attackers and legitimate scripts and human users. In the former case, attempts per unit time as time goes to infinity is what they are concerned about. In the latter case, access is usually bursty. Consider allowing 50 matches every hour, as in
iptables -A SSH_Brute_Force -m recent ! --rcheck --seconds 3600 --hitcount 50 --name SSH --rsource -j RETURN
Now attackers have fewer than a third of the attacks per hour they can mount on the system, but chatty backup scripts or admins are allowed the bursty access (like opening 5 shells/scp streams at once) that they might sometimes need or like to use. (Whether this legitimate but potentially disruptive behavior should be allowed is another thing.)
(3) Also, is this the only position for the negation that makes the rule work as intended? It seems to say "Allow/return packets where it isn't the case that we've seen more than or equal to three connection attempts in the last 60 seconds." To me, the following is more intuitive:
iptables -A SSH_Brute_Force -m recent --rcheck --seconds 60 ! --hitcount 3 --name SSH --rsource -j RETURN
which reads "Allow/return packets from sources that have attempted connections less than three times in the past 60 seconds." This is totally a subjective preference, but in the interest of learning how the recent match really works and avoiding stupid mistakes, I want to verify that this revised rule would have the same effect in the context of this chain.
Thanks!
Hi everybody,
Quoted 1 line · curby .
(1) First of all, why should there be a -p tcp in the TARPIT line?
This is because what TARPIT does, only works with tcp. It is similar to the fact, that for example the specification of --dport in a rule only works with either -p tcp or -p udp. Since the next logical question might be "But what exactly does TARPIT do that only works with tcp?" I took the freedom and googled a site that I think covers the rough basics quite straightforward: http://cpc.freeshell.org/linux/kernel-tarpit.html
The short version of this is: the -p tcp is not for a special filtering purpose, it's there because -j TARPIT wants it and you just have to do it ;-)
Quoted 2 lines
(2) Next, people interested in implementing this sort of protection might consider changing the duration and number of matches.
IMHO this is mostly just a matter of personal taste (or rather, what your network environment needs), so it is all your choice how you configure it. Personally I like to use it with short timespan and very small number of connections, because SSH access from outside is not frequently needed, just from time to time. The main reason for me to use it, is to stop script(kiddie)s from using my bandwidth, processor time and precious harddrive space by filling my logs with their attempts guess a weak username/password. Since none of them ever tried more than 50 combinations, I am very happy with DROPping them for a while until they move to the next host after their third connection attempt. But if you are more afraid that somebody might try to get into your machine by guessing passwords over an extended period of time, the longe timespan with a corresponding number of connections might be more suitable for you. Also if your SSH is used in a "bulky" way like you described it, a low number of tries within a short timespan might not be suitable for your setup. But as I said, my reason for using this is more to annoy automated scans, than to provide additional protection. Your passwords/allowed users should be configured in a way, that even without additional limits you don't have to worry about anybody breaking in. So the best is to see this ruleset as a nice addition that keeps your logs cleaner.
Quoted 2 lines
(3) Also, is this the only position for the negation that makes the rule work as intended?
Yes, you are telling the recent module to do an rcheck in the list, the --seconds and --hitcount just specifiy further criteria for that rcheck. The negation belongs to the whole rcheck construct, you cannot invert just single comparisons within that. Btw, be careful there, at least with my installation an incorrectly placed "!" does not trigger an error message, but is just ignored, so your rule might do the opposite of what you wanted it to do. For details and where negations are valid you can also have a look at "iptables -m recent --help"
HTH,
Marius
On another note; I was criticized for using
-m limit --limit 2/minute -p tcp --dport 22
which was too perceived as too restrictive by some (or just one). Anyway, I've looked closer into the manpage of dstlimit and I would be glad to exchange my lines with
-m dstlimit --dstlimit 2/minute --dstlimit-mode srcip-dstip -p tcp --dport 22
Comments please :)
Jan Engelhardt
Signature · 2 lines
| Gesellschaft fuer Wissenschaftliche Datenverarbeitung Goettingen, | Am Fassberg, 37077 Goettingen, www.gwdg.de
Quoted 11 lines · Marius Mertens
Quoted 2 lines
(3) Also, is this the only position for the negation that makes the rule work as intended?
Yes, you are telling the recent module to do an rcheck in the list, the --seconds and --hitcount just specifiy further criteria for that rcheck. The negation belongs to the whole rcheck construct, you cannot invert just single comparisons within that. Btw, be careful there, at least with my installation an incorrectly placed "!" does not trigger an error message, but is just ignored, so your rule might do the opposite of what you wanted it to do. For details and where negations are valid you can also have a look at "iptables -m recent --help"
Ah, indeed that command shows that --seconds and --hitcount cannot take a negation. I was going by
http://www.netfilter.org/documentation/HOWTO//netfilter-extensions-HOWTO-3.html#ss3.16
which shows that almost all options can take a negation. Reading the fine manual was my downfall. =)
Thank you for the informative response.
Hi everybody,
Quoted 5 lines · Jan Engelhardt
On another note; I was criticized for using
-m limit --limit 2/minute -p tcp --dport 22
which was too perceived as too restrictive by some (or just one).
Well, if it is too restrictive can only be said by knowing your environment, but I certainly agree that it is a very restrictive setting to allow not more than 2 connections per minute no matter what their source is. If your system is accessible from the internet, it would allow a way too easy DOS, and I assume that is exactly the reason why you want to exchange the line ;-)
Quoted 7 lines
Anyway, I've looked closer into the manpage of dstlimit and I would be glad to exchange my lines with
-m dstlimit --dstlimit 2/minute --dstlimit-mode srcip-dstip -p tcp --dport 22
Comments please :)
This seems to do virtually the same, what the version with -m recent does, so I think it should do the trick for you, especially if you successfully used -m limit before and had no trouble with it. I have never used -m dstlimit before, but there are two comments that came into my mind:
If you are afraid of somebody trying to DOS you, the recent match with the added TTL check might be an even better choice.
Second is that while reading the manpage, I was a bit surprised when I saw the default of 10 seconds for --dstlimit-htable-expire. You should test if you don't have to increase that value to not end up with a rule that allows one connection attempt every 10 seconds.
When implemented correctly to allow state NEW packets at the given rate and DROP or REJECT them if they exceed their limits both recent and dstlimit should do well, so it's probably just a matter of taste then ;-) I can only say that I'm using the version with recent match like the "NetFilter MailList SSH_Brute_Force Chain version 1.0" Grant posted, and it works perfectly well for me. But I can't see a reason that dstlimit would not work, so maybe you just want to try and test it ;-)
HTH,
Marius
Quoted 2 lines · Marius Mertens
If you are afraid of somebody trying to DOS you, the recent match with the added TTL check might be an even better choice.
I've been wondering about recent's TTL check and its ability to prevent or even reduce DOSing.
To test if the TTL option is used, the attacker can send regular (nonspoofed) ssh requests until they start becoming unresponsive (call this number n), then try sending another request with a different TTL.
If that request returns, TTL match is being used so simply send n-1
requests with different TTLs and the spoofed address. Unless the route from the attacker to the sshd is longer than the route from the spoofed client IP to the sshd and the client uses a TTL of 255 (or something similarly high), the legitimate client will still be DOSed. Knowledge of the client system's OS or TCP stack is also reasonably easy to acquire, and can help narrow down the TTLs that need to be sent. Or the attacker can be lazy and send 250 or so SYN packets with different TTLs and the spoofed IP.
In short, if an attacker know's you're using recent to track ssh requests, and is not so clueless that he doesn't know about the TTL option to recent, you're dead whether or not you use the TTL option.
On a related note, I really hope ISPs are doing some egress filtering to prevent these packets with source IPs not on the expected subnet from getting out. I wonder how many do...
Quoted 6 lines · * curby . ([email protected]) wrote:
Quoted 2 lines · Marius Mertens
If you are afraid of somebody trying to DOS you, the recent match with the added TTL check might be an even better choice.
I've been wondering about recent's TTL check and its ability to prevent or even reduce DOSing.
recent's not going to prevent a DOS. It tries to avoid becoming the cause of a DOS but there's only so much it can do.
Quoted 8 lines
To test if the TTL option is used, the attacker can send regular (nonspoofed) ssh requests until they start becoming unresponsive (call this number n), then try sending another request with a different TTL.
If that request returns, TTL match is being used so simply send n-1
requests with different TTLs and the spoofed address. Unless the route from the attacker to the sshd is longer than the route from the spoofed client IP to the sshd and the client uses a TTL of 255 (or something similarly high), the legitimate client will still be DOSed.
Sure, but at least it's a little harder than just having to know the other client's IP address, that's kind of the point. There's also an opportunity here for someone to notice what's happening and take action about it. Alternatively, there could be additional checks to try and thwart this (such as checking for multiple packets with different TTLs from the same source IP and raising a big stink about it, or flipping the check around to require a specific TTL for a given IP, etc).
Quoted 8 lines
Knowledge of the client system's OS or TCP stack is also reasonably easy to acquire, and can help narrow down the TTLs that need to be sent. Or the attacker can be lazy and send 250 or so SYN packets with different TTLs and the spoofed IP.
In short, if an attacker know's you're using recent to track ssh requests, and is not so clueless that he doesn't know about the TTL option to recent, you're dead whether or not you use the TTL option.
ipt_recent is far from fool-proof and you're describing a known limitation of it, not anything new. The TTL option is just there to make it more difficult, it's by no means a solution to the potential problem. Generally I'd think finding the client IP is expected to be somewhat difficult by itself. There's also escape-routes to some extent, you can have a special rule which watches for packets matching something and removes blocks on that source address, perhaps indefinitely or for some specified time, or only when matching the same TTL as the key packet, etc.
Quoted 3 lines
On a related note, I really hope ISPs are doing some egress filtering to prevent these packets with source IPs not on the expected subnet from getting out. I wonder how many do...
Not nearly enough, unfortunately.
Stephen