forked from RajUmadas/Protobuf-Easy-Decode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotobuf_easy_decode.py
executable file
·158 lines (136 loc) · 5.04 KB
/
protobuf_easy_decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python
"""
Author: Rajendra Umadas
"""
import sys
import binascii
import struct
import pprint
class WIRETYPE:
VARINT = 0
FIXED_64 = 1
LENGTHDELIM = 2
STARTGROUP = 3
ENDGROUP = 4
FIXED_32 = 5
class ProtobufEasyDecode:
GLOBAL_DEBUG = False
def __init__(self,new_message):
self.raw_message = new_message
self.decoded_message = {}
self.decoded_message_deep = {}
def decode_fixed_64(self, buf, pos):
newpos = pos + 8
data = buf[pos:newpos]
data = struct.unpack('<Q',data)
return (data,newpos)
def decode_fixed_32(self, buf, pos):
newpos = pos + 4
data = buf[pos:newpos]
data = struct.unpack('<I',data)
return (data,newpos)
def decode_varint(self,buf,pos):
#pass in buffer and starting position
#return the int and the ending pos
result = 0
shifts = 0
while True:
current_byte = ord(buf[pos])
result = result | ((current_byte & 0x7f) << shifts)
pos = pos + 1
if not (current_byte & 0x80):
return (result,pos)
shifts = shifts + 7
def decode_tag_header(self, tag_header):
#returns tag_id, and tag_type
return (tag_header >> 3, tag_header & 0x07)
def decode_lengthdelim (self, buf, pos):
#pass in buffer and start pos
#return bytes and ending pos
length,pos = self.decode_varint(buf,pos)
new_pos = pos + length
return buf[pos:new_pos],new_pos
def decode_raw_message(self,message, deep = False):
if self.GLOBAL_DEBUG:
print "Entering 'decode_raw_message'"
alls_good = True
pos = 0
temp_proto = {}
while alls_good:
try:
current_tag_header,pos=self.decode_varint(message,pos)
current_tag_id,current_tag_type = \
self.decode_tag_header(current_tag_header)
if self.GLOBAL_DEBUG:
print ("Tag ID: %i, Tag Type: %i" % (current_tag_id, current_tag_type))
except:
#could not extract a correct tag header
if self.GLOBAL_DEBUG:
print "Couldn't get tag header"
current_tag_type= -1
pos = len(message)
try:
if current_tag_type == WIRETYPE.LENGTHDELIM:
data,pos = self.decode_lengthdelim(message,pos)
if deep:
old_data = data
data = (old_data,self.decode_raw_message(data,True))
elif current_tag_type == WIRETYPE.VARINT:
data,pos = self.decode_varint(message,pos)
elif current_tag_type == WIRETYPE.FIXED_64:
data,pos = self.decode_fixed_64(message,pos)
elif current_tag_type == WIRETYPE.FIXED_32:
data,pos = self.decode_fixed_32(message,pos)
else:
#did not get a valid tag_type
data = "ERR"
pos = len(message)
alls_good = False
except:
#got a valid tag_type but parsing failed
data = "Err"
pos = len(message)
alls_good = False
if current_tag_id in temp_proto:
temp_data = temp_proto[current_tag_id][1]
if type(temp_data) == list:
temp_proto[current_tag_id][1].append(data)
else:
temp_proto[current_tag_id]=(temp_proto[current_tag_id][0],[temp_data,data])
else:
temp_proto[current_tag_id] = (current_tag_type,data)
if pos == len(message):
alls_good = False
return temp_proto
def get_decoded_raw_message (self):
if self.decoded_message != {}:
return self.decoded_message
self.decoded_message = self.decode_raw_message(self.raw_message)
return self.decoded_message
def get_decoded_raw_message_deep(self):
if self.decoded_message_deep != {}:
return self.decoded_message_deep
self.decoded_message_deep = self.decode_raw_message(self.raw_message,True)
return self.decoded_message_deep
def pretty_print_decoded_message_deep(self):
pp = pprint.PrettyPrinter()
pp.pprint(self.decoded_message_deep)
def pretty_print_decoded_message(self):
pp = pprint.PrettyPrinter()
pp.pprint(self.decoded_message)
if __name__ == "__main__":
if len(sys.argv) != 2:
print 'Usage: %s "Hex Encoded Protobuf"' % sys.argv[0]
exit()
x = ProtobufEasyDecode(binascii.unhexlify(sys.argv[1]))
x.get_decoded_raw_message()
x.get_decoded_raw_message_deep()
print "Recursive Decoding"
print "*****"
x.pretty_print_decoded_message_deep()
print "*****"
print ""
print "One Level Decoding"
print "*****"
x.pretty_print_decoded_message()
print "*****"