-
Notifications
You must be signed in to change notification settings - Fork 477
/
main.go
297 lines (230 loc) · 5.32 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
func main() {
var domains []string
var dates bool
flag.BoolVar(&dates, "dates", false, "show date of fetch in the first column")
var noSubs bool
flag.BoolVar(&noSubs, "no-subs", false, "don't include subdomains of the target domain")
var getVersionsFlag bool
flag.BoolVar(&getVersionsFlag, "get-versions", false, "list URLs for crawled versions of input URL(s)")
flag.Parse()
if flag.NArg() > 0 {
// fetch for a single domain
domains = []string{flag.Arg(0)}
} else {
// fetch for all domains from stdin
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
domains = append(domains, sc.Text())
}
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
}
// get-versions mode
if getVersionsFlag {
for _, u := range domains {
versions, err := getVersions(u)
if err != nil {
continue
}
fmt.Println(strings.Join(versions, "\n"))
}
return
}
fetchFns := []fetchFn{
getWaybackURLs,
getCommonCrawlURLs,
getVirusTotalURLs,
}
for _, domain := range domains {
var wg sync.WaitGroup
wurls := make(chan wurl)
for _, fn := range fetchFns {
wg.Add(1)
fetch := fn
go func() {
defer wg.Done()
resp, err := fetch(domain, noSubs)
if err != nil {
return
}
for _, r := range resp {
if noSubs && isSubdomain(r.url, domain) {
continue
}
wurls <- r
}
}()
}
go func() {
wg.Wait()
close(wurls)
}()
seen := make(map[string]bool)
for w := range wurls {
if _, ok := seen[w.url]; ok {
continue
}
seen[w.url] = true
if dates {
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)
}
fmt.Printf("%s %s\n", d.Format(time.RFC3339), w.url)
} else {
fmt.Println(w.url)
}
}
}
}
type wurl struct {
date string
url string
}
type fetchFn func(string, bool) ([]wurl, error)
func getWaybackURLs(domain string, noSubs bool) ([]wurl, error) {
subsWildcard := "*."
if noSubs {
subsWildcard = ""
}
res, err := http.Get(
fmt.Sprintf("http://web.archive.org/cdx/search/cdx?url=%s%s/*&output=json&collapse=urlkey", subsWildcard, domain),
)
if err != nil {
return []wurl{}, err
}
raw, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return []wurl{}, err
}
var wrapper [][]string
err = json.Unmarshal(raw, &wrapper)
out := make([]wurl, 0, len(wrapper))
skip := true
for _, urls := range wrapper {
// The first item is always just the string "original",
// so we should skip the first item
if skip {
skip = false
continue
}
out = append(out, wurl{date: urls[1], url: urls[2]})
}
return out, nil
}
func getCommonCrawlURLs(domain string, noSubs bool) ([]wurl, error) {
subsWildcard := "*."
if noSubs {
subsWildcard = ""
}
res, err := http.Get(
fmt.Sprintf("http://index.commoncrawl.org/CC-MAIN-2018-22-index?url=%s%s/*&output=json", subsWildcard, domain),
)
if err != nil {
return []wurl{}, err
}
defer res.Body.Close()
sc := bufio.NewScanner(res.Body)
out := make([]wurl, 0)
for sc.Scan() {
wrapper := struct {
URL string `json:"url"`
Timestamp string `json:"timestamp"`
}{}
err = json.Unmarshal([]byte(sc.Text()), &wrapper)
if err != nil {
continue
}
out = append(out, wurl{date: wrapper.Timestamp, url: wrapper.URL})
}
return out, nil
}
func getVirusTotalURLs(domain string, noSubs bool) ([]wurl, error) {
out := make([]wurl, 0)
apiKey := os.Getenv("VT_API_KEY")
if apiKey == "" {
// no API key isn't an error,
// just don't fetch
return out, nil
}
fetchURL := fmt.Sprintf(
"https://www.virustotal.com/vtapi/v2/domain/report?apikey=%s&domain=%s",
apiKey,
domain,
)
resp, err := http.Get(fetchURL)
if err != nil {
return out, err
}
defer resp.Body.Close()
wrapper := struct {
URLs []struct {
URL string `json:"url"`
// TODO: handle VT date format (2018-03-26 09:22:43)
//Date string `json:"scan_date"`
} `json:"detected_urls"`
}{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&wrapper)
for _, u := range wrapper.URLs {
out = append(out, wurl{url: u.URL})
}
return out, nil
}
func isSubdomain(rawUrl, domain string) bool {
u, err := url.Parse(rawUrl)
if err != nil {
// we can't parse the URL so just
// err on the side of including it in output
return false
}
return strings.ToLower(u.Hostname()) != strings.ToLower(domain)
}
func getVersions(u string) ([]string, error) {
out := make([]string, 0)
resp, err := http.Get(fmt.Sprintf(
"http://web.archive.org/cdx/search/cdx?url=%s&output=json", u,
))
if err != nil {
return out, err
}
defer resp.Body.Close()
r := [][]string{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&r)
if err != nil {
return out, err
}
first := true
seen := make(map[string]bool)
for _, s := range r {
// skip the first element, it's the field names
if first {
first = false
continue
}
// fields: "urlkey", "timestamp", "original", "mimetype", "statuscode", "digest", "length"
if seen[s[5]] {
continue
}
seen[s[5]] = true
out = append(out, fmt.Sprintf("https://web.archive.org/web/%sif_/%s", s[1], s[2]))
}
return out, nil
}