Linux

/etc/hosts.deny 不應該在它命中 ssh 日誌之前攔截它嗎?

  • November 8, 2012

我在我的 ubuntu 10.04.04 伺服器上使用 /etc/hosts.deny 和 ufw 的組合。大多數時候,我看到來自 .com.cn 域中機器的 ssh 嘗試失敗,報告如下:

Failed logins from:
112.114.63.139 (139.63.114.112.broad.km.yn.dynamic.163data.com.cn): 1 time

但是,在 /etc/hosts.deny 中,我有這個規則:

ALL: .com.cn

這不應該在連接到 ssh 之前就阻止連接嗎?我已經通過阻止我的家用機器對其進行了測試,它肯定會在我收到登錄提示之前立即拒絕我的連接(是的,我已經將我的 ssh 密鑰移開,因此不涉及這些密鑰)。

這是否按預期工作?

編輯: James Sneeringer 促使我更仔細地查看日誌,也許我明白為什麼會發生這種情況。來自 auth.log:

Nov  5 09:38:40 mymachine sshd[22864]: warning: /etc/hosts.deny, line 21: can't verify hostname: getaddrinfo(139.63.114.112.broad.km.yn.dynamic.163data.com.cn, AF_INET) failed
Nov  5 09:38:44 mymachine sshd[22864]: reverse mapping checking getaddrinfo for 139.63.114.112.broad.km.yn.dynamic.163data.com.cn [112.114.63.139] failed - POSSIBLE BREAK-IN ATTEMPT!
Nov  5 09:38:45 mymachine sshd[22864]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=112.114.63.139  user=root
Nov  5 09:38:47 mymachine sshd[22864]: Failed password for root from 112.114.63.139 port 37245 ssh2
Nov  5 09:38:47 mymachine sshd[22866]: Connection closed by 112.114.63.139

這對我來說意味著如果 sshd 不確定 IP->name 查找,那麼它會謹慎行事並且不會阻止該主機。是對的嗎?

這是 sshd 所期望的,因為它直接連結到 libwrap,因此 sshd 實際上是執行主機檢查的對象。如果要在呼叫 sshd 之前阻止連接,則需要在其前面放置一些東西來處理連接嘗試,然後再將其傳遞給 sshd。幾個選擇是:

  • 在inetd(或xinetd)下執行sshd。這將允許您呼叫 sshd 作為 tcpd 的參數,而 tcpd 最終會執行實際的主機檢查。
  • 在 xinetd 下執行 sshd,它具有提供類似於 /etc/hosts.allow 和 /etc/hosts.deny 的功能的服務選項only_fromno_access

但是,sshd 聯機幫助頁不鼓勵使用這些方法:

-i      Specifies that sshd is being run from inetd(8).  sshd is normally
        not run from inetd because it needs to generate the server key
        before it can respond to the client, and this may take tens of
        seconds.  Clients would have to wait too long if the key was
        regenerated every time.  However, with small key sizes (e.g. 512)
        using sshd from inetd may be feasible.

使用 iptables 或在伺服器前面放置防火牆似乎是一個不錯的選擇,但大多數都不執行任何名稱解析,因此您只能通過 IP 地址控制訪問。這不一定是一件壞事,但它對你想要完成的事情沒有幫助。

引用自:https://serverfault.com/questions/444431