Home Assistant + fail2ban + SSL/TLS + Storing Secret

Home Assistantにfail2banのルールを適用

https://www.home-assistant.io/cookbook/fail2ban/

オートメーションシステムへの不正ログインに有効な手段であるfail2banを導入します。同一IPアドレスからシステムへのログイン認証の際、設定回数を超えて認証が失敗すると、そのIPアドレスからのアクセスを拒否します。


fail2banのインストール

ソースから最新版をインストールします。

$ git clone https://github.com/fail2ban/fail2ban.git
$ cd fail2ban
$ sudo python3 setup.py install 

システムデーモンとして起動するための設定後、起動します。

$ cp files/debian-initd /etc/init.d/fail2ban
$ sudo update-rc.d fail2ban defaults
$ sudo service fail2ban start

Home Assistantの設定

configuration.yaml内でHome Assistantのログを有効にします。

logger:
  default: critical
  logs:
    homeassistant.components.http.ban: warning

Home Assistantを再起動します。(設定画面内で再起動)

誤ったパスワードを入力し、アクセス元のIPアドレスがログに記録されるか確認します。

$ tail -f /home/homeassistant/.homeassistant/home-assistant.log | grep WARNING
2018-08-29 14:28:15 WARNING (MainThread) [homeassistant.components.http.ban] Login attempt or request with invalid authentication from xxx.xxx.xxx.xxx

fail2banの設定

次の2つのファイルを作成します。

  • /etc/fail2ban/filter.d/ha.conf
  • /etc/fail2ban/jail.d/ha.conf

/etc/fail2ban/filter.d/ha.conf :

[INCLUDES]
before = common.conf

[Definition]
failregex = ^%(__prefix_line)s.*Login attempt or request with invalid authentication from <HOST>.*$
ignoreregex =

/etc/fail2ban/jail.d/ha.conf

注)logpathはインストール環境により異なります。

[DEFAULT]
# Email config
sender = [email protected]
destemail = [email protected]

# Action "%(action_mwl)s" will ban the IP and send an email notification including whois data and log entries.
action = %(action_mwl)s

[ha]
enabled = true
filter = ha
logpath = /home/homeassistant/.homeassistant/home-assistant.log

# 3600 seconds = 1 hour
bantime = 30 # during testing it is useful to have a short ban interval, comment out this line later

# Maximum amount of login attempts before IP is blocked
maxretry = 3

動作確認

fail2banの再起動

$ sudo service fail2ban restart

fail2banステータス確認

sudo systemctl status fail2ban

jailを確認

sudo fail2ban-client status
Status
|- Number of jail:	1
`- Jail list:	ha

誤ったパスワードを入力しfail2banに反映されるか以下のコマンドで確認します。

$ sudo tail -f -n 20 /var/log/fail2ban.log
2018-08-29 13:25:37,907 fail2ban.server         [10208]: INFO    Starting Fail2ban v0.10.3.fix1
2018-08-29 13:25:37,916 fail2ban.database       [10208]: INFO    Connected to fail2ban persistent database '/var/lib/fail2ban/fail2ban.sqlite3'
2018-08-29 13:25:37,918 fail2ban.jail           [10208]: INFO    Creating new jail 'ha'
2018-08-29 13:25:37,922 fail2ban.jail           [10208]: INFO    Jail 'ha' uses poller {}
2018-08-29 13:25:37,922 fail2ban.jail           [10208]: INFO    Initiated 'polling' backend
2018-08-29 13:25:37,932 fail2ban.filter         [10208]: INFO    Added logfile: '/home/homeassistant/.homeassistant/home-assistant.log' (pos = 5873, hash = 02ec3aefc005465a6cd8db91eff2d5e57c45757e)
2018-08-29 13:25:37,932 fail2ban.filter         [10208]: INFO      encoding: UTF-8
2018-08-29 13:25:37,933 fail2ban.filter         [10208]: INFO      maxRetry: 3
2018-08-29 13:25:37,934 fail2ban.filter         [10208]: INFO      findtime: 600
2018-08-29 13:25:37,934 fail2ban.actions        [10208]: INFO      banTime: 30
2018-08-29 13:25:37,938 fail2ban.jail           [10208]: INFO    Jail 'ha' started
2018-08-29 13:27:49,125 fail2ban.filter         [10208]: INFO    [ha] Found xxx.xxx.xxx.xxx - 2018-08-29 13:27:48
2018-08-29 13:27:51,330 fail2ban.filter         [10208]: INFO    [ha] Found xxx.xxx.xxx.xxx - 2018-08-29 13:27:51
2018-08-29 13:27:52,533 fail2ban.filter         [10208]: INFO    [ha] Found xxx.xxx.xxx.xxx - 2018-08-29 13:27:52
2018-08-29 13:27:52,678 fail2ban.actions        [10208]: NOTICE  [ha] Ban xxx.xxx.xxx.xxx
2018-08-29 13:28:23,941 fail2ban.actions        [10208]: NOTICE  [ha] Unban xxx.xxx.xxx.xxx

動作に問題がなければ、以下ファイルのbantime=30を任意の時間に設定し直してfail2banを再起動します。

/etc/fail2ban/jail.d/ha.conf

jail haへのアクセスを無条件で許可するIPアドレスを指定する場合は以下のようになります。

$ sudo fail2ban-client set ha unbanip xxx.xxx.xxx.xxx

SSL/TLSの導入

セルフ認証によりSSL/TLSを導入します。

Home Assistantの設定フォルダ.homeassistant内で以下のコマンドを実行してpemファイルを作成します。

$ openssl req -sha256 -newkey rsa:4096 -nodes -keyout privkey.pem -x509 -days 730 -out certificate.pem

configuration.yamlファイルに以下の内容を記述します。

http:
  ssl_certificate: /home/your_user/.homeassistant/certificate.pem
  ssl_key: /home/your_user/.homeassistant/privkey.pem

httpの他のオプションについては以下参照して下さい。

上記httpのtrusted_networksオプションは以下authenticationのtrusted-networksオプションに置き換わりました。

ログイン無しでアクセスできるIPアドレスのwhitelistの設定は以下のようになります。

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  auth_providers:
    - type: homeassistant
    - type: trusted_networks #typeを追加
      trusted_networks:
        - 127.0.0.1
        - fd00::/8

Storing Secret

configuration.yaml内のパスワードなど直接見られないよう、secrets.yamlファイルを別に用意して、この中でパスワードなどを記述します。位置情報やメールサーバのパスワードなどにも適用します。

http:
  api_password: YOUR_PASSWORD

!secret の後ろにパスワードとリンクする任意のワードを記述します。

http:
  api_password: !secret http_password

secrets.yaml ファイル内に上記ワードに対応したパスワードを記述します。

http_password: YOUR_PASSWORD