This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
forked from clanner/cocdp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcocprefs.py
145 lines (104 loc) · 4.01 KB
/
cocprefs.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
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
import argparse
import sys
from base64 import b64encode, b64decode
import xml.etree.ElementTree as ET
from xml.sax.saxutils import escape as xml_escape
from xml.sax.saxutils import unescape as xml_unescape
"""
Tool for decoding or updating these configuration files:
/data/data/com.supercell.clashofclans/shared_prefs/localPrefs.xml
/data/data/com.supercell.clashofclans/shared_prefs/storage.xml
The android id can be found in openudid_prefs.xml
or in adb shell: settings get secure android_id
decoding:
python cocprefs.py --from YOURANDROIDID storage.xml
-> will print the decrypted config file
converting
python cocprefs.py --from YOURANDROIDID --to OTHERANDROIDID storage.xml
-> will print the config file, converted for your OTHERANDROIDID device
encoding
python cocprefs.py --to YOURANDROIDID storage.xml
-> will print the encrypted config for storage.xml
You will have to use adb on a rooted device to get the storage files from the device yourself.
"""
""" ====================================================================== """
def makekeycipher(key):
hashedkey= SHA256.new(key).digest()
return AES.new(hashedkey, AES.MODE_ECB)
def makevalcipher(key):
hashedkey= SHA256.new(key).digest()
return AES.new(hashedkey, AES.MODE_CBC, "fldsjfodasjifuds")
""" ====================================================================== """
def parse_xml(xml):
""" convert xml to config dict """
d={}
root= ET.fromstring(xml)
# root.tag=="map"
for line in list(root):
# line.tag=="string"
k= line.attrib["name"]
v= line.text
d[k]= v
return d
def create_xml(cfg):
""" convert config dict to xml """
xml= "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
xml += "<map>\n"
for k,v in list(cfg.items()):
xml += " <string name=\"%s\">%s</string>\n" % (xml_escape(k), xml_escape(v))
xml += "</map>\n"
return xml
""" ====================================================================== """
def pkcs5_pad(s):
BLOCK_SIZE= 16
return s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
def pkcs5_unpad(s):
return s[0:-ord(s[-1])]
""" ====================================================================== """
def decrypt(value, cipher):
return pkcs5_unpad(cipher.decrypt(b64decode(value)))
def encrypt(value, cipher):
return b64encode(cipher.encrypt(pkcs5_pad(value)))
""" ====================================================================== """
def decode_xml(xml, key):
""" decrypted xml config to config dict """
enc= parse_xml(xml)
if key is None:
return enc
dec= {}
keycipher= makekeycipher(key)
for k, v in list(enc.items()):
valcipher= makevalcipher(key)
dec[decrypt(k, keycipher)]= decrypt(v, valcipher)
return dec
def encode_xml(cfg, key):
""" convert config dict to encrypted xml """
if key is None:
return create_xml(cfg)
enc= {}
keycipher= makekeycipher(key)
for k, v in list(cfg.items()):
valcipher= makevalcipher(key)
enc[encrypt(k, keycipher)]= encrypt(v, valcipher)
return create_xml(enc)
""" ====================================================================== """
def handle_fh(fh, args):
xml= fh.read()
cfg= decode_xml(xml, args.from_)
print(encode_xml(cfg, args.to))
def handle_xmlfile(fn, args):
with open(fn) as fh:
handle_fh(fh, args)
""" ====================================================================== """
parser = argparse.ArgumentParser(description='Clash-of-Clans configuration editor')
parser.add_argument("--from", dest='from_', type=str, help="source android_id")
parser.add_argument("--to", type=str, help="target android_id")
parser.add_argument("xmlfiles", type=str, metavar="XML", nargs="*", help="a xml config file")
args = parser.parse_args()
if not args.xmlfiles:
handle_fh(sys.stdin, args)
else:
for fn in args.xmlfiles:
handle_xmlfile(fn, args)