forked from sandia-minimega/discovery
/
host.go
234 lines (187 loc) · 4.98 KB
/
host.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
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
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package main
import (
"fmt"
"io"
"net"
"strings"
log "pkg/minilog"
)
type Host struct {
// We may detect multiple operating systems for a single host so we store
// the OS and it's weight from every event.
OS map[OS]uint
Nameservers map[string]bool
Routers map[string]bool
Hostnames map[Hostname]bool
Services map[Service]bool
AdvertisedServices map[string]bool
IPs map[string]net.IP
MACs map[string]net.HardwareAddr
// If we haven't seen any DHCP traffic for the host, we track it by its IP
// address as an `external` host. If, in the future, we do see DHCP traffic
// for this IP, we will forget about the `external` host and relearn the
// host attributes.
External bool
// Set to true if the Host is a router
Router bool
}
// HostOut represents Host for serialization
type HostOut struct {
OS map[string]float64 `json:"os,omitempty"`
Nameservers []string `json:"nameservers,omitempty"`
Routers []string `json:"routers,omitempty"`
Hostnames []Hostname `json:"hostnames,omitempty"`
Services []Service `json:"services,omitempty"`
AdvertisedServices []string `json:"advertised_services,omitempty"`
IPs []string `json:"ips,omitempty"`
MACs []string `json:"macs,omitempty"`
External bool `json:"external"`
Router bool `json:"router"`
}
func NewHost() *Host {
return &Host{
OS: map[OS]uint{},
Nameservers: map[string]bool{},
Routers: map[string]bool{},
Hostnames: map[Hostname]bool{},
Services: map[Service]bool{},
AdvertisedServices: map[string]bool{},
IPs: map[string]net.IP{},
MACs: map[string]net.HardwareAddr{},
}
}
// Merge two hosts. You should probably forget about other afterwards...
func (h *Host) Merge(other *Host) {
log.Debug("Merging %v and %v", h.IPs, other.IPs)
for k, v := range other.IPs {
h.IPs[k] = v
}
for k, v := range other.MACs {
h.MACs[k] = v
}
for k, v := range other.OS {
h.OS[k] += v
}
for k := range other.Services {
h.Services[k] = true
}
for k := range other.AdvertisedServices {
h.AdvertisedServices[k] = true
}
for k := range other.Hostnames {
h.Hostnames[k] = true
}
for k := range other.Nameservers {
h.Nameservers[k] = true
}
for k := range other.Routers {
h.Routers[k] = true
}
}
func (h *Host) Write(out io.Writer) {
fmt.Fprintf(out, "external=%t\n", h.External)
fmt.Fprintf(out, "router=%t\n", h.Router)
for ip := range h.IPs {
fmt.Fprintf(out, "ip=%v\n", ip)
}
for mac := range h.MACs {
fmt.Fprintf(out, "mac=%v\n", mac)
}
if v := fmtOS(h.OS); v != "" {
fmt.Fprintf(out, "os=%v\n", v)
}
if v := fmtServices(h.Services); v != "" {
fmt.Fprintf(out, "services=%v\n", v)
}
if v := fmtStrings(h.AdvertisedServices); v != "" {
fmt.Fprintf(out, "advertised-services=%v\n", v)
}
if v := fmtHostnames(h.Hostnames); v != "" {
fmt.Fprintf(out, "hostnames=%v\n", v)
}
if v := fmtStrings(h.Nameservers); v != "" {
fmt.Fprintf(out, "nameservers=%v\n", v)
}
if v := fmtStrings(h.Routers); v != "" {
fmt.Fprintf(out, "routers=%v\n", v)
}
fmt.Fprintln(out, "\n")
}
func (h *Host) Out() *HostOut {
out := &HostOut{
OS: calcOS(h.OS),
External: h.External,
Router: h.Router,
}
for v := range h.Nameservers {
out.Nameservers = append(out.Nameservers, v)
}
for v := range h.Routers {
out.Routers = append(out.Routers, v)
}
for v := range h.Hostnames {
out.Hostnames = append(out.Hostnames, v)
}
for v := range h.Services {
out.Services = append(out.Services, v)
}
for v := range h.AdvertisedServices {
out.AdvertisedServices = append(out.AdvertisedServices, v)
}
for _, v := range h.IPs {
out.IPs = append(out.IPs, v.String())
}
for v := range h.MACs {
out.MACs = append(out.MACs, v)
}
return out
}
func calcOS(vals map[OS]uint) map[string]float64 {
var sum float64
res := map[string]float64{}
// Compute OS -> total weight
for os, weight := range vals {
val := float64(weight)
// Cut the weight of fuzzy results
if os.Fuzzy {
val /= 2
}
res[os.Label] += val
sum += val
}
// Normalize
for os := range res {
res[os] /= sum
}
return res
}
func fmtOS(vals map[OS]uint) string {
if len(vals) == 0 {
return ""
}
return fmt.Sprintf("%v", calcOS(vals))
}
func fmtServices(vals map[Service]bool) string {
keys := []string{}
for k := range vals {
keys = append(keys, fmt.Sprintf("%v", k))
}
return strings.Join(keys, ",")
}
func fmtHostnames(vals map[Hostname]bool) string {
keys := []string{}
for k := range vals {
keys = append(keys, fmt.Sprintf("%q", k))
}
return strings.Join(keys, ",")
}
func fmtStrings(vals map[string]bool) string {
keys := []string{}
for k := range vals {
keys = append(keys, fmt.Sprintf("%q", k))
}
return strings.Join(keys, ",")
}