forked from owasp-amass/amass
/
shadowserver.go
197 lines (165 loc) · 4.61 KB
/
shadowserver.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
// 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 sources
import (
"bufio"
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/OWASP/Amass/amass/core"
"github.com/OWASP/Amass/amass/utils"
)
const (
// ShadowServerWhoisURL is the URL for the ShadowServer whois server.
ShadowServerWhoisURL = "asn.shadowserver.org"
)
// ShadowServer is the Service that handles access to the ShadowServer data source.
type ShadowServer struct {
core.BaseService
SourceType string
RateLimit time.Duration
addr string
}
// NewShadowServer returns he object initialized, but not yet started.
func NewShadowServer(config *core.Config, bus *core.EventBus) *ShadowServer {
s := &ShadowServer{
SourceType: core.API,
RateLimit: time.Second,
}
s.BaseService = *core.NewBaseService(s, "ShadowServer", config, bus)
return s
}
// OnStart implements the Service interface
func (s *ShadowServer) OnStart() error {
s.BaseService.OnStart()
if answers, err := core.Resolve(ShadowServerWhoisURL, "A", core.PriorityHigh); err == nil {
ip := answers[0].Data
if ip != "" {
s.addr = ip
}
}
s.Bus().Subscribe(core.IPToASNTopic, s.SendASNRequest)
go s.processRequests()
return nil
}
func (s *ShadowServer) processRequests() {
last := time.Now().Truncate(10 * time.Minute)
loop:
for {
select {
case <-s.Quit():
return
case req := <-s.ASNRequestChan():
if req.Address == "" && req.ASN == 0 {
continue loop
}
if time.Now().Sub(last) < s.RateLimit {
time.Sleep(s.RateLimit)
}
last = time.Now()
if req.Address != "" {
s.executeASNAddrQuery(req.Address)
} else {
s.executeASNQuery(req.ASN)
}
last = time.Now()
case <-s.DNSRequestChan():
case <-s.AddrRequestChan():
case <-s.WhoisRequestChan():
}
}
}
func (s *ShadowServer) executeASNQuery(asn int) {
s.SetActive()
blocks := s.netblocks(asn)
if len(blocks) == 0 {
return
}
time.Sleep(s.RateLimit)
req := s.origin(strings.Trim(blocks[0], "/"))
if req == nil {
return
}
req.Netblocks = utils.UniqueAppend(req.Netblocks, blocks...)
s.Bus().Publish(core.NewASNTopic, req)
}
func (s *ShadowServer) executeASNAddrQuery(addr string) {
s.SetActive()
req := s.origin(addr)
if req == nil {
return
}
time.Sleep(s.RateLimit)
req.Netblocks = utils.UniqueAppend(req.Netblocks, s.netblocks(req.ASN)...)
s.Bus().Publish(core.NewASNTopic, req)
}
func (s *ShadowServer) origin(addr string) *core.ASNRequest {
if ip := net.ParseIP(addr); ip == nil || !utils.IsIPv4(ip) {
return nil
}
name := utils.ReverseIP(addr) + ".origin.asn.shadowserver.org"
answers, err := core.Resolve(name, "TXT", core.PriorityHigh)
if err != nil {
s.Config().Log.Printf("%s: %s: DNS TXT record query error: %v", s.String(), name, err)
return nil
}
fields := strings.Split(strings.Trim(answers[0].Data, "\""), " | ")
if len(fields) < 5 {
s.Config().Log.Printf("%s: %s: Failed to parse the origin response", s.String(), name)
return nil
}
asn, err := strconv.Atoi(strings.TrimSpace(fields[0]))
if err != nil {
s.Config().Log.Printf("%s: %s: Failed to parse the origin response: %v", s.String(), name, err)
return nil
}
return &core.ASNRequest{
ASN: asn,
Prefix: strings.TrimSpace(fields[1]),
CC: strings.TrimSpace(fields[3]),
Description: strings.TrimSpace(fields[2]) + " - " + strings.TrimSpace(fields[4]),
Netblocks: []string{strings.TrimSpace(fields[1])},
Tag: s.SourceType,
Source: s.String(),
}
}
func (s *ShadowServer) netblocks(asn int) []string {
var netblocks []string
if s.addr == "" {
answers, err := core.Resolve(ShadowServerWhoisURL, "A", core.PriorityHigh)
if err != nil {
s.Config().Log.Printf("%s: %s: %v", s.String(), ShadowServerWhoisURL, err)
return netblocks
}
ip := answers[0].Data
if ip == "" {
s.Config().Log.Printf("%s: Failed to resolve %s", s.String(), ShadowServerWhoisURL)
return netblocks
}
s.addr = ip
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
d := net.Dialer{}
conn, err := d.DialContext(ctx, "tcp", s.addr+":43")
if err != nil {
s.Config().Log.Printf("%s: %v", s.String(), err)
return netblocks
}
defer conn.Close()
fmt.Fprintf(conn, "prefix %d\n", asn)
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
if err := scanner.Err(); err == nil {
netblocks = utils.UniqueAppend(netblocks, strings.TrimSpace(line))
}
}
if len(netblocks) == 0 {
s.Config().Log.Printf("%s: Failed to acquire netblocks for ASN %d", s.String(), asn)
}
return netblocks
}