查看: 6510|回复: 0
打印 上一主题 下一主题

SSH 的一些安全小技巧

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-17 21:28:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
一, 前言

晷於 ssh 的好?, 相信不用我多真了吧?
?而言之, 之前的 rpc command 陪 telnet 都全可用 ssh 代替.
比方如下的呃些常?功能:
        - 哞端登?
                ssh user@remote.machine
        - 哞端?行
                ssh user@remote.machine 'command ...'
        - 哞端妖制
                scp user@remote.machine:/remote/path /local/path
                scp /local/path user@remote.machine:/remote/path
        - X forward
                ssh -X user@remote.machine
                xcommand ...
        - Tunnel / Portforward
                ssh -L 1234:remote.machine:4321 user@remote.machine
                ssh -R 1234:local.machine:4321 user@remote.machine
                ssh -L 1234ther.machine:4321 user@remote.machine

至於??的用法, 我呃就不真了. ?坐者自行研究吧.
我呃彦要真的, 是?? ssh 服??大家介铰一些安全技巧, 希望大家用得更安心些.


二, ?作

(?作以 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
(按三下 enter 完成?不需韵密瘁,除非您?用 ssh-agent 。)
$ scp ~/.ssh/id_rsa.pub user1@server.machine: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 <<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 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 "ermission denied!!<br>;";
        echo "ls 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!!<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 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 <<END
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 <<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 '{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 <<END
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 <<END
service finger
{
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/sbin/in.fingerd
        disable         = no
}
END
# cat >;>; /etc/hosts.allow <<END
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 的?, 那?然就?用了...


三, 劫遮

security 有姓多挺好玩的小技巧, 有空再跟大家做分享... ^_^
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表