-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
248 lines (211 loc) · 5.17 KB
/
main.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
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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/alwalxed/juicyurls/suspicious"
"os"
"sort"
"strings"
"sync"
)
const (
maxURLLength = 2048
)
type URLChecker struct {
checkKeywords bool
checkExtensions bool
checkPaths bool
checkHidden bool
excludePatterns []string
}
type Config struct {
filePath string
outputPath string
categories string
excludes string
urlChecker URLChecker
}
func (c *URLChecker) isSuspicious(url string) bool {
if url == "" || len(url) > maxURLLength {
return false
}
urlLower := strings.ToLower(strings.TrimSpace(url))
for _, exclude := range c.excludePatterns {
if strings.Contains(urlLower, strings.ToLower(exclude)) {
return false
}
}
if c.checkKeywords {
for _, keyword := range suspicious.Keywords {
if strings.Contains(urlLower, keyword) {
return true
}
}
}
if c.checkExtensions {
for _, ext := range suspicious.Extensions {
if strings.HasSuffix(urlLower, ext) {
return true
}
}
}
if c.checkPaths {
for _, path := range suspicious.Paths {
if strings.Contains(urlLower, path) {
return true
}
}
}
if c.checkHidden {
for _, hiddenFile := range suspicious.Hidden {
if strings.Contains(urlLower, hiddenFile) {
return true
}
}
}
return false
}
func processURLs(urls []string, checker *URLChecker, numWorkers int) []string {
var (
wg sync.WaitGroup
mutex sync.Mutex
suspiciousURLs []string
urlChan = make(chan string, len(urls))
)
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for url := range urlChan {
if checker.isSuspicious(url) {
mutex.Lock()
suspiciousURLs = append(suspiciousURLs, url)
mutex.Unlock()
}
}
}()
}
for _, url := range urls {
urlChan <- url
}
close(urlChan)
wg.Wait()
return suspiciousURLs
}
func writeResults(urls []string, outputPath string) error {
if len(urls) == 0 {
fmt.Println("No suspicious URLs found.")
return nil
}
if outputPath == "" {
for _, url := range urls {
fmt.Println(url)
}
return nil
}
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer file.Close()
for _, url := range urls {
if _, err := fmt.Fprintln(file, url); err != nil {
return fmt.Errorf("error writing to output file: %v", err)
}
}
fmt.Printf("Results written to: %s\n", outputPath)
return nil
}
func processFile(config *Config) error {
file, err := os.Open(config.filePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
var urls []string
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
for scanner.Scan() {
if url := strings.TrimSpace(scanner.Text()); url != "" {
if len(url) <= maxURLLength {
urls = append(urls, url)
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %v", err)
}
suspiciousURLs := processURLs(urls, &config.urlChecker, 4)
sort.Strings(suspiciousURLs)
uniqueURLs := removeDuplicates(suspiciousURLs)
return writeResults(uniqueURLs, config.outputPath)
}
func removeDuplicates(urls []string) []string {
seen := make(map[string]struct{}, len(urls))
uniqueURLs := make([]string, 0, len(urls))
for _, url := range urls {
if _, exists := seen[url]; !exists {
seen[url] = struct{}{}
uniqueURLs = append(uniqueURLs, url)
}
}
return uniqueURLs
}
func printUsage() {
fmt.Println("Usage: juicyurls [options]")
fmt.Println("\nOptions:")
fmt.Println(" -h Show this help message")
fmt.Println(" -l <path> Path to the list of URLs (required)")
fmt.Println(" -m <categories> Comma-separated list of categories (optional)")
fmt.Println(" -o <path> Output file path (optional)")
fmt.Println(" -e <patterns> Comma-separated patterns to exclude (optional)")
fmt.Println("\nCategories: keywords, extensions, paths, hidden")
fmt.Println("By default, all categories are checked.")
os.Exit(0)
}
func main() {
config := &Config{}
flag.Usage = printUsage
flag.StringVar(&config.filePath, "l", "", "")
flag.StringVar(&config.categories, "m", "", "")
flag.StringVar(&config.outputPath, "o", "", "")
flag.StringVar(&config.excludes, "e", "", "")
flag.Parse()
if len(os.Args) < 2 || config.filePath == "" {
printUsage()
}
config.urlChecker = URLChecker{
checkKeywords: true,
checkExtensions: true,
checkPaths: true,
checkHidden: true,
}
if config.excludes != "" {
config.urlChecker.excludePatterns = strings.Split(config.excludes, ",")
}
if config.categories != "" {
config.urlChecker = URLChecker{
excludePatterns: config.urlChecker.excludePatterns,
}
categories := strings.Split(config.categories, ",")
for _, category := range categories {
switch strings.TrimSpace(category) {
case "keywords":
config.urlChecker.checkKeywords = true
case "extensions":
config.urlChecker.checkExtensions = true
case "paths":
config.urlChecker.checkPaths = true
case "hidden":
config.urlChecker.checkHidden = true
default:
printUsage()
}
}
}
if err := processFile(config); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}