Docs/redis/redis安全加固.md
2022-10-18 16:59:37 +08:00

1.9 KiB
Raw Blame History

新建用户

使用root用户若被攻入安全风险急剧升高

groupadd -g 1000 appgroup
useradd -u 1001 -g 1000 app

修改配置文件

修改密码

密码足够长足够复杂因为redis速度很快简单的密码很容易被暴力破解

# 修改配置文件redis.cnf中requirepass
...
requirepass xxxxxxxxxxxxxxx
...

禁用或重命名某些命令

redis若被攻入攻击者可能使用config配置redis或者shutdown干掉redis或者flushdb/flushall清空redis

# 配置文件redis.cnf中添加以下配置
...
## 代码中需要修改redis配置config暂不能重命名
#rename-command CONFIG CONFIG_524045429941cc 
rename-command SHUTDOWN SHUTDOWN_9941cc15f59e41cb
rename-command FLUSHDB ""
rename-command FLUSHALL ""
...

监听本地或者在安全网络中监听所有ip

redis目前没有配置项可以使其只处理特定ip的请求若所有ip都可以访问风险很大

# 监听本地 redis.cnf
bind 127.0.0.1

或添加防火墙配置

# 只允许内网网段访问6379
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="172.19.0.0/16" port protocol="tcp" port="6379" accept"
# 只允许特定ip访问6379
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="123.146.236.55" port protocol="tcp" port="6379" accept"
# 重新加载firewall
firewall-cmd --reload

若为公有云服务器,可使用安全组

# 配置安全组,略
# 监听所有 redis.cnf
bind 0.0.0.0

启动redis

若之前已经使用root启动redis的数据和日志文件都需要修改所属用户与组

# 切换到app用户然后启动redis
su - app
/opt/redis-stable/src/redis-server /opt/redis-stable/redis.cnf
# 或者直接再root中执行如下命令
su - app -c "/opt/redis-stable/src/redis-server /opt/redis-stable/redis.cnf"