-
Notifications
You must be signed in to change notification settings - Fork 475
/
wbk.go
186 lines (149 loc) · 4.16 KB
/
wbk.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
package main
/*
** name: wbk (waybackurls)
** cred: https://github.com/tomnomnom/hacks/tree/master/waybackurls
** docs: https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server#url-match-scope
** m4ll0k (github.com/m4ll0k)
DOC LINK: https://bit.ly/2UCEIyH
main features compared to gau / waybackurls:
filtring (https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server#filtering):
Date Range:
E.g:
$ wbk -fromto "2010-2015" paypal.com
Regex filtering
E.g:
$ wbk -filter "statuscode:200,mimetype:application/json" paypal.com # show only urls with statuscode 200 and mimetype application/json
$ wbk -filter "\!statuscode:200,\!mimetype:application/json" paypal.com # not show urls with statuscode 200 and mimetype application/json
url match scope (https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server#url-match-scope)
E.g:
$ wbk -match "exact" paypal.com/signin
$ wbk -match "host" paypal.com
$ ...
inputs
E.g:
$ cat mydomains.txt | wbk ... # stdin
$ wbk ... mydomains.txt # file
$ wbk ... mydomain.com # string
*/
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
const VERSION = "v0.1 (beta)"
const fetchCDX = "http://web.archive.org/cdx/search/cdx?url=*.%s/*&output=json&fl=original&collapse=urlkey"
var (
_fromto = flag.String("fromto", "", "\nresults filtered by timestamp using from= and to= params\nE.g:\n-fromto \"2012-2015\"\n")
_filter = flag.String("filter", "", "\nfilter:\n\tmimetype:\tE.g: mimetype:application/json\n\tstatuscode:\tE.g: statuscode:200\nE.g:\n-filter \"mimetype:application/json\"\n")
_match = flag.String("match", "", "\n\texact\n\tprefix\n\thost\n\tdomain\n")
_version = flag.Bool("version", false, "show version and exit\n\nDoc link: https://bit.ly/2UCEIyH\n")
)
func main() {
// main
var domains []string
var targets []string
matchTypies := map[string]bool{
"exact": true,
"prefix": true,
"host": true,
"domain": true,
}
filterTypies := map[string]bool{
"statuscode": true,
"mimetype": true,
"!statuscode": true,
"!mimetype": true,
"digest": true,
"!digest": true,
}
fetchCDXS := ""
flag.Parse()
filter := *_filter
fromto := *_fromto
match := *_match
if *_version {
fmt.Fprint(os.Stdout, "WBK version: ", VERSION, "\n")
os.Exit(0)
}
if flag.NArg() > 0 {
domains = []string{flag.Arg(0)}
_, b := os.Stat(domains[0])
if b != nil {
targets = domains
} else /* read file */ {
file, err := os.Open(domains[0])
if err != nil {
fmt.Fprint(os.Stderr, "Error: ", err, "\n")
}
read_file := bufio.NewScanner(file)
for read_file.Scan() {
targets = append(targets, read_file.Text())
}
}
} else /* read from stdin */ {
stdin_input := bufio.NewScanner(os.Stdin)
for stdin_input.Scan() {
targets = append(targets, stdin_input.Text())
}
if err := stdin_input.Err(); err != nil {
fmt.Fprint(os.Stderr, "", err, "\n")
}
}
fetchCDXS = fetchCDX
if strings.Contains(fromto, "-") {
fetchCDXS = fetchCDXS + "&from=" + strings.Split(fromto, "-")[0] + "&to=" + strings.Split(fromto, "-")[1]
}
if filter != "" {
filters := strings.Split(filter, ",")
params := ""
for _, v := range filters {
if filterTypies[strings.Split(v, ":")[0]] {
params = params + "&filter=" + v
}
}
fetchCDXS = fetchCDXS + params
}
if matchTypies[match] {
fetchCDXS = fetchCDXS + "&matchType=" + match
}
for _, domain := range targets {
urls, err := getContent(fetchCDXS, domain)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to fetch URLs\n")
continue
}
for _, url := range urls {
fmt.Println(url)
}
}
}
func getContent(fetchCDXS string, domain string) ([]string, error) {
// get content
fetch := fmt.Sprintf(fetchCDXS, domain)
out := make([]string, 0)
res, err := http.Get(fetch)
if err != nil {
return out, err
}
raw, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return out, err
}
var wrapper [][]string
err = json.Unmarshal(raw, &wrapper)
skip := true
for _, urls := range wrapper {
if skip {
skip = false
continue
}
out = append(out, urls...)
}
return out, nil
}