Skip to content

Instantly share code, notes, and snippets.

@kun432
Created October 31, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kun432/45cc17cdf46b8bd8b1ba718f7fbc3274 to your computer and use it in GitHub Desktop.
Save kun432/45cc17cdf46b8bd8b1ba718f7fbc3274 to your computer and use it in GitHub Desktop.
今更denyhosts

epelにパッケージはもうないので、直接。

$ rpm -Uvh https://rpmfind.net/linux/dag/redhat/el7/en/x86_64/dag/RPMS/denyhosts-2.6-5.el7.rf.noarch.rpm

設定ファイルはこれだけ。

$ tree /etc/denyhosts
/etc/denyhosts
└── denyhosts.cfg

最低限設定すべきは以下あたり

DAEMON_SLEEP = 30s
DAEMON_PURGE = 1m

PURGE_DENY = 1m
PURGE_THRESHOLD = 2

DENY_THRESHOLD_INVALID = 3
DENY_THRESHOLD_VALID = 6
RESET_ON_SUCCESS = yes
  • DAEMON_SLEEP
    • 多分ポーリング間隔
  • DAEMON_PURGE

  • DENY_THRESHOLD_INVALID
    • 存在しないユーザへのアクセスでブロックするしきい値
  • DENY_THRESHOLD_VALID = 10
    • 存在するユーザへのアクセスでブロックするしきい値
  • RESET_ON_SUCCESS = yes
    • 正しいアクセスならば失敗回数がリセットされる
  • PURGE_DENY
    • 拒否したあとで解除するまでの期間。m/h/d/w/yで期限指定。1wとか。何も指定しなければ恒久
  • PURGE_THRESHOLD
    • 自動解除を何回行うか。これをすぎると恒久的になる。
    • 指定しなければこの機能はオフになる。

起動

$ sudo systemctl start denyhosts

パスワード認証無効で一旦テストしてみる。

存在しないユーザでやってみる

$ ssh foo@192.168.240.21
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

$ ssh foo@192.168.240.21
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

$ ssh foo@192.168.240.21
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

$ ssh foo@192.168.240.21
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

登録された。そのあと1.5mで解除されている。

2021-10-31 15:34:56,101 - denyhosts   : INFO     new denied hosts: ['192.168.240.22']
2021-10-31 15:35:26,113 - denyfileutil: INFO     purging entries older than: Sun Oct 31 15:34:26 2021
2021-10-31 15:35:26,114 - denyfileutil: INFO     num entries purged: 0
2021-10-31 15:36:26,167 - denyfileutil: INFO     purging entries older than: Sun Oct 31 15:35:26 2021
2021-10-31 15:36:26,168 - loginattempt: INFO     purging_hosts: ['192.168.240.22']
2021-10-31 15:36:26,169 - denyfileutil: INFO     num entries purged: 1

2回失敗すると解除されなくなった。/usr/share/denyhosts/data/あたりで管理している様子。

/usr/share/denyhosts/data/
├── hosts
├── hosts-restricted
├── hosts-root
├── hosts-valid
├── offset
├── purge-history
├── suspicious-logins
├── users-hosts
├── users-invalid
└── users-valid

ホワイトリストは/etc/hosts.allowにかいておけばよい。

sshd:127.0.0.1
sshd:192.168.240.

もしくはWORKDIR/allowed-hostsの様子。多分ここに書いておくと登録されなくなるということだと思う。こちらは正規表現でかく模様。CIDR指定はできなさそう。

192.168.240.*

hosts.allowかallowed-hosts、どっちがいいかは微妙だな。

登録されたものの解除はとてもめんどくさい

http://denyhosts.sourceforge.net/faq.html#3_19

Stop DenyHosts
Remove the IP address from /etc/hosts.deny
Edit WORK_DIR/hosts and remove the lines containing the IP address. Save the file.
Edit WORK_DIR/hosts-restricted and remove the lines containing the IP address. Save the file.
Edit WORK_DIR/hosts-root and remove the lines containing the IP address. Save the file.
Edit WORK_DIR/hosts-valid and remove the lines containing the IP address. Save the file.
Edit WORK_DIR/user-hosts and remove the lines containing the IP address. Save the file.
(optional) Consider adding the IP address to WORK_DIR/allowed-hosts
Start DenyHosts
@kun432
Copy link
Author

kun432 commented Oct 31, 2021

パスワード認証不可(鍵のみの場合)で、存在するユーザでやってみたけど、引っかからなかった。

  • パスワード認証有効なら存在するユーザへのアクセスもチェック
  • 存在しないユーザへのアクセスは基本的にチェックされる

という感じ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment