I know there are a lot of questions on this topic but the answers aren't particularly well explained so it's difficult to adapt to my use case. The one here seems very promising but the syntax is rather complex and I'm having difficulty understanding and adapting it.
I have a need to convert the raw CSV output from Nessus to a standard format which essentially dumps a lot of the columns keeping only the Severity, IP addresses, and output of each finding. I've put a script together which does just that but if a finding is on multiple hosts/ports there is a different row for each host/port.
What I need is to merge rows based on the vulnerability name but only keeping the IP address data.
Example input - shortened for ease
High,10.10.10.10,MS12-345(this is the name),Hackers can do bad things
High,10.10.10.11,MS12-345(this is the name),Hackers can do bad things
Example output
High,10.10.10.10 10.10.10.11,MS12-345(this is the name),Hackers can do bad things
Below is my script so far. I would appreciate if you make your answer easily adaptable (read: idiot-proof) for future readers and I'm sure they would too.
Bonus:
Sometimes the output field is different for findings with the same name, sometimes it is the same. If you've got some time on your hands, why not help a man check for this and append in the same way as the IP addresses if there's a difference in the output?
import sys
import csv
def manipulate(inFile):
with open(inFile, 'rb') as csvFile:
fileReader = csv.reader(csvFile, dialect='excel')
# Check for multiple instances of findings and merge the rows
# This happens when the finding is on multiple hosts/ports
//YOUR CODE WILL GO HERE (Probably...)
# Place findings into lists: crits, highs, meds, lows for sorting later
crits = []
highs = []
meds = []
lows = []
for row in fileReader:
if row[3] == "Critical":
crits.append(row)
elif row[3] == "High":
highs.append(row)
elif row[3] == "Medium":
meds.append(row)
elif row[3] == "Low":
lows.append(row)
# Open an output file for writing
with open('output.csv', 'wb') as outFile:
fileWriter = csv.writer(outFile)
# Add in findings from lists in order of severity. Only relevant columns included
for c in crits:
fileWriter.writerow( (c[3], c[4], c[7], c[12]) )
for h in highs:
fileWriter.writerow( (h[3], h[4], h[7], h[12]) )
for m in meds:
fileWriter.writerow( (m[3], m[4], m[7], m[12]) )
for l in lows:
fileWriter.writerow( (l[3], l[4], l[7], l[12]) )
# Input validation
if len(sys.argv) != 2:
print 'You must provide a csv file to process'
raw_input('Example: python nesscsv.py foo.csv')
else:
print "Working..."
# Store filename for use in manipulate function
inFile = str(sys.argv[1])
# Call manipulate function passing csv
manipulate(inFile)
print "Done!"
raw_input("Output in output.csv. Hit return to finish.")