-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (173 loc) · 5.19 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/logrusorgru/aurora"
"github.com/valyala/fasthttp"
)
type Config struct {
APIKey string `json:"apiKey"`
}
type SubdomainResponse struct {
Domain string `json:"domain"`
Subdomains []string `json:"subdomains"`
}
func loadConfig() (Config, error) {
var config Config
file, err := os.ReadFile("config.json")
if err != nil {
return config, err
}
err = json.Unmarshal(file, &config)
return config, err
}
func fetchSubdomains(client *fasthttp.Client, domain, apiKey string) ([]string, error) {
url := fmt.Sprintf("https://api.xreverselabs.my.id/subdomain?apiKey=%s&url=%s", apiKey, domain)
// Create a new request and response
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
// Set the request details
req.SetRequestURI(url)
// Perform the GET request
if err := client.Do(req, resp); err != nil {
return nil, err
}
// Check for a 200 OK status code
if resp.StatusCode() != fasthttp.StatusOK {
return nil, fmt.Errorf("non-200 response code: %d", resp.StatusCode())
}
// Parse the response body
var result SubdomainResponse
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return nil, err
}
// Release the request and response objects
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return result.Subdomains, nil
}
func scanDomain(client *fasthttp.Client, domain string, apiKey string, wg *sync.WaitGroup, output chan<- string) {
defer wg.Done()
fmt.Printf("%s Scanning domain: %s\n", aurora.Cyan("[*]").String(), aurora.BrightBlue(domain).String())
subdomains, err := fetchSubdomains(client, domain, apiKey)
if err != nil {
fmt.Printf("Error fetching subdomains for %s: %v\n", domain, err)
return
}
if len(subdomains) > 0 {
output <- strings.Join(subdomains, "\n") + "\n"
fmt.Printf("%s Found %d subdomains for %s\n", aurora.Green("[+]"), len(subdomains), aurora.BrightBlue(domain))
} else {
fmt.Printf("%s No subdomains found for %s\n", aurora.Red("[-]"), aurora.BrightBlue(domain))
}
}
func writeOutputToFile(filename string, output <-chan string, done chan<- bool) {
file, err := os.Create(filename)
if err != nil {
fmt.Printf("Error creating output file: %v\n", err)
done <- false
return
}
defer file.Close()
for line := range output {
_, err := file.WriteString(line)
if err != nil {
fmt.Printf("Error writing to output file: %v\n", err)
done <- false
return
}
}
done <- true
}
func displayHelp() {
helpText := `
Usage of Subdomain Scanner:
-f [file] File containing list of domains to scan
-d [domain] Single domain to scan
-o [file] Output file (default: output.txt)
-t [threads] Number of concurrent threads (default: 5)
Examples:
go run main.go -f list_domain.txt -t 10 -o output.txt
go run main.go -d xreverselabs.my.id -o output.txt
Credits : https://github.com/xReverselabs/SubRecon`
fmt.Println(helpText)
}
func main() {
// Load API key from config file
config, err := loadConfig()
if err != nil {
fmt.Printf("Error loading config: %v\n", err)
return
}
// Command-line flags
fileFlag := flag.String("f", "", "File containing list of domains")
domainFlag := flag.String("d", "", "Single domain to scan")
outputFlag := flag.String("o", "output.txt", "Output file")
threadsFlag := flag.Int("t", 5, "Number of threads")
helpFlag := flag.Bool("help", false, "Show help")
flag.Parse()
if *helpFlag {
displayHelp()
return
}
if *fileFlag == "" && *domainFlag == "" {
fmt.Println("Please specify either a file with -f or a single domain with -d\nOr use --help for more information")
return
}
var domains []string
// Handle domain list from file
if *fileFlag != "" {
file, err := os.Open(*fileFlag)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domains = append(domains, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
}
// Handle single domain
if *domainFlag != "" {
domains = append(domains, *domainFlag)
}
// Initialize fasthttp client
client := &fasthttp.Client{}
// Create channels and wait group
output := make(chan string, len(domains))
done := make(chan bool)
var wg sync.WaitGroup
// Write output to file asynchronously
go writeOutputToFile(*outputFlag, output, done)
// Start scanning with concurrency
semaphore := make(chan struct{}, *threadsFlag)
start := time.Now()
for _, domain := range domains {
wg.Add(1)
semaphore <- struct{}{}
go func(domain string) {
defer func() { <-semaphore }()
scanDomain(client, domain, config.APIKey, &wg, output)
}(domain)
}
wg.Wait()
close(output)
success := <-done
elapsed := time.Since(start)
if success {
fmt.Printf("\n%s Results saved to %s in %s\n", aurora.Green("[+]"), aurora.BrightGreen(*outputFlag), aurora.BrightYellow(elapsed.String()))
} else {
fmt.Println("Failed to save results.")
}
}