-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrdns.go
More file actions
167 lines (147 loc) · 4.7 KB
/
rdns.go
File metadata and controls
167 lines (147 loc) · 4.7 KB
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
/*
* ZAnnotate Copyright 2025 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"context"
"flag"
"fmt"
"net"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/zmap/dns"
"github.com/zmap/zdns/v2/src/zdns"
)
type RDNSOutput struct {
DomainNames []string `json:"domain_names,omitempty"`
}
type RDNSAnnotatorFactory struct {
BasePluginConf
RawResolvers string
zdnsConfig *zdns.ResolverConfig
timeoutSecs int
}
type RDNSAnnotator struct {
Factory *RDNSAnnotatorFactory
Id int
zdnsResolver *zdns.Resolver
}
// RDNS Annotator Factory (Global)
func (a *RDNSAnnotatorFactory) MakeAnnotator(i int) Annotator {
var v RDNSAnnotator
v.Factory = a
v.Id = i
return &v
}
func (a *RDNSAnnotatorFactory) Initialize(_ *GlobalConf) error {
a.zdnsConfig = zdns.NewResolverConfig()
a.zdnsConfig.NetworkTimeout = time.Second * 5
if len(strings.TrimSpace(a.RawResolvers)) > 0 {
// Parse and Validate the User-Specified Resolvers
// 1. split on comma
resolvers := strings.Split(a.RawResolvers, ",")
// 2. trim whitespace
for _, resolver := range resolvers {
trimmedString := strings.TrimSpace(resolver)
// 3. validate IP
ip := net.ParseIP(trimmedString)
if ip == nil {
return fmt.Errorf("failed to parse dns server IP address: %s", trimmedString)
}
// 4. Differentiate between IPv4 and IPv6
ns := zdns.NameServer{
IP: ip,
Port: 53,
DomainName: "",
}
if ip.To4() != nil {
a.zdnsConfig.ExternalNameServersV4 = append(a.zdnsConfig.ExternalNameServersV4, ns)
} else {
a.zdnsConfig.ExternalNameServersV6 = append(a.zdnsConfig.ExternalNameServersV6, ns)
}
}
}
return nil
}
func (a *RDNSAnnotatorFactory) GetWorkers() int {
return a.Threads
}
func (a *RDNSAnnotatorFactory) Close() error {
return nil
}
func (a *RDNSAnnotatorFactory) IsEnabled() bool {
return a.Enabled
}
func (a *RDNSAnnotatorFactory) AddFlags(flags *flag.FlagSet) {
// Reverse DNS Lookup
flags.BoolVar(&a.Enabled, "rdns", false, "reverse dns lookup")
flags.StringVar(&a.RawResolvers, "rdns-dns-servers", "", "list of DNS servers to use for DNS lookups, comma-separated IP list. If empty, will use system defaults")
flags.IntVar(&a.Threads, "rdns-threads", 100, "how many reverse dns threads")
flags.IntVar(&a.timeoutSecs, "rdns-timeout", 2, "timeout for each rdns query, in seconds")
}
// RDNS Annotator (Per-Worker)
func (a *RDNSAnnotator) Initialize() (err error) {
a.zdnsResolver, err = zdns.InitResolver(a.Factory.zdnsConfig)
if err != nil {
return fmt.Errorf("failed to initialize zdns resolver: %w", err)
}
return nil
}
func (a *RDNSAnnotator) GetFieldName() string {
return "rdns"
}
// Annotate performs a reverse DNS lookup for the given IP address and returns the results.
// If an error occurs or a lookup fails, it returns nil
func (a *RDNSAnnotator) Annotate(ip net.IP) interface{} {
q := zdns.Question{
Type: dns.TypePTR,
Class: dns.ClassINET,
Name: ip.String(),
}
output := &RDNSOutput{}
res, _, status, err := a.zdnsResolver.ExternalLookup(context.Background(), &q, nil)
if err != nil {
log.Debug("encountered error when resolving rdns for ", ip.String(), ": ", err)
return output
}
if status != zdns.StatusNoError {
log.Debug("could not resolve rdns for ", ip.String(), " with status: ", status)
return output
}
if res == nil {
// this should never happen, but this will be more helpful than a panic
log.Fatalf("zdns returned a nil result without erroring, zannotate cannot continue")
}
output.DomainNames = make([]string, 0, len(res.Answers))
for _, answer := range res.Answers {
if castAns, ok := answer.(zdns.Answer); ok {
// Sometimes, CNAME records are returned in addition to PTR records. We'll ignore all non-PTR records.
// This replicates the behavior of Go's net.LookupAddr
if castAns.Type != "PTR" {
continue
}
// remove trailing period from domain name, ex: example.com. -> example.com
output.DomainNames = append(output.DomainNames, strings.TrimSuffix(castAns.Answer, "."))
}
}
return output
}
func (a *RDNSAnnotator) Close() error {
a.zdnsResolver.Close()
return nil
}
func init() {
s := new(RDNSAnnotatorFactory)
RegisterAnnotator(s)
}