您的位置:首页 > 数据库 > Redis

Redis Essentials 读书笔记 - 第七章: Security Techniques (Guard Your Data)

2016-05-05 23:13 579 查看

Chapter 7. Security Techniques (Guard Your Data)

Redis被设计成运行于可信的私有网络,与服务器的通讯仅支持未加密的口令。

一些命令如FLUSHALL会导致数据全部丢失。

本章介绍一些如何提升Redis的安全机制。

The basic security

Redis的设计主要考虑性能和使用简单,而非最大安全性。Redis仅支持平文本口令,而没有实现ACL,无法为用户指定不同的权限。

requirepass可以要求客户端输入口令,口令应尽量复杂。

可以在配置文件redis.conf 中设置口令,每次配置文件改变,都需要重启Redis以使其生效。

例如:

# redis.conf
requirepass a7f$f35eceb7e@3edd502D892f5885007869dd2f80434Fed5b4!fac0057f51fM


重启服务:

$ redis-server /path/to/chapter7/redis.conf


客户端使用AUTH来提供口令, 不过AUTH的口令在网络中也是不加密的:

$ redis-cli
127.0.0.1:6379> AUTH a7f$f35eceb7e@3edd502D892f5885007869dd2f80434Fed5b4!fac0057f51fM
OK


Obfuscating critical commands

可以禁止和obfuscating一些关键命令,如FLUSHDB, FLUSHALL, CONFIG, KEYS, DEBUG, 和 SAVE,这些命令可能会破坏你的数据和造成安全隐患。

禁止命令可以将其设成空串,当这些命令还需要使用时,obfuscating命令可以将命令重命名为复杂的难以猜到的字符串。

Create a file called in the chapter 7 folder with the following code:

建议创建一个 renamed-commands.conf 文件,包含禁止和obfuscating操作,例如:

rename-command FLUSHDB e0cc96ad2eab73c2c347011806a76b73
rename-command FLUSHALL a31907b21c437f46808ea49322c91d23a
rename-command CONFIG ""
rename-command KEYS ""
rename-command DEBUG ""
rename-command SAVE ""


然后在redis.conf中引用renamed-commands.conf 文件:

include /path/to/chapter7/rename-commands.conf


设置后,效果如下:

$ redis-cli
127.0.0.1:6379> SAVE
(error) ERR unknown command 'SAVE'
127.0.0.1:6379> FLUSHALL
(error) ERR unknown command 'FLUSHALL'
127.0.0.1:6379> a31907b21c437f46808ea49322c91d23a
OK


让我们回顾下这些重要命令:

[redis@tt12c ~]$ redis-cli
127.0.0.1:6379> help flushall

FLUSHALL - 数据丢失风险
summary: Remove all keys from all databases
since: 1.0.0
group: server

127.0.0.1:6379> help flushdb

FLUSHDB - 数据丢失风险
summary: Remove all keys from the current database
since: 1.0.0
group: server

127.0.0.1:6379> help keys

KEYS pattern - 遍历所有的key,阻塞客户端访问,性能影响
summary: Find all keys matching the given pattern
since: 1.0.0
group: generic

127.0.0.1:6379> DEBUG SEGFAULT - 使Redis服务器崩溃

127.0.0.1:6379> help save - 阻塞客户端访问,性能影响

SAVE -
summary: Synchronously save the dataset to disk
since: 1.0.0
group: server


Networking security

Redis建议运行于私有可行网络,但有时也会运行在公有云中,则会带来安全问题。

Redis的一些安全措施包括:

* 使用防火墙来阻塞未知客户端访问

* Redis运行在loopback接口,而非可访问的网络接口。

* Redis运行在虚拟私有云而非公有云

* 对客户端和服务器之间的数据通讯加密

Protecting Redis with firewall rules

Redis通常运行在Linux下,在Linux中,可以通过iptables来设置防火墙规则。

iptables可以控制数据的进出和转发(INPUT, OUTPUT, FORWARD)。

如果Redis运行在公有云如AWS中,就必须依赖云提供商的工具来配置防火墙。

It is more flexible to create firewall rules based on security group IDs than on CIDR blocks.

Running Redis on the loopback network interface

将redis服务器绑定在虚拟的loopback接口(127.0.0.1)上,可以阻塞外部的访问,不过估计应用程序也需要跑在此服务器上。

在redis.conf文件中设置如下:

bind 127.0.0.1


unning Redis in a Virtual Private Cloud

目前,一些公有云提供商提供Virtual Private Clouds (VPCs), VPC有利于提高Redis的安全性,运行Redis于VPC中是一个不错的选择。

VPC可以在同一个公有云中将不同的用户做虚拟的隔离。只有在一个VPC中的机器可以相互访问。

VPCs may sound extremely complicated, but cloud providers make them simpler to work with, and it is a great idea to run Redis on a VPC.

在VPC中启用防火墙控制也是一个不错的选择。

Encrypting client-to-server communication

缺省Redis不提供网络加密,需要额外的工具来支持。使用SSL加密Redis通讯数据可防止网络侦听,确保可信的客户端访问。

Redis可以使用的SSL加密工具为stunnel,可以有两种方法:

1. 在客户端和服务器端都运行stunnel, 使用相同的private key

2. 仅在服务器端运行stunnel, 但Redis客户端必须支持SSL

具体设置方法略。

Benjamin Cane有一篇很好的博客介绍如何让Redis使用stunnel, 可参见:http://bencane.com/2014/02/18/sending-redis-traffic-through-an-ssl-tunnel-with-stunnel/

Summary

总之,Redis的安全较弱,毕竟其主要目的是性能和使用简便性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: