| 1 | package syn_scan |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
| 5 | "crypto/x509" |
| 6 | "fmt" |
| 7 | "golang.org/x/net/html" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func isTitleElement(n *html.Node) bool { |
| 13 | return n.Type == html.ElementNode && strings.ToLower(n.Data) == "title" |
| 14 | } |
| 15 | |
| 16 | func searchForTitle(n *html.Node) string { |
| 17 | if isTitleElement(n) && n.FirstChild != nil { |
| 18 | return n.FirstChild.Data |
| 19 | } |
| 20 | |
| 21 | for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 22 | if result := searchForTitle(c); result != "" { |
| 23 | return result |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return "" |
| 28 | } |
| 29 | |
| 30 | func httpGrabber(mod *SynScanner, ip string, port int) string { |
| 31 | schema := "http" |
| 32 | client := &http.Client{ |
| 33 | Timeout: bannerGrabTimeout, |
| 34 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 35 | return nil |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | sport := fmt.Sprintf("%d", port) |
| 40 | if strings.Contains(sport, "443") { |
| 41 | schema = "https" |
| 42 | client = &http.Client{ |
| 43 | Timeout: bannerGrabTimeout, |
| 44 | Transport: &http.Transport{ |
| 45 | TLSClientConfig: &tls.Config{ |
| 46 | InsecureSkipVerify: true, |
| 47 | VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { |
| 48 | return nil |
| 49 | }, |
| 50 | }, |
| 51 | }, |
| 52 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 53 | return nil |
| 54 | }, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // https://stackoverflow.com/questions/12260003/connect-returns-invalid-argument-with-ipv6-address |
| 59 | if strings.Contains(ip, ":") { |
| 60 | ip = fmt.Sprintf("[%s%%25%s]", ip, mod.Session.Interface.Name()) |
| 61 | } |
| 62 | |
| 63 | url := fmt.Sprintf("%s://%s:%d/", schema, ip, port) |
| 64 | resp, err := client.Get(url) |
| 65 | if err != nil { |
| 66 | mod.Debug("error while grabbing banner from %s: %v", url, err) |
| 67 | return "" |
| 68 | } |
| 69 | defer resp.Body.Close() |
| 70 | |
| 71 | fallback := "" |
| 72 | for name, values := range resp.Header { |
| 73 | for _, value := range values { |
| 74 | header := strings.ToLower(name) |
| 75 | if len(value) > len(fallback) && (header == "x-powered-by" || header == "server") { |
| 76 | mod.Debug("found header %s for %s:%d -> %s", header, ip, port, value) |
| 77 | fallback = value |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | doc, err := html.Parse(resp.Body) |
| 83 | if err != nil { |
| 84 | mod.Debug("error while reading and parsing response from %s: %v", url, err) |
| 85 | return fallback |
| 86 | } |
| 87 | |
| 88 | if title := searchForTitle(doc); title != "" { |
| 89 | return title |
| 90 | } |
| 91 | |
| 92 | return fallback |
| 93 | } |