-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
312 lines (262 loc) · 8.18 KB
/
api.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package udig
import (
"crypto/x509"
"net/http"
"time"
"github.com/domainr/whois"
"github.com/miekg/dns"
)
/////////////////////////////////////////
// COMMON
/////////////////////////////////////////
const (
// DefaultTimeout is a default timeout used in all network clients.
DefaultTimeout = 3 * time.Second
)
// ResolutionType is an enumeration type for resolutions types.
type ResolutionType string
const (
// TypeDNS is a type of all DNS resolutions.
TypeDNS ResolutionType = "DNS"
// TypeWHOIS is a type of all WHOIS resolutions.
TypeWHOIS ResolutionType = "WHOIS"
// TypeTLS is a type of all TLS resolutions.
TypeTLS ResolutionType = "TLS"
// TypeHTTP is a type of all HTTP resolutions.
TypeHTTP ResolutionType = "HTTP"
// TypeCT is a type of all CT resolutions.
TypeCT ResolutionType = "CT"
// TypeBGP is a type of all BGP resolutions.
TypeBGP ResolutionType = "BGP"
// TypeGEO is a type of all GeoIP resolutions.
TypeGEO ResolutionType = "GEO"
)
// Udig is a high-level facade for domain resolution which:
// 1. delegates work to specific resolvers
// 2. deals with domain crawling
// 3. caches intermediate results and summarizes the outputs
type Udig interface {
Resolve(domain string) []Resolution
AddDomainResolver(resolver DomainResolver)
AddIPResolver(resolver IPResolver)
}
// DomainResolver is an API contract for all Resolver modules that resolve domains.
// Discovered domains that relate to the original query are recursively resolved.
type DomainResolver interface {
ResolveDomain(domain string) Resolution // Resolves a given domain.
}
// IPResolver is an API contract for all Resolver modules that resolve IPs.
type IPResolver interface {
ResolveIP(ip string) Resolution // Resolves a given IP.
}
// Resolution is an API contract for all Resolutions (i.e. results).
type Resolution interface {
Type() ResolutionType // Returns a type of this resolution.
Query() string // Returns the queried domain or IP.
Domains() []string // Returns a list of domains discovered in this resolution.
IPs() []string // Returns a list of IP addresses discovered in this resolution.
}
// ResolutionBase is a shared implementation for all Resolutions (i.e. results).
type ResolutionBase struct {
Resolution `json:"-"`
query string
}
// Query getter.
func (res *ResolutionBase) Query() string {
return res.query
}
// Domains returns a list of domains discovered in this resolution.
func (res *ResolutionBase) Domains() (domains []string) {
// Not supported by default.
return domains
}
// IPs returns a list of IP addresses discovered in this resolution.
func (res *ResolutionBase) IPs() (ips []string) {
// Not supported by default.
return ips
}
/////////////////////////////////////////
// DNS
/////////////////////////////////////////
// DNSResolver is a Resolver which is able to resolve a domain
// to a bunch of the most interesting DNS records.
//
// You can configure which query types are actually used
// and you can also supply a custom name server.
// If you don't a name server for each domain is discovered
// using NS record query, falling back to a local NS
// (e.g. the one in /etc/resolv.conf).
type DNSResolver struct {
DomainResolver
QueryTypes []uint16
NameServer string
Client *dns.Client
nameServerCache map[string]string
resolvedDomains map[string]bool
}
// DNSResolution is a DNS multi-query resolution yielding many DNS records
// in a form of query-answer pairs.
type DNSResolution struct {
*ResolutionBase
Records []DNSRecordPair
nameServer string
}
// DNSRecordPair is a pair of DNS record type used in the query
// and a corresponding record found in the answer.
type DNSRecordPair struct {
QueryType uint16
Record *DNSRecord
}
// DNSRecord is a wrapper for the actual DNS resource record.
type DNSRecord struct {
dns.RR
}
/////////////////////////////////////////
// WHOIS
/////////////////////////////////////////
// WhoisResolver is a Resolver responsible for resolution of a given
// domain to a list of WHOIS contacts.
type WhoisResolver struct {
DomainResolver
Client *whois.Client
}
// WhoisResolution is a WHOIS query resolution yielding many contacts.
type WhoisResolution struct {
*ResolutionBase
Contacts []WhoisContact
}
// WhoisContact is a wrapper for any item of interest from a WHOIS banner.
type WhoisContact struct {
RegistryDomainId string
Registrant string
RegistrantOrganization string
RegistrantStateProvince string
RegistrantCountry string
Registrar string
RegistrarIanaId string
RegistrarWhoisServer string
RegistrarUrl string
CreationDate string
UpdatedDate string
Registered string
Changed string
Expire string
NSSet string
Contact string
Name string
Address string
}
/////////////////////////////////////////
// TLS
/////////////////////////////////////////
// TLSResolver is a Resolver responsible for resolution of a given domain
// to a list of TLS certificates.
type TLSResolver struct {
DomainResolver
Client *http.Client
}
// TLSResolution is a TLS handshake resolution, which yields a certificate chain.
type TLSResolution struct {
*ResolutionBase
Certificates []TLSCertificate
}
// TLSCertificate is a wrapper for the actual x509.Certificate.
type TLSCertificate struct {
x509.Certificate
}
/////////////////////////////////////////
// HTTP
/////////////////////////////////////////
// HTTPResolver is a Resolver responsible for resolution of a given domain
// to a list of corresponding HTTP headers.
type HTTPResolver struct {
DomainResolver
Headers []string
Client *http.Client
}
// HTTPResolution is a HTTP header resolution yielding many HTTP protocol headers.
type HTTPResolution struct {
*ResolutionBase
Headers []HTTPHeader
}
// HTTPHeader is a pair of HTTP header name and corresponding value(s).
type HTTPHeader struct {
Name string
Value []string
}
/////////////////////////////////////////
// CT
/////////////////////////////////////////
// CTResolver is a Resolver responsible for resolution of a given domain
// to a list of CT logs.
type CTResolver struct {
DomainResolver
Client *http.Client
cachedResults map[string]*CTResolution
}
// CTResolution is a certificate transparency project resolution, which yields a CT log.
type CTResolution struct {
*ResolutionBase
Logs []CTAggregatedLog
}
// CTAggregatedLog is a wrapper of a CT log that is aggregated over all logs
// with the same CN in time.
type CTAggregatedLog struct {
CTLog
FirstSeen string
LastSeen string
}
// CTLog is a wrapper for attributes of interest that appear in the CT log.
// The json mapping comes from crt.sh API schema.
type CTLog struct {
Id int64 `json:"id"`
IssuerName string `json:"issuer_name"`
NameValue string `json:"name_value"`
LoggedAt string `json:"entry_timestamp"`
NotBefore string `json:"not_before"`
NotAfter string `json:"not_after"`
}
/////////////////////////////////////////
// BGP
/////////////////////////////////////////
// BGPResolver is a Resolver which is able to resolve an IP
// to AS name and ASN.
//
// Internally this resolver is leveraging a DNS interface of
// IP-to-ASN lookup service by Team Cymru.
type BGPResolver struct {
IPResolver
Client *dns.Client
cachedResults map[string]*BGPResolution
}
// BGPResolution is a BGP resolution of a given IP yielding AS records.
type BGPResolution struct {
*ResolutionBase
Records []ASRecord
}
// ASRecord contains information about an Autonomous System (AS).
type ASRecord struct {
Name string
ASN uint32
BGPPrefix string
Registry string
Allocated string
}
/////////////////////////////////////////
// GEO
/////////////////////////////////////////
// GeoResolver is a Resolver which is able to resolve an IP to a geographical location.
type GeoResolver struct {
IPResolver
enabled bool
cachedResults map[string]*GeoResolution
}
// GeoResolution is a GeoIP resolution of a given IP yielding geographical records.
type GeoResolution struct {
*ResolutionBase
Record *GeoRecord
}
// GeoRecord contains information about a geographical location.
type GeoRecord struct {
CountryCode string
}