-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathTools.kt
More file actions
361 lines (290 loc) · 13.4 KB
/
Copy pathTools.kt
File metadata and controls
361 lines (290 loc) · 13.4 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package net.portswigger.mcp.tools
import burp.api.montoya.MontoyaApi
import burp.api.montoya.burpsuite.TaskExecutionEngine.TaskExecutionEngineState.PAUSED
import burp.api.montoya.burpsuite.TaskExecutionEngine.TaskExecutionEngineState.RUNNING
import burp.api.montoya.core.BurpSuiteEdition
import burp.api.montoya.http.HttpMode
import burp.api.montoya.http.HttpService
import burp.api.montoya.http.message.HttpHeader
import burp.api.montoya.http.message.requests.HttpRequest
import io.modelcontextprotocol.kotlin.sdk.server.Server
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import net.portswigger.mcp.config.McpConfig
import net.portswigger.mcp.schema.toSerializableForm
import java.awt.KeyboardFocusManager
import java.util.regex.Pattern
import javax.swing.JTextArea
fun Server.registerTools(api: MontoyaApi, config: McpConfig) {
mcpTool<SendHttp1Request>("Issues an HTTP/1.1 request and returns the response.") {
val fixedContent = content.replace("\r", "").replace("\n", "\r\n")
val request = HttpRequest.httpRequest(toMontoyaService(), fixedContent)
val response = api.http().sendRequest(request)
response?.toString() ?: "<no response>"
}
mcpTool<SendHttp2Request>("Issues an HTTP/2 request and returns the response. Do NOT pass headers to the body parameter.") {
val orderedPseudoHeaderNames = listOf(":scheme", ":method", ":path", ":authority")
val fixedPseudoHeaders = LinkedHashMap<String, String>().apply {
orderedPseudoHeaderNames.forEach { name ->
val value = pseudoHeaders[name.removePrefix(":")] ?: pseudoHeaders[name]
if (value != null) {
put(name, value)
}
}
pseudoHeaders.forEach { (key, value) ->
val properKey = if (key.startsWith(":")) key else ":$key"
if (!containsKey(properKey)) {
put(properKey, value)
}
}
}
val headerList = (fixedPseudoHeaders + headers).map { HttpHeader.httpHeader(it.key.lowercase(), it.value) }
val request = HttpRequest.http2Request(toMontoyaService(), headerList, requestBody)
val response = api.http().sendRequest(request, HttpMode.HTTP_2)
response?.toString() ?: "<no response>"
}
mcpTool<CreateRepeaterTab>("Creates a new Repeater tab with the specified HTTP request and optional tab name. Make sure to use carriage returns appropriately.") {
val request = HttpRequest.httpRequest(toMontoyaService(), content)
api.repeater().sendToRepeater(request, tabName)
}
mcpTool<SendToIntruder>("Sends an HTTP request to Intruder with the specified HTTP request and optional tab name. Make sure to use carriage returns appropriately.") {
val request = HttpRequest.httpRequest(toMontoyaService(), content)
api.intruder().sendToIntruder(request, tabName)
}
mcpTool<UrlEncode>("URL encodes the input string") {
api.utilities().urlUtils().encode(content)
}
mcpTool<UrlDecode>("URL decodes the input string") {
api.utilities().urlUtils().decode(content)
}
mcpTool<Base64Encode>("Base64 encodes the input string") {
api.utilities().base64Utils().encodeToString(content)
}
mcpTool<Base64Decode>("Base64 decodes the input string") {
api.utilities().base64Utils().decode(content).toString()
}
mcpTool<GenerateRandomString>("Generates a random string of specified length and character set") {
api.utilities().randomUtils().randomString(length, characterSet)
}
mcpTool(
"output_project_options",
"Outputs current project-level configuration in JSON format. You can use this to determine the schema for available config options."
) {
api.burpSuite().exportProjectOptionsAsJson()
}
mcpTool(
"output_user_options",
"Outputs current user-level configuration in JSON format. You can use this to determine the schema for available config options."
) {
api.burpSuite().exportUserOptionsAsJson()
}
val toolingDisabledMessage =
"User has disabled configuration editing. They can enable it in the MCP tab in Burp by selecting 'Enable tools that can edit your config'"
mcpTool<SetProjectOptions>("Sets project-level configuration in JSON format. This will be merged with existing configuration. Make sure to export before doing this, so you know what the schema is. Make sure the JSON has a top level 'user_options' object!") {
if (config.configEditingTooling) {
api.logging().logToOutput("Setting project-level configuration: $json")
api.burpSuite().importProjectOptionsFromJson(json)
"Project configuration has been applied"
} else {
toolingDisabledMessage
}
}
mcpTool<SetUserOptions>("Sets user-level configuration in JSON format. This will be merged with existing configuration. Make sure to export before doing this, so you know what the schema is. Make sure the JSON has a top level 'project_options' object!") {
if (config.configEditingTooling) {
api.logging().logToOutput("Setting user-level configuration: $json")
api.burpSuite().importUserOptionsFromJson(json)
"User configuration has been applied"
} else {
toolingDisabledMessage
}
}
if (api.burpSuite().version().edition() == BurpSuiteEdition.PROFESSIONAL) {
mcpPaginatedTool<GetScannerIssues>("Displays information about issues identified by the scanner") {
api.siteMap().issues().asSequence().map { Json.encodeToString(it.toSerializableForm()) }
}
}
mcpPaginatedTool<GetProxyHttpHistory>("Displays items within the proxy HTTP history") {
api.proxy().history().asSequence()
.map {
// Limit the size of serialized data to prevent overflow
val serialized = Json.encodeToString(it.toSerializableForm())
if (serialized.length > 5000) {
// Truncate long responses to prevent chat overflow
val truncated = serialized.substring(0, 5000) + "... (truncated)"
truncated
} else {
serialized
}
}
}
mcpPaginatedTool<GetProxyHttpHistoryRegex>("Displays items matching a specified regex within the proxy HTTP history") {
val compiledRegex = Pattern.compile(regex)
api.proxy().history { it.contains(compiledRegex) }.asSequence()
.map {
// Limit the size of serialized data to prevent overflow
val serialized = Json.encodeToString(it.toSerializableForm())
if (serialized.length > 5000) {
// Truncate long responses to prevent chat overflow
val truncated = serialized.substring(0, 5000) + "... (truncated)"
truncated
} else {
serialized
}
}
}
mcpPaginatedTool<GetProxyHttpHistoryScope>("Displays all in-scope items within the proxy HTTP history") {
api.proxy().history { api.scope().isInScope(it.toString()) }.asSequence()
.map {
// Limit the size of serialized data to prevent overflow
val serialized = Json.encodeToString(it.toSerializableForm())
if (serialized.length > 5000) {
// Truncate long responses to prevent chat overflow
val truncated = serialized.substring(0, 5000) + "... (truncated)"
truncated
} else {
serialized
}
}
}
mcpPaginatedTool<GetProxyWebsocketHistory>("Displays items within the proxy WebSocket history") {
api.proxy().webSocketHistory().asSequence()
.map {
// Limit the size of serialized data to prevent overflow
val serialized = Json.encodeToString(it.toSerializableForm())
if (serialized.length > 5000) {
// Truncate long responses to prevent chat overflow
val truncated = serialized.substring(0, 5000) + "... (truncated)"
truncated
} else {
serialized
}
}
}
mcpPaginatedTool<GetProxyWebsocketHistoryRegex>("Displays items matching a specified regex within the proxy WebSocket history") {
val compiledRegex = Pattern.compile(regex)
api.proxy().webSocketHistory { it.contains(compiledRegex) }.asSequence()
.map {
// Limit the size of serialized data to prevent overflow
val serialized = Json.encodeToString(it.toSerializableForm())
if (serialized.length > 5000) {
// Truncate long responses to prevent chat overflow
val truncated = serialized.substring(0, 5000) + "... (truncated)"
truncated
} else {
serialized
}
}
}
mcpTool<SetTaskExecutionEngineState>("Sets the state of Burp's task execution engine (paused or unpaused)") {
api.burpSuite().taskExecutionEngine().state = if (running) RUNNING else PAUSED
"Task execution engine is now ${if (running) "running" else "paused"}"
}
mcpTool<SetProxyInterceptState>("Enables or disables Burp Proxy Intercept") {
if (intercepting) {
api.proxy().enableIntercept()
} else {
api.proxy().disableIntercept()
}
"Intercept has been ${if (intercepting) "enabled" else "disabled"}"
}
mcpTool("get_active_editor_contents", "Outputs the contents of the user's active message editor") {
getActiveEditor(api)?.text ?: "<No active editor>"
}
mcpTool<SetActiveEditorContents>("Sets the content of the user's active message editor") {
val editor = getActiveEditor(api) ?: return@mcpTool "<No active editor>"
if (!editor.isEditable) {
return@mcpTool "<Current editor is not editable>"
}
editor.text = text
"Editor text has been set"
}
mcpTool<AddTargetToScope>("Adds the specified target to Burp Suite's scope") {
api.scope().includeInScope(target)
"Target has been added to scope"
}
}
fun getActiveEditor(api: MontoyaApi): JTextArea? {
val frame = api.userInterface().swingUtils().suiteFrame()
val focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager()
val permanentFocusOwner = focusManager.permanentFocusOwner
val isInBurpWindow = generateSequence(permanentFocusOwner) { it.parent }
.any { it == frame }
return if (isInBurpWindow && permanentFocusOwner is JTextArea) {
permanentFocusOwner
} else {
null
}
}
interface HttpServiceParams {
val targetHostname: String
val targetPort: Int
val usesHttps: Boolean
fun toMontoyaService(): HttpService = HttpService.httpService(targetHostname, targetPort, usesHttps)
}
@Serializable
data class SendHttp1Request(
val content: String,
override val targetHostname: String,
override val targetPort: Int,
override val usesHttps: Boolean
) : HttpServiceParams
@Serializable
data class SendHttp2Request(
val pseudoHeaders: Map<String, String>,
val headers: Map<String, String>,
val requestBody: String,
override val targetHostname: String,
override val targetPort: Int,
override val usesHttps: Boolean
) : HttpServiceParams
@Serializable
data class CreateRepeaterTab(
val tabName: String = "",
val content: String,
override val targetHostname: String,
override val targetPort: Int,
override val usesHttps: Boolean
) : HttpServiceParams
@Serializable
data class SendToIntruder(
val tabName: String = "",
val content: String,
override val targetHostname: String,
override val targetPort: Int,
override val usesHttps: Boolean
) : HttpServiceParams
@Serializable
data class UrlEncode(val content: String)
@Serializable
data class UrlDecode(val content: String)
@Serializable
data class Base64Encode(val content: String)
@Serializable
data class Base64Decode(val content: String)
@Serializable
data class GenerateRandomString(val length: Int, val characterSet: String)
@Serializable
data class SetProjectOptions(val json: String)
@Serializable
data class SetUserOptions(val json: String)
@Serializable
data class SetTaskExecutionEngineState(val running: Boolean)
@Serializable
data class SetProxyInterceptState(val intercepting: Boolean)
@Serializable
data class SetActiveEditorContents(val text: String)
@Serializable
data class AddTargetToScope(val target: String)
@Serializable
data class GetScannerIssues(override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetProxyHttpHistory(override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetProxyHttpHistoryRegex(val regex: String, override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetProxyHttpHistoryScope( override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetProxyWebsocketHistory(override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetProxyWebsocketHistoryRegex(val regex: String, override val count: Int, override val offset: Int) :
Paginated