南方Linux论坛   首页 | 行业 | 下载 | Blog | 桌面应用 | 数据库 | 电子商务 | 文摘 | 网络服务 | 开源 | 系统管理 | 内核代码 | 教程

返回   南方Linux论坛 > Linux 软件应用讨论区 > 网络安全
注册账号 Blog 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记讨论区已读

网络安全 讨论基于Linux下的网络安全解决办法

发表新主题 回复
 
主题工具 显示模式
旧 2007-04-17, 07:39 AM   #1
TOM
级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时
论坛义工
 
注册日期: 2006-07-05
帖子: 477
精华: 0
现金: 1632 金币
资产: 1632 金币
声望: 16 TOM 正向着好的方向发展
不错 SSH 的一些安全小技巧

一, 前言

关於 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 1234 : other.machine:4321 user@remote.machine

至於详细的用法, 我这就不說了. 请读者自行研究吧.
我这裡要說的, 是针对 ssh 服务为大家介绍一些安全技巧, 希望大家用得更安心些.


二, 实作

(实作以 RedHat 9 为范例)

--------------------------------------------------
转往 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 端已经禁止密码登入, 那请用其它放法复制 publick key.
---------------------------------------------------

登入 server 端:

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
$ 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 "Permission denied!!<br>";
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!!<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 on demand
[ "$1" = clear ] && {
iptables -F $chain_name
cat /dev/null > $list_file
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
exit 0
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_o...p?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 的话, 那当然就沒用了...

作者:网中人
TOM 当前离线  
回复时引用此帖
发表新主题 回复


当前查看此主题的会员: 1 (0 位会员和 1 位游客)
 
主题工具
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

论坛启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码


所有时间均为北京时间。现在的时间是 03:23 AM


vBulletin 3.6.8 Powered by 南方Linux联盟
版权所有 ©2004 - 2009, bbs.linuxunion.net