nccgroup / blackboxprotobuf

Blackbox Protobuf is a set of tools for working with encoded Protocol Buffers (protobuf) without the matching protobuf definition.
MIT License
516 stars 86 forks source link

Export map decode #9

Closed tolgatasci closed 3 years ago

tolgatasci commented 3 years ago

I was looking for something like this and couldn't find it. I wanted to share. It gives you a shortcut to fix int and string variables. sample :

message["1"][0]["3"][0]["4"] = "string"
message["1"][0]["3"][0]["1"] = 1

shortcut for those who want to see and edit with their locations


#!/usr/bin/env python

import sys
import base64
sys.path.insert(0, '../')

import blackboxprotobuf

def export(data,old_data = None):
    if isinstance(data, dict):
        for data_key in data:
            yeni_old = old_data + "["+type_check(data_key)+"]"
            if isinstance(data[data_key], str) or isinstance(data[data_key], int):
                print(yeni_old + " = " + str(val_type(data[data_key])) )
            export(data[data_key],yeni_old)
    elif isinstance(data, list):
        i = 0
        for list_data in data:

            yeni_old = str(old_data + "["+str(type_check(i))+"]")
            if isinstance(list_data, str) or isinstance(list_data, int):
                print(yeni_old + " = " + str(val_type(list_data)))
            export(list_data, yeni_old)
            i+=1

def val_type(val):
    if type(val) is str:
        return "\""+str(val)+"\""
    else:
        return val
def type_check(data):
    if type(data) is str:
        return "\""+str(data)+"\""
    else:
        return data

typedef = {}

protobuf = sys.stdin.read()

data = base64.b64decode(protobuf)
message, typedef = blackboxprotobuf.decode_message(data)

for data_ in message:
    export(message[data_],"message["+type_check(data_)+"]")
rwinkelmaier-ncc commented 3 years ago

Hi,

Thanks for submitting this. I might try to find a way to work this into the tool or documentation. Could you elaborate on what issues you're trying to solve? I know personally I've had some pain points with trying to edit large messages and typedefs and think something like message["1"][0]["3"][0]["4"] might make it easier in some cases. Or maybe adding a path reference to the fields so you don't have to manually figure out the nesting. In a future build, I've added on some better sorting of keys and an "example value" field for typedef editing.

But not sure what you meant by " It gives you a shortcut to fix int and string variables". Is there an issue with the dictionary keys, or just for formatting the message output?

tolgatasci commented 3 years ago

just a shortcut. For those who want to do manual editing. This code acts as a map. To assist. I just wanted to share it in case it's useful for you.

In the meantime, I tried for days, but I could not succeed. Thanks to this project, I can edit protobuf. Thank you so much