-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
372 lines (327 loc) · 10 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
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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
)
// WebURL represents a web URL with an optional date
type WebURL struct {
Date string
URL string
}
// fetchFunction defines the signature for fetcher functions
type fetchFunction func(string, bool, string) ([]WebURL, error)
func main() {
// Parse command-line flags
targetFlag := flag.String("t", "", "Target domain or file with list of domains")
outputFileFlag := flag.String("o", "", "Output file to write results (default: stdout)")
showDatesFlag := flag.Bool("d", false, "Show date of fetch in the first column")
noSubdomainsFlag := flag.Bool("n", false, "Don't include subdomains of the target domain")
getVersionsFlag := flag.Bool("v", false, "List URLs for crawled versions of input URL(s)")
virusTotalAPIKeyFlag := flag.String("vt", "", "VirusTotal API key for additional URL fetching")
flag.Usage = customUsage
flag.Parse()
// Validate input
if *targetFlag == "" && flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "Error: either -t or a domain argument is required\n")
flag.Usage()
os.Exit(1)
}
// Get list of domains to process
domains, err := getDomains(*targetFlag, flag.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// If -v flag is set, get versions of the URLs
if *getVersionsFlag {
if err := getVersionURLs(domains); err != nil {
fmt.Fprintf(os.Stderr, "Error getting version URLs: %v\n", err)
os.Exit(1)
}
return
}
// Fetch URLs using the specified fetch functions
results, errs := fetchURLs(domains, *noSubdomainsFlag, *virusTotalAPIKeyFlag)
// Handle any errors encountered during fetching
if len(errs) > 0 {
fmt.Fprintf(os.Stderr, "Encountered %d errors while fetching URLs:\n", len(errs))
for _, err := range errs {
fmt.Fprintf(os.Stderr, " - %v\n", err)
}
if len(results) == 0 {
os.Exit(1)
}
}
// Write the fetched URLs to the output
if err := writeOutput(results, *outputFileFlag, *showDatesFlag); err != nil {
fmt.Fprintf(os.Stderr, "Error writing output: %v\n", err)
os.Exit(1)
}
}
// customUsage defines the usage message for the program
func customUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [DOMAIN...]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "A web crawler inspired by WayBackURL by @tomnomnom.\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, " -d Show fetch date in first column\n")
fmt.Fprintf(os.Stderr, " -t <target> Target domain or file with list of domains\n")
fmt.Fprintf(os.Stderr, " -n Exclude subdomains\n")
fmt.Fprintf(os.Stderr, " -o <file> Output file (default: stdout)\n")
fmt.Fprintf(os.Stderr, " -v List crawled URL versions\n")
fmt.Fprintf(os.Stderr, " -vt <key> VirusTotal API key\n")
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s example.com\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s -t domains.txt -o results.txt\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s -d -n -t example.com\n", os.Args[0])
}
// getDomains retrieves domains from a file or command-line arguments
func getDomains(target string, args []string) ([]string, error) {
if target != "" {
if isDomain(target) {
return []string{target}, nil
}
return readDomainsFromFile(target)
}
return args, nil
}
// isDomain checks if a string is a valid domain
func isDomain(s string) bool {
return strings.Contains(s, ".") && !strings.Contains(s, "/")
}
// readDomainsFromFile reads domains from a file, one per line
func readDomainsFromFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
var domains []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain != "" {
domains = append(domains, domain)
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
return domains, nil
}
// fetchURLs fetches URLs from various sources concurrently
func fetchURLs(domains []string, noSubdomains bool, virusTotalAPIKey string) ([]WebURL, []error) {
fetchFunctions := []fetchFunction{
getWaybackURLs,
getCommonCrawlURLs,
}
// Include VirusTotal fetcher if API key is provided
if virusTotalAPIKey != "" {
fetchFunctions = append(fetchFunctions, getVirusTotalURLs)
}
var results []WebURL
var errs []error
var wg sync.WaitGroup
resultsChan := make(chan []WebURL)
errorsChan := make(chan error)
// Use a worker group to fetch URLs concurrently
for _, domain := range domains {
for _, fn := range fetchFunctions {
wg.Add(1)
go func(d string, f fetchFunction) {
defer wg.Done()
resp, err := f(d, noSubdomains, virusTotalAPIKey)
if err != nil {
errorsChan <- fmt.Errorf("error fetching URLs for %s: %v", d, err)
return
}
resultsChan <- resp
}(domain, fn)
}
}
// Close channels when all fetchers are done
go func() {
wg.Wait()
close(resultsChan)
close(errorsChan)
}()
// Collect results and errors
for resp := range resultsChan {
results = append(results, resp...)
}
for err := range errorsChan {
errs = append(errs, err)
}
return results, errs
}
// writeOutput writes the fetched URLs to the specified output
func writeOutput(results []WebURL, outputFile string, showDates bool) error {
var writer io.Writer = os.Stdout
if outputFile != "" {
file, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("error opening output file: %v", err)
}
defer file.Close()
writer = file
}
for _, w := range results {
if showDates && w.Date != "" {
d, err := time.Parse("20060102150405", w.Date)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse date [%s] for URL [%s]\n", w.Date, w.URL)
continue
}
if _, err := fmt.Fprintf(writer, "%s %s\n", d.Format(time.RFC3339), w.URL); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
} else {
if _, err := fmt.Fprintln(writer, w.URL); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
}
}
return nil
}
// getVersionURLs retrieves different versions of URLs from the Wayback Machine
func getVersionURLs(domains []string) error {
for _, u := range domains {
versions, err := getVersions(u)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting versions for %s: %v\n", u, err)
continue
}
for _, v := range versions {
fmt.Println(v)
}
}
return nil
}
// getVersions fetches different archived versions of a URL
func getVersions(u string) ([]string, error) {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(fmt.Sprintf(
"http://web.archive.org/cdx/search/cdx?url=%s&output=json", u,
))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var records [][]string
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&records); err != nil {
return nil, err
}
var out []string
seen := make(map[string]bool)
for i, record := range records {
if i == 0 {
continue // Skip header
}
if len(record) < 6 {
continue // Ensure record has enough fields
}
digest := record[5]
if seen[digest] {
continue
}
seen[digest] = true
timestamp, originalURL := record[1], record[2]
out = append(out, fmt.Sprintf("https://web.archive.org/web/%sif_/%s", timestamp, originalURL))
}
return out, nil
}
// Fetcher functions
// getWaybackURLs fetches URLs from the Wayback Machine
func getWaybackURLs(domain string, noSubdomains bool, _ string) ([]WebURL, error) {
prefix := "*."
if noSubdomains {
prefix = ""
}
client := &http.Client{Timeout: 30 * time.Second}
url := fmt.Sprintf("http://web.archive.org/cdx/search/cdx?url=%s%s/*&output=json&collapse=urlkey", prefix, domain)
res, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("error fetching from Wayback Machine: %v", err)
}
defer res.Body.Close()
var records [][]string
if err := json.NewDecoder(res.Body).Decode(&records); err != nil {
return nil, fmt.Errorf("error parsing Wayback Machine JSON: %v", err)
}
var out []WebURL
for i, record := range records {
if i == 0 || len(record) < 3 {
continue // Skip header or incomplete records
}
out = append(out, WebURL{Date: record[1], URL: record[2]})
}
return out, nil
}
// getCommonCrawlURLs fetches URLs from the Common Crawl index
func getCommonCrawlURLs(domain string, noSubdomains bool, _ string) ([]WebURL, error) {
prefix := "*."
if noSubdomains {
prefix = ""
}
client := &http.Client{Timeout: 30 * time.Second}
url := fmt.Sprintf("http://index.commoncrawl.org/CC-MAIN-2018-22-index?url=%s%s/*&output=json", prefix, domain)
res, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("error fetching from Common Crawl: %v", err)
}
defer res.Body.Close()
scanner := bufio.NewScanner(res.Body)
var out []WebURL
for scanner.Scan() {
var record struct {
URL string `json:"url"`
Timestamp string `json:"timestamp"`
}
if err := json.Unmarshal(scanner.Bytes(), &record); err != nil {
continue
}
out = append(out, WebURL{Date: record.Timestamp, URL: record.URL})
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading Common Crawl response: %v", err)
}
return out, nil
}
// getVirusTotalURLs fetches URLs from VirusTotal if an API key is provided
func getVirusTotalURLs(domain string, noSubdomains bool, apiKey string) ([]WebURL, error) {
if apiKey == "" {
return nil, nil
}
client := &http.Client{Timeout: 30 * time.Second}
url := fmt.Sprintf(
"https://www.virustotal.com/vtapi/v2/domain/report?apikey=%s&domain=%s",
apiKey,
domain,
)
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("error fetching from VirusTotal: %v", err)
}
defer resp.Body.Close()
var data struct {
URLs []struct {
URL string `json:"url"`
} `json:"detected_urls"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, fmt.Errorf("error parsing VirusTotal JSON: %v", err)
}
var out []WebURL
for _, u := range data.URLs {
out = append(out, WebURL{URL: u.URL})
}
return out, nil
}