作者 主題: [整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )  (閱讀 119701 次)

0 會員 與 1 訪客 正在閱讀本文。

日京三子

  • 全區板主
  • 俺是博士!
  • *****
  • 文章數: 8831
    • 檢視個人資料
    • http://www.24online.cjb.net
原本的文章來自於這裡.

小弟的主機最近一直持續的遭受來自中國大陸那邊對台灣網路的攻擊, 利用ssh服務, 加上針對RedHat系系統各項服務帳號所進行的攻擊. 日前我所整理出來的攻擊IP如下:
    220.194.0.0
    211.34.0.0
    63.170.0.0
    67.18.0.0
    221.253.0.0
    .
    .
    族繁不及備載

現在這種攻擊有幾種特點, 1是會利用帳號列表展開攻擊, 利用一系列的英文常用ID (別懷疑, 你看歐美影片時常出現的名字幾乎都包括在內, 像Abel, alin 等等)
2. 還有會利用系統服務程式帳號的, 例如小弟以下的secure log這種:
代碼: [選擇]
Jul 29 15:18:39 alpha sshd[26431]: Failed password for bin from 220.194.55.126 port 59711 ssh2
Jul 29 15:18:43 alpha sshd[26433]: Failed password for daemon from 220.194.55.126 port 60574 ssh2
Jul 29 15:18:47 alpha sshd[26435]: Failed password for adm from 220.194.55.126 port 60979 ssh2
Jul 29 15:18:50 alpha sshd[26437]: Failed password for lp from 220.194.55.126 port 33309 ssh2
Jul 29 15:18:54 alpha sshd[26439]: Failed password for sync from 220.194.55.126 port 34090 ssh2
Jul 29 15:18:58 alpha sshd[26441]: Failed password for shutdown from 220.194.55.126 port 34899 ssh2
Jul 29 15:19:02 alpha sshd[26443]: Failed password for halt from 220.194.55.126 port 35290 ssh2
Jul 29 15:19:05 alpha sshd[26445]: Failed password for mail from 220.194.55.126 port 35795 ssh2
Jul 29 15:19:09 alpha sshd[26447]: Failed password for news from 220.194.55.126 port 36567 ssh2
Jul 29 15:19:13 alpha sshd[26449]: Failed password for uucp from 220.194.55.126 port 37375 ssh2
Jul 29 15:19:16 alpha sshd[26451]: Failed password for operator from 220.194.55.126 port 37462 ssh2
Jul 29 15:19:20 alpha sshd[26453]: Failed password for games from 220.194.55.126 port 38272 ssh2
Jul 29 15:19:24 alpha sshd[26455]: Failed password for gopher from 220.194.55.126 port 39041 ssh2
Jul 29 15:19:28 alpha sshd[26457]: Failed password for ftp from 220.194.55.126 port 39849 ssh2
Jul 29 15:19:32 alpha sshd[26459]: Failed password for nobody from 220.194.55.126 port 40237 ssh2
Jul 29 15:19:36 alpha sshd[26461]: Failed password for vcsa from 220.194.55.126 port 40747 ssh2
Jul 29 15:19:39 alpha sshd[26463]: Failed password for apache from 220.194.55.126 port 41525 ssh2
Jul 29 15:19:41 alpha sshd[26465]: Invalid user webadmin from 220.194.55.126
Jul 29 15:19:43 alpha sshd[26465]: Failed password for invalid user webadmin from 220.194.55.126 port 42377 ssh2
Jul 29 15:19:44 alpha sshd[26467]: Invalid user popa3d from 220.194.55.126
Jul 29 15:19:47 alpha sshd[26467]: Failed password for invalid user popa3d from 220.194.55.126 port 42780 ssh2
Jul 29 15:19:50 alpha sshd[26469]: Failed password for sshd from 220.194.55.126 port 43314 ssh2
Jul 29 15:19:54 alpha sshd[26471]: Failed password for mailnull from 220.194.55.126 port 44112 ssh2
Jul 29 15:19:58 alpha sshd[26473]: Failed password for smmsp from 220.194.55.126 port 44952 ssh2
Jul 29 15:20:01 alpha sshd[26475]: Failed password for named from 220.194.55.126 port 45045 ssh2
Jul 29 15:20:05 alpha sshd[26479]: Failed password for rpc from 220.194.55.126 port 45891 ssh2
Jul 29 15:20:09 alpha sshd[26481]: Failed password for rpm from 220.194.55.126 port 46692 ssh2
Jul 29 15:20:12 alpha sshd[26483]: Failed password for pcap from 220.194.55.126 port 47121 ssh2
Jul 29 15:20:16 alpha sshd[26485]: Failed password for mysql from 220.194.55.126 port 47648 ssh2

可以很明顯的看得出來, 這是針對大約在去年年底, RedHat公司所附加的sshd版本漏洞所展開的系統攻擊.

為了加強管制, 除了限制root帳號登入這種必備的手段之外, 還參照了前輩們的做法, 從三部分來著手:

1. sshd_config 部分
    這是一個很簡單的設定手段, 除了限制root之外, 還順便限制連線數, 如下:
代碼: [選擇]
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 2

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
PermitEmptyPasswords no
PasswordAuthentication yes

# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes
這些設定所使用的英文都很簡單, 小弟就不多加介紹.
[/list]
2. 利用hosts.deny與hosts.allow 設定可以連線的ip:
    先在 /etc/hosts.allow 裡面, 設定哪些ip是可以連線的, 例如前輩們所舉例的:
代碼: [選擇]
sshd:140.128.1.123 , 接著, 再到 /etc/hosts.deny 裡面設定拒絕所有ip連線:
代碼: [選擇]
sshd:all 於是, 就只有在 /etc/hosts.allow 列表裡面的IP, 才可以使用ssh 來主機連線, 其餘都會被拒絕掉.[/list]


3. 利用pam機制:
最上面那文章裡面提到, 利用pam機制, 讓sshd接到連線時, 先檢查是否在黑名單之中的做法. 方法很簡單, 就是在/etc/pam.d/ssh裡面增加下面的設定:
    auth       required     pam_stack.so service=system-auth
auth       required     /lib/security/pam_listfile.so item=user sense=deny file=/etc/sshd_user_deny_list onerr=succeed
account    required     pam_nologin.so
account    required     pam_stack.so service=system-auth
password   required     pam_stack.so service=system-auth
session    required     pam_stack.so service=system-auth
session    required     pam_loginuid.so
[/list]
(特別注意, 上面設定都是同一行, 並無斷行)
特別新增的是紅色字的部分. 當你的名字在該檔案( /etc/sshd_user_deny_list )之中時, 就無法利用ssh來登入系統.

[/list]

利用以上三個方法, 可以讓你的主機在提供sshd連線的時候, 安全度提升不少. 但切記, 經常的檢查與更新, 減少不必要的服務, 增加許多的安全限制, 才是主機管理的最佳策略!  :wink:
哈克不愛的多合一輸入平台----->新香草口味
過去的時間不斷流逝,抹去的眼淚已成追憶;
乾枯的雙手無力阻止,再會了我遠去的曾經。

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #1 於: 2005-07-30 10:40 »
如沒特殊原因, 設定允許連線的使用者會比設定不允許的要好多了.
一般用會 sshd 的應該只有少數人.

wbyron

  • 懷疑的國中生
  • **
  • 文章數: 81
  • 性別: 男
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #2 於: 2005-09-06 10:03 »
請問:

我在 hosts.allow 用,
代碼: [選擇]
sshd:123.45.67.0/24
這樣的語法好像行不通,是我錯了嗎?

leafmouse

  • 懷疑的國中生
  • **
  • 文章數: 67
    • 檢視個人資料
    • 小老鼠的網站
幾個提升ssh安全性的設定
« 回覆 #3 於: 2005-09-08 00:17 »
以下是小弟之前整理的文章,也提供大家參考,若有錯誤的話歡迎指正 ^^

以前在看Log的時侯也發現很多IP會沒事太閒嘗試ssh登入你的主機or try 帳號
便想做一點限制,通常我只有開放家裡操作那台PC的IP和公司的Proxy Server
or IP分享器的IP,其餘的IP address全都擋掉

1. 限制root不能ssh登入

預設允許root帳號登入,這裡將#號去掉並改成不允許root用ssh登入

代碼: [選擇]
編輯    /etc/ssh/sshd_config
將    #PermitRootLogin Yes
改成  PermitRootLogin no



2. tcp_wrappers 套件限制連線的範圍

設定可以連線的IP

代碼: [選擇]
編輯  /etc/hosts.allow
新增  sshd: 192.168.1.100 , 61.132.220.17 : allow


設定拒絕所有的連線,這樣一來便只有/etc/hosts.allow允許的IP才能連入使用

代碼: [選擇]
編輯  /etc/hosts.deny
新增  sshd: ALL : Deny



3. Pam 認證機制只允許某些帳號使用ssh連入

代碼: [選擇]

編輯  /etc/pam.d/sshd
新增  auth   required   pam_listfile.so item=user sense=allow file=/etc/ssh/allow_list onerr=succeed

代碼: [選擇]

編輯  /etc/ssh/allow_list
新增您想允許使用ssh登入主機的帳號  p.s. 一行一個帳號喔


目前我的server在ssh的部份有做這些設定,用tcp_wrappers做IP連線的限制我覺得滿不錯用的連進我的主機時檢查為不允許的IP就直接connection refuse了,ssh只有allow_list裡的帳號才能登入,我是只設我自己在用的帳號,我想這樣應該安全性提升了一部份,也可以限制某群組才能su,架mail server的話我是沒用virutal account,所以/etc/passwd一般的帳號我也不會讓他們登入shell,如:

代碼: [選擇]
account:x:501:501::/home/account:/bin/false

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #4 於: 2005-09-08 01:02 »
auth   required   pam_listfile.so item=user sense=allow file=/etc/ssh/allow_list onerr=succeed

一般來說, 若 sense=allow 的話, 那 onerr 就不要設 succeed , 而要設為 fail .

leafmouse

  • 懷疑的國中生
  • **
  • 文章數: 67
    • 檢視個人資料
    • 小老鼠的網站
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #5 於: 2005-09-11 18:41 »
引述: "netman"
auth   required   pam_listfile.so item=user sense=allow file=/etc/ssh/allow_list onerr=succeed

一般來說, 若 sense=allow 的話, 那 onerr 就不要設 succeed , 而要設為 fail .


感謝netman前輩的指教,目前也改成onerr=fail了
pam認證的東西並不是很懂,不過之前有試過onerr=succeed
時一樣只有allow_list檔案裡的帳號才能登入所以就沒想那麼多了
有空再去找一下這些資料,謝謝您囉 ^^

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #6 於: 2005-09-11 21:44 »
呵... 別客氣, 感謝你來跟大家交流心得呢!

filelist module 的 onerr 是指讀取 file 時碰到錯誤.
然後其後的 succeed 與 fail 是指前面的 sense 是否要達成, 而不是指 pam 返回的結果.
我所提到的 sense=allow 然後 onerr=fail 只是一般的通用設法. 具體的環境或有不同.

既然大家對 ssh 的控管有興趣, 那我再補充一個控管方法:
1) 將 sshd 的 firewall 連線全部 block 掉.
2) 然後在 httpd 那設一個 directory, 可設 ssl+htpasswd+allow/deny control,
然後在目錄內寫一個 php 將 browser ip 記錄於一份 .txt 文字檔裡.
視你的轉寫能力, 你可自動抓取 browser 端的 IP, 也可讓 browser 端傳入參數來指定.
文字檔只有單一記錄, 每次蓋寫.
3) 修改 /etc/services , 增加一個新項目(如 xxx), 並指定一個新 port(如 1111)
4) 再用 xinetd 監聽該 port , 並啟動令一隻 script, 設定 iptables , 從 step2 的清單裡取得 IP, 為之打開 ssh 連線.
5) 設 crontab 每數分中清理 iptables 關於 ssh 連線的規則. 這並不影響既有連線, 若逾時再連, 則重複上述.

具體的設定我這裡暫時不寫了, 看得懂的人, 一定有自己的方法的...  ^_6

