/
combineReports.go
53 lines (41 loc) · 1.43 KB
/
combineReports.go
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
package nessusTools
import (
"os"
"strings"
"github.com/beevik/etree"
)
//CombineReports adds issues from a list of nessus reports to a copy of the first report.
//The new report has a randomly generated name and all the meta data associated with the first report
//that was passed to the function. The new report will be saved in the current directory.
func CombineReports(filePath []string) (string, error) {
//report for hosts to be added too
newReport := etree.NewDocument()
if err := newReport.ReadFromFile(filePath[0]); err != nil {
return "", err
}
newRoot := newReport.SelectElement("NessusClientData_v2")
for i := 0; i < len(filePath); i++ {
//current report hosts are being added from
report := etree.NewDocument()
if err := report.ReadFromFile(filePath[i]); err != nil {
return "", err
}
root := report.SelectElement("NessusClientData_v2")
var hosts []*etree.Element
for _, report := range root.SelectElements("Report") { //Select report branch to iterate over
for _, host := range report.SelectElements("ReportHost") { //Select Reporthost branch to iterate over
hosts = append(hosts, host)
}
}
for _, report := range newRoot.SelectElements("Report") {
for _, b := range hosts {
report.AddChild(b)
}
}
}
//create a random filename and write to disk.
newFilePath := TempFileName()
newReport.WriteToFile(newFilePath)
os.Remove(strings.Join(filePath, ""))
return newFilePath, nil
}