, 前言
有关 ssh 好处, 相信不用我多说了吧?
简而言的, 的前 rpc command 和 telnet 都全可用 ssh 代替.
比方如下这些常见功能:
- 远程登录
ssh [email protected]
- 远程执行
ssh [email protected] 'command ...'
- 远程粗?
scp [email protected]:/remote/path /local/path
scp /local/path [email protected]:/remote/path
- X forward
ssh -X [email protected]
xcommand ...
- Tunnel / Portforward
ssh -L 1234:remote.machine:4321 [email protected]
ssh -R 1234:local.machine:4321 [email protected]
ssh -L 1234:other.machine:4321 [email protected]
至于详细使用方法, 我这就不说了. 请读者自行研究吧.
我这里要说, 是针对 ssh 服务为大家介绍些窍门技巧, 希望大家用得更安心些.
2, 实作
(实作以 RedHat 9 为范例)
1) 禁止 root 登录
# vi /etc/ssh/sshd_config
PermitRootLogin no
2) 废除密码登录, 强迫使用 RSA 验证(假设 ssh 账户为 user1 )
# vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
# service sshd restart
# su - user1
$ mkdir ~/.ssh 2>/dev/null
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/authorized_keys
--------------------------------------------------
转往 client 端:
$ ssh-keygen -t rsa
(按 3下 enter 完成﹔不需设密码除非您会用 ssh-agent )
$ scp ~/.ssh/id_rsa.pub [email protected]:id_rsa.pub
(若是 windows client, 可用 puttygen.exe 产生 public key,
然后复制到 server 端后修改的, 使其内容成为单行.)
---------------------------------------------------
回到 server 端:
$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
$ rm ~/id_rsa.pub
$ exit
3) 限制 su / sudo 名单:
# vi /etc/pam.d/su
auth required /lib/security/$ISA/pam_wheel.so use_uid
# visudo
%wheel ALL=(ALL) ALL
# gpasswd -a user1 wheel
4) 限制 ssh 使用者名单
# vi /etc/pam.d/sshd
auth required pam_listfile.so item=user sense=allow file=/etc/ssh_users _disibledevent=> # echo user1 >> /etc/ssh_users
5) 封锁 ssh 联机并改用 web 控管清单
# iptables -I INPUT -p tcp --dport 22 -j DROP
# mkdir /var/www/html/ssh_open
# cat > /var/www/html/ssh_open/.htaccess <<END
AuthName "ssh_open"
AuthUserFile /var/www/html/ssh_open/.htpasswd
AuthType basic
require valid-user
END
# htpasswd -c /var/www/html/ssh_open/.htpasswd user1
(最好还将 SSL 设起来, 或只限 https 联机更佳, 我这里略过 SSL 设定, 请读者自补.)
(如需控制联机来源, 那请再补 Allow/Deny 项目, 也请读者自补.)
# cat > /var/www/html/ssh_open/ssh_open.php <<END
<?
//Set dir path for ip list
$dir_path=".";
//Set filename for ip list
$ip_list="ssh_open.txt";
//Get client ip
$user_ip=$_SERVER['REMOTE_ADDR'];
//allow specying ip needed
(@$_GET['myip']) {
$user_ip=$_GET['myip'];
}
//checking IP format
($user_iplong2ip(ip2long($user_ip))) {
//Put client ip to a file
(@!($file = fopen("$dir_path/$ip_list","w+")))
{
echo "Permission denied!!<br>";
echo "Pls Check your rights to dir $dir_path or file $ip_list";
}
{
fputs($file,"$user_ip");
fclose($file);
echo "client ip($user_ip) has put o $dir_path/$ip_list";
}
} {
echo "Invalid IP format!!<br>ssh_open.txt was not changed.";
}
?>
END
# touch /var/www/html/ssh_open/ssh_open.txt
# chmod 640 /var/www/html/ssh_open/*
# chgrp apache /var/www/html/ssh_open/*
# chmod g+w /var/www/html/ssh_open/ssh_open.txt
# chmod o+t /var/www/html/ssh_open
# service httpd restart
# mkdir /etc/iptables
# cat > /etc/iptables/sshopen.sh <<END
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
list_dir=/var/www/html/ssh_open
list_file=$list_dir/ssh_open.txt
chain_name=ssh_rules
mail_to=root
# clear chain exits, or create chain.
iptables -L -n | /bin/grep -q "^Chain $chain_name" && {
iptables -F $chain_name
true
} || {
iptables -N $chain_name
iptables -I INPUT -p tcp --dport 22 -j $chain_name
}
# clear chain when needed
[ "$1" = clear ] && {
iptables -F $chain_name
exit 0
}
# do nothing while list is empty
[ -s $list_file ] || exit 1
# add rule
iptables -A $chain_name -p tcp --dport 22 -s $(< $list_file) -j ACCEPT && echo "ssh opened to $(< $list_file) _disibledevent=> _type = stream
protocol = tcp
wait = no
user = root
server = /etc/iptables/sshopen.sh
}
# iptables -I INPUT -p tcp --dport 1234 -j ACCEPT
# cat > /etc/cron.d/sshopen <<END
*/5 * * * * root /etc/iptables/sshopen.sh clear
END
---------------------------
转往 client 端
在 browser URL 输入:
http://server.machine/ssh_open/ssh_open.php?myip=1.2.3.4
(若不指定 ?myip=1.2.3.4 则以 client 当时 IP 为准, 若没经 proxy 话.)
如此, server 端 ssh_open.txt 文件只有单记录, 每次盖写.
接着:
$ telnet server.machine 1234
然后你有最多 5 分钟时间用 ssh 联机 server !
---------------------------
此步骤基本构思如下:
5.1) 将 sshd firewall 联机全部 block 掉.
5.2) 然后在 httpd 那设个 directory, 可设 ssl+htpasswd+allow/deny control,
然后在目录内写个 php 将 browser ip 记录于份 .txt 文字文件里.
视你转写能力, 你可自动抓取 browser 端 IP, 也可让 browser 端传入参数来指定.
文字文件只有单记录, 每次盖写.
5.3) 修改 /etc/services , 增加个新项目(如 xxx), 并指定个新 port(如 1234)
5.4) 再用 xinetd 监听该 port , 并启动令只 script, 设定 iptables , 从 step2 清单里取得 IP, 为的打开 ssh 联机.
5.5) 设 crontab 每数分中清理 iptables 有关 ssh 联机规则. 这并不影响既有联机, 若逾时再连, 则重复上述.
6) 要是上步骤没设定, 你或许会担心过多人来 try 你 ssh 服务话:
# cat > /etc/iptables/sshblock.sh <<END
#!/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 '{pr $NF}' | xargs)
bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{pr $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##*
最新评论