Skip to content

Instantly share code, notes, and snippets.

@extremeheat
Created February 25, 2018 22:07
Show Gist options
  • Save extremeheat/dd4e5050807cb2d3c384beca16301bfc to your computer and use it in GitHub Desktop.
Save extremeheat/dd4e5050807cb2d3c384beca16301bfc to your computer and use it in GitHub Desktop.
libminecraftpe objdump parser for MCPE network packets
import re
import json
LST_FILE = 'PATH_TO_OBJDUMPED_FILE'
def read_file(file_name):
file = open(file_name, 'r')
return file.readlines()
reading_packet_readwrite = False
reading_packet_id = False
reading_network_id = False
packet_map = {}
current_packet = None
print "// reading file '%s'..." % LST_FILE
lines = read_file(LST_FILE)
print "// read file."
# save some cpu
found_text_section = False
def handle_branch(subroutine_addr):
# TODO: handle branch instructions
pass
for line in lines:
if not found_text_section:
if "Disassembly of section .text:" in line:
found_text_section = True
continue
if reading_packet_readwrite:
if not line.startswith(' '):
reading_packet_readwrite = False
current_packet = None
else:
#print line # print instruction
m = re.match(".*Stream::(.*?)\(", line)
if m:
print " \t", line.strip()
current_packet.append(m.group(1))
else:
print " //", line.strip()
elif reading_packet_id:
if not line.startswith(' '):
reading_packet_id = False
current_packet = None
else:
m = re.match(".*movs.*#(\d+)", line)
if m:
current_packet['id'] = int(m.group(1))
elif reading_network_id:
if not line.startswith(' '):
reading_network_id = False
else:
m = re.match(".*?\t([0-9a-z]+)", line)
if m:
v = int(m.group(1), 16)
print "NETWORK VERSION IS", v
else:
# if line.startswith(' '):
# continue # reading a subroutine
m = re.match("^[a-f0-9]+ \<([a-zA-Z]+Packet)::(read|write)", line)
if m:
reading_packet_readwrite = True
apacket = m.group(1)
atype = m.group(2)
if apacket not in packet_map:
packet_map[apacket] = {
atype: []
}
elif atype not in packet_map[apacket]:
packet_map[apacket][atype] = []
if "lambda" in line:
packet_map[apacket]['ContainsExtraRecursiveData'] = True
current_packet = packet_map[apacket][atype]
print line
m2 = re.match("^[a-f0-9]+ \<([a-zA-Z]+Packet)::(getId)", line)
if m2:
reading_packet_id = True
apacket = m2.group(1)
if apacket not in packet_map:
packet_map[apacket] = {}
current_packet = packet_map[apacket]
print line
m3 = re.match("^[a-f0-9]+ \<SharedConstants::NetworkProtocolVersion", line)
if m3:
reading_network_id = True
print json.dumps(packet_map, indent=4)
#r"(.*)Packet::write"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment