11211 - Pentesting Memcache
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
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
To exfiltrate all the information saved inside a memcache instance you need to:
Find slabs with active items
Get the key names of the slabs detected before
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.
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:
Keys can only be dumped by slab class, grouping keys of similar content size.
A limit exists of one page per slab class, equating to 1MB of data.
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.
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.
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.
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:
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. |
Note that prio to memcached 1.4 you cannot store objects larger than 1MB due to the default maximum slab size.
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:
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.
memcached itself does not support replication. If you really need it you need to use 3rd party solutions:
repcached: Multi-master async replication (memcached 1.2 patch set)
Couchbase memcached interface: Use CouchBase as memcached drop-in
yrmcds: memcached compatible Master-Slave key value store
twemproxy (aka nutcracker): proxy with memcached support
port:11211 "STAT pid"
"STAT pid"
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)