forked from 003random/getJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
298 lines (251 loc) · 6.95 KB
/
main.go
File metadata and controls
298 lines (251 loc) · 6.95 KB
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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/logrusorgru/aurora"
flag "github.com/spf13/pflag"
)
type logger interface {
Log(msg string)
Error(msg string, err error)
}
type silent struct{}
func (s silent) Log(msg string) {
}
func (s silent) Error(msg string, err error) {
}
type verbose struct {
}
func (v verbose) Log(msg string) {
fmt.Println(au.Cyan(msg))
}
func Log(l logger, msg string) {
l.Log(msg)
}
func (v verbose) Error(msg string, err error) {
fmt.Fprintln(os.Stderr, au.Red(msg))
if err != nil {
fmt.Fprintln(os.Stderr, au.Red("[!] Error: "), au.Red(err))
}
}
func Error(l logger, msg string, err error) {
l.Error(msg, err)
}
var output logger
var au aurora.Aurora
func main() {
urlArg := flag.String("url", "", "The url to get the javascript sources from")
methodArg := flag.String("method", "GET", "The request method. e.g. GET or POST")
outputFileArg := flag.String("output", "", "Output file to save the results to")
inputFileArg := flag.String("input", "", "Input file with urls")
resolveArg := flag.Bool("resolve", false, "Output only existing files")
completeArg := flag.Bool("complete", false, "Complete the url. e.g. append the domain to the path")
verboseArg := flag.Bool("verbose", false, "Display info of what is going on")
noColorsArg := flag.Bool("nocolors", false, "Enable or disable colors")
HeaderArg := flag.StringArrayP("header", "H", nil, "Any HTTP headers(-H \"Authorization:Bearer token\")")
insecureArg := flag.Bool("insecure", false, "Check the SSL security checks. Use when the certificate is expired or invalid")
timeoutArg := flag.Int("timeout", 10, "Max timeout for the requests")
flag.Parse()
au = aurora.NewAurora(!*noColorsArg)
var urls []string
var allSources []string
output = silent{}
if *verboseArg {
output = verbose{}
}
stat, err := os.Stdin.Stat()
if err != nil {
output.Error("[!] Couldnt read Stdin", err)
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
if err := scanner.Err(); err != nil {
output.Error("[!] Couldnt read Stdin", err)
}
if len(urls) > 0 {
output.Log("[+] Received urls from Stdin")
}
}
if *inputFileArg != "" {
f, err := os.Open(*inputFileArg)
if err != nil {
output.Error("[!] Couldn't open input file", err)
return
}
defer f.Close()
lines, err := readLines(f)
if err != nil {
output.Error("[!] Couldn't read from input file", err)
}
output.Log("[+] Set url file to " + *inputFileArg)
urls = append(urls, lines...)
}
if *urlArg != "" {
output.Log(fmt.Sprintf("[+] Set url to %s", *urlArg))
urls = append(urls, *urlArg)
}
if len(urls) == 0 {
output.Error("[!] No urls supplied", nil)
os.Exit(3)
}
if *resolveArg && !*completeArg {
output.Error("[!] Resolve can only be used in combination with -complete", nil)
os.Exit(3)
}
for _, e := range urls {
var sourcesBak []string
var completedSuccessfully = true
output.Log("[+] Getting sources from " + e)
sources, err := getScriptSrc(e, *methodArg, *HeaderArg, *insecureArg, *timeoutArg)
if err != nil {
output.Error(fmt.Sprintf("[!] Couldn't get sources from %s", e), err)
}
if *completeArg {
output.Log("[+] Completing URLs")
sourcesBak = sources
sources, err = completeUrls(sources, e)
if err != nil {
output.Error("[!] Couldn't complete URLs", err)
sources = sourcesBak
completedSuccessfully = false
}
}
if *resolveArg && *completeArg {
if completedSuccessfully {
output.Log("[+] Resolving files")
sourcesBak = sources
sources, err = resolveUrls(sources)
if err != nil {
output.Error("[!] Couldn't resolve URLs", err)
sources = sourcesBak
}
} else {
output.Error("[!] Couldn't resolve URLs", nil)
}
} else if *resolveArg {
output.Error("[!] Resolve can only be used in combination with -complete", nil)
}
for _, i := range sources {
fmt.Println(i)
}
if *outputFileArg != "" {
allSources = append(allSources, sources...)
}
}
// Save to file
if *outputFileArg != "" {
output.Log(fmt.Sprintf("[+] Saving output to %s", *outputFileArg))
err := saveToFile(allSources, *outputFileArg)
if err != nil {
output.Error(fmt.Sprintf("[!] Couldn't save to output file %s", *outputFileArg), err)
}
}
}
func saveToFile(sources []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range sources {
fmt.Fprintln(w, line)
}
return w.Flush()
}
func getScriptSrc(url string, method string, headers []string, insecure bool, timeout int) ([]string, error) {
// Request the HTML page.
req, err := http.NewRequest(method, url, nil)
if err != nil {
return []string{}, err
}
for _, d := range headers {
values := strings.Split(d, ":")
if len(values) == 2 {
output.Log("[+] New Header: " + values[0] + ": " + values[1])
req.Header.Set(values[0], values[1])
}
}
tr := &http.Transport{
ResponseHeaderTimeout: time.Duration(time.Duration(timeout) * time.Second),
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
var client = &http.Client{
Timeout: time.Duration(time.Duration(timeout) * time.Second),
Transport: tr,
}
res, err := client.Do(req)
if err != nil {
return []string{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
output.Error(fmt.Sprintf("[!] %s returned an %d instead of %d", url, res.StatusCode, http.StatusOK), nil)
return nil, nil
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
var sources []string
// Find the script tags, and get the src
doc.Find("script").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
dsrc, _ := s.Attr("data-src")
if src != "" {
sources = append(sources, src)
}
if dsrc != "" {
sources = append(sources, dsrc)
}
})
return sources, nil
}
func readLines(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func resolveUrls(s []string) ([]string, error) {
for i := len(s) - 1; i >= 0; i-- {
resp, err := http.Get(s[i])
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 304 {
s = append(s[:i], s[i+1:]...)
}
}
return s, nil
}
func completeUrls(s []string, mainUrl string) ([]string, error) {
u, err := url.Parse(mainUrl)
if err != nil {
return nil, err
}
for i := range s {
if strings.HasPrefix(s[i], "//") {
s[i] = u.Scheme + ":" + s[i]
} else if strings.HasPrefix(s[i], "/") && string(s[i][1]) != "/" {
s[i] = u.Scheme + "://" + u.Host + s[i]
} else if !strings.HasPrefix(s[i], "http://") && !strings.HasPrefix(s[i], "https://") {
s[i] = u.Scheme + "://" + u.Host + u.Path + "/" + s[i]
}
}
return s, nil
}