[Memo2017][ITPASS]fail2ban のインストールと設定

fail2ban のインストール

$ sudo -s
# apt-get update
# apt-get install fail2ban

iptables -L でインストールされたことを確認した. 以下のように fail2ban の記述が表示された.

# iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
fail2ban-ssh  tcp  --  anywhere             anywhere            multiport dports ssh    

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain fail2ban-ssh (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere             

設定ファイルの編集

以下の通り設定ファイルを編集した.

ssh

ssh に関する設定を以下のようにを書き換えた.

[ssh]
enabled = true
port    = ssh
filter  = sshd
logpath  = /var/log/auth.log
maxretry = 4

apache

apache に関する設定を以下のようにを書き換えた.

[apache]

enabled = true
port    = http,https
filter  = apache-auth
findtime= 60  
logpath = /var/log/httpd-*error.log
maxretry = 60
bantime  = 1200  

# default action is now multiport, so apache-multiport jail was left
# for compatibility with previous (<0.7.6-2) releases
[apache-multiport]

enabled   = true
port      = http,https
filter    = apache-auth
findtime  = 10  
logpath = /var/log/httpd-*error.log
maxretry  = 60
bantime  = 1200

 [apache-nohome]
 enabled  = true
 port     = http,https
 filter   = apache-nohome
 findtime = 60
 logpath  = /var/log/httpd-*error.log
 maxretry = 60
 bantime  = 1200

また以下を書き加えた.

[apache-ddos]
enabled  = true
port     = http,https
filter   = apache-ddos
findtime= 10
logpath  = /var/log/httpd-*access.log
maxretry = 60
bantime  = 1200

failregex の追加

apache

  • /etc/fail2ban/filter.d/apache-auth.conf の failregex を以下のように編集した.

apache-auth.conf, apache-common.conf, apache-nohome.conf, apache-noscript.conf, apache-overflows.conf の中身を以下のように書き換えた.

  • /etc/fail2ban/filter.d/apache-auth.conf, apache-ddos.conf

/etc/fail2ban/filter.d/apache-auth.conf を /etc/fail2ban/filter.d/apache-ddos.conf にコピーした.

# cp /etc/fail2ban/filter.d/apache-auth.conf /etc/fail2ban/filter.d/apache-ddos.conf

/etc/fail2ban/filter.d/apache-ddos.conf を以下のように書き換えた.

failregex =^<HOST> -.*GET.*
ignoreregex = \.(?i)(jpe?g|gif|png|bmp|pdf|js|css|woff|eot|ttf|ico|txt|xml|swf|xlsx?|docx?|pptx?)

/etc/fail2ban/filter.d/apache-auth.conf を以下のように書き換えた.

failregex = ^%(_apache_error_client)s (AH(01797|01630): )?client denied by server configuration: (uri )?\S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01617: )?user .*? authentication failure for "\S*": Password Mismatch(, referer: \S+)?$
            ^%(_apache_error_client)s (AH01618: )?user .*? not found(:)?\S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01614: )?client used wrong authentication scheme: \S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH\d+: )?Authorization of user \S+ to access \S* failed, reason: .*$
            ^%(_apache_error_client)s (AH0179[24]: )?(Digest: )?user .*?: password mismatch: \S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH0179[01]: |Digest: )user `.*?' in realm `.+' (not found|denied by provider): \S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01631: )?user .*?:authorization failure for "\S*":(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01775: )?(Digest: )?invalid nonce .* received - length is not \S+(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01788: )?(Digest: )?realm mismatch - got `.*?' but expected `.+'(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01789: )?(Digest: )?unknown algorithm `.*?' received: \S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01793: )?invalid qop `.*?'received: \S*(, referer: \S+)?\s*$
            ^%(_apache_error_client)s (AH01777: )?(Digest: )?invalidnonce .*? received - user attempted time travel(, referer: \S+)?\s*$
ignoreregex = ^<HOST> -.*GET.*(jpg|jpeg|gif|png).*
  • /etc/fail2ban/filter.d/apache-common.conf

以下のように記述されているかを確認した.

apache_error_client = \[[^]]*\] \[(:?error|\S+:\S+)\]( \[pid \d+(:\S+\d+)?\])? \[client <HOST>(:\d{1,5})?\]
  • /etc/fail2ban/filter.d/apache-nohome.conf

以下のように記述されているかを確認した.

failregex = ^%(_apache_error_client)s (AH00128: )?File does not exist: .*/~.*
  • /etc/fail2ban/filter.d/apache-noscript.conf

以下のように記述されているか確認した.

failregex = ^%(_apache_error_client)s ((AH001(28|30): )?File does not exist|(AH01264: )?script not found or unable to stat):/\S*(php([45]|[.-]cgi)?|\.asp|\.exe|\.pl)(, referer: \S+)?\s*$
            ^%(_apache_error_client)s script'/\S*(php([45]|[.-]cgi)?|\.asp|\.exe|\.pl)\S*' not found or unable to stat(, referer: \S+)?\s*$
  • /etc/fail2ban/filter.d/apache-overflows.conf

以下のように記述されているかを確認した.

