SSH-Dictionary-Attacken und DenyHosts

Ich beginne meinen Tag gewöhnlicherweise mit einem Check meiner Server und bekomme bei der auth.log des Sysloggers wie üblich einen Schreikrampf, da diese stetig wächst. Standardbenutzer werden mit einem Dictionary getestet – und wer Accounts wie ‘test’ mit gleichnamigem Passwort beheimatet wird sehr bald über unliebsamen Besuch zu klagen haben.

Das Programm DenyHosts, ein Python Script parsed die Logs des SSHds und findet die angegriffenen Accounts und die IP des Angreifers, welche leider zu oft ein Proxy ist. Da ich OpenSSH benutze und dieser so gebaut ist, dass er die Datei /etc/hosts.deny beachtet werden die Angreifenden IPs genau dort eingetragen.

Die Website von DenyHosts verrät uns die Features:

Features

  • Parses /var/log/secure to find all login attempts and filters failed and successful attempts.
  • Synchronization mode (new in 2.0) allows DenyHosts daemons the ability to share data via a centralized server to proactively thwart attacks.
  • Can be run from the command line, cron or as a daemon (new in 0.9)
  • Records all failed login attempts for the user and offending host
  • For each host that exceeds a threshold count, records the evil host
  • Keeps track of each non-existent user (eg. sdadasd) when a login attempt failed.
  • Keeps track of each existing user (eg. root) when a login attempt failed.
  • Keeps track of each offending host (with 0.8+ these hosts can be purged if the associated entry in /etc/hosts.deny is expired)
  • Keeps track of suspicious logins (that is, logins that were successful for a host that had many login failures)
  • Keeps track of the file offset, so that you can reparse the same file (/var/log/secure) continuously (until it is rotated).
  • When the log file is rotated, the script will detect it and parse from the beginning.
  • Appends /etc/hosts.deny and adds the newly banned hosts
  • Optionally sends an email of newly banned hosts and suspicious logins.
  • Keeps a history of all user, host, user/host combo and suspicious logins encountered which includes the data and number of corresponding failed login attempts.
  • Maintains failed valid and invalid user login attempts in separate files, such that it is easy to see which valid user is under attack (which would give you the opportunity to remove the account, change the password or change it’s default shell to something like /sbin/nologin
  • Upon each run, the script will load the previously saved data and re-use it to append new failures.
  • Resolves IP addresses to hostnames, if available (new in v0.6.0).
  • /etc/hosts.deny entries can be expired (purge) at a user specified time (new in 0.8)
  • FreeBSD support (added in 0.7)

Kurz gesagt, angreifende IP-Adressen werden von Denyhosts geblockt und an einem zentralen Server gemeldet, mit welchem man synchronisieren kann um Angreifer schon vor ihrem Angriff zu blocken. – Der Haken an der Sache ist jedoch, dass man sehr bald eine lange Liste von IP Adressen und man somit oftmals ganze Netzwerkranges blockiert. Daher bietet es sich an, die Blockade auf einzelne Dienste wie SMTP und SSH zu beschränken, welche ohnehin nur eine begrenzte Anzahl von Benutzern benötigen.

Die Installation von DenyHosts war für mich als Gentoo-Benutzer sehr einfach, da sie sich auf ein ’emerge app-admin/denyhosts’ beschränkte, nachdem ich das Paket unmaskiert hatte:

echo "app-admin/denyhosts ~x86" >> /etc/portage/package.keywords
emerge app-admin/denyhosts

Um sicherzugehen dass mein SSHd mit tcpd Support compiled wurde loggte ich mich auf der lokalen Konsole ein und trug 127.0.0.1 in /etc/hosts.deny ein. Das File sieht wie folgt aus:

sshd: 127.0.0.1

Da mein SSHd mit tcpd-Unterstützung gebaut wurde verweigert er mir nun den Login via SSH von 127.0.0.1 kommend, was er mit der Meldung “ssh_exchange_identification: Connection closed by remote host” kund tat.

So weit so gut.

Die Konfiguration von DenyHosts liegt in /etc/denyhosts.conf und verlangt unsere Aufmerksamkeit. Hier die wichtigsten Dinge:

Achtung: Dies ist nur ein Auszug aus der Config-Datei!


SECURE_LOG = /var/log/messages # Sofern der SSHd dorthin loggt
DAEMON_LOG = /var/log/denyhosts # Wir wollen wissen was DenyHosts so macht

SYNC_SERVER = http://xmlrpc.denyhosts.net:9911 # Damit wir nun Zentral synchronisieren können
SYNC_DOWNLOAD = yes # Wir laden Listen herunter
SYNC_UPLOAD = yes # Wir melden böse Hosts ein

“rc-update add denyhosts default” und “/etc/init.d/denyhosts start” schliessen unsere Arbeit ab und klopfen uns auf die Schulter. Nach nicht allzulanger Zeit bekommen wir die ersten geblockten Hosts, einerseits durch unser Logfile welches gelesen wird – andererseits durch eben erfolgende Angriffe eines Script Kiddies.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *