Skip to content

Commit 843b3ab

Browse files
committed
Change a few pedantic/minor python things
Enforced open file as readable to file reads for sanity, added logo printing enforcement, shortened an if-block, added exit statuses, and wrapped main in an name == main conditional
1 parent 9eee648 commit 843b3ab

File tree

1 file changed

+86
-81
lines changed

1 file changed

+86
-81
lines changed

xssless.py

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import os
44
import sys
55
import json
6-
import base64
76
import binascii
87
import mimetypes
98
import xml.etree.ElementTree as et
9+
from base64 import b64decode
1010

1111
# Import burp export and return a list of decoded data
1212
def get_burp_list(filename):
1313
if not os.path.exists(filename):
1414
return []
1515

16-
with open(filename) as f:
16+
with open(filename, 'r') as f:
1717
filecontents = f.read()
1818

1919
tree = et.fromstring(filecontents)
@@ -23,19 +23,15 @@ def get_burp_list(filename):
2323
for dict_el in tree.iterfind('item'):
2424
tmpDict = {}
2525
for item in dict_el:
26-
if item.tag == "request":
27-
tmpDict['request'] = base64.b64decode(item.text)
28-
if item.tag == "response":
29-
tmpDict['response'] = base64.b64decode(item.text)
30-
if item.tag == "url":
31-
tmpDict['url'] = item.text
26+
if item.tag in ["request", "response", "url"]:
27+
tmpDict[item.tag] = b64decode(item.text)
3228
requestList.append(tmpDict)
3329

3430
return requestList
3531

