-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathGoLinkFinder.go
236 lines (175 loc) · 4.75 KB
/
GoLinkFinder.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
/*
https://0xsha.io
by @0xsha 1/2020
*/
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
// repos makes things easier
"github.com/PuerkitoBio/goquery"
"github.com/akamensky/argparse"
"github.com/tomnomnom/gahttp"
)
/// Global variables ///
const version = "1.0.0-alpha"
// you can change it
const concurrency = 10
// regex foo from https://github.com/GerbenJavado/LinkFinder
const regexStr = `(?:"|')(((?:[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,}|)))(?:"|')`
// will add everything to this list
// you can change it to SQLite
var founds []string
func unique(strSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range strSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func downloadJSFile(urls []string, concurrency int) {
pipeLine := gahttp.NewPipeline()
pipeLine.SetConcurrency(concurrency)
for _, u := range urls {
pipeLine.Get(u, gahttp.Wrap(parseFile, gahttp.CloseBody))
}
pipeLine.Done()
pipeLine.Wait()
}
func parseFile(req *http.Request, resp *http.Response, err error) {
if err != nil {
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// if you like it beautiful
//options := jsbeautifier.DefaultOptions()
//code := jsbeautifier.BeautifyFile(string(fileBytes), options)
matchAndAdd(string(body))
}
func extractUrlFromJS(urls []string, baseUrl string) []string {
urls = unique(urls)
var cleaned []string
for i := 1; i < len(urls); i++ {
urls[i] = strings.ReplaceAll(urls[i], "'", "")
urls[i] = strings.ReplaceAll(urls[i], "\"", "")
if len(urls[i]) < 5 {
continue
}
if !strings.Contains(urls[i], ".js") {
continue
}
if urls[i][:4] == "http" || urls[i][:5] == "https" {
cleaned = append(cleaned, urls[i])
continue
}
if urls[i][:2] == "//" {
cleaned = append(cleaned, "https:"+urls[i])
continue
}
if urls[i][:1] == "/" {
{
cleaned = append(cleaned, baseUrl+urls[i])
}
}
}
return cleaned
}
func matchAndAdd(content string) []string {
regExp, err := regexp.Compile(regexStr)
if err != nil {
log.Fatal(err)
}
links := regExp.FindAllString(content, -1)
linksLength := len(links)
if linksLength > 1 {
for i := 0; i < linksLength; i++ {
founds = append(founds, links[i])
}
}
return founds
}
func appendBaseUrl(urls []string, baseUrl string) []string {
urls = unique(urls)
var n []string
for i := 0; i < len(urls); i++ {
n = append(n, baseUrl+strings.TrimSpace(urls[i]))
}
return n
}
func extractJSLinksFromHTML(baseUrl string) []string {
var resp, err = http.Get(baseUrl)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.Body == nil {
log.Fatal("Response body is nil")
}
goos, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
var htmlJS = matchAndAdd(goos.Find("script").Text())
var urls = extractUrlFromJS(htmlJS, baseUrl)
goos.Find("script").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
urls = append(urls, src)
})
urls = appendBaseUrl(urls, baseUrl)
return urls
}
// prepares the final output by replacing \" and ' with ""
func prepareResult(result []string) []string {
for i := 0; i < len(result); i++ {
result[i] = strings.ReplaceAll(result[i], "\"", "")
result[i] = strings.ReplaceAll(result[i], "'", "")
}
return result
}
func main() {
parser := argparse.NewParser("goLinkFinder", "GoLinkFinder")
domain := parser.String("d", "domain", &argparse.Options{Required: true, Help: "Input a URL."})
output := parser.String("o", "out", &argparse.Options{Required: false, Help: "File name : (e.g : output.txt)"})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
var baseUrl = *domain
if !strings.HasPrefix(baseUrl, "http://") &&
!strings.HasPrefix(baseUrl, "https://") {
baseUrl = "https://" + baseUrl
}
var htmlUrls = extractJSLinksFromHTML(baseUrl)
downloadJSFile(htmlUrls, concurrency)
founds = unique(founds)
founds = prepareResult(founds)
for _, found := range founds {
fmt.Println(found)
}
if *output != "" {
f, err := os.OpenFile("./"+*output,
os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
for _, found := range founds {
if _, err := f.WriteString(found + "\n"); err != nil {
log.Fatal(err)
}
}
}
}