-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.py
102 lines (83 loc) · 2.91 KB
/
output.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
from __future__ import absolute_import, division, print_function, unicode_literals
import six
import json
import logging
import pprint
import csv
import tools
class OutputFormat(object):
def retrieve(self, results):
raise NotImplementedError
class JSONOutput(OutputFormat):
"""
:param indent: optional integer specifying indentation of output
:return: dict dumped to string in JSON format
"""
def retrieve(self, results, indent=4):
if indent:
return json.dumps(results, indent=indent)
return json.dumps(results)
class ConsolePrettyOutput(OutputFormat):
"""
:return: dict as string formatted with pprint
"""
def retrieve(self, results):
return pprint.pformat(results)
class HumanReadableOutput(OutputFormat):
"""
:return: string formatted to be easy to read
"""
def retrieve(self, results):
if not results:
return ''
output = ''
for url, data_dicts in six.iteritems(results):
output += 'Web application detection results for website {url}, found applications:\n'.format(url=url)
for data in data_dicts:
output += '\t{app} ({type})'.format(app=data['app'], type=data['type'])
if data['ver']:
output += ', version: {version}'.format(version=data['ver'])
output += '\n'
return output
class CSVOutput(OutputFormat):
"""
:param
:return: string formatted as CSV
"""
def __init__(self, filename=None):
super(CSVOutput, self).__init__()
self.filename = filename
if self.filename:
self.get_buffer = self.get_file
self.return_handler = self.return_file
else:
self.get_buffer = self.get_stringio
self.return_handler = self.return_stringio
def retrieve(self, results):
"""
:param results:
:param filename:
:return:
"""
buf = self.get_buffer()
fieldnames = ['URL', 'Finding', 'Version', 'Type']
writer = csv.DictWriter(buf, fieldnames)
# Can't use writeheader method, because it was introduced in python 2.7
writer.writerow(dict([(field, field) for field in fieldnames]))
for url, data_dicts in six.iteritems(results):
for data in data_dicts:
writer.writerow({'URL': url, 'Finding': data['app'], 'Version': data['ver'], 'Type': data['type']})
return self.return_handler(buf)
def get_stringio(self):
return six.StringIO()
def get_file(self):
try:
return open(self.filename, 'wb')
except IOError:
logging.error("Couldn't open %s while outputting results as csv", self.filename, tools.error_to_str(e))
raise
def return_stringio(self, buf):
buf.seek(0)
return buf.read()
def return_file(self, buf):
return buf