分类目录归档:shell

ssh安全措施

以前买过许多vps,国内,国外的都有。无论idc供应商防火墙做的多好,策略有多复杂,总是会有很多连续的IP段不停的扫描你的主机服务端口,特别是Linux系统的默认远程管理端口22
最开始博主也是放置不管,并没有起多大疑心
但是麻烦就来了。不到一周的时间 ssh系统日志达到了10G,当时我一看磁盘空间就懵了,刚买的主机,什么服务都没放,怎么磁盘就增加了這么多
细细检查下才发现是/var/log/secure 这个记录ssh登陆情况的日志占用了空间
再检查内容,发现全是记录的ssh登陆失败的日志,恐怖的是某几个IP的验证密码的次数达到了十几万次
吸取教训,删除了secure日志。但是治标不治本,决定对ssh安全花些心思

No1 修改ssh端口号

更换ssh是一个很实用的方法,直接略去了百分之80以上的恶意扫描。操作也很简单,缺点是剩下百分之20还是会侦探到远程端口,没有根治

#vi /etc/ssh/sshd_config
Port $port

No2 针对固定IP开发端口

这种方法是很多企业使用的方式,效果好,安全,能根治恶意扫描,操作也相对简单
在防火墙中添加规则

iptables -t filter -I INPUT -s $IP -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

问题是如果没有固定的IP需要搭建VPN跳板

No3 编写脚本自动拒绝恶意攻击

博主最开始就是从这角度出发的,最开始在网上找了许多shell脚本,但是都不符合博主本意。偶然看到前辈一篇博客,备受启发,于是觉得编写一个适合自己的

#!/bin/bash
#function	    DROP all failed IP if  more than $Number(default is 99)
#author         weskiller 	2014-10-31
#drop this script on /root/ext_ssh_deny/ext_ssh_deny.sh
#and add crond like this
#crontab -e
#*/30 * * * * /root/ext_ssh_deny/ext_ssh_deny.sh
#service crond start

#Check whether the iptables installed
[ -e /usr/sbin/iptables ] || { echo "iptables-services not installed" && exit 1 ;}

#set limit number
Number=

#set  IP of regular expressions
Regular_Expression_Ip='(\<(22[0-3]|2[01][0-9]|1[3-9][0-9]|12[0-689]|1[0-1][0-9]|[1-9]?[0-9]|[1-9]))(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])){2}(\.(25[0-4]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[1-9])\>)'

#set Centos ssh log file
File=/root/ext_ssh_deny/secure

#Extract failed information
FIND() {
	grep -Eo "((failure|Failed)(.*)$Regular_Expression_Ip|$Regular_Expression_Ip(.*)failed)" /var/log/secure > $File
}

#Find the attack IP
ATTACK_LIST() {
	grep -Eo "$Regular_Expression_Ip" $File |sort|uniq -c|awk '($1>'${Number:=99}') {printf $2"\n"}'|sort > /root/ext_ssh_deny/Attack_Ip
	
}

#Add iptables rule 
DENY_ATTACK_IP() {
	for IP in $1
	do
	Re_Ip=`echo $IP |sed  's|\.|\\\.|g'`
	TIMES=`grep "$Re_Ip" /root/ext_ssh_deny/secure |wc -l`
	/sbin/iptables -vnL|grep "$Re_Ip" >/dev/null 2>&1 || /sbin/iptables -I INPUT -s $IP -m state --state NEW,RELATED,ESTABLISHED -p tcp --dport 22 -j DROP && echo "`date +%Y%m%d-%H:%M:%S`    IP:$IP  TIMES=$TIMES" >> /root/ext_ssh_deny/deny.log
	done
}

#Extract iptables deny
IPTABLES_DENY_IP() {
 	/sbin/iptables -vnL|grep -Eo "DROP.*$Regular_Expression_Ip" |awk '{print $NF}'|sort > /root/ext_ssh_deny/Iptables_Deny_Ip
}

#the IP from the script log
LOG_IP() {
	grep -Eo "$Regular_Expression_Ip" /root/ext_ssh_deny/deny.log |sort > /root/ext_ssh_deny/Log_Ip 
}

#clear iptables rules and script log
CLEAR() {
	cat /dev/null > /root/ext_ssh_deny/deny.log
	sed -i '/\/32/'d /etc/sysconfig/iptables
	/sbin/service iptables restart >/dev/null 2>&1
}

#initialization
RESET(){
	CLEAR
	FIND
	ATTACK_LIST
	DENY_ATTACK_IP "`cat /root/ext_ssh_deny/Attack_Ip`"
	/sbin/service iptables save >/dev/null 2>&1
	exit 0
}

#order to refresh data
PREPARE() {
	IPTABLES_DENY_IP
	LOG_IP
}

#just add new Attack_Ip in iptables
UPDATE() {
	FIND
	ATTACK_LIST
	comm -13 /root/ext_ssh_deny/Log_Ip /root/ext_ssh_deny/Attack_Ip > /root/ext_ssh_deny/Update_Ip 
	[ -s /root/ext_ssh_deny/Update_Ip ] || exit 0
	DENY_ATTACK_IP "`cat /root/ext_ssh_deny/Update_Ip`"
	/sbin/service iptables save >/dev/null 2>&1
	exit 0
}

#check script health in order to Released  ip if a long time ago  
CHECK_HEALTH() {
	[ -f /var/log/secure ] || return 1
	[ -s /var/log/secure ] || return 1
	[ -f /root/ext_ssh_deny/deny.log ] || return 2
	[ -s /root/ext_ssh_deny/deny.log ] || return 2
	/usr/sbin/iptables -vnL|grep -Eo "DROP.*$Regular_Expression_Ip"  >/dev/null 2>&1 || return 3
	PREPARE
	[ -z "`comm -13 /root/ext_ssh_deny/Iptables_Deny_Ip /root/ext_ssh_deny/Log_Ip`" ] && return 0 || return 3
}

#start
MAIN() {
CHECK_HEALTH
case $? in
	1)
	CLEAR && return 1
	;;
	2|3)
	RESET 
	;;
	0)
	UPDATE
	;;
esac
}

MAIN

如上,脚本的功能是自动找出ssh登陆次数大于限定次数的ip,并加入防火墙中拒绝登陆,而且做到自动更新的功能
缺点是适应性不强,根据个人定制,无法满足所有人

No4 取消密码登陆,使用密钥验证

优点是操作简单,实用性很好。缺点是密钥文件需要本地存储,无法使用交互式命令如scp
生成密码文件

#ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
40:86:b1:06:62:0b:ea:76:ce:2f:28:76:2c:55:23:b7 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|o.. .oo          |
|+....+           |
|..  o .          |
|.  o + .         |
| o .+ o S        |
|. +. E           |
|  +o             |
|.+ +.            |
|o o ..           |
+-----------------+
#cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
#chmod 400 ~/.ssh/authorized_keys
#vi /etc/ssh/sshd_config
PasswordAuthentication no
#service sshd restart

No5 使用vnc远程管理

这种方式直接弃用了ssh,十分彻底的屏蔽了恶意主机的扫描和攻击,随意设定的端口也为攻击带来了难度,而且使用的真实的用户命令接口。但是问题也很明显。依旧使用口令,远程管理,流量开销大,对系统资源占用也比ssh要多,导致经常性的网络中断。建议管理内网的服务器使用