Re: Password Guessing

From: Martin Paul (map_at_par.univie.ac.at)
Date: 08/19/05

  • Next message: Per Hedeland: "Re: PAM changing user name"
    Date: 19 Aug 2005 12:26:26 GMT
    
    

    Geoff Dolman <geoff.dolman@cimr.cam.ac.uk> wrote:
    > I have an ssh gateway linux-box which is the victim of daily visits by
    > password guessing attacks typically against root, nobody or other
    > system-account type names like operator, oracle, guest, etc.
    > Other guesses are clearly dictionary attacks: lion, lynx, monkey etc.

    Jumping in a little late on this thread, but here's a simple solution
    to lock out hosts after a certain amount of ssh accesses in a given
    time (3 in one minute works for me).

    All you need is an ssh daemon that's linked against tcp-wrappers (default
    with Sun's SSH, optional with OpenSSH), and a simple shell script (see
    below).

    In /etc/hosts.deny you need:

      # ssh-throttle
      sshd: ALL: spawn (/usr/sbin/ssh-throttle %a): ALLOW

    Like this, on any ssh connection the script ssh-throttle will be called,
    supplied with the IP address of the source host, and the connection
    will be allowed. ssh-throttle keeps track of the connections, and
    adds a DENY rule right after "# ssh-throttle" to /etc/hosts.deny if
    a limit of connections has been exceeded.

    You can add default ALLOW rules for friendly hosts or networks above
    the "# ssh-throttle", so those connections will never be throttled.

    Here's the ssh-throttle script:

      #!/bin/sh

      clog="/var/run/ssh-throttle"
      clogt="/tmp/ssh-throttle.$$"
      deny="/etc/hosts.deny"
      tdeny="/tmp/hosts.deny.$$"
      limit=3

      hh=`/usr/bin/date +%H`
      mm=`/usr/bin/date +%M`
      ip=$1

      # Log connection
      echo "$hh $mm $ip" >> $clog

      # See if there were more than $limit connections from $ip in one minute
      count=`grep "$hh $mm $ip" $clog | wc -l`
      if test $count -gt $limit
      then
        # Check if the IP address is already listed in hosts.deny
        #
        exist=`grep "$ip" $deny`
        if test "$exist" = ""
        then
          # Log a warning, and add an entry to hosts.deny
          #
          logger -p auth.warn -t ssh-throttle "Denying $ip ($hh:$mm)"
          cat $deny | sed "/# ssh-throttle/a\\
    sshd: $ip: DENY
    " > $tdeny
          mv $tdeny $deny
        fi
      fi

      # Only keep current entries in the connection log
      grep "$hh $mm " $clog > $clogt
      mv $clogt $clog

    This approach has worked like a charm for me in the last few weeks.
    The basic concept isn't restricted to ssh, it could easily be extended
    to protect other services, too.

    On busy systems some locking around the modification of hosts.deny
    could be necessary, and a possible danger is that any user from a
    host can deliberately lock out that host (and other users on that
    host) - the same issue as with locking accounts after x failed attempts.

    It's a cheap hack, far from perfect, and won't fit anybody, but the
    net effect has been very positive for me.

    mp.

    -- 
    Systems Administrator | Institute of Scientific Computing | Univ. of Vienna
    

  • Next message: Per Hedeland: "Re: PAM changing user name"

    Relevant Pages

    • Re: How to create an ssh chain A->B->C to do http over ssh across the chain?
      ... running the following on host A: ... will be forwarded over an SSH connection to port 8080 on host B. The ... second "ssh" command running on B, meanwhile, will then act as a SOCKS ...
      (Debian-User)
    • Re: rcp and rlogin
      ... I just tried three ssh connections to work to machines my ip should not be ... Connection closed by remote host ... ssh: connect to host port 22: Connection timed out ...
      (RedHat)
    • Question on SSH configuration in a cluster environment.
      ... When a failover happens in a cluster, ... known_hosts file is picking up the public key from the physical host. ... In investigating the ssh configuration issue for the cluster I have ... there are ssh connection issues because the host_keys are ...
      (comp.security.ssh)
    • Question on SSH configuration in a cluster environment.
      ... When a failover happens in a cluster, ... known_hosts file is picking up the public key from the physical host. ... In investigating the ssh configuration issue for the cluster I have ... there are ssh connection issues because the host_keys are ...
      (comp.security.unix)
    • Question on SSH configuration in a cluster environment.
      ... When a failover happens in a cluster, ... known_hosts file is picking up the public key from the physical host. ... In investigating the ssh configuration issue for the cluster I have ... there are ssh connection issues because the host_keys are ...
      (comp.unix.solaris)