11211 - Pentesting Memcache

Protocol Information

From wikipedia:

Memcached (pronunciation: mem-cashed, mem-cash-dee) is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Although Memcached supports SASL, most instances are exposed without authentication.

Default port: 11211

PORT      STATE SERVICE
11211/tcp open  unknown

Enumeration

Manual

To exfiltrate all the information saved inside a memcache instance you need to:

  1. Find slabs with active items

  2. Get the key names of the slabs detected before

  3. Ex-filtrate the saved data by getting the key names

Remember that this service is just a cache, so data may be appearing and disappearing.

Manual2

Automatic

Dumping Memcache Keys

In the realm of memcache, a protocol that assists in organizing data by slabs, specific commands exist for inspecting the stored data, albeit with notable constraints:

  1. Keys can only be dumped by slab class, grouping keys of similar content size.

  2. A limit exists of one page per slab class, equating to 1MB of data.

  3. This feature is unofficial and may be discontinued at any time, as discussed in community forums.

The limitation of only being able to dump 1MB from potentially gigabytes of data is particularly significant. However, this functionality can still offer insights into key usage patterns, depending on specific needs. For those less interested in the mechanics, a visit to the tools section reveals utilities for comprehensive dumping. Alternatively, the process of using telnet for direct interaction with memcached setups is outlined below.

How it Works

Memcache's memory organization is pivotal. Initiating memcache with the "-vv" option reveals the slab classes it generates, as shown below:

To display all currently existing slabs, the following command is used:

Adding a single key to memcached 1.4.13 illustrates how slab classes are populated and managed. For instance:

Executing the "stats slabs" command post key addition yields detailed statistics about slab utilization:

This output reveals the active slab types, utilized chunks, and operational statistics, offering insights into the efficiency of read and write operations.

Another useful command, "stats items", provides data on evictions, memory constraints, and item lifecycles:

These statistics allow for educated assumptions about application caching behavior, including cache efficiency for different content sizes, memory allocation, and capacity for caching large objects.

Dumping Keys

For versions prior to 1.4.31, keys are dumped by slab class using:

For example, to dump a key in class #1:

This method iterates over slab classes, extracting and optionally dumping key values.

DUMPING MEMCACHE KEYS (VER 1.4.31+)

With memcache version 1.4.31 and above, a new, safer method for dumping keys in a production environment is introduced, utilizing non-blocking mode as detailed in the release notes. This approach generates extensive output, hence the recommendation to employ the 'nc' command for efficiency. Examples include:

DUMPING TOOLS

Table from here.

Programming Languages
Tools
Functionality

PHP

Prints key names.

Perl

Prints keys and values

Ruby

Prints key names.

Perl

Tool in CPAN module

ached/)

PHP

Memcache Monitoring GUI that also allows dumping keys

libmemcached

Does freeze your memcached process!!! Be careful when using this in production. Still using it you can workaround the 1MB limitation and really dump all keys.

Troubleshooting

1MB Data Limit

Note that prio to memcached 1.4 you cannot store objects larger than 1MB due to the default maximum slab size.

Never Set a Timeout > 30 Days!

If you try to “set” or “add” a key with a timeout bigger than the allowed maximum you might not get what you expect because memcached then treats the value as a Unix timestamp. Also if the timestamp is in the past it will do nothing at all. Your command will silently fail.

So if you want to use the maximum lifetime specify 2592000. Example:

Disappearing Keys on Overflow

Despite the documentation saying something about wrapping around 64bit overflowing a value using “incr” causes the value to disappear. It needs to be created using “add”/”set” again.

Replication

memcached itself does not support replication. If you really need it you need to use 3rd party solutions:

Commands Cheat-Sheet

Memcache Commands

Shodan

  • port:11211 "STAT pid"

  • "STAT pid"

References

Last updated