SSHGuard
sshguard.net
sshguard.net
These tools are popular, but I think they're kind of silly.
Later
I read some comments below, which compared this to Fail2ban (I had written this tool off as yet another Fail2ban-style script). But it's even scarier: it's written in C. I guess this is from like 2010? Don't run stuff like this.
Well, even when accepting only certificates some brute force bots are dumb enough to keep trying to connect to try passwords, and this can give a lot of unnecessary CPU load due to all the initial connection crypto handshakes.
Though I have to say most of the bots these days are smart enough to quit as soon as password auth is turned off. But 10 years ago most were really dumb. Using fail2ban was more of a DDOS prevention than a security thing.
> Using fail2ban was more of a DDOS prevention than a security thing.
But it was a SPARC server with 1 core at a few hundred MHz and no crypto acceleration. As you can tell this is a while ago :) So yeah I guess these days it's much less of an issue. But the number of possible attempts will have increased too of course.
For the past 30 days, I've seen a combined total of 86k SSH attempts. I've not bothered to filter out my own legitimate sessions, given that they're in the ballpark of ~3 connections a day tops.
Total load on each of these systems has stayed below 0.5, and that's including the other actual work the servers do. I don't have a great way to pull out sshd specifically, but it's safe to say that these servers haven't had their operations affected at all by the nefarious attempts.
For what it's worth, the one thing I'm doing that could potentially impact the resource consumption is hardcoding cipher/keyexchange/MAC selections. Given that I control 100% of the legitimate clients for these SSH daemons, I've hardcoded the settings as follows:
Ciphers chacha20-poly1305@openssh.com
KexAlgorithms curve25519-sha256@libssh.org
MACs hmac-sha2-512-etm@openssh.com
HostKey /etc/ssh/ssh_host_ed25519_key
Over 95% of the spam connection attempts get dropped before my server ever does any crypto, because the connecting system doesn't support that KeyExchange choice.
If it's using systemd, try
systemctl status sshd
If CPU accounting is turned on, you'll see how much CPU time was used by sshd, plus when it was last restarted, which at least gives you something to work with.If not, you can enable it with:
systemctl edit sshd
then adding: [Service]
CPUAccounting=yes
and restarting the daemon.If someone wants to go to the trouble to break crypto, it would be cheaper to infiltrate the ssh project and implement back doors after a few years of building trust. If that’s your threat model, double wrap your protocols (WireGuard jumphost > ssh type stuff)
My only point being that there are some great use cases for fail2ban, and while this isn’t one of them it’s important to remember that this does not make fail2ban a misdirected or useless security initiative by default, in case anyone gets that impression from reading these comments.
If brute forcing anything is a concern then addressing that concern should come first, focusing effort on fail2ban is a cost that is probably better spent elsewhere.
Now the issue of logs is real. I tried to configure debian to remove noise from the logs and I couldn't. May be someone with more knowledge would suggest how to do so. Because I'd like to see successful authentications in the logs and only those. So that could be a valid reason to change port, etc. But that's only because logs are lacking proper configurability.
Not very elegant (as in, not with built-in tools), but using Logstash would be one way of achieving that. There probably is a better (simpler) way, though.
https://www.elastic.co/guide/en/logstash/current/pipeline.ht...
Edit: After some research, rsyslog seems like a more appropriate candidate. But the docs don't seem too novice-friendly and the configuration is quite obscure. Tradeoffs.
However, I've worked at a lot of startups, and no matter how much you try to discipline people, someone thinks their circumstance is special (and it somehow requires a password-based login), or someone is just lazy and sets a password for unfathomable reasons. So, for those people, I run fail2ban to allow it to act as a rate-limiter. But tptacek is right, in any sane situation, you don't really need it. But sometimes the situation isn't sane.
> just disable password authentication and that's about it.
a.) for what ever reason, I've had third-parties insist on passwords. (In healthcare¹, too. Ugh.) b.) For quite a while, Azure's "AADLogin" used keyboard-interactive, which meant that you couldn't disable it. Thankfully, that got fixed in a recent version.
¹In addition to fail2ban, we also did firewalls at the cloud level. The password was a good (secure) one, but I'd sleep easier with a key-based login.
You'll see the same thing in financial software for basically the same reasons.
Sounds like you understand at least one reason people use stuff like this (including, most prominently, fail2ban). Rate limiting and banning IPs trying to brute force and/or probe open ports (not just SSH) goes a long way to cleaning up your logs, your real time state, as well as other things, like frustrating the attacker, not wasting connections on them.
Having said that, this particular tool looks a bit shit ...
You don't need fail2ban to rate-limit SSH.
RFTM ... MaxStartups, PerSourceMaxStartups, PerSourceNetBlockSize.
> trying to brute force
AuthenticationMethods publickey
PasswordAuthentication no
ChallengeResponseAuthentication no
PermitRootLogin noWe know about the SSH daemon config. You still get all sorts of probing that isn't a brute force on SSH.
https://github.com/fail2ban/fail2ban/tree/master/config/filt...
But, it's nonetheless a fact that a (temporary) nft drop rule in the kernel is much more effective than traffic hitting a user space daemon like SSH.
Some NICs even allow offloading that (e.g., in form of a bpf program) to the card itself, so that such traffic won't ever hit the system (CPU) at all.
So yes, SSH can be made secure and rate-limited without anything else, but that won't be the most efficient solution; now fail2ban/sshguard/... ain't no free lunch either, so one needs to weight the added (IMO small, but still) complexity against benefits in a specific use case.
The server is reinstalling the OS right now...
Still, if you cannot explain how that second line got in there, it is probably prudent to reinstall (perhaps even reflash BIOS).
Do you think using key-based auth and disabling password authentication is enough? or are there other things you would recommend?
OT - nice Fibonacci in your profile :D
If there’s something better please correct me!
I find it interesting that one of the suggestions is to disable the ECDSA host key. I thought ECDSA was one of the "good" algorithms?
Here's the long answer: https://blog.cr.yp.to/20140323-ecdsa.html
Ed2519 has the virtue of both a better curve and a better signing algorithm, which is why it's used in preference to ECDSA. If you're using 25519, don't bother keeping the rest of the algorithms enabled.
I think these hardening guides are mostly pretty silly too, for whatever that's worth. The important thing is making sure nobody can log into anything with a password. The rest is just liturgical.
Take a look at the "concerns" section of the wiki page. https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signatu...
For sane default, you can generate your working but sane ssh_config and sshd_config based on my inner code knowledge and network security and as an IDS/IPS architect.
Each and every known OpenSSL (v8.4; v9.0 is underway) settings in the config files have annotations and details and many have additional links detailing why. I also note where each and every config settings are found in the source code by nesting, what protocol state, control state, authentication stage, and lockings (makes code review so much easier for me) also in its comment section of each config setting.
Even has a bash script to let you create these config files (defaults to your subdirectory for pretesting, but can also as an option update /etc/ssh, which I confidently do) into something that would pass an SSH audit for ssh-audit (I’m a contributor), CISecurity and often better.
Also these scripts generates a script containing proper file permission settings based on top generic Linux File System variants (basic variants like APT, pacman, Portage, DNF/RPM) ).
I also offer SSH bastion setup (adjacent to my URL given below) as well.
I also enjoy using certificate-by-user as an authentication mechanism for maximum ease of sysadmin use when it comes to emergency mass-blocking or occasional employee departures.
https://github.com/egberts/easy-admin/tree/b74765baa450593be...
Why are you scared of C? Also, should I remove sshguard? I dont want my server going down.
Btw, it has been updated after 2010. Are you talking about 2010 release ?
Why would being written in C make it scarier?
No programming language can stop a determined developer from creating a vulnerability, but some languages make the task easier than others.
(Originally…)
I'm a bit curious; it also seems to imply that it logs the fd number, and then blocklistd queries the kernel to obtain the remote's address. That seems like a race condition:
1. blocklist call is made
2. daemon closes the socket (connection has failed, after all)
3. next client connects
4. lookup is made by blocklistd, and the wrong peer is potentially incriminated
(Actually, perhaps it's sending the fd over the socket. I forget you can do that, and the README is hard for me to follow. The "like syslog" also implied that it's logging over syslog to me, but that's not what the code does.)That's pretty much the reason why I started to write my own guard tools.
I don't need people from China or Russia logging in to my server. I don't understand why tools like this are never built in an ASN aware manner, blocking mechanisms would be so much easier.
[1] https://research.securitum.com/fail2ban-remote-code-executio...
if (notMe)
panic();
Though I agree, security is a mindset not a tool you can simply download or run.At my last gig, I had thousands of domains and hundreds of servers running on every major (and a shitload of minor) service provider. I custom scripted all these servers to compile all the IP's that attempted a connection, send those lists back to HQ, and then distribute back to each node a block-list table that I could dynamically apply to each edge device. One compromised computer even trying to login was completely blocked from 5000+ domains within 15 minutes.
I'm not worried about my ssh login being brute-forced, but if your computer is compromised I want nothing to do with you.
edit: That isn't to say they should be logging every packet... But if I work for XYZ Hosting Company and spin up a new VM, hand you the IP, and you turn the IP into a mini-shodan scanner... Is the hosting company at least a little responsible for what happens next?
While its similar to sshguard or blocklistd, I didn't know they existed when badips died a couple years back. https://www.nubi-network.com/faq.php
The list file should work for either ingress or egress (or both).
I wanted to post about it on here some time ago, but I'm kindof bad at advertising.
But it's even scarier: it's written in C.
I don't want to defend this tool, but OpenSSH is written in C as well :).Really? And how do you block someone who slowly try's (every 2-4 minutes) to bruteforce your smtp? Yes your openSSH has some slowdown mechanisms builtin, but your Imap-Server? Smtp-Server? Your openTTD-Server? However i think using the logs is the wrong approach hence my use of blacklistd* whenever possible.
* https://www.freebsd.org/cgi/man.cgi?query=blacklistd&sektion...
Even obscurity has its place (e.g. using a different SSH port)
If you only use SSH keys, no brute force attack will work anyway.
https://www.systutorials.com/how-to-use-iptables-to-limit-ra... https://making.pusher.com/per-ip-rate-limiting-with-iptables... https://stackoverflow.com/questions/18616246/iptables-limit-... https://blog.oshim.net/2009/06/iptables-to-rate-limit-incomi... https://stackoverflow.com/questions/47775809/iptables-rate-l... http://blog.serverbuddies.com/using-iptables-to-rate-limit-i...
Limiting Internet-facing SSH connections to key-only is definitely a good idea, though.
ServerAliveInterval 60
ControlMaster auto
ControlPath ~/.ssh/conn/%r@%h:%p
ControlPersist 1h
I use it to speed up new SSH connections after the first one is made, but it should also prevent the issue you've described.And, please don't use passwords, instead use certificates. You can easily have system to create and use a short-term valid login certificates. Also, don't forget to authenticate your hosts to users with host certificates.
All of the above is needed even if it is behind wireguard. That's defense in depth. Ensure there is no shared-fate between the two authentication systems.
Also, consider multi-factor personal authentication – biometric+external auth (FIDO2 device with fingerprint auth) and device-auth (device-secure-enclave bound certificates) where device is unlocked by a password/pin from your memory.
If you do these, you don't need silly hacks like SSHGuard.
Defense-in-depth is a good idea, but has its limits. I don’t think there’s too much value to layering SSH over Wireguard.
Agreed on SSHGuard type things not being necessary (for authentication protocol). SSH is not special. People can attempt to bruteforce Wireguard too. In fact, the only difference seems to be SSH making the logs (more easily) accessible.
The main downside to SSH is that it allows in default configuration to use insecure passwords for authentication (but this can be disabled). Wireguard is strictly better since passwords are never an option.
Protocol negotiation is another downside, but I think it’s a smaller one. The SSH devs are fairly good about removing insecure schemes from the defaults.
Future in-the-wild vulnerabilities in the SSH protocol layer?
I think that layering may be useful given a certain threat model, but you shouldn’t just layer protocols without actually considering the probability of vulnerability and considering the other tradeoffs you are making. Otherwise the only correct answer would be to layer SSH over every independent VPN protocol.
Considering the security track record of SSH, I don’t see the value to layering, when on the other side, you now need to manage multiple keys and an entirely separate protocol that isn’t as ubiquitous (yet). The time spent doing this might be better spent building robust and testable auto-update infrastructure or architecting the applications on the server to reduce the risk of contagion given an RCE vulnerability.
Reason to put wireguard as the first line of defense before ssh is because of wireguard's simplicity and robustness vs the track record of ssh implementations.
Why is the tradeoff of adding Wireguard worth it? Sure it’s extra security, but is it really worthy managing multiple keys, not-as-ubiquitous protocol, less flexible administration and no CA? I don’t think there is a correct answer (it depends on the threat model), but your original comment made it seem like there was no tradeoff and there was an obvious correct answer.
https://www.sshguard.net/docs.html
The setting up link points at https://web.archive.org/web/20180901061425/https://www.sshgu... and the original is 404: https://www.sshguard.net/docs/setup/
Mentioned in other comments, protecting from brute force isn't really the big benefit. The benefit is reducing noise in logs so that you can see more serious activity.
https://www.daemonology.net/blog/2012-08-30-protecting-sshd-...
Perhaps I'm overlooking something?
Do non-technical users know how to operate a secure element? Is that an encrypted home drive on a laptop protected by a weak password?
I appreciate your input, but I still think 2FA is the most secure and usable method for non-technical people. Of course, no SMS (at least outside the US).
This completely silenced our logs.
It's time another prevention system may save your day.
The only help against that scenario is a true multi factor setup, or maybe a hardware-based security token that needs a second factor (fingerprint or PIN) to unlock.
SSH also leaks the keys it'll accept, it doesn't use a zero knowledge proof to first determine if the client and the server have mutually acceptable keys.
Couldn't find anything about Exim or Postfix integrated brute force prevention on SMTP connections...
I use crowdsec also nowadays, but I don't trust it like I trust fail2ban. Due to opaque answers from the engine on how it has protected my hosts. Considering going all the way Back to fail2ban again.
The main issue is that on my hosts where fail2ban are running, I see week on week activity and banned hosts.
When I look into cscli decisions or cscli metrics, it gives me the indication that nothing is happening which I don't believe is true. Maybe it is doing the work it promises, but I can't easily see it.
This could be a false negative, and that there genuinely is less malicious connection attempts. The busy fail2ban are bastion hosts on AWS, while the others are hosted on DigitalOcean. For me as a user though, I wish there was a way to see historically blocked hosts. The last time I looked half a year ago, this was not available in CrowdSec.
Tools like these are essential if you have Internet-facing SSH services.
If you do not need Internet-facing SSH, do not enable it. It can be an attack vector and there are hundreds of bots that will try to brute-force access within minutes of opening Internet-facing SSH daemon.
Best practices for Internet-facing SSH:
- run on non-standard port (not port 22)
- disable passwords, use SSH passkeys instead: https://www.techrepublic.com/article/how-to-setup-ssh-key-au...
- disable root SSH login
- run fail2ban, sshguard, or similar "block IP addresses for suspicious activity" services
- setup port knocking: https://www.tecmint.com/port-knocking-to-secure-ssh/
edit:
- run WireGuard VPN (https://en.wikipedia.org/wiki/WireGuard) for defense-in-depth
SSHGuard however is much lighter and even less to configure manually.
I used fail2ban in the past and it was helpful.
If I was currently running an Internet-facing SSH daemon, I would give SSHGuard a try.
Nice! Wireguard was not around when I was using Internet-facing SSH in a previous job.
Adding it to the recommendations.
It is also fairly easy to DoS SSH by having too many connections in the authentication state leaving no slots open, which SSHGuard is useful to counter.
Apart from that, SSH's intrinsic security is the same, but if you have password authentication enabled, you are only as strong as the weakest password.
A number of configurations also ship with ssh root login enabled by default. For example if you setup a new Linode VPS. You need to add a user, remember to at least turn off root access and probably also password access. I get why they're doing it because it easier, but yeah ... not a huge fan of this configuration.
Edit: deleted false information about TLS.
And honestly some of the issues that were there are not something preventable by limiting oneself to even 50 LOC. I don’t want to speculate how they came to be though. It’s really baffling.
Here's a good summary:
https://arstechnica.com/gadgets/2021/03/buffer-overruns-lice...
Contrast this with how easily and well Wireguard was integrated into OpenBSD.
Wireguard is just a very simple network bridge. Whoever has the key can send anything over the network. There isn't a robust mechanism required to keep the key, to revoke it, to audit its use, to enable it to only provide access to specific applications. It doesn't have 1/20th the features SSH has, and SSH itself lacks a bunch of security features.
Bottom line is that you should only use Wireguard if you need a simple encrypted network bridge. It does not remove the need for other more complex security products.
Cars and bikes are both fine for point to point travel. They'll each give you a vastly different experience though. It"s prolly better to stick with what you know how to use, in my opinion. You're likely gonna have a bad day if you crash and die.
It's really not. It's a technical question with many technical answers, thankfully they were provided by other commenters before you posted this.
If anything, it's similar to asking if a bike is faster than a car, to which you would reply than a bike might be faster in traffic because of small size but slower over long distances because of propulsion. It is possible to compare apples and oranges over specific axes.
Using telnet makes me feel a little dirty and a lot alive. Using it over wireguard seems like taking a 60s muscle car and adding brakes that actually work before a track day; probably not a bad idea.
Basically security comes with layers of obscurity. Even key based auth is a random string that no one is meant to guess.
If you have more obscurity, the less chance one reaches to the actual attack surface.
If an attacker gains a key, without fail2ban etc, he may try bunch of user names against the key file consecutively but if a login attempt is limited to 3 times every 5 minutes, see it's better than nothing.
why not ufw ? Isn't it widely used?