-
Notifications
You must be signed in to change notification settings - Fork 29
/
protobuf-finder.py
394 lines (328 loc) · 18.1 KB
/
protobuf-finder.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
Protobuf Finder
> IDA plugin for reconstructing original .proto files from binary
"""
import re
# require google's protobuf library
from google.protobuf.descriptor_pb2 import FileDescriptorProto, FieldDescriptorProto
from google.protobuf.message import DecodeError
# IDA API imports
import idaapi
import ida_bytes
import ida_ida
import ida_kernwin
# Max protobuf size - used to load code from IDA database;
MAX_PROTOBUF_SIZE = 0x100000
# Icon
icon = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00\x00%\x08\x06\x00\x00\x00\x04\x19j\xaf\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x06\x0cIDATXG\xed\x97{PTu\x14\xc7\xcf\xdd]vY\x9e\xbb\x08\x08\xb8\x12h>\x92t@\x13\x07\xb2\x04\x93\xac\x1c\tL\x19\x9f\x199\x16\x16\x1a\x98\xa3i9\x84J\xa33\x068\xcd\xa0\xd9\x03m0Lj4\x07\xa3\xc9\x07\xa4#\xf8\xd8\x89-5\x1b\x9f\xa4;$\x12\xb2\xbca\x1f\xf7\xf6\xfb\xdd=\xbb\xee\xb2w/\xeb\x8c\x7f\xf4\xc7~\x98\xe3\xfd}\xcf]\x99s\xf8\x9d\xf3\xfb\x9d\x05/^\xbcx\xd1\x10+&v\x97\x18\xe7`\xf5\xc4\x0e\x11[@\xec\x7f\t\r\x9c\x06\xe8\x18\xb4;\xa3\x9f\xa3\x9f\x7f\xec0\xf8\xf4\x98\xbb\xebr\x17W^\xb9\x9a\xb8\xbe\xe6\xe4{\xe8\xf2\x98\xc9k2v\x8dz%\xa9\x89\x05\x0b\xaf\x1f\xf4wv\x87\xfa\xaa\x03\xa8\xee6\xf7\x0f<\x19\x1ce({.\xa7\x92\x7f\xe9!\x8f\x94\x80~c^^C]]\xc9\x82\x06\x1dz\xac\xbc<<\x04\xb2\xa3#!\xd2W\x0e\xe1\xea\x90\xf6\xf0\xf9\x0b\xa7\xa8__\xf9\x0cyE\xcb\xc7\xa9\x84\x026\xcd\x02\xe9\xe8PT\xce0\xe4gz\xd8\x84\xcc\xea\xf4\xcdG\xd05$\x1e\'\xa0/)\x8a\xef\xff\xa5\xa6v\xf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\x97\x83\xd9\x1aWR\x9c\x04\x0e\x15>L\xe2\xd3w\xa5$xr\x96\xcaG\xacf\x9e\xfeVt\xb7\x1dy\xa4/\xf5\x94\x8c-\xfa7dJ\x19?j\xd8\xb2\xb7}S\xb0\xe9\xd8\x08\x85n\xc72u\x1dJ\x1e\xee\xec\x98x\x90\xca\x1cv\x87~\x9a\x85\xa5\x05\xcd3*\n5\xbf\xf2\xbf\xc5\'\xd8\x00\x93\x1b\xbe!;.\xfc\xe5\xc3\x8b\x17/^\xbc<^\x00\xfe\x03t\xd3\x12d\xcd*\xb7\xe1\x00\x00\x00\x00IEND\xaeB`\x82'
icon_id = idaapi.load_custom_icon(data=icon, format="png")
class util:
def syntax_highlight_proto(line):
colored_line = line
# Strings
str = re.search("\".*\"",colored_line)
if str:
colored_line = colored_line[:str.start()] + idaapi.COLSTR(colored_line[str.start():str.end()], idaapi.SCOLOR_DSTR) + colored_line[str.end():]
# Variables
var = re.search("\w+\s*(?==)",colored_line)
if var:
colored_line = colored_line[:var.start()] + idaapi.COLSTR(colored_line[var.start():var.end()], idaapi.SCOLOR_CODNAME) + colored_line[var.end():]
# Objects
obj = re.search("\w+\s*(?={)",colored_line)
if obj:
colored_line = colored_line[:obj.start()] + idaapi.COLSTR(colored_line[obj.start():obj.end()], idaapi.SCOLOR_IMPNAME) + colored_line[obj.end():]
# Numbers
num = re.search("\W\d+\W",colored_line)
if num:
colored_line = colored_line[:num.start()] + idaapi.COLSTR(colored_line[num.start():num.end()], idaapi.SCOLOR_DNUM) + colored_line[num.end():]
return colored_line
# IDA plugin Action Handler
class ProtobufFinder(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Called when the Search -> Protobuf Finder is clicked
def activate(self, ctx):
print("[Protobuf] Search started.")
runProtod() # run search
print("[Protobuf] Search done.")
pass
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class protobuf_fetch_t(idaapi.plugin_t):
comment = "Protobuf string finder."
help = "A plugin to find a parse embedded proto files in binaries with basic syntax highlight."
wanted_name = "Protobuf"
wanted_hotkey = ""
flags = idaapi.PLUGIN_KEEP
def init(self):
self.printBanner()
protobuf_desc = idaapi.action_desc_t(
'protobuf:search', # The action name. This acts like an ID and must be unique
'Protobuf Finder', # The action text.
ProtobufFinder(), # The action handler.
'', # Optional: the action shortcut
'', # Optional: the action tooltip (available in menus/toolbar)
icon_id) # Optional: the action icon (shows when in menus/toolbars)
idaapi.register_action(protobuf_desc)
# Insert the action in the menu
if not idaapi.attach_action_to_menu("Search", "protobuf:search", idaapi.SETMENU_APP):
print("[Protobuf] Failed attaching to menu.")
return idaapi.PLUGIN_KEEP
def run(self):
pass
def term(self):
pass
def printBanner(self):
print("---------------------------------------------------------------------------------------------")
print("Protobuf string finder loaded.")
print("Run via Search -> Protobuf Finder")
print("---------------------------------------------------------------------------------------------")
def runProtod():
extractor = ProtobufExtractor()
extractor.extract()
def PLUGIN_ENTRY():
return protobuf_fetch_t()
# =======================================================================================
# rewritten Protod code to Python3, using IDA API
# =======================================================================================
###########
# helpers
###########
def is_valid_filename(filename):
'''
Check if given filename may be valid
'''
charset = 'abcdefghijklmnopqrstuvwxyz0123456789-_/$,.[]()'
for char in filename.lower():
if chr(char) not in charset:
return False
return True
def decode_varint128(stream):
'''
Decode Varint128 from buffer
'''
bits = ''
count = 0
for stream_byte in stream:
count += 1
raw_byte = stream_byte
bits += (bin((raw_byte&0x7F))[2:]).rjust(7,'0')
if (raw_byte&0x80) != 0x80:
break
return (int(bits, 2), count)
def render_type(field_type, package):
'''
Return the string representing a given type inside a given package
'''
i = 0
nodes = field_type.split('.')
nodes_ = package.split('.')
for i in range(len(nodes)):
if i < len(nodes_):
if nodes[i] != nodes_[i]:
return '.'.join(nodes[i:])
else:
return '.'.join(nodes[i:])
return '.'.join(nodes[i:])
#############################
# Protobuf fields walker
#############################
class ProtobufFieldsWalker:
'''
Homemade Protobuf fields walker
This class allows Protod to walk the fields
and determine the probable size of the protobuf
serialized file.
'''
def __init__(self, stream):
self._stream = stream
self._size = -1
def get_size(self):
return self._size
def walk(self):
end = False
offset = 0
while (not end) and (offset<len(self._stream)):
# read tag
tag = self._stream[offset]
offset += 1
wire_type = tag&0x7
if wire_type == 0:
value, size = decode_varint128(self._stream[offset:])
offset += size
elif wire_type == 1:
offset += 8
elif wire_type == 2:
value, size = decode_varint128(self._stream[offset:])
offset += size + value
elif wire_type == 5:
offset += 4
elif wire_type == 3:
continue
elif wire_type == 4:
continue
else:
end = True
self._size = offset-1
#############################
# Serialized metadata parsing
#############################
class FileDescriptorDisassembler:
'''
Core disassembling class
This class parses the provided serialized data and
produces one or many .proto files.
'''
def __init__(self, file_desc):
self.desc = file_desc
def getLabel(self, l):
return [None, 'optional', 'required', 'repeated'][l]
def getTypeStr(self, t):
types = [
None,
'double',
'float',
'int64',
'uint64',
'int32',
'fixed64',
'fixed32',
'bool',
'string',
'group',
'message',
'bytes',
'uint32',
'enum',
'sfixed32',
'sfixed64',
'sint32',
'sint64'
]
return types[t]
def renderEnum(self, enum, depth=0, package='', nested=False):
buffer = '\n'
buffer += '%senum %s {\n' % (' '*depth, enum.name)
for value in enum.value:
buffer += '%s%s = %d;\n' % (' '*(depth+1), value.name, value.number)
buffer += '%s}' % (' '*depth)
buffer += '\n'
return buffer
def renderField(self, field, depth=0, package='', nested=False):
buffer = ''
try:
if field.HasField('type'):
# message case
if field.type == FieldDescriptorProto.TYPE_MESSAGE or field.type == FieldDescriptorProto.TYPE_ENUM:
field.type_name = render_type(field.type_name[1:], package)
buffer += '%s%s %s %s = %d;\n' % (' '*depth, self.getLabel(field.label), field.type_name, field.name, field.number)
else:
if field.HasField('default_value'):
if self.getTypeStr(field.type) == 'string':
field.default_value = '"%s"'% field.default_value
buffer += '%s%s %s %s = %d [default = %s];\n' % (' '*depth, self.getLabel(field.label), self.getTypeStr(field.type), field.name, field.number, field.default_value)
else:
buffer += '%s%s %s %s = %d;\n' % (' '*depth, self.getLabel(field.label), self.getTypeStr(field.type), field.name, field.number)
except ValueError:
buffer += '%smessage %s {\n' % (' '*depth, field.name)
_package = package+'.'+field.name
if len(field.nested_type)>0:
for nested in field.nested_type:
buffer += self.renderField(nested, depth+1, _package, nested=True)
if len(field.enum_type)>0:
for enum in field.enum_type:
buffer += self.renderEnum(enum, depth+1, _package)
if len(field.field)>0:
for field in field.field:
buffer += self.renderField(field, depth+1, _package)
buffer += '%s}' % (' '*depth)
buffer += '\n\n'
return buffer
def render(self, filename=None):
print('[Protobuf][+] Processing %s' % self.desc.name)
buffer = ''
buffer += 'package %s;\n\n' % self.desc.package
# add dependencies
if len(self.desc.dependency)>0:
for dependency in self.desc.dependency:
buffer += 'import "%s";\n' % dependency
buffer += '\n'
if len(self.desc.enum_type)>0:
for enum in self.desc.enum_type:
buffer += self.renderEnum(enum, package=self.desc.package)
if len(self.desc.message_type)>0:
for message in self.desc.message_type:
buffer += self.renderField(message, package=self.desc.package)
# print result into IDA window
textWindow = ida_kernwin.simplecustviewer_t()
if textWindow.Create("Protobuf file: " + self.desc.name):
for line in buffer.split('\n'):
textWindow.AddLine(util.syntax_highlight_proto(line))
textWindow.Show()
else:
print ("[Protobuf] Failed to open window - it is probably already open.")
#############################
# Main code
#############################
class ProtobufExtractor:
def __init__(self):
pass
def extract(self):
protos = []
searchStartAddr = 0
while True:
# search binary for ".proto" string
r = ida_bytes.bin_search(searchStartAddr,ida_ida.MAXADDR,bytes([0x2E, 0x70, 0x72, 0x6F, 0x74,0x6F]),bytes([0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]),1,1)
if r == idaapi.BADADDR:
print("[Protobuf][dbg] Search results into BADADDR (not found). Break!")
break
searchStartAddr = r+1
print(f"[Protobuf][dbg] String \".proto\" found at {hex(r)}.")
# Find beginning of the whole proto string
for j in range(64):
try:
protostring = ida_bytes.get_bytes(r-j-1, MAX_PROTOBUF_SIZE, False)
if decode_varint128(protostring[1:])[0]==(j+5) and is_valid_filename(protostring[1+1:j+6+1]):
print(f"[Protobuf][dbg] Protostring identified: {protostring[:40]} [...]")
# Walk the fields and get a probable size
walker = ProtobufFieldsWalker(protostring)
walker.walk()
probable_size = walker.get_size()
print(f"[Protobuf][dbg] Protostring section size {hex(probable_size)}.")
"""
Probable size approach is not perfect,
we add a delta of 1024 bytes to be sure
not to miss something =)
"""
for k in range(probable_size+1024, 0, -1):
try:
fds = FileDescriptorProto()
fds.ParseFromString(protostring[:k])
protos.append(protostring[:k])
print('[Protobuf][dbg][i] Found protofile %s (%d bytes)' % (protostring[:j+5], k))
break
except DecodeError:
pass
except UnicodeDecodeError:
pass
break
except IndexError:
pass
if protos:
# Load successively each binary proto file and rebuild it from scratch
seen = []
for content in protos:
try:
# Load the prototype
fds = FileDescriptorProto()
fds.ParseFromString(content)
res = FileDescriptorDisassembler(fds)
if len(res.desc.name)>0:
if res.desc.name not in seen:
res.render() # disassemble and show .proto file in IDA window
seen.append(res.desc.name)
except DecodeError:
pass
else:
ida_kernwin.info("No embedded proto files were discovered.")