-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.go
More file actions
355 lines (325 loc) · 10.3 KB
/
Copy pathextractor.go
File metadata and controls
355 lines (325 loc) · 10.3 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
package main
import (
"net/url"
"regexp"
"strings"
"sync"
)
// Finding represents a single match with context
type Finding struct {
Pattern string `json:"pattern"`
Match string `json:"match"`
Category Category `json:"-"`
CatName string `json:"category"`
Severity Severity `json:"-"`
SevName string `json:"severity"`
Context string `json:"context,omitempty"`
}
// ScanResult holds all findings for a single JS file
type ScanResult struct {
URL string `json:"url"`
Secrets []Finding `json:"secrets,omitempty"`
Sinks []Finding `json:"sinks,omitempty"`
Sources []Finding `json:"sources,omitempty"`
Endpoints []Finding `json:"endpoints,omitempty"`
Subdomains []Finding `json:"subdomains,omitempty"`
Cloud []Finding `json:"cloud,omitempty"`
PII []Finding `json:"pii,omitempty"`
Frameworks []Finding `json:"frameworks,omitempty"`
SourceMaps []Finding `json:"sourcemaps,omitempty"`
Interesting []Finding `json:"interesting,omitempty"`
Error string `json:"error,omitempty"`
}
// TotalFindings returns the count of all findings
func (r *ScanResult) TotalFindings() int {
return len(r.Secrets) + len(r.Sinks) + len(r.Sources) + len(r.Endpoints) +
len(r.Subdomains) + len(r.Cloud) + len(r.PII) + len(r.Frameworks) +
len(r.SourceMaps) + len(r.Interesting)
}
// HasHighSeverity returns true if any critical/high finding exists
func (r *ScanResult) HasHighSeverity() bool {
for _, f := range r.Secrets {
if f.Severity <= SevHigh {
return true
}
}
for _, f := range r.Sinks {
if f.Severity <= SevHigh {
return true
}
}
for _, f := range r.Sources {
if f.Severity <= SevHigh {
return true
}
}
for _, f := range r.SourceMaps {
if f.Severity <= SevHigh {
return true
}
}
return false
}
// compiled regex cache
var (
compiledPatterns []compiledPattern
compiledPatternsOnce sync.Once
)
type compiledPattern struct {
Pattern
re *regexp.Regexp
}
func getCompiledPatterns() []compiledPattern {
compiledPatternsOnce.Do(func() {
for _, p := range AllPatterns() {
re, err := regexp.Compile(p.Regex)
if err != nil {
continue
}
compiledPatterns = append(compiledPatterns, compiledPattern{Pattern: p, re: re})
}
})
return compiledPatterns
}
// contextWindow controls how many characters of surrounding context to capture
const contextWindow = 80
// extractContext returns surrounding context for a match position
func extractContext(content string, matchStart, matchEnd int) string {
start := matchStart - contextWindow
if start < 0 {
start = 0
}
end := matchEnd + contextWindow
if end > len(content) {
end = len(content)
}
ctx := content[start:end]
// Clean up for display: replace newlines, collapse whitespace
ctx = strings.ReplaceAll(ctx, "\n", " ")
ctx = strings.ReplaceAll(ctx, "\r", "")
ctx = strings.ReplaceAll(ctx, "\t", " ")
return strings.TrimSpace(ctx)
}
// Extract runs all patterns against content and returns categorized results
func Extract(content, base, sourceURL, customRegex string, showContext bool) ScanResult {
seen := make(map[string]struct{})
result := ScanResult{URL: sourceURL}
// Custom regex mode — only run the user's regex
if customRegex != "" {
re, err := regexp.Compile(customRegex)
if err != nil {
result.Error = "invalid custom regex: " + err.Error()
return result
}
for _, loc := range re.FindAllStringIndex(content, -1) {
match := content[loc[0]:loc[1]]
if _, exists := seen[match]; exists {
continue
}
seen[match] = struct{}{}
f := Finding{
Pattern: "custom",
Match: match,
Category: CatEndpoint,
CatName: CatEndpoint.String(),
Severity: SevInfo,
SevName: SevInfo.String(),
}
if showContext {
f.Context = extractContext(content, loc[0], loc[1])
}
result.Endpoints = append(result.Endpoints, f)
}
return result
}
// Run all compiled patterns
for _, cp := range getCompiledPatterns() {
locs := cp.re.FindAllStringSubmatchIndex(content, -1)
for _, loc := range locs {
// Use first capture group if available, else full match
matchStart, matchEnd := loc[0], loc[1]
if len(loc) >= 4 && loc[2] >= 0 {
matchStart, matchEnd = loc[2], loc[3]
}
match := content[matchStart:matchEnd]
// Clean quotes
match = strings.Trim(match, "\"'`")
if len(match) < 4 {
continue
}
// Dedup
dedup := cp.Name + ":" + match
if _, ok := seen[dedup]; ok {
continue
}
seen[dedup] = struct{}{}
f := Finding{
Pattern: cp.Name,
Match: match,
Category: cp.Category,
CatName: cp.Category.String(),
Severity: cp.Severity,
SevName: cp.Severity.String(),
}
if showContext {
f.Context = extractContext(content, loc[0], loc[1])
}
// Resolve endpoints against base
if cp.Category == CatEndpoint {
if isFilteredEndpoint(match) {
continue
}
f.Match = resolveURL(match, base)
}
// Categorize into result
switch cp.Category {
case CatSecret:
result.Secrets = append(result.Secrets, f)
case CatSink:
result.Sinks = append(result.Sinks, f)
case CatSource:
result.Sources = append(result.Sources, f)
case CatEndpoint:
result.Endpoints = append(result.Endpoints, f)
case CatCloud:
result.Cloud = append(result.Cloud, f)
case CatPII:
result.PII = append(result.PII, f)
case CatFramework:
result.Frameworks = append(result.Frameworks, f)
case CatSourceMap:
result.SourceMaps = append(result.SourceMaps, f)
case CatInteresting:
result.Interesting = append(result.Interesting, f)
}
}
}
// Extract subdomains from content
extractSubdomains(content, sourceURL, &result, seen)
return result
}
// isFilteredEndpoint returns true for common false positive endpoints
func isFilteredEndpoint(path string) bool {
if strings.ContainsAny(path, " {}[]()<>;") {
return true
}
// Filter common JS/CSS noise
noise := []string{".js.map", ".css.map", ".woff", ".ttf", ".eot", ".png", ".jpg", ".gif", ".svg", ".ico"}
lower := strings.ToLower(path)
for _, n := range noise {
if strings.HasSuffix(lower, n) {
return true
}
}
return false
}
// resolveURL prepends base URL to relative paths
func resolveURL(path, base string) string {
if base == "" || strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "ws://") || strings.HasPrefix(path, "wss://") {
return path
}
parsedBase, err := url.Parse(base)
if err != nil {
return path
}
ref, err := url.Parse(path)
if err != nil {
return path
}
return parsedBase.ResolveReference(ref).String()
}
// extractSubdomains finds subdomains referenced in JS content
func extractSubdomains(content, sourceURL string, result *ScanResult, seen map[string]struct{}) {
// Get the base domain from source URL to find related subdomains
var baseDomain string
if parsed, err := url.Parse(sourceURL); err == nil && parsed.Host != "" {
parts := strings.Split(parsed.Hostname(), ".")
if len(parts) >= 2 {
baseDomain = parts[len(parts)-2] + "." + parts[len(parts)-1]
}
}
// Find FQDN-like strings — require at least 2 labels before TLD, each label 2+ chars
re := regexp.MustCompile(`(?:https?://)?([a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])*\.(?:com|net|org|io|co|dev|app|us|uk|de|fr|cn|ru|in|br|au|ca|nl|se|no|fi|dk|ch|at|be|es|it|pt|pl|cz|sk|hu|ro|bg|hr|rs|ua|lt|lv|ee|za|ng|ke|eg|il|ae|sa|jp|kr|tw|hk|sg|my|th|ph|id|vn|nz|mx|ar|cl|pe|gov|edu|mil|int|info|biz|name|pro|museum|coop|aero|xyz|online|site|tech|cloud|ai|me|tv|cc|ly|to|fm|am|gg|gl|sh|ws))\b`)
for _, match := range re.FindAllStringSubmatch(content, -1) {
domain := strings.ToLower(match[1])
// Skip if it doesn't look like a real domain
if !isLikelyDomain(domain) {
continue
}
// Skip common CDN/library domains unless they match target
if isCommonDomain(domain) && (baseDomain == "" || !strings.HasSuffix(domain, baseDomain)) {
continue
}
dedup := "subdomain:" + domain
if _, ok := seen[dedup]; ok {
continue
}
seen[dedup] = struct{}{}
sev := SevInfo
// Higher severity if it matches the target's base domain
if baseDomain != "" && strings.HasSuffix(domain, baseDomain) {
sev = SevLow
}
result.Subdomains = append(result.Subdomains, Finding{
Pattern: "subdomain",
Match: domain,
Category: CatSubdomain,
CatName: CatSubdomain.String(),
Severity: sev,
SevName: sev.String(),
})
}
}
// isLikelyDomain checks if the string looks like a real domain (not minified JS)
func isLikelyDomain(domain string) bool {
parts := strings.Split(domain, ".")
// Require at least a subdomain + domain + TLD (3 parts) OR 2 parts with meaningful labels
if len(parts) < 2 {
return false
}
// Reject very short domains (e.g., "ke.th")
if len(domain) < 6 {
return false
}
// Reject if any label is a common JS keyword/method
jsNoise := map[string]bool{
"this": true, "self": true, "window": true, "document": true,
"prototype": true, "constructor": true, "length": true, "name": true,
"type": true, "data": true, "value": true, "target": true,
"parent": true, "child": true, "node": true, "text": true,
"call": true, "apply": true, "bind": true, "push": true,
"slice": true, "concat": true, "join": true, "split": true,
"test": true, "exec": true, "match": true, "replace": true,
"trim": true, "keys": true, "each": true, "find": true,
"filter": true, "index": true, "first": true, "last": true,
"prev": true, "next": true, "body": true, "head": true,
"style": true, "class": true, "event": true, "error": true,
"state": true, "props": true, "cache": true, "queue": true,
}
for _, p := range parts[:len(parts)-1] { // Check all labels except TLD
if jsNoise[strings.ToLower(p)] {
return false
}
// Reject single-char labels (minified variable names)
if len(p) < 2 {
return false
}
}
return true
}
func isCommonDomain(domain string) bool {
common := []string{
"googleapis.com", "gstatic.com", "google.com", "facebook.com",
"fbcdn.net", "cloudflare.com", "cdnjs.cloudflare.com",
"jsdelivr.net", "unpkg.com", "bootstrapcdn.com",
"jquery.com", "w3.org", "schema.org", "mozilla.org",
"apple.com", "microsoft.com", "github.com", "npmjs.com",
"gravatar.com", "wordpress.com", "wp.com",
}
for _, c := range common {
if domain == c || strings.HasSuffix(domain, "."+c) {
return true
}
}
return false
}