Skip to content

Commit 1427b01

Browse files
OZONthierry-f-78
OZON
andauthored
Force output format (EnableSecurity#130)
In some case it is useful to force the format on stdout (--output=-). This patch add --format=csv|json|text. This patch doesn't modify anything in the current Wafw00f behavior, it ensure CLI compatibility. rules are: - if recognize file extension (csv, json), format is ingnored - if do not reconize extension and format is not set, default format is text - if do not reconize extension and format set, sue the format. Co-authored-by: Thierry Fournier <[email protected]>
1 parent 4aeeaf9 commit 1427b01

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

wafw00f/main.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ def main():
320320
parser.add_option('-t', '--test', dest='test', help='Test for one specific WAF')
321321
parser.add_option('-o', '--output', dest='output', help='Write output to csv, json or text file depending on file extension. For stdout, specify - as filename.',
322322
default=None)
323+
parser.add_option('-f', '--format', dest='format', help='Force output format to csv, json or text.',
324+
default=None)
323325
parser.add_option('-i', '--input-file', dest='input', help='Read targets from a file. Input format can be csv, json or text. For csv and json, a `url` column name or element is required.',
324326
default=None)
325327
parser.add_option('-l', '--list', dest='list', action='store_true',
@@ -456,7 +458,20 @@ def main():
456458
if options.output:
457459
if options.output == '-':
458460
enableStdOut()
459-
print(os.linesep.join(getTextResults(results)))
461+
if options.format == 'json':
462+
json.dump(results, sys.stdout, indent=2)
463+
elif options.format == 'csv':
464+
csvwriter = csv.writer(sys.stdout, delimiter=',', quotechar='"',
465+
quoting=csv.QUOTE_MINIMAL)
466+
count = 0
467+
for result in results:
468+
if count == 0:
469+
header = result.keys()
470+
csvwriter.writerow(header)
471+
count += 1
472+
csvwriter.writerow(result.values())
473+
else:
474+
print(os.linesep.join(getTextResults(results)))
460475
elif options.output.endswith('.json'):
461476
log.debug("Exporting data in json format to file: %s" % (options.output))
462477
with open(options.output, 'w') as outfile:
@@ -475,8 +490,23 @@ def main():
475490
csvwriter.writerow(result.values())
476491
else:
477492
log.debug("Exporting data in text format to file: %s" % (options.output))
478-
with open(options.output, 'w') as outfile:
479-
outfile.write(os.linesep.join(getTextResults(results)))
493+
if options.format == 'json':
494+
with open(options.output, 'w') as outfile:
495+
json.dump(results, outfile, indent=2)
496+
elif options.format == 'csv':
497+
with open(options.output, 'w') as outfile:
498+
csvwriter = csv.writer(outfile, delimiter=',', quotechar='"',
499+
quoting=csv.QUOTE_MINIMAL)
500+
count = 0
501+
for result in results:
502+
if count == 0:
503+
header = result.keys()
504+
csvwriter.writerow(header)
505+
count += 1
506+
csvwriter.writerow(result.values())
507+
else:
508+
with open(options.output, 'w') as outfile:
509+
outfile.write(os.linesep.join(getTextResults(results)))
480510

481511
if __name__ == '__main__':
482512
if sys.hexversion < 0x2060000:

0 commit comments

Comments
 (0)