I found that when I put \r\n
in the key to get the value from memcached server, it can be used to inject the memcached protocol to run another command. Following is the code example,
In PHP:
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$key = "foo\r\nset bar 0 10 10000\r\n";
echo $m->get($key);
echo $m->get($key); // <- hang here
?>
In Python:
import pylibmc
mc = pylibmc.Client(['127.0.0.1:11211'])
key = 'foo\r\nset bar 0 10 10000\r\n';
print mc.get(key)
print mc.get(key) # <- hang here
If key
is read from the user, a user could execute arbitrary commands in the memcached server, such as to run flush_all
to slow down the website or set the password cache for another user.
Why the client library doesn't strip these illegal characters? How to properly prevent it in my code?