-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBookmarkTab.kt
389 lines (330 loc) · 13.3 KB
/
BookmarkTab.kt
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
package burp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.swing.Swing
import kotlinx.coroutines.withContext
import java.awt.FlowLayout
import java.awt.Font
import java.net.URL
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import javax.swing.*
import javax.swing.table.AbstractTableModel
import javax.swing.table.TableRowSorter
class BookmarkTab(callbacks: IBurpExtenderCallbacks) : ITab {
val bookmarkTable = BookmarksPanel(callbacks)
override fun getTabCaption() = "[^]"
override fun getUiComponent() = bookmarkTable.panel
}
data class Bookmark(
val requestResponse: IHttpRequestResponse,
val dateTime: String,
val host: String,
val url: URL,
val method: String,
val statusCode: String,
val title: String,
var tags: MutableSet<String>,
val length: String,
val mimeType: String,
val protocol: String,
val file: String,
val parameters: Boolean,
val repeated: Boolean,
var comments: String
)
class BookmarksPanel(private val callbacks: IBurpExtenderCallbacks) {
val bookmarkOptions = BookmarkOptions(this, callbacks)
val model = BookmarksModel(bookmarkOptions)
val table = JTable(model)
private val messageEditor = MessageEditor(callbacks)
val requestViewer: IMessageEditor? = messageEditor.requestViewer
val responseViewer: IMessageEditor? = messageEditor.responseViewer
val panel = JSplitPane(JSplitPane.VERTICAL_SPLIT)
val bookmarks = model.bookmarks
val rowSorter = TableRowSorter(model)
private val repeatInTable = JCheckBox("Add repeated request to table")
init {
BookmarkActions(this, callbacks)
table.autoResizeMode = JTable.AUTO_RESIZE_OFF
table.columnModel.getColumn(0).preferredWidth = 30 // ID
table.columnModel.getColumn(1).preferredWidth = 145 // date
table.columnModel.getColumn(2).preferredWidth = 125 // host
table.columnModel.getColumn(3).preferredWidth = 380 // url
table.columnModel.getColumn(4).preferredWidth = 130 // title
table.columnModel.getColumn(5).preferredWidth = 75 // tags
table.columnModel.getColumn(6).preferredWidth = 60 // repeated
table.columnModel.getColumn(7).preferredWidth = 55 // params
table.columnModel.getColumn(8).preferredWidth = 50 // method
table.columnModel.getColumn(9).preferredWidth = 50 // status
table.columnModel.getColumn(10).preferredWidth = 50 // length
table.columnModel.getColumn(11).preferredWidth = 50 // mime
table.columnModel.getColumn(12).preferredWidth = 50 // protocol
table.columnModel.getColumn(13).preferredWidth = 80 // file
table.columnModel.getColumn(14).preferredWidth = 120 // comments
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
table.rowSorter = rowSorter
table.autoscrolls = true
table.selectionModel.addListSelectionListener {
if (table.selectedRow != -1) {
val displayedBookmarks = model.displayedBookmarks
val selectedRow = table.convertRowIndexToModel(table.selectedRow)
val requestResponse = displayedBookmarks[selectedRow].requestResponse
messageEditor.requestResponse = requestResponse
requestViewer?.setMessage(requestResponse.request, true)
responseViewer?.setMessage(requestResponse.response ?: ByteArray(0), false)
}
}
val requestPanel = JPanel(FlowLayout(FlowLayout.LEFT))
val requestText = JLabel("Request")
requestText.font = Font(Font.DIALOG, Font.BOLD, 18)
val repeatButton = JButton("Repeat Request")
repeatButton.addActionListener { repeatRequest() }
repeatInTable.isSelected = true
requestPanel.layout = BoxLayout(requestPanel, BoxLayout.X_AXIS)
requestPanel.add(Box.createHorizontalStrut(20))
requestPanel.add(requestText)
requestPanel.add(Box.createHorizontalStrut(20))
requestPanel.add(repeatButton)
requestPanel.add(Box.createHorizontalStrut(10))
requestPanel.add(repeatInTable)
requestPanel.add(requestViewer?.component)
val responsePanel = JPanel(FlowLayout(FlowLayout.LEFT))
responsePanel.layout = BoxLayout(responsePanel, BoxLayout.X_AXIS)
val responseText = JLabel("Response")
responseText.font = Font(Font.DIALOG, Font.BOLD, 18)
responsePanel.add(Box.createHorizontalStrut(20))
responsePanel.add(responseText)
val resSplitVert = JSplitPane(JSplitPane.VERTICAL_SPLIT, responsePanel, responseViewer?.component)
val reqSplitVert = JSplitPane(JSplitPane.VERTICAL_SPLIT, requestPanel, requestViewer?.component)
val bookmarksTable = JScrollPane(table)
val reqResSplit =
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, reqSplitVert, resSplitVert)
reqResSplit.resizeWeight = 0.5
val bookmarksOptSplit =
JSplitPane(JSplitPane.VERTICAL_SPLIT, bookmarkOptions.panel, bookmarksTable)
panel.topComponent = bookmarksOptSplit
panel.bottomComponent = reqResSplit
panel.resizeWeight = 0.5
callbacks.customizeUiComponent(panel)
}
fun addBookmark(requestsResponses: Array<IHttpRequestResponse>) {
for (requestResponse in requestsResponses) {
createBookmark(requestResponse)
}
}
private fun createBookmark(
requestResponse: IHttpRequestResponse,
repeated: Boolean = false,
proxyHistory: Boolean = true
) {
val savedRequestResponse = callbacks.saveBuffersToTempFiles(requestResponse)
val now = LocalDateTime.now()
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val dateTime = now.format(dateFormatter) ?: ""
val requestInfo = callbacks.helpers.analyzeRequest(requestResponse)
val response = if (requestResponse.response != null) {
callbacks.helpers.analyzeResponse(requestResponse.response)
} else {
null
}
val host = requestInfo.url.host
val url = requestInfo.url
val method = requestInfo?.method ?: ""
val statusCode = response?.statusCode?.toString() ?: ""
val title = getTitle(requestResponse.response)
val tags = mutableSetOf<String>()
val mimeType = response?.inferredMimeType ?: ""
val length = requestResponse.response?.size?.toString() ?: ""
val protocol = requestInfo?.url?.protocol ?: ""
val file = requestInfo?.url?.file ?: ""
val parameters =
!requestInfo.parameters.filter { it.type != IParameter.PARAM_COOKIE }
.isEmpty() || !requestInfo.url.query.isNullOrEmpty()
val comments = requestResponse.comment ?: ""
val bookmark = Bookmark(
savedRequestResponse,
dateTime,
host,
url,
method,
statusCode,
title,
tags,
length,
mimeType,
protocol,
file,
parameters,
repeated,
comments
)
model.addBookmark(bookmark)
if (proxyHistory) {
requestResponse.highlight = "magenta"
}
}
private fun getTitle(response: ByteArray?): String {
if (response == null) return ""
val html = callbacks.helpers.bytesToString(response)
val titleRegex = "<title>(.*?)</title>".toRegex()
val title = titleRegex.find(html)?.value ?: ""
return title.removePrefix("<title>").removeSuffix("</title>")
}
private fun repeatRequest() {
model.refreshBookmarks()
GlobalScope.launch(Dispatchers.IO) {
val requestResponse = try {
callbacks.makeHttpRequest(messageEditor.httpService, requestViewer?.message)
} catch (e: java.lang.RuntimeException) {
RequestResponse(requestViewer?.message, null, messageEditor.httpService)
}
withContext(Dispatchers.Swing) {
SwingUtilities.invokeLater {
responseViewer?.setMessage(requestResponse?.response ?: ByteArray(0), false)
if (repeatInTable.isSelected && requestResponse != null) {
createBookmark(requestResponse, repeated = true, proxyHistory = false)
}
}
}
}
}
}
class MessageEditor(callbacks: IBurpExtenderCallbacks) : IMessageEditorController {
var requestResponse: IHttpRequestResponse? = null
val requestViewer: IMessageEditor? = callbacks.createMessageEditor(this, true)
val responseViewer: IMessageEditor? = callbacks.createMessageEditor(this, false)
override fun getResponse(): ByteArray = requestResponse?.response ?: ByteArray(0)
override fun getRequest(): ByteArray? = requestResponse?.request
override fun getHttpService(): IHttpService? = requestResponse?.httpService
}
class BookmarksModel(private val bookmarkOptions: BookmarkOptions) : AbstractTableModel() {
private val columns =
listOf(
"ID",
"Added",
"Host",
"URL",
"Title",
"Tags",
"Repeated",
"Params",
"Method",
"Status",
"Length",
"MIME",
"Protocol",
"File",
"Comments"
)
var bookmarks: MutableList<Bookmark> = ArrayList()
var tags: List<String> = listOf()
var displayedBookmarks: MutableList<Bookmark> = ArrayList()
private set
override fun getRowCount(): Int = displayedBookmarks.size
override fun getColumnCount(): Int = columns.size
override fun getColumnName(column: Int): String {
return columns[column]
}
override fun getColumnClass(columnIndex: Int): Class<*> {
return when (columnIndex) {
0 -> java.lang.Integer::class.java
1 -> String::class.java
2 -> String::class.java
3 -> String::class.java
4 -> String::class.java
5 -> String::class.java
6 -> java.lang.Boolean::class.java
7 -> java.lang.Boolean::class.java
8 -> String::class.java
9 -> String::class.java
10 -> String::class.java
11 -> String::class.java
12 -> String::class.java
13 -> String::class.java
14 -> String::class.java
else -> throw RuntimeException()
}
}
override fun getValueAt(rowIndex: Int, columnIndex: Int): Any {
val bookmark = displayedBookmarks[rowIndex]
return when (columnIndex) {
0 -> rowIndex
1 -> bookmark.dateTime
2 -> bookmark.host
3 -> bookmark.url.toString()
4 -> bookmark.title
5 -> bookmark.tags.joinToString()
6 -> bookmark.repeated
7 -> bookmark.parameters
8 -> bookmark.method
9 -> bookmark.statusCode
10 -> bookmark.length
11 -> bookmark.mimeType
12 -> bookmark.protocol
13 -> bookmark.file
14 -> bookmark.comments
else -> ""
}
}
override fun isCellEditable(rowIndex: Int, columnIndex: Int): Boolean {
return when (columnIndex) {
5 -> true
14 -> true
else -> false
}
}
override fun setValueAt(value: Any?, rowIndex: Int, colIndex: Int) {
val bookmark: Bookmark = bookmarks[rowIndex]
when (colIndex) {
5 -> bookmark.tags = value.toString().split(",").map { it.trim() }.toMutableSet()
14 -> bookmark.comments = value.toString()
else -> return
}
refreshBookmarks()
}
fun addBookmark(bookmark: Bookmark) {
bookmarks.add(bookmark)
displayedBookmarks = bookmarks
fireTableRowsInserted(displayedBookmarks.lastIndex, displayedBookmarks.lastIndex)
refreshBookmarks()
}
fun removeBookmarks(selectedBookmarks: MutableList<Bookmark>) {
bookmarks.removeAll(selectedBookmarks)
refreshBookmarks()
}
fun clearBookmarks() {
bookmarks.clear()
refreshBookmarks()
}
fun refreshBookmarks(updatedBookmarks: MutableList<Bookmark> = bookmarks) {
displayedBookmarks = updatedBookmarks
fireTableDataChanged()
updateTags()
}
fun updateTags() {
val newTags = displayedBookmarks.flatMap { it.tags }.toSet().toList()
tags = newTags
bookmarkOptions.updateTags()
}
}
class RequestResponse(private var req: ByteArray?, private var res: ByteArray?, private var service: IHttpService?) :
IHttpRequestResponse {
override fun getComment(): String? = null
override fun setComment(comment: String?) {}
override fun getRequest(): ByteArray? = req
override fun getHighlight(): String? = null
override fun getHttpService(): IHttpService? = service
override fun getResponse(): ByteArray? = res
override fun setResponse(message: ByteArray?) {
res = message
}
override fun setRequest(message: ByteArray?) {
req = message
}
override fun setHttpService(httpService: IHttpService?) {
service = httpService
}
override fun setHighlight(color: String?) {}
}