Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving RFC request/response passive parsing #2192

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions integration_tests/offlinehttp/data/req-resp-with-http-keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GET / HTTP/1.1
Host: pastebin.com
User-Agent: curl/7.79.1
Accept: */*
Connection: close

HTTP/1.1 200 OK
Date: Tue, 21 Jun 2022 09:32:01 GMT
Content-Type: text/plain; charset=utf-8
Connection: close
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
cache-control: public, max-age=1801
CF-Cache-Status: HIT
Age: 1585
Last-Modified: Tue, 21 Jun 2022 09:05:36 GMT
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 71ebbc0a7ea83b8b-CDG

54
line1
this is a line containing HTTP/1.1 FOO BAR
line3
0
15 changes: 15 additions & 0 deletions integration_tests/offlinehttp/rfc-req-resp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: rfc-req-resp

info:
name: Basic GET Request
author: pdteam
severity: info

requests:
- method: GET
path:
- "{{BaseURL}}"
matchers:
- type: word
words:
- "this is a line containing HTTP/1.1 FOO BAR"
1 change: 1 addition & 0 deletions v2/cmd/integration-test/integration-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
"templatesPath": templatesPathTestCases,
"templatesDir": templatesDirTestCases,
"file": fileTestcases,
"offlineHttp": offlineHttpTestcases,
}
)

Expand Down
21 changes: 21 additions & 0 deletions v2/cmd/integration-test/offline-http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
)

var offlineHttpTestcases = map[string]testutils.TestCase{
"offlinehttp/rfc-req-resp.yaml": &RfcRequestResponse{},
}

type RfcRequestResponse struct{}

// Execute executes a test case and returns an error if occurred
func (h *RfcRequestResponse) Execute(filePath string) error {
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "offlinehttp/data/", debug, "-passive")
if err != nil {
return err
}

return expectResultsCount(results, 1)
}
10 changes: 9 additions & 1 deletion v2/pkg/protocols/offlinehttp/read_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ var noMinor = regexp.MustCompile(`HTTP/([0-9]) `)

// readResponseFromString reads a raw http response from a string.
func readResponseFromString(data string) (*http.Response, error) {
var final string
// Check if "data" contains RFC compatible Request followed by a response
br := bufio.NewReader(strings.NewReader(data))
if req, err := http.ReadRequest(br); err == nil {
if resp, err := http.ReadResponse(br, req); err == nil {
return resp, nil
}
}

// otherwise tries to patch known cases such as http minor version
var final string
if strings.HasPrefix(data, "HTTP/") {
final = addMinorVersionToHTTP(data)
} else {
Expand Down