-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathTools.kt
More file actions
543 lines (445 loc) · 20.7 KB
/
Copy pathTools.kt
File metadata and controls
543 lines (445 loc) · 20.7 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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.collaborator.InteractionFilter
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.coroutines.runBlocking
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import net.portswigger.mcp.config.McpConfig
import net.portswigger.mcp.schema.toSerializableForm
import net.portswigger.mcp.security.DataAccessSecurity
import net.portswigger.mcp.security.DataAccessType
import net.portswigger.mcp.security.HttpRequestSecurity
import net.portswigger.mcp.security.filterConfigCredentials
import java.awt.KeyboardFocusManager
import java.util.regex.Pattern
import javax.swing.JTextArea
private suspend fun checkDataAccessOrDeny(
accessType: DataAccessType, config: McpConfig, api: MontoyaApi, logMessage: String
): Boolean {
val allowed = DataAccessSecurity.checkDataAccessPermission(accessType, config)
if (!allowed) {
api.logging().logToOutput("MCP $logMessage access denied")
return false
}
api.logging().logToOutput("MCP $logMessage access granted")
return true
}
private fun truncateIfNeeded(serialized: String): String {
return if (serialized.length > 5000) {
serialized.substring(0, 5000) + "... (truncated)"
} else {
serialized
}
}
private fun buildHttp2HeaderList(
pseudoHeaders: Map<String, String>, headers: Map<String, String>
): List<HttpHeader> {
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)
}
}
}
return (fixedPseudoHeaders + headers).map { HttpHeader.httpHeader(it.key.lowercase(), it.value) }
}
/**
* Normalizes HTTP request line endings from MCP clients.
*
* MCP clients (e.g. Claude Code) often emit `\r\n` as the 4-character literal
* sequence backslash-r-backslash-n in JSON tool parameters rather than actual
* CR (0x0D) + LF (0x0A) bytes. The resulting text parses as a single line,
* which strict servers (e.g. Apache-Coyote) reject with 400 Bad Request and
* which Burp/Montoya may "repair" by injecting headers after the body
* separator.
*
* Normalization is applied only to the request prelude (request line and
* headers, up to and including the first blank line). The body is preserved
* verbatim so that legitimate escape sequences in bodies — e.g. `\n` inside a
* JSON string literal — and binary payloads remain byte-exact. If no blank
* line is present, the entire content is treated as prelude.
*/
internal fun normalizeHttpContent(content: String): String {
val preludeEnd = findPreludeEnd(content) ?: return normalizePrelude(content)
return normalizePrelude(content.substring(0, preludeEnd)) + content.substring(preludeEnd)
}
private val BLANK_LINE_MARKERS = listOf(
"\r\n\r\n", // actual CRLF blank line
"\n\n", // actual LF blank line
"\\r\\n\\r\\n", // literal CRLF blank line
"\\n\\n", // literal LF blank line
)
private fun findPreludeEnd(content: String): Int? {
var bestStart = -1
var bestLen = 0
for (marker in BLANK_LINE_MARKERS) {
val idx = content.indexOf(marker)
if (idx >= 0 && (bestStart < 0 || idx < bestStart)) {
bestStart = idx
bestLen = marker.length
}
}
return if (bestStart < 0) null else bestStart + bestLen
}
private fun normalizePrelude(prelude: String): String = prelude
.replace("\\r\\n", "\n") // Literal \r\n escape sequences → LF
.replace("\\n", "\n") // Remaining literal \n → LF
.replace("\\r", "") // Remaining literal \r → remove
.replace("\r", "") // Actual CR → remove
.replace("\n", "\r\n") // All LF → proper CRLF
fun Server.registerTools(api: MontoyaApi, config: McpConfig) {
mcpTool<SendHttp1Request>("Issues an HTTP/1.1 request and returns the response.") {
val allowed = runBlocking {
HttpRequestSecurity.checkHttpRequestPermission(targetHostname, targetPort, config, content, api)
}
if (!allowed) {
api.logging().logToOutput("MCP HTTP request denied: $targetHostname:$targetPort")
return@mcpTool "Send HTTP request denied by Burp Suite"
}
api.logging().logToOutput("MCP HTTP/1.1 request: $targetHostname:$targetPort")
val fixedContent = normalizeHttpContent(content)
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 http2RequestDisplay = buildString {
pseudoHeaders.forEach { (key, value) ->
val headerName = if (key.startsWith(":")) key else ":$key"
appendLine("$headerName: $value")
}
headers.forEach { (key, value) ->
appendLine("$key: $value")
}
if (requestBody.isNotBlank()) {
appendLine()
append(requestBody)
}
}
val allowed = runBlocking {
HttpRequestSecurity.checkHttpRequestPermission(targetHostname, targetPort, config, http2RequestDisplay, api)
}
if (!allowed) {
api.logging().logToOutput("MCP HTTP request denied: $targetHostname:$targetPort")
return@mcpTool "Send HTTP request denied by Burp Suite"
}
api.logging().logToOutput("MCP HTTP/2 request: $targetHostname:$targetPort")
val headerList = buildHttp2HeaderList(pseudoHeaders, headers)
val request = HttpRequest.http2Request(toMontoyaService(), headerList, requestBody)
val response = api.http().sendRequest(request, HttpMode.HTTP_2)
response?.toString() ?: "<no response>"
}
mcpTool<CreateRepeaterTab>("Creates an HTTP/1.1 Repeater tab with the specified raw HTTP request and optional tab name. Make sure to use carriage returns appropriately. Prefer create_repeater_tab_http2 for modern web targets that speak HTTP/2.") {
val fixedContent = normalizeHttpContent(content)
val request = HttpRequest.httpRequest(toMontoyaService(), fixedContent)
api.repeater().sendToRepeater(request, tabName)
}
mcpTool<CreateRepeaterTabHttp2>("Creates an HTTP/2 Repeater tab with the specified HTTP/2 request and optional tab name. Use this by default for modern web targets. Do NOT pass headers to the body parameter.") {
val headerList = buildHttp2HeaderList(pseudoHeaders, headers)
val request = HttpRequest.http2Request(toMontoyaService(), headerList, requestBody)
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 fixedContent = normalizeHttpContent(content)
val request = HttpRequest.httpRequest(toMontoyaService(), fixedContent)
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."
) {
val json = api.burpSuite().exportProjectOptionsAsJson()
if (config.filterConfigCredentials) {
filterConfigCredentials(json)
} else {
json
}
}
mcpTool(
"output_user_options",
"Outputs current user-level configuration in JSON format. You can use this to determine the schema for available config options."
) {
val json = api.burpSuite().exportUserOptionsAsJson()
if (config.filterConfigCredentials) {
filterConfigCredentials(json)
} else {
json
}
}
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()) }
}
val collaboratorClient by lazy { api.collaborator().createClient() }
mcpTool<GenerateCollaboratorPayload>(
"Generates a Burp Collaborator payload URL for out-of-band (OOB) testing. " +
"Inject this payload into requests to detect server-side interactions (DNS lookups, HTTP requests, SMTP). " +
"Use get_collaborator_interactions with the returned payloadId to check for interactions."
) {
api.logging().logToOutput("MCP generating Collaborator payload${customData?.let { " with custom data" } ?: ""}")
val payload = if (customData != null) {
collaboratorClient.generatePayload(customData)
} else {
collaboratorClient.generatePayload()
}
val server = collaboratorClient.server()
"Payload: $payload\nPayload ID: ${payload.id()}\nCollaborator server: ${server.address()}"
}
mcpTool<GetCollaboratorInteractions>(
"Polls Burp Collaborator for out-of-band interactions (DNS, HTTP, SMTP). " +
"Optionally filter by payloadId from generate_collaborator_payload. " +
"Returns interaction details including type, timestamp, client IP, and protocol-specific data."
) {
api.logging().logToOutput("MCP polling Collaborator interactions${payloadId?.let { " for payload: $it" } ?: ""}")
val interactions = if (payloadId != null) {
collaboratorClient.getInteractions(InteractionFilter.interactionIdFilter(payloadId))
} else {
collaboratorClient.getAllInteractions()
}
if (interactions.isEmpty()) {
"No interactions detected"
} else {
interactions.joinToString("\n\n") {
Json.encodeToString(it.toSerializableForm())
}
}
}
}
mcpPaginatedTool<GetProxyHttpHistory>("Displays items within the proxy HTTP history") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.HTTP_HISTORY, config, api, "HTTP history")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("HTTP history access denied by Burp Suite")
}
api.proxy().history().asSequence().map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
mcpPaginatedTool<GetProxyHttpHistoryRegex>("Displays items matching a specified regex within the proxy HTTP history") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.HTTP_HISTORY, config, api, "HTTP history")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("HTTP history access denied by Burp Suite")
}
val compiledRegex = Pattern.compile(regex)
api.proxy().history { it.contains(compiledRegex) }.asSequence()
.map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
mcpPaginatedTool<GetOrganizerItems>("Displays items within the Organizer tab") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.ORGANIZER, config, api, "Organizer")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("Organizer access denied by Burp Suite")
}
api.organizer().items().asSequence().map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
mcpPaginatedTool<GetOrganizerItemsRegex>("Displays items matching a specified regex within the Organizer tab") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.ORGANIZER, config, api, "Organizer")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("Organizer access denied by Burp Suite")
}
val compiledRegex = Pattern.compile(regex)
api.organizer().items { it.contains(compiledRegex) }.asSequence()
.map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
mcpPaginatedTool<GetProxyWebsocketHistory>("Displays items within the proxy WebSocket history") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.WEBSOCKET_HISTORY, config, api, "WebSocket history")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("WebSocket history access denied by Burp Suite")
}
api.proxy().webSocketHistory().asSequence()
.map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
mcpPaginatedTool<GetProxyWebsocketHistoryRegex>("Displays items matching a specified regex within the proxy WebSocket history") {
val allowed = runBlocking {
checkDataAccessOrDeny(DataAccessType.WEBSOCKET_HISTORY, config, api, "WebSocket history")
}
if (!allowed) {
return@mcpPaginatedTool sequenceOf("WebSocket history access denied by Burp Suite")
}
val compiledRegex = Pattern.compile(regex)
api.proxy().webSocketHistory { it.contains(compiledRegex) }.asSequence()
.map { truncateIfNeeded(Json.encodeToString(it.toSerializableForm())) }
}
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"
}
}
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 CreateRepeaterTabHttp2(
val tabName: String,
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 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 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 GetOrganizerItems(override val count: Int, override val offset: Int) : Paginated
@Serializable
data class GetOrganizerItemsRegex(val regex: String, 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
@Serializable
data class GenerateCollaboratorPayload(
val customData: String? = null
)
@Serializable
data class GetCollaboratorInteractions(
val payloadId: String? = null
)