3632
# Return hex encoded string output of binary input
3733
def payload_encode_file(input_file):
38-
with open(input_file) as f:
34+
with open(input_file, 'r') as f:
3935
filecontents = f.read()
4036
hue = binascii.hexlify(filecontents)
4137
filecontents = '\\x' + '\\x'.join(hue[i:i+2] for i in xrange(0, len(hue), 2)) # Stackoverflow, because pythonistic
@@ -404,78 +400,87 @@ def xss_gen(requestList, settingsDict):
404400
-n Turn off payload optimization
405401
406402
"""
407-
if len(sys.argv) < 2:
408-
print logo
409-
print helpmenu
410-
else:
411-
# settingsDict will contain code generation settings, such as waiting for each request to complete, etc.
412-
settingsDict = {}
413-
settingsDict['opt'] = True
414-
415-
showlogo = True
416-
417-
for option in sys.argv[1:]:
418-
if option == "-h":
419-
print logo
420-
print helpmenu
421-
sys.exit()
422-
if option == "-s":
423-
showlogo = False
424-
if "-m=" in option:
425-
metafile = option.replace("-m=", "")
426-
if os.path.isfile(metafile):
427-
tmpList = open(metafile).readlines()
428-
for key,value in enumerate(tmpList):
429-
tmpList[key] = value.replace("\n", "")
430-
if len(tmpList):
431-
settingsDict['metaList'] = tmpList
432-
else:
433-
print "Error, meta list not found!"
434-
if "-p=" in option:
435-
parsefile = option.replace("-p=", "")
436-
if os.path.isfile(parsefile):
437-
tmpList = open(parsefile).readlines()
438-
for key,value in enumerate(tmpList):
439-
tmpList[key] = value.replace("\n", "")
440-
if len(tmpList):
441-
settingsDict['parseList'] = tmpList
442-
else:
443-
print "Error, parse list not found!"
444-
if "-n" in option:
445-
settingsDict['opt'] = False
446-
if "-f=" in option:
447-
fileuploadlist = option.replace("-f=", "")
448-
if os.path.isfile(fileuploadlist):
449-
tmpDict = {}
450-
fileuploadlinesList = open(fileuploadlist).readlines()
451-
for key, value in enumerate(fileuploadlinesList):
452-
rowparts = value.replace("\n", "").split(",", 1)
453-
if len(rowparts) == 2:
454-
if os.path.isfile(rowparts[1]):
455-
tmpDict[rowparts[0]] = rowparts[1]
456-
else:
457-
print "File '" + rowparts[1] + "' not found!"
458-
sys.exit()
459-
else:
460-
print "Error while parsing file " + fileuploadlist + " on line #" + str(key)
461-
print " ->'" + value.replace("\n", "") + "'"
462-
sys.exit()
463-
if tmpDict:
464-
settingsDict['fileDict'] = tmpDict
465-
else:
466-
print "Input filelist not found!"
467-
sys.exit()
468-
469-
if os.path.exists(sys.argv[-1]):
470-
inputfile = sys.argv[-1]
403+
if __name__ == '__main__':
404+
if len(sys.argv) < 2:
405+
print logo
406+
print helpmenu
471407
else:
472-
inputfile = ""
408+
# settingsDict will contain code generation settings, such as waiting for each request to complete, etc.
409+
settingsDict = {}
410+
settingsDict['opt'] = True
473411

474-
if showlogo:
475-
print logo
412+
showlogo = False if "-s" in sys.argv[1:] else True # quick check for this first incase they hate logos
476413

477-
if inputfile:
478-
requestList = get_burp_list(inputfile)
479-
print xss_gen(requestList, settingsDict)
480-
else:
481-
print "Error while processing Burp export, please ensure the file exists!"
414+
for option in sys.argv[1:]:
415+
if option == "-s":
416+
pass
417+
if option == "-h":
418+
if showlogo:
419+
print logo
420+
print helpmenu
421+
sys.exit(0)
422+
if "-m=" in option:
423+
metafile = option.replace("-m=", "")
424+
if os.path.isfile(metafile):
425+
tmpList = open(metafile).readlines()
426+
for key,value in enumerate(tmpList):
427+
tmpList[key] = value.replace("\n", "")
428+
if len(tmpList):
429+
settingsDict['metaList'] = tmpList
430+
else:
431+
print "Error, meta list not found!"
432+
if "-p=" in option:
433+
parsefile = option.replace("-p=", "")
434+
if os.path.isfile(parsefile):
435+
tmpList = open(parsefile).readlines()
436+
for key,value in enumerate(tmpList):
437+
tmpList[key] = value.replace("\n", "")
438+
if len(tmpList):
439+
settingsDict['parseList'] = tmpList
440+
else:
441+
print "Error, parse list not found!"
442+
if "-n" in option:
443+
settingsDict['opt'] = False
444+
if "-f=" in option:
445+
fileuploadlist = option.replace("-f=", "")
446+
if os.path.isfile(fileuploadlist):
447+
tmpDict = {}
448+
fileuploadlinesList = open(fileuploadlist).readlines()
449+
for key, value in enumerate(fileuploadlinesList):
450+
rowparts = value.replace("\n", "").split(",", 1)
451+
if len(rowparts) == 2:
452+
if os.path.isfile(rowparts[1]):
453+
tmpDict[rowparts[0]] = rowparts[1]
454+
else:
455+
print "File '" + rowparts[1] + "' not found!"
456+
sys.exit(1)
457+
else:
458+
print "Error while parsing file " + fileuploadlist + " on line #" + str(key)
459+
print " ->'" + value.replace("\n", "") + "'"
460+
sys.exit(1)
461+
if tmpDict:
462+
settingsDict['fileDict'] = tmpDict
463+
else:
464+
print "Input filelist not found!"
465+
sys.exit(1)
466+
else:
467+
print "Option " + option + " not recognized."
468+
if showlogo:
469+
print logo
470+
print helpmenu
471+
sys.exit(1)
472+
473+
if os.path.exists(sys.argv[-1]):
474+
inputfile = sys.argv[-1]
475+
else:
476+
inputfile = ""
477+
478+
if showlogo:
479+
print logo
480+
481+
if inputfile:
482+
requestList = get_burp_list(inputfile)
483+
print xss_gen(requestList, settingsDict)
484+
else:
485+
print "Error while processing Burp export, please ensure the file exists!"
486+
sys.exit(1)

0 commit comments

Comments
 (0)