sshguard
sshguard は侵入防止システムです。サーバーのログを精査して害意ある行動を検出し、危害を加えようとする接続元IPアドレスを拒絶するファイアーウォールを用います。sshguard は、C言語で書かれており、インタプリターは不要です。
動作の仕組み
sshguard は、一つないし複数のログファイルを継続追跡する簡単なデーモンです。他のデーモンが記録したログイン試行失敗のログを精査して、その接続を拒絶するようにシステムファイアーウォールを更新することで、更なる試行を阻止します。
sshguard は、その名称にも拘わらず、 SSH のログのみを精査するわけではありません。多くのメールシステムやいくらかの FTP システムにも対応しています。対応しているサービスの全リストは、sshguard.net ウェブサイト で公開されています。
インストール
Emerge
app-admin/sshguard をインストール:
root #
emerge --ask app-admin/sshguard
追加のソフトウェア
sshguard が悪意のあるアクターをブロックするために、init システム、および sshguard に使用させたいファイアウォールバックエンドに応じて、追加のソフトウェアを emerge する必要があるかもしれません。
対応している様々なバックエンドについての詳しい情報は、セットアップ man ページを読むことで見つかります:
root #
man 7 sshguard-setup
iptables
システムのファイアウォールとして iptables が使用されているときは。
root #
emerge --ask net-firewall/iptables
iptables の利用と設定に関しての詳細は、 iptables の記事 も参照してください。
nftables
システムのファイアウォールとして nftables が使用されているときは:
root #
emerge --ask net-firewall/nftables
設定
iptables バックエンド
ファイアーウォールの準備
sshguard が悪意のあるユーザーをブロック (IP アドレスを遮断することによって) するとき、sshguard チェーンを使用します。
iptables でチェーンを準備し、外からの新しい接続が検出されたときにそれが発動することも確認してください:
root #
iptables -N sshguard
root #
iptables -A INPUT -j sshguard
そして、/etc/sshguard.conf で iptables バックエンドライブラリへの適切なパスが設定されていることを確認してください:
/etc/sshguard.conf
BACKEND に iptables ライブラリを設定する# Full path to backend executable (required, no default) BACKEND="/usr/libexec/sshg-fw-iptables"
ログファイルの監視
sshguard の背後にある基本的な考え方は、管理者がアプリケーションのオプションとして監視対象のログファイルを渡す、ということです。sshguard 固有の構成ファイルは存在しません。
Gentoo では、このオプションは /etc/sshguard.conf ファイル内に設定するのがベストでしょう:
/etc/sshguard.conf
sshguard が /var/log/messages と /var/log/auth.log を読むように設定する# Space-separated list of log files to monitor. (optional, no default) FILES="/var/log/messages /var/log/auth.log" # # How many problematic attempts trigger a block THRESHOLD=10 # Blocks last at least 24 hours (60480 seconds) BLOCKTIME=60480 # Track IP addresses for 24 hours (60480 seconds) DETECTION_TIME=60480 # # IPv6 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 128) IPV6_SUBNET=64 # IPv4 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 32) IPV4_SUBNET=24 # # Full path to PID file (optional, no default) PID_FILE=/run/sshguard.pid
sshguardランタイムユーザーが、使用するログファイルにアクセス可能であることを確認してください。
サービス
OpenRC
sshguardをデフォルトのランレベルに追加することでデフォルトで起動されます:
root #
rc-update add sshguard default
root #
rc-service sshguard start
systemd
systemd の標準的な方法で有効化して、起動してください:
root #
systemctl enable sshguard
root #
systemctl restart sshguard
ホストをブラックリストに入れる
With the blacklisting option after a number of abuses the IP address of the attacker or a IP subnet will be blocked permanently. The blacklist will be loaded at each startup and extended with new entries during operation. sshguard inserts a new address after it exceeded a threshold of abuses.
Blacklisted addresses are never scheduled to be released (allowed) again.
To enable blacklisting, create an appropriate directory and file:
root #
mkdir -p /var/lib/sshguard
root #
touch /var/lib/sshguard/blacklist.db
While defining a blacklist it is important to exclude trusted IP networks and hosts in a whitelist.
To enable whitelisting, create an appropriate directory and file:
root #
mkdir -p /etc/sshguard
root #
touch /etc/sshguard/whitelist
The whitelist has to include the loopback interface, and should have at least 1 IP trusted network f.e. 192.0.2.0/24.
/etc/sshguard/whitelist
Whitelisting trusted networks127.0.0.0/8 ::1/128 192.0.2.0/24
The 192.0.2.0/24 entry has to be adjusted to fit the own needs.
Add the BLACKLIST_FILE and WHITELIST_FILE file to the configuration. Example configuration listed blocks all hosts after the first login attempt. To setup a less agressive blocking policy, adjust the THRESHOLD and BLACKLIST_FILE integer, and set it to f.e. 10 instead of 2:
/etc/sshguard.conf
Configuring sshguard to blacklist abusersBACKEND="/usr/libexec/sshg-fw-iptables" FILES="/var/log/auth.log" # THRESHOLD=2 BLOCK_TIME=43200 DETECTION_TIME=604800 # IPV4_SUBNET=24 IPV6_SUBNET=64 # PID_FILE=/run/sshguard.pid # # Add following lines BLACKLIST_FILE=2:/var/lib/sshguard/blacklist.db WHITELIST_FILE=/etc/sshguard/whitelist
Restart the sshguard daemon to have the changes take effect:
root #
/etc/init.d/sshguard restart
トラブルシューティング
File '/var/log/auth.log' vanished while adding!
起動時に sshguard が次のエラーを報告します:
Sep 23 03:39:11 foo.bar.com sshguard[64933]: File '/var/log/auth.log' vanished while adding!
ターゲット・ファイルがシステム上で利用できない場合、このようなエラーが(ファイルパス自体が異なる場合があります)が発生します。それが作成されていることを確認し、または監視のためにそれを追加しないようにsshguard構成を更新します。
OpenRC 上で syslog-ng を動かしているシステムでは、syslog-ng.conf に以下の追記を行うだけで十分です:
/etc/syslog-ng/syslog-ng.conf
auth.log ファイルを作成するlog { source(src); destination(messages); }; log { source(src); destination(console_all); }; destination authlog {file("/var/log/auth.log"); }; filter f_auth { facility(auth); }; filter f_authpriv { facility(auth, authpriv); }; log { source(src); filter(f_authpriv); destination(authlog); };
変更を有効にするために、設定を再読み込みしてください:
root #
rc-service syslog-ng reload
関連項目
- Fail2ban — a system denying hosts causing multiple authentication errors access to a service.
- Iptables — a program used to configure and manage the kernel's netfilter modules.
外部の情報
sshguard documentation はアプリケーションをさらに調整するのに必要なすべての情報を提供しています。