LoginSignup
0
0

More than 1 year has passed since last update.

MiracleLinux8.4/8.6/8.7でDenyHosts

Last updated at Posted at 2023-02-02

はじめに

Redhat8 になってから、DenyHostsは、リポジトリから消え、皆さん、Fail2Banにシフトされたのかなと思いますが、他人と不正アクセス情報が共有できる所が気に入っており、Redhat8で使う方法はない物かと、気になっていたので、実践してみました。

現バージョンの確認

こちらで、最新を確認すると、DenyHosts3.0が最新のようです。
が、結論から言うとMiracleLinux8.4では、エラーがでて、動きませんでした。
深追いはせず、DenyHosts2.10をインストールし、正常に動作したので備忘録として記載してみます。

インストール

まずは、ダウンロード

wget https://nchc.dl.sourceforge.net/project/denyhosts/denyhosts/2.10/denyhosts-2.10.zip

移動して解凍

mv denyhosts-2.10.zip /usr/local/
cd /usr/local/
unzip denyhosts-2.10.zip

Readme 関連を確認します

cd denyhosts-2.10
ls
CHANGELOG.txt  Makefile   build                denyhosts.conf     scripts
DenyHosts      PKG-INFO   daemon-control       denyhosts.py       setup.py
LICENSE.txt    README.md  daemon-control-dist  denyhosts.service  tests
MANIFEST.in    TODO       denyhosts.8          plugins

cat README.md
省略

README.mdにインストールの方法が書かれています。
Redhat8でも動作可能なのかも確認します。

cat denyhosts.conf 
省略
IPTABLES = /sbin/iptables
省略

噂には聞いていましたが、iptablesを発見、iptablesコマンドで使用できそうです。
少なくともrpm提供されているDenyHosts2.6には、この設定がありませんでした。

Firewalldとiptablesの共存も検討していましたが、
DenyHostsの他にSWATCHも使いたいので、iptablesを採用します。
ということで、Firewalledを停止して、iptablesを使用します。

Firewalldを停止、iptablesをインストール

systemctl disable firewalld
systemctl stop firewalld

dnf -y install iptables-services
systemctl start iptables

動作に必要なpython2のインストール

dnf -y install python2

DenyHostsのインストール

python2 setup.py install

denyhosts.confを編集

vi /etc/denyhosts.conf

SECURE_LOG = /var/log/messages
#SECURE_LOG = /var/log/auth.log

以下は、適宜設定をしてください。

#PURGE_DENY =
PURGE_DENY = 5d

BLOCK_SERVICE = ALL
#BLOCK_SERVICE  = sshd

#SYNC_SERVER = http://xmlrpc.denyhosts.net:9911
SYNC_SERVER = http://xmlrpc.denyhosts.net:9911

#SYNC_INTERVAL = 1h
SYNC_INTERVAL = 5m     # ←テストの時のみ設定(5分間隔でサーバーに接続)

#SYNC_UPLOAD = no
SYNC_UPLOAD = yes

#SYNC_DOWNLOAD = no
SYNC_DOWNLOAD = yes

サービスとして起動される為の設定

vi /usr/local/denyhosts-2.10/daemon-control
#!/usr/bin/env python
#!/usr/bin/env python2

#DENYHOSTS_BIN   = "/usr/sbin/denyhosts"
DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"

#PYTHON_BIN      = "/usr/bin/env python"
PYTHON_BIN      = "/usr/bin/env python2"
cd /usr/local/denyhosts-2.10/
cp daemon-control-dist daemon-control

ファイルを新規作成します。

vi /usr/lib/systemd/system/denyhosts.service

[Unit]
Description=SSH log watcher
Before=sshd.service

[Service]
Type=forking
#ExecStartPre=/bin/rm -rf /var/lib/denyhosts
ExecStart=/usr/local/denyhosts-2.10/daemon-control start
ExecStop=/usr/local/denyhosts-2.10/daemon-control stop
#PIDFile=/var/lib/denyhosts/denyhosts.pid

[Install]
WantedBy=multi-user.target

下準備

touch /etc/hosts.deny

動作確認

systemctl start denyhosts

tail -f /var/log/denyhosts

エラーが出ていなければOK
5分後に、サーバーと同期します。

sync        : ERROR    [Errno 2] No such file or directory: '/var/lib/denyhosts/sync-timestamp'

は、無視して問題ありません。初回ファイルがないので、作成されます。
次の5分後には、エラーがなくなります。

2023-02-02 17:51:46,344 - sync        : INFO     received 1 new host
2023-02-02 17:51:46,345 - denyhosts   : INFO     received new hosts: ['5.161.182.32']
2023-02-02 17:51:46,345 - denyhosts   : INFO     Creating new firewall rule /sbin/iptables -I INPUT -s 5.161.182.32 -j DROP

正常に動作しています。

確認

iptables -L
省略
DROP       all  --  net-93-145-70-90.cust.vodafonedsl.it  anywhere
省略

cat /etc/hosts.deny
# DenyHosts: Thu Feb  2 17:51:46 2023 | ALL: 5.161.182.32
ALL: 5.161.182.32

Redhat8では、TCP Wrapperが無効になっていますので、/etc/hosts.denyは、記録用になると思います。サーバー再起動時に利用すると良いかもですね。

テスト起動を停止、設定を変更

systemctl stop denyhosts

vi /etc/denyhosts.conf
SYNC_INTERVAL = 1h
#SYNC_INTERVAL = 5m

本番動作 起動と自動起動

systemctl start denyhosts
systemctl enable denyhosts

おわりに

MiracleLinux8.4でDenyHostsが動作しました。
省略しているところもありますが、ほぼ動作すると思います。
起動しないときは、エラーログなど各自で調べてみてください。
Firewalldで運用したい方も、基本動作するはずです。

CentOS7環境をMiracleLinux8.4に移行すべく、少しずつお勉強中です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0