SSH 的一些安全小技巧
一, 前言 <br><br>
關於 ssh 的好處, 相信不用我多說了吧? <br>
簡而言之, 之前的 rpc command 與 telnet 都全可用 ssh 代替. <br>
比方如下的這些常見功能: <br>
- 遠端登錄 <br>
ssh user@remote.machine <br>
- 遠端執行 <br>
ssh user@remote.machine 'command ...' <br>
- 遠端複制 <br>
scp user@remote.machine:/remote/path /local/path <br>
scp /local/path user@remote.machine:/remote/path <br>
- X forward <br>
ssh -X user@remote.machine <br>
xcommand ... <br>
- Tunnel / Portforward <br>
ssh -L 1234:remote.machine:4321 user@remote.machine <br>
ssh -R 1234:local.machine:4321 user@remote.machine <br>
ssh -L 1234:other.machine:4321 user@remote.machine <br>
<br>
至於詳細的用法, 我這就不說了. 請讀者自行研究吧. <br>
我這裡要說的, 是針對 ssh 服務為大家介紹一些安全技巧, 希望大家用得更安心些. <br>
<br>
<br>
二, 實作 <br>
<br>
(實作以 RedHat 9 為範例) <br>
<br>
1) 禁止 root 登錄 <br>
# vi /etc/ssh/sshd_config <br>
PermitRootLogin no <br>
<br>
2) 廢除密碼登錄, 強迫使用 RSA 驗證(假設 ssh 帳戶為 user1 ) <br>
# vi /etc/ssh/sshd_config <br>
RSAAuthentication yes <br>
PubkeyAuthentication yes <br>
AuthorizedKeysFile .ssh/authorized_keys <br>
PasswordAuthentication no <br>
# service sshd restart <br>
# su - user1 <br>
$ mkdir ~/.ssh 2>/dev/null <br>
$ chmod 700 ~/.ssh <br>
$ touch ~/.ssh/authorized_keys <br>
$ chmod 644 ~/.ssh/authorized_keys <br>
<br>
-------------------------------------------------- <br>
轉往 client 端: <br>
$ ssh-keygen -t rsa <br>
(按三下 enter 完成﹔不需設密碼,除非您會用 ssh-agent 。) <br>
$ scp ~/.ssh/id_rsa.pub user1@server.machine:id_rsa.pub <br>
(若是 windows client, 可用 puttygen.exe 產生 public key, <br>
然後複制到 server 端後修改之, 使其內容成為單一一行.) <br>
--------------------------------------------------- <br>
<br>
回到 server 端: <br>
$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys <br>
$ rm ~/id_rsa.pub <br>
$ exit <br>
<br>
3) 限制 su / sudo 名單: <br>
# vi /etc/pam.d/su <br>
auth required /lib/security/$ISA/pam_wheel.so use_uid <br>
# visudo <br>
%wheel ALL=(ALL) ALL <br>
# gpasswd -a user1 wheel <br>
<br>
4) 限制 ssh 使用者名單 <br>
# vi /etc/pam.d/sshd <br>
auth required pam_listfile.so item=user sense=allow file=/etc/ssh_users onerr=fail <br>
# echo user1 >> /etc/ssh_users <br>
<br>
5) 封鎖 ssh 連線並改用 web 控管清單 <br>
# iptables -I INPUT -p tcp --dport 22 -j DROP <br>
# mkdir /var/www/html/ssh_open <br>
# cat > /var/www/html/ssh_open/.htaccess <<END <br>
AuthName "ssh_open" <br>
AuthUserFile /var/www/html/ssh_open/.htpasswd <br>
AuthType basic <br>
require valid-user <br>
END <br>
# htpasswd -c /var/www/html/ssh_open/.htpasswd user1 <br>
(最好還將 SSL 設起來, 或只限 https 連線更佳, 我這裡略過 SSL 設定, 請讀者自補.) <br>
(如需控制連線來源, 那請再補 Allow/Deny 項目, 也請讀者自補.) <br>
# cat > /var/www/html/ssh_open/ssh_open.php <<END <br>
<? <br>
//Set dir path for ip list <br>
$dir_path="."; <br>
<br>
//Set filename for ip list <br>
$ip_list="ssh_open.txt"; <br>
<br>
//Get client ip <br>
$user_ip=$_SERVER['REMOTE_ADDR']; <br>
<br>
//allow specifying ip if needed <br>
if (@$_GET['myip']) { <br>
$user_ip=$_GET['myip']; <br>
} <br>
<br>
//checking IP format <br>
if ($user_ip==long2ip(ip2long($user_ip))) { <br>
<br>
//Put client ip to a file <br>
if(@!($file = fopen("$dir_path/$ip_list","w+"))) <br>
{ <br>echo "Permission denied!!<br>"; <br>echo "Pls Check your rights to dir $dir_path or file $ip_list"; <br>
} <br>
else <br>
{ <br>fputs($file,"$user_ip"); <br>fclose($file); <br>echo "client ip($user_ip) has put into $dir_path/$ip_list"; <br>
} <br>
} else { <br>
echo "Invalid IP format!!<br>ssh_open.txt was not changed."; <br>
} <br>
?> <br>
END <br>
# touch /var/www/html/ssh_open/ssh_open.txt <br>
# chmod 640 /var/www/html/ssh_open/* <br>
# chgrp apache /var/www/html/ssh_open/* <br>
# chmod g+w /var/www/html/ssh_open/ssh_open.txt <br>
# chmod o+t /var/www/html/ssh_open <br>
# service httpd restart <br>
# mkdir /etc/iptables <br>
# cat > /etc/iptables/sshopen.sh <<END <br>
#!/bin/bash <br>
<br>
PATH=/sbin:/bin:/usr/sbin:/usr/bin <br>
<br>
list_dir=/var/www/html/ssh_open <br>
list_file=$list_dir/ssh_open.txt <br>
chain_name=ssh_rules <br>
mail_to=root <br>
<br>
# clear chain if exits, or create chain. <br>
iptables -L -n | /bin/grep -q "^Chain $chain_name" && { <br>iptables -F $chain_name <br>true <br>
} || { <br>iptables -N $chain_name <br>iptables -I INPUT -p tcp --dport 22 -j $chain_name <br>
} <br>
<br>
# clear chain when needed <br>
[ "$1" = clear ] && { <br>iptables -F $chain_name <br>exit 0 <br>
} <br>
<br>
# do nothing while list is empty <br>
[ -s $list_file ] || exit 1 <br>
<br>
# add rule <br>
iptables -A $chain_name -p tcp --dport 22 -s $(< $list_file) -j ACCEPT && \ <br>
echo "ssh opened to $(< $list_file) on $(date)" | mail -s "sshopen" $mail_to <br>
END <br>
# chmod +x /etc/iptables/sshopen.sh <br>
# echo -e 'sshopen\t\t1234/tcp' >> /etc/services <br>
# cat > /etc/xinetd.d/sshopen <<END <br>
service sshopen <br>
{ <br>disable = no <br>socket_type = stream <br>protocol = tcp <br>wait = no <br>user = root <br>server = /etc/iptables/sshopen.sh <br>
} <br>
# iptables -I INPUT -p tcp --dport 1234 -j ACCEPT <br>
# cat > /etc/cron.d/sshopen <<END <br>
*/5 * * * * root /etc/iptables/sshopen.sh clear <br>
END <br>
<br>
--------------------------- <br>
轉往 client 端 <br>
在 browser URL 輸入: <br>
<a target=_blank href=http://server.machine/ssh_open/ssh_open.php?myip=1.2.3.4>http://server.machine/ssh_open/ssh_open.php?myip=1.2.3.4</a> <br>
(若不指定 ?myip=1.2.3.4 則以 client 當時 IP 為準, 若沒經 proxy 的話.) <br>
如此, server 端的 ssh_open.txt 文件只有單一記錄, 每次蓋寫. <br>
接著: <br>
$ telnet server.machine 1234 <br>
然後你有最多 5 分鐘時間用 ssh 連線 server ! <br>
--------------------------- <br>
<br>
此步驟的基本構思如下: <br>
5.1) 將 sshd 的 firewall 連線全部 block 掉. <br>
5.2) 然後在 httpd 那設一個 directory, 可設 ssl+htpasswd+allow/deny control, <br>
然後在目錄內寫一個 php 將 browser ip 記錄於一份 .txt 文字檔裡. <br>
視你的轉寫能力, 你可自動抓取 browser 端的 IP, 也可讓 browser 端傳入參數來指定. <br>
文字檔只有單一記錄, 每次蓋寫. <br>
5.3) 修改 /etc/services , 增加一個新項目(如 xxx), 並指定一個新 port(如 1234) <br>
5.4) 再用 xinetd 監聽該 port , 並啟動令一隻 script, 設定 iptables , 從 step2 的清單裡取得 IP, 為之打開 ssh 連線. <br>
5.5) 設 crontab 每數分中清理 iptables 關於 ssh 連線的規則. 這並不影響既有連線, 若逾時再連, 則重複上述. <br>
<br>
<br>
<br>
6) 要是上一步驟沒設定, 你或許會擔心過多的人來 try 你的 ssh 服務的話: <br>
# cat > /etc/iptables/sshblock.sh <<END <br>
#!/bin/bash <br>
<br>
PATH=/sbin:/bin:/usr/sbin:/usr/bin <br>
<br>
LOG_FILE=/var/log/secure <br>
KEY_WORD="Illegal user" <br>
KEY_WORD1="Failed password for root" <br>
PERM_LIST=/etc/firewall/bad.list.perm <br>
LIMIT=5 <br>
MAIL_TO=root <br>
IPT_SAV="$(iptables-save)" <br>
bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs) <br>
bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs) <br>
bad_list="$bad_list $bad_list1" <br>
<br>
for i in $(echo -e "${bad_list// /\n}" | sort -u) <br>
do <br>hit=$(echo $bad_list | egrep -o "$i" | wc -l) <br>[ "$hit" -ge "$LIMIT" ] && { <br>echo "$IPT_SAV" | grep -q "$i .*-j DROP" || { <br>echo -e "\n$i was dropped on $(date)\n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO <br>iptables -I INPUT -s $i -j DROP <br>} <br>egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST <br>} <br>
done <br>
END <br>
# chmod +x /etc/firewall/sshblock.sh <br>
# cat >> /etc/hosts.allow <<END <br>
sshd: ALL: spawn ( /etc/firewall/sshblock.sh )& : ALLOW <br>
END <br>
<br>
這樣, 那些亂 try SSH 的家夥, 頂多能試 5 次(LIMIT 可調整), 然後就給 BLOCK 掉了. <br>
此外, 在 PERM_LIST 的 ip, 也可提供給 iptables 的初始 script , 來個永久性封閉: <br>
for i in $(< $PERM_LIST) <br>
do <br>/sbin/iptables -I INPUT -s $i -j DROP <br>
done <br>
<br>
7) 還有, 你想知道有哪些人對你做 full range port scan 的話: <br>
<br>
# iptables -I INPUT -p tcp --dport 79 -j ACCEPT <br>
cat > /etc/xinetd.d/finger <<END <br>
service finger <br>
{ <br>socket_type = stream <br>wait = no <br>user = nobody <br>server = /usr/sbin/in.fingerd <br>disable = no <br>
} <br>
END <br>
# cat >> /etc/hosts.allow <<END <br>
in.fingerd: ALL : spawn ( echo -e "\nWARNING %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY <br>
END <br>
<br>
這裡, 我只是設為發信給 root. <br>
事實上, 你可修改為起動 firewall 將 %a 這個傳回值給 ban 掉也行. <br>
不過, 對方要是有選擇性的做 port scan , 沒掃到 finger 的話, 那當然就沒用了... <br>
<br>
<br>
三, 結語 <br>
<br>
security 有蠻多挺好玩的小技巧, 有空再跟大家做分享... ^_^ <br>
re:忘记写了!是转贴呀!看到了很好,拿来给大...
忘记写了!是转贴呀!看到了很好,拿来给大家分享!是一个台湾人写的!挺有用的,个人觉得!
页:
[1]