failregex = ^%(_apache_error_client)s ((AH0013[456]: )?Invalid
(method|URI) in request .*( - possible attempt to establish SSL
connection on non-SSL port)?|(AH00565: )?request failed: URI too long
\(longer than \d+\)|request failed: erroneous characters after protocol
string: .*|AH00566: request failed: invalid characters in URI)(,referer: \S+)?$

ssh

  • /etc/fail2ban/filter.d/sshd.conf の failregex を編集した.
    • 以下の1行を追記した.

      ^%(__prefix_line)s[iI] .* [[]<HOST>[]] .* POSSIBLE BREAK-IN ATTEMPT\s*S

ログレベルの確認

  • /etc/fail2ban/fail2ban.conf の中のログレベルを確認した.
    • ban, unban の記録がログに出力されるレベル

loglevel = INFO

fail2ban の起動

以下のコマンドで fail2ban を起動した.

# systemctl start fail2ban

fail2ban が動いているか確認した.

# systemctl status fail2ban

● fail2ban.service - Fail2Ban Service

Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-11-21 16:42:39 JST; 19min ago
  Docs: man:fail2ban(1)
Process: 1610 ExecStop=/usr/bin/fail2ban-client stop (code=exited, status=0/SUCCESS)
Process: 1642 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=0/SUCCESS)
Main PID: 1645 (fail2ban-server)
   Tasks: 13 (limit: 4915)
  CGroup: /system.slice/fail2ban.service
          mq1645 /usr/bin/python3 /usr/bin/fail2ban-server -s /var/run/fail2ban/fail2ban.sock -p /var/r

Nov 21 16:42:38 tako-itpass systemd[1]: Starting Fail2Ban Service... Nov 21 16:42:38 tako-itpass fail2ban-client[1642]: 2017-11-21 16:42:38,919 fail2ban.server [1643 Nov 21 16:42:38 tako-itpass fail2ban-client[1642]: 2017-11-21 16:42:38,919 fail2ban.server [1643 Nov 21 16:42:39 tako-itpass systemd[1]: Started Fail2Ban Service.

動作テスト

ssh

johoXX から, 存在しないユーザ名で tako に ssh を行い, 動作をチェックを行った.

$ ssh hoge@133.30.109.21
 hoge@133.30.109.21's password:
 Permission denied (Publickey).
(これを繰り返す)

maxretry 回目には接続が切られ,

hoge@johoXX-itpass:~$ ssh hoge@133.30.109.21
ssh: connect to host 133.30.109.21 port 22: Connection refused

とメッセージが届いた.

tako で iptables -L をして, リストに加えられているかを確認した.

root@tako-itpass:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
fail2ban-ssh  tcp  --  anywhere             anywhere            multiport dports ssh 
(省略)
Chain fail2ban-ssh (1 references)
target     prot opt source                             destination         
DROP       all  --  133.30.109.XX anywhere  <-- これ           
RETURN     all  --  anywhere                           anywhere            

fail2ban のログに記録されているか確認した. 以下のような警告がログ /var/log/fail2ban.log に記載された.

2017-11-21 16:37:44,159 fail2ban.actions        [1514]: NOTICE  [ssh] Ban 133.30.109.XX

apache

error のテスト

  • fail2ban を再起動した.
# /etc/init.d/fail2ban restart
  • ブラウザを立ち上げ, johoXX から
133.30.109.21/hoge 

を入力して, findtime 以内に maxretry 回以上アクセスした.

  • iptables -L を見て, アクセスした IP アドレスが ban されていることを確認した.
Chain fail2ban-apache-ddos (1 references)
target     prot opt source               destination         
REJECT     all  --  133.30.109.XX        anywhere             reject-with   icmp-port-unreachable 
RETURN     all  --  anywhere             anywhere            
  • /var/log/fail2ban.log についても確認した.

    2017-11-21 16:43:24,922 fail2ban.actions [1645]: NOTICE [apache-ddos] Ban 133.30.109.XX

access のテスト

  • ブラウザから tako-itpass.scitec.kobe-u.ac.jp 以下のファイルに findtime 以内に maxretry 回更新した.
133.30.109.21/test
  • iptables -L を見て, アクセスした IP アドレスが ban されていることを確認した.
Chain fail2ban-apache-ddos (1 references)
target     prot opt source               destination         
REJECT     all  --  133.30.109.XX        anywhere             reject-with  icmp-port-unreachable
RETURN     all  --  anywhere             anywhere  
  • /var/log/fail2ban.log についても確認した.

    2017-11-21 16:47:35,429 fail2ban.actions [1645]: NOTICE [apache-ddos] Ban 133.30.109.XX

挙動がシステムログメールに記載されるように設定

スクリプトの設置

  • ika の ~itpass/ftp/server/2011/system_report/loginfail が tako の /etc/cron.local/daily/800_loginfail にコピーされていることを確認した.

fail2ban の動作確認

  • fail2ban のフィルターの動作確認を行った.
# fail2ban-regex --print-all-missed /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
  • 表示内容を確認し, フィルターにかかっていることを確認した.
Last modified:2017/11/21 17:12:20
Keyword(s):
References:[[ITPASS2017]2017年度サーバ構築ログ]