Skip to content

Commit f36c935

Browse files
authored
Merge pull request #1566 from HackTricks-wiki/update_Assessing_the_Attack_Surface_of_Remote_MCP_Servers_20251111_012259
Assessing the Attack Surface of Remote MCP Servers
2 parents f73f3e0 + de5b41c commit f36c935

File tree

1 file changed

+75
-46
lines changed

1 file changed

+75
-46
lines changed

src/generic-methodologies-and-resources/phishing-methodology/ai-agent-abuse-local-ai-cli-tools-and-mcp.md

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,81 +56,110 @@ Notes:
5656

5757
- Gemini CLI session logs: `~/.gemini/tmp/<uuid>/logs.json`
5858
- Fields commonly seen: `sessionId`, `type`, `message`, `timestamp`.
59-
- Example `message`: `"@.bashrc what is in this file?"` (user/agent intent captured).
59+
- Example `message`: "@.bashrc what is in this file?" (user/agent intent captured).
6060
- Claude Code history: `~/.claude/history.jsonl`
6161
- JSONL entries with fields like `display`, `timestamp`, `project`.
6262

63-
Correlate these local logs with requests observed at your LLM gateway/proxy (e.g., LiteLLM) to detect tampering/model‑hijacking: if what the model processed deviates from the local prompt/output, investigate injected instructions or compromised tool descriptors.
64-
6563
---
6664

67-
## Endpoint Telemetry Patterns
65+
## Pentesting Remote MCP Servers
6866

69-
Representative chains on Amazon Linux 2023 with Node v22.19.0 and Python 3.13:
67+
Remote MCP servers expose a JSON‑RPC 2.0 API that fronts LLM‑centric capabilities (Prompts, Resources, Tools). They inherit classic web API flaws while adding async transports (SSE/streamable HTTP) and per‑session semantics.
7068

71-
1) Built‑in tools (local file access)
72-
- Parent: `node .../bin/claude --model <model>` (or equivalent for the CLI)
73-
- Immediate child action: create/modify a local file (e.g., `demo-claude`). Tie the file event back via parent→child lineage.
69+
Key actors
70+
- Host: the LLM/agent frontend (Claude Desktop, Cursor, etc.).
71+
- Client: per‑server connector used by the Host (one client per server).
72+
- Server: the MCP server (local or remote) exposing Prompts/Resources/Tools.
7473

75-
2) MCP over STDIO (local tool server)
76-
- Chain: `node → uv → python → file_write`
77-
- Example spawn: `uv run --with fastmcp fastmcp run /home/ssm-user/tools/server.py`
74+
AuthN/AuthZ
75+
- OAuth2 is common: an IdP authenticates, the MCP server acts as resource server.
76+
- After OAuth, the server issues an authentication token used on subsequent MCP requests. This is distinct from `Mcp-Session-Id` which identifies a connection/session after `initialize`.
7877

79-
3) MCP over HTTP (remote tool server)
80-
- Client: `node/<ai-cli>` opens outbound TCP to `remote_port: 8000` (or similar)
81-
- Server: remote Python process handles the request and writes `/home/ssm-user/demo_http`.
78+
Transports
79+
- Local: JSON‑RPC over STDIN/STDOUT.
80+
- Remote: Server‑Sent Events (SSE, still widely deployed) and streamable HTTP.
8281

83-
Because agent decisions differ by run, expect variability in exact processes and touched paths.
82+
A) Session initialization
83+
- Obtain OAuth token if required (Authorization: Bearer ...).
84+
- Begin a session and run the MCP handshake:
8485

85-
---
86+
```json
87+
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"capabilities":{}}}
88+
```
8689

87-
## Detection Strategy
90+
- Persist the returned `Mcp-Session-Id` and include it on subsequent requests per transport rules.
8891

89-
Telemetry sources
90-
- Linux EDR using eBPF/auditd for process, file and network events.
91-
- Local AI‑CLI logs for prompt/intent visibility.
92-
- LLM gateway logs (e.g., LiteLLM) for cross‑validation and model‑tamper detection.
92+
B) Enumerate capabilities
93+
- Tools
9394

94-
Hunting heuristics
95-
- Link sensitive file touches back to an AI‑CLI parent chain (e.g., `node → <ai-cli> → uv/python`).
96-
- Alert on access/reads/writes under: `~/.ssh`, `~/.aws`, browser profile storage, cloud CLI creds, `/etc/passwd`.
97-
- Flag unexpected outbound connections from the AI‑CLI process to unapproved MCP endpoints (HTTP/SSE, ports like 8000).
98-
- Correlate local `~/.gemini`/`~/.claude` artifacts with LLM gateway prompts/outputs; divergence indicates possible hijacking.
95+
```json
96+
{"jsonrpc":"2.0","id":10,"method":"tools/list"}
97+
```
9998

100-
Example pseudo‑rules (adapt to your EDR):
99+
- Resources
101100

