-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (154 loc) · 4.99 KB
/
index.js
File metadata and controls
166 lines (154 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
Tool,
} from "@modelcontextprotocol/sdk/types.js";
import chalk from 'chalk';
class ReasonerServer {
constructor() {
this.attackSteps = [];
this.branches = {};
}
validateInput(input) {
const data = input;
if (!data.attackStep || typeof data.attackStep !== 'string') {
throw new Error('Invalid attackStep: must be a string');
}
if (!data.attackStepNumber || typeof data.attackStepNumber !== 'number') {
throw new Error('Invalid attackStepNumber: must be a number');
}
if (!data.totalAttackSteps || typeof data.totalAttackSteps !== 'number') {
throw new Error('Invalid totalAttackSteps: must be a number');
}
if (typeof data.nextAttackStepNeeded !== 'boolean') {
throw new Error('Invalid nextAttackStepNeeded: must be a boolean');
}
return true;
}
formatAttackStep(attackStepData) {
const { attackStepNumber, totalAttackSteps, attackStep, asset, recommendedTool, critical } = attackStepData;
const prefix = critical ? chalk.red('🔥 Critical Path') : chalk.blue('🛡️ Attack Step');
const header = `${prefix} ${attackStepNumber}/${totalAttackSteps}`;
const border = '─'.repeat(Math.max(header.length, attackStep.length) + 4);
return `\n┌${border}┐\n│ ${header.padEnd(border.length - 2)} │\n├${border}┤\n│ ${attackStep.padEnd(border.length - 2)} │\n│ Target Asset: ${asset || 'N/A'}${' '.repeat(Math.max(0, border.length - 15 - (asset ? asset.length : 3)))}│\n│ Recommended Tool: ${recommendedTool || 'N/A'}${' '.repeat(Math.max(0, border.length - 22 - (recommendedTool ? recommendedTool.length : 3)))}│\n└${border}┘`;
}
processAttackStep(input) {
try {
this.validateInput(input);
if (input.attackStepNumber > input.totalAttackSteps) {
input.totalAttackSteps = input.attackStepNumber;
}
this.attackSteps.push(input);
const formattedAttackStep = this.formatAttackStep(input);
console.error(formattedAttackStep);
return {
content: [{
type: "text",
text: JSON.stringify({
attackStepNumber: input.attackStepNumber,
totalAttackSteps: input.totalAttackSteps,
nextAttackStepNeeded: input.nextAttackStepNeeded,
attackStepCount: this.attackSteps.length,
asset: input.asset,
recommendedTool: input.recommendedTool,
critical: input.critical || false
}, null, 2)
}]
};
} catch (error) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: error.message,
status: 'failed'
}, null, 2)
}],
isError: true
};
}
}
}
const REASONER_TOOL = {
name: "pentestthinking",
description: "A pentest reasoning engine that helps break down and analyze attack paths step by step",
inputSchema: {
type: "object",
properties: {
attackStep: {
type: "string",
description: "The current attack step description"
},
attackStepNumber: {
type: "integer",
description: "Current step number",
minimum: 1
},
totalAttackSteps: {
type: "integer",
description: "Estimated total steps needed",
minimum: 1
},
nextAttackStepNeeded: {
type: "boolean",
description: "Whether another step is needed"
},
asset: {
type: "string",
description: "Target asset for this step"
},
recommendedTool: {
type: "string",
description: "Recommended tool for this step"
},
critical: {
type: "boolean",
description: "Is this step part of the critical path?"
}
},
required: ["attackStep", "attackStepNumber", "totalAttackSteps", "nextAttackStepNeeded"]
}
};
// Initialize MCP server
const server = new Server(
{
name: "pentestthinkingMCP",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
const reasonerServer = new ReasonerServer();
// Register tool listing handler
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [REASONER_TOOL],
}));
// Register tool execution handler
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "pentestthinking") {
return reasonerServer.processAttackStep(request.params.arguments);
}
return {
content: [{
type: "text",
text: `Unknown tool: ${request.params.name}`
}],
isError: true
};
});
// Start the server
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Reasoner MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});