BLOGTIMES
2021/02/28

CentOS 8 へのブルートフォースアタックを SSHGuard で防御

  ssh  centos8  firewalld 
このエントリーをはてなブックマークに追加

SSH に対する攻撃の対策については iptables でやるのであれば昔は ipt_recent、最近は hashlimit を使ったりするようですが、単純なレートリミットだと正常なログインでも引っかかってしまう可能性があるので 今回は SSHGuard をインスト-ルしてみました。

SSHGuard はログを監視して ssh に関するログインの失敗が一定以上ある IP を自動的に一定期間ブロック(場合によっては永久にブロック)するという機能を持ったdaemon です。機能的には fail2ban とかと同じカテゴリのソフトウェアです。

インストールから起動まで

メインの設定ファイル /etc/sshguard.confdnf でインストールしただけでは作成されないので、man sshguard-setup の内容を参考に自分で設定する必要があります。CentOS 8(パケットフィルタは firewalld、ログの監視先はjournalctl という構成)の場合には、以下のような感じでセットアップすることができるはずです。sshguard.whitelistの IP アドレス等は適当に調整してください。

# install package dnf -y install sshguard # create conf cp /usr/share/doc/sshguard/examples/sshguard.conf.sample /etc/sshguard.conf cp -a /etc/sshguard.whitelist{,.org} # change conf pushd /etc patch -p2 <<'EOF' --- /etc/sshguard.whitelist.org 2020-08-02 14:53:08.000000000 +0900 +++ /etc/sshguard.whitelist 2021-02-28 16:20:01.621414766 +0900 @@ -5,9 +5,9 @@ #2001:0db8:85a3:08d3:1319:8a2e:0370:7344 # address blocks in CIDR notation -#127.0.0.0/8 +127.0.0.0/8 #10.11.128.0/17 -#192.168.0.0/24 +192.168.0.0/16 # hostnames #rome-fw.enterprise.com EOF patch -p2 <<'EOF' --- /usr/share/doc/sshguard/examples/sshguard.conf.sample 2018-12-16 11:41:51.000000000 +0900 +++ /etc/sshguard.conf 2021-03-02 09:22:13.490714460 +0900 @@ -6,14 +6,14 @@ #### REQUIRED CONFIGURATION #### # Full path to backend executable (required, no default) -#BACKEND="/usr/local/libexec/sshg-fw-iptables" +BACKEND="/usr/libexec/sshguard/sshg-fw-firewalld" # Space-separated list of log files to monitor. (optional, no default) -#FILES="/var/log/auth.log /var/log/authlog /var/log/maillog" +#FILES="/var/log/secure" # Shell command that provides logs on standard output. (optional, no default) # Example 1: ssh and sendmail from systemd journal: -#LOGREADER="LANG=C /usr/bin/journalctl -afb -p info -n1 -t sshd -t sendmail -o cat" +LOGREADER="LANG=C /usr/bin/journalctl -afb -p info -n1 -t sshd -t sendmail -o cat" # Example 2: ssh from os_log (macOS 10.12+) #LOGREADER="/usr/bin/log stream --style syslog --predicate '(processImagePath contains \"sshd\")'" @@ -24,17 +24,17 @@ # Block attackers for initially BLOCK_TIME seconds after exceeding THRESHOLD. # Subsequent blocks increase by a factor of 1.5. (optional, default 120) -BLOCK_TIME=120 +BLOCK_TIME=300 # Remember potential attackers for up to DETECTION_TIME seconds before # resetting their score. (optional, default 1800) -DETECTION_TIME=1800 +DETECTION_TIME=86400 # Size of IPv6 'subnet to block. Defaults to a single address, CIDR notation. (optional, default to 128) IPV6_SUBNET=128 # Size of IPv4 subnet to block. Defaults to a single address, CIDR notation. (optional, default to 32) -IPV4_SUBNET=32 +IPV4_SUBNET=24 #### EXTRAS #### # !! Warning: These features may not work correctly with sandboxing. !! @@ -44,8 +44,8 @@ # Colon-separated blacklist threshold and full path to blacklist file. # (optional, no default) -#BLACKLIST_FILE=90:/var/lib/sshguard/enemies +BLACKLIST_FILE=60:/var/lib/sshguard/enemies # IP addresses listed in the WHITELIST_FILE are considered to be # friendlies and will never be blocked. -#WHITELIST_FILE=/etc/friends +WHITELIST_FILE=/etc/sshguard.whitelist EOF popd # enable/start daemon systemctl enable --now sshguard.service

動作確認

SSHGuard はバックエンドが firewalld の場合、対象となるログを監視して、一定の閾値を超えた場合にsshguard4/sshguard6という名前のブラックリスト用 ipset に IP アドレスを追加するという動作を行います。よって、フィルタがきちんと動作するためには firewalld から sshguard4/sshguard6 がきちんと参照されているかということと、sshguard4/sshguard6 が SSHGuard によってきちんと読み書きされていることが必要になります。

前者については、以下のコマンドの rich rules: 欄を見ると確認ができます。

# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens192 sources: services: cockpit dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv6" source ipset="sshguard6" drop rule family="ipv4" source ipset="sshguard4" drop

後者については例えばログを見てみたときに以下のようなログ(999.999.999.999は例なので、実際には攻撃元の IP が入ります)が残っていれば SSHGuard が実際にログを読み取って処理を行っていることが分かります(ただし、これは実際に攻撃を受けないといけませんが)。

# journalctl -u sshguard Feb 28 16:58:51 sshhost.example.com sshguard[57453]: Attack from "999.999.999.999" on service SSH with danger 2. Feb 28 16:58:51 sshhost.example.com sshguard[57453]: Attack from "999.999.999.999" on service SSH with danger 2. Feb 28 16:59:23 sshhost.example.com sshguard[57453]: Attack from "999.999.999.999" on service SSH with danger 2. Feb 28 16:59:23 sshhost.example.com sshguard[57453]: Blocking "999.999.999.999/32" for 120 secs (3 attacks in 32 secs, after 1 abuses over 32 secs.)

さらに、実際にブロックが行われていれば、以下のように ipset を使って登録されている IP アドレスを見ることができます。

# ipset list sshguard4 Name: sshguard4 Type: hash:net Revision: 6 Header: family inet hashsize 1024 maxelem 65536 Size in memory: 440 References: 0 Number of entries: 1 Members: 999.999.999.999

    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/12319
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン