FreeBSD下的ssh安全防護

來源:互聯網
上載者:User

 在查看freebsd日誌的時候總免不了在auth.log中發現大量的ssh使用者名稱掃描,說實話,這幫人實在無聊,就算我使用者名稱和密碼都告訴他了他一樣登陸不了,何必呢?本文將說說freebsd下的ssh安全我們可以做些什麼。按本文操作,不敢說你的ssh就100%安全,不過至少我們可以更安全點。下面是日誌的一段:

Dec 13 10:16:30 ddd sshd[46500]: Invalid user raimundo from 59.124.60.69
Dec 13 10:17:49 ddd sshd[46512]: Invalid user alexandre from 59.124.60.69
Dec 13 10:20:18 ddd sshd[46523]: Did not receive identification string from 59.124.60.69
Dec 13 10:21:10 ddd sshd[46524]: Invalid user a from 59.124.60.69
Dec 13 10:22:06 ddd sshd[46541]: Invalid user mercedes from 59.124.60.69
Dec 13 10:23:26 ddd sshd[46545]: Invalid user raimundo from 59.124.60.69
Dec 13 10:23:36 ddd sshd[46547]: Invalid user raimundo from 59.124.60.69
Dec 13 10:23:40 ddd sshd[46549]: Did not receive identification string from 59.124.60.69
Dec 13 10:23:49 ddd sshd[46550]: Invalid user raimundo from 59.124.60.69
Dec 13 10:24:25 ddd sshd[46552]: Invalid user raimundo from 59.124.60.69
Dec 13 10:24:33 ddd sshd[46554]: Invalid user raimundo from 59.124.60.69
Dec 13 10:24:37 ddd sshd[46556]: Did not receive identification string from 59.124.60.69
首先,如果你採用預設的驗證(pam)登入方式,且允許root登陸,如果不做其他安全防護的話那麼你的伺服器被攻入的機率非常高。現在暴力破解軟體非常之多,想想,以現在電腦的效率,10位以內的密碼你認為多少時間能被算出來?況且很多人的密碼沒有達到十位。

一、無論如何,我建議fb的伺服器開啟防火牆ipfw,iptables什麼的都行,看個人習慣。以ipfw為例:我的原則是沒用的連接埠全部封了,對於進入的連結全部採取限制,比如ssh對於同一個ip我一般只允許有一條連結,這樣可以降低暴力破解的速度節省伺服器資源:

01500 allow tcp from any to me dst-port 22 in via em0 setup limit src-addr 1

二、禁止root登陸

#ee /etc/ssh/sshd_config

Port 22  #這裡的連接埠可根據需要修改,不過要注意防火牆也要開啟其連接埠
Protocol 2
PermitRootLogin no  #這將禁止root直接登入
# Authentication:
PermitRootLogin no  #以下相同顏色的幾行將允許採用密匙驗證
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
# Change to yes to enable built-in password authentication.
PasswordAuthentication no
PermitEmptyPasswords no
# Change to no to disable PAM authentication

三、採用密匙登陸

這在操作上要及其小心以免把自己關注外面。原則上等密匙登陸成功後再關閉pam登陸模式(openssh預設的驗證方式)。製作密匙的過程如下:

登陸伺服器切換到你需要登陸的使用者名稱,然後執行如下命令:

#ssh-keygen -t rsa -b 1024
執行上面命令後將問你密匙儲存位置和密碼。最後將在使用者的家庭目錄下的.ssh/下產生一個公匙(id_rsa.pub)和一個私匙(id_rsa)
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ezool/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ezool/.ssh/id_rsa.
Your public key has been saved in /home/ezool/.ssh/id_rsa.pub.

上面的過程你可以不輸入密碼,那樣只要載入密匙就登陸伺服器,不過不建議那樣做。產生後你需要切換到ssh密匙目錄下修改公匙檔案名稱
#cd /home/test/.ssh  #test改成你產生密匙的使用者名稱
#cat id_rsa.pub > authorized_keys #也可以用 mv id_rsa.pub authorized_keys直接修改檔案名稱
#chmod 600 authorized_keys #僅供本人讀寫
#rm id_rsa.pub #移除剛產生的公匙檔案

接著你需要把私匙下載下來:
#cat id_rsa > /www/myweb/mykey.ppk #注意路勁哦,這裡由於是承接上面,因此使用者還在.ssh目錄下
#rm id_rsa
通過ftp把密匙下載下來,然後需要注意,這裡下載下來的密匙還沒辦法直接用於PuTTY,需要通過puttygen轉換

啟用putty key generator,點擊load載入剛下載下來的私匙,這時他會提示轉換,只要在save private key就可以,這樣這個檔案就能用。

登陸的時候注意左邊的ssh下面有個 Auth,通過這個載入私匙,然後就可以登入。如果登陸成功,這這時可以關閉預設的pam驗證方式

#ee  /etc/ssh/sshd_config 修改以下的兩行
ChallengeResponseAuthentication no
UsePam no
#/etc/rc.d/sshd restart #重啟sshd使配置生效
四、禁止ssh暴力破解

我們採用密匙驗證方式後,除非你的私匙、使用者名稱、密碼泄露,否則不管別人掃描你都不用擔心了。唯一不爽的是天天有人掃描,這極大浪費了伺服器資源,我們可以通過軟體來禁止別人無故掃描。基本工作原理是:
讀取auth.log檔案,從中匹配使用者登入失敗使用者,如果連續幾次登陸失敗,則把ip加入denyhost或者防火牆,或者斷開該ip的連結,這樣使得他的掃描無法繼續下去。相關的軟體如:fail2ban、Sshguard、DenyHosts等,詳細的以後會介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.