64 points · by lisper · 11y ago · 59 comments
1. Disable password authentication altogether
2. Create and use keypairs
3. Add an "AllowUsers foo" line to /etc/ssh/sshd_config so that only your user is allowed to SSH
4. Install sshguard or fail2ban and set the ban limit to a week
There are other things you can do, but I find this to be the lowest hanging fruit for the best security.
* Arch Linux (openssh-6.9p1-1): #PermitRootLogin no
* CentOS 7 (openssh-server 6.6.1p1-12.el7_1): #PermitRootLogin yes
* Debian 8.1 (openssh-server 1:6.7p1-5): PermitRootLogin without-password
* Fedora 22 (openssh-server 6.9p1-2.fc22): #PermitRootLogin yes
* openSUSE 13.2 (openssh 6.6p1-5.1.3): #PermitRootLogin yes
* Ubuntu 14.04.2 (openssh-server 1:6.6p1-2ubuntu1): PermitRootLogin without-password
* Ubuntu 15.04 (openssh-server 1:6.7p1-5ubuntu1): PermitRootLogin without-passwordDuring the years I've found that changing the port to something like 7865 decreases login attempts up to 100%. Personally it's always the first thing I do along with changing LoginGraceTime to 5s
You probably also want to set kex, ciphers and MAC preferences in your client config for when you connect to servers with suboptimal settings.
Personally, I have also moved away from RSA keys and prefer using Ed25519 ones instead.
I ask because I administer about 300 servers with DenyHost grandfathered on the servers. I've googled a bit to see the relative merits and DH is just for ssh whereas fail2ban is more general.
I somehow didn't know about sshguard - it looks interesting (and also seems to do more than just ssh brute force blocking).
So... do I need to take DH off the herd and replace?
Mozilla's wiki has some information on strengthening the security of your OpenSSH setup.
I'm not saying that port knocking doesn't have uses, just that it doesn't in this case. Unless you're trying to hide the service I guess, but I don't see why that would be necessary.
Now I do wonder: does this 'exploit' still cause a log line immediately after each attempt? And also, do iptables firewall rules also apply to open connections, or just to new ones? In the latter case, Sshguard and fail2ban are circumvented for a rather high number of attempts.
One of the common gotchas though is that if you add a rule to allow "Established" connections, and then try to add a new rule after that which says "block IP X", that will not sever existing connections.
To block existing connections you can remove that entry from the connection tracking table, or you can insert your "block IP X" rule above your "allow Established connections rule".
In the case of cracking, you'd just want to apply a blanket blan: completely black-hole any packet whatsoever coming from that IP to your box. That will freeze all existing connections.
To ban new connections, you'd have a rule that applies to TCP connections in the NEW state, leaving the ESTABLISHED ones alone.
Port knocking has been mentioned before on HN and it generally gets a luke warm to negative response. It completely baffles me, this response.
Any service, no matter how locked down and properly configured, still presents an attack surface. SSHD is a program like any other, and it can have bugs.
When you use port knocking with a properly configured SSHD, you are much more secure because that attack surface has been removed.
Who cares about a few connections per hour if the sun will die before they brute force your private key?
Also, with SSH, I use keys, not passwords. I encourage others to do that as well. You could have this patch on a honeypot and by using a SSH key, your account password would not be in play.
It is just the kind of thing I would expect dumb IT mgmt to require so they can ban swear words from passwords.
Edit: Thinking about it, I guess that blocking single IPv6 addresses, as sshguard does, doesn't really help at all, as it's easy to obtain huge subnets. (This might even open the door to an iptables DDoS?) Blocking whole subnets, on the other hand, doesn't sound clever either. (Who knows how many users are sharing the same subnet?)
So, should I just close port 22 on IPv6 altogether? Not a very sustainable solution. This needs more research!
I imagine banning /64 networks is completely harmless, but I'm not really confident on that. I'd be wary of banning anything bigger, but getting a bigger address range is very cheap, so that may be required.
I've recently written a banning layer over my email server, in case somebody tries to guess the computer's passwords. It's a certainty that an attack exploiting IPv6 will go through, so I'm interested on the subject. By the other side, no attack it received up to now cared enough to read the EHLO reply and see that the server does not support login authentication, so I'm not that concerned, I guess I'll fully migrate to public key before any brute-force attempt is successful.
Far too many people ban themselves with fail2ban.
Oh, and a successful login will clear the record: the software scrubs all records of that IP from its cache, so then if you make new logins from the same IP with mistyped passwords, you're starting with a clean slate.
I've never locked myself out.
Especially since any sane bruteforcers iterate over IP addresses instead of passwords to avoid just this.
Once you have that though, fail2ban does do a nice job of making the perpetual brute force attacks significantly less obnoxious. I've seen machines effectively DDoSed by brute-force ssh attempts, either with the network overloaded or filling up disk with ssh's logs of the failed attempts. Not as bad as getting hacked, but still annoying. A serious botnet attack will be able to spread it over thousands of IP addresses, but IME, those are relatively rare. Most attacks are still coming from a handful of hosts at a time and fail2ban basically stops them dead in their tracks.
The other useful thing to do with fail2ban is to use it along with a whitelisted range of networks. Even if you aren't on a private network, not every server needs ssh open to the entire world. You can often get away with whitelisting the local network and maybe a few ISPs' ranges where admins are likely to access it from. I run fail2ban even on machines on private networks. If one of the other machines gets hacked, one of the first things they'll usually do is start trying to brute-force other machines on the network. Since it will probably only be a couple compromised machines, fail2ban is useful here.
I find that it does also give me useful data as well. Eg, I have simple graphite metrics based on the number of bans/minute. If it gets unusually high, I can get an alert and perhaps notice that other hosts have been compromised, or do a quick firewall rule or two to drop packets from an entire network for a while.
So it's like 15 bcrypt rounds on average hardware for ~500ms per hash?
Linux seems to use a pathetic 5000 rounds of SHA-512: http://www.akkadia.org/drepper/SHA-crypt.txt