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

http: allow raw header capture #347

Merged
merged 1 commit into from
Apr 14, 2022

Conversation

codyprime
Copy link
Member

The golang textproto library does a few things when parsing the HTTP
headers:

  • consume some whitespace characters (e.g. \r\n)
  • canonicalizes the header keys (e.g. "content-type" => "Content-Type")
  • moves the headers into a map

This all makes sense when parsing HTTP, but for a scanner some may want
to have the exact headers, to match on order, non-canonical keys, etc.

This adds that option, if '--raw-headers' is specified during an HTTP
scan. This is accomplished by implementing a tee reader on the pconn
interface, that tees before the bufio reader is put in place. The
tee copy can be disabled once the headers have been read, so as to not
waste memory while consuming the HTTP body.

How to Test

echo "8.8.8.8"  | ./zgrab2 http -p 443 --use-https --raw-headers --max-redirects=0 | \
    jq -r .data.http.result.response.headers_raw | base64 -d

stdout output should look like:

X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Location: https://dns.google/
Date: Tue, 12 Apr 2022 15:53:04 GMT
Content-Type: text/html; charset=UTF-8
Server: HTTP server (unknown)
Content-Length: 216
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

The golang textproto library does a few things when parsing the HTTP
headers:

* consume some whitespace characters (e.g. \r\n)
* canonicalizes the header keys (e.g. "content-type" => "Content-Type")
* moves the headers into a map

This all makes sense when parsing HTTP, but for a scanner some may want
to have the exact headers, to match on order, non-canonical keys, etc.

This adds that option, if '--raw-headers' is specified during an HTTP
scan.  This is accomplished by implementing a tee reader on the pconn
interface, that tees before the bufio reader is put in place.  The
tee copy can be disabled once the headers have been read, so as to not
waste memory while consuming the HTTP body.

While denoted as "raw headers", this will also capture the raw status
line as well.
@ghost
Copy link

ghost commented Apr 14, 2022

lgtm

@codyprime codyprime merged commit 83e55e0 into zmap:feature/TLS1.3 Apr 14, 2022
@p-l-
Copy link

p-l- commented Apr 21, 2022

Hi @codyprime, thanks for this, it will be super helpful. Is there a reason why this has been merged to a feature branch and not to master? Could it be ported to master?

codyprime added a commit to codyprime/zgrab2 that referenced this pull request Apr 21, 2022
The golang textproto library does a few things when parsing the HTTP
headers:

* consume some whitespace characters (e.g. \r\n)
* canonicalizes the header keys (e.g. "content-type" => "Content-Type")
* moves the headers into a map

This all makes sense when parsing HTTP, but for a scanner some may want
to have the exact headers, to match on order, non-canonical keys, etc.

This adds that option, if '--raw-headers' is specified during an HTTP
scan.  This is accomplished by implementing a tee reader on the pconn
interface, that tees before the bufio reader is put in place.  The
tee copy can be disabled once the headers have been read, so as to not
waste memory while consuming the HTTP body.

While denoted as "raw headers", this will also capture the raw status
line as well.

(cherry picked from commit 83e55e0)
Signed-off-by: Jeff Cody <jcody@censys.io>
@codyprime
Copy link
Member Author

Hi @codyprime, thanks for this, it will be super helpful. Is there a reason why this has been merged to a feature branch and not to master? Could it be ported to master?

Hi! The reason for the feature branch was because A) I was using the feature branch :) and B) That enabled some volume testing.

I've cherry-picked the commit and created a PR on the master branch: #349

zakird pushed a commit that referenced this pull request Feb 18, 2024
The golang textproto library does a few things when parsing the HTTP
headers:

* consume some whitespace characters (e.g. \r\n)
* canonicalizes the header keys (e.g. "content-type" => "Content-Type")
* moves the headers into a map

This all makes sense when parsing HTTP, but for a scanner some may want
to have the exact headers, to match on order, non-canonical keys, etc.

This adds that option, if '--raw-headers' is specified during an HTTP
scan.  This is accomplished by implementing a tee reader on the pconn
interface, that tees before the bufio reader is put in place.  The
tee copy can be disabled once the headers have been read, so as to not
waste memory while consuming the HTTP body.

While denoted as "raw headers", this will also capture the raw status
line as well.

(cherry picked from commit 83e55e0)
Signed-off-by: Jeff Cody <jcody@censys.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants