一個危險 Redis 命令,導致公司損失 400 萬!


什麼樣的 Redis 命令會有如此威力,造成如此大的損失?

具體消息如下:

據云頭條報道,某公司技術部發生 2 起本年度 PO 級特大事故,造成公司資金損失 400 萬,原因如下:由於 PHP 工程師直接操作上線 redis,執行 keys * wxdb(此處省略)cf8* 這樣的命令,導致redis鎖住,導致 CPU 飆升,引起所有支付鏈路卡住,等十幾秒結束後,所有的請求流量全部擠壓到了 rds 數據庫中,使數據庫產生了雪崩效應,發生了數據庫宕機事件。該公司表示,如再犯類似事故,將直接開除,並表示之後會逐步收回運維部各項權限。

看完這個消息後,我心又一驚,為什麼這麼低級的問題還在犯?為什麼線上的危險命令沒有被禁用?這事件報道出來真是覺得很低級。。。

且不說是哪家公司,發生這樣的事故,不管是大公司還是小公司,我覺得都不應該,相關負責人應該引咎辭職!!!

對 Redis 稍微有點使用經驗的人都知道線上是不能執行 keys * 相關命令的,雖然其模糊匹配功能使用非常方便也很強大,在小數據量情況下使用沒什麼問題,數據量大會導致 Redis 鎖住及 CPU 飆升,在生產環境建議禁用或者重命名!

還有哪些危險命令?

Redis 的危險命令主要有以下幾個:

  • keys


客戶端可查詢出所有存在的鍵。

  • flushdb


Delete all the keys of the currently selected DB. This command never fails.

刪除 Redis 中當前所在數據庫中的所有記錄,並且此命令從不會執行失敗。

  • flushall


Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.

刪除 Redis 中所有數據庫中的所有記錄,不只是當前所在數據庫,並且此命令從不會執行失敗。

  • config


客戶端可修改 Redis 配置。

怎麼禁用或重命名危險命令?

看下 redis.conf 默認配置文件,找到 SECURITY 區域,如以下所示。

################################## SECURITY ###################################
# Require clients to issue AUTH before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the

# AOF file or transmitted to slaves may cause problems.

看說明,添加 rename-command 配置即可達到安全目的。

1)禁用命令

rename-command KEYS ""
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""

2)重命名命令

rename-command KEYS "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command FLUSHALL "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command FLUSHDB "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command CONFIG "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

上面的 XX 可以定義新命令名稱,或者用隨機字符代替。

經過以上的設置之後,危險命令就不會被客戶端執行了。


分享到:


相關文章: