149 points · by jgrahamc · 15y ago · 102 comments
Code re-use please!
apt-get install fail2ban
Works out of the box to prevent brute force attacks against ssh, and can also be configured for other services, like web authentication, POP, IMAP, etc.
I'm not going to be able to read out my private key over the phone. I guess this is only for password based authentication.
command="/sbin/reboot",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AAAAB3Nza...LiPk== user@example.net
See sshd(8) "AUTHORIZED_KEYS FILE FORMAT" for details.You should never be giving out private keys, people need to generate those themself!
A key alone, with no idea what it will unlock, is going to be a useless thing for anyone else.
Obviously I'm aware how crazy a public store of private keys sounds, but keys are really only useful if you know what lock they fit.
But like you said, if you posted the key alone anonymously, it will be hard to guess what lock it fits.
Note that it already happens that people put their private keys into pastebin, by accident or probably by sheer incompetence.
> if you posted the key alone anonymously, it will be hard to guess what lock it fits.
As an attacker, I would simply collect as many of those "published" private keys as I can get. Then, when attacking a bunch of systems, I'd simply try one key after another on each system.
The important difference to the "real world" is that an attacker can try lots of "locks" at once (i.e. can connect to multiple target systems at once).
It uses two mechanisms to protect my details:
- a secret key file (backed up on an usb stick and on dropbox). If you steal my keypass db you need this file as well
- a ridiculously long sentence that I can never forget for various reasons as a passphrase. It's plain text, so probably everything in there is in a dictionary, but - good luck trying to figure this out.
With this set up I'm relaxing now. I know that I ~cannot~ lose access or forget my passwords :)
I think this only works if you want to give credentials once to someone while still knowing when those credentials are being given, unfortunately :(
Sigh.
Inevitably, if you have such a buddy, all they can really say is "yup, I'm not the only user here", and you're back at square one.
A better investment of the time would be systematizing your configurations, so in this situation you can just rebuild a new machine and kill the old one (ideally, after figuring out how the password was compromised in the first place).
As the article says, he just wanted it shut down. Fixing it can come later, but stopping it from sitting on your network sending out millions of spam e-mails is still useful.
Seems like for a business, this would be a great service to have available from the likes of Linode, which pays people to be there in any event, rather than your 'buddy', who might go on vacation when you do.
Not perfect but can work well as a line of defence.
I did used to use Twilio until they dropped international text messaging etc.
<machine nickname> <passcode>
It is on my todo list to improve this at some point - but not had chance yet.
My message looks like this: <machine> <hashed passcode> <number>
The passcode is "hashed" with the number. When the server receives the message, it adds the number to a deny-list. That way, the number is only good for one use.
Also, the number has to conform to a certain pattern. I might change that to a pre-generated list of valid numbers, but for the moment, I can work out a valid number with pen and paper.
Not perfect, but I think it's good enough.
I'm going to ask a really audacious question: Why?
His systems all worked as expected. Injection attacks were thwarted and login attempts failed. He received (as it turns out, erroneous) notification about the login attempts and knows he needs to do something about it for the future.
Why is that something the sharing of credentials? Why is it that people still allow for remote root login? Why do people still allow user SSH access via password?
There's a better way, and it leaves you a damn sight better prepared for intrusion attempts than receiving SMS messages that, as he so perfectly demonstrated, were not actionable. Limit logons to PKI-only. Live happier. Sleep easier.
Turning the system off could potentially reduce your ability to audit where they originally got in. Anywhom.
SSH keys :)
I don't see anything wrong with having a human backup. Heck, what if you die and things need to be wound down?
One person simply can't cover a server 100%, adding a "buddy" doesn't help much. Either you need a full-on netops operation, or you need to be able to deal with not having 100%.
Maybe the alert goes to multiple buddies who have the tech skills to handle it ... maybe he could be their SSH Buddy in return? In other words, rather than a SSH-Buddy, have a pool of server sysadmin friends for emergencies.
ps. If the original poster is reading this, I would have posted this on your blog but I didn't have other account passwords to hand ... if your moderating all comments anyway, why not allow anonymous comments?
For every new connector, send some sort of code to your phone, and require it to be inputted back into the computer before any access is given whatsoever. Summarily disregard every single connection and command given without this code re-input.
You're even most of the way there: if you're sending messages to yourself, you might as well implement this behavior with it.
I have an agreement with many friends to be an operator so they can look something up on the web, or anything else requiring some efficient technical prowess.
We just call up each other and say "operator", and if you're near a computer, you help out with whatever it is.
Any particular reason you didn't want to use Snort or Bro?
You send the magic string to any tcp port and it'll instantly kill all SSH logins and disable the root shell. Send the string again and the process is reversed. Caveat: if your syslog contains trigger entries more than a year old this will blow up.
I also recommend you disable root logins and password authentication, but if you insist on enabling them, this may work for you. Modify as necessary.
iptables -t raw -A PREROUTING -p tcp -m string --algo bm --string "_-()ThisIsAReallyLongAndComplicatedRandomStringToMatchOn()-_" --from 0 --to lengthofthelongstring -j LOG --log-prefix "29CharacterMaxTriggerString "
#!/usr/bin/perl
# sentinel - disable root based on a syslog trigger
# Copyright (C) 2011 Peter Willis <peterwwillis@yahoo.com>
use strict;
use POSIX qw(mktime);
my $LOG = "/var/log/syslog";
my $LOCKED = 0;
my $TRIGGER = "29CharacterMaxTriggerString";
my %M = ( "jan"=>0, "feb"=>1, "mar"=>2, "apr"=>3, "may"=>4, "jun"=>5, "jul"=>6, "aug"=>7, "sep"=>8, "oct"=>9, "nov"=>10, "dec"=>11 );
for ( ;; ) {
sleep(1);
open(F, "<$LOG") || die "Error: $!";
for ( ;; ) {
sleep(1);
while ( <F> ) {
select(undef, undef, undef, 0.001);
#print STDERR "Reading \"$_\"\n";
if ( /^(\w+) (\d+) (\d+):(\d+):(\d+) \w+ kernel: $TRIGGER / ) {
my $time = time();
my $stamp = mktime($5, $4, $3, $2, $M{lc $1}, (localtime($time))[5]);
if ( $stamp > $^T ) {
#print STDERR "Found Trigger\n";
trigger();
} else {
#print STDERR "Error: found trigger but timestamp $stamp is before script begin time $^T\n";
}
}
}
}
close(F);
}
sub trigger {
if ( $LOCKED ) {
system("/usr/bin/chsh -s /bin/bash root");
$LOCKED=0;
} else {
my @procs = map { @_=split(/\s+/,$_); $_[1] } grep(/^root\s+.*sshd:/, `ps -aux 2>/dev/null`);
#print STDERR "Killing processes: @procs\n";
kill(15, @procs);
kill(9, @procs);
system("/usr/bin/chsh -s /bin/false root");
$LOCKED=1;
}
}I had to smirk when I clicked on complex password scheme.
Works like a charm!
Get a watchdog timer.
(Not that I'm arguing your point. Just taking all steps that you can think of to prevent something, doesn't mean you'll actually prevent it. Being prepared for a worst case is always a good idea.)
Our polo-necked messiah has given us iPhones that can even run an SSH client so how sort-of pointless to talk about "ssh buddies" and showing iPhone screenshots in the very same article? Since you were getting IDS alarms on your iPhone anyway, why didn't you SSH in and shutdown the machine yourself?
And on the root login bit: you would only need a sort-of dummy account for you logging in or for your "ssh buddy", if you love that idea. And all they need to be able to run is "sudo shutdown".
Get a mobile ssh client, set it and your sshd up with RSA keys, setup port forwarding on your home router on a non-standard port, disable Root login and move along.
PermitRootLogin without-password
That way root must use a public key to login, but other people can still use passwords.
(But watch out for sudo.)
What's bad about password auth if your password is strong enough?