APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Security Paranoia - restricting sshaccess


Security Paranoia - restricting ssh access



I had email from someone today whose system was hacked, apparently by a dictionary attack over ssh. They overwrote most of his hard drive with "DIE SCO DIE". He said there were "over 8000 root login attempts and another 6000 for various other names".

Several things come to mind immediately. First, you shouldn't allow root logins over ssh anyway. The



#PermitRootLogin yes
 

in /etc/ssh/sshd_config should be uncommented and changed to no. Second, you should have an AllowUsers line, and if possible, limit that to names that aren't obvious. For example, I wouldn't use "tony" or "apl". I might use "mghtrey" (though I don't), because that's a completely made up name. With that line, only the listed users are allowed to login with ssh at all. That includes root even if PermitRootLogin isn't turned off, but I set both anyway. Exclusionary duplication never hurts.

Next, don't have passwords susceptible to dictionary attack. Letters, numbers, and punctuation are a must. Do NOT use the same passwords on multiple servers and do not allow user equivalency or ssh_agent unless you absolutely have to have it.

Finally, don't let someone have 8,000 chances at guessing your password. I have two limitations set. One is the

MaxStartups 10:30:60
 

which (from the man page):

             Specifies the maximum number of concurrent unauthenticated con-
             nections to the sshd daemon.  Additional connections will be
             dropped until authentication succeeds or the LoginGraceTime
             expires for a connection.  The default is 10.

             Alternatively, random early drop can be enabled by specifying the
             three colon separated values ``start:rate:full'' (e.g.,
             "10:30:60").  sshd will refuse connection attempts with a proba-
             bility of ``rate/100'' (30%) if there are currently ``start''
             (10) unauthenticated connections.  The probability increases lin-
             early and all connection attempts are refused if the number of
             unauthenticated connections reaches ``full'' (60).
 

But I don't stop there on my most critical servers. I also have

auth required /lib/security/$ISA/pam_tally.so no_magic_root onerr=fail
account    required      /lib/security/$ISA/pam_tally.so onerr=fail
file=/var/log/faillog deny=1 no_magic_root even_deny_root_account
 

in my /etc/pam.d/system-auth file. That kills login if you type a bad password just twice. I reset it with a cron job every hour during the day when I'm working in case I screw up twice (" /sbin/pam_tally --reset") but not at night.

So, for someone to break in with ssh, they first have to know the one account that allows ssh at all and they have exactly two chances to guess the password - miss those two and pam locks them out. Keep on trying, and ssh itself will also lock you out eventually.

You really cannot be too careful now. If you don't need ssh outside of working hours, turn it off with a cron job. The "bad guys" are constantly hammering at our servers; there's no reason to make it easy for them.




If this page was useful to you, please help others find it:  





6 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!

---October 26, 2004

Thanks for the last part of the article. I had already done the first few things, but locking them out after two bad password attempts is a great idea. My logs are filled with ssh hacking on a daily basis now. Mostly, someone is trying to login as root, which I do not allow anyway, and now I see attempts for other usernames. I just recently changed my regular username password to something much harder than I had before. Also, the IP addresses from the host that is doing all this hacking, is never resolvable with the 'host' command. I still blacklist their IP in my firewall, but I am wondering if there is some IP spoofing going on as well.

Thanks again for the good ideas, Tony!

- Bruce Garlock

---October 26, 2004

I did forget to mention the final step - blacklist 'em after you see them in the logs. That step can even be automated, though between spoofing and constantly changing addresses, I don't do that until I've seen them more than once.

--TonyLawrence



---October 26, 2004

I seem to have the same jerk, using the same login names, from different IP addresses. What is the easiest way to play bad guy, and spoof your IP address, so I can test my defenses? Is there a way I can spoof my IP, and try to crack my own machine to make sure everything is working? I guess I need some direction on being the bad guy, to make sure the defenses I am putting up, are working. What about running ssh through xinetd, and using tcpwrappers, and sending a banner with a warning? Can that be done?

- Bruce Garlock


---October 26, 2004


Looks like I found the answer to my question about tcp wrappers here:

http://www.redhat.com/docs/manuals/enterprise/RHEL-3-Manual/security-guide/ch-server.html

But, how would one go about being a bad guy, and spoofing my IP address to connect to the ssh port on my machine?

- Bruce Garlock

---October 26, 2004

See http://www.iss.net/security_center/advice/Underground/Hacking/Methods/Technical/Spoofing/default.htm

--TonyLawrence


---October 26, 2004

If sshd is compiled with LIBWRAP, you don't need tcpd - it will check /etc/hosts.allow itself

http://www.snailbook.com/faq/libwrap.auto.html


--TonyLawrence



---October 27, 2004

I have a feeling in my searching for how to hack my own system by spoofing my IP address, that I will be brought to places I never knew or thought would exist. Although I have always taken the preventative stance when it comes to security, and done a little hacking to make sure things are safe, I never fully used the tools of the crackers. I am sure there are some c files out there to help with the process, and I will probably be inventing a few of my own. Usually running nmap on a new host, and setting passwords, and access permissions properly would be my first line of defense, but I have never taken on the role of the bad guy. In high school, I always found the holes into our schools system, and reported them. Although I think my server is as good as I can secure it, it will never be secure enough. I always keep binaries patched, and deploy patches as fast as I can after a security announcment is made, but I have never tried to brute force crack on any of my services. Since this is apparantly what the clown who is trying to break into my system is doing, I figured I would look at my system as he does, to see what he can see. I honestly doubt I will get anywhere, since I use special characters, numbers, and letters in my password, and that is the only account allowed to log in remotely. This should be interesting :-)

