自学内容网 自学内容网

Redis性能瓶颈与安全隐患排查验证纪实

在写《Redis怎样保证数据安全?》这篇文章,我是有对redis设置密码需要哪些步骤,设置密码的性能损耗有验证的。这就涉及到要对redis的配置做修改。

开始时我是打算采用直接使用redis配置文件的方式。所以我从redis官网下载了一个默认的配置文件,就打算只将这个配置文件的requirepass注释打开,设置一个密码。

00d1f1c2ef5c1c6e14d757a1d8920f8a.png

redis我用docker启动,如果 docker run -p 6379:6379 --name ai-redis -v c:/docker/redis.conf:/etc/redis/redis.conf -d redis/redis-stack-server redis-server /etc/redis/redis.conf

使用本机的redis.conf文件拷贝一份到docker容器中启动,docker里可以正常运行,用docker内的redis-cli可以正常访问服务端,但是使用主机的java程序访问就会报 Unexpected end of stream.; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.

我到咱们「编程一生」用户交流群里,问出了我的问题。咱们群里的朋友提示我说:telnet 可以验证下redis响应

我其实在他说之前是验证过的,当时telnet没有反应。既然人家说了,我就又试了一下。其实这次测试的配置和上次不一样了。我为了测试是不是我redis.conf文件的格式或者编码有问题。redis.conf直接弄了一个空文件,自然也是没有密码的。这时候给出了下面的提示(java客户端连提示和telnet的是一样的):

DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

这么具体的问题,第一反应当然是问AI工具了。AI给了这个答案:

您所描述的信息是Redis的一个安全特性。当Redis在没有设置密码的情况下运行时,它会进入保护模式(protected mode)。在这种模式下,Redis仅接受来自本地回环接口(loopback interface)的连接,这通常意味着只能从运行Redis的同一台机器上进行连接。这是为了防止未经授权的外部访问。

如果您希望从外部计算机连接到Redis,您有几个选择:

  1. 禁用保护模式:您可以通过在本地回环接口上连接到Redis,并发送命令CONFIG SET protected-mode no来禁用保护模式。但是,请注意,这会增加安全风险,因为任何能够访问Redis服务器的机器都可以进行连接,除非您还实施了其他安全措施(如防火墙规则)。

  2. 设置密码:您可以在Redis配置文件中设置密码,方法是修改requirepass字段。例如,在redis.conf文件中,您可以添加或修改以下行:

    requirepass your_strong_password

    然后,您需要重启Redis服务器以使更改生效。设置密码后,您需要在连接到Redis时提供该密码。

根据提示,redis配置改成不允许本地接口外运行 protected-mode yes。同样,docker里可以正常运行。但从客户端连接,结果是:

 ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?

另外一个朋友提示我抓包看一下,我就犯愁犯懒了。我是在windows上做的验证,默认不能使用tcmpdump。我是安装了wireshark的。wireshark貌似只能抓tcp和udp协议的包,不支持redis协议。有朋友能给点思路,对我这种懒人在这种情况下怎么抓包吗?

但是根据目前的情况:Redis仅接受来自本地回环接口(loopback interface)的连接,我想到了bind的配置

980d6cd37a26a4bca827f06ca3c71694.png

实际验证,默认bind是127.0.0.1。手动加上

bind 0.0.0.0

问题解决。回归验证时,我建立了一个空redis.conf,只有两样配置,也同样验证问题解决

272e044e641beac8be94b06446feb81e.png

但实际上这个问题与我真正的验证目标:对redis设置密码需要哪些步骤,设置密码的性能损耗做验证,是跑题的。

所以当时我并没有先全力去解决这个问题,而是使用了另外一种验证方式,不使用redis.conf文件,而是启动参数里直接指定参数,先验证了真正的目标之后再来解决的。启动参数如下:

docker run -p 6379:6379 --name ai-redis  -d redis/redis-stack-server redis-server --requirepass 123456 --protected-mode yes       

之后,我重新看了一下群里的聊天记录

f4d3df84d349530c763a7781fc811d05.png

群里朋友都给出我正确答案了,我没有领会到!!!

总结

总结一下本次排查的过程步骤:

我的整个排查过程使用的是《性能之巅》里介绍的科学法。

科学法采用以下框架:问题->假设->预测->实验->分析。在分析过程中,可以有效利用各种资源:AI技术、网上资料、仔细阅读Redis的错误提示,咨询群里的朋友。此外,在验证问题的过程中,不要因为新的问题,迷失了原本的目标,这里就根据具体目标和问题特点采取合适的验证方法,如直接指定启动参数来验证目标。


原文地址:https://blog.csdn.net/xiexiaojing/article/details/137252437

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!