Chuyển tới nội dung chính

Bảo Mật Redis

1. Giới Hạn Network Access

1.1 Bind vào Localhost

# Chỉnh sửa redis.conf
sudo nano /etc/redis/redis.conf

# Đảm bảo dòng sau được uncomment
bind 127.0.0.1 ::1

# Khởi động lại Redis
sudo systemctl restart redis

# Kiểm tra binding
sudo netstat -lnp | grep redis
mẹo

Nếu không có lệnh netstat:

sudo apt install net-tools

2. Cấu Hình Authentication

2.1 Thiết Lập Mật Khẩu

# Tạo mật khẩu mạnh
openssl rand 60 | openssl base64 -A

# Chỉnh sửa redis.conf
sudo nano /etc/redis/redis.conf

# Uncomment và cập nhật requirepass
requirepass your_strong_password

2.2 Kiểm Tra Authentication

# Kết nối Redis CLI
redis-cli

# Test command khi chưa auth
127.0.0.1:6379> set key1 10
(error) NOAUTH Authentication required.

# Auth với mật khẩu
127.0.0.1:6379> auth your_redis_password
OK

# Test lại sau khi auth
127.0.0.1:6379> set key1 10
OK

3. Vô Hiệu Hóa Các Command Nguy Hiểm

3.1 Rename hoặc Disable Commands

# Chỉnh sửa redis.conf
sudo nano /etc/redis/redis.conf

# Disable commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""

# Rename commands
rename-command CONFIG ASC12_CONFIG
rename-command SHUTDOWN SHUTDOWN_MENOT
Lưu ý về Rename Commands
  • Chỉ rename các commands khi chưa sử dụng AOF persistence
  • Đảm bảo rename giống nhau trên tất cả các instances trong môi trường master-slave
  • Ghi nhớ các tên mới của commands đã rename

4. Sử Dụng Redis ACLs (Redis 6.0+)

4.1 Cấu Hình ACL File

# Tạo ACL file
sudo nano /etc/redis/users.acl

# Thêm cấu hình users
user default on +@all ~* >strong-password-here
user app_user on +@read +@write -@dangerous ~* >another-strong-password

4.2 Enable ACL File

# Trong redis.conf
aclfile /etc/redis/users.acl

5. Các Biện Pháp Bảo Mật Khác

  1. Firewall Rules
# Chỉ cho phép các IP cần thiết
sudo ufw allow from trusted_ip_address to any port 6379
  1. Giới Hạn Resources
# Trong redis.conf
maxmemory 2gb
maxmemory-policy allkeys-lru
  1. Monitoring và Logging
# Enable verbose logging
loglevel notice
logfile /var/log/redis/redis-server.log

Tài Liệu Tham Khảo