damon

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 4227
    • 檢視個人資料
    • http://blog.damon.tw/
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #7 於: 2005-09-12 09:08 »
有更簡單的作法,也不用搞那麼複雜,就是只用public key做認證,也不會問密碼,不用擔心密碼洩漏,除非privite key外流,也不用作黑名單,就算帳號對,key不對,你的sshd會直接拒絕連線
你也不用每天手動maintain什麼黑名單這種東西,試試看吧,十分鐘內就可以做完了

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #8 於: 2005-09-12 16:06 »
嗯...

rsa auth 的方式, 對 ssh 或 ssl 本身的漏洞沒啥幫助.
也不能排除 client 機器遭"誤用", 比方一台被入侵, 其它全部受遷連.
之所以搞那麼複雜, 就是為了在你還沒修補之前給你"多"一份保護.

做 security, 最能應驗如下一句話:
--- 勿以善小不為, 勿以惡小為之!

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #9 於: 2005-09-15 09:43 »
再補一招:

先寫一個 shell script:
代碼: [選擇]
#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

LOG_FILE=/var/log/secure
KEY_WORD="Illegal user"
KEY_WORD1="Failed password for root"
PERM_LIST=/etc/firewall/bad.list.perm
LIMIT=5
MAIL_TO=root
IPT_SAV="$(iptables-save)"
bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs)
bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs)
bad_list="$bad_list $bad_list1"

for i in $(echo -e "${bad_list// /\n}" | sort -u)
do
        hit=$(echo $bad_list | egrep -o "$i" | wc -l)
        [ "$hit" -ge "$LIMIT" ] && {
                echo "$IPT_SAV" | grep -q "$i .*-j DROP" || {
                        echo -e "\n$i was dropped on $(date)\n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO
                        iptables -I INPUT -s $i -j DROP
                }
                egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST
        }
done


測試 script 並存好, 並記下路逕, 如 /etc/firewall/block_ssh.sh

然後在 /etc/hosts.allow 寫入該路逕:
sshd: ALL: spawn ( /etc/firewall/block_ssh.sh )& : ALLOW

這樣, 那些亂 try SSH 的家夥, 頂多能試 5 次(LIMIT 可調整), 然後就給 BLOCK 掉了.

此外, 在 PERM_LIST 的 ip, 也可提供給 iptables 的初始 script , 來個永久性封閉:
代碼: [選擇]
for i in $(< $PERM_LIST)
do
        /sbin/iptables -I INPUT -s $i -j DROP
done

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #10 於: 2005-09-15 09:52 »
還有, 你想知道有哪些人對你做 full range port scan 的話:

打開 firewall 的 tcp/79
還有 xinetd 的 finger, 修改 /etc/xinetd.d/finger
代碼: [選擇]
service finger
{
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/sbin/in.fingerd
        disable         = no
}


然後在 /etc/hosts.allow 加入一行:
代碼: [選擇]
in.fingerd: ALL : spawn ( echo -e "\nWARNING %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY
這裡, 我只是設為發信給 root.
事實上, 你可修改為起動 firewall 將 %a 這個傳回值給 ban 掉也行.
不過, 對方要是有選擇性的做 port scan , 沒掃到 finger 的話, 那當然就沒用了...

security 有蠻多挺好玩的小技巧, 有空再跟大家做分享...  ^_^

phantom

  • SA 苦力組
  • 俺是博士!
  • *****
  • 文章數: 2185
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #11 於: 2005-09-15 10:51 »
引述: "netman"
再補一招:

先寫一個 shell script:
代碼: [選擇]
#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

LOG_FILE=/var/log/secure
KEY_WORD="Illegal user"
KEY_WORD1="Failed password for root"
PERM_LIST=/etc/firewall/bad.list.perm
LIMIT=5
MAIL_TO=root
IPT_SAV="$(iptables-save)"
bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs)
bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs)
bad_list="$bad_list $bad_list1"

for i in $(echo -e "${bad_list// /\n}" | sort -u)
do
        hit=$(echo $bad_list | egrep -o "$i" | wc -l)
        [ "$hit" -ge "$LIMIT" ] && {
                echo "$IPT_SAV" | grep -q "$i .*-j DROP" || {
                        echo -e "\n$i was dropped on $(date)\n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO
                        iptables -I INPUT -s $i -j DROP
                }
                egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST
        }
done


測試 script 並存好, 並記下路逕, 如 /etc/firewall/block_ssh.sh

然後在 /etc/hosts.allow 寫入該路逕:
sshd: ALL: spawn ( /etc/firewall/block_ssh.sh )& : ALLOW

這樣, 那些亂 try SSH 的家夥, 頂多能試 5 次(LIMIT 可調整), 然後就給 BLOCK 掉了.

此外, 在 PERM_LIST 的 ip, 也可提供給 iptables 的初始 script , 來個永久性封閉:
代碼: [選擇]
for i in $(< $PERM_LIST)
do
        /sbin/iptables -I INPUT -s $i -j DROP
done


這招不錯.

另外, for Debian system, LOG_FILE 要改.
代碼: [選擇]
LOG_FILE=/var/log/auth.log

用您的 firewall script 的話, 這是否要改 (-I 改為 -A)?
代碼: [選擇]
/sbin/iptables -A INPUT -s $i -j DROP
Linux 非萬能, 沒 Linux 萬萬不能.
root = God
apt-get install ultimate-horsepower

phantom

  • SA 苦力組
  • 俺是博士!
  • *****
  • 文章數: 2185
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #12 於: 2005-09-15 10:52 »
引述: "netman"
還有, 你想知道有哪些人對你做 full range port scan 的話:

打開 firewall 的 tcp/79
還有 xinetd 的 finger, 修改 /etc/xinetd.d/finger
代碼: [選擇]
service finger
{
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/sbin/in.fingerd
        disable         = no
}


然後在 /etc/hosts.allow 加入一行:
代碼: [選擇]
in.fingerd: ALL : spawn ( echo -e "\nWARNING %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY
這裡, 我只是設為發信給 root.
事實上, 你可修改為起動 firewall 將 %a 這個傳回值給 ban 掉也行.
不過, 對方要是有選擇性的做 port scan , 沒掃到 finger 的話, 那當然就沒用了...

security 有蠻多挺好玩的小技巧, 有空再跟大家做分享...  ^_^


用 snort 也不錯. ^_^
Linux 非萬能, 沒 Linux 萬萬不能.
root = God
apt-get install ultimate-horsepower

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #13 於: 2005-09-15 13:23 »
引述: "phantom"
用您的 firewall script 的話, 這是否要改 (-I 改為 -A)?
代碼: [選擇]
/sbin/iptables -A INPUT -s $i -j DROP

若前面沒有衝突的規則, 用 -A 是可以的.
但, 若有諸如 -p tcp --dport 80 -j ACCEPT 之類的,
那 -A 在後面就沒啥用了...

用 -I 是肯定可行的. -A 則未必.

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #14 於: 2005-09-16 05:56 »
好了, 我將具體做法整理如下:

http://www.study-area.org/tips/ssh_tips.htm

leafmouse

  • 懷疑的國中生
  • **
  • 文章數: 67
    • 檢視個人資料
    • 小老鼠的網站
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #15 於: 2005-09-22 01:39 »
引述: "netman"
好了, 我將具體做法整理如下:

http://www.study-area.org/tips/ssh_tips.htm


呼....果然是前輩,系統方面我想我還有許多可以研究的東西,一一
計畫研究的時間一個個去弄懂它囉,shell script、正規表示式、OpenLDAP
.....甚至更多的東西都還待小弟去研究呢,有機會再多多拜讀您和幾位先進的文章 ^^

lio.lee

  • 可愛的小學生
  • *
  • 文章數: 10
    • 檢視個人資料
    • http://epage.catcatlio.idv.tw
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #16 於: 2005-10-12 22:23 »
真的是一篇很棒的文章!
所有的大大都是一流的!
置頂真是當之無愧!

論壇就是為了這樣設立的吧!
集眾人之力,將力量發揮至極限!
希望有一天,我也能這樣貢獻所長哩!

hanky

  • 可愛的小學生
  • *
  • 文章數: 3
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #17 於: 2006-01-10 10:25 »
引述: "netman"
再補一招:

先寫一個 shell script:
代碼: [選擇]
#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

LOG_FILE=/var/log/secure
KEY_WORD="Illegal user"
KEY_WORD1="Failed password for root"
PERM_LIST=/etc/firewall/bad.list.perm
LIMIT=5
MAIL_TO=root
IPT_SAV="$(iptables-save)"
bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs)
bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs)
bad_list="$bad_list $bad_list1"

