This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprotocol_info_generator_objdump.py
150 lines (135 loc) · 5.74 KB
/
protocol_info_generator_objdump.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
import subprocess
import re
import struct
from version import Version
from protocol_info_generator import generate_stuff
import sys
import os
import threading
if len(sys.argv) < 3:
exit("Required args: BDS binary path, path to BedrockProtocol")
def convert_windows_path(path):
return path.replace('\\', '/').replace('C:', '/mnt/c')
bds_path = convert_windows_path(sys.argv[1])
bedrockprotocol_path = convert_windows_path(sys.argv[2])
print 'Dumping data from ' + bds_path
print 'BedrockProtocol path ' + bedrockprotocol_path
asm_regex = re.compile(r'.*\$(0x[A-Fa-f\d]+),%eax.*')
symbol_match_regex = re.compile(r'([\da-zA-Z]+) (.{7}) (\.[A-Za-z\d_]+)\s+([\da-zA-Z]+)\s+(?:Base|\.hidden)?\s+(.+)')
rodata_offset_regex = re.compile(r"^\s*\d+\s+\.rodata\s+[\da-f]+\s+[\da-f]+\s+([\da-f]+)\s+([\da-f]+)")
def get_value_at(file, offset, size, format):
file = open(file, 'rb')
if offset == None or offset < 0:
return -1
file.seek(offset)
return struct.unpack(format, file.read(size))[0]
def stop_address(start, size):
return hex(int('0x' + start, 16) + int('0x' + size, 16))
def dump_packet_id(start, size, symbol):
proc = subprocess.Popen(['objdump', '--disassemble', '--demangle', '--section=.text', '--start-address=0x' + start, '--stop-address=' + stop_address(start, size), bds_path], stdout=subprocess.PIPE)
lines = []
while True:
line = proc.stdout.readline()
lines.append(line)
if not line:
break
parts = line.split('mov')
if len(parts) < 2:
continue
matches = re.match(asm_regex, parts[1])
if not matches:
continue
return int(matches.groups()[0], 16)
for l in lines:
print l
raise Exception("Packet ID not found for symbol " + symbol)
def dump_packet_id_threaded(start, size, symbol, packet_name, packets, packets_lock):
id = dump_packet_id(start, size, symbol)
packets_lock.acquire()
packets[id] = packet_name
print 'Found ' + packet_name + ' ' + hex(id)
packets_lock.release()
def parse_symbol(symbol):
parts = re.match(symbol_match_regex, symbol)
if not parts:
raise Exception("Regex match failed for \"" + symbol + "\"")
if len(parts.groups()) < 5:
raise Exception("Wrong number of matches for \"" + symbol + "\" " + str(len(parts.groups())))
start = parts.groups()[0]
flags = parts.groups()[1]
section = parts.groups()[2]
size = parts.groups()[3]
symbol = parts.groups()[4]
return start, flags, section, size, symbol
def dump_packet_ids():
packets = {}
packets_lock = threading.Lock()
threads = [None] * 8
proc = subprocess.Popen(['objdump --demangle -tT --dwarf=follow-links \'' + bds_path + '\' | grep Packet | grep \'::getId()\''], shell=True, stdout=subprocess.PIPE)
while True:
symbol = proc.stdout.readline()
if not symbol:
break
start, _, _, size, symbol = parse_symbol(symbol)
packet_name = symbol.split('::')[0]
thread_index = None
for i in range(len(threads)):
if threads[i] is None:
thread_index = i
break
while thread_index is None:
for i in range(len(threads)):
threads[i].join(0.1)
if not threads[i].isAlive():
thread_index = i
threads[i] = None
break
t = threading.Thread(target=dump_packet_id_threaded, args=(start, size, symbol, packet_name, packets, packets_lock))
t.start()
threads[i] = t
for t in threads:
t.join()
return packets
def get_rodata_file_shift():
proc = subprocess.Popen(['objdump -h -j \'.rodata\' \'' + bds_path + '\''], shell=True, stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
matches = re.match(rodata_offset_regex, line.strip())
if matches:
lma = int('0x' + matches.groups()[0], 16)
physical_address = int('0x' + matches.groups()[1], 16)
return physical_address - lma
raise Exception("Unable to calculate offset for .rodata")
def dump_version():
rodata_shift = get_rodata_file_shift()
proc = subprocess.Popen(['objdump --demangle -tT --dwarf=follow-links \'' + bds_path + '\' | grep SharedConstants'], shell=True, stdout=subprocess.PIPE)
major = None
minor = None
patch = None
revision = None
beta = False
protocol = None
while True:
symbol = proc.stdout.readline()
if not symbol:
break
start, _, _, size, symbol = parse_symbol(symbol)
if symbol.endswith('MajorVersion'):
major = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'i')
elif symbol.endswith('MinorVersion'):
minor = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'i')
elif symbol.endswith('PatchVersion'):
patch = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'i')
elif symbol.endswith('RevisionVersion'):
revision = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'i')
elif symbol.endswith('IsBeta'):
beta = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'B') == 1
elif symbol.endswith('NetworkProtocolVersion'):
protocol = get_value_at(bds_path, int('0x' + start, 16) + rodata_shift, int('0x' + size, 16), 'i')
print major, minor, patch, revision, beta, protocol
return Version(major, minor, patch, revision, beta, protocol)
version = dump_version()
packets = dump_packet_ids()
generate_stuff(packets, version, bedrockprotocol_path)