/
gr-subdomains.go
396 lines (327 loc) · 9.68 KB
/
gr-subdomains.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/fatih/color"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/D3Ext/go-recon/core"
)
var red func(a ...interface{}) string = color.New(color.FgRed).SprintFunc()
var cyan func(a ...interface{}) string = color.New(color.FgCyan).SprintFunc()
var green func(a ...interface{}) string = color.New(color.FgGreen).SprintFunc()
var magenta func(a ...interface{}) string = color.New(color.FgMagenta).SprintFunc()
var yellow func(a ...interface{}) string = color.New(color.FgYellow).SprintFunc()
type SubdomainsInfo struct {
Subdomains []string `json:"subdomains"`
Length int `json:"length"`
Time string `json:"time"`
}
func listProviders(quiet bool) {
if !quiet {
fmt.Println(`[*] Available providers:
- alienvault
- anubis
- commoncrawl
- crt
- digitorus
- hackertarget
- rapiddns
- wayback
Providers used by default: [alienvault, crt, hackertarget, rapiddns, digitorus]`)
} else {
fmt.Println("alienvault\nanubis\ncommoncrawl\ncrt\ndigitorus\nhackertarget\nrapiddns\nwayback")
}
}
func helpPanel() {
fmt.Println(`Usage of gr-subdomains:
INPUT:
-d, -domain string domain to find its subdomains (i.e. example.com)
-l, -list string file containing a list of domains to find their subdomains (one domain per line)
OUTPUT:
-o, -output string file to write subdomains into
-oj, -output-json string file to write subdomains into (JSON format)
PROVIDERS:
-all use all available providers to discover subdomains (slower than default)
-p, -providers string[] providers to use for subdomain discovery (separated by comma)
-lp, -list-providers list available providers
CONFIG:
-proxy string proxy to send requests through (i.e. http://127.0.0.1:8080)
-t, -timeout int milliseconds to wait before each request timeout (default=5000)
-c, -color print colors on output
-q, -quiet print neither banner nor logging, only print output
DEBUG:
-version show go-recon version
-h, -help print help panel
Examples:
gr-subdomains -d example.com -o subdomains.txt -c
gr-subdomains -l domains.txt -p crt,hackertarget -t 8000
cat domain.txt | gr-subdomains -all
cat domain.txt | gr-subdomains -p anubis -oj subdomains.json -c
`)
}
// nolint: gocyclo
func main() {
var domain string
var list string
var all bool
var providers_str string
var list_providers bool
var output string
var json_output string
var proxy string
var stdin bool
var timeout int
var quiet bool
var use_color bool
var version bool
var help bool
flag.StringVar(&domain, "d", "", "")
flag.StringVar(&domain, "domain", "", "")
flag.StringVar(&list, "l", "", "")
flag.StringVar(&list, "list", "", "")
flag.BoolVar(&all, "all", false, "")
flag.StringVar(&providers_str, "p", "", "")
flag.StringVar(&providers_str, "providers", "", "")
flag.BoolVar(&list_providers, "lp", false, "")
flag.BoolVar(&list_providers, "list-providers", false, "")
flag.StringVar(&output, "o", "", "")
flag.StringVar(&output, "output", "", "")
flag.StringVar(&json_output, "oj", "", "")
flag.StringVar(&json_output, "output-json", "", "")
flag.StringVar(&proxy, "proxy", "", "")
flag.IntVar(&timeout, "t", 5000, "")
flag.IntVar(&timeout, "timeout", 5000, "")
flag.BoolVar(&quiet, "q", false, "")
flag.BoolVar(&quiet, "quiet", false, "")
flag.BoolVar(&use_color, "c", false, "")
flag.BoolVar(&use_color, "color", false, "")
flag.BoolVar(&version, "version", false, "")
flag.BoolVar(&help, "h", false, "")
flag.BoolVar(&help, "help", false, "")
flag.Parse()
t1 := core.StartTimer()
if version {
fmt.Println("go-recon version:", core.Version())
os.Exit(0)
}
if !quiet {
fmt.Println(core.Banner())
}
if help {
helpPanel()
os.Exit(0)
}
if list_providers {
listProviders(quiet)
os.Exit(0)
}
var err error
// Check if stdin has value
fi, err := os.Stdin.Stat()
if err != nil {
log.Fatal(err)
}
if fi.Mode()&os.ModeNamedPipe == 0 {
stdin = false // stdin is empty
} else {
stdin = true // stdin has value
}
// if domain, list and stdin parameters are empty print help panel and exit
if (domain == "") && (list == "") && (!stdin) {
helpPanel()
os.Exit(0)
}
if (domain != "") && (list != "") {
helpPanel()
core.Red("You can't use (-d) and (-l) at same time", use_color)
os.Exit(0)
}
if proxy != "" {
os.Setenv("HTTP_PROXY", proxy)
os.Setenv("HTTPS_PROXY", proxy)
}
if providers_str == "" { // default providers
providers_str = "alienvault,crt,digitorus,hackertarget,rapiddns"
} else if all { // all providers
providers_str = "alienvault,anubis,commoncrawl,crt,digitorus,hackertarget,rapiddns,wayback"
}
providers := strings.Split(strings.ToLower(strings.ReplaceAll(providers_str, " ", "")), ",")
// define variables which will be used to write subdomains to file
var txt_out *os.File
if output != "" {
txt_out, err = os.Create(output)
if err != nil {
log.Fatal(err)
}
}
var counter int
var total_counter int
var found_subdomains []string
client := core.CreateHttpClient(timeout)
// Create channel for asynchronous jobs
results := make(chan string)
var wg sync.WaitGroup
var out sync.WaitGroup
out.Add(1)
if !quiet {
core.Warning("Use with caution", use_color)
}
if domain != "" { // if domain parameter has value enter here
if !quiet {
core.Magenta("Discovering "+domain+" subdomains...", use_color)
core.Magenta(fmt.Sprintf("Selected providers: %s\n", providers), use_color)
}
wg.Add(1) // used to wait until main goroutine finish, that goroutine will generate 8 concurrent workers
go func() { // goroutine used for printing results
for subdomain := range results {
//fmt.Println(subdomain)
if checkSubdomain(subdomain, found_subdomains) {
fmt.Println(subdomain)
found_subdomains = append(found_subdomains, subdomain)
if output != "" { // if output parameter has value, write subdomains to file
_, err = txt_out.WriteString(subdomain + "\n")
if err != nil {
log.Fatal(err)
}
}
counter++
}
}
out.Done()
}()
// run all providers enum
go func() {
defer wg.Done()
//func GetAllSubdomains(dom string, c chan string, providers []string, client *http.Client) error
core.GetSubdomains(domain, results, providers, client)
}()
wg.Wait()
close(results)
out.Wait()
} else if (list != "") || (stdin) { // enter here if list parameter has value or if stdin has value
var f *os.File
var err error
if list != "" {
f, err = os.Open(list)
if err != nil {
log.Fatal(err)
}
defer f.Close()
} else if stdin {
f = os.Stdin
}
var doms_length int
var domains []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if (scanner.Text() != "") && (strings.Contains(scanner.Text(), ".")) {
doms_length += 1
domains = append(domains, scanner.Text())
}
}
if !quiet {
if use_color {
fmt.Println("["+magenta("*")+"] Discovering subdomains for", green(doms_length), "domains")
} else {
fmt.Println("[*] Discovering subdomains for", doms_length, "domains")
}
core.Magenta(fmt.Sprintf("Selected providers: %s", providers), use_color)
}
// Create channel for asynchronous jobs
results := make(chan string)
go func() { // goroutine used for printing results
for subdomain := range results {
if checkSubdomain(subdomain, found_subdomains) {
fmt.Println(subdomain)
found_subdomains = append(found_subdomains, subdomain)
if output != "" { // if output parameter has value, write subdomains to file
_, err = txt_out.WriteString(subdomain + "\n")
if err != nil {
log.Fatal(err)
}
}
total_counter += 1
counter++
}
}
out.Done()
}()
for _, dom := range domains {
wg.Add(1)
if !quiet {
core.Magenta("Enumerating "+dom+" subdomains", use_color)
}
core.GetSubdomains(dom, results, providers, client)
if !quiet {
core.Green(strconv.Itoa(counter)+" subdomains found for "+dom+"\n", use_color)
}
counter = 0
wg.Done()
}
wg.Wait()
close(results)
out.Wait()
}
if json_output != "" {
json_subs := SubdomainsInfo{
Subdomains: found_subdomains,
Length: counter,
Time: core.TimerDiff(t1).String(),
}
json_body, err := json.Marshal(json_subs)
if err != nil {
log.Fatal(err)
}
json_out, err := os.Create(json_output)
if err != nil {
log.Fatal(err)
}
_, err = json_out.WriteString(string(json_body))
if err != nil {
log.Fatal(err)
}
}
// Finally some logging to aid users
if !quiet {
if total_counter >= 1 {
if use_color {
fmt.Println("["+green("+")+"]", cyan(total_counter), "subdomains found in total")
} else {
fmt.Println("[+]", total_counter, "subdomains found in total")
}
} else if counter >= 1 {
if use_color {
fmt.Println("\n["+green("+")+"]", cyan(counter), "subdomains found")
} else {
fmt.Println("\n[+]", counter, "subdomains found")
}
}
if total_counter >= 1 || counter >= 1 { // Check if at least one url was vulnerable to open redirect
if output != "" {
core.Green("Subdomains written to "+output, use_color)
} else if json_output != "" {
core.Green("Subdomains written to "+json_output, use_color)
}
}
if use_color {
fmt.Println("["+green("+")+"] Elapsed time:", green(core.TimerDiff(t1)))
} else {
fmt.Println("[+] Elapsed time:", core.TimerDiff(t1))
}
}
}
func checkSubdomain(str string, slice []string) bool {
for _, s := range slice {
if strings.ToLower(str) == strings.ToLower(s) {
//fmt.Println(strings.ToLower(str), "-", strings.ToLower(s))
return false
}
}
return true
}