for i in $(echo -e "${bad_list// /\n}" | sort -u)
do
        hit=$(echo $bad_list | egrep -o "$i" | wc -l)
        [ "$hit" -ge "$LIMIT" ] && {
                echo "$IPT_SAV" | grep -q "$i .*-j DROP" || {
                        echo -e "\n$i was dropped on $(date)\n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO
                        iptables -I INPUT -s $i -j DROP
                }
                egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST
        }
done


測試 script 並存好, 並記下路逕, 如 /etc/firewall/block_ssh.sh

然後在 /etc/hosts.allow 寫入該路逕:
sshd: ALL: spawn ( /etc/firewall/block_ssh.sh )& : ALLOW

這樣, 那些亂 try SSH 的家夥, 頂多能試 5 次(LIMIT 可調整), 然後就給 BLOCK 掉了.

此外, 在 PERM_LIST 的 ip, 也可提供給 iptables 的初始 script , 來個永久性封閉:
代碼: [選擇]
for i in $(< $PERM_LIST)
do
        /sbin/iptables -I INPUT -s $i -j DROP
done


如果使用 fedora 的朋友,發覺使用以上的script都不能 Block IP 的話
可能是 IPV6的問題
因為IPV6會在 /var/log/secure 入面的 IP 加上 ::ffff:
那麼以上的 script 就不能運作

解決放法可於  /etc/modprobe.conf 加上
代碼: [選擇]

alias net-pf-10 off
alias ipv6 off

然後 reboot或者重新reload module(這個我不懂 coomand如何)

另外想請教各位大大
如果想定時解放被 Block 的 IP
該如何處理?
跟據以上的 script來看,如果不改動 /var/log/secure 的設定該如何?

謝謝

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #18 於: 2006-01-10 14:38 »
感謝補充!

要清掉的話, 再寫一段:
代碼: [選擇]
for i in $(< $PERM_LIST)
do
        /sbin/iptables -D INPUT -s $i -j DROP
done

hsinan

  • 懷疑的國中生
  • **
  • 文章數: 66
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #19 於: 2006-08-31 06:15 »
參考這篇文章, 個人一點小觀點 :-)

1. 考慮電腦不見的隨時在身邊, 可用的電腦不見得有安裝ssh軟體, 考慮IP不固定, 考慮必須及時動作 (power failure and need to shutdown immediately/asap...)

我改了hosts.allow and profile. 若是login from known IP, same as before. 若是from somewhere not listed in hosts.allow, 用一個簡單的script, 改變profile, 然後自動send mail 給你. 所以要是user login but NOT you, you are hacked! :-P

P.S 我的手機可以收email so I can know IMMEDIATELY...

2. 理由同上, 不可能要求all softwares are installed for all PCs you can use at any time. So I still open a telnet but will ALWAYS send me email if someone login. 要是sshd掛了, 可是人在外面, 起碼還有一個機會連回去. 不要笑! 我去年滑雪時朋友半夜三點接到電話, 公司的server down了, hotel pc can't allow you install any other software, what can you do if for ssh only??

3. Suggestion: We should always use ssh version2, no version1. See this link: http://www.snailbook.com/faq/ssh-1-vs-2.auto.html

So, use ssh protocol version 2 option as you can and no version 1 allowed.

4. We can still do some minor changes on xinetd. For example:
   a. Add "server_args = -h" in xinetd.d/telnet so people don't know what OS you are running so reduce OS specific attacks.
   b. Add "server_args = -u -l" in xinetd.d/finger so user can not guess any your login name by using "finger @yourdomain.com" and see who is there

5. 那個sshd tip最後一段, 是否應該是hosts.deny better? 我通常不讓外面的人finger我的server, 但是我會reject and got warning email about this potential posrt scan.

