Linux, 工作, 生活, 家人

Gentoo, Linux, Network, Security, UNIX

[Gentoo] 用 SSHGUARD 保護 openssh daemon

之前韓狗的 IP 一直在 try 我管的機器, 覺得很煩
再加上最近有一台機器被莫名其妙的破台了, 找來找去都找不到問題在那邊
我猜可能是被 ssh 攻擊,
所以想對 ssh 做一些簡單的防護. 找了一下. 看起來 sshguard 還算是很多人用的

首先是下載 sshguard

接下來是解壓縮

# tar xf sshguard-0.91.tar.bz2
# cd sshguard-0.91

設定, 因為我們用的是 Linux , 所以要用 iptables

# ./configure –with-firewall=iptables

設定完之後, compile 和 install

# make
# make install

設定 syslog-ng, 編輯 /etc/syslog-ng/syslog-ng.conf
加上這五行, 原來 Document 的 Rule 有點小問題, sshguard 會找不到.

# sshguard
filter f_authpriv { facility(auth, authpriv); };
destination authlog { file(“/var/log/auth.log”); };
log { source(src); filter(f_authpriv); destination(authlog); };
destination sshguardproc { program(“/usr/local/sbin/sshguard”); };
log { source(src); filter(f_authpriv); destination(sshguardproc); };

重新啟動 syslog-ng

# /etc/init.d/syslog-ng restart

為了確定 sshguard 更有效率, 要關掉 sshd 的 DNS 反查功能
編輯 /etc/ssh/sshd_config , 打開

UseDNS no

重新啟動 sshd

#/etc/init.d/sshd restart

最後就是替 iptables 加上chain

# iptables -N sshguard
# iptables -I INPUT -p tcp –dport 22 -j sshguard

儲存並重新啟動

# /etc/init.d/iptables save
# /etc/init.d/iptables restart

現在拿其他台機器測試一下, 在 /var/log/auth.log 會看到如下的字樣

Apr 27 16:23:45 null sshguard[2969]: Matched IP address 140.xxx.xx.xx
Apr 27 16:23:45 null sshguard[2969]: Blocking 140.xxx.xx.xx: 4 failures over 197 seconds.
Apr 27 16:23:45 null sshguard[2969]: Running command “/sbin/iptables”

查看 iptables 的 sshguard rule

Chain sshguard (1 references)
 pkts bytes target     prot opt in     out     source               destination
   15  1412 DROP       0    —  *      *       140.xxx.xx.xx        0.0.0.0/0

大功告成.

sshguard 算是很簡單的. 連 daemon 都不用弄, syslog-ng restart 就可以了.

ref.

[Tags] Gentoo, sshguard , openssh, iptables [/Tags]

2 留言

  1. Kasim

    其實SSHGuard有overlay 的…sunrise 那個

  2. San-Yuan Chen

    http://ylchang.blogspot.com/2007/04/sshguard-helps-to-prevent-ssh-brute.html

    ylchang 長輩有寫 一篇可供長輩參考……

回覆留言對象 取消回覆