forked from projectdiscovery/cdncheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cdncheck.go
153 lines (139 loc) · 4.26 KB
/
cdncheck.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
package cdncheck
import (
"net"
"strings"
"sync"
"github.com/projectdiscovery/retryabledns"
)
var (
DefaultCDNProviders string
DefaultWafProviders string
DefaultCloudProviders string
)
// DefaultResolvers trusted (taken from fastdialer)
var DefaultResolvers = []string{
"1.1.1.1:53",
"1.0.0.1:53",
"8.8.8.8:53",
"8.8.4.4:53",
}
// Client checks for CDN based IPs which should be excluded
// during scans since they belong to third party firewalls.
type Client struct {
sync.Once
cdn *providerScraper
waf *providerScraper
cloud *providerScraper
retriabledns *retryabledns.Client
}
// New creates cdncheck client with default options
// NewWithOpts should be preferred over this function
func New() *Client {
client, _ := NewWithOpts(3, []string{})
return client
}
// NewWithOpts creates cdncheck client with custom options
func NewWithOpts(MaxRetries int, resolvers []string) (*Client, error) {
if MaxRetries <= 0 {
MaxRetries = 3
}
if len(resolvers) == 0 {
resolvers = DefaultResolvers
}
retryabledns, err := retryabledns.New(resolvers, MaxRetries)
if err != nil {
return nil, err
}
client := &Client{
cdn: newProviderScraper(generatedData.CDN),
waf: newProviderScraper(generatedData.WAF),
cloud: newProviderScraper(generatedData.Cloud),
retriabledns: retryabledns,
}
return client, nil
}
// CheckCDN checks if an IP is contained in the cdn denylist
func (c *Client) CheckCDN(ip net.IP) (matched bool, value string, err error) {
matched, value, err = c.cdn.Match(ip)
return matched, value, err
}
// CheckWAF checks if an IP is contained in the waf denylist
func (c *Client) CheckWAF(ip net.IP) (matched bool, value string, err error) {
matched, value, err = c.waf.Match(ip)
return matched, value, err
}
// CheckCloud checks if an IP is contained in the cloud denylist
func (c *Client) CheckCloud(ip net.IP) (matched bool, value string, err error) {
matched, value, err = c.cloud.Match(ip)
return matched, value, err
}
// Check checks if ip belongs to one of CDN, WAF and Cloud . It is generic method for Checkxxx methods
func (c *Client) Check(ip net.IP) (matched bool, value string, itemType string, err error) {
if matched, value, err = c.cdn.Match(ip); err == nil && matched && value != "" {
return matched, value, "cdn", nil
}
if matched, value, err = c.waf.Match(ip); err == nil && matched && value != "" {
return matched, value, "waf", nil
}
if matched, value, err = c.cloud.Match(ip); err == nil && matched && value != "" {
return matched, value, "cloud", nil
}
return false, "", "", err
}
// Check Domain with fallback checks if domain belongs to one of CDN, WAF and Cloud . It is generic method for Checkxxx methods
// Since input is domain, as a fallback it queries CNAME records and checks if domain is WAF
func (c *Client) CheckDomainWithFallback(domain string) (matched bool, value string, itemType string, err error) {
dnsData, err := c.retriabledns.Resolve(domain)
if err != nil {
return false, "", "", err
}
matched, value, itemType, err = c.CheckDNSResponse(dnsData)
if err != nil {
return false, "", "", err
}
if matched {
return matched, value, itemType, nil
}
// resolve cname
dnsData, err = c.retriabledns.CNAME(domain)
if err != nil {
return false, "", "", err
}
return c.CheckDNSResponse(dnsData)
}
// CheckDNSResponse is same as CheckDomainWithFallback but takes DNS response as input
func (c *Client) CheckDNSResponse(dnsResponse *retryabledns.DNSData) (matched bool, value string, itemType string, err error) {
if dnsResponse.A != nil {
for _, ip := range dnsResponse.A {
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
continue
}
matched, value, itemType, err := c.Check(ipAddr)
if err != nil {
return false, "", "", err
}
if matched {
return matched, value, itemType, nil
}
}
}
if dnsResponse.CNAME != nil {
matched, discovered, itemType, err := c.CheckSuffix(dnsResponse.CNAME...)
if err != nil {
return false, "", itemType, err
}
if matched {
// for now checkSuffix only checks for wafs
return matched, discovered, itemType, nil
}
}
return false, "", "", err
}
func mapKeys(m map[string][]string) string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return strings.Join(keys, ", ")
}