-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNessusCombiner.py
More file actions
169 lines (146 loc) · 5 KB
/
NessusCombiner.py
File metadata and controls
169 lines (146 loc) · 5 KB
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
#!/usr/bin/python
# Evan Pena
# evan.pena@mandiant.com
# Mandiant 2012
#This method was found here: www.rykerexum.com/2012/03/12/how-to-merge-multiple-nessus-files-into-a-single-report/
#I am just automating it so it's easier/faster for people to do.
import getopt
import sys, os, fileinput
import shutil
def description():
print '''\nNessus Combiner Utility
Will combine multiple .nessus files into one .nessus file.
Author: Evan Pena
Overview:
This script will combine multiple .nessus files into one .nessus file.
This will make it easier for consultants to view all the results in
one report within Nessus.
It will also allow consultants to generate finding bloks from one .nessus
file instead of multiple.'''
def usage():
scriptname = sys.argv[0]
print '''
Usage:
python %s args
Options:
-h, --help Will generate this help menu.
Examples:
python %s [filename.nessus] [filename2.nessus] filename3.nessus]
Support:
evan.pena@mandiant.com
''' % (scriptname, scriptname)
def reportNameReplacer():
try:
TAG = '<Report name='
input = open('file1_temp.nessus')
output = open('file1.nessus','w')
for line in input.xreadlines( ):
if line.startswith(TAG):
output.write(line.replace(line, '<Report name="Concatenated Nessus Reports" xmlns:cm="http://www.nessus.org/cm">\n'))
else:
output.write(line)
output.close( )
input.close( )
os.remove('file1_temp.nessus')
except Exception, e:
print e
def combiner( filenames):
try:
TAG = '<Report name='
newFiles = []
#Modifies the first file to remove the "end of report tags"
firstFile = str(filenames[:1])
firstFile = firstFile.replace("[", "")
firstFile = firstFile.replace("]", "")
firstFile = firstFile.replace("'", "")
readFile = open(firstFile)
lines = readFile.readlines()
readFile.close()
w = open('file1_temp.nessus','w')
w.writelines([item for item in lines[:-2]])
w.close()
reportNameReplacer()
newFiles.append('file1.nessus')
#This is for all the files in between. It will remove the line that starts with "<Report name=" and all the lines before it.
#It will also remove the last two lines of the file
#There is an error here when you add more than 3 files. Somethign to do with the for loops. Logic issue
midFiles = filenames[1:-1]
for item in midFiles:
fileOutput = item.split('\\')
fileOutput = fileOutput[len(fileOutput)-1]
tag_found = False
with open(item) as in_file:
with open(str(fileOutput)+'NEW.nessus', 'w') as out_file:
for line in in_file:
if not tag_found:
if line.startswith(TAG):
tag_found = True
else:
out_file.write(line)
readFile = open(str(fileOutput)+'NEW.nessus')
lines = readFile.readlines()
readFile.close()
w = open(str(fileOutput)+'NEW.nessus','w')
w.writelines([item for item in lines[:-2]])
w.close()
newFiles.append(str(fileOutput)+'NEW.nessus')
#Can also do this:
#regex2 = '^<Report name=.*xmlns:cm=.*>$' # Variable @name & @xmlns:cm
#with open(firstFile, "r") as fileInput:
#listLines = fileInput.readlines()
# listIndex = [i for i, item in enumerate(listLines) if re.search(regex2, item)]
#with open("out_" + firstFile, "w") as fileOutput:
#fileOutput.write("\n".join(lines[listIndex:]))
#For the last file we only remove the line that starts with "<Report name=" and all the lines before it.
#It will need to keep the end script tags
tag_found1 = False
lastFile = str(filenames[-1:])
lastFile = lastFile.replace("[", "")
lastFile = lastFile.replace("]", "")
lastFile = lastFile.replace("'", "")
with open(lastFile) as in_file:
with open('lastfile.nessus', 'w') as out_file:
for line in in_file:
if not tag_found1:
if line.startswith(TAG):
tag_found1 = True
else:
out_file.write(line)
newFiles.append('lastfile.nessus')
#Now we must concatenate them all together.
with open('concat_file.nessus', "w") as concat_file:
for items in newFiles:
shutil.copyfileobj(open(items, "r"), concat_file)
#This will remove the files at the end.
os.remove(items)
print "\nSuccess! You have combined all your nessus files into 1 file."
print "Look for the concat_file.nessus in the current working directory."
except Exception, e:
print e
def main():
try:
list1 = []
try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError, err:
print str(err) #will print something like "option -a not recognized"
print "Try + " + scriptname + " --help' for more information."
sys.exit(2)
# Parse command line options
for o, a in opts:
print o
if o in ('-h', '--help'):
description()
usage()
sys.exit()
else:
assert False, "invalid arguments"
filenames = sys.argv[1:]
for items in filenames:
if not items.endswith('.nessus'):
assert False, "\nThe filename must be a .nessus file."
combiner(filenames)
except Exception, e:
print e
if __name__ == '__main__':
main()