Api Key Exposure in Redis
How API Key Exposure Manifests in Redis
API keys are often stored as string values in Redis for fast lookup by microservices. When a Redis instance is exposed without authentication or with weak access controls, an attacker can enumerate keys and extract those values. Common manifestations include:
- Unauthenticated TCP access to the default port 6379 (or a TLS‑wrapped port) allowing the use of
redis-clior any client to runKEYS *orSCAN 0to list all keys. - Abuse of administrative commands such as
CONFIG GET *,INFO, orDEBUG OBJECTthat return server configuration or internal metadata, which can reveal key names or indirectly aid in key discovery. - Lua sandbox escapes (e.g., CVE‑2021‑32761) that let an attacker execute arbitrary Lua scripts, enabling them to iterate over the key space and exfiltrate values stored as API keys.
- ACL bypass flaws like CVE‑2022‑24834, where a malformed username can bypass
+@allrestrictions and grant execution of normally prohibited commands, again leading to key disclosure.
Once the attacker has a list of keys, they can issue GET, MGET, or hash‑specific commands (HGETALL, HMGET) to harvest the API key values. Because Redis is an in‑memory store, the exposure is immediate and does not rely on logs or backups.
Redis-Specific Detection of API Key Exposure
Detecting exposed API keys in Redis requires probing the unauthenticated surface and inspecting returned values for patterns that resemble secrets. middleBrick performs the following checks when pointed at a Redis host:
- Connection attempt without credentials; if the server responds with a
+PONGtoPING, the instance is considered unauthenticated. - Execution of
INFOto collect server version, mode, and enabled modules, which helps correlate findings with known CVEs (e.g., checking for versions vulnerable to CVE‑2021‑32761). - Running
CONFIG GET *(if not blocked) to see whether dangerous settings likeprotected-mode noorbind 0.0.0.0are present. - Scanning the key space with
SCAN 0 COUNT 1000(orKEYS *if permitted) to gather up to a few thousand keys, then sampling their values withGETorMGET. - Applying entropy‑based and regex heuristics to sampled values to detect strings that match common API key formats (e.g., AWS Access Key IDs, Stripe live keys, GitHub personal tokens, Azure SAS tokens). When a match is found, middleBrick flags the finding as an API key exposure with severity high.
The scanner does not attempt to modify data; it only reads what is openly accessible. Findings are reported with the exact key name, the masked value (showing only the first and last two characters), and remediation guidance.
Redis-Specific Remediation for API Key Exposure
Securing Redis against API key leakage involves a combination of authentication, network hardening, command restriction, and secret‑management best practices.
| Control | Implementation |
|---|---|
| Enable authentication | Set a strong requirepass in redis.conf or use ACL users with ACL SETUSER. Example: |
| |
| Restrict dangerous commands | Rename or disable commands that can leak information: |
| |
| Bind to trusted interfaces | Avoid binding to 0.0.0.0; listen only on private IPs or localhost: |
| |
| Encrypt traffic | Enable TLS (Redis 6+) and require client certificates: |
| |
| Use ACLs to limit command sets | Create a user that can only execute GET, SET on specific key patterns: |
| |
| Avoid storing raw secrets | Prefer to store only references (e.g., a UUID) that map to a secret kept in a dedicated vault (AWS Secrets Manager, HashiCorp Vault). Retrieve the secret at runtime. |
| Rotate and monitor | Rotate any API keys found in Redis immediately and enable alerts for new key‑value writes matching high‑entropy patterns. |
Example of a secure client connection in Python (redis‑py):
import redis
r = redis.Redis(
host='redis.internal.example.com',
port=6379,
username='appuser',
password='v3ry$tr0ngP@ssw0rd!',
ssl=True,
ssl_cert_reqs='required'
)
# Safe operation – only allowed keys
value = r.get('api_keys:payment_gateway')
Using the middleBrick CLI to verify the fix:
middlebrick scan redis://redis.internal.example.com:6379
The scan will now return an authentication failure and no API key findings, confirming that the exposure has been mitigated.
Frequently Asked Questions
Can middleBrick detect API keys stored in Redis hashes or streams?
HGETALL; for streams it uses XRANGE to sample entries. The same entropy‑based and regex heuristics are applied to each field or entry value to spot API keys.Is it safe to rely solely on renaming dangerous commands like KEYS and CONFIG to prevent key exposure?
requirepass/ACLs, TLS, and binding to private interfaces.