-
Notifications
You must be signed in to change notification settings - Fork 1
/
nessusedit.py
executable file
·249 lines (198 loc) · 8.74 KB
/
nessusedit.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
#!/usr/bin/env python
from argparse import ArgumentParser, RawTextHelpFormatter
from pprint import pprint
from prettytable import PrettyTable, ALL
from textwrap import fill
import lxml.etree as le
import readchar
import sys
class NessusFile(object):
def __init__(self, nessusfile):
self.doc = self.loadnessusfile(nessusfile)
self.filter = '*'
def loadnessusfile(self, nf):
with open(nf) as f:
doc = le.parse(nf)
return doc
def vulns(self):
vulns = {}
for elem in self.getvulns():
if not elem.attrib['pluginName'] in vulns:
vulns[elem.attrib['pluginName']] = {"severity":elem.attrib['severity'],
"count":1,
"port":elem.attrib['port'],
"hosts":[elem.getparent().attrib['name']]}
else:
vulns[elem.attrib['pluginName']]['count'] += 1
if elem.getparent().attrib['name'] not in vulns[elem.attrib['pluginName']]['hosts']:
vulns[elem.attrib['pluginName']]['hosts'].append(elem.getparent().attrib['name'])
return sorted(vulns.items(), key=lambda(k,v): (v['severity'], v['count']), reverse=True)
def printsummary(self, filter=None):
t = PrettyTable(['pluginName','port','severity','count','hosts'], hrules=ALL)
if self.vulns():
for k,v in self.vulns():
t.add_row([k, v['port'], v['severity'], v['count'], '\n'.join(v['hosts'])])
print t
def printvuln(self, elem):
if self.doc.getpath(elem) == '/ReportItem':
return False
t = PrettyTable(['host','port','pluginName','severity','plugin_output', 'synopsis', 'solution'])
t.add_row([elem.getparent().attrib['name'],
elem.attrib['port'],
fill(elem.attrib['pluginName'], width=60),
elem.attrib['severity'],
fill(elem.findtext('plugin_output') or "", width=60),
fill(elem.findtext('synopsis') or "", width=60),
fill(elem.findtext('solution') or "", width=60)])
print t
return True
def getvulns(self, filter=None):
f = filter or self.filter
return self.doc.xpath("//ReportItem[%s]" % f, namespaces={"re": "http://exslt.org/regular-expressions"})
def setfilter(self, filter, boolop=None, mode=None):
self.filter = self.createfilter(filter, boolop, mode)
def createfilter(self, filter, boolop='and', mode='include'):
filterstr = ''
boolstr = ' %s ' % boolop
if not filter:
filterstr = '*'
else:
filterlist = []
for f in filter:
k,v = f.items()[0]
negate = False
if k.endswith('!'):
k = k.replace('!','')
negate = True
if k == 'host':
node = '../@name'
else:
node = '@%s' % k
if '*' in v:
v = v.replace('.','\.')
v = v.replace('*','.*')
fitem = 're:match(%s,"^%s$")' % (node,v)
else:
fitem = '%s="%s"' % (node,v)
if negate:
fitem = 'not(%s)' % fitem
filterlist.append(fitem)
filterstr = boolstr.join(filterlist)
if mode == 'include':
return filterstr
elif mode == 'exclude':
return 'not(%s)' % filterstr
def filtervulns(self, filter=None):
count = 0
for count,elem in enumerate(self.getvulns(filter), 1):
elem.getparent().remove(elem)
return count
def stepthrough(self, filter=None):
abort = False
vulncount = len(self.getvulns())
ok_findings = []
for count,elem in enumerate(self.getvulns(), 1):
if abort:
break
if elem.attrib['pluginName'] in ok_findings:
continue
if not self.printvuln(elem):
continue
done = False
while not done:
done = True
sys.stdout.write("%d/%d (a)ccept,(A)ccept all,(r)emove,(R)emove all,(h)elp,(s)ummary,(q)uit => " % (count, vulncount))
cmd = readchar.readkey()
print cmd
if cmd == 'a' or cmd == '\r':
done = True
elif cmd == 'A':
ok_findings.append(elem.attrib['pluginName'])
print "Accepting all findings with pluginName '%s'." % elem.attrib['pluginName']
elif cmd == 'r':
elem.getparent().remove(elem)
print "Finding removed"
elif cmd == 'R':
filter = {}
properties = raw_input("Remove all based on which properties [pluginName]? ").split(",")
try:
if properties == ['']:
properties = ['pluginName']
elif 'host' in properties:
filter['host'] = elem.getparent().attrib['name']
properties.remove('host')
for property in properties:
filter[property] = elem.attrib[property]
print "Removed %d matching findings" % self.filtervulns(self.createfilter([filter]))
except KeyError:
print "Invalid property name"
done = False
elif cmd == 'h':
print "\nSome help text\n"
done = False
elif cmd == 's':
self.printsummary()
done = False
elif cmd == 'q':
abort = True
else:
print "Invalid command"
done = False
def tostring(self):
return le.tostring(self.doc)
def save(self, file):
if file == '-':
f = sys.stdout
else:
f = open(file,'w')
f.write(self.tostring() + '\n')
f.close()
def main():
argparser = ArgumentParser(description='A script for viewing and filtering Nessus report files.', \
epilog='Filters are input as comma-separated key-value pairs, so for instance ' \
'to keep all\nfindings that have severity 4 or 5, or come from the host "host1",' \
'do the following:\n\n' \
'nessusedit.py -k -f severity=4,severity=5,host=host1 -o newfile.nessus oldfile.nessus\n\n' \
'You can also negate a filter using "!=" (for instance severity!=0)',
formatter_class=RawTextHelpFormatter)
argparser.add_argument('-b','--boolop', help="Boolean operator to apply between filter terms. Default is 'or'", default='or')
argparser.add_argument('-f','--filter', help="Filters to apply")
argparser.add_argument('-k','--keep', help='Keep (only) findings matched by filter', action='store_true')
argparser.add_argument('-r','--remove', help='Remove findings matched by filter', action='store_true')
argparser.add_argument('-o','--output', help="File to write output to")
argparser.add_argument('-n','--no-output', help="Don't write output to file", action='store_true')
argparser.add_argument('-s','--summary', help="Print a summary of findings", action='store_true')
argparser.add_argument('nessusfile', help='Nessus report file to read')
args = argparser.parse_args()
if not args.nessusfile:
optparser.print_usage()
sys.exit(0)
n = NessusFile(args.nessusfile)
if args.filter:
if args.keep:
mode = 'exclude'
else:
mode = 'include'
n.setfilter(filter=[dict([arg.split('=')]) for arg in args.filter.split(',')],boolop=args.boolop,mode=mode)
else:
filter = None
if args.summary:
n.printsummary()
sys.exit(0)
elif args.remove or args.keep:
n.filtervulns()
else:
n.printsummary()
print "\nPress any key to continue...\n"
readchar.readkey()
print "\nStarting stepthrough.\n"
n.stepthrough()
if not args.no_output:
if args.output:
n.save(args.output)
print "Output written to '%s'." % args.output
else:
f = raw_input("\nFile to write output to: ")
n.save(f)
if __name__ == "__main__":
main()