If you are running a modern web application, chances are you are using an in-memory data structure store like Redis to handle session caching, background task queues, or real-time analytics. Because Redis is designed for absolute maximum performance, it is built with the assumption that it is running inside a deeply trusted, secure network.

By default, older versions of Redis lack authentication and bind to all available network interfaces. If you install Redis on a VPS and fail to secure it, attackers can scan your public IP, connect to the default port (6379), and gain complete read/write access to your cache. This is one of the most common vectors for deploying cryptocurrency miners on compromised servers, as attackers can use Redis to overwrite authorized SSH keys and gain root access.

Binding to the Local Loopback Interface

The single most critical step in securing Redis is ensuring it is completely inaccessible from the public internet. If your application and your Redis instance are hosted on the exact same VPS, you must restrict its listening interface.

  • Connect to your server via SSH and open the main Redis configuration file, typically located at /etc/redis/redis.conf.

  • Search the file for the bind directive.

  • If it is set to bind 0.0.0.0 or includes your server's public IP address, change it immediately to bind strictly to the localhost: bind 127.0.0.1 ::1

  • Additionally, locate the protected-mode directive. Ensure it is set to yes. When enabled, protected mode acts as a fail-safe that refuses external connections if no password is set and no specific bind address is configured.

Enabling Strong Authentication

Even if Redis is bound to localhost, applying defense-in-depth is essential. If an attacker manages to exploit a Server-Side Request Forgery (SSRF) vulnerability in your web application, they could trick your own server into sending malicious commands to the local Redis instance. Requiring a password mitigates this risk.

  • In your redis.conf file, search for the requirepass directive.

  • Uncomment the line and add an exceptionally long, complex password. Because Redis is incredibly fast, it can process millions of password guesses per second, meaning standard passwords can be brute-forced instantly. Generate a random cryptographic string (e.g., using openssl rand -hex 32).

  • Example: requirepass a7b8c9d0e1f2g3h4i5j6k7l8m9n0...

  • Once configured, your application's connection string must be updated to include this password (e.g., redis://:your_password@127.0.0.1:6379).

Disabling or Renaming Dangerous Commands

Redis includes several administrative commands that allow users to flush the entire database, reconfigure server parameters on the fly, or save the database to specific file paths on the Linux disk (which is exactly how attackers overwrite SSH authorized_keys).

If standard application users do not need these commands, you should completely disable them or rename them to unguessable strings directly in the configuration file.

  • Scroll down to the SECURITY section of your redis.conf file.

  • Use the rename-command directive to neutralize dangerous commands by renaming them to an empty string "".

  • Add the following lines to disable the most commonly abused administrative functions:

    Plaintext
     
    rename-command FLUSHDB ""
    rename-command FLUSHALL ""
    rename-command CONFIG ""
    rename-command SHUTDOWN ""
    rename-command DEBUG ""
    
  • If your system administrator occasionally needs to use the CONFIG command for tuning, you can rename it to a secret, hard-to-guess value instead of disabling it entirely (e.g., rename-command CONFIG "CONFIG_b7f9a2").

Applying the Configuration

After saving your changes to the configuration file, you must restart the Redis service for the new security rules to take effect.

  • Restart the service by running: sudo systemctl restart redis-server (or redis depending on your Linux distribution).

  • Test your connection from the command line by running redis-cli.

  • Attempt to run a simple command like PING. The server should reject it with an authentication error.

  • Authenticate by typing AUTH your_secure_password. The server should reply with OK, confirming that your database is now locked down.

Ця відповідь Вам допомогла? 0 Користувачі, які знайшли це корисним (0 Голосів)