Skip to content

Instantly share code, notes, and snippets.

@shuax
Created May 18, 2017 12:59
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shuax/b1a4a0cb4bc12b8d8e1b01810223c41c to your computer and use it in GitHub Desktop.
Save shuax/b1a4a0cb4bc12b8d8e1b01810223c41c to your computer and use it in GitHub Desktop.
import zipfile
import xxtea
import re
import os
# setXXTEAKeyAndSign
apk = zipfile.ZipFile("xxxx.apk", "r")
probe_file = []
for filename in apk.namelist():
if filename.endswith(".luac"):
probe_file.append(apk.read(filename))
print ('File:', filename)
if len(probe_file)==2:
break
sign = []
for i in range(len(probe_file[0])):
if probe_file[0][i]!=probe_file[1][i]:
sign= probe_file[0][:i]
break
print('Sign:', sign, len(sign))
key = []
pattern = re.compile("(\w+)\0")
for filename in apk.namelist():
if filename.endswith("libcocos2dlua.so"):
print ('File:', filename)
content = apk.read(filename)
pos = content.find(sign)
subcontent = content[pos-512:pos+512]
sign = sign.decode()
strlist = pattern.findall(subcontent.decode("ascii", "ignore"))
for index, found_str in enumerate(strlist):
if found_str==sign:
key = strlist[index-1].encode('ascii')
print('Key:', key)
for filename in apk.namelist():
if filename.endswith(".luac"):
content = apk.read(filename)
data = b''
data = xxtea.decrypt(content[len(sign):], key)
filename = os.path.splitext(filename)[0] + ".lua"
os.makedirs(os.path.dirname(filename), exist_ok=True)
print(filename)
with open(filename, 'wb') as decrypted_file:
decrypted_file.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment