memcached是什么
memcached 是一套被广泛使用的开源高性能的、分布式内存对象缓存系统。授权协议为BSD license,主要开发语言为C语言,数据以Key/Value方式存储在内存中。
为什么要认证
memcached基于C/S架构,OpenStack的Nova等组件使用memcached作为缓存系统,由于memcached默认不开启认证机制,导致客户端无需认证即可读取、修改缓存内容。
安全问题详见:http://blog.nsfocus.net/memcache-unauthorized-access-exploit/
本文介绍如何给memcached增加认证机制。
memcached认证机制
memcached目前支持的认证方式为SASL,由于memcached在1.4.3版本才加入对SASL的支持,所以要使用认证的话需要将memcached升级到>=1.4.3的版本。
SASL是什么
SASL全称Simple Authentication and Security Layer(简单认证与安全层),是一种用来扩充C/S模式验证能力的机制,SASL只是认证过程,将应用层与系统认证机制整合起来,SASL本身不能进行认证。
SASL支持的认证方法为:getpwent kerberos5 pam rimap shadow ldap httpform。
实现方式
1.安装sasl相关包
1
| yum -y install cyrus-sasl cyrus-sasl-lib cyrus-sasl-devel cyrus-sasl-plain dovecot
|
2.修改saslauthd验证方式
3.开启saslauthd服务
1 2
| systemctl start saslauthd systemctl enable saslauthd
|
4.查看saslauthd服务及版本
1 2 3
| [root@localhost init.d] saslauthd 2.1.26 authentication mechanisms: getpwent kerberos5 pam rimap shadow ldap httpform
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [root@localhost init.d]# systemctl status saslauthd ● saslauthd.service - SASL authentication daemon. Loaded: loaded (/usr/lib/systemd/system/saslauthd.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2024-05-06 16:43:38 CST; 1s ago Process: 13808 ExecStart=/usr/sbin/saslauthd -m $SOCKETDIR -a $MECH $FLAGS (code=exited, status=0/SUCCESS) Main PID: 13809 (saslauthd) CGroup: /system.slice/saslauthd.service ├─13809 /usr/sbin/saslauthd -m /run/saslauthd -a shadow ├─13810 /usr/sbin/saslauthd -m /run/saslauthd -a shadow ├─13811 /usr/sbin/saslauthd -m /run/saslauthd -a shadow ├─13812 /usr/sbin/saslauthd -m /run/saslauthd -a shadow └─13813 /usr/sbin/saslauthd -m /run/saslauthd -a shadow
May 06 16:43:38 localhost.localdomain systemd[1]: Starting SASL authentication daemon.... May 06 16:43:38 localhost.localdomain saslauthd[13809]: detach_tty : master pid is: 13809 May 06 16:43:38 localhost.localdomain saslauthd[13809]: ipc_init : listening on socket: /run/saslauthd/mux May 06 16:43:38 localhost.localdomain systemd[1]: Started SASL authentication daemon..
|
5.添加用户给指定的程序,对memcached服务程序设置SASL认证密码。
1 2 3 4
| saslpasswd2 -a memcached -c memcached
密码: memcached 确认密码: memcached
|
6.查看当前SASL用户
1 2
| [root@localhost init.d]# sasldblistusers2 memcached@localhost.localdomain: userPassword
|
7.由于yum安装memcached,已默认生成linux用户memcached。所以下面直接绑定这个用户,注意命令完毕之后再输入跟LINUX下memcahed账号密码一样即可。
1 2 3 4
| passwd memcached
密码: memcached 确认密码: memcached
|
8.测试生成绑定的用户是否成功,进行验证,必须是Linux用户
1 2
| [root@localhost init.d]# testsaslauthd -u memcached -p 'memcached' 0: OK "Success."
|
9.由于 SASL 账户密码默认保存在 /etc/sasldb2 文件里,但是该文件权限为 0640 或者 0660,对于运行 Memcached 的 user/group 不可读,因此需要做一些权限上面的修改。
1 2
| sudo chown :memcached /etc/sasldb2 chmod 755 /etc/sasldb2
|
10.需要授予从saslauthd引用 /etc/shadow 的权限
1 2 3 4 5
| 一次性: setsebool saslauthd_read_shadow on
永久: setsebool -P saslauthd_read_shadow on
|
11.在 memcached 的配置文件,或者启动参数中,使用 ** -S ** 即可开启 SASL 认证
1 2 3 4 5 6 7
| vim /etc/sysconfig/memcached
PORT="11222" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="-S"
|
1 2
| /usr/bin/memcached -S -d -vvv -P /var/www/kdpui/kdpui/memcached.pid \ -u memcached -l 127.0.0.1:11222 -m 8192 -M -c 1024 > /var/log/memcached.log 2>&1 &
|
12.bmemcached安装 python-binary-memcached-0.31.2.tar.gz
1 2 3
| tar -zxvf python-binary-memcached-0.31.2.tar.gz cd python-binary-memcached-0.31.2 python3 setup.py install
|
13.django-bmemcached安装 django-bmemcached-0.3.0.tar.gz
1
| pip3 install django-bmemcached-0.3.0.tar.gz
|
14.django配置
1 2 3 4 5 6 7 8 9 10 11 12
| CACHES = { 'default': { 'BACKEND': 'django_bmemcached.memcached.BMemcached', 'LOCATION': '127.0.0.1:11222', 'OPTIONS': { 'username': 'memcached', 'password': 'memcached', 'compression': None, 'pickle_protocol': 0 } } }
|