mirror of
https://github.com/carlospolop/hacktricks
synced 2024-12-13 06:42:54 +00:00
229 lines
17 KiB
Markdown
229 lines
17 KiB
Markdown
# Memcache 命令
|
||
|
||
<details>
|
||
|
||
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks 云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
||
|
||
* 你在一家 **网络安全公司** 工作吗?你想在 HackTricks 中看到你的 **公司广告**吗?或者你想获得 **PEASS 的最新版本或下载 HackTricks 的 PDF 版本**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 发现我们的独家 [**NFTs**](https://opensea.io/collection/the-peass-family) 收藏品 - [**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
||
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
|
||
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**Telegram 群组**](https://t.me/peass),或者在 **Twitter** 上 **关注**我 [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
|
||
* **通过向** [**hacktricks 仓库**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud 仓库**](https://github.com/carlospolop/hacktricks-cloud) **提交 PR 来分享你的黑客技巧。**
|
||
|
||
</details>
|
||
|
||
## 命令速查表
|
||
|
||
**来自** [**https://lzone.de/cheat-sheet/memcached**](https://lzone.de/cheat-sheet/memcached)\*\*\*\*
|
||
|
||
支持的命令(官方命令和一些非官方命令)在 [doc/protocol.txt](https://github.com/memcached/memcached/blob/master/doc/protocol.txt) 文档中有记录。
|
||
|
||
遗憾的是,语法描述并不清晰,一个简单的帮助命令列出现有的命令会更好。以下是你可以在 [source](https://github.com/memcached/memcached) 中找到的命令概述(截至 2016 年 8 月 19 日):
|
||
|
||
| 命令 | 描述 | 示例 |
|
||
| --------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||
| get | 读取值 | `get mykey` |
|
||
| set | 无条件设置键 | <p><code>set mykey <flags> <ttl> <size></code><br><br><p>在使用 Unix CLI 工具时,请确保使用 \r\n 作为换行符。例如</p> <code>printf "set mykey 0 60 4\r\ndata\r\n" | nc localhost 11211</code></p> |
|
||
| add | 添加新键 | `add newkey 0 60 5` |
|
||
| replace | 覆盖现有键 | `replace key 0 60 5` |
|
||
| append | 追加数据到现有键 | `append key 0 60 15` |
|
||
| prepend | 在现有键前添加数据 | `prepend key 0 60 15` |
|
||
| incr | 将数值键的值增加给定的数字 | `incr mykey 2` |
|
||
| decr | 将数值键的值减去给定的数字 | `decr mykey 5` |
|
||
| delete | 删除现有键 | `delete mykey` |
|
||
| flush\_all | 立即使所有项目失效 | `flush_all` |
|
||
| flush\_all | 在 n 秒内使所有项目失效 | `flush_all 900` |
|
||
| stats | 打印常规统计信息 | `stats` |
|
||
| | 打印内存统计信息 | `stats slabs` |
|
||
| | 打印更高级别的分配统计信息 | `stats malloc` |
|
||
| | 打印项目信息 | `stats items` |
|
||
| | | `stats detail` |
|
||
| | | `stats sizes` |
|
||
| | 重置统计计数器 | `stats reset` |
|
||
| lru\_crawler metadump | 转储缓存中(大部分)项目的元数据 | `lru_crawler metadump all` |
|
||
| version | 打印服务器版本 | `version` |
|
||
| verbosity | 增加日志级别 | `verbosity` |
|
||
| quit | 终止会话 | `quit` |
|
||
|
||
#### 流量统计 <a href="#traffic-statistics" id="traffic-statistics"></a>
|
||
|
||
你可以使用以下命令查询当前的流量统计信息
|
||
```
|
||
stats
|
||
```
|
||
您将获得一个列表,其中包含连接数、字节输入/输出等信息。
|
||
|
||
示例输出:
|
||
```
|
||
STAT pid 14868
|
||
STAT uptime 175931
|
||
STAT time 1220540125
|
||
STAT version 1.2.2
|
||
STAT pointer_size 32
|
||
STAT rusage_user 620.299700
|
||
STAT rusage_system 1545.703017
|
||
STAT curr_items 228
|
||
STAT total_items 779
|
||
STAT bytes 15525
|
||
STAT curr_connections 92
|
||
STAT total_connections 1740
|
||
STAT connection_structures 165
|
||
STAT cmd_get 7411
|
||
STAT cmd_set 28445156
|
||
STAT get_hits 5183
|
||
STAT get_misses 2228
|
||
STAT evictions 0
|
||
STAT bytes_read 2112768087
|
||
STAT bytes_written 1000038245
|
||
STAT limit_maxbytes 52428800
|
||
STAT threads 1
|
||
END
|
||
```
|
||
#### 内存统计 <a href="#memory-statistics" id="memory-statistics"></a>
|
||
|
||
您可以使用以下命令查询当前的内存统计信息:
|
||
```
|
||
stats slabs
|
||
```
|
||
# Memcache Commands
|
||
|
||
Memcache is a widely used distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in memory to reduce the need for repeated database queries.
|
||
|
||
## Basic Commands
|
||
|
||
### SET
|
||
|
||
**Command**: `set <key> <flags> <exptime> <bytes> [noreply]`
|
||
|
||
**Description**: Stores the specified value in the cache under the given key. The `flags` parameter is used to store metadata about the value, such as its data type or compression method. The `exptime` parameter specifies the expiration time in seconds. The `bytes` parameter indicates the size of the value in bytes. The optional `noreply` parameter tells the server not to send a response.
|
||
|
||
**Example**: `set mykey 0 3600 5\r\nhello\r\n`
|
||
|
||
### GET
|
||
|
||
**Command**: `get <key>`
|
||
|
||
**Description**: Retrieves the value stored in the cache under the given key.
|
||
|
||
**Example**: `get mykey`
|
||
|
||
### DELETE
|
||
|
||
**Command**: `delete <key> [noreply]`
|
||
|
||
**Description**: Deletes the value stored in the cache under the given key. The optional `noreply` parameter tells the server not to send a response.
|
||
|
||
**Example**: `delete mykey`
|
||
|
||
## Advanced Commands
|
||
|
||
### INCR
|
||
|
||
**Command**: `incr <key> <value>`
|
||
|
||
**Description**: Increments the value stored in the cache under the given key by the specified amount. If the value is not numeric, an error is returned.
|
||
|
||
**Example**: `incr mykey 10`
|
||
|
||
### DECR
|
||
|
||
**Command**: `decr <key> <value>`
|
||
|
||
**Description**: Decrements the value stored in the cache under the given key by the specified amount. If the value is not numeric, an error is returned.
|
||
|
||
**Example**: `decr mykey 5`
|
||
|
||
### APPEND
|
||
|
||
**Command**: `append <key> <bytes> [noreply]`
|
||
|
||
**Description**: Appends the specified value to the value stored in the cache under the given key. The `bytes` parameter indicates the size of the value to be appended. The optional `noreply` parameter tells the server not to send a response.
|
||
|
||
**Example**: `append mykey 5\r\nworld\r\n`
|
||
|
||
### PREPEND
|
||
|
||
**Command**: `prepend <key> <bytes> [noreply]`
|
||
|
||
**Description**: Prepends the specified value to the value stored in the cache under the given key. The `bytes` parameter indicates the size of the value to be prepended. The optional `noreply` parameter tells the server not to send a response.
|
||
|
||
**Example**: `prepend mykey 5\r\nhello\r\n`
|
||
|
||
### CAS
|
||
|
||
**Command**: `cas <key> <flags> <exptime> <bytes> <cas_unique> [noreply]`
|
||
|
||
**Description**: Stores the specified value in the cache under the given key, but only if the value has not been modified since the last retrieval. The `cas_unique` parameter is a unique identifier for the value. The `flags`, `exptime`, and `bytes` parameters have the same meaning as in the `set` command. The optional `noreply` parameter tells the server not to send a response.
|
||
|
||
**Example**: `cas mykey 0 3600 5 12345\r\nhello\r\n`
|
||
|
||
## Conclusion
|
||
|
||
These are some of the basic and advanced commands available in Memcache. Understanding and utilizing these commands can greatly enhance your ability to work with Memcache and optimize the performance of your applications.
|
||
```
|
||
STAT 1:chunk_size 80
|
||
STAT 1:chunks_per_page 13107
|
||
STAT 1:total_pages 1
|
||
STAT 1:total_chunks 13107
|
||
STAT 1:used_chunks 13106
|
||
STAT 1:free_chunks 1
|
||
STAT 1:free_chunks_end 12886
|
||
STAT 2:chunk_size 100
|
||
STAT 2:chunks_per_page 10485
|
||
STAT 2:total_pages 1
|
||
STAT 2:total_chunks 10485
|
||
STAT 2:used_chunks 10484
|
||
STAT 2:free_chunks 1
|
||
STAT 2:free_chunks_end 10477
|
||
[...]
|
||
STAT active_slabs 3
|
||
STAT total_malloced 3145436
|
||
END
|
||
```
|
||
如果您不确定您的memcached实例是否有足够的内存,请始终关注“stats”命令给出的“evictions”计数器。如果您的实例有足够的内存,那么“evictions”计数器应该为0,或者至少不会增加。
|
||
|
||
#### 使用了哪些键?<a href="#which-keys-are-used" id="which-keys-are-used"></a>
|
||
|
||
没有内置函数可以直接确定当前使用的键集。但是您可以使用以下命令:
|
||
```
|
||
stats items
|
||
```
|
||
### Command to Determine How Many Keys Exist
|
||
|
||
To determine the number of keys that exist in a Memcache server, you can use the `stats` command. This command provides statistics about the server, including the total number of keys stored.
|
||
|
||
```bash
|
||
stats items
|
||
```
|
||
|
||
This command will return a list of items with their corresponding statistics. Look for the line that starts with `STAT items:`, which indicates the total number of items (keys) stored in the Memcache server.
|
||
|
||
For example:
|
||
|
||
```bash
|
||
STAT items:1:number 10
|
||
```
|
||
|
||
In this example, the Memcache server has a total of 10 keys stored.
|
||
```
|
||
stats items
|
||
STAT items:1:number 220
|
||
STAT items:1:age 83095
|
||
STAT items:2:number 7
|
||
STAT items:2:age 1405
|
||
[...]
|
||
END
|
||
```
|
||
这至少有助于查看是否使用了任何键。要从已经执行了memcache访问的PHP脚本中转储键名称,您可以使用来自[100days.de](http://100days.de/serendipity/archives/55-Dumping-MemcacheD-Content-Keys-with-PHP.html)的PHP代码。
|
||
|
||
<details>
|
||
|
||
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 YouTube 🎥</strong></a></summary>
|
||
|
||
* 您在**网络安全公司**工作吗?您想在HackTricks中看到您的**公司广告**吗?或者您想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 发现我们的独家[NFT收藏品](https://opensea.io/collection/the-peass-family)——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
||
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
||
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或在**Twitter**上**关注**我[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
|
||
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享您的黑客技巧。**
|
||
|
||
</details>
|