Распухание логов роутера заставило меня обратить более пристальное внимание на записи в журналах безопасности sshd.
Dec 12 13:15:35 gw1 sshd[19967]: Received disconnect from 66.234.240.251: 11: Bye Bye
Эта запись появляется по одной причине: у меня запрещён вход на ssh по паролю. Разрешён только по сертификату. Бот который подбирает пароль к ssh к сожалению “тупой” и непрерывно долбится пока у него не закончится словарь. Когда ботов становится много и они настырны, то это и приводит к распуханию логов.
Я посчитал сколько таких записей есть в логе с количеством попыток больше 20-ти.
grep '11: Bye Bye' /var/log/secure* | sed -e 's/^.*Received disconnect from \([0-9]\{1,3\}\(.[0-9]\{1,3\}\)\{3\}\):.*$/\1/g' | sort | uniq -c | awk '{if($1>=20){print $0}}' | sort -nr
10852 85.236.191.80
8675 180.166.99.9
8043 112.216.109.98
5200 66.234.240.251
4260 202.159.16.251
860 46.166.162.14
788 37.58.80.10
751 139.159.118.166
344 202.114.72.243
339 27.221.10.43
243 118.175.7.84
241 117.79.146.58
239 81.212.109.229
227 103.23.102.5
223 197.242.145.194
207 159.122.113.152
203 218.248.13.234
169 115.84.178.168
138 202.106.211.99
134 218.38.12.218
134 210.97.115.248
129 74.208.221.217
129 46.148.16.98
128 5.10.78.54
120 169.50.6.153
110 218.57.11.7
110 157.7.133.67
107 111.2.199.136
97 123.116.127.5
96 168.235.149.30
94 31.14.162.21
91 182.131.21.69
85 123.49.32.40
74 93.174.93.138
69 101.251.239.38
68 200.60.5.75
56 60.169.78.38
56 109.169.48.102
51 61.188.189.7
50 200.35.150.97
47 5.10.78.52
47 115.85.192.40
45 5.10.74.194
42 58.60.168.236
41 89.163.212.64
38 219.235.227.190
38 182.73.50.86
35 219.93.67.114
35 169.54.91.220
33 80.82.64.127
33 159.122.107.155
27 80.195.4.83
27 37.59.173.229
26 121.199.65.21
26 104.200.78.34
25 185.112.102.222
24 89.163.140.142
23 65.181.123.194
22 5.10.71.227
20 94.182.163.75
20 62.210.214.89
fail2ban на такие записи никак не реагировал. Значит нужно это исправить.
Добавим регулярное выражение в файл /etc/fail2ban/filter.d/sshd.conf
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision: 728 $
#
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf
[Definition]
_daemon = sshd
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)
# Values: TEXT
#
failregex = ^%(__prefix_line)s(?:error: PAM: )?Authentication failure for .* from <HOST>\s*$
^%(__prefix_line)s(?:error: PAM: )?User not known to the underlying authentication module for .* from <HOST>\s*$
^%(__prefix_line)sFailed (?:password|publickey) for .* from <HOST>(?: port \d*)?(?: ssh\d*)?$
^%(__prefix_line)sROOT LOGIN REFUSED.* FROM <HOST>\s*$
^%(__prefix_line)s[iI](?:llegal|nvalid) user .* from <HOST>\s*$
^%(__prefix_line)sUser \S+ from <HOST> not allowed because not listed in AllowUsers$
^%(__prefix_line)sauthentication failure; logname=\S* uid=\S* euid=\S* tty=\S* ruser=\S* rhost=<HOST>(?:\s+user=.*)?\s*$
^%(__prefix_line)srefused connect from \S+ \(<HOST>\)\s*$
^%(__prefix_line)sAddress <HOST> .* POSSIBLE BREAK-IN ATTEMPT!*\s*$
^%(__prefix_line)sUser \S+ from <HOST> not allowed because none of user's groups are listed in AllowGroups$
^%(__prefix_line)sReceived disconnect from <HOST>: 11: Bye Bye$
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
Проверим то что оно срабатывает. Результат вывожу в файл так как он у меня получается большим.
fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/sshd.conf > aa.aa.txt
Нас интересует срабатывание выражения с индексом 11. Цитату приведу частично.
- Number of matches: [1] 0 match(es) [2] 0 match(es) [3] 0 match(es) [4] 0 match(es) [5] 2797 match(es) [6] 0 match(es) [7] 0 match(es) [8] 0 match(es) [9] 0 match(es) [10] 0 match(es) [11] 18924 match(es)
Видим что вместе с 5-м сработало и 11-е правило. Что и требовалось.
Теперь можно перезапустить fail2ban.
service fail2ban restart
И посмотреть как fail2ban заблокирует крацкера (cracker).
# tail /var/log/messages Dec 12 13:17:56 gw1 fail2ban.jail : INFO Jail 'ssh-iptables' stopped Dec 12 13:17:56 gw1 fail2ban.server : INFO Exiting Fail2ban Dec 12 13:17:57 gw1 fail2ban.server : INFO Changed logging target to SYSLOG for Fail2ban v0.8.4 Dec 12 13:17:57 gw1 fail2ban.jail : INFO Creating new jail 'ssh-iptables' Dec 12 13:17:57 gw1 fail2ban.jail : INFO Jail 'ssh-iptables' uses Inotify Dec 12 13:17:57 gw1 .<30>fail2ban.filter : INFO Added logfile = /var/log/secure Dec 12 13:17:57 gw1 .<30>fail2ban.filter : INFO Set maxRetry = 5 Dec 12 13:17:57 gw1 .<30>fail2ban.filter : INFO Set findtime = 600 Dec 12 13:17:57 gw1 fail2ban.actions: INFO Set banTime = 600 Dec 12 13:17:57 gw1 fail2ban.jail : INFO Jail 'ssh-iptables' started Dec 12 13:18:03 gw1 .<28>fail2ban.actions: WARNING [ssh-iptables] Ban 66.234.240.251