Skip to content

Instantly share code, notes, and snippets.

@aperpen
Created July 20, 2018 23:30
Show Gist options
  • Save aperpen/79600a80bf64a9df40a171324c951212 to your computer and use it in GitHub Desktop.
Save aperpen/79600a80bf64a9df40a171324c951212 to your computer and use it in GitHub Desktop.
Decrypt Supercell Clash Royale XML files
from Crypto.Cipher import AES
import base64
import hashlib
import xml.etree.ElementTree
import sys
package_name = "com.supercell.clashroyale"
def build_storage_new_key():
key_map = [102, 76, 120, 89, 66, 57, 77, 56, 52, 65, 98, 101, 117, 115, 69, 82, 77, 89, 57, 89, 70, 122, 86, 71]
out = ""
for i1 in range(len(package_name) - 1, -1, -1):
i2 = ord(package_name[i1])
i3 = key_map[i1 % 24]
out += chr((((i2 ^ i3) & 0x1F) + 48))
return out
class AESCipher:
def __init__(self, key, mode):
self.bs = 16
self.mode = mode
self.key = hashlib.sha256(key.encode()).digest()
self.iv_key = False
if mode == AES.MODE_CBC:
self.iv_key = b'fldsjfodasjifudslfjdsaofshaufihadsf'[0:self.bs]
def decrypt(self, crypted):
crypted = base64.b64decode(crypted)
cipher = AES.new(self.key, self.mode, self.iv_key) if self.iv_key else AES.new(self.key, self.mode)
return self.unpad(cipher.decrypt(crypted)).decode('utf-8')
def unpad(self, s):
return s[:-ord(s[len(s) - 1:])]
if __name__ == "__main__":
if len(sys.argv) > 1:
file = sys.argv[1]
if file == "storage.xml" or file == 'localPrefs.xml':
if len(sys.argv) == 2:
print(
"[X] The " + file + " key is your androidId, please provide it when running the script: python decrypt.py storage.xml androidId")
sys.exit(0)
else:
key = sys.argv[2]
else:
key = build_storage_new_key()
if file != "storage_new.xml":
print("[!] Couldn't determine filetype. Using storage_new.xml key.")
else:
print("No file specified")
sys.exit(0)
print("[*] Using key " + key)
try:
file = xml.etree.ElementTree.parse(file).getroot()
except FileNotFoundError:
print("File not found")
sys.exit(0)
for entry in file.findall('string'):
key_stream = AESCipher(key, AES.MODE_ECB)
value_stream = AESCipher(key, AES.MODE_CBC)
decrypted_key = key_stream.decrypt(entry.get('name'))
decrypted_string = value_stream.decrypt(entry.text)
print("[+] " + decrypted_key + "=" + decrypted_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment