forked from PortSwigger/python-scripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
340 lines (277 loc) · 15.8 KB
/
gui.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
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
from javax.swing import JTabbedPane, JPanel, JButton, JLabel, JTextField, SwingConstants, JOptionPane, GroupLayout, JCheckBox, JSplitPane, JRadioButton, ButtonGroup, JFileChooser
from javax.swing.event import ChangeListener, DocumentListener
from javax.swing.LayoutStyle.ComponentPlacement import RELATED, UNRELATED
from java.awt import BorderLayout, Font, Component, Color
from java.beans import PropertyChangeListener
from org.python.core.util import StringUtil
from burp import IExtensionStateListener
from uicomponents import BurpUI, TabComponent, TabComponentEditableTabMixin, TabComponentCloseableMixin, TabComponentCloseListener, TabComponentTitleChangedListener
from models import ObservableCollection, Script
from utils import EditorFileAdapter
class ScriptTabbedPane(JTabbedPane):
EMPTY_SCRIPT_TEXT = '''<html><body>
<div style="font-size: 16pt;text-align:center">
You have no Python scripts created.<br/> Please use the add tab (+) button to create a new Python script.
</div></body></html>'''
def __init__(self, extender, callbacks, helpers, scripts):
super(ScriptTabbedPane, self).__init__()
self.scripts = scripts
self.extender = extender
self.callbacks = callbacks
self.helpers = helpers
self.addChangeListener(ScriptTabbedPane.TabsStateChanged())
self.scripts.add_listener(self)
self.create_add_tab()
def create_add_tab(self):
self.add_tab = JButton("+", actionPerformed=self.add_clicked)
self.add_tab.font = Font('Monospaced', Font.PLAIN, 18)
self.add_tab.contentAreaFilled = False
self.add_tab.border = None
self.emptyTab = JPanel(BorderLayout())
emptyScriptLabel = JLabel(ScriptTabbedPane.EMPTY_SCRIPT_TEXT, SwingConstants.CENTER)
emptyScriptLabel.putClientProperty("html.disable", None)
self.emptyTab.add(emptyScriptLabel, BorderLayout.CENTER)
self.addTab(None, self.emptyTab)
self.setTabComponentAt(0, self.add_tab)
def add_clicked(self, event):
idx = self.tabCount - 1
title = 'New Script {}'.format(idx + 1)
script = Script(self.extender, self.callbacks, self.helpers, title)
self.scripts.add(script)
def create_script_tab(self, script, idx):
new_tab = ScriptTabComponent(script)
new_tab.tabbed_pane = self
new_tab.addCloseListener(ScriptTabbedPane.ScriptTabCloseListener(self, self.scripts, script))
new_tab.addTitleChangedListener(ScriptTabbedPane.ScriptTabTabTitleChangedListener(script))
new_panel = ScriptPanel(script, self.callbacks)
self.add(new_panel, idx)
self.setTabComponentAt(idx, new_tab)
self.selectedIndex = idx
def collection_changed(self, collection, type, script):
if type == ObservableCollection.ITEM_ADDED:
idx = self.tabCount - 1
self.create_script_tab(script, idx)
class ScriptTabCloseListener(TabComponentCloseListener):
def __init__(self, tabbedpane, scripts, script):
self.tabbed_pane = tabbedpane
self.scripts = scripts
self.script = script
def tabClose(self, event):
result = JOptionPane.showConfirmDialog(None, 'Are you sure you want to close \'{}\' ?'.format(event.source.text),
"Close Tab",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE)
if result == JOptionPane.YES_OPTION:
idx = self.tabbed_pane.indexOfTabComponent(event.source)
self.tabbed_pane.remove(idx)
self.scripts.remove(self.script)
class ScriptTabTabTitleChangedListener(TabComponentTitleChangedListener):
def __init__(self, script):
self.script = script
def titleChanged(self, event):
self.script.title = event.getTitle()
class TabsStateChanged(ChangeListener):
def stateChanged(self, event):
# prevents the add tab from being selected apart from when there are no other tabs created (i.e. starting from fresh)
# instead the tab next the add tab is selected
tabbed_pane = event.source
if tabbed_pane.tabCount > 1 and tabbed_pane.selectedIndex == tabbed_pane.tabCount - 1:
tabbed_pane.selectedIndex = tabbed_pane.tabCount - 2
class ScriptTabComponent(TabComponentEditableTabMixin, TabComponentCloseableMixin, TabComponent):
def __init__(self, script):
super(ScriptTabComponent, self).__init__()
self.script = script
self.text = self.script.title
self.close_button.font = Font('Dialog', Font.PLAIN, 16)
self.text_field.toolTipText = self.toolTipText = 'Double click to rename, Enter to confirm or Esc to cancel'
class ScriptEditingPanel(JPanel, DocumentListener):
def __init__(self, callbacks, script):
super(ScriptEditingPanel, self).__init__()
self.callbacks = callbacks
self.script = script
self.enabledCheckbox = JCheckBox('Enabled', self.script.enabled, itemStateChanged=self.enabled_changed, alignmentX=Component.LEFT_ALIGNMENT)
self.scriptEditor = callbacks.createTextEditor()
self.scriptEditor.text = script.content
self.scriptText = self.scriptEditor.component
self.loadButton = JButton('Load', actionPerformed=self.load)
self.compileButton = JButton('Compile', actionPerformed=self.compile, enabled=False)
editingLayout = GroupLayout(self, autoCreateGaps=True, autoCreateContainerGaps=True)
editingLayout.setHorizontalGroup(editingLayout.createParallelGroup()
.addGroup(editingLayout.createSequentialGroup()
.addComponent(self.enabledCheckbox)
.addPreferredGap(UNRELATED)
)
.addGroup(editingLayout.createParallelGroup()
.addComponent(self.scriptText)
)
.addGroup(editingLayout.createSequentialGroup()
.addComponent(self.loadButton)
.addComponent(self.compileButton)
)
)
editingLayout.setVerticalGroup(editingLayout.createSequentialGroup()
.addGroup(editingLayout.createParallelGroup()
.addComponent(self.enabledCheckbox)
)
.addGroup(editingLayout.createSequentialGroup()
.addComponent(self.scriptText)
)
.addGroup(editingLayout.createParallelGroup()
.addComponent(self.loadButton)
.addComponent(self.compileButton)
)
)
self.layout = editingLayout
BurpUI.get_textarea(self.scriptEditor).document.addDocumentListener(self)
self.compile(None)
def enabled_changed(self, event):
self.script.enabled = self.enabledCheckbox.isSelected()
def load(self, event):
file_chooser = JFileChooser()
choice = file_chooser.showOpenDialog(None)
if choice == JFileChooser.APPROVE_OPTION:
with open(file_chooser.selectedFile.path, 'r') as input_file:
self.scriptEditor.text = ''.join(input_file.readlines())
def compile(self, event):
self.script.compile()
self.compileButton.enabled = False
def changedUpdate(self, event):
self._update_content()
self._can_compile(event)
def insertUpdate(self, event):
self._update_content()
self._can_compile(event)
def removeUpdate(self, event):
self._update_content()
self._can_compile(event)
def _update_content(self):
self.script.content = StringUtil.fromBytes(self.scriptEditor.text)
def _can_compile(self, event):
self.compileButton.enabled = False
if event.document.length > 0:
self.compileButton.enabled = self.script.requires_compile
class ScriptOutputPanel(JPanel, PropertyChangeListener, IExtensionStateListener):
def __init__(self, callbacks, script):
super(ScriptOutputPanel, self).__init__()
self.callbacks = callbacks
self.script = script
self.script.addPropertyChangeListener(self)
self.tabbedPane = JTabbedPane()
self._create_output_panel()
self._create_error_panel()
self.tabbedPane.addTab('Output', self.outputPanel)
self.tabbedPane.addTab('Errors', self.errorPanel)
self.layout = BorderLayout()
self.add(self.tabbedPane, BorderLayout.CENTER)
self.script.stdout = EditorFileAdapter(self.outputEditor)
self.script.stderr = EditorFileAdapter(self.errorEditor)
self.output_file = None
# register to be notified when the extension is unloaded so if a output_file ref is in use it can be closed
callbacks.registerExtensionStateListener(self)
def clear_stderr(self, event):
self.errorEditor.text = ''
def clear_stdout(self, event):
self.outputEditor.text = ''
def save_file_output(self, event):
self.outputFileBrowseButton.enabled = True
if self.output_file: # already have an output_file
self.script.stdout = self.output_file
return
if not self.set_output_file():
# didn't choose a file then revert to using UI
self.outputUIRadioButton.selected = True
def view_ui_output(self, event):
self.outputFileBrowseButton.enabled = False
self.script.stdout = EditorFileAdapter(self.outputEditor)
def set_output_file(self, event=None):
file_chooser = JFileChooser()
choice = file_chooser.showSaveDialog(None)
if choice == JFileChooser.APPROVE_OPTION:
self.outputFileTextField.text = file_chooser.selectedFile.path
self.output_file = open(file_chooser.selectedFile.path, 'a')
self.script.stdout = self.output_file
return True
return False
def propertyChange(self, event):
if event.propertyName == Script.Properties.IS_COMPILED:
if event.newValue:
self.errorEditor.text = ''
elif event.propertyName == Script.Properties.COMPILATION_ERROR:
self.errorEditor.text = event.newValue
self.tabbedPane.selectedIndex = 1
def extensionUnloaded(self):
if self.output_file: # if we have a file ref then close it
print('Closing output file reference for script: \'{}\''.format(self.script.title))
self.output_file.close()
def _create_output_panel(self):
self.outputPanel = JPanel()
self.outputEditor = self.callbacks.createTextEditor()
self.outputEditor.editable = False
self.outputText = self.outputEditor.component
self.clearOutputButton = JButton('Clear', actionPerformed=self.clear_stdout)
self.outputButtonGroup = ButtonGroup()
self.outputFileRadioButton = JRadioButton('Save to File:', actionPerformed=self.save_file_output)
self.outputUIRadioButton = JRadioButton('Show in UI:', selected=True, actionPerformed=self.view_ui_output)
self.outputFileTextField = JTextField(50, enabled=False, disabledTextColor=Color.black)
self.outputFileBrowseButton = JButton('Browse...', enabled=False, actionPerformed=self.set_output_file)
self.outputButtonGroup.add(self.outputFileRadioButton)
self.outputButtonGroup.add(self.outputUIRadioButton)
outputLayout = GroupLayout(self.outputPanel, autoCreateGaps=True, autoCreateContainerGaps=True)
outputLayout.setHorizontalGroup(outputLayout.createParallelGroup()
.addGroup(
outputLayout.createSequentialGroup()
.addComponent(self.outputFileRadioButton)
.addComponent(self.outputFileTextField, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(self.outputFileBrowseButton)
)
.addComponent(self.outputUIRadioButton)
.addComponent(self.outputText)
.addComponent(self.clearOutputButton)
)
outputLayout.setVerticalGroup(outputLayout.createSequentialGroup()
.addGroup(
outputLayout.createParallelGroup()
.addComponent(self.outputFileRadioButton)
.addComponent(self.outputFileTextField, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(self.outputFileBrowseButton)
)
.addComponent(self.outputUIRadioButton)
.addComponent(self.outputText)
.addComponent(self.clearOutputButton)
)
self.outputPanel.layout = outputLayout
def _create_error_panel(self):
self.errorPanel = JPanel()
self.errorEditor = self.callbacks.createTextEditor()
self.errorEditor.editable = False
self.errorText = self.errorEditor.component
self.clearErrorButton = JButton('Clear', actionPerformed=self.clear_stderr)
errorLayout = GroupLayout(self.errorPanel, autoCreateGaps=True, autoCreateContainerGaps=True)
errorLayout.setHorizontalGroup(errorLayout.createParallelGroup()
.addComponent(self.errorText)
.addComponent(self.clearErrorButton)
)
errorLayout.setVerticalGroup(errorLayout.createSequentialGroup()
.addComponent(self.errorText)
.addComponent(self.clearErrorButton)
)
self.errorPanel.layout = errorLayout
class ScriptPanel(JPanel):
def __init__(self, script, callbacks):
self.script = script
self.layout = BorderLayout()
self.editingPanel = ScriptEditingPanel(callbacks, script)
self.outputPanel = ScriptOutputPanel(callbacks, script)
self.splitPane = JSplitPane(JSplitPane.VERTICAL_SPLIT, dividerSize=10)
self.splitPane.topComponent = self.editingPanel
self.splitPane.bottomComponent = self.outputPanel
self.add(self.splitPane, BorderLayout.CENTER)
class GUI(object):
def __init__(self, extender, callbacks, helpers, scripts):
self.panel = JPanel()
self.tabs = ScriptTabbedPane(extender, callbacks, helpers, scripts)
layout = BorderLayout()
self.panel.setLayout(layout)
self.panel.add(self.tabs)
def build(self):
return self.panel