-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.py
149 lines (119 loc) · 4.41 KB
/
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
#!/usr/bin/env python
from mitmproxy.models import decoded
import queue
import protocol
import subprocess
import argparse
import sys
# A map of request ids to FIFO data structures to keep track of request types
# becauses responses don't have an explicit type; the type is infered from the
# requested information.
requests = {};
def request(ctx, flow):
if not flow.match("~d pgorelease.nianticlabs.com"):
return
with decoded(flow.request):
req = protocol.RequestEnvelope()
req.ParseFromString(flow.request.content)
if req.id in requests:
ctx.log("Duplicate Request", req.id)
requests[req.id] = queue.Queue()
for request in req.requests:
messageName = toCamelCase(protocol.Method.Name(request.method))
requests[req.id].put(messageName)
if args.filter and messageName not in args.filter:
continue
if args.ignore and messageName in args.ignore:
continue
messageName += "Request"
print("Request: (%s, %s)" % (messageName, request.method))
if not request.payload:
continue
if args.always_raw:
print("Request: Raw (type: %s) (name: %s)" %
(request.method, messageName))
printRawMessage(request.payload)
else:
try:
MessageType = getattr(protocol, messageName)
Message = MessageType()
Message.ParseFromString(request.payload)
print(Message)
except:
print("Request: Unknown Message (name: %s)" % messageName)
printRawMessage(request.payload)
def response(ctx, flow):
if not flow.match("~d pgorelease.nianticlabs.com"):
return
with decoded(flow.response):
resp = protocol.ResponseEnvelope()
resp.ParseFromString(flow.response.content)
i = -1
while not requests[resp.id].empty():
i += 1
requestName = requests[resp.id].get()
if args.filter and requestName not in args.filter:
continue
if args.ignore and requestName in args.ignore:
continue
request = requestName + "Response"
print("Response: (%s)" % request)
if not resp.responses[i]:
continue
if args.always_raw:
print("Response: Unknown Message (name: %s)"
% request)
printRawMessage(resp.responses[i])
else:
try:
MessageType = getattr(protocol, request)
Message = MessageType()
Message.ParseFromString(resp.responses[i])
print(Message)
except:
print("Response: Unknown Message (name: %s)"
% request)
printRawMessage(resp.responses[i])
del requests[resp.id]
def start(ctx, argv):
global args
parser = argparse.ArgumentParser(
description="A tool for reverse engineering and viewing pokemon go messages."
)
rawGroup = parser.add_mutually_exclusive_group()
rawGroup.add_argument("--no-raw",
help="Disable printing raw messages",
action='store_true',
default=False
)
rawGroup.add_argument("--always-raw",
help="Ignore proto files and decode as raw message",
action='store_true',
default=False
)
filterGroup = parser.add_mutually_exclusive_group()
filterGroup.add_argument("--filter",
help="Only request names specified with this flag will be shown",
nargs="+",
type=str,
)
filterGroup.add_argument("--ignore",
help="Request names specified with this flag will be removed",
nargs="+",
type=str,
)
args = parser.parse_args(argv[1:])
# Conditionally prints a protobuf to stdout using protoc --decode-raw
def printRawMessage(encoded):
if args.no_raw:
return
protoc = subprocess.Popen(
['protoc', '--decode_raw'],
stdin=subprocess.PIPE, stdout=sys.stdout
)
protoc.communicate(input=encoded)
# Converts functions from snake case to upper camel case.
def toCamelCase(string):
string = string.lower()
pieces = string.split("_")
return "".join(piece.title() for piece in pieces)