40.Redis安装及利用Redis未授权访问漏洞的攻击与预防
redis是一个开源的,给予内存也可以持久化的日志型,key-value数据库。redis在默认情况下,会绑定0.0.0.0:6379,如果没有开启认证服务,会导致任意用户可以访问目标服务器,可以成功的在redis服务器上写入公钥,从而使用相应的私钥直接登录目标服务器。
一、redis安装过程
在官网上下载redis的安装包,下载完成后解压,进入redis的安装目录
1.make

报错一:gcc未找到

解决方法:安装gcc

2.再次 make
报错二:zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录

解决方法:添加malloc=libc的参数

3.make成功后,进入src目录
make install 成功

4.make test
报错三:You need tcl 8.5 or newer in order to run the Redis test
解决方法:下载tcl源码包并安装
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
sudo tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/
cd /usr/local/tcl8.6.1/unix/
sudo ./configure
sudo make
sudo make install
5.为了方便管理,建立一个目录,将redis的配置文件和常用命令放进去

ps:redis安装步骤
二、模仿攻击
1.两台centos机器都已安装redis。
目标机器:192.168.252.130
攻击机器:192.168.252.128
1.1.查看目标机器上redis服务是否开启,若没有,则使用redis-server命令开启


开启后发现redis服务默认前台执行,再开一个终端,查看端口情况,显示redis服务确已开启

1.2.将redis服务放在后台执行,有三种办法
a.可以通过在命令后加一个&
b.命令执行和ctrl+z


1.3.修改配置文件
vim /usr/local/redis/redis.conf //将daemonize no改为yes


再此开启redis服务,就直接在后台执行了。

2.在攻击机器上配置
2.1使用ssh-keygen -t rsa会在/root/.ssh/目录下创建本地的ssh公私钥文件,id_rsa为私钥,id_rsa.pub为公钥文件
因为我这台虚拟机已经装过了,所以这边不覆盖(没有装过的,一直回车默认安装)

2.2进入/root/.ssh目录,将公钥写入foo.txt

2.3做好文件之后,尝试连接redis服务器,将foo.txt文件写入服务器中(将自己的公钥放在服务器)

报错一:连接不上
解决方法:在redis服务器上,修改配置文件/usr/local/redis/redis.conf,将bind 127.0.0.1注释


再次尝试连接redis服务器。
报错二:保护模式

解决方法:在redis服务器上,修改配置文件/usr/local/redis/redis.conf,将protected-mode yes改为no.

再次尝试,成功。

2.4连接redis服务器,并进入其/root/.ssh目录,将传入的foo.txt文件重新定义为authorized_keys。

2.5在redis服务器端,查看刚刚收到的authorized_keys(即客户机的公钥)


2.6以上步骤将自己的公钥写入到了该服务器上,就可以利用自己的私钥登录到该服务器上.
在192.168.252.128上使用ssh -i id_rsa root@192.168.252.130 命令来连接服务器
如上,已将成功连接到redis服务器上,并且具有root权限。

三、预防攻击
1.预防方案1:修改配置文件,为其设置密码。

客户机在连接redis服务器时需要输入刚刚设置的密码,否则不予理财。

2.预防方案二:修改配置文件,取消bind 127.0.0.1的注释

客户机在连接redis服务器的时候会直接被拒绝。

根据实验吧实验编写:redis未授权访问漏洞利用与修复