-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhatcloud.go
276 lines (251 loc) · 7.7 KB
/
whatcloud.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
package main
import (
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
"github.com/99designs/clouddetect"
"github.com/projectdiscovery/retryabledns"
"golang.org/x/exp/slices"
)
var cloudNames = []string{
"azure",
"amazon",
"google",
"oracle",
}
var CLOUD_CNAME_DOMAINS = map[string]string{
"cloudfront.net": "amazon",
"amazonaws.com": "amazon",
}
var CLOUD_WAPPALYZER_TECHNOLOGIES = map[string]string{
"cloudfront": "amazon",
}
func check_cloud_url(input string, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
var response *http.Response
url_parsed, err := url.Parse(input)
if err != nil {
return
}
url_domain := url_parsed.Hostname()
isCloud, cloudName, detectionType, err = check_cloud_domain(url_domain, result)
if !isCloud {
if result.HttpResponse == nil {
response, err = whatcdn.httpClient.Get(input)
if err != nil {
return
}
result.HttpResponse = response
}
isCloud, cloudName, detectionType, err = check_cloud_with_http_response(result.HttpResponse, result)
}
return isCloud, cloudName, detectionType, err
}
func check_cloud_domain(input string, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
var response *http.Response
var dnsresponse *retryabledns.DNSData
if result.DnsResponse == nil {
dnsresponse, err = whatcdn.dnsClient.Resolve(input)
if err != nil {
return
}
result.DnsResponse = dnsresponse
}
isCloud, cloudName, detectionType, err = check_cloud_with_dns(result.DnsResponse, result)
if !isCloud && whatcdn.http_fallback {
if result.HttpResponse == nil {
response, err = whatcdn.httpClient.Get("https://" + input)
if err != nil {
return
}
result.HttpResponse = response
}
isCloud, cloudName, detectionType, err = check_cloud_with_http_response(result.HttpResponse, result)
}
return isCloud, cloudName, detectionType, err
}
func check_cloud_ip(input string, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
return ip_list_cloud_check([]string{input}, result)
}
func ip_list_cloud_check(ips []string, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
if result.InputType != "ip" && result.IPs == nil {
result.IPs = ips
}
var (
dns_response *retryabledns.DNSData
internalIPs []string
cloud *clouddetect.Response
)
for _, ip := range ips {
ip_parsed := net.ParseIP(ip)
if ip_parsed == nil {
log.Printf("invalid IP: %s", ip)
continue
}
/* change here to check for internal ip */
if ip_parsed.IsPrivate() {
log.Printf("[*] private IP: %s", ip)
internalIPs = append(internalIPs, ip)
continue
}
// check if this ip is a CLOUD
isCloud, cloudName, err = whatcdn.cdncheckClient.Check(ip_parsed)
if err != nil {
return
}
if isCloud && slices.Contains(cloudNames, cloudName) {
detectionType = "ip"
result.HasCloud = true
detectedCloud := DetectedCloud{
Name: cloudName,
DetectionMethod: detectionType,
DetectedIps: []string{ip},
}
result.DetectedCloud = append(result.DetectedCloud, detectedCloud)
break
} else {
cloud, err = clouddetect.Resolve(ip_parsed)
if err != nil {
return
}
if cloud.ProviderName != "" {
cloudName = cloud.ProviderName
isCloud = true
detectionType = "ip"
result.HasCloud = true
detectedCloud := DetectedCloud{
Name: cloud.ProviderName,
DetectionMethod: detectionType,
DetectedIps: []string{ip},
}
result.DetectedCloud = append(result.DetectedCloud, detectedCloud)
break
} else {
dns_response, err = whatcdn.dnsClient.PTR(ip)
if err != nil {
return
}
isCloud, cloudName, detectionType, err = check_cloud_with_dns(dns_response, result)
if err != nil {
return
}
}
}
}
if len(internalIPs) > 0 && result.InputType != "ip" {
result.HasInternalIps = true
result.InternalIPs = internalIPs
}
return isCloud, cloudName, detectionType, err
}
func cname_list_cloud_check(cname_list []string, result *Result) (isCloud bool, cloudName string, detectionType string) {
for _, cname := range cname_list {
// check if this cname ends with a CLOUD domain
for cloud_domain, cloud_name_iter := range CLOUD_CNAME_DOMAINS {
if strings.HasSuffix(cname, cloud_domain) {
isCloud = true
cloudName = cloud_name_iter
detectionType = "cname"
result.HasCloud = true
detectedCloud := DetectedCloud{
Name: cloudName,
DetectionMethod: detectionType,
DetectedPTRDomains: []string{cname},
}
result.DetectedCloud = append(result.DetectedCloud, detectedCloud)
return
}
}
}
return isCloud, cloudName, detectionType
}
func ptr_list_cloud_check(ptr_list []string, result *Result) (isCloud bool, cloudName string, detectionType string) {
// var cloud_detected_ptr []string
for _, ptr_domain := range ptr_list {
// check if this ptr ends with a Cloud domain
for cloud_domain, cloud_name_iter := range CLOUD_CNAME_DOMAINS {
if strings.HasSuffix(ptr_domain, cloud_domain) {
isCloud = true
cloudName = cloud_name_iter
detectionType = "ptr"
result.HasCloud = true
detectedCloud := DetectedCloud{
Name: cloudName,
DetectionMethod: detectionType,
DetectedPTRDomains: []string{ptr_domain},
}
result.DetectedCloud = append(result.DetectedCloud, detectedCloud)
return
}
}
}
return isCloud, cloudName, detectionType
}
func check_cloud_with_dns(dnsResponse *retryabledns.DNSData, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
if result.DnsResponse == nil {
result.DnsResponse = dnsResponse
}
if dnsResponse.PTR != nil {
if result.PTRs == nil {
result.PTRs = dnsResponse.PTR
}
if !isCloud {
isCloud, cloudName, detectionType = ptr_list_cloud_check(result.PTRs, result)
}
}
if dnsResponse.CNAME != nil {
if result.CNAMEs == nil {
result.CNAMEs = dnsResponse.CNAME
}
if !isCloud {
isCloud, cloudName, detectionType = cname_list_cloud_check(result.CNAMEs, result)
}
}
if dnsResponse.A != nil {
if result.IPs == nil {
result.IPs = dnsResponse.A
}
if !isCloud {
isCloud, cloudName, detectionType, err = ip_list_cloud_check(result.IPs, result)
}
}
// if in_ipv6_list(dnsResponse.AAAA) {
// return true
// }
return isCloud, cloudName, detectionType, err
}
func check_cloud_with_wappalyzerResponse(wappalyzerResponse map[string]struct{}, result *Result) (isCloud bool, cloudName string, detectionType string) {
// matches := wappalyzerClient.Fingerprint(httpResponse.Header, body)
for technology := range wappalyzerResponse {
for cloud_tech, cloud_name_iter := range CLOUD_WAPPALYZER_TECHNOLOGIES {
// check if technology.lower contains a CDN technology
if strings.Contains(strings.ToLower(technology), cloud_tech) {
isCloud = true
cloudName = cloud_name_iter
detectionType = "http"
result.HasCloud = true
detectedCloud := DetectedCloud{
Name: cloudName,
DetectionMethod: detectionType,
DetectedTechnologies: []string{technology},
}
result.DetectedCloud = append(result.DetectedCloud, detectedCloud)
return
}
}
}
return isCloud, cloudName, detectionType
}
func check_cloud_with_http_response(httpResponse *http.Response, result *Result) (isCloud bool, cloudName string, detectionType string, err error) {
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return
}
if result.WappalyzerResponse == nil {
result.WappalyzerResponse = whatcdn.wappalyzerClient.Fingerprint(httpResponse.Header, body)
}
isCloud, cloudName, detectionType = check_cloud_with_wappalyzerResponse(result.WappalyzerResponse, result)
return isCloud, cloudName, detectionType, err
}