-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJSON-Escaper.py
180 lines (140 loc) · 6.14 KB
/
JSON-Escaper.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
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
from burp import IBurpExtender, ITab
from java.awt import GridBagLayout, GridBagConstraints, Insets
from java.io import PrintWriter
from java.lang import RuntimeException
from javax.swing import JPanel, JLabel, JTextField, JButton, JScrollPane, JTextArea, BorderFactory, SwingConstants, JPopupMenu, JMenuItem, JFileChooser
import json
class JSONEscaperTab(ITab):
def __init__(self, extender):
self._extender = extender
self._txtPayload = JTextArea(6, 20)
self._txtPayload.setLineWrap(True)
self._txtPayload.setWrapStyleWord(True)
self._scrollPanePayload = JScrollPane(self._txtPayload)
self._btnEscape = JButton("Escape", actionPerformed=self.escape)
self._txtResult = JTextArea(10, 26)
self._txtResult.setLineWrap(True)
self._txtResult.setWrapStyleWord(True)
self._txtResult.setEditable(True)
self._scrollPaneResult = JScrollPane(self._txtResult)
# Add a button for file upload
self._btnUpload = JButton("Upload File", actionPerformed=self.uploadFile)
panel = JPanel()
layout = GridBagLayout()
panel.setLayout(layout)
gbc = GridBagConstraints()
gbc.gridx = 0
gbc.gridy = 0
gbc.weightx = 1.0
gbc.weighty = 0.0
gbc.fill = GridBagConstraints.HORIZONTAL
gbc.insets = Insets(5, 5, 5, 5)
panel.add(JLabel("Payload:"), gbc)
gbc.gridy = 1
gbc.weighty = 1.0
gbc.fill = GridBagConstraints.BOTH
gbc.insets = Insets(0, 5, 5, 5)
panel.add(self._scrollPanePayload, gbc)
gbc.gridy = 2
gbc.weighty = 0.0
gbc.fill = GridBagConstraints.HORIZONTAL
panel.add(self._btnEscape, gbc)
# Add file upload button in the interface
gbc.gridy = 3
gbc.weighty = 0.0
panel.add(self._btnUpload, gbc)
gbc.gridy = 4
gbc.weighty = 0.0
gbc.fill = GridBagConstraints.HORIZONTAL
panel.add(JLabel("Escaped Payload:"), gbc)
gbc.gridy = 5
gbc.weighty = 1.0
gbc.fill = GridBagConstraints.BOTH
panel.add(self._scrollPaneResult, gbc)
# Add borders to the text areas
self._scrollPanePayload.setBorder(BorderFactory.createEtchedBorder())
self._scrollPaneResult.setBorder(BorderFactory.createEtchedBorder())
# Add right-click context menu to the input and output areas
self._txtPayload.setComponentPopupMenu(self.createPopupMenu(self._txtPayload))
self._txtResult.setComponentPopupMenu(self.createPopupMenu(self._txtResult))
self.component = panel
def getTabCaption(self):
return "JSON Escaper"
def getUiComponent(self):
return self.component
def escape(self, event):
try:
# Get the payload and split it by lines
payload = self._txtPayload.getText()
lines = payload.splitlines()
# JSON-escape each line and join them with newlines
json_escaped_lines = [json.dumps(line, separators=(',', ':'), ensure_ascii=False) for line in lines]
json_escaped = "\n".join(json_escaped_lines)
# Set the escaped payload into the result text area
self._txtResult.setText(json_escaped)
except Exception as e:
self._extender._callbacks.printError(str(e))
def uploadFile(self, event):
# Use JFileChooser to open a file chooser dialog
fileChooser = JFileChooser()
result = fileChooser.showOpenDialog(None)
if result == JFileChooser.APPROVE_OPTION:
try:
# Read the selected file
file = fileChooser.getSelectedFile()
with open(file.getPath(), 'r') as f:
content = f.read()
# Set the file content as payload and display it in the payload text area
self._txtPayload.setText(content)
# Split the file content into lines
lines = content.splitlines()
# Escape each line as JSON and join them with newlines
json_escaped_lines = [json.dumps(line, separators=(',', ':'), ensure_ascii=False) for line in lines]
json_escaped = "\n".join(json_escaped_lines)
# Display the escaped payload in the result text area
self._txtResult.setText(json_escaped)
except Exception as e:
self._extender._callbacks.printError(str(e))
def createPopupMenu(self, text_area):
# Create the right-click context menu
menu = JPopupMenu()
# Create menu items
copy_item = JMenuItem("Copy")
paste_item = JMenuItem("Paste")
cut_item = JMenuItem("Cut")
select_all_item = JMenuItem("Select All")
clear_item = JMenuItem("Clear")
# Add action listeners to the menu items
copy_item.addActionListener(lambda event: self.copyText(text_area))
paste_item.addActionListener(lambda event: self.pasteText(text_area))
cut_item.addActionListener(lambda event: self.cutText(text_area))
select_all_item.addActionListener(lambda event: self.selectAllText(text_area))
clear_item.addActionListener(lambda event: self.clearText(text_area))
# Add menu items to the menu
menu.add(copy_item)
menu.add(paste_item)
menu.add(cut_item)
menu.add(select_all_item)
menu.add(clear_item)
return menu
def copyText(self, text_area):
# Copy selected text to the clipboard
text_area.copy()
def pasteText(self, text_area):
# Paste text from the clipboard
text_area.paste()
def cutText(self, text_area):
# Cut selected text
text_area.cut()
def selectAllText(self, text_area):
# Select all text
text_area.selectAll()
def clearText(self, text_area):
# Clear the text
text_area.setText("")
class BurpExtender(IBurpExtender):
def registerExtenderCallbacks(self, callbacks):
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
callbacks.setExtensionName("JSON Escaper")
callbacks.addSuiteTab(JSONEscaperTab(self))