@zebbern/hacktricks-mcp 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -9
- package/data/hacktricks.db +0 -0
- package/data/meta.json +8 -4
- package/data/toc.json +14631 -5537
- package/dist/books.js +27 -0
- package/dist/db.js +349 -64
- package/dist/kubernetes-rbac.js +138 -0
- package/dist/markdown.js +12 -0
- package/dist/permission-check.js +224 -0
- package/dist/permissions.js +195 -0
- package/dist/refresh.js +185 -0
- package/dist/server.js +331 -74
- package/dist/toc.js +25 -5
- package/package.json +10 -3
package/dist/server.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* hacktricks-mcp — MCP server for searching and retrieving HackTricks content.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Serves the pre-built SQLite FTS5 index bundled with the package (data/),
|
|
6
|
+
* which covers both HackTricks books (src/books.ts). Every tool is read-only
|
|
7
|
+
* and answers from that local index. The only network use is a background
|
|
8
|
+
* check for a newer index (src/refresh.ts), which never delays a tool call.
|
|
7
9
|
*/
|
|
8
10
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
9
11
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
@@ -11,23 +13,37 @@ import { z } from "zod";
|
|
|
11
13
|
import path from "node:path";
|
|
12
14
|
import fs from "node:fs";
|
|
13
15
|
import { fileURLToPath } from "node:url";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
+
import { BOOKS } from "./books.js";
|
|
17
|
+
import { searchPages, getPage, listCategories } from "./db.js";
|
|
18
|
+
import { extractSection, extractCodeBlocks, sectionLengths } from "./markdown.js";
|
|
19
|
+
import { checkPermissions } from "./permission-check.js";
|
|
20
|
+
import { PROVIDERS, SEVERITIES } from "./permissions.js";
|
|
21
|
+
import { newestCachedIndex, openIndex, refreshInBackground } from "./refresh.js";
|
|
16
22
|
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
23
|
+
const DATA_DIR = path.join(PKG_ROOT, "data");
|
|
24
|
+
const OFFLINE = ["1", "true"].includes((process.env.HACKTRICKS_OFFLINE ?? "").toLowerCase());
|
|
25
|
+
// The bundled index answers from the first call on. One that an earlier run
|
|
26
|
+
// downloaded is used right away if it is newer, and a newer one still is
|
|
27
|
+
// swapped in by the background check in main(); every tool reads this variable
|
|
28
|
+
// at call time.
|
|
29
|
+
let index = openIndex(DATA_DIR, process.env.HACKTRICKS_DB);
|
|
30
|
+
const downloaded = process.env.HACKTRICKS_DB ? null : newestCachedIndex(index);
|
|
31
|
+
if (downloaded) {
|
|
32
|
+
index.db.close();
|
|
33
|
+
index = downloaded;
|
|
34
|
+
}
|
|
35
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(PKG_ROOT, "package.json"), "utf8"));
|
|
36
|
+
const count = (n) => n.toLocaleString("en-US");
|
|
37
|
+
const pages = (n) => `${count(n)} ${n === 1 ? "page" : "pages"}`;
|
|
38
|
+
const server = new McpServer({ name: "hacktricks-mcp", version: pkg.version }, {
|
|
25
39
|
instructions: [
|
|
26
|
-
"HackTricks wiki knowledge base (offensive security: pentesting,
|
|
27
|
-
"web exploitation, Active Directory,
|
|
40
|
+
"HackTricks wiki knowledge base in two books: HackTricks (offensive security: pentesting,",
|
|
41
|
+
"privilege escalation, web exploitation, Active Directory, Wi-Fi, forensics, macOS/Windows/Linux)",
|
|
42
|
+
"and HackTricks Cloud (AWS, Azure, GCP and other providers, Kubernetes, CI/CD).",
|
|
28
43
|
"Use hacktricks_search to find relevant pages, then hacktricks_get_page to read them.",
|
|
44
|
+
"For cloud permissions, hacktricks_cloud_permissions rates them and lists the escalation techniques they enable.",
|
|
29
45
|
"Prefer section= or codeOnly= on get_page to keep responses small.",
|
|
30
|
-
`Index covers ${meta.pageCount} pages, synced from upstream at ${meta.syncedAt}.`,
|
|
46
|
+
`Index covers ${count(index.meta.pageCount)} pages, synced from upstream at ${index.meta.syncedAt}.`,
|
|
31
47
|
].join(" "),
|
|
32
48
|
});
|
|
33
49
|
const READONLY = {
|
|
@@ -36,58 +52,118 @@ const READONLY = {
|
|
|
36
52
|
idempotentHint: true,
|
|
37
53
|
openWorldHint: false,
|
|
38
54
|
};
|
|
55
|
+
/** The first line of a page: its title and where a human can read it online (the source link for pages the wiki does not publish). */
|
|
56
|
+
const pageHeader = (page) => `# ${page.title}\n${page.wiki || page.url}\n\n`;
|
|
57
|
+
/**
|
|
58
|
+
* A page's headings with the size of each section and none of the content.
|
|
59
|
+
* Agents read whole pages just to find the headings, then read sections of the
|
|
60
|
+
* same pages again; this makes the first step cheap.
|
|
61
|
+
*/
|
|
62
|
+
function pageOutline(page, headings) {
|
|
63
|
+
const size = (n) => n.toLocaleString("en-US");
|
|
64
|
+
const sizes = sectionLengths(page.content, headings);
|
|
65
|
+
const outline = headings.map((h, i) => {
|
|
66
|
+
// A heading whose section is mostly its subsections is a container. Asking
|
|
67
|
+
// for it returns all of them, so say that instead of a bare size.
|
|
68
|
+
let subsections = 0;
|
|
69
|
+
for (const next of headings.slice(i + 1)) {
|
|
70
|
+
if (next.level <= h.level)
|
|
71
|
+
break;
|
|
72
|
+
if (next.level === h.level + 1)
|
|
73
|
+
subsections++;
|
|
74
|
+
}
|
|
75
|
+
const extent = subsections > 0 ? `${size(sizes[i])} chars in ${subsections} subsections` : `${size(sizes[i])} chars`;
|
|
76
|
+
return `${" ".repeat(h.level - 1)}${h.text} (${extent})`;
|
|
77
|
+
});
|
|
78
|
+
const summary = headings.length === 0
|
|
79
|
+
? `Page is ${size(page.content.length)} chars and has no headings, so read it without section.`
|
|
80
|
+
: `Page is ${size(page.content.length)} chars in ${headings.length} sections. Read one with section=<heading>, adding codeOnly=true for its commands only.`;
|
|
81
|
+
return {
|
|
82
|
+
content: [{ type: "text", text: pageHeader(page) + `${summary}\n\n${outline.join("\n")}` }],
|
|
83
|
+
structuredContent: {
|
|
84
|
+
title: page.title,
|
|
85
|
+
book: page.book,
|
|
86
|
+
path: page.path,
|
|
87
|
+
url: page.url,
|
|
88
|
+
wikiUrl: page.wiki,
|
|
89
|
+
outline,
|
|
90
|
+
content: "",
|
|
91
|
+
truncated: false,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const bookSchema = z
|
|
96
|
+
.enum(BOOKS.map((b) => b.id))
|
|
97
|
+
.describe(`Book to read from. Only matters for the few paths that exist in both books, such as README.md; the default is "${BOOKS[0].id}".`);
|
|
98
|
+
const urlFields = {
|
|
99
|
+
url: z.string().describe("Markdown source of the page on GitHub"),
|
|
100
|
+
wikiUrl: z.string().describe("The page on the public wiki, to cite. Empty for the few pages the wiki does not publish."),
|
|
101
|
+
};
|
|
39
102
|
const searchHitSchema = {
|
|
40
103
|
pageId: z.number(),
|
|
104
|
+
book: z.enum(BOOKS.map((b) => b.id)).describe("Book the page belongs to"),
|
|
41
105
|
path: z.string().describe("Page identifier; pass this to hacktricks_get_page"),
|
|
42
106
|
title: z.string(),
|
|
43
107
|
category: z.string(),
|
|
44
|
-
|
|
108
|
+
...urlFields,
|
|
109
|
+
chars: z.number().describe("Length of the page in characters. Under about 8,000 is cheap to read whole; above that, read the outline first."),
|
|
45
110
|
snippet: z.string(),
|
|
46
111
|
};
|
|
47
112
|
server.registerTool("hacktricks_search", {
|
|
48
113
|
title: "Search HackTricks",
|
|
49
|
-
description: "Full-text search over the HackTricks
|
|
50
|
-
"privilege escalation, web vulns, AD attacks,
|
|
51
|
-
"
|
|
52
|
-
"
|
|
114
|
+
description: "Full-text search over the HackTricks wiki: both the HackTricks book (pentesting techniques, " +
|
|
115
|
+
"privilege escalation, web vulns, AD attacks, forensics) and the HackTricks Cloud book (AWS, " +
|
|
116
|
+
"Azure, GCP and other providers, Kubernetes, CI/CD). Returns ranked pages " +
|
|
117
|
+
"with snippets and each page's size. Use technical terms or the question in plain words (e.g. 'kerberoast' or " +
|
|
118
|
+
"'how do I abuse unconstrained delegation'); the page named after the technique is ranked first. " +
|
|
119
|
+
"Follow up with hacktricks_get_page on a result's path to read the content.",
|
|
53
120
|
inputSchema: {
|
|
54
|
-
query: z.string().min(1).describe("Search terms, e.g. 'docker escape' or '
|
|
121
|
+
query: z.string().min(1).describe("Search terms or a plain question, e.g. 'docker escape', 'aws s3 enumeration' or 'how do I escalate from the lxd group'"),
|
|
55
122
|
category: z
|
|
56
123
|
.string()
|
|
57
124
|
.optional()
|
|
58
|
-
.describe("Optional category
|
|
125
|
+
.describe("Optional: restrict to one category when the top hits are for the wrong one (substring match), e.g. 'Linux' or 'Windows'. hacktricks_get_toc lists them"),
|
|
59
126
|
limit: z.number().int().min(1).max(25).default(10).describe("Max results (default 10)"),
|
|
60
127
|
},
|
|
61
128
|
outputSchema: { results: z.array(z.object(searchHitSchema)) },
|
|
62
129
|
annotations: READONLY,
|
|
63
130
|
}, async ({ query, category, limit }) => {
|
|
64
|
-
const hits = searchPages(db, query, category, limit);
|
|
131
|
+
const hits = searchPages(index.db, query, category, limit);
|
|
65
132
|
const results = hits.map((h) => ({
|
|
66
133
|
pageId: h.page_id,
|
|
134
|
+
book: h.book,
|
|
67
135
|
path: h.path,
|
|
68
136
|
title: h.title,
|
|
69
137
|
category: h.category,
|
|
70
138
|
url: h.url,
|
|
139
|
+
wikiUrl: h.wiki,
|
|
140
|
+
chars: h.chars,
|
|
71
141
|
snippet: h.snippet.replace(/\n+/g, " ").trim(),
|
|
72
142
|
}));
|
|
73
143
|
const text = results.length === 0
|
|
74
144
|
? `No HackTricks pages matched "${query}"${category ? ` in category "${category}"` : ""}. Try broader or different terms.`
|
|
75
145
|
: results
|
|
76
|
-
.map((r, i) => `${i + 1}. ${r.title}\n path: ${r.path}\n category: ${r.category}\n ${r.snippet}`)
|
|
146
|
+
.map((r, i) => `${i + 1}. ${r.title}\n book: ${r.book}\n path: ${r.path}\n category: ${r.category}\n size: ${r.chars.toLocaleString("en-US")} chars\n ${r.snippet}`)
|
|
77
147
|
.join("\n\n");
|
|
78
148
|
return { content: [{ type: "text", text }], structuredContent: { results } };
|
|
79
149
|
});
|
|
80
150
|
server.registerTool("hacktricks_get_page", {
|
|
81
151
|
title: "Read HackTricks Page",
|
|
82
|
-
description: "Retrieve content of a HackTricks page found via hacktricks_search.
|
|
83
|
-
"
|
|
84
|
-
"heading name, or codeOnly=true to get just the commands/payloads.
|
|
85
|
-
"
|
|
152
|
+
description: "Retrieve content of a HackTricks page found via hacktricks_search. For a long page, start with " +
|
|
153
|
+
"outlineOnly=true to list its sections with their sizes, then use section= to read one section by " +
|
|
154
|
+
"heading name, or codeOnly=true to get just the commands/payloads. A short page (the search " +
|
|
155
|
+
"result shows its size) can be read whole. Text comes in slices of 20,000 characters; a cut " +
|
|
156
|
+
"slice ends with the offset where the rest continues.",
|
|
86
157
|
inputSchema: {
|
|
87
158
|
path: z
|
|
88
159
|
.string()
|
|
89
160
|
.min(1)
|
|
90
161
|
.describe("Page path from hacktricks_search (e.g. 'windows-hardening/active-directory-methodology/kerberoast.md'). Unique suffixes like 'kerberoast' also work."),
|
|
162
|
+
book: bookSchema.optional(),
|
|
163
|
+
outlineOnly: z
|
|
164
|
+
.boolean()
|
|
165
|
+
.default(false)
|
|
166
|
+
.describe("If true, return the headings with the size of each section instead of the content. Use it to choose a section before reading."),
|
|
91
167
|
section: z
|
|
92
168
|
.string()
|
|
93
169
|
.optional()
|
|
@@ -96,18 +172,29 @@ server.registerTool("hacktricks_get_page", {
|
|
|
96
172
|
.boolean()
|
|
97
173
|
.default(false)
|
|
98
174
|
.describe("If true, return only fenced code blocks (commands/payloads) from the page or section"),
|
|
175
|
+
offset: z
|
|
176
|
+
.number()
|
|
177
|
+
.int()
|
|
178
|
+
.min(0)
|
|
179
|
+
.default(0)
|
|
180
|
+
.describe("Skip this many characters of the page or section. A truncated response ends with the offset where the rest continues."),
|
|
99
181
|
},
|
|
100
182
|
outputSchema: {
|
|
101
183
|
title: z.string(),
|
|
184
|
+
book: z.string(),
|
|
102
185
|
path: z.string(),
|
|
103
|
-
|
|
186
|
+
...urlFields,
|
|
104
187
|
outline: z.array(z.string()),
|
|
105
188
|
content: z.string(),
|
|
106
189
|
truncated: z.boolean(),
|
|
190
|
+
nextOffset: z
|
|
191
|
+
.number()
|
|
192
|
+
.optional()
|
|
193
|
+
.describe("Where the rest of a truncated text continues (pass it as offset); absent when everything was returned"),
|
|
107
194
|
},
|
|
108
195
|
annotations: READONLY,
|
|
109
|
-
}, async ({ path: pagePath, section, codeOnly }) => {
|
|
110
|
-
const page = getPage(db, pagePath);
|
|
196
|
+
}, async ({ path: pagePath, book, outlineOnly, section, codeOnly, offset }) => {
|
|
197
|
+
const page = getPage(index.db, pagePath, book);
|
|
111
198
|
if (!page) {
|
|
112
199
|
return {
|
|
113
200
|
content: [
|
|
@@ -119,7 +206,10 @@ server.registerTool("hacktricks_get_page", {
|
|
|
119
206
|
isError: true,
|
|
120
207
|
};
|
|
121
208
|
}
|
|
122
|
-
const
|
|
209
|
+
const headings = JSON.parse(page.outline);
|
|
210
|
+
if (outlineOnly)
|
|
211
|
+
return pageOutline(page, headings);
|
|
212
|
+
const outline = headings.map((h) => `${" ".repeat(h.level - 1)}${h.text}`);
|
|
123
213
|
let content;
|
|
124
214
|
if (section) {
|
|
125
215
|
const sec = extractSection(page.content, section);
|
|
@@ -143,82 +233,249 @@ server.registerTool("hacktricks_get_page", {
|
|
|
143
233
|
const blocks = extractCodeBlocks(content);
|
|
144
234
|
content = blocks.length > 0 ? blocks.map((b) => "```\n" + b + "\n```").join("\n\n") : "(no code blocks found)";
|
|
145
235
|
}
|
|
236
|
+
// A long text comes in slices of MAX characters, and a slice that is cut
|
|
237
|
+
// says where the next one starts: a section without subsections has no
|
|
238
|
+
// smaller piece to ask for.
|
|
146
239
|
const MAX = 20000;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
240
|
+
const whole = content.length;
|
|
241
|
+
const slice = content.slice(offset, offset + MAX);
|
|
242
|
+
const truncated = offset + slice.length < whole;
|
|
243
|
+
const nextOffset = truncated ? offset + slice.length : undefined;
|
|
244
|
+
if (slice.length === 0) {
|
|
245
|
+
content = `Nothing left to read: the ${section ? "section" : "page"} is ${whole.toLocaleString("en-US")} characters and the offset is ${offset}.`;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
content = slice;
|
|
249
|
+
if (truncated) {
|
|
250
|
+
content +=
|
|
251
|
+
`\n\n--- [TRUNCATED at ${nextOffset} of ${whole.toLocaleString("en-US")} characters. Read the rest with offset=${nextOffset}, ` +
|
|
252
|
+
`or use section= for a smaller part. Sections:\n${outline.join("\n")}`;
|
|
253
|
+
}
|
|
154
254
|
}
|
|
155
|
-
const header = `# ${page.title}\n${page.url}\n\n`;
|
|
156
255
|
return {
|
|
157
|
-
content: [{ type: "text", text:
|
|
256
|
+
content: [{ type: "text", text: pageHeader(page) + content }],
|
|
158
257
|
structuredContent: {
|
|
159
258
|
title: page.title,
|
|
259
|
+
book: page.book,
|
|
160
260
|
path: page.path,
|
|
161
261
|
url: page.url,
|
|
262
|
+
wikiUrl: page.wiki,
|
|
162
263
|
outline,
|
|
163
264
|
content,
|
|
164
265
|
truncated,
|
|
266
|
+
nextOffset,
|
|
165
267
|
},
|
|
166
268
|
};
|
|
167
269
|
});
|
|
270
|
+
/** Entries whose title contains `needle`, each with everything under it. */
|
|
271
|
+
function tocMatching(needle) {
|
|
272
|
+
const visit = (nodes, depth, out) => {
|
|
273
|
+
for (const node of nodes) {
|
|
274
|
+
const sub = [];
|
|
275
|
+
visit(node.children, depth + 1, sub);
|
|
276
|
+
if (sub.length > 0 || node.title.toLowerCase().includes(needle)) {
|
|
277
|
+
out.push(" ".repeat(depth) + "- " + node.title + (node.path ? ` [${node.path}]` : ""), ...sub);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const lines = [];
|
|
282
|
+
for (const book of BOOKS) {
|
|
283
|
+
const found = [];
|
|
284
|
+
visit(index.toc[book.id] ?? [], 0, found);
|
|
285
|
+
if (found.length > 0)
|
|
286
|
+
lines.push(`${book.title}:`, ...found);
|
|
287
|
+
}
|
|
288
|
+
return lines;
|
|
289
|
+
}
|
|
290
|
+
/** Pages (entries linking to one) in a subtree, the root included. */
|
|
291
|
+
const pagesIn = (nodes) => nodes.reduce((n, node) => n + (node.path ? 1 : 0) + pagesIn(node.children), 0);
|
|
292
|
+
/** The map to read before searching: each book's categories and the chapters under them, no page paths. */
|
|
293
|
+
function tocOverview(categories) {
|
|
294
|
+
const lines = [
|
|
295
|
+
`Index: ${count(index.meta.pageCount)} pages, synced ${index.meta.syncedAt}.`,
|
|
296
|
+
];
|
|
297
|
+
for (const book of BOOKS) {
|
|
298
|
+
const roots = index.toc[book.id] ?? [];
|
|
299
|
+
if (roots.length === 0)
|
|
300
|
+
continue;
|
|
301
|
+
const inBook = categories.filter((c) => c.book === book.id);
|
|
302
|
+
lines.push("", `${book.title}: ${pages(inBook.reduce((n, c) => n + c.pages, 0))}, upstream ${String(index.meta.books?.[book.id] ?? "?").slice(0, 7)}`);
|
|
303
|
+
let category;
|
|
304
|
+
for (const root of roots) {
|
|
305
|
+
if (root.category !== category) {
|
|
306
|
+
category = root.category;
|
|
307
|
+
lines.push(`${category || "(other)"} (${pages(inBook.find((c) => c.category === (category ?? ""))?.pages ?? 0)})`);
|
|
308
|
+
}
|
|
309
|
+
lines.push(` - ${root.title} (${pages(pagesIn([root]))})`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
lines.push("", `Pass a filter (e.g. "Kerberos", "AWS" or "CI/CD") to list the pages under matching entries.`);
|
|
313
|
+
return lines.join("\n");
|
|
314
|
+
}
|
|
315
|
+
const TOC_MAX_LINES = 300;
|
|
168
316
|
server.registerTool("hacktricks_get_toc", {
|
|
169
317
|
title: "HackTricks Table of Contents",
|
|
170
|
-
description: "Get the HackTricks
|
|
171
|
-
"
|
|
172
|
-
"lives before searching.",
|
|
318
|
+
description: "Get the structure of both HackTricks books from their tables of contents. Call without arguments for " +
|
|
319
|
+
"the overview (each book's categories and chapters with page counts), or pass a filter to list the " +
|
|
320
|
+
"pages under the entries whose title contains it. Useful to see where a topic lives before searching.",
|
|
173
321
|
inputSchema: {
|
|
174
322
|
category: z
|
|
175
323
|
.string()
|
|
176
324
|
.optional()
|
|
177
|
-
.describe("Optional category
|
|
325
|
+
.describe("Optional text filter on entry titles, case-insensitive: a category to expand (e.g. 'Linux Hardening', 'Pentesting Cloud') or a topic to list (e.g. 'Kerberos', 'AWS', 'GitHub Actions'). Omit for the overview."),
|
|
178
326
|
},
|
|
179
327
|
outputSchema: {
|
|
180
|
-
categories: z.array(z.object({ category: z.string(), pages: z.number() })),
|
|
181
|
-
tree: z.string().describe("
|
|
328
|
+
categories: z.array(z.object({ book: z.string(), category: z.string(), pages: z.number() })),
|
|
329
|
+
tree: z.string().describe("Overview, or the indented entries matching the filter"),
|
|
182
330
|
},
|
|
183
331
|
annotations: READONLY,
|
|
184
332
|
}, async ({ category }) => {
|
|
185
|
-
const categories = listCategories(db);
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
let
|
|
190
|
-
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return lines;
|
|
206
|
-
};
|
|
207
|
-
tree = matchSection(toc, 0).join("\n");
|
|
333
|
+
const categories = listCategories(index.db);
|
|
334
|
+
const bookTitle = (id) => BOOKS.find((b) => b.id === id)?.title ?? id;
|
|
335
|
+
const catText = categories.map((c) => `- ${bookTitle(c.book)} > ${c.category} (${pages(c.pages)})`).join("\n");
|
|
336
|
+
let tree;
|
|
337
|
+
let text;
|
|
338
|
+
if (!category) {
|
|
339
|
+
tree = tocOverview(categories);
|
|
340
|
+
text = tree;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
const lines = tocMatching(category.toLowerCase());
|
|
344
|
+
if (lines.length > TOC_MAX_LINES) {
|
|
345
|
+
tree = lines.slice(0, TOC_MAX_LINES).join("\n");
|
|
346
|
+
text = `Entries matching "${category}" (first ${TOC_MAX_LINES} of ${lines.length} lines; use a more specific filter):\n${tree}`;
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
tree = lines.length > 0 ? lines.join("\n") : "(none)";
|
|
350
|
+
text = `Entries matching "${category}":\n${tree}`;
|
|
351
|
+
}
|
|
352
|
+
text += `\n\nCategories:\n${catText}`;
|
|
208
353
|
}
|
|
209
|
-
const catText = categories.map((c) => `- ${c.category} (${c.pages} pages)`).join("\n");
|
|
210
|
-
const text = category
|
|
211
|
-
? `Pages matching "${category}":\n${tree || "(none)"}\n\nAll categories:\n${catText}`
|
|
212
|
-
: `HackTricks categories:\n${catText}\n\nStructure (top 2 levels):\n${tree}`;
|
|
213
354
|
return {
|
|
214
355
|
content: [{ type: "text", text }],
|
|
215
356
|
structuredContent: { categories, tree },
|
|
216
357
|
};
|
|
217
358
|
});
|
|
359
|
+
const severitySchema = z.enum([...SEVERITIES, "not rated"]);
|
|
360
|
+
server.registerTool("hacktricks_cloud_permissions", {
|
|
361
|
+
title: "Check Cloud Permissions",
|
|
362
|
+
description: "Check cloud permissions against HackTricks Cloud. Give the permissions a principal holds and get each one's " +
|
|
363
|
+
"rating (critical, high, medium, low) from the book's rated list, the combination rules they complete, and " +
|
|
364
|
+
"every escalation technique in the book's catalog whose required permissions are all held, with the page " +
|
|
365
|
+
"section that explains each. Use it for questions like 'is this permission dangerous?', 'what can I do with " +
|
|
366
|
+
"these permissions?' or 'which of these permissions should I remove?'. Resources, scopes and request " +
|
|
367
|
+
"conditions are not evaluated, so read a technique's conditions and its page before relying on it.",
|
|
368
|
+
inputSchema: {
|
|
369
|
+
provider: z.enum(PROVIDERS).describe('The cloud: "aws", "gcp", "azure" or "k8s"'),
|
|
370
|
+
permissions: z
|
|
371
|
+
.array(z.string().min(1))
|
|
372
|
+
.min(1)
|
|
373
|
+
.max(200)
|
|
374
|
+
.describe('The permissions held, as the cloud writes them: "iam:PassRole", "iam.serviceAccounts.actAs", ' +
|
|
375
|
+
'"Microsoft.Authorization/roleAssignments/write", "Application.ReadWrite.All". Wildcards as in a policy ' +
|
|
376
|
+
'("iam:*", "s3:Get*"). For k8s, one grant per entry as "verb resource[.group][/subresource]", "verb *" or ' +
|
|
377
|
+
'"verb /url": "create pods", "get secrets", "create pods/exec", "get *", "impersonate users".'),
|
|
378
|
+
limit: z
|
|
379
|
+
.number()
|
|
380
|
+
.int()
|
|
381
|
+
.min(1)
|
|
382
|
+
.max(100)
|
|
383
|
+
.default(20)
|
|
384
|
+
.describe("How many escalation techniques to list at most (default 20); the total is always reported"),
|
|
385
|
+
},
|
|
386
|
+
outputSchema: {
|
|
387
|
+
provider: z.enum(PROVIDERS),
|
|
388
|
+
risk: z.array(z.object({
|
|
389
|
+
permission: z.string(),
|
|
390
|
+
severity: severitySchema,
|
|
391
|
+
note: z.string().optional().describe("What a wildcard matches, or why a Kubernetes rule rates the grant"),
|
|
392
|
+
})),
|
|
393
|
+
combinations: z.array(z.object({ severity: z.enum(SEVERITIES), permissions: z.array(z.string()) })),
|
|
394
|
+
escalation: z.array(z.object({
|
|
395
|
+
id: z.string(),
|
|
396
|
+
service: z.string(),
|
|
397
|
+
title: z.string(),
|
|
398
|
+
summary: z.string(),
|
|
399
|
+
severity: z.enum(SEVERITIES),
|
|
400
|
+
confidence: z.string(),
|
|
401
|
+
needs: z.array(z.string()),
|
|
402
|
+
gains: z.string().describe("What the technique gains: the kind of principal or resource, and where"),
|
|
403
|
+
conditions: z.array(z.string()),
|
|
404
|
+
blockers: z.array(z.string()),
|
|
405
|
+
mitigation: z.string(),
|
|
406
|
+
page: z.string().describe("Page path; pass it to hacktricks_get_page"),
|
|
407
|
+
section: z.string().optional().describe("Heading of the part that explains the technique, for hacktricks_get_page"),
|
|
408
|
+
})),
|
|
409
|
+
escalationTotal: z.number(),
|
|
410
|
+
notEvaluated: z.number().describe("Techniques of this cloud that depend on more than permissions and are never listed"),
|
|
411
|
+
sections: z.array(z.object({
|
|
412
|
+
permission: z.string(),
|
|
413
|
+
page: z.string(),
|
|
414
|
+
heading: z.string().describe("Heading of a section named after the permission, for hacktricks_get_page"),
|
|
415
|
+
})),
|
|
416
|
+
},
|
|
417
|
+
annotations: READONLY,
|
|
418
|
+
}, async ({ provider, permissions, limit }) => {
|
|
419
|
+
let check;
|
|
420
|
+
try {
|
|
421
|
+
check = checkPermissions(index.db, provider, permissions, limit);
|
|
422
|
+
}
|
|
423
|
+
catch (err) {
|
|
424
|
+
return { content: [{ type: "text", text: err.message }], isError: true };
|
|
425
|
+
}
|
|
426
|
+
const label = (s) => s.padEnd(10);
|
|
427
|
+
const read = (page, heading) => `hacktricks_get_page path="${page}"${heading ? ` section="${heading.replace(/"/g, "'")}"` : ""}`;
|
|
428
|
+
const lines = [`hacktricks_cloud_permissions: ${provider}, ${check.risk.length} permissions.`, "", "Risk per permission"];
|
|
429
|
+
for (const r of check.risk)
|
|
430
|
+
lines.push(` ${label(r.severity)} ${r.permission}${r.note ? ` (${r.note})` : ""}`);
|
|
431
|
+
lines.push("", check.combinations.length > 0 ? "Combinations held (every permission of the combination is held)" : "Combinations held: none");
|
|
432
|
+
for (const c of check.combinations)
|
|
433
|
+
lines.push(` ${label(c.severity)} ${c.permissions.join(" + ")}`);
|
|
434
|
+
lines.push("", `Escalation techniques whose permissions are all held: ${check.escalationTotal}` +
|
|
435
|
+
(check.escalationTotal > check.escalation.length ? ` (first ${check.escalation.length} listed)` : ""));
|
|
436
|
+
check.escalation.forEach((m, i) => lines.push(...describeTechnique(m, i + 1, read)));
|
|
437
|
+
// A section already named by a listed technique is not repeated.
|
|
438
|
+
const covered = new Set(check.escalation.map((m) => `${m.page}#${m.section ?? ""}`));
|
|
439
|
+
const sections = check.sections.filter((s) => !covered.has(`${s.page}#${s.heading}`));
|
|
440
|
+
if (sections.length > 0) {
|
|
441
|
+
lines.push("", "Sections of HackTricks Cloud named after these permissions");
|
|
442
|
+
for (const s of sections)
|
|
443
|
+
lines.push(` ${s.permission}: ${read(s.page, s.heading)}`);
|
|
444
|
+
}
|
|
445
|
+
lines.push("", "Not evaluated: resources, scopes, request conditions and the properties of the target. A technique is listed " +
|
|
446
|
+
"when every permission it names is held; read its conditions and its page before relying on it. " +
|
|
447
|
+
`${check.notEvaluated} ${provider} techniques depend on such facts and are never listed.`);
|
|
448
|
+
const { provider: _, ...rest } = check;
|
|
449
|
+
return { content: [{ type: "text", text: lines.join("\n") }], structuredContent: { provider, ...rest } };
|
|
450
|
+
});
|
|
451
|
+
/** The lines of one technique in the tool's text. */
|
|
452
|
+
function describeTechnique(m, n, read) {
|
|
453
|
+
const lines = [
|
|
454
|
+
`${n}. [${m.severity}] ${m.title}`,
|
|
455
|
+
` needs: ${m.needs.join(", ")} gains: ${m.gains || "see page"} confidence: ${m.confidence}`,
|
|
456
|
+
];
|
|
457
|
+
if (m.summary)
|
|
458
|
+
lines.push(` ${m.summary}`);
|
|
459
|
+
if (m.conditions.length > 0)
|
|
460
|
+
lines.push(` conditions: ${m.conditions.join(" ")}`);
|
|
461
|
+
if (m.blockers.length > 0)
|
|
462
|
+
lines.push(` blocked by: ${m.blockers.join(", ")}`);
|
|
463
|
+
lines.push(` read: ${read(m.page, m.section)}`);
|
|
464
|
+
if (m.mitigation)
|
|
465
|
+
lines.push(` mitigate: ${m.mitigation}`);
|
|
466
|
+
return lines;
|
|
467
|
+
}
|
|
218
468
|
async function main() {
|
|
219
469
|
const transport = new StdioServerTransport();
|
|
220
470
|
await server.connect(transport);
|
|
221
471
|
console.error("hacktricks-mcp running on stdio");
|
|
472
|
+
// A custom database path is an explicit choice of index, so leave it alone.
|
|
473
|
+
if (!OFFLINE && !process.env.HACKTRICKS_DB) {
|
|
474
|
+
refreshInBackground(() => index, (next) => {
|
|
475
|
+
index.db.close();
|
|
476
|
+
index = next;
|
|
477
|
+
});
|
|
478
|
+
}
|
|
222
479
|
}
|
|
223
480
|
main().catch((err) => {
|
|
224
481
|
console.error("Fatal error:", err);
|
package/dist/toc.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Parser for HackTricks' src/SUMMARY.md (mdBook/GitBook table of contents).
|
|
3
3
|
*
|
|
4
|
-
* Produces a nested tree of { title, path?, children } plus a flat map from
|
|
4
|
+
* Produces a nested tree of { title, path?, category?, children } plus a flat map from
|
|
5
5
|
* markdown path -> { title, category, breadcrumb } used to enrich the index.
|
|
6
6
|
*/
|
|
7
7
|
const ENTRY_RE = /^(\s*)[-*]\s+\[([^\]]+)\]\(([^)]*)\)\s*$/;
|
|
@@ -13,10 +13,18 @@ export function parseSummary(summary) {
|
|
|
13
13
|
const stack = [];
|
|
14
14
|
let currentSection = "";
|
|
15
15
|
const breadcrumbStack = [];
|
|
16
|
-
for (const
|
|
16
|
+
for (const raw of summary.split("\n")) {
|
|
17
|
+
// Upstream annotates some entries with an HTML comment about where their
|
|
18
|
+
// title came from (17 carried one on 2026-11-27). It is not part of the entry.
|
|
19
|
+
const line = raw.replace(/<!--.*?-->/g, "");
|
|
17
20
|
const header = HEADER_RE.exec(line);
|
|
18
21
|
if (header) {
|
|
19
|
-
|
|
22
|
+
// Keep the words and the punctuation inside names ("CI/CD", "Hardware/Physical
|
|
23
|
+
// Access"); drop the emoji every heading starts with.
|
|
24
|
+
currentSection = header[1]
|
|
25
|
+
.replace(/[^\p{L}\p{N}\s&/+.#-]/gu, "")
|
|
26
|
+
.replace(/\s+/g, " ")
|
|
27
|
+
.trim();
|
|
20
28
|
continue;
|
|
21
29
|
}
|
|
22
30
|
const m = ENTRY_RE.exec(line);
|
|
@@ -29,8 +37,10 @@ export function parseSummary(summary) {
|
|
|
29
37
|
const node = { title, path, children: [] };
|
|
30
38
|
while (stack.length > 0 && stack[stack.length - 1].indent >= indent)
|
|
31
39
|
stack.pop();
|
|
32
|
-
if (stack.length === 0)
|
|
40
|
+
if (stack.length === 0) {
|
|
41
|
+
node.category = currentSection;
|
|
33
42
|
tree.push(node);
|
|
43
|
+
}
|
|
34
44
|
else
|
|
35
45
|
stack[stack.length - 1].node.children.push(node);
|
|
36
46
|
stack.push({ indent, node });
|
|
@@ -42,7 +52,17 @@ export function parseSummary(summary) {
|
|
|
42
52
|
}
|
|
43
53
|
breadcrumbStack.push({ indent, title });
|
|
44
54
|
}
|
|
45
|
-
|
|
55
|
+
// Entries that link nowhere point at another site ("Pentesting Printers", or a
|
|
56
|
+
// page of the other book). With no page of their own they are dropped.
|
|
57
|
+
return { tree: pruneToc(tree), pages };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Remove the entries `keep` rejects, then any left that neither link to a
|
|
61
|
+
* page nor hold other entries.
|
|
62
|
+
*/
|
|
63
|
+
export function pruneToc(nodes, keep = () => true) {
|
|
64
|
+
const kept = nodes.filter(keep).map((node) => ({ ...node, children: pruneToc(node.children, keep) }));
|
|
65
|
+
return kept.filter((node) => node.path || node.children.length > 0);
|
|
46
66
|
}
|
|
47
67
|
/** Render the TOC tree as an indented plain-text outline for agent consumption. */
|
|
48
68
|
export function renderTocText(nodes, depth = 0) {
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zebbern/hacktricks-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"mcpName": "io.github.zebbern/hacktricks-mcp",
|
|
5
|
-
"description": "MCP server giving AI agents fast full-text search and section-level retrieval over the HackTricks security wiki. Ships with a pre-built
|
|
5
|
+
"description": "MCP server giving AI agents fast full-text search and section-level retrieval over the HackTricks security wiki and its Cloud book (AWS, Azure, GCP, Kubernetes, CI/CD), and a check of cloud permissions against the book's rated lists and escalation catalog. Ships with a pre-built index of both books, synced with upstream every 3 days and refreshed in the background.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
8
|
"hacktricks-mcp": "dist/server.js"
|
|
@@ -21,7 +21,13 @@
|
|
|
21
21
|
"start": "node dist/server.js",
|
|
22
22
|
"sync": "tsx scripts/sync.ts",
|
|
23
23
|
"eval": "tsx scripts/eval.ts",
|
|
24
|
+
"eval:workflow": "tsx scripts/eval-workflow.ts",
|
|
25
|
+
"eval:permissions": "tsx scripts/eval-permissions.ts",
|
|
24
26
|
"smoke": "tsx scripts/smoke.ts",
|
|
27
|
+
"smoke:refresh": "tsx scripts/smoke-refresh.ts",
|
|
28
|
+
"sync:skills": "node scripts/sync-skills.mjs",
|
|
29
|
+
"sync:agents": "node scripts/sync-agents.mjs",
|
|
30
|
+
"version": "node scripts/version-sync.mjs && node scripts/sync-skills.mjs && node scripts/sync-agents.mjs && git add server.json .claude-plugin/plugin.json .claude-plugin/marketplace.json .codex-plugin/plugin.json .zcode-plugin/plugin.json kimi-plugin/skills kimi-plugin/agents .codex/agents",
|
|
25
31
|
"prepublishOnly": "npm run build"
|
|
26
32
|
},
|
|
27
33
|
"keywords": [
|
|
@@ -47,6 +53,7 @@
|
|
|
47
53
|
"devDependencies": {
|
|
48
54
|
"@types/node": "^24.19.0",
|
|
49
55
|
"tsx": "^4.0.0",
|
|
50
|
-
"typescript": "^5.9.3"
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"yaml": "^2.9.1"
|
|
51
58
|
}
|
|
52
59
|
}
|