forked from RaizeTheLimit/ProtoDecoderUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproto_parser.py
More file actions
486 lines (417 loc) · 21.3 KB
/
proto_parser.py
File metadata and controls
486 lines (417 loc) · 21.3 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python3
"""
Proto parser - EXACT replica of src/parser/proto-parser.ts
100% compliant with original JavaScript
"""
import base64
import json
import logging
from pathlib import Path
import importlib.util
# Import protobuf classes - EXACT replica of JavaScript imports
try:
from protos.pogo_pb2 import _globals as pogo_protos
PROTOBUF_AVAILABLE = True
logging.info("Protobuf classes loaded successfully")
except ImportError as e:
PROTOBUF_AVAILABLE = False
logging.warning(f"Protobuf classes not available: {e}")
pogo_protos = {}
# Import from Python constants (generated from JavaScript)
try:
from constants import REQUEST_MESSAGES_RESPONSES as requestMessagesResponses
CONSTANTS_AVAILABLE = True
logging.info(f"Constants loaded: {len(requestMessagesResponses)} methods")
except ImportError:
logging.warning("Python constants not found, using basic implementation")
CONSTANTS_AVAILABLE = False
requestMessagesResponses = {}
# Global variables - EXACT replica of JavaScript
action_social = 0
action_gar_proxy = 0
def get_protobuf_class(method_id: int, data_type: str):
"""Get protobuf class for method - EXACT replica of JavaScript found_method[1]"""
if not CONSTANTS_AVAILABLE or not PROTOBUF_AVAILABLE:
return None
for method_name, proto_tuple in requestMessagesResponses.items():
req_method = proto_tuple[0]
if req_method == method_id:
if data_type == "request" and proto_tuple[1] is not None:
return proto_tuple[1]
elif data_type == "response" and proto_tuple[2] is not None:
return proto_tuple[2]
return None
def decode_protobuf_data(proto_class, data: bytes):
"""Decode protobuf data - EXACT replica of JavaScript foundMethod[1].decode(b64Decode(data)).toJSON()"""
try:
if not proto_class:
return {"decoded": data.hex(), "error": "Proto class not found"}
# Create protobuf instance and decode
proto_instance = proto_class()
proto_instance.ParseFromString(data)
# Convert to JSON-like dict - EXACT replica of JavaScript .toJSON()
return protobuf_to_dict(proto_instance)
except Exception as e:
return {"decoded": data.hex(), "error": f"Decode error: {str(e)}"}
def protobuf_to_dict(proto_obj):
"""Convert protobuf to dictionary - EXACT replica of JavaScript .toJSON()"""
try:
# Use protobuf's built-in MessageToDict conversion
from google.protobuf.json_format import MessageToDict
return MessageToDict(proto_obj)
except:
# Fallback: manual conversion
result = {}
for field, value in proto_obj.ListFields():
if hasattr(value, 'DESCRIPTOR'):
# Nested message
result[field.name] = protobuf_to_dict(value)
elif hasattr(value, '__iter__') and not isinstance(value, (str, bytes)):
# Repeated field
result[field.name] = [protobuf_to_dict(item) if hasattr(item, 'DESCRIPTOR') else item for item in value]
else:
# Simple field
result[field.name] = value
return result
def b64Decode(data: str) -> bytes:
"""EXACT replica of JavaScript b64Decode"""
if not data or data == "":
return b""
return base64.b64decode(data)
def remasterOrCleanMethodString(method_str: str) -> str:
"""EXACT replica of JavaScript remasterOrCleanMethodString"""
return method_str.replace("REQUEST_TYPE_", "") \
.replace("METHOD_", "") \
.replace("PLATFORM_", "") \
.replace("SOCIAL_ACTION_", "") \
.replace("GAME_ANTICHEAT_ACTION_", "") \
.replace("GAME_BACKGROUND_MODE_ACTION_", "") \
.replace("GAME_IAP_ACTION_", "") \
.replace("GAME_LOCATION_AWARENESS_ACTION_", "") \
.replace("GAME_ACCOUNT_REGISTRY_ACTION_", "") \
.replace("GAME_FITNESS_ACTION_", "") \
.replace("TITAN_PLAYER_SUBMISSION_ACTION_", "")
def DecoderInternalGarPayloadAsResponse(method: int, data: any) -> any:
"""EXACT replica of JavaScript DecoderInternalGarPayloadAsResponse"""
global action_gar_proxy
action_gar_proxy = 0
if not data:
return {}
# For now, return basic implementation
# TODO: Implement POGOProtos.Rpc.InternalCreateSharedLoginTokenResponse.decode
return {"Not_Implemented_yet": data, "method": method}
def DecoderInternalPayloadAsResponse(method: int, data: any) -> any:
"""EXACT replica of JavaScript DecoderInternalPayloadAsResponse"""
global action_social
action_social = 0
if not data:
return {}
# Find method in requestMessagesResponses - EXACT replica of JavaScript logic
for method_name, proto_tuple in requestMessagesResponses.items():
my_req = proto_tuple[0]
if my_req == method:
if proto_tuple[2] is not None and data and len(b64Decode(data)) > 0:
try:
# TODO: Implement proto_tuple[2].decode(b64Decode(data)).toJSON()
# For now, return basic implementation
result = {"decoded": b64Decode(data).hex(), "method": method}
except Exception as error:
print(f"Internal ProxySocial decoder {my_req} Error: {error}")
result = {
"Error": str(error),
"Data": data
}
return result
return result
return {"Not_Implemented_yet": data, "method": method}
def decodePayloadTraffic(methodId: int, content: any, dataType: str) -> list:
"""EXACT replica of JavaScript decodePayloadTraffic"""
global action_social, action_gar_proxy
parsed_proto_data = []
# Add logging - EXACT replica of JavaScript console.log
logging.info(f"decodePayloadTraffic: methodId={methodId}, dataType={dataType}, content={content}")
decoded_proto = decodeProto(methodId, content, dataType)
if not isinstance(decoded_proto, str):
parsed_proto_data.append(decoded_proto)
logging.info(f"Successfully decoded proto: {decoded_proto}")
else:
logging.warning(f"Failed to decode proto: {decoded_proto}")
return parsed_proto_data
def decodePayload(contents: any, dataType: str) -> list:
"""EXACT replica of JavaScript decodePayload"""
global action_social, action_gar_proxy
parsed_proto_data = []
for proto in contents:
method_id = proto.get('method', 0)
data = proto.get('data', '')
decoded_proto = decodeProto(method_id, data, dataType)
if not isinstance(decoded_proto, str):
parsed_proto_data.append(decoded_proto)
return parsed_proto_data
def decodeProto(method: int, data: str, dataType: str) -> dict | str:
"""EXACT replica of JavaScript decodeProto"""
global action_social, action_gar_proxy
return_object = "Not Found"
method_found = False
# EXACT replica of JavaScript loop through requestMessagesResponses
for method_name, found_method in requestMessagesResponses.items():
found_req = found_method[0]
if found_req == method:
method_found = True
if found_method[1] is not None and dataType == "request":
try:
if not data or data == "":
parsed_data = {}
else:
# EXACT replica of JavaScript: foundMethod[1].decode(b64Decode(data)).toJSON()
proto_class = get_protobuf_class(method, "request")
if proto_class:
parsed_data = decode_protobuf_data(proto_class, b64Decode(data))
else:
parsed_data = {"decoded": b64Decode(data).hex(), "error": "Proto class not found"}
# EXACT replica of JavaScript special handling for method 5012
if found_req == 5012:
action_social = parsed_data.get("action", 0)
# Find social action method - EXACT replica of JavaScript
if action_social > 0:
social_proto_class = get_protobuf_class(action_social, "request")
if social_proto_class:
payload_data = parsed_data.get("payload", "")
if payload_data and b64Decode(payload_data):
try:
parsed_data["payload"] = decode_protobuf_data(social_proto_class, b64Decode(payload_data))
except:
pass
break
# EXACT replica of JavaScript special handling for method 600005
elif found_req == 600005:
action_gar_proxy = parsed_data.get("action", 0)
if action_gar_proxy == 4:
payload_data = parsed_data.get("payload", "")
if payload_data:
# TODO: Implement POGOProtos.Rpc.InternalGarAccountInfoProto.decode
parsed_data["payload"] = {"decoded": b64Decode(payload_data).hex()}
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name),
"data": parsed_data,
}
except Exception as error:
print(f"Error parsing request {method_name} -> {error}")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [PARSE ERROR]",
"data": {
"error": "Failed to decode proto",
"rawBase64": data,
"errorMessage": str(error)
},
}
elif dataType == "request":
print(f"Request {found_req} Not Implemented")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [NOT IMPLEMENTED]",
"data": {
"error": "Proto not implemented",
"rawBase64": data
},
}
if found_method[2] is not None and dataType == "response":
try:
if not data or data == "":
parsed_data = {}
else:
# EXACT replica of JavaScript: foundMethod[2].decode(b64Decode(data)).toJSON()
proto_class = get_protobuf_class(method, "response")
if proto_class:
parsed_data = decode_protobuf_data(proto_class, b64Decode(data))
else:
parsed_data = {"decoded": b64Decode(data).hex(), "error": "Proto class not found"}
# EXACT replica of JavaScript special handling for method 5012
if found_req == 5012:
if action_social > 0:
social_proto_class = get_protobuf_class(action_social, "response")
if social_proto_class:
payload_data = parsed_data.get("payload", "")
if payload_data:
try:
parsed_data["payload"] = decode_protobuf_data(social_proto_class, b64Decode(payload_data))
except:
pass
# EXACT replica of JavaScript special handling for method 600005
elif found_req == 600005:
if action_gar_proxy > 0:
payload_data = parsed_data.get("payload", "")
if payload_data:
parsed_data["payload"] = DecoderInternalGarPayloadAsResponse(action_gar_proxy, payload_data)
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name),
"data": parsed_data,
}
except Exception as error:
print(f"Error parsing response {method_name} method: [{found_req}] -> {error}")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [PARSE ERROR]",
"data": {
"error": "Failed to decode proto",
"rawBase64": data,
"errorMessage": str(error)
},
}
elif dataType == "response":
print(f"Response {found_req} Not Implemented")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [NOT IMPLEMENTED]",
"data": {
"error": "Proto not implemented",
"rawBase64": data
},
}
if not method_found and return_object == "Not Found":
return_object = {
"methodId": str(method),
"methodName": f"Unknown Method {method} [UNKNOWN]",
"data": {
"error": "Unknown method ID",
"rawBase64": data
},
}
return return_object
def decodeProtoFromBytes(method: int, data: bytes, dataType: str) -> dict | str:
"""EXACT replica of JavaScript decodeProtoFromBytes"""
global action_social, action_gar_proxy
return_object = "Not Found"
method_found = False
# EXACT replica of JavaScript loop through requestMessagesResponses
for method_name, found_method in requestMessagesResponses.items():
found_req = found_method[0]
if found_req == method:
method_found = True
if found_method[1] is not None and dataType == "request":
try:
if not data or len(data) == 0:
parsed_data = {}
else:
# TODO: Implement found_method[1].decode(data).toJSON()
# For now, return basic implementation
parsed_data = {"decoded": data.hex()}
# EXACT replica of JavaScript special handling for method 5012
if found_req == 5012:
action_social = parsed_data.get("action", 0)
# Find social action method - EXACT replica of JavaScript
for social_method_name, social_method in requestMessagesResponses.items():
social_req = social_method[0]
if social_req == action_social and social_method[1] is not None:
payload_data = parsed_data.get("payload", "")
if payload_data and b64Decode(payload_data):
try:
# TODO: Implement social_method[1].decode(b64Decode(payload_data)).toJSON()
parsed_data["payload"] = {"decoded": b64Decode(payload_data).hex()}
except:
pass
break
# EXACT replica of JavaScript special handling for method 600005
elif found_req == 600005:
action_gar_proxy = parsed_data.get("action", 0)
if action_gar_proxy == 4:
payload_data = parsed_data.get("payload", "")
if payload_data:
# TODO: Implement POGOProtos.Rpc.InternalGarAccountInfoProto.decode
parsed_data["payload"] = {"decoded": payload_data.hex()}
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name),
"data": parsed_data,
}
except Exception as error:
print(f"Error parsing request {method_name} -> {error}")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [PARSE ERROR]",
"data": {
"error": "Failed to decode proto",
"rawBase64": data.hex(),
"errorMessage": str(error)
},
}
elif dataType == "request":
print(f"Request {found_req} Not Implemented")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [NOT IMPLEMENTED]",
"data": {
"error": "Proto not implemented",
"rawBase64": data.hex()
},
}
if found_method[2] is not None and dataType == "response":
try:
if not data or len(data) == 0:
parsed_data = {}
else:
# TODO: Implement found_method[2].decode(data).toJSON()
# For now, return basic implementation
parsed_data = {"decoded": data.hex()}
# EXACT replica of JavaScript special handling for method 5012
if found_req == 5012:
if action_social > 0:
payload_data = parsed_data.get("payload", "")
if payload_data:
parsed_data["payload"] = DecoderInternalPayloadAsResponse(action_social, payload_data)
# EXACT replica of JavaScript special handling for method 600005
elif found_req == 600005:
if action_gar_proxy > 0:
payload_data = parsed_data.get("payload", "")
if payload_data:
parsed_data["payload"] = DecoderInternalGarPayloadAsResponse(action_gar_proxy, payload_data)
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name),
"data": parsed_data,
}
except Exception as error:
print(f"Error parsing response {method_name} method: [{found_req}] -> {error}")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [PARSE ERROR]",
"data": {
"error": "Failed to decode proto",
"rawBase64": data.hex(),
"errorMessage": str(error)
},
}
elif dataType == "response":
print(f"Response {found_req} Not Implemented")
return_object = {
"methodId": found_req,
"methodName": remasterOrCleanMethodString(method_name) + " [NOT IMPLEMENTED]",
"data": {
"error": "Proto not implemented",
"rawBase64": data.hex()
},
}
if not method_found and return_object == "Not Found":
return_object = {
"methodId": str(method),
"methodName": f"Unknown Method {method} [UNKNOWN]",
"data": {
"error": "Unknown method ID",
"rawBase64": data.hex()
},
}
return return_object
# Export functions - EXACT replica of JavaScript exports
__all__ = [
'decodePayloadTraffic',
'decodePayload',
'decodeProto',
'decodeProtoFromBytes',
'remasterOrCleanMethodString',
'b64Decode',
'DecoderInternalGarPayloadAsResponse',
'DecoderInternalPayloadAsResponse'
]