不管是架设好 Linux 跟 FreeBSD 通常都会有国外的 hacker 来 try 机器的 SSH 账号密码,我想这是很正常的,网络上也提供很多方法来分析 Log 檔,FreeBSD:/var/log/auth.log,我可以分析档案,然后再利用 firewall like ipfw or pf 的方式来达到,或者是利用写到 /etc/hosts.deny 来达到 Block 的功能,网络上有很多工具可以用,例如 DenyHosts,sshguard 或者是 sshit,可以参考我之前写的一篇利用 sshit 来达到阻挡 ssh 使用者,然而今天来介绍一下 DenyHosts 再 FreeBSD 底下如何设定,还蛮方便的。
首先利用 ports 安装,DenyHosts 是利用 python 的程序语言写出来的:
Port: denyhosts-2.6_2
Path: /usr/ports/security/denyhosts
Info: Script to thwart ssh attacks
Maint: janos.mohacsi@bsd.hu
B-deps: python25-2.5.2_3
R-deps: python25-2.5.2_3
WWW: http://denyhosts.sourceforge.net/
/* 利用 ports 安装 */
cd /usr/ports/security/denyhosts; make install clean
安装好之后接下来设定系统
#
# 在 /etc/rc.conf 加入 denyhosts_enable="YES"
#
denyhosts_enable="YES"
#
# 编辑 /etc/hosts.allow 加入
#
sshd : /etc/hosts.deniedssh : deny
sshd : ALL : allow
#
# 假如 /etc/hosts.deniedssh 不存在,那就新增一个
#
touch /etc/hosts.deniedssh
接下来就是设定 /usr/local/etc/denyhosts.conf
#
# 设定需要分析 Log 档案位置
#
# FreeBSD or OpenBSD
SECURE_LOG = /var/log/auth.log
# Redhat or Fedora Core:
#SECURE_LOG = /var/log/secure
# SuSE:
#SECURE_LOG = /var/log/messages
#
# 我们要阻挡的 IP 写入到的档案
#
HOSTS_DENY = /etc/hosts.deniedssh
#
# 我们要清除 hosts.deniedssh 里面的 entries
# 'm' = minutes
# 'h' = hours
# 'd' = days
# 'w' = weeks
# 'y' = years
# 格式:i[dhwmy] i 是数字
PURGE_DENY = 5d
#
# 我们要阻挡的服务:sshd
#
BLOCK_SERVICE = sshd
#
# 如果该账号不存在 /etc/passwd 尝试超过5次失败,就阻挡该ip登入此服务
#
DENY_THRESHOLD_INVALID = 5
#
# 如果该账号存在 /etc/passwd 尝试超过10次失败,就阻挡该ip登入此服务
#
DENY_THRESHOLD_VALID = 10
#
# 阻挡 root 账号错误登入次数,不过这对 FreeBSD 没影响
# 因为 FreeBSD 架设完成,是不能远程利用 root 登入的
DENY_THRESHOLD_ROOT = 1
#
# 把 deny 的 host 或者是 ip 纪录到 Work_dir 里面
# 尽量把这数据夹改变到 root 账号以外不能存取的地方
#
WORK_DIR = /usr/local/share/denyhosts/data
#
# 设定 deny host 写入到该数据夹
#
DENY_THRESHOLD_RESTRICTED = 1
#
# 当 DenyHOts 启动的时候写入 pid,已确保服务正确启动,防止同时启动多个服务
