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

Adding support for gRPC messages #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions extender.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
import inspect
import base64
from struct import pack

from burp import IBurpExtender
from burp import IHttpListener
Expand Down Expand Up @@ -66,12 +67,11 @@ def json_to_protobuf_in_python3(self, json_body):
except subprocess.CalledProcessError as e:
print("Subprocess exited with error (status code {}):".format(e.returncode))
print(e.output.decode())

output = output.decode("utf-8").strip()
protobuf = base64.b64decode(output)

return protobuf

# Implement IHttpListener methods
def processHttpMessage(self, toolFlag, messageIsRequest, messageInfo):
# Only continue if the extension is enabled
Expand Down Expand Up @@ -103,7 +103,20 @@ def processHttpMessage(self, toolFlag, messageIsRequest, messageInfo):
json_body = json.loads(body_string)
# Convert the JSON to Protobuf
protobuf = self.json_to_protobuf_in_python3(json_body)

#For header value that contains application/grpc content type
if any("application/grpc" in header.lower() for header in headers):
# Calculate the length of the serialized message.
message_length = len(protobuf)

# Create the gRPC message header.
# It consists of a compressed flag (0 for uncompressed) and the message length.
grpc_header = pack('>B', 0) + pack('>I', message_length)

# Concatenate the header and the serialized message.
protobuf = grpc_header + protobuf

# Create a new HTTP message with the Protobuf body
new_message = self._helpers.buildHttpMessage(headers, protobuf)
# Update the request in the messageInfo object
messageInfo.setRequest(new_message)
messageInfo.setRequest(new_message)
2 changes: 1 addition & 1 deletion json-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def generate_example_json(protobuf_module, message_name):
set_placeholder_values(message)

# Convert the protobuf message to a JSON string and return it
return MessageToJson(message, including_default_value_fields=True)
return MessageToJson(message, preserving_proto_field_name=True)

def main():
try:
Expand Down