一 前言
二, 实作
1) 禁止 root 登录
# vi /etc/ssh/sshd_config PermitRootLogin no
2) 废除密码登录, 强迫使用 RSA 验证(假设 ssh 账户为 user1 )
# vi /etc/ssh/sshd_config
RSAAuthenticationyes
PubkeyAuthenticationyes
AuthorizedKeysFile.ssh/authorized_keys
PasswordAuthenticationno
#servicesshdrestart
#su-user1
$mkdir~/.ssh2>/dev/null
$chmod700~/.ssh
$touch~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/authorized_keys
--------------------------------------------------
转往client端:
$ssh-keygen-trsa
(按三下enter完成﹔不需设密码,除非您会用ssh-agent。)
$scp~/.ssh/[email protected]:id_rsa.pub
(若是windowsclient,可用puttygen.exe产生publickey,
然后复制到server端后修改之,使其内容成为单一一行.)
---------------------------------------------------
回到 server 端:
$cat~/id_rsa.pub>>~/.ssh/authorized_keys
$rm~/id_rsa.pub
$ exit
3) 限制 su / sudo 名单:
# vi /etc/pam.d/su
authrequired/lib/security/$ISA/pam_wheel.souse_uid
#visudo
%wheelALL=(ALL)ALL
# gpasswd -a user1 wheel
4) 限制 ssh 使用者名单
# vi /etc/pam.d/sshd
authrequiredpam_listfile.soitem=usersense=allowfile=/etc/ssh_usersonerr=fail
# 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<AuthName"ssh_open"
AuthUserFile/var/www/html/ssh_open/.htpasswd
AuthTypebasic
requirevalid-user
END
#htpasswd-c/var/www/html/ssh_open/.htpasswduser1
(最好还将SSL设起来,或只限https联机更佳,我这里略过SSL设定,请读者自补.)
(如需控制联机来源,那请再补Allow/Deny项目,也请读者自补.)
#cat>/var/www/html/ssh_open/ssh_open.php<//Setdirpathforiplist
$dir_path=".";
//Set filename for ip list $ip_list="ssh_open.txt";
//Get client ip $user_ip=$_SERVER['REMOTE_ADDR'];
//allow specifying ip if needed
if(@$_GET['myip']){
$user_ip=$_GET['myip'];
}
//checking IP format if ($user_ip==long2ip(ip2long($user_ip))) {
//Put client ip to a file
if(@!($file=fopen("$dir_path/$ip_list","w+")))
{
echo"Permissiondenied!!
";
echo"PlsCheckyourrightstodir$dir_pathorfile$ip_list";
}
else
{
fputs($file,"$user_ip");
fclose($file);
echo"clientip($user_ip)hasputinto$dir_path/$ip_list";
}
}else{
echo"InvalidIPformat!!
ssh_open.txtwasnotchanged.";
}
?>
END
#touch/var/www/html/ssh_open/ssh_open.txt
#chmod640/var/www/html/ssh_open/*
#chgrpapache/var/www/html/ssh_open/*
#chmodg+w/var/www/html/ssh_open/ssh_open.txt
#chmodo+t/var/www/html/ssh_open
#servicehttpdrestart
#mkdir/etc/iptables
# cat > /etc/iptables/sshopen.sh < #!/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 if exits, or create chain.
iptables-L-n|/bin/grep-q"^Chain$chain_name"&&{
iptables-F$chain_name
true
}||{
iptables-N$chain_name
iptables-IINPUT-ptcp--dport22-j$chain_name
}
# clear chain when needed
["$1"=clear]&&{
iptables-F$chain_name
exit0
}
# do nothing while list is empty [ -s $list_file ] || exit 1
# add rule
iptables-A$chain_name-ptcp--dport22-s$(<$list_file)-jACCEPT&&\
echo"sshopenedto$(<$list_file)on$(date)"|mail-s"sshopen"$mail_to
END
#chmod+x/etc/iptables/sshopen.sh
#echo-e'sshopen\t\t1234/tcp'>>/etc/services
#cat>/etc/xinetd.d/sshopen<servicesshopen
{
disable=no
socket_type=stream
protocol=tcp
wait=no
user=root
server=/etc/iptables/sshopen.sh
}
#iptables-IINPUT-ptcp--dport1234-jACCEPT
#cat>/etc/cron.d/sshopen<*/5****root/etc/iptables/sshopen.shclear
END
---------------------------
转往client端
在browserURL输入:
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文件只有单一记录,每次盖写.
接着:
$telnetserver.machine1234
然后你有最多5分钟时间用ssh联机server!
---------------------------
此步骤的基本构思如下:
5.1)将sshd的firewall联机全部block掉.
5.2)然后在httpd那设一个directory,可设ssl+htpasswd+allow/denycontrol,
然后在目录内写一个php将browserip记录于一份.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 < #!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
LOG_FILE=/var/log/secure
KEY_WORD="Illegaluser"
KEY_WORD1="Failedpasswordforroot"
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.*-jDROP"||{
echo-e"\n$iwasdroppedon$(date)\n"|mail-s"DROPby${0##*/}:$i"$MAIL_TO
iptables-IINPUT-s$i-jDROP
}
egrep-q"^$i$"$PERM_LIST||echo$i>>$PERM_LIST
}
done
END
#chmod+x/etc/firewall/sshblock.sh
#cat>>/etc/hosts.allow<sshd:ALL:spawn(/etc/firewall/sshblock.sh)&:ALLOW
END
这样, 那些乱 try SSH 的家伙, 顶多能试 5 次(LIMIT 可调整), 然后就给 BLOCK 掉了.
此外,在PERM_LIST的ip,也可提供给iptables的初始script,来个永久性封闭:
foriin$(<$PERM_LIST)
do
/sbin/iptables-IINPUT-s$i-jDROP
done
7) 还有, 你想知道有哪些人对你做 full range port scan 的话:
# iptables -I INPUT -p tcp --dport 79 -j ACCEPT
cat>/etc/xinetd.d/finger<servicefinger
{
socket_type=stream
wait=no
user=nobody
server=/usr/sbin/in.fingerd
disable=no
}
END
#cat>>/etc/hosts.allow<in.fingerd:ALL:spawn(echo-e"\nWARNING%awastryingfinger.\n$(date)"|mail-s"fingerfrom%a"root)&:DENY
END
这里, 我只是设为发信给 root.
事实上,你可修改为起动firewall将%a这个传回值给ban掉也行.
不过, 对方要是有选择性的做 port scan , 没扫到 finger 的话, 那当然就没用了...
SSH客户端命令
Submittedbyamxkuon2006,June14,11:35PM.帝国系统
ssh –l user –p 22 upsdn.net
输入密码即可登录
l login_name
===================================================
-c blowfish|3des
在期间内选择所加密的密码型式。预设是3des,3des(作三次的资料加密) 是用三种不同的密码键作三次的加密-解密-加密。 blowfish 是一个快速区块密码编制器,它比3des更安全以及更快速。
===================================================
-v
Verbose 模式。使ssh 去印出关于行程的除错讯息,这在连接除错,认 证和设定的问题上有很的帮助。
===================================================
-f
要求ssh 在背景执行命令,假如ssh要询问密码或通行证,但是使用者 想要它在幕后执行就可以用这个方式,最好还是加上-l user 例如在远程场所上激活 X11,有点像是 ssh –f host xterm 。
===================================================
-i identity_file
选择所读取的 RSA 认证识别的档案。预设是在使用者的家目录 中的 .ssh/identity
===================================================
-n
重 导 stdin 到 /dev/null (实际上是避免读取 stdin)。必须当 ssh 在幕后执行时才使用。常见的招数是使用这选项在远程机器上去执行 X11 的程序 例如,ssh -n shadows.cs.hut.fi emacs &,将在 shadows.cs.hut.fi 上激活 emace,并且 X11 连接将自动地在加密的信道上发送。ssh 程序将把它放 在幕后。(假如ssh需要去询问密码时,这将不会动作)
===================================================
-t
强制配置 pseudo-tty。这可以在远程机器上去执行任意的 screen-based 程 式,例如操作 menu services。
===================================================
-C
要 求压缩所有资料(包含 stdin, stdout,stderr 和 X11 和 TCP/IP 连接) 压缩演算规则与 gzip 相同,但是压缩的等级不能控制。在调制解调器或 联机速度很慢的地方,压缩是个很好的选择,但如果读者的网络速路很 快的话,速度反而会慢下来。
=====================================================
-p port
连接远程机器上的 port。 不用这个选项,默认就是22
======================================================
-P
使用非特定的 port 去对外联机。如果读者的防火墙不淮许从特定的 port去联机时,就可以使用这个选项。注意这个选项会关掉 RhostsAuthentication 和 RhostsRSAAuthentication。
=====================================================
-L listen-port:host:port
指派本地的 port 到达端机器地址上的 port。
====================================================
-R listen-port:host:port
指派远程上的 port 到本地地址上的 port。
-2 强制 ssh 去使用协议版本 2。
-4 强制 ssh 去使用 IPv4 地址。
-6 强制 ssh 去使用 IPv6 地址。
=====================================================
-g
允许远程主机去连接本地指派的 ports。
-a
关闭认证代理联机。
-e character
设定跳脱字符
scp 使用 scp 在远程机器上 copy 档案
======================================================
copy 本地的档案到远程的机器上
=====================================================
copy远程机器上的档案到本地来
=====================================================
保持从来源 host 档案的属性
ssh-keygen
产生公开钥 (pulib key) 和私人钥 (private key),以保障 ssh 联机的安性.
当 ssh 连 shd 服务器,会交换公开钥上,系统会检查 /etc/ssh_know_hosts 内储存的 key,如果找到客户端就用这个 key 产生一个随机产生的session key 传给服务器,两端都用这个 key 来继续完成 ssh 剩下来的阶段。
它会 产生 identity.pub、identity 两个档案,私人钥存放于identity,公开钥 存放于 identity.pub 中,接下来使用 scp 将 identity.pub copy 到远程机器的家目录下.ssh下的authorized_keys。 .ssh/authorized_keys(这个 authorized_keys 档案相当于协议的 rhosts 档案),之后使用者能够不用密码去登入。RSA的认证绝对是比 rhosts 认证更来的安全可靠。
执行:
在此,这里输入的密码可以跟帐号的密码不同,也可以不输入密码。
SSH protocol version 1:
每一部主机都可以使用 RSA 加密方式来产生一个 1024-bit 的 RSA Key ,这个 RSA 的加密方式,主要就是用来产生公钥与私钥的演算方法!这个 version 1 的整个联机的加密步骤可以简单的这么看:
1. 当每次 SSH daemon (sshd) 激活时,就会产生一支 768-bit 的公钥(或称为 server key)存放在 Server 中;
2. 若有 client 端的需求传送来时,那么 Server 就会将这一支公钥传给 client ,而 Client 藉由比对本身的 RSA 加密方式来确认这一支公钥;
3. 在 Client 接受这个 768-bit 的 server key 之后,Client 自己也会随机产生一支 256-bit 的私钥(host key),并且以加密的方式将 server key 与 host key 整合成一支完整的 Key ,并且将这支 Key 也传送给 server ;
4. 之后,Server 与 Client 在这次的联机当中,就以这一支 1024-bit 的 Key 来进行资料的传递!
当然啦,因为 Client 端每次的 256-bit 的 Key 是随机取的,所以你这次的联机与下次的联机的 Key 就会不一样啦!
==============================================
SSH protocol version 2:
与 version 1 不同的是,在 version 2 当中将不再产生 server key 了,所以,当 Client 端联机到 Server 端时,两者将藉由 Diffie-Hellman key 的演算方式来产生一个分享的 Key ,之后两者将藉由类似 Blowfish 的演算方式进行同步解密的动作!
每一个 sshd 都提供这两个版本的联机,而决定这两种模式联机的,就必需要在 client 端联机时选择联机的模式才能确认。目前预设情况下,会自动使用 version 2 的联机模式喔!而由于我们的联机资料中,经过了这个 Public 与 Private Key 的加密、解密动作,所以在中间的传送过程中,当然就比较安全的多啰!
如果直接以 ssh hostname 来连接进入 hostname 这个主机时,则进入 hostname 这个主机的『帐号名称』将会是目前你所在的这个环境当中的使用者帐号!以上面为例,因为我是以 root 的身份在执行,所以如果我执行了『 ssh host.domain.name 』时,那么对方 host.domain.name 这部主机,就会以 root 的身份来让我进行密码确认的登入动作!因此,为了避免这样的麻烦,通常我都是以简单的 e-mail 的写法来登入远方的主机,例如『ssh user@hostname 』即表示,我是以 user 这个帐号去登入 hostname 这部主机的意思。当然,也可以使用 -l username 这样的形式来书写!登入对方主机之后,其它的所有执行行为都跟在 Linux 主机内没有两样~所以,真的是很简单吧! ^_^ 这样就可以达到远程控管主机的目的了!此外,在预设的情况下, SSH 是『允许你以 root 的身份登入』喔!呵呵!更是爽快啦!此外,请特别留意的是,当您要连接到对方的主机时,如果是首次连接,那么 Server 会问你,你的联机的 Key 尚未被建立,要不要接受 Server 传来的 Key ,并建立起联机呢?呵呵!这个时候请『务必要输入 yes 而不是 y 或 Y』,这样程序才会接受
sftp -l username hostname 或者 sftp user@hosname
进入到 sftp 之后,那就跟在一般 FTP 模式下的操作方法没有两样
cd
ls dir
mkdir
rmdir
pwd
chgrp
chown
chmod
ln oldname newname
rm path
rename oldname newname
exit bye