-
Notifications
You must be signed in to change notification settings - Fork 0
/
Universal-Match-Replacer.py
149 lines (125 loc) · 7.01 KB
/
Universal-Match-Replacer.py
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
from burp import IBurpExtender
from burp import ISessionHandlingAction
from burp import ITab
from javax import swing
from java import awt
from types import *
import re
from java.awt import Color
class BurpExtender(IBurpExtender, ITab, ISessionHandlingAction):
matchreplacestart = False
def registerExtenderCallbacks(self, callback_2):
# set our extension name
callback_2.setExtensionName("Universal Match Replacer")
self._helpers = callback_2.getHelpers()
self._callbackRef = callback_2
callback_2.registerSessionHandlingAction(self)
# Tab Match replace
self._jPanelMatchreplace = swing.JPanel()
self._jPanelMatchreplace.setLayout(awt.GridBagLayout())
self._jPanelMatchreplaceConstraints = awt.GridBagConstraints()
# UI Match Replace
# label
self._jLabelMPInfo = swing.JLabel("Universal Match Replacer")
self._jLabelMPInfo.setForeground(Color.RED)
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 0
self._jPanelMatchreplaceConstraints.gridy = 0
self._jPanelMatchreplaceConstraints.gridwidth = 2
self._jPanelMatchreplace.add(self._jLabelMPInfo, self._jPanelMatchreplaceConstraints)
# Lists to hold dynamically created fields
self.regex_text_fields = []
self.replacement_text_fields = []
# Function to add new regex and replacement text fields
def addRegexField():
index = len(self.regex_text_fields) + 1
regex_text_field = swing.JTextField("Enter Regex {}".format(index), 12)
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 0
self._jPanelMatchreplaceConstraints.gridy = index + 1 # Adjust the grid position as needed
self._jPanelMatchreplace.add(regex_text_field, self._jPanelMatchreplaceConstraints)
self.regex_text_fields.append(regex_text_field)
replacement_text_field = swing.JTextField("Enter Text to Replace {}".format(index), 18)
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 1 # Adjust the column for replacement text field
self._jPanelMatchreplaceConstraints.gridy = index + 1 # Same row as regex text field
self._jPanelMatchreplace.add(replacement_text_field, self._jPanelMatchreplaceConstraints)
self.replacement_text_fields.append(replacement_text_field)
# Refresh the UI
self._jPanelMatchreplace.revalidate()
self._jPanelMatchreplace.repaint()
# Button to add more regex and replacement text fields
self._jAddRegexFieldButton = swing.JButton("Add Regex Field", actionPerformed=lambda e: addRegexField())
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 0
self._jPanelMatchreplaceConstraints.gridy = 1 # Initial position, adjust as needed
self._jPanelMatchreplaceConstraints.gridwidth = 1
self._jPanelMatchreplace.add(self._jAddRegexFieldButton, self._jPanelMatchreplaceConstraints)
# Button to submit
self._jSetregex = swing.JButton("Submit", actionPerformed=self.startmatchreplace)
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 1 # Initial column, adjust as needed
self._jPanelMatchreplaceConstraints.gridy = 1 # Initial position, adjust as needed
self._jPanelMatchreplace.add(self._jSetregex, self._jPanelMatchreplaceConstraints)
# Button to clear
self._jtextclear = swing.JButton("Clean", actionPerformed=self.mrclear)
self._jPanelMatchreplaceConstraints.fill = awt.GridBagConstraints.HORIZONTAL
self._jPanelMatchreplaceConstraints.gridx = 2 # Initial column, adjust as needed
self._jPanelMatchreplaceConstraints.gridy = 1 # Initial position, adjust as needed
self._jPanelMatchreplace.add(self._jtextclear, self._jPanelMatchreplaceConstraints)
# Create Match Replace Panel
self._jConfigTab = swing.JTabbedPane()
self._jConfigTab.addTab("Match Replace", self._jPanelMatchreplace)
callback_2.customizeUiComponent(self._jConfigTab)
callback_2.addSuiteTab(self)
# Implement ISessionHandlingAction
def getActionName(self):
return "Universal Match Replacer"
# Implement ITab
def getTabCaption(self):
return 'Universal Match Replacer'
def getUiComponent(self):
return self._jConfigTab
# Function to find header and body of request
def getRequestHeadersAndBody(self, content):
request = content.getRequest()
request_data = self._helpers.analyzeRequest(request)
headers = list(request_data.getHeaders() or '')
body = request[request_data.getBodyOffset():].tostring()
return headers, body
# Function to find response headers
def getResponseHeadersAndBody(self, content):
request = content.getResponse()
request_data = self._helpers.analyzeResponse(request)
headers = list(request_data.getHeaders() or '')
body = request[request_data.getBodyOffset():].tostring()
return headers, body
def startmatchreplace(self, e):
self.matchreplacestart = True
self.mr_regexes = [field.getText() for field in self.regex_text_fields]
self.mr_texts = [field.getText() for field in self.replacement_text_fields]
self._jSetregex.setEnabled(False)
def mrclear(self, e):
for field in self.regex_text_fields:
self._jPanelMatchreplace.remove(field)
for field in self.replacement_text_fields:
self._jPanelMatchreplace.remove(field)
self.mr_regexes = []
self.mr_texts = []
self.matchreplacestart = False
self._jSetregex.setEnabled(True)
self.regex_text_fields = []
self.replacement_text_fields = []
# This function is used to process HTTP messages
def performAction(self, currentRequest, macroItems):
if self.matchreplacestart:
headers = self._helpers.analyzeRequest(currentRequest).getHeaders()
request_body = currentRequest.getRequest()[self._helpers.analyzeRequest(currentRequest).getBodyOffset():].tostring()
for regex, text in zip(self.mr_regexes, self.mr_texts):
# Process headers
for i, header in enumerate(headers):
headers[i] = re.sub(regex, text, header)
# Process body
request_body = re.sub(regex, text, request_body)
new_request = self._helpers.buildHttpMessage(headers, request_body)
currentRequest.setRequest(new_request)