SSH命令使用技巧

一 前言


关于 ssh 的好处, 相信不用我多说了吧?

简而言之, 之前的 rpc command 与 telnet 都全可用 ssh 代替.

比方如下的这些常见功能:

xcommand ...

- Tunnel / Portforward

ssh -L 1234:remote.machine:4321 ​​​

ssh -R 1234:local.machine:4321 ​​​

ssh -L 1234:other.machine:4321 ​​


 


二, 实作


 


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

(按三下 enter 完成﹔不需设密码,除非您会用 ssh-agent 。)

$ scp ~/.ssh/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 onerr=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

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 < //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 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 "Permission denied!!

";

    echo "Pls Check your rights to dir $dir_path or file $ip_list";

}

else

{

    fputs($file,"$user_ip");

    fclose($file);

    echo "client ip($user_ip) has put into $dir_path/$ip_list";

}

} else {

echo "Invalid IP format!!

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 < #!/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 -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) 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 < service sshopen

{

    disable = no

    socket_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 < */5 * * * *   root   /etc/iptables/sshopen.sh clear

END


---------------------------

转往 client 端

在 browser URL 输入:

(若不指定 ?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 < #!/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

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 , 来个永久性封闭:

for i in $(< $PERM_LIST)

do

    /sbin/iptables -I INPUT -s $i -j DROP

done  


7) 还有, 你想知道有哪些人对你做 full range port scan 的话:


# iptables -I INPUT -p tcp --dport 79 -j ACCEPT

cat > /etc/xinetd.d/finger < service finger

{

    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 %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY

END


这里, 我只是设为发信给 root.

事实上, 你可修改为起动 firewall 将 %a 这个传回值给 ban 掉也行.

不过, 对方要是有选择性的做 port scan , 没扫到 finger 的话, 那当然就没用了...


 


 


SSH客户端命令

Submitted by amxku on 2006, June 14, 11:35 PM. 帝国系统

ssh –l user –p 22 upsdn.net


输入密码即可登录


l login_name


指定登入于远程机器上的使用者,若没加这个选项,而直接打 ssh lost 也是可以的,它是以读者目前的使用者去做登入的动作​


===================================================


-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 档案


======================================================