forked from projectdiscovery/httpx
/
virtualhost.go
54 lines (42 loc) · 1.39 KB
/
virtualhost.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
package httpx
import (
"fmt"
"github.com/hbakhtiyor/strsim"
retryablehttp "github.com/projectdiscovery/retryablehttp-go"
"github.com/rs/xid"
)
const simMultiplier = 100
// IsVirtualHost checks if the target endpoint is a virtual host
func (h *HTTPX) IsVirtualHost(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (bool, error) {
httpresp1, err := h.Do(req, unsafeOptions)
if err != nil {
return false, err
}
// request a non-existing endpoint
req.Host = fmt.Sprintf("%s.%s", xid.New().String(), req.Host)
httpresp2, err := h.Do(req, unsafeOptions)
if err != nil {
return false, err
}
// Status Code
if !h.Options.VHostIgnoreStatusCode && httpresp1.StatusCode != httpresp2.StatusCode {
return true, nil
}
// Content - Bytes Length
if !h.Options.VHostIgnoreContentLength && httpresp1.ContentLength != httpresp2.ContentLength {
return true, nil
}
// Content - Number of words (space separated)
if !h.Options.VHostIgnoreNumberOfWords && httpresp1.Words != httpresp2.Words {
return true, nil
}
// Content - Number of lines (newline separated)
if !h.Options.VHostIgnoreNumberOfLines && httpresp1.Lines != httpresp2.Lines {
return true, nil
}
// Similarity Ratio - if similarity is under threshold we consider it a valid vHost
if int(strsim.Compare(httpresp1.Raw, httpresp2.Raw)*simMultiplier) <= h.Options.VHostSimilarityRatio {
return true, nil
}
return false, nil
}