Skip to content

Commit

Permalink
added web socket detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jun 23, 2020
1 parent 15dc4c0 commit 0ffd4c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
9 changes: 8 additions & 1 deletion cmd/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ retry:
}
}

// web socket
isWebSocket := resp.StatusCode == 101
if isWebSocket {
builder.WriteString(" [websocket]")
}

// store responses in directory
if scanopts.StoreResponse {
var domainFile = strings.Replace(domain, "/", "_", -1) + ".txt"
Expand All @@ -304,7 +310,7 @@ retry:
}
}

output <- Result{URL: fullURL, ContentLength: resp.ContentLength, StatusCode: resp.StatusCode, Title: title, str: builder.String(), VHost: isvhost, WebServer: serverHeader, Response: serverResponseRaw}
output <- Result{URL: fullURL, ContentLength: resp.ContentLength, StatusCode: resp.StatusCode, Title: title, str: builder.String(), VHost: isvhost, WebServer: serverHeader, Response: serverResponseRaw, WebSocket: isWebSocket}
}

// Result of a scan
Expand All @@ -318,6 +324,7 @@ type Result struct {
VHost bool `json:"vhost"`
WebServer string `json:"webserver"`
Response string `json:"serverResponse,omitempty"`
WebSocket bool `json:"websocket,omitempty"`
}

// JSON the result
Expand Down
17 changes: 17 additions & 0 deletions common/httputilz/httputilz.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package httputilz

import (
"fmt"
"net/http"
"net/http/httputil"

"github.com/projectdiscovery/retryablehttp-go"
Expand All @@ -12,3 +14,18 @@ func DumpRequest(req *retryablehttp.Request) (string, error) {

return string(dump), err
}

// DumpResponse to string
func DumpResponse(resp *http.Response) (string, error) {
// httputil.DumpResponse does not work with websockets
if resp.StatusCode == 101 {
raw := resp.Status + "\n"
for h, v := range resp.Header {
raw += fmt.Sprintf("%s: %s\n", h, v)
}
return raw, nil
}

raw, err := httputil.DumpResponse(resp, true)
return string(raw), err
}
28 changes: 16 additions & 12 deletions common/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"unicode/utf8"

"github.com/microcosm-cc/bluemonday"
"github.com/projectdiscovery/httpx/common/cache"
"github.com/projectdiscovery/httpx/common/httputilz"
retryablehttp "github.com/projectdiscovery/retryablehttp-go"
)

Expand All @@ -24,7 +24,6 @@ type HTTPX struct {
CustomHeaders map[string]string
}


// New httpx instance
func New(options *Options) (*HTTPX, error) {
httpx := &HTTPX{}
Expand All @@ -43,22 +42,22 @@ func New(options *Options) (*HTTPX, error) {
return http.ErrUseLastResponse // Tell the http client to not follow redirect
}

if httpx.Options.FollowRedirects{
if httpx.Options.FollowRedirects {
// Follow redirects
redirectFunc = nil
}

if httpx.Options.FollowHostRedirects{
if httpx.Options.FollowHostRedirects {
// Only follow redirects on the same host
redirectFunc = func(redirectedRequest *http.Request, previousRequest []*http.Request) error { // timo
// Check if we get a redirect to a differen host
var newHost = redirectedRequest.URL.Host
var oldHost = previousRequest[0].URL.Host
if newHost != oldHost{
if newHost != oldHost {
return http.ErrUseLastResponse // Tell the http client to not follow redirect
}
}
return nil

}
}

Expand Down Expand Up @@ -103,18 +102,23 @@ func (h *HTTPX) Do(req *retryablehttp.Request) (*Response, error) {

resp.Headers = httpresp.Header.Clone()

rawresp, err := httputil.DumpResponse(httpresp, true)
// httputil.DumpResponse does not handle websockets
rawresp, err := httputilz.DumpResponse(httpresp)
if err != nil {
return nil, err
}

resp.Raw = string(rawresp)

respbody, err := ioutil.ReadAll(httpresp.Body)
if err != nil {
return nil, err
var respbody []byte
// websockets don't have a readable body
if httpresp.StatusCode != 101 {
var err error
respbody, err = ioutil.ReadAll(httpresp.Body)
if err != nil {
return nil, err
}
}

respbodystr := string(respbody)

// check if we need to strip html
Expand Down

0 comments on commit 0ffd4c4

Please sign in to comment.