-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_metrics.go
133 lines (106 loc) · 2.09 KB
/
report_metrics.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
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
// SPDX-FileCopyrightText: (c) Mauve Mailorder Software GmbH & Co. KG, 2020. Licensed under [MIT](LICENSE) license
//
// SPDX-License-Identifier: MIT
package main
import (
"encoding/xml"
"fmt"
"github.com/pkg/errors"
)
type vuln struct {
cve string
level string
isExloit bool
}
type service struct {
port string
protocol string
name string
}
type host struct {
name string
addr string
}
type reportMetrics struct {
hosts uint32
services map[service][]host
vulns map[vuln][]host
}
func newReportMetrics() *reportMetrics {
return &reportMetrics{
services: make(map[service][]host),
vulns: make(map[vuln][]host),
}
}
func (m *reportMetrics) parseReportXML(b []byte) error {
r := &NmapRun{}
err := xml.Unmarshal(b, r)
if err != nil {
return err
}
if r == nil {
return errors.Errorf("no NMAP run was found")
}
m.processHosts(r)
return nil
}
func (m *reportMetrics) processHosts(run *NmapRun) {
for _, h := range run.Hosts {
if h.Status.State != "up" {
continue
}
m.hosts++
fmt.Println(h)
ho := host{
addr: h.Address.Addr,
}
if len(h.HostNames.Names) > 0 {
ho.name = h.HostNames.Names[0].Name
}
m.processPorts(h, ho)
}
}
func (m *reportMetrics) processPorts(h HostResult, ho host) {
for _, po := range h.Ports.Ports {
if po.State.State != "open" {
continue
}
svc := service{
port: po.Number,
protocol: po.Protocol,
}
if po.Service.Method == "probed" {
svc.name = po.Service.Name
}
m.services[svc] = append(m.services[svc], ho)
m.processVulns(po, ho)
}
}
func (m *reportMetrics) processVulns(p PortResult, ho host) {
if p.Script.ID != "vulners" {
return
}
for _, t := range p.Script.Table.Tables {
v := m.vulnFromTable(t)
m.vulns[v] = append(m.vulns[v], ho)
}
}
func (m *reportMetrics) vulnFromTable(t Table) vuln {
vuln := vuln{}
for _, elem := range t.Elements {
switch elem.Key {
case "id":
vuln.cve = elem.Text
break
case "cvss":
vuln.level = elem.Text
break
case "is_exploit":
if elem.Text == "true" {
vuln.isExloit = true
}
break
}
}
return vuln
}