Ubuntu – SSH broken in for user with no shell

authenticationSecurityssh

I think that my Ubuntu box was broken in and the hacker tried to spam email (through port 25) with my computer.

Part of /var/log/auth.log is extracted as below (I changed the actual user into myuser and some external IP address into 1.2.3.x):

Feb 20 06:07:12 ubuntu systemd-logind[954]: New session 77 of user myuser.
Feb 20 06:08:22 ubuntu sshd[21251]: error: connect_to 1.2.3.4 port 25: failed.
Feb 20 06:08:22 ubuntu sshd[21251]: error: connect_to 1.2.3.5 port 25: failed.
Feb 20 06:08:22 ubuntu sshd[21251]: error: connect_to 1.2.3.5 port 25: failed.
Feb 20 06:08:22 ubuntu sshd[21251]: error: connect_to 1.2.3.6 port 25: failed.
...(thousands of similar lines follows)...

Since the error message was generated by sshd, I suppose that the hacker gained access through logging in SSH as myuser. Its respective record in /etc/passwd is as below:

myuser:x:1003:1004:myuser:/home/myuser:/bin/false

I suppose the hacker should not have been able to log in as the shell for myuser is /bin/false, i.e. it should be kicked out immediately even if he can log in. I cannot login myuser through PuTTY as well. The login record for my own login is something like:

Feb 22 10:26:58 ubuntu sshd[3653]: pam_unix(sshd:session): session opened for user myuser by (uid=0)
Feb 22 10:26:58 ubuntu systemd-logind[734]: New session 2 of user myuser.
Feb 22 10:26:58 ubuntu sshd[3653]: pam_unix(sshd:session): session closed for user myuser

So, obviously there is something wrong with my settings, and/or /bin/false is not working in /etc/passwd file. What's wrong with my current setting, and how should I fix the loophole?

Best Answer

This article in commandline.ninja about /bin/false, /sbin/nologin and SSH seems to answer your question.

Executive Summary

A hacker can bypass the protection of using /bin/false as the shell for that user using port forwarding via the ssh command like:

[user@panel~] ssh -N testacct@linuxserver.cconn.info -L 2525:127.0.0.1:25

Temp fixes

Disable port forwarding

The simplest and most drastic, if possible in your scenario, is to simply disable TCP port forwarding in your sshd configuration file (/etc/ssh/sshd_config on many systems):

AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no

Block user account/password

  • Edit /etc/shadow and set the user’s password to be *
  • Confirm that no SSH key for this user exists in your system.

Shadow password having * as the encrypted password in /etc/shadow means that the account is locked, the user will be unable to log-in via password authentication but other methods (e.g. SSH key) may be still allowed.


More info can be found at commandline.ninja about /bin/false, /sbin/nologin and SSH.