-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfindHostnameInUserInput.go
More file actions
64 lines (53 loc) · 1.8 KB
/
Copy pathfindHostnameInUserInput.go
File metadata and controls
64 lines (53 loc) · 1.8 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
package ssrf
import (
"main/helpers"
"main/log"
"net/url"
"strconv"
"strings"
)
func getVariants(userInput string) []string {
variants := []string{userInput, "http://" + userInput, "https://" + userInput}
decodedUserInput, err := url.QueryUnescape(userInput)
if err == nil && decodedUserInput != userInput {
variants = append(variants, decodedUserInput, "http://"+decodedUserInput, "https://"+decodedUserInput)
}
return variants
}
func findHostnameInUserInput(userInput string, hostname string, port uint32) bool {
log.Debugf("findHostnameInUserInput: userInput: %s, hostname: %s, port: %d", userInput, hostname, port)
if len(userInput) <= 1 {
return false
}
// if hostname contains : we need to add the [ and ] to the hostname (ipv6)
if strings.Contains(hostname, ":") {
hostname = "[" + hostname + "]"
}
hostnameURL := helpers.TryParseURL("http://" + hostname + ":" + strconv.Itoa(int(port)))
if hostnameURL == nil {
return false
}
userInput = helpers.ExtractResourceOrOriginal(userInput)
userInput = helpers.NormalizeRawUrl(userInput)
variants := getVariants(userInput)
for _, variant := range variants {
userInputURL := helpers.TryParseURL(variant)
if userInputURL == nil {
continue
}
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
// "The host subcomponent is case-insensitive."
if strings.EqualFold(userInputURL.Hostname(), hostnameURL.Hostname()) {
userPort := helpers.GetPortFromURL(userInputURL)
if port == 0 {
/* If we couldn't extract the port from the original URL (maybe the scheme is not http or https, or the port is not present in the URL)
or the port was not provided with the outgoing request function, we just skip the comparison of the ports. */
return true
}
if userPort == port {
return true
}
}
}
return false
}