- Bruce Garlock

---October 27, 2004

Why not disable password authentication completely? It's the weakest form of authentication that OpenSSH supports.

You disable password authetication, set the allowed hosts if you want, and depend on a PubkeyAuthentication method that is supported by version 2 of the SSH protocol.

That way even if a person figured out your password they would still not be able to login thru ssh. You simply cary around your private half of the keypair on a usb keychain device and use the passphrase option. That way even if they get your private key, they still need to get your passphrase, which can be different from your normal login password.

--Drag




---January 13, 2005

Thanks! This article has been extremely helpful. I get hack attempts nightly and put all your tips in place and it works like a charm.
Jason Russo



---January 27, 2005

Very useful, just what I was hunting for. Also an option for some folks that may be helpful (although with creatures like nmap, not against anyone with more than trivial knowledge) is changing the port number with the 'Port ##' line option. Thanks for the useful info!
Landis Vinchattle





Thu Feb 24 11:14:37 2005: 63   TonyLawrence

gravatar
One thing I forgot to mention:


When the user gets locked out like this, attempts to login with the wrong password still get logged. If they subsequently give the RIGHT password, that doesn't get logged , but they still don't get in.



Wed Jun 8 20:52:59 2005: 634   anonymous


DenyHosts is a nice little script that reads through your authentication logs and adds attackers to your /etc/hosts.deny file. Very nice, simple, effective.







Sat Jul 22 08:13:36 2006: 2276   JimKeller


If you are the only one (or just about the only one) logging into your servers via SSH, it might be good to make MaxStartups even more restrictive than the default. MaxStartups does not limit the number of SSH connections you can have open, just the number of "concurrent unauthenticated connections". As such, if you expect very few simultaneous SSH connections, you might want to do something like MaxStartups 3:70:20 instead. Essentially you can set the first number to be the number of people you expect to ever be trying to connect at once. The only downside of making this directive more restrictive is that valid login attempts will be temporarily rejected during an attack if there are "limit" (the last number) connections pending. However, since MaxStartups only applies to unauthenticated connections, they will close very quickly and you should be able to get in without much of a wait.



Sat Jul 22 09:28:07 2006: 2277   TonyLawrence

gravatar
Good tip, Jim, thanks!



Thu Aug 17 21:36:53 2006: 2407   anonymous


as mentioned use public key authentication. the absolute worst thing you can do is allow someone to DoS you out of your own box, its fine if its a home system; if its colocated you will cry.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Blog
       - SSH
       - Security















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)