Skip to content

Commit 6dc8d2e

Browse files
committed
Readability improvements
1 parent 59732fb commit 6dc8d2e

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

xssless.py

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python
22

3-
import sys
43
from bs4 import BeautifulSoup
5-
import base64
6-
import json
4+
75
import os
6+
import sys
7+
import json
8+
import base64
9+
import binascii
810
import mimetypes
911

1012
# Import burp export and return a list of decoded data
@@ -13,33 +15,32 @@ def get_burp_list(filename):
1315
return []
1416

1517
with open(filename) as f:
16-
filecontents = f.read()
18+
filecontents = f.read()
1719

1820
archive = BeautifulSoup(filecontents, "xml")
1921

2022
requestList = []
21-
item = archive.find_all('item')
2223

2324
for item in archive.find_all('item'):
2425
tmpDict = {}
2526
tmpDict['request'] = base64.b64decode(item.request.string)
2627
tmpDict['response'] = base64.b64decode(item.response.string)
2728
tmpDict['url'] = item.url.string
2829
requestList.append(tmpDict)
29-
del tmpDict
3030

3131
return requestList
3232

3333
# Return hex encoded string output of binary input
3434
def payload_encode_file(input_file):
35-
filecontents = open(input_file).read()
36-
hue = filecontents.encode("hex")
35+
with open(input_file) as f:
36+
filecontents = f.read()
37+
hue = binascii.hexlify(filecontents)
3738
filecontents = '\\x' + '\\x'.join(hue[i:i+2] for i in xrange(0, len(hue), 2)) # Stackoverflow, because pythonistic
3839
return filecontents
3940

4041
# Return hex encoded string output of binary input
4142
def payload_encode_input(filecontents):
42-
hue = filecontents.encode("hex")
43+
hue = binascii.hexlify(filecontents)
4344
filecontents = '\\x' + '\\x'.join(hue[i:i+2] for i in xrange(0, len(hue), 2)) # Stackoverflow, because pythonistic
4445
return filecontents
4546

@@ -71,18 +72,16 @@ def parse_request(input_var, url):
7172
headerList = []
7273
host = ""
7374
for line in header_lines:
74-
tmpList = line.split(": ")
75+
key, value = line.split(": ", 1)
7576
headerDict = {}
76-
headerDict['Key'] = tmpList[0]
77-
headerDict['Value'] = tmpList[1]
77+
headerDict['Key'] = key
78+
headerDict['Value'] = value
7879

7980
# Grab important values
8081
if headerDict['Key'].lower() == "host":
8182
host = headerDict['Value']
8283

8384
headerList.append(headerDict)
84-
del headerDict
85-
del tmpList
8685

8786
postisupload = False
8887
fileboundary = ""
@@ -122,9 +121,6 @@ def parse_request(input_var, url):
122121

123122
tmp['body'] = sectionBody
124123
bodyList.append(tmp)
125-
del tmp
126-
del sectionHeader
127-
del sectionBody
128124

129125
else:
130126
# Create a list of body values (check for JSON, etc)
@@ -133,14 +129,11 @@ def parse_request(input_var, url):
133129
body_var_List = body_data.split("&")
134130
body_var_List = filter(None, body_var_List)
135131
for item in body_var_List:
136-
tmpList = item.split("=")
132+
key, value = item.split("=", 1)
137133
bodyDict = {}
138-
bodyDict['Key'] = tmpList[0]
139-
bodyDict['Value'] = tmpList[1]
134+
bodyDict['Key'] = key
135+
bodyDict['Value'] = value
140136
bodyList.append(bodyDict)
141-
del tmpList
142-
del bodyDict
143-
144137

145138
# Returned dict, chocked full of useful information formatted nicely for your convienience!
146139
returnDict = {}
@@ -165,9 +158,7 @@ def parse_response(input_var, url):
165158
flags = []
166159

167160
# Split request into headers/body and parse header into list
168-
request_parts = input_var.split("\r\n\r\n")
169-
header_data = request_parts[0]
170-
body_data = request_parts[1]
161+
header_data, body_data = input_var.split("\r\n\r\n", 1)
171162
header_lines = header_data.split("\r\n")
172163
header_lines = filter(None, header_lines) # Filter any blank lines
173164

@@ -181,17 +172,15 @@ def parse_response(input_var, url):
181172
headerList = []
182173
content_type = ""
183174
for line in header_lines:
184-
tmpList = line.split(": ")
175+
key, value = line.split(": ", 1)
185176
headerDict = {}
186-
headerDict['Key'] = tmpList[0]
187-
headerDict['Value'] = tmpList[1]
177+
headerDict['Key'] = key
178+
headerDict['Value'] = value
188179

189180
if headerDict['Key'].lower() == "Content-Type".lower():
190181
content_type = headerDict['Value']
191182

192183
headerList.append(headerDict)
193-
del headerDict
194-
del tmpList
195184

196185
# Returned dict, chocked full of useful information formatted nicely for your convienience!
197186
returnDict = {}
@@ -261,9 +250,7 @@ def xss_gen(requestList, settingsDict):
261250
# Each request is done as a function that one requestion completion, calls the next function.
262251
# The result is an unclobered browser and no race conditions! (Because cookies may need to be set, etc)
263252

264-
# Counter for function numbers
265-
i = 0
266-
for conv in requestList:
253+
for i, conv in enumerate(requestList):
267254
requestDict = parse_request(conv['request'], conv['url'])
268255
responseDict = parse_response(conv['response'], conv['url']) # Currently unused, for future heuristics
269256

@@ -290,9 +277,6 @@ def xss_gen(requestList, settingsDict):
290277
multipart += 'Content-Disposition: form-data; name="' + item['name'] + '"; filename="' + item['filename'] + '"\\r\\n'
291278
multipart += 'Content-Type: ' + content_type + '\\r\\n\\r\\n'
292279
multipart += filecontents + '\\r\\n'
293-
294-
del filecontents
295-
del content_type
296280
else:
297281
multipart += 'Content-Disposition: form-data; name="' + item['name'] + '"; filename="' + item['filename'] + '"\\r\\n'
298282
multipart += 'Content-Type: ' + item['contenttype'] + '\\r\\n\\r\\n'
@@ -339,7 +323,6 @@ def xss_gen(requestList, settingsDict):
339323

340324
payload += " }\n"
341325
payload += "\n"
342-
i += 1
343326

344327
payload += "</script>"
345328
return payload
@@ -389,7 +372,6 @@ def xss_gen(requestList, settingsDict):
389372
tmpList[key] = value.replace("\n", "")
390373
if len(tmpList):
391374
settingsDict['parseList'] = tmpList
392-
del tmpList
393375
else:
394376
print "Error, parse list not found!"
395377
if "-f=" in option:
@@ -409,12 +391,8 @@ def xss_gen(requestList, settingsDict):
409391
print "Error while parsing file " + fileuploadlist + " on line #" + str(key)
410392
print " ->'" + value.replace("\n", "") + "'"
411393
sys.exit()
412-
del rowparts
413394
if tmpDict:
414395
settingsDict['fileDict'] = tmpDict
415-
416-
del tmpDict
417-
del fileuploadlinesList
418396
else:
419397
print "Input filelist not found!"
420398
sys.exit()

0 commit comments

Comments
 (0)