-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
198 lines (173 loc) · 4.38 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/asaskevich/govalidator"
"github.com/projectdiscovery/cdncheck"
"github.com/projectdiscovery/iputil"
"github.com/projectdiscovery/retryabledns"
"github.com/projectdiscovery/retryablehttp-go"
wappalyzer "github.com/projectdiscovery/wappalyzergo"
)
var wg sync.WaitGroup
var DEFAULT_RESOLVERS = []string{"8.8.8.8:53", "8.8.4.4:53"}
type WHATCDN struct {
dnsClient *retryabledns.Client
httpClient *retryablehttp.Client
wappalyzerClient *wappalyzer.Wappalyze
cdncheckClient *cdncheck.Client
resolvers []string
retries int
http_fallback bool
json_output bool
}
var whatcdn WHATCDN
func main() {
// usage ./whatcdn -i <192.168.0.1-> <-json>
// usage cat inputs | ./whatcdn <-t 100> <-json>
var (
input string
max_threads int
json_output bool
max_retries int
http_fallback bool
)
flag.StringVar(&input, "i", "", "input to resolve/check")
flag.IntVar(&max_threads, "t", 10, "number of threads")
flag.BoolVar(&json_output, "json", false, "print json output")
flag.BoolVar(&http_fallback, "http-fallback", false, "print json output")
flag.IntVar(&max_retries, "retries", 3, "number of retries")
// receber uma lista de resolvers
flag.Parse()
HTTPOpts := retryablehttp.Options{
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 30 * time.Second,
Timeout: 30 * time.Second,
RetryMax: max_retries,
RespReadLimit: 4096 * 5,
KillIdleConn: true,
}
wappalyzerClient, err := wappalyzer.New()
if err != nil {
log.Fatal(err)
}
cdncheckClient, err := cdncheck.NewWithCache()
if err != nil {
log.Fatal(err)
}
dnsClient, err := retryabledns.New(DEFAULT_RESOLVERS, max_retries)
if err != nil {
log.Fatal(err)
}
whatcdn = WHATCDN{
resolvers: DEFAULT_RESOLVERS,
retries: max_retries,
dnsClient: dnsClient,
httpClient: retryablehttp.NewClient(HTTPOpts),
wappalyzerClient: wappalyzerClient,
cdncheckClient: cdncheckClient,
http_fallback: http_fallback,
json_output: json_output,
}
goroutines := make(chan struct{}, max_threads)
if input != "" {
goroutines <- struct{}{}
wg.Add(1)
go worker(input, goroutines)
} else {
stdin := os.Stdin
scanner := bufio.NewScanner(stdin)
for scanner.Scan() {
// check if input is a comment
line := scanner.Text()
if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") {
continue
}
goroutines <- struct{}{}
wg.Add(1)
go worker(line, goroutines)
}
}
wg.Wait()
close(goroutines)
}
func worker(input string, goroutines <-chan struct{}) {
result := new(Result)
result.Input = input
defer wg.Done()
// global variables
var (
input_type string
err error
)
input_type = detect_input_type(input)
result.InputType = input_type
switch input_type {
case "ip":
_, _, _, err = check_cdn_ip(input, result)
_, _, _, _ = check_cloud_ip(input, result)
case "domain":
_, _, _, err = check_cdn_domain(input, result)
_, _, _, _ = check_cloud_domain(input, result)
case "url":
_, _, _, err = check_cdn_url(input, result)
_, _, _, _ = check_cloud_url(input, result)
case "":
err = fmt.Errorf("invalid input: %s", input)
}
if err != nil {
log.Printf("[ERROR] %s", err)
<-goroutines
return
}
if whatcdn.json_output {
output_bytes, err := json.Marshal(result)
if err != nil {
log.Printf("[ERROR] %s", err)
<-goroutines
return
}
fmt.Println(string(output_bytes))
} else {
format_txt_output(result)
}
<-goroutines
}
/* hackerone.com domain cloudflare ip*/
func format_txt_output(result *Result) {
if !result.HasCDN {
fmt.Printf("%s cdn not detected\n", result.Input)
}
if !result.HasCloud {
fmt.Printf("%s cloud not detected\n", result.Input)
}
if result.HasCDN {
for _, cdn := range result.DetectedCDNs {
fmt.Printf("%s cdnName: %s detectionMethod: %s\n", result.Input, cdn.Name, cdn.DetectionMethod)
}
}
if result.HasCloud {
for _, cloud := range result.DetectedCloud {
fmt.Printf("%s cloudName: %s detectionMethod: %s\n", result.Input, cloud.Name, cloud.DetectionMethod)
}
}
}
func detect_input_type(input string) string {
if iputil.IsIP(input) {
return "ip"
}
if strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") {
return "url"
}
if govalidator.IsDNSName(input) {
return "domain"
}
return ""
}