-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRequestAsPython-PowerShell.py
375 lines (303 loc) · 11.9 KB
/
RequestAsPython-PowerShell.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
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
__author__ = 'Jake Miller (@LaconicWolf)'
__date__ = '20190214'
__version__ = '0.01'
__description__ = """Burp Extension that changes HTTP requests
into Python and PowerShell formats.
"""
from burp import IBurpExtender, ITab, IContextMenuFactory
from javax import swing
from java.awt import BorderLayout
from java.util import ArrayList
import sys
import urllib
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
try:
from exceptions_fix import FixBurpExceptions
except ImportError:
pass
class BurpExtender(IBurpExtender, ITab, IContextMenuFactory):
def registerExtenderCallbacks(self, callbacks):
# Required for easier debugging:
# https://github.com/securityMB/burp-exceptions
sys.stdout = callbacks.getStdout()
# Keep a reference to our callbacks object
self.callbacks = callbacks
# Set our extension name
self.callbacks.setExtensionName("Request-as-Python-or-PowerShell")
# Create a context menu
callbacks.registerContextMenuFactory(self)
# Create the tab
self.tab = swing.JPanel(BorderLayout())
# Create a split panel where you can manually
# adjust the panel size.
splitPane = swing.JSplitPane(swing.JSplitPane.VERTICAL_SPLIT)
# Create the top panel containing the text area
textPanel = swing.JPanel()
# An empty text area 12 columns deep and
# full screen length.
self.textArea = swing.JTextArea('', 12, 100)
# Wrap the lines in the text area
self.textArea.setLineWrap(True)
# When the lines get bigger than the area
# implement a scroll pane and put the text area
# in the pane.
scroll = swing.JScrollPane(self.textArea)
# Set this scroll pane as the top of our split panel
splitPane.setTopComponent(scroll)
# Create the bottom panel for the transformed request.
# Each label and text field will go in horizontal
# boxes (rows) which will then go in a bigger box (box)
# The big box
box = swing.Box.createVerticalBox()
# Row containing the button that calls transformRequestsAs
row = swing.Box.createHorizontalBox()
row.add(swing.JButton('Transform',
actionPerformed=self.transformRequestsAs))
# Add the row to the big box
box.add(row)
# Row containing label and text area
row = swing.Box.createHorizontalBox()
self.pythonRequests = swing.JTextArea('', 2, 100)
self.pythonRequests.setLineWrap(True)
scroll = swing.JScrollPane(self.pythonRequests)
row.add(swing.JLabel("<html><pre> Python<br/> Requests </pre></html>"))
row.add(scroll)
box.add(row)
# Row containing label and text area
row = swing.Box.createHorizontalBox()
self.pythonUrlLib = swing.JTextArea('', 2, 100)
self.pythonUrlLib.setLineWrap(True)
scroll = swing.JScrollPane(self.pythonUrlLib)
row.add(swing.JLabel("<html><pre> Python<br/> Urllib2 </pre></html>"))
row.add(scroll)
box.add(row)
# Row containing label and text area
row = swing.Box.createHorizontalBox()
self.powershellIwr = swing.JTextArea('', 2, 100)
self.powershellIwr.setLineWrap(True)
scroll = swing.JScrollPane(self.powershellIwr)
row.add(swing.JLabel("<html><pre> PowerShell<br/> IWR </pre></html>"))
row.add(scroll)
box.add(row)
# Add this Panel to the bottom of the split panel
splitPane.setBottomComponent(box)
splitPane.setDividerLocation(150)
self.tab.add(splitPane)
# Add the custom tab to Burp's UI
callbacks.addSuiteTab(self)
return
# Implement ITab
def getTabCaption(self):
"""Return the text to be displayed on the tab"""
return "Request as Python/PowerShell"
def getUiComponent(self):
"""Passes the UI to burp"""
return self.tab
# Implement IContextMenuFactory
def createMenuItems(self, invocation):
'''Adds the extension to the context menu that
appears when you right-click an object.
'''
self.context = invocation
itemContext = invocation.getSelectedMessages()
# Only return a menu item if right clicking on a
# HTTP object
if itemContext > 0:
# Must return a Java list
menuList = ArrayList()
menuItem = swing.JMenuItem("Send to Request as Python/PowerShell",
actionPerformed=self.handleHttpTraffic)
menuList.add(menuItem)
return menuList
return None
def handleHttpTraffic(self, event):
"""Calls the function to write the HTTP object to
the extensions text area, and then begins to parse
the HTTP traffic for use in other functions.
"""
self.writeRequestToTextBox()
httpTraffic = self.context.getSelectedMessages()
for item in httpTraffic:
self.httpService = item.getHttpService()
def writeRequestToTextBox(self):
"""Writes HTTP context item to RequestTransformer
tab text box.
"""
httpTraffic = self.context.getSelectedMessages()
httpRequest = [item.request.tostring() for item in httpTraffic]
request = ''.join(httpRequest)
self.textArea.text = request
def transformRequestsAs(self, event):
"""Calls functions that transform the HTTP object
text area into a python or powershell request if
applicable.
"""
self.pythonRequests.text = self.parseAsPythonRequests()
self.pythonUrlLib.text = self.parseAsPythonUrlLib()
self.powershellIwr.text = self.parseAsPowershellIwr()
def parseAsPythonRequests(self):
"""Uses BaseHttpRequestHandler to parse an HTTP
request and print a command to make the same request
using the Python requests library.
"""
request = HTTPRequest(self.textArea.text)
url = self.httpService.toString()
url += request.path
headerValuesSplit = str(request.headers.values).split('\n')[1:]
headerNames = [header.split(':')[0] for header in headerValuesSplit]
headerValues = []
for value in headerValuesSplit:
if value.count(':') > 1:
headerValues.append(':'.join(value.split(':')[1:]).strip())
else:
headerValues.append(''.join(value.split(':')[1:]).strip())
url = ''.join(url)
header_dict = dict(zip(headerNames, headerValues))
headers = {k: v for k, v in header_dict.items() if v}
verb = request.command.lower()
data = ''
dataVerbs = ["PUT, POST, DELETE"]
if request.command in dataVerbs or int(request.headers.getheader('content-length')):
length = int(request.headers.getheader('content-length'))
body = request.rfile.read(length)
data = urlparse.parse_qsl(body)
if data:
command = \
"""import requests
s=requests.Session()
s.headers = {}
data = {}
resp = s.{}('{}', data)""".format(headers, data, verb, url)
else:
command =\
"""import requests
s = requests.Session()
s.headers = {}
resp = s.{}('{}'')""".format(headers, verb, url)
return command
def parseAsPythonUrlLib(self):
"""Uses BaseHttpRequestHandler to parse an HTTP
request and print a command to make the same request
using the Python urllib2 library.
"""
request = HTTPRequest(self.textArea.text)
verbs = ['GET', 'POST']
if request.command not in verbs:
return "Only GET and POST requests currently implemented."
url = self.httpService.toString()
url += request.path
headerValuesSplit = str(request.headers.values).split('\n')[1:]
headerNames = [header.split(':')[0] for header in headerValuesSplit]
headerValues = []
for value in headerValuesSplit:
if value.count(':') > 1:
headerValues.append(':'.join(value.split(':')[1:]).strip())
else:
headerValues.append(''.join(value.split(':')[1:]).strip())
url = ''.join(url)
header_dict = dict(zip(headerNames, headerValues))
headers = {k: v for k, v in header_dict.items() if v}
verb = request.command.lower()
data = ''
if request.command == 'POST':
length = int(request.headers.getheader('content-length'))
body = request.rfile.read(length)
data = urlparse.parse_qsl(body)
if data:
command = \
"""import urllib2
import urllib
data = urllib.urlencode({})
headers = {}
req = urllib2.Request('{}')
req.headers = headers
resp = urllib2.urlopen(req, data)""".format(data, headers, url)
else:
command =\
"""import urllib2
headers = {}
req = urllib2.Request('{}')
req.headers = headers
resp = urllib2.urlopen(req,)""".format(headers, url)
return command
def parseAsPowershellIwr(self):
"""Uses BaseHttpRequestHandler to parse an HTTP
request and print a command to make the same request
using the Invoke-WebRequest powershell Cmdlet.
"""
request = HTTPRequest(self.textArea.text)
url = self.httpService.toString()
url += request.path
headerValuesSplit = str(request.headers.values).split('\n')[1:]
headerNames = [header.split(':')[0] for header in headerValuesSplit]
headerValues = []
for value in headerValuesSplit:
if value.count(':') > 1:
headerValues.append(':'.join(value.split(':')[1:]).strip())
else:
headerValues.append(''.join(value.split(':')[1:]).strip())
url = ''.join(url)
header_dict = dict(zip(headerNames, headerValues))
headers = {k: v for k, v in header_dict.items() if v}
headers.pop('connection', None)
iwrHeaderHashTable = '@{'
for item in headers:
iwrHeaderHashTable += "'{}'='{}';".format(item, headers[item])
iwrHeaderHashTable = iwrHeaderHashTable[:-1] + '}'
verb = request.command.lower()
data = ''
dataVerbs = ["PUT, POST, DELETE"]
if request.command in dataVerbs or int(request.headers.getheader('content-length')):
length = int(request.headers.getheader('content-length'))
body = request.rfile.read(length)
data = urlparse.parse_qsl(body)
iwrData = '@{'
for item in data:
try:
iwrData += "'{}'='{}';".format(item[0], item[1])
except Exception as e:
print e
iwrData = iwrData[:-1] + '}'
if iwrData:
command = \
"""\
$Method = '{}'
$Uri = '{}'
$Headers = {}
$Body = {}
Invoke-WebRequest \
-Method $Method \
-Uri $Uri \
-Headers $Headers \
-Body $Body
""".format(request.command, url, iwrHeaderHashTable, iwrData)
else:
command =\
"""\
$Method = '{}'
$Uri = '{}'
$Headers = {}
Invoke-WebRequest \
-Method $Method \
-Uri $Uri \
-Headers $Headers \
""".format(request.command, url, iwrHeaderHashTable)
return command
class HTTPRequest(BaseHTTPRequestHandler):
'''Parses an HTTP request
#https://stackoverflow.com/questions/4685217/parse-raw-http-headers
'''
def __init__(self, request_text):
self.rfile = StringIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
try:
FixBurpExceptions()
except:
pass