>>>然後在 /etc/hosts.allow 加入一行:
>>>代碼:
>>>in.fingerd: ALL : spawn ( echo -e "\nWARNING %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY

同理, 可以寫一個script要是有人一分鐘之內連續access幾個重要的port (ftp/telnet/ssh/smtp/pop/finger/www...), then "assume" it's port scan and do something!

Just share with you and thanks for all you guys's tips and contribution. :-)

hsinan

  • 懷疑的國中生
  • **
  • 文章數: 66
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #20 於: 2006-08-31 06:24 »
關於2再補充一些. I know, telnet is not safe, but I NEVER login it from outside so no one can get your password. If hacker is doing something sniff, it needs to take time to keep monitor your PC at same network, plus not only telnet, but also other services without ssh/ssl they can still get your password. So, it's just a backup plan, 最遭的情況之下. Not just for something, for example, 斷電. 有一次我人在外, 收到server email關於UPS電力的訊息, 那時的我的手機還沒裝ssh, 又無法telnet回去, 就"眼睜睜"幻想著server run到最後斷電....神阿! 給我一分鐘login and shutdown多好..........:-D

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #21 於: 2006-08-31 14:41 »
感謝 hsinan 的建議與分享!
安全的确是一跟需要耐心與細心的工作, 防微杜漸.

關於您提到的 powerfailure, 一般的 linux 都可以在 inittab 裡面設定自動關機的.
大可不必 ssh/telnet 回去關機的. 您參考一下?

hsinan

  • 懷疑的國中生
  • **
  • 文章數: 66
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #22 於: 2006-08-31 22:28 »
感謝netman. 當時其實我是兩台servers接同一個UPS, 我又不願意開放rsh讓沒有直接連上UPS的server可以用rsh remote shutdown, 所以....吃過兩次斷電然後資料損毀的虧, 甚至有一次硬碟也毀了, 現在寧願花些錢一台server一個UPS, '冬暖夏熱'.............>_<

下一篇我post一些小技巧這裡沒有提的, 大家可以發揮想像力創照力自己在加些功能吧

hsinan

  • 懷疑的國中生
  • **
  • 文章數: 66
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #23 於: 2006-08-31 22:44 »
1. /sysAdmin/scripts/new_accton.pl 如果你想知道誰login你的system, 執行了哪些指令, 你又不想讓他知道你在monitor它
   This is a simple script to record all commands run in your system so you can later check it out who was doing what and prevent potential attacks, if your system allowed other people login.

#! /usr/bin/perl
#
($sec, $min, $hour, $mday, $mon) = localtime();
$mon = $mon + 1;

system("/bin/mv /var/log/accton.log /var/log/acct/accton.log.$mon.$mday.$hour.$min.$sec");
system("/bin/touch /var/log/accton.log");
system("/sbin/accton /var/log/accton.log");

I put this file at /etc/rc.d/rc.local, and in cron job, here is an example:
0 0 * * * /sysAdmin/scripts/new_accton.pl 2>&1

Now, you can use "lastcomm -f var/log/accton.log" or other file names in other place like /var/log/acct/yourfilename and see all commands from all users

2. Two simple scripts to change /etc/profile based on different telnet/ssh user login
Here is the /etc/hosts.allow

in.telnetd:     127.0.0.1 : spawn (/sysAdmin/scripts/localuserlogin.pl)& : allow
in.telnetd:     192.168.1.0/255.255.255.0 : spawn (/sysAdmin/scripts/localuserlogin.pl)& : allow
in.telnetd:     ALL : spawn (/sysAdmin/scripts/remoteuserlogin.pl)& : allow
in.telnetd:     mycompany : spawn (/sysAdmin/scripts/remoteuserlogin.pl)& : allow

sshd:           127.0.0.1 : spawn (/sysAdmin/scripts/localuserlogin.pl)& : allow
sshd:           192.168.1.0/255.255.255.0 : spawn (/sysAdmin/scripts/localuserlogin.pl)& : allow
sshd:           .vzw.com .myvzw.com : spawn (/sysAdmin/scripts/remoteuserlogin.pl)& : allow
sshd:           mycompany : spawn (/sysAdmin/scripts/remoteuserlogin.pl)& : allow

Here is /sysAdmin/scripts/localuserlogin.pl
#! /usr/bin/perl
#
system("/bin/rm /etc/profile");
system("/bin/cp /etc/profile.normal /etc/profile");

Here is /sysAdmin/scripts/remoteuserlogin.pl
#! /usr/bin/perl
#
system("/bin/rm /etc/profile");
system("/bin/cp /etc/profile.alert /etc/profile");

The /etc/profile.normal is same as your original /etc/profile. The /etc/profile.alert added something (copy/paste from online somewhere I visit before) :-)

