温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

openssh 加固

发布时间:2020-07-09 05:13:35 来源:网络 阅读:905 作者:紫色葡萄 栏目:安全技术

很多场合,我们不得不在公网开启ssh 22端口,以CentOS6为例,下面的几个办法可以加固ssh连接


1、限制密码尝试次数(denyhosts)

yum install denyhosts --enablerepo=epel
chkconfig denyhosts on
/etc/init.d/denyhosts start


2、除掉密码认证,采用ssh密钥登陆

修改/etc/ssh/sshd_config

PasswordAuthentication no


3、禁止root登陆

修改 /etc/ssh/sshd_config

PermitRootLogin no


4、限制连接频率

/sbin/iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name ssh --rsource
/sbin/iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent ! --rcheck --seconds 60 --hitcount 2 --name ssh --rsource -j ACCEPT


也可以利用iptables recent模块搞另类一点的策略,默认关闭ssh端口,用ping来解锁。

iptables -A INPUT -p icmp --icmp-type 8 -m length --length 78 -j LOG --log-prefix "SSHOPEN: "
#记录日志,前缀SSHOPEN:

iptables -A INPUT -p icmp --icmp-type 8 -m length --length 78 -m recent --set --name sshopen --rsource -j ACCEPT
#linux默认ping包一般为56字节,加上IP头20字节,ICMP头部8字节,共84字节。我们这里指定78字节,回头用特定大小的ping包来解锁。

iptables -A INPUT -p tcp --dport 22 --syn -m recent --rcheck --seconds 15 --name sshopen --rsource -j ACCEPT
#符合sshopen的IP才会放行22端口

ping -s 50 host #Linux下解锁
ping -l 50 host #Windows下解锁


5、限制IP来源

这个稍微复杂一点点,采用geoip数据库来识别IP来源,比如只允许中国的IP访问


写个脚本

#!/bin/bash
# UPPERCASE space-separated country codes to ACCEPT
ALLOW_COUNTRIES="CN"

if [ $# -ne 1 ]; then
  echo "Usage:  `basename $0` <ip>" 1>&2
  exit 0 # return true in case of config issue
fi

COUNTRY=`/usr/bin/geoiplookup $1 | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`

[[ $COUNTRY = "IP Address not found" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY"

if [ $RESPONSE = "ALLOW" ]then
  exit 0
else
  logger "$RESPONSE sshd connection from $1 ($COUNTRY)"
  exit 1
fi


利用tcp_wrapper调用那个脚本

chmod 775 /usr/bin/sshfilter.sh
echo "sshd: ALL" >>/etc/hosts.deny
echo "sshd: 10.0.0.0/8" >>/etc/hosts.allow
echo "sshd: ALL: aclexec /usr/bin/sshfilter.sh %a" >>/etc/hosts.allow


6、设置超时

ssh session 超时应该属于安全范畴,可以防止人离开后,终端被他人利用。

这里设置为1800秒(30分钟)


方法一、利用环境变量TMOUT

echo "export TMOUT=1800" >/etc/profile.d/timeout.sh
source /etc/profile.d/timeout.sh

方法二、修改sshd_config

ClientAliveInterval 60
ClientAliveCountMax 30



参考文章

http://www.axllent.org/docs/view/ssh-geoip/

http://www.haiyun.me/archives/iptables-recent.html

http://www.cnblogs.com/fhefh/archive/2011/10/19/2217954.html



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI