-
Notifications
You must be signed in to change notification settings - Fork 16
/
gourlex.go
309 lines (259 loc) · 7.66 KB
/
gourlex.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
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
package main
import (
"crypto/tls"
"errors"
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"golang.org/x/net/html"
)
func main() {
var urlFlag string
var cookie string
var customHeader string
var proxyFlag string
var urlOnly bool
var pathOnly bool
var silentMode bool
var client *http.Client
flag.StringVar(&urlFlag, "t", "", "Specify the target URL (e.g., domain.com or https://domain.com)")
flag.StringVar(&cookie, "c", "", "Specify cookies (e.g., user_token=g3p21ip21h; )")
flag.StringVar(&customHeader, "r", "", "Specify headers (e.g., Myheader: test )")
flag.StringVar(&proxyFlag, "p", "", "Specify the proxy URL (e.g., 127.0.0.1:8080)")
flag.BoolVar(&urlOnly, "uO", false, "Extract only URLs")
flag.BoolVar(&pathOnly, "pO", false, "Extract only paths")
flag.BoolVar(&silentMode, "s", false, "Avoid printing banner and other messages")
helpFlag := flag.Bool("h", false, "Display help")
flag.Parse()
if *helpFlag {
fmt.Println("gourlex is a tool for extracting URLs from a webpage.")
fmt.Println("\nUsage:")
fmt.Printf(" %s [arguments]\n", os.Args[0])
fmt.Println("\nThe arguments are:")
fmt.Println(" -t string Specify the target URL (e.g., domain.com or https://domain.com)")
fmt.Println(" -c string Specify cookies (e.g., user_token=g3p21ip21h; )")
fmt.Println(" -r string Specify headers (e.g., Myheader: test )")
fmt.Println(" -p string Specify the proxy URL (e.g., 127.0.0.1:8080)")
fmt.Println(" -s Silent Mode, avoid printing banner and other messages")
fmt.Println(" -uO Extract only full URLs")
fmt.Println(" -pO Extract only URL paths")
fmt.Println(" -h Display help")
fmt.Println("\nExample:")
fmt.Println(" gourlex -t domain.com ")
return
}
if !silentMode {
printBanner()
}
if urlFlag == "" {
fmt.Println("Please provide a target.\n Example usage: gourlex -t domain.com\n Use -h for help.")
return
}
validUrl, err := validateUrl(urlFlag)
if err != nil {
fmt.Printf("Error: %v\n\n", err)
return
}
if urlOnly && pathOnly {
fmt.Println("You can't use both -uO and -pO flags together")
return
}
if len(proxyFlag) > 0 {
if isValidProxy(proxyFlag) {
if !silentMode {
fmt.Printf("Using proxy: %s\n\n", proxyFlag)
}
client, err = createHTTPClientWProxy(proxyFlag)
if err != nil {
fmt.Println(err)
return
}
} else {
fmt.Println("Invalid proxy:", proxyFlag)
fmt.Println("Please insert a valid proxy in the ip:port format")
return
}
} else {
client = &http.Client{}
}
req, err := http.NewRequest("GET", validUrl, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
if cookie != "" {
req.Header.Set("Cookie", cookie)
}
if customHeader != "" {
headerParts := strings.SplitN(customHeader, ":", 2)
if len(headerParts) == 2 {
req.Header.Add(strings.TrimSpace(headerParts[0]), strings.TrimSpace(headerParts[1]))
} else {
fmt.Printf("Invalid header format: %s\n", customHeader)
}
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
if !silentMode {
fmt.Printf("\033[1;97mExtracting URLs from: %s\033[0m\n\n", urlFlag)
}
urls, paths, err := extractURLsAndPathsFromResponse(resp)
if err != nil {
fmt.Println(err)
return
}
printURLsAndPaths(urls, paths, urlOnly, pathOnly, silentMode)
}
func extractURLsAndPathsFromResponse(resp *http.Response) ([]string, []string, error) {
tokenizer := html.NewTokenizer(resp.Body)
var urls []string
var paths []string
for {
tokenType := tokenizer.Next()
switch tokenType {
case html.ErrorToken:
return urls, paths, nil
case html.StartTagToken, html.SelfClosingTagToken:
token := tokenizer.Token()
var hrefValue string
var srcValue string
for _, attr := range token.Attr {
if attr.Key == "href" {
hrefValue = strings.TrimSpace(attr.Val)
} else if attr.Key == "src" {
srcValue = strings.TrimSpace(attr.Val)
}
}
if hrefValue != "" && hrefValue != "#" {
if strings.HasPrefix(hrefValue, "http://") || strings.HasPrefix(hrefValue, "https://") {
urls = append(urls, hrefValue)
} else if u, err := url.Parse(hrefValue); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
urls = append(urls, u.String())
} else {
paths = append(paths, hrefValue)
}
}
if srcValue != "" && hrefValue != "#" {
if strings.HasPrefix(srcValue, "http://") || strings.HasPrefix(srcValue, "https://") {
urls = append(urls, srcValue)
} else if u, err := url.Parse(srcValue); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
urls = append(urls, u.String())
} else {
paths = append(paths, srcValue)
}
}
}
}
}
func printURLsAndPaths(urls []string, paths []string, urlOnly, pathOnly, silentMode bool) {
if !pathOnly {
if !silentMode {
str := "Extracted URLs:\n\n"
coloredUrls := colorize(str, "\033[1;32m")
fmt.Print(coloredUrls)
}
for _, u := range urls {
fmt.Println(u)
}
}
fmt.Println()
if !urlOnly {
if !silentMode {
str2 := "Extracted Paths:\n\n"
coloredPaths := colorize(str2, "\033[1;32m")
fmt.Print(coloredPaths)
}
for _, p := range paths {
fmt.Println(p)
}
}
}
func validateUrl(inputURL string) (string, error) {
u, err := url.Parse(inputURL)
if err != nil {
return "", fmt.Errorf("Error parsing URL: %v", err)
}
if u.Scheme == "" {
inputURL = "https://" + inputURL
u, _ = url.Parse(inputURL)
}
if u.Scheme != "http" && u.Scheme != "https" {
return "", errors.New("Invalid URL scheme")
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}
_, err = net.LookupHost(host)
if err != nil {
return "", err
}
if port != "" {
inputURL = fmt.Sprintf("%s://%s:%s%s", u.Scheme, host, port, u.RequestURI())
} else {
inputURL = fmt.Sprintf("%s://%s%s", u.Scheme, host, u.RequestURI())
}
return inputURL, nil
}
func createHTTPClientWProxy(proxy string) (*http.Client, error) {
parts := strings.Split(proxy, ":")
proxyIP := parts[0]
proxyPortStr := parts[1]
proxyPort, err := strconv.Atoi(proxyPortStr)
if err != nil {
return nil, fmt.Errorf("error converting proxy port to integer: %v", err)
}
client := &http.Client{}
if proxyIP != "" && proxyPort != 0 {
proxyURL, err := url.Parse(fmt.Sprintf("http://%s:%d", proxyIP, proxyPort))
if err != nil {
return nil, fmt.Errorf("error parsing proxy URL: %v", err)
}
client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
return client, nil
}
func isValidProxy(input string) bool {
parts := strings.Split(input, ":")
if len(parts) != 2 {
return false
}
ip := parts[0]
portStr := parts[1]
if net.ParseIP(ip) == nil {
return false
}
port, err := strconv.Atoi(portStr)
if err != nil || port < 1 || port > 65535 {
return false
}
return true
}
func colorize(text string, colorCode string) string {
resetColor := "\033[0m"
return colorCode + text + resetColor
}
func printBanner() {
purple := "\033[1;35m"
fmt.Println(colorize(" _ ", purple))
fmt.Println(colorize(" __ _ ___ _ _ _ __| | _____ __", purple))
fmt.Println(colorize(" / _` |/ _ \\| | | | '__| |/ _ \\ \\/ /", purple))
fmt.Println(colorize(" | (_| | (_) | |_| | | | | __/> < ", purple))
fmt.Println(colorize(" \\__, |\\___/ \\__,_|_| |_|\\___/_/\\_\\", purple))
fmt.Println(colorize(" |___/ ", purple))
fmt.Println("")
fmt.Print(colorize("Gourlex - WebPage Urls Extractor Tool\n\n", purple))
}