-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriya.go
More file actions
401 lines (354 loc) · 11.1 KB
/
Copy pathriya.go
File metadata and controls
401 lines (354 loc) · 11.1 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
type Category struct {
Description string `yaml:"description"`
Patterns []string `yaml:"patterns"`
Examples []string `yaml:"examples"`
}
type PatternFile struct {
Categories map[string]Category `yaml:"categories"`
}
var (
flagHelp = flag.Bool("h", false, "Show help/usage")
flagList = flag.Bool("list", false, "List all patterns for selected categories")
flagNoColor = flag.Bool("no-color", false, "Disable colored output")
flagAll = flag.Bool("a", false, "Match all sensitive files/paths (default if no flags given)")
flagSQL = flag.Bool("s", false, "Match SQL/database files")
flagGraphQL = flag.Bool("g", false, "Match GraphQL endpoints")
flagPHP = flag.Bool("p", false, "Match PHP source/config files")
flagBackup = flag.Bool("b", false, "Match backup and temporary files")
flagConfig = flag.Bool("c", false, "Match config and environment files")
flagJSJSON = flag.Bool("j", false, "Match JavaScript and JSON files")
flagLogs = flag.Bool("l", false, "Match log and text files")
flagCerts = flag.Bool("k", false, "Match certificate and key files")
flagFramework = flag.Bool("f", false, "Match framework-specific files")
flagVCS = flag.Bool("v", false, "Match version control files")
flagArchive = flag.Bool("x", false, "Match archive/compressed files")
flagCloudInfra = flag.Bool("d", false, "Match cloud and infrastructure config")
flagAuthAdmin = flag.Bool("m", false, "Match admin and authentication paths")
flagDirs = flag.Bool("r", false, "Match sensitive directories")
flagMisc = flag.Bool("misc", false, "Match miscellaneous sensitive files")
// New feature flags
flagExclude = flag.String("exc", "", "Exclude patterns (comma-separated extensions/patterns)")
flagInclude = flag.String("inc", "", "Include only these patterns (comma-separated)")
flagOutputFile = flag.String("o", "", "Output results to file (e.g., results.txt)")
flagUnique = flag.Bool("u", false, "Remove duplicate URLs (show each URL only once)")
flagStats = flag.Bool("stats", false, "Show statistics summary instead of URLs")
)
var categoryColors = map[string]string{
"s": "\033[31m",
"g": "\033[35m",
"p": "\033[34m",
"b": "\033[33m",
"c": "\033[36m",
"j": "\033[38;5;220m",
"l": "\033[38;5;130m",
"k": "\033[38;5;205m",
"f": "\033[34m",
"v": "\033[38;5;208m",
"x": "\033[32m",
"d": "\033[38;5;51m",
"m": "\033[38;5;118m",
"r": "\033[38;5;183m",
"misc": "\033[37m",
}
const colorReset = "\033[0m"
var categoryPriority = map[string]int{
"x": 1, "v": 2, "f": 3, "j": 4, "l": 5, "b": 6, "p": 7, "s": 8,
"c": 9, "g": 10, "d": 11, "m": 12, "r": 13, "misc": 14, "k": 15,
}
func printHelp() {
flags := []struct {
flag string
desc string
colorKey string
}{
{"-h", "Show this help message", ""},
{"-list", "List all known patterns for selected categories", ""},
{"-no-color", "Disable colored output", ""},
{"-a", "Match all sensitive files and paths (default if no flags given)", ""},
{"-s", "Match SQL/database backup files", "s"},
{"-g", "Match GraphQL endpoints", "g"},
{"-p", "Match PHP source and config files", "p"},
{"-b", "Match backup and temporary files", "b"},
{"-c", "Match config and environment files", "c"},
{"-j", "Match JavaScript and JSON files", "j"},
{"-l", "Match log and text files", "l"},
{"-k", "Match certificate and key files", "k"},
{"-f", "Match framework-specific files", "f"},
{"-v", "Match version control files", "v"},
{"-x", "Match archive/compressed files", "x"},
{"-d", "Match cloud and infrastructure config files", "d"},
{"-m", "Match admin and authentication paths", "m"},
{"-r", "Match sensitive directories", "r"},
{"-misc", "Match miscellaneous sensitive files", "misc"},
}
fmt.Println("Usage: riya [flags]")
fmt.Println()
fmt.Println("Category Flags:")
for _, f := range flags {
colorCode := ""
if c, ok := categoryColors[f.colorKey]; ok && !*flagNoColor {
colorCode = c
}
flagText := f.flag
if colorCode != "" {
flagText = colorCode + flagText + colorReset
}
fmt.Printf(" %-15s %s\n", flagText, f.desc)
}
fmt.Println()
fmt.Println("Filter & Output Flags:")
fmt.Println(" -exc <list> Exclude patterns (comma-separated: js,json,png)")
fmt.Println(" -inc <list> Include only these patterns (comma-separated)")
fmt.Println(" -u Remove duplicate URLs")
fmt.Println(" -stats Show match statistics instead of URLs")
fmt.Println(" -o <file> Save output to file (e.g., results.txt)")
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" cat urls.txt | riya -s -p")
fmt.Println(" waybackurls target.com | riya -a -exc js,json,css -o results.txt")
fmt.Println(" cat urls.txt | riya -s -u -o sensitive.txt")
fmt.Println(" waybackurls target.com | riya -inc sql,env,config -stats")
fmt.Println(" cat urls.txt | riya -a -o links.txt")
fmt.Println(" riya -g -list # lists all GraphQL patterns")
}
func loadPatterns(filename string) (PatternFile, error) {
var pf PatternFile
data, err := os.ReadFile(filename)
if err != nil {
return pf, err
}
err = yaml.Unmarshal(data, &pf)
return pf, err
}
func selectedCategories() []string {
var cats []string
if *flagAll || (!*flagSQL && !*flagGraphQL && !*flagPHP && !*flagBackup &&
!*flagConfig && !*flagJSJSON && !*flagLogs && !*flagCerts &&
!*flagFramework && !*flagVCS && !*flagArchive && !*flagCloudInfra &&
!*flagAuthAdmin && !*flagDirs && !*flagMisc) {
return []string{"all"}
}
flags := map[string]*bool{
"s": flagSQL, "g": flagGraphQL, "p": flagPHP, "b": flagBackup,
"c": flagConfig, "j": flagJSJSON, "l": flagLogs, "k": flagCerts,
"f": flagFramework, "v": flagVCS, "x": flagArchive, "d": flagCloudInfra,
"m": flagAuthAdmin, "r": flagDirs, "misc": flagMisc,
}
for cat, f := range flags {
if *f {
cats = append(cats, cat)
}
}
return cats
}
func buildCategoryRegexMap(pf PatternFile, cats []string) (map[*regexp.Regexp]string, error) {
result := make(map[*regexp.Regexp]string)
for _, cat := range cats {
if cat == "all" {
for k, catData := range pf.Categories {
for _, pattern := range catData.Patterns {
re, err := regexp.Compile("(?i)" + pattern)
if err != nil {
return nil, fmt.Errorf("failed to compile pattern %s for category %s: %v", pattern, k, err)
}
result[re] = k
}
}
} else {
catData, ok := pf.Categories[cat]
if !ok {
continue
}
for _, pattern := range catData.Patterns {
re, err := regexp.Compile("(?i)" + pattern)
if err != nil {
return nil, fmt.Errorf("failed to compile pattern %s for category %s: %v", pattern, cat, err)
}
result[re] = cat
}
}
}
return result, nil
}
func parseFilterPatterns(patternStr string) ([]*regexp.Regexp, error) {
if patternStr == "" {
return nil, nil
}
patterns := strings.Split(patternStr, ",")
var regexes []*regexp.Regexp
for _, p := range patterns {
p = strings.TrimSpace(p)
if p == "" {
continue
}
if !strings.Contains(p, "\\") && !strings.Contains(p, "^") && !strings.Contains(p, "$") {
p = "\\." + strings.TrimPrefix(p, ".") + "(\\?|#|$)"
}
re, err := regexp.Compile("(?i)" + p)
if err != nil {
return nil, fmt.Errorf("invalid pattern %s: %v", p, err)
}
regexes = append(regexes, re)
}
return regexes, nil
}
func matchesAnyPattern(line string, patterns []*regexp.Regexp) bool {
for _, re := range patterns {
if re.MatchString(line) {
return true
}
}
return false
}
func colorize(text, color string) string {
if *flagNoColor || *flagOutputFile != "" {
return text
}
return color + text + colorReset
}
func printLists(pf PatternFile, cats []string) {
for _, cat := range cats {
catData, ok := pf.Categories[cat]
if !ok {
fmt.Printf("Unknown category key: %s\n", cat)
continue
}
color := categoryColors[cat]
fmt.Printf("%s\n", colorize(fmt.Sprintf("Category [%s] - %s:", cat, catData.Description), color))
fmt.Println("Patterns:")
for _, p := range catData.Patterns {
fmt.Printf(" • %s\n", p)
}
fmt.Println("Examples:")
for _, e := range catData.Examples {
fmt.Printf(" • %s\n", colorize(e, color))
}
fmt.Println()
}
}
func main() {
flag.Parse()
if *flagHelp {
printHelp()
return
}
pf, err := loadPatterns("patterns.yml")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load pattern file: %v\n", err)
os.Exit(1)
}
cats := selectedCategories()
if *flagList {
printLists(pf, cats)
return
}
regexMap, err := buildCategoryRegexMap(pf, cats)
if err != nil {
fmt.Fprintf(os.Stderr, "Regex compilation error: %v\n", err)
os.Exit(1)
}
if len(regexMap) == 0 {
fmt.Fprintln(os.Stderr, "No valid patterns found for the selected categories")
os.Exit(1)
}
// Parse exclude and include patterns
excludePatterns, err := parseFilterPatterns(*flagExclude)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing exclude patterns: %v\n", err)
os.Exit(1)
}
includePatterns, err := parseFilterPatterns(*flagInclude)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing include patterns: %v\n", err)
os.Exit(1)
}
// Setup output destination
var output *os.File
var writer *bufio.Writer
if *flagOutputFile != "" {
output, err = os.Create(*flagOutputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating output file: %v\n", err)
os.Exit(1)
}
defer output.Close()
writer = bufio.NewWriter(output)
defer writer.Flush()
} else {
output = os.Stdout
writer = bufio.NewWriter(output)
defer writer.Flush()
}
// Tracking for unique and stats modes
seenURLs := make(map[string]bool)
categoryStats := make(map[string]int)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
// Skip if already seen (when -u flag is set)
if *flagUnique {
if seenURLs[line] {
continue
}
seenURLs[line] = true
}
// Check exclusion patterns
if len(excludePatterns) > 0 && matchesAnyPattern(line, excludePatterns) {
continue
}
// Check inclusion patterns (if specified)
if len(includePatterns) > 0 && !matchesAnyPattern(line, includePatterns) {
continue
}
// Match against category patterns
bestCat := ""
bestPrio := 9999
for re, cat := range regexMap {
if re.MatchString(line) {
prio := categoryPriority[cat]
if prio < bestPrio {
bestPrio = prio
bestCat = cat
}
}
}
if bestCat != "" {
if *flagStats {
categoryStats[bestCat]++
} else {
color := categoryColors[bestCat]
fmt.Fprintln(writer, colorize(line, color))
}
}
}
// Print statistics if -stats flag is set
if *flagStats {
fmt.Fprintln(writer)
fmt.Fprintln(writer, "=== Match Statistics ===")
totalMatches := 0
for cat, count := range categoryStats {
catData := pf.Categories[cat]
color := categoryColors[cat]
fmt.Fprintf(writer, "%s [%s] %s: %d matches\n",
colorize("●", color),
cat,
catData.Description,
count)
totalMatches += count
}
fmt.Fprintf(writer, "\nTotal matches: %d\n", totalMatches)
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
}
}