-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmain.go
194 lines (171 loc) · 5.43 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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
)
type patternDef struct {
Name string `yaml:"name"`
Regex string `yaml:"regex"`
Confidence string `yaml:"confidence"`
}
type patternWrapper struct {
Pattern patternDef `yaml:"pattern"`
}
type yamlPatterns struct {
Patterns []patternWrapper `yaml:"patterns"`
}
var httpClient = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: time.Second,
DualStack: true,
}).DialContext,
},
}
func request(fullurl string, printStatus bool) string {
req, err := http.NewRequest("GET", fullurl, nil)
if err != nil {
fmt.Println(err)
return ""
}
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36")
resp, err := httpClient.Do(req)
if err != nil {
fmt.Println(err)
return ""
}
defer resp.Body.Close()
if printStatus && resp.StatusCode != 404 {
fmt.Printf("[Linkfinder] %s : %d\n", fullurl, resp.StatusCode)
}
var bodyString string
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return ""
}
bodyString = string(bodyBytes)
}
return bodyString
}
func regexGrep(content string, baseUrl string, patterns []patternDef) {
for _, p := range patterns {
r := regexp.MustCompile(p.Regex)
matches := r.FindAllString(content, -1)
for _, v := range matches {
fmt.Printf("[+] Found [%s] [%s] [%s]\n", p.Name, v, baseUrl)
}
}
}
func linkFinder(content, baseURL string, completeURL, printStatus bool) {
linkRegex := `(?:"|')(((?:[a-zA-Z]{1,10}://|//)[^"'/]{1,}\.[a-zA-Z]{2,}[^"']{0,})|((?:/|\.\./|\./)[^"'><,;| *()(%%$^/\\\[\]][^"'><,;|()]{1,})|([a-zA-Z0-9_\-/]{1,}/[a-zA-Z0-9_\-/]{1,}\.(?:[a-zA-Z]{1,4}|action)(?:[\?|#][^"|']{0,}|))|([a-zA-Z0-9_\-/]{1,}/[a-zA-Z0-9_\-/]{3,}(?:[\?|#][^"|']{0,}|))|([a-zA-Z0-9_\-]{1,}\.(?:php|asp|aspx|jsp|json|action|html|js|txt|xml)(?:[\?|#][^"|']{0,}|)))(?:"|')`
r := regexp.MustCompile(linkRegex)
matches := r.FindAllString(content, -1)
base, err := url.Parse(baseURL)
if err != nil {
return
}
for _, match := range matches {
cleanedMatch := strings.Trim(match, `"'`)
link, err := url.Parse(cleanedMatch)
if err != nil {
continue
}
if completeURL {
link = base.ResolveReference(link)
}
if printStatus {
request(link.String(), true)
} else {
fmt.Printf("[+] Found link: [%s] in [%s] \n", link.String(), base.String())
}
}
}
func main() {
var concurrency int
var enableLinkFinder, completeURL, checkStatus, enableSecretFinder bool
var yamlFilePath string
flag.BoolVar(&enableLinkFinder, "l", false, "Enable linkFinder")
flag.BoolVar(&completeURL, "e", false, "Complete scope URL or not")
flag.BoolVar(&checkStatus, "k", false, "Check status codes for found links")
flag.BoolVar(&enableSecretFinder, "s", false, "Enable secretFinder")
flag.IntVar(&concurrency, "c", 10, "Number of concurrent workers")
flag.StringVar(&yamlFilePath, "t", "", "Path to YAML file containing regex patterns") // <-- New flag
flag.Parse()
var patterns []patternDef
if yamlFilePath != "" {
loadedPatterns, err := loadPatternsFromYAML(yamlFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading YAML patterns: %v\n", err)
os.Exit(1)
}
for _, pw := range loadedPatterns.Patterns {
patterns = append(patterns, pw.Pattern)
}
}
urls := make(chan string, concurrency)
go func() {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
urls <- sc.Text()
}
close(urls)
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
}()
wg := sync.WaitGroup{}
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for vUrl := range urls {
res := request(vUrl, false)
if enableSecretFinder && len(patterns) > 0 {
regexGrep(res, vUrl, patterns)
}
if enableLinkFinder {
linkFinder(res, vUrl, false, false)
}
if completeURL {
linkFinder(res, vUrl, true, false)
}
if checkStatus {
linkFinder(res, vUrl, true, true)
}
}
}()
}
wg.Wait()
}
func loadPatternsFromYAML(filePath string) (*yamlPatterns, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
decoder := yaml.NewDecoder(f)
var yp yamlPatterns
if err := decoder.Decode(&yp); err != nil {
return nil, err
}
return &yp, nil
}