Just added the following at the end of /etc/profile.alert

NOW=`date +%Y%m%d:%H%M%S`
LAST=`last $USER -10`
echo "[$NOW] $HOSTNAME This user $USER just login..." > /tmp/remote_userlogin.log
echo "$LAST" >> /tmp/remote_userlogin.log
mail -s "Attention !! User $USER just login $HOSTNAME" alert@localhost < /tmp/remote_userlogin.log
rm -f /tmp/remote_userlogin.log

And, add ".forward" file in /home/alert. Now, you can easily add any email you want to receive in this .forward file. For example, I added my cellular email account, company email, personal email, and to another server

至於telnet, 你可以把netman的script加入, 若是login failed幾次就鎖IP. 儘管hacker再利害, 不知道你的ID, 沒有你的passwd file, 不至於try 3次就login成功了吧. 打開telnet只是個人警急需要時已備之需, 強烈建議平時不要從外面telnet login!

3. My /etc/xinetd.d/telnet
service telnet
{
        disable = no
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/sbin/in.telnetd
        server_args     = -h
        log_on_failure  += USERID
        per_source      = 2
}

NOTE: only 2 connections allowed from same IP (per_source) and no OS version and information showed "server_Args = -h"

4. My /etc/xinetd.d/finger
service finger
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/sbin/in.fingerd
        server_args     = -u -l
}

NOTE: add "server_args = -u -l". Check it out yourself by "man" if you want to know what's it loh...:-D

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #24 於: 2006-09-01 00:01 »
呵, 感謝分享!!  ^_^

不過, 現在看到的入侵, 都好像很少用 account/password 這種需要 login 的方式呢...
唉, 真是好恐怖!!
最安全還是拔網路線~~ 哈....

hsinan

  • 懷疑的國中生
  • **
  • 文章數: 66
    • 檢視個人資料
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #25 於: 2006-09-01 02:14 »
哈 對呀 各種千奇百怪的hack方式都有 要注意新的漏洞 又要應付DoS 曾經又個大陸和南韓的IP在我們美國這過節的時段密集的攻擊 到最後可能火大攻不進我的server竟然卯起來DoS 大家比耐力拼頻寬.....

講到這, 有人有有效的方式堵DoS嗎? 除了鎖IP.....感謝分享! :-)

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
[整理]sshd 安全設定 ( 設定"禁止使用ssh名單" )
« 回覆 #26 於: 2006-09-01 12:06 »
若是針對主機資源的 dos , 鎖 IP 是較為直接了當的.
但如果是針對 bandwidth 的  dos, 那最好是請 ISP 幫忙了, 或許要收費.

uuilee

  • 可愛的小學生
  • *
  • 文章數: 4
    • 檢視個人資料
我ap  dd-wrt 被入侵那怎麼辦阿sshd還沒修正這bug嗎
有人的ap linux embed被當跳板的嗎?

coosos

  • 可愛的小學生
  • *
  • 文章數: 5
    • 檢視個人資料
請問
在/etc/hosts.allow那個設定檔裡
可以設定成
sshd:[使用者名稱]
其餘的設定直跟您一樣

但是將變成ssh都不能連了
如果將他改回來又可以了

我想要的功能是
只讓一個特定帳號能夠登入ssh作為其管理員
謝謝您

湯姆貓

  • 活潑的大學生
  • ***
  • 文章數: 475
    • 檢視個人資料
請問
在/etc/hosts.allow那個設定檔裡
可以設定成
sshd:[使用者名稱]
其餘的設定直跟您一樣

但是將變成ssh都不能連了
如果將他改回來又可以了

我想要的功能是
只讓一個特定帳號能夠登入ssh作為其管理員
謝謝您

特定帳號登入ssh.
請修改/etc/ssh/sshd_config
加上下面這行
AllowUsers      特定帳號
記得把root可以登入ssh的功能關掉.
(直接加#號也可以)
#PermitRootLogin yes