====== Tinyhoneypot ====== Une astuce pour sécuriser son serveur en utilisant Tiny Honeypot et Fail2Ban. (via http://book.knah-tsaeb.org/?SVAp2w ) Dernière mise à jour : **19 février 2017** J'ai eu un problème avec la regex de fail2ban qui ne "matchait" pas. J'ai posté sur [[https://debian-fr.org/t/fail2ban-regex-qui-ne-match-pas/72589|le forum Debian-fr.org]] pour trouver une solution et finalement, je me suis rendu compte que c'est le format de date qui posait un problème. J'ai donc changé les locales pour avoir un format de date anglais, ce qui a résolu mon problème. ===== Prérequis ===== Avoir changé le port ssh par défaut (22) . ===== Installation ===== Installation des programmes nécessaires : # apt-get update # apt-get install tinyhoneypot fail2ban xinetd Lecture des listes de paquets... Fait Construction de l'arbre des dépendances Lecture des informations d'état... Fait fail2ban est déjà la plus récente version disponible. Les paquets supplémentaires suivants seront installés : libfile-copy-recursive-perl libpopt0 logrotate update-inetd Les NOUVEAUX paquets suivants seront installés : libfile-copy-recursive-perl libpopt0 logrotate tinyhoneypot update-inetd xinetd 0 mis à jour, 6 nouvellement installés, 0 à enlever et 1 non mis à jour. Il est nécessaire de prendre 318 ko dans les archives. Après cette opération, 959 ko d'espace disque supplémentaires seront utilisés. Souhaitez-vous continuer ? [O/n] Copie d'un exemple de configuration : # cp -v /usr/share/doc/tinyhoneypot/examples/xinetd.d/thp-pasv /etc/xinetd.d/ ‘/usr/share/doc/tinyhoneypot/examples/xinetd.d/thp-pasv’ -> ‘/etc/xinetd.d/thp-pasv’ Modification de la configuration copiée juste avant : # vim /etc/xinetd.d/thp-pasv # cat /etc/xinetd.d/thp-pasv # default: on # description: thp-ftpd calls the generic thpsvcs with param "ftp", # resulting in an ftpd emulation. service thp-pasv { type = UNLISTED socket_type = stream protocol = tcp port = 22 wait = no user = thpot server = /usr/sbin/thpot server_args = nullresp nice = 10 disable = no instances = 1 per_source = 1 } On regarde le statut du service xinetd : # service xinetd status ● xinetd.service - LSB: Starts or stops the xinetd daemon. Loaded: loaded (/etc/init.d/xinetd) Active: active (running) since sam. 2017-02-04 17:25:30 CET; 2min 56s ago CGroup: /system.slice/xinetd.service └─6351 /usr/sbin/xinetd -pidfile /run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6 févr. 04 17:25:30 C-3PO xinetd[6351]: removing daytime févr. 04 17:25:30 C-3PO xinetd[6351]: removing daytime févr. 04 17:25:30 C-3PO xinetd[6351]: removing discard févr. 04 17:25:30 C-3PO xinetd[6351]: removing discard févr. 04 17:25:30 C-3PO xinetd[6351]: removing echo févr. 04 17:25:30 C-3PO xinetd[6351]: removing echo févr. 04 17:25:30 C-3PO xinetd[6351]: removing time févr. 04 17:25:30 C-3PO xinetd[6351]: removing time févr. 04 17:25:30 C-3PO xinetd[6351]: xinetd Version 2.3.15 started with libwrap loadavg options compiled in. févr. 04 17:25:30 C-3PO xinetd[6351]: Started working: 0 available services On redémarre le service xinetd et on vérifie qu'il écoute bien sur le port SSH par défaut : # service xinetd restart # netstat -lptn | grep xinetd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4426/xinetd On peut maintenant essayer de se connecter sans spécifier de port (donc tenter une connexion sur le port 22 ou Xinetd écoute) : (en local) : # ssh 127.0.0.1 ^C (sur le serveur) : # cat /var/log/thpot/captures févr. 04 17:30:38 SID=589601AEBFC6C.nullresp PID=6419 SRC=127.0.0.1 SPT=41690 ET=00:00:05 BYTES=39 Ok, la première partie marche! On regarde maintenant pour utiliser Fail2ban avec ça. (ATTENTION : On ne doit pas modifier le fichier "jail.conf" mais uniquement le fichier "jail.local" !) On fait donc une copie de jail.conf => jail.local : # cp -v /etc/fail2ban/jail.conf /etc/fail2ban/jail.local ‘/etc/fail2ban/jail.conf’ -> ‘/etc/fail2ban/jail.local’ # vim /etc/fail2ban/jail.local # cat /etc/fail2ban/jail.local (...) [ssh] enabled = true port = xxxx filter = sshd logpath = /var/log/auth.log maxretry = 6 #SLF: (voir https://www.howtoforge.com/tutorial/increase-ipv4-security-with-fail2ban-and-tinyhoneypot-on-debian-jessie/ ) [thp-ssh] enabled = true port = all filter = thpot logpath = /var/log/thpot/captures banaction = iptables-allports maxretry = 1 findtime = 300 bantime = 600 (...) **note**: J'ai mis un bantime relativement court au départ (600 secondes) le temps de tester que tout fonctionne bien. Au pire je suis banni juste 10 minutes si je fais une bêtise... Une fois que tout fonctionne bien, il faut augmenter le temps de ban. On rajoute le filtre fail2ban pour Tiny Honeypot : # vim /etc/fail2ban/filter.d/thpot.local # cat /etc/fail2ban/filter.d/thpot.local # Fail2Ban configuration file for Tiny Honeypot [Definition] # patern : # Mar 02 13:28:06 SID=56D6DC56E6F47.nullresp PID=4437 SRC=xxx.xxx.xxx.xxx SPT=45797 ET=00:00:09 BYTES=39 # Mar 02 13:44:25 SID=56D6E02951E5.nullresp PID=4566 SRC=yyy.yyy.yyy.yyy SPT=44993 ET=00:00:04 failregex = .* SRC= .* ignoreregex = On peut tester si les regex de fail2ban fonctionnent bien : # fail2ban-regex /var/log/thpot/captures /etc/fail2ban/filter.d/thpot.local Running tests ============= Use failregex file : /etc/fail2ban/filter.d/thpot.local Use log file : /var/log/thpot/captures Results ======= Failregex: 86 total |- #) [# of hits] regular expression | 1) [86] .* SRC= .* `- Ignoreregex: 0 total Date template hits: |- [# of hits] date format | [86] MONTH Day Hour:Minute:Second `- Lines: 86 lines, 0 ignored, 86 matched, 0 missed On redémarre ensuite le service fail2ban et on regarde ses logs et son statut : # service fail2ban restart # cat /var/log/fail2ban.log 2016-03-02 14:49:40,128 fail2ban.server [4862]: INFO Stopping all jails 2016-03-02 14:49:40,240 fail2ban.jail [4862]: INFO Jail 'ssh' stopped 2016-03-02 14:49:41,244 fail2ban.jail [4862]: INFO Jail 'thp-ssh' stopped 2016-03-02 14:49:41,245 fail2ban.server [4862]: INFO Exiting Fail2ban 2016-03-02 14:49:41,796 fail2ban.server [4934]: INFO Changed logging target to /var/log/fail2ban.log for Fail2ban v0.8.13 2016-03-02 14:49:41,797 fail2ban.jail [4934]: INFO Creating new jail 'ssh' 2016-03-02 14:49:41,817 fail2ban.jail [4934]: INFO Jail 'ssh' uses pyinotify 2016-03-02 14:49:41,833 fail2ban.jail [4934]: INFO Initiated 'pyinotify' backend 2016-03-02 14:49:41,834 fail2ban.filter [4934]: INFO Added logfile = /var/log/auth.log 2016-03-02 14:49:41,835 fail2ban.filter [4934]: INFO Set maxRetry = 6 2016-03-02 14:49:41,836 fail2ban.filter [4934]: INFO Set findtime = 600 2016-03-02 14:49:41,837 fail2ban.actions[4934]: INFO Set banTime = 600 2016-03-02 14:49:41,865 fail2ban.jail [4934]: INFO Creating new jail 'thp-ssh' 2016-03-02 14:49:41,865 fail2ban.jail [4934]: INFO Jail 'thp-ssh' uses pyinotify 2016-03-02 14:49:41,868 fail2ban.jail [4934]: INFO Initiated 'pyinotify' backend 2016-03-02 14:49:41,869 fail2ban.filter [4934]: INFO Added logfile = /var/log/thpot/captures 2016-03-02 14:49:41,870 fail2ban.filter [4934]: INFO Set maxRetry = 1 2016-03-02 14:49:41,871 fail2ban.filter [4934]: INFO Set findtime = 1800 2016-03-02 14:49:41,871 fail2ban.actions[4934]: INFO Set banTime = 3600 2016-03-02 14:49:41,876 fail2ban.jail [4934]: INFO Jail 'ssh' started 2016-03-02 14:49:41,880 fail2ban.jail [4934]: INFO Jail 'thp-ssh' started # service fail2ban status ● fail2ban.service - LSB: Start/stop fail2ban Loaded: loaded (/etc/init.d/fail2ban) Active: active (running) since Wed 2016-03-02 14:49:41 CET; 8min ago Process: 4905 ExecStop=/etc/init.d/fail2ban stop (code=exited, status=0/SUCCESS) Process: 4922 ExecStart=/etc/init.d/fail2ban start (code=exited, status=0/SUCCESS) CGroup: /system.slice/fail2ban.service └─4934 /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock -p /var/run/fail2ban/fail2ban.pid Mar 02 14:49:41 xxxxxx fail2ban[4922]: Starting authentication failure monitor: fail2ban. Malgré un "failure monitor: fail2ban ..." ça fonctionne. Ma session active (dans un autre terminal) a même été dégagée. ===== Tests ===== (en local vers 15h10 :) $ ssh xxx.xxx.xxx.xxx ^C $ ssh xxx.xxx.xxx.xxx ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused $ ssh -p xxxx LOGIN@xxx.xxx.xxx.xxx ssh: connect to host xxx.xxx.xxx.xxx port xxxx: Connection refused (dernière tentative à 15h16) Par contre, le ping marche toujours. Surprenant! Si on regarde les logs de fail2ban une fois qu'on est "débanni" du serveur, je remarque que j'ai déjà une "prise" : # cat /var/log/fail2ban.log 2016-03-02 15:23:22,329 fail2ban.actions[4934]: WARNING [thp-ssh] Ban 84.37.137.216 Tiens, c'est pas moi ça ! La suite : http://links.simonlefort.be/?Cif-aA === Bonus === Pour voir les ports utilisés : # netstat -lptn