-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsreader.go
More file actions
477 lines (418 loc) · 13.2 KB
/
jsreader.go
File metadata and controls
477 lines (418 loc) · 13.2 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
colorOrange = "\033[38;5;208m"
colorTeal = "\033[38;5;6m"
colorPink = "\033[38;5;13m"
colorMagenta = "\033[35m"
)
var (
threads int
inputFile string
jsFile string
pipeMode bool
outputFile string
)
type Result struct {
Type string
URL string
Source string
Details string
}
type SafePrinter struct {
mu sync.Mutex
fileMu sync.Mutex
outputFH *os.File
}
func (p *SafePrinter) PrintResult(result Result) {
p.mu.Lock()
defer p.mu.Unlock()
var color, prefix string
switch result.Type {
case "S3 Bucket":
color = colorRed
prefix = "S3 Bucket"
case "Firebase URL":
color = colorYellow
prefix = "Firebase DB"
case "Firebase Storage":
color = colorYellow
prefix = "Firebase Storage"
case "Firebase API":
color = colorYellow
prefix = "Firebase API"
case "API Endpoint":
color = colorGreen
prefix = "API"
case "GraphQL":
color = colorCyan
prefix = "GraphQL"
case "Auth Endpoint":
color = colorPurple
prefix = "Auth"
case "URL in variable":
color = colorBlue
prefix = "JS Variable"
case "Telegram Token":
color = colorOrange
prefix = "Telegram Bot"
case "API Subdomain":
color = colorTeal
prefix = "API Subdomain"
case "API Version":
color = colorPink
prefix = "API Version"
case "API Component":
color = colorMagenta
prefix = "API Component"
default:
color = colorWhite
prefix = result.Type
}
fmt.Printf("%s[%s]%s %s%s%s\n",
color, prefix, colorReset,
colorWhite, result.URL, colorReset)
if result.Details != "" {
fmt.Printf(" %sDetails:%s %s\n",
colorWhite, colorReset, result.Details)
}
if result.Source != "" {
fmt.Printf(" %sSource:%s %s\n\n",
colorWhite, colorReset, result.Source)
} else {
fmt.Println()
}
if p.outputFH != nil {
p.fileMu.Lock()
defer p.fileMu.Unlock()
entry := fmt.Sprintf("[%s] %s\n", prefix, result.URL)
if result.Details != "" {
entry += fmt.Sprintf("Details: %s\n", result.Details)
}
if result.Source != "" {
entry += fmt.Sprintf("Source: %s\n", result.Source)
}
entry += "\n"
_, err := p.outputFH.WriteString(entry)
if err != nil {
fmt.Printf("%s[ERROR]%s Failed to write to output file: %v\n", colorRed, colorReset, err)
}
}
}
func (p *SafePrinter) PrintStatus(msg string) {
p.mu.Lock()
defer p.mu.Unlock()
fmt.Printf("%s[STATUS]%s %s\n", colorBlue, colorReset, msg)
}
func (p *SafePrinter) PrintError(source, msg string) {
p.mu.Lock()
defer p.mu.Unlock()
fmt.Printf("%s[ERROR]%s %s - %s\n", colorRed, colorReset, source, msg)
}
func (p *SafePrinter) CloseOutput() {
if p.outputFH != nil {
p.outputFH.Close()
}
}
var printer = &SafePrinter{}
func main() {
flag.IntVar(&threads, "t", 1, "Number of threads to use")
flag.StringVar(&inputFile, "i", "", "Path to file with list of JS URLs (one per line)")
flag.StringVar(&jsFile, "f", "", "Path to single JS file to analyze")
flag.BoolVar(&pipeMode, "p", false, "Enable pipe mode (read from stdin)")
flag.StringVar(&outputFile, "o", "", "Output file to save results (.txt)")
flag.Parse()
if outputFile != "" {
fh, err := os.Create(outputFile)
if err != nil {
printer.PrintError("Output", fmt.Sprintf("Failed to create output file: %v", err))
os.Exit(1)
}
printer.outputFH = fh
defer printer.CloseOutput()
_, err = fh.WriteString("=== JS Parser Results ===\n\n")
if err != nil {
printer.PrintError("Output", fmt.Sprintf("Failed to write to output file: %v", err))
}
}
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
pipeMode = true
}
var jsFiles []string
switch {
case pipeMode:
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
jsFiles = append(jsFiles, strings.TrimSpace(scanner.Text()))
}
if err := scanner.Err(); err != nil {
printer.PrintError("stdin", fmt.Sprintf("Error reading from stdin: %v", err))
os.Exit(1)
}
case jsFile != "":
jsFiles = append(jsFiles, jsFile)
case inputFile != "":
file, err := os.Open(inputFile)
if err != nil {
printer.PrintError(inputFile, fmt.Sprintf("Error opening input file: %v", err))
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
jsFiles = append(jsFiles, strings.TrimSpace(scanner.Text()))
}
if err := scanner.Err(); err != nil {
printer.PrintError(inputFile, fmt.Sprintf("Error reading input file: %v", err))
os.Exit(1)
}
default:
printer.PrintError("Args", "You must specify input source (-f, -i, or pipe)")
flag.Usage()
os.Exit(1)
}
if len(jsFiles) == 0 {
printer.PrintError("Input", "No JS files to analyze")
os.Exit(1)
}
if !pipeMode {
printer.PrintStatus(fmt.Sprintf("Found %d JS files to analyze", len(jsFiles)))
printer.PrintStatus(fmt.Sprintf("Using %d threads", threads))
if outputFile != "" {
printer.PrintStatus(fmt.Sprintf("Saving results to: %s", outputFile))
}
}
workChan := make(chan string, len(jsFiles))
resultsChan := make(chan Result, 100)
var wg sync.WaitGroup
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for file := range workChan {
analyzeJSFile(file, resultsChan)
}
}()
}
go func() {
for result := range resultsChan {
printer.PrintResult(result)
}
}()
for _, file := range jsFiles {
workChan <- file
}
close(workChan)
wg.Wait()
close(resultsChan)
}
func analyzeJSFile(jsURL string, resultsChan chan<- Result) {
var content []byte
var err error
if strings.HasPrefix(jsURL, "http") {
if !pipeMode {
printer.PrintStatus(fmt.Sprintf("Fetching remote file: %s", jsURL))
}
content, err = fetchRemoteJS(jsURL)
} else {
if !pipeMode {
printer.PrintStatus(fmt.Sprintf("Reading local file: %s", jsURL))
}
content, err = os.ReadFile(jsURL)
}
if err != nil {
printer.PrintError(jsURL, err.Error())
return
}
if !pipeMode {
printer.PrintStatus(fmt.Sprintf("Analyzing %s (%d bytes)", jsURL, len(content)))
}
jsContent := string(content)
patterns := map[string]*regexp.Regexp{
"S3 Bucket": regexp.MustCompile(`https?://[a-zA-Z0-9.-]*\.?s3[.-][a-z0-9-]*\.amazonaws\.com[^\s"']*`),
"Firebase URL": regexp.MustCompile(`https?://[a-zA-Z0-9-]+\.firebaseio\.com[^\s"']*`),
"Firebase Storage": regexp.MustCompile(`https?://firebasestorage\.googleapis\.com[^\s"']*`),
"Firebase API": regexp.MustCompile(`https?://[a-zA-Z0-9-]+\.firebaseapp\.com[^\s"']*`),
"API Endpoint": regexp.MustCompile(`https?://[a-zA-Z0-9.-]+/(v[0-9]+/|api/)[a-zA-Z0-9./_-]*`),
"GraphQL": regexp.MustCompile(`https?://[a-zA-Z0-9.-]+/(graphql|gql)[^\s"']*`),
"Auth Endpoint": regexp.MustCompile(`https?://[a-zA-Z0-9.-]+/(auth|oauth|token|login|register|user|admin)[^\s"']*`),
"Telegram Token": regexp.MustCompile(`[0-9]{8,10}:[a-zA-Z0-9_-]{35}`),
"API Version": regexp.MustCompile(`["'](v[0-9]+(\.[0-9]+)?)["']`),
"API Subdomain": regexp.MustCompile(`https?://(api|api-[a-zA-Z0-9]+)\.([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}[^\s"']*`),
"API Component": regexp.MustCompile(`["'](/(api|rest|v[0-9]+)/[a-zA-Z0-9/_-]+)["']`),
}
excludePatterns := []*regexp.Regexp{
regexp.MustCompile(`w3\.org/`),
regexp.MustCompile(`schema\.org/`),
regexp.MustCompile(`\.min\.js`),
regexp.MustCompile(`://localhost`),
regexp.MustCompile(`://127\.0\.0\.1`),
}
found := make(map[string]bool)
var mu sync.Mutex
reportFinding := func(name, match, details string) {
for _, excludeRe := range excludePatterns {
if excludeRe.MatchString(strings.ToLower(match)) {
return
}
}
mu.Lock()
defer mu.Unlock()
if !found[match] {
resultsChan <- Result{
Type: name,
URL: match,
Source: jsURL,
Details: details,
}
found[match] = true
}
}
for name, re := range patterns {
matches := re.FindAllString(jsContent, -1)
for _, match := range matches {
details := ""
switch name {
case "S3 Bucket":
details = "Potential public S3 bucket - check permissions"
case "Firebase URL", "Firebase Storage", "Firebase API":
details = "Firebase service - check security rules"
case "Auth Endpoint":
details = "Authentication endpoint - check for vulnerabilities"
case "API Endpoint":
details = "API endpoint - investigate available methods"
case "Telegram Token":
details = "Telegram Bot API token - check if it's exposed"
case "API Version":
details = "API version identifier - may indicate available API versions"
case "API Subdomain":
details = "Dedicated API subdomain - investigate available endpoints"
case "API Component":
details = "API path component - may indicate service structure"
}
reportFinding(name, match, details)
}
}
apiVersionRe := regexp.MustCompile(`https?://[^/]+/v([0-9]+(\.[0-9]+)?)/`)
apiVersionMatches := apiVersionRe.FindAllStringSubmatch(jsContent, -1)
for _, match := range apiVersionMatches {
if len(match) > 1 {
versionStr := "v" + match[1]
details := "API version from URL path - investigate other available versions"
reportFinding("API Version", versionStr, details)
}
}
telegramContextRe := regexp.MustCompile(`(bot|token|api|key)[\s]*[=:][\s]*["']([0-9]{8,10}:[a-zA-Z0-9_-]{35})["']`)
telegramContextMatches := telegramContextRe.FindAllStringSubmatch(jsContent, -1)
for _, match := range telegramContextMatches {
if len(match) > 2 {
details := "Telegram Bot API token in a variable context - high confidence match"
reportFinding("Telegram Token", match[2], details)
}
}
variablePatterns := []*regexp.Regexp{
regexp.MustCompile(`const\s+[a-zA-Z0-9_]+\s*=\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`let\s+[a-zA-Z0-9_]+\s*=\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`var\s+[a-zA-Z0-9_]+\s*=\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`[a-zA-Z0-9_]+\s*:\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`(url|endpoint|api|baseUrl|apiUrl|baseURL|apiURL)\s*[=:]\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`(url|endpoint|api|baseUrl|apiUrl|baseURL|apiURL)\s*[=:]\s*["'](\/[^"'\s]+)["']`),
}
for _, re := range variablePatterns {
matches := re.FindAllStringSubmatch(jsContent, -1)
for _, match := range matches {
if len(match) > 1 {
reportFinding("URL in variable", match[1], "Found in JavaScript variable - may contain sensitive API URLs")
}
}
}
apiObjectPatterns := []*regexp.Regexp{
regexp.MustCompile(`endpoints\s*:\s*\{\s*[^}]*["']([^"']+)["']\s*:\s*["']([^"']+)["']`),
regexp.MustCompile(`api\s*:\s*\{\s*[^}]*["']([^"']+)["']\s*:\s*["']([^"']+)["']`),
regexp.MustCompile(`routes\s*:\s*\{\s*[^}]*["']([^"']+)["']\s*:\s*["']([^"']+)["']`),
}
for _, re := range apiObjectPatterns {
matches := re.FindAllStringSubmatch(jsContent, -1)
for _, match := range matches {
if len(match) > 2 {
endpoint := match[2]
if strings.HasPrefix(endpoint, "http") {
reportFinding("API Component", endpoint, "API endpoint found in object definition - "+match[1])
} else if strings.HasPrefix(endpoint, "/") {
reportFinding("API Component", endpoint, "API path found in object definition - "+match[1])
}
}
}
}
apiVersionCommentRe := regexp.MustCompile(`\/\/.*\b(v[0-9]+(\.[0-9]+)?)\b.*api`)
apiVersionCommentMatches := apiVersionCommentRe.FindAllStringSubmatch(jsContent, -1)
for _, match := range apiVersionCommentMatches {
if len(match) > 1 {
reportFinding("API Version", match[1], "API version mentioned in code comment")
}
}
apiCallPatterns := []*regexp.Regexp{
regexp.MustCompile(`(fetch|axios\.get|axios\.post|ajax|request)\s*\(\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`\.(get|post|put|delete|patch)\s*\(\s*["'](https?://[^"'\s]+)["']`),
regexp.MustCompile(`\.(get|post|put|delete|patch)\s*\(\s*["'](\/[^"'\s]+)["']`),
}
for _, re := range apiCallPatterns {
matches := re.FindAllStringSubmatch(jsContent, -1)
for _, match := range matches {
if len(match) > 2 {
endpoint := match[2]
if strings.Contains(endpoint, "/api/") ||
strings.Contains(endpoint, "/v1/") ||
strings.Contains(endpoint, "/v2/") ||
regexp.MustCompile(`/v[0-9]+/`).MatchString(endpoint) {
reportFinding("API Endpoint", endpoint, "API endpoint found in "+match[1]+" call")
} else if strings.HasPrefix(endpoint, "http") {
reportFinding("URL in variable", endpoint, "URL found in "+match[1]+" call")
}
}
}
}
}
func fetchRemoteJS(jsURL string) ([]byte, error) {
client := &http.Client{
Timeout: 15 * time.Second,
}
req, err := http.NewRequest("GET", jsURL, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error fetching JS file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading JS file: %w", err)
}
return content, nil
}