forked from owasp-amass/amass
/
viz.go
144 lines (124 loc) · 3.17 KB
/
viz.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
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package graph
import (
"github.com/chrisswanson/Amass/v3/graph/db"
"github.com/chrisswanson/Amass/v3/stringset"
"github.com/chrisswanson/Amass/v3/viz"
)
// VizData returns the current state of the Graph as viz package Nodes and Edges.
func (g *Graph) VizData(uuid string) ([]viz.Node, []viz.Edge) {
event, err := g.db.ReadNode(uuid)
if err != nil {
return nil, nil
}
discovered, err := g.db.ReadOutEdges(event)
if err != nil {
return nil, nil
}
var idx int
var nodes []viz.Node
ids := stringset.New()
rnodes := make(map[string]int)
// Identify unique nodes that should be included in the visualization
for _, d := range discovered {
if id := g.db.NodeToID(d.To); id != "" && !ids.Has(id) {
ids.Insert(id)
properties, err := g.db.ReadProperties(d.To, "type")
if err != nil || len(properties) == 0 || properties[0].Value == "source" {
continue
}
if g.IsTLDNode(id) {
continue
}
if n := g.buildVizNode(d.To, properties[0].Value, uuid); n != nil {
n.ID = idx
// Keep track of which indices nodes were assigned to
rnodes[id] = idx
idx++
nodes = append(nodes, *n)
}
}
}
var edges []viz.Edge
// Identify the edges between nodes that should be included in the visualization
for _, n := range nodes {
node, err := g.db.ReadNode(n.Label)
if err != nil {
continue
}
e, err := g.db.ReadOutEdges(node, "root", "cname_record",
"a_record", "aaaa_record", "ptr_record", "service",
"srv_record", "ns_record", "mx_record", "contains", "prefix")
if err != nil || len(e) == 0 {
continue
}
for _, edge := range e {
if toID, found := rnodes[g.db.NodeToID(edge.To)]; found {
edges = append(edges, viz.Edge{
From: n.ID,
To: toID,
Title: edge.Predicate,
})
}
}
}
return nodes, edges
}
func (g *Graph) buildVizNode(node db.Node, ntype, uuid string) *viz.Node {
id := g.db.NodeToID(node)
edges, err := g.db.ReadInEdges(node)
if err != nil && len(edges) == 0 {
return nil
}
var sources []string
// Select one of the data sources to be used in the visualization
for _, edge := range edges {
if g.db.NodeToID(edge.From) == uuid {
sources = append(sources, edge.Predicate)
}
}
if len(sources) == 0 {
return nil
}
src := sources[randomIndex(len(sources))]
// Update the type names for visualization
if ntype == "fqdn" {
var pred string
// Look for edge predicates of interest
for _, edge := range edges {
if edge.Predicate == "root" ||
edge.Predicate == "mx_record" ||
edge.Predicate == "ns_record" {
pred = edge.Predicate
break
}
}
if pred != "" {
switch pred {
case "root":
ntype = "domain"
case "ns_record":
ntype = "ns"
case "mx_record":
ntype = "mx"
}
} else if g.IsPTRNode(id) {
ntype = "ptr"
} else {
ntype = "subdomain"
}
} else if ntype == "ipaddr" {
ntype = "address"
}
title := ntype + ": " + id
if ntype == "as" {
title = title + ", Desc: " + g.ReadASDescription(id)
}
return &viz.Node{
Type: ntype,
Label: id,
Title: title,
Source: src,
}
}