102-
```yaml
103-
- when: file_write AND path IN ["$HOME/.ssh/*","$HOME/.aws/*","/etc/passwd"]
104-
and ancestor_chain CONTAINS ["node", "claude|gemini|warp", "python|uv"]
105-
then: alert("AI-CLI secrets touch via tool chain")
101+
```json
102+
{"jsonrpc":"2.0","id":1,"method":"resources/list"}
103+
```
104+
105+
- Prompts
106106

107-
- when: outbound_tcp FROM process_name =~ "node|python" AND parent =~ "claude|gemini|warp"
108-
and dest_port IN [8000, 3333, 8787]
109-
then: tag("possible MCP over HTTP")
107+
```json
108+
{"jsonrpc":"2.0","id":20,"method":"prompts/list"}
110109
```
111110

112-
Hardening ideas
113-
- Require explicit user approval for file/system tools; log and surface tool plans.
114-
- Constrain network egress for AI‑CLI processes to approved MCP servers.
115-
- Ship/ingest local AI‑CLI logs and LLM gateway logs for consistent, tamper‑resistant auditing.
111+
C) Exploitability checks
112+
- Resources → LFI/SSRF
113+
- The server should only allow `resources/read` for URIs it advertised in `resources/list`. Try out‑of‑set URIs to probe weak enforcement:
116114

117-
---
115+
```json
116+
{"jsonrpc":"2.0","id":2,"method":"resources/read","params":{"uri":"file:///etc/passwd"}}
117+
```
118+
119+
```json
120+
{"jsonrpc":"2.0","id":3,"method":"resources/read","params":{"uri":"http://169.254.169.254/latest/meta-data/"}}
121+
```
122+
123+
- Success indicates LFI/SSRF and possible internal pivoting.
124+
- Resources → IDOR (multi‑tenant)
125+
- If the server is multi‑tenant, attempt to read another user’s resource URI directly; missing per‑user checks leak cross‑tenant data.
126+
- Tools → Code execution and dangerous sinks
127+
- Enumerate tool schemas and fuzz parameters that influence command lines, subprocess calls, templating, deserializers, or file/network I/O:
128+
129+
```json
130+
{"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"TOOL_NAME","arguments":{"query":"; id"}}}
131+
```
132+
133+
- Look for error echoes/stack traces in results to refine payloads. Independent testing has reported widespread command‑injection and related flaws in MCP tools.
134+
- Prompts → Injection preconditions
135+
- Prompts mainly expose metadata; prompt injection matters only if you can tamper with prompt parameters (e.g., via compromised resources or client bugs).
118136

119-
## Blue‑Team Repro Notes
137+
D) Tooling for interception and fuzzing
138+
- MCP Inspector (Anthropic): Web UI/CLI supporting STDIO, SSE and streamable HTTP with OAuth. Ideal for quick recon and manual tool invocations.
139+
- HTTP–MCP Bridge (NCC Group): Bridges MCP SSE to HTTP/1.1 so you can use Burp/Caido.
140+
- Start the bridge pointed at the target MCP server (SSE transport).
141+
- Manually perform the `initialize` handshake to acquire a valid `Mcp-Session-Id` (per README).
142+
- Proxy JSON‑RPC messages like `tools/list`, `resources/list`, `resources/read`, and `tools/call` via Repeater/Intruder for replay and fuzzing.
120143

121-
Use a clean VM with an EDR or eBPF tracer to reproduce chains like:
122-
- `node → claude --model claude-sonnet-4-20250514` then immediate local file write.
123-
- `node → uv run --with fastmcp ... → python3.13` writing under `$HOME`.
124-
- `node/<ai-cli>` establishing TCP to an external MCP server (port 8000) while a remote Python process writes a file.
144+
Quick test plan
145+
- Authenticate (OAuth if present) → run `initialize` → enumerate (`tools/list`, `resources/list`, `prompts/list`) → validate resource URI allow‑list and per‑user authorization → fuzz tool inputs at likely code‑execution and I/O sinks.
125146

126-
Validate that your detections tie the file/network events back to the initiating AI‑CLI parent to avoid false positives.
147+
Impact highlights
148+
- Missing resource URI enforcement → LFI/SSRF, internal discovery and data theft.
149+
- Missing per‑user checks → IDOR and cross‑tenant exposure.
150+
- Unsafe tool implementations → command injection → server‑side RCE and data exfiltration.
127151

128152
---
129153

130154
## References
131155

132156
- [Commanding attention: How adversaries are abusing AI CLI tools (Red Canary)](https://redcanary.com/blog/threat-detection/ai-cli-tools/)
133157
- [Model Context Protocol (MCP)](https://modelcontextprotocol.io)
134-
- [LiteLLM – LLM Gateway/Proxy](https://docs.litellm.ai)
158+
- [Assessing the Attack Surface of Remote MCP Servers](https://blog.kulkan.com/assessing-the-attack-surface-of-remote-mcp-servers-92d630a0cab0)
159+
- [MCP Inspector (Anthropic)](https://github.com/modelcontextprotocol/inspector)
160+
- [HTTP–MCP Bridge (NCC Group)](https://github.com/nccgroup/http-mcp-bridge)
161+
- [MCP spec – Authorization](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization)
162+
- [MCP spec – Transports and SSE deprecation](https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#backwards-compatibility)
163+
- [Equixly: MCP server security issues in the wild](https://equixly.com/blog/2025/03/29/mcp-server-new-security-nightmare/)
135164

136165
{{#include ../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)