script.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # coding:utf8
  2. import os
  3. import logging
  4. import re
  5. import glob
  6. import shutil
  7. import distutils.dir_util
  8. import gw_file_system
  9. import xml.etree.ElementTree as ET
  10. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  11. def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  12. logging.info('game_pack_info: ' + str(channel_sdk_info))
  13. # 逆向渠道id
  14. if channel_sdk_info['id'] == "323":
  15. fixLifecycleMethod(extract_dir)
  16. return
  17. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  18. if channel_sdk_info['SDKName'] == 'x7sy':
  19. logging.info('workspace_sdk_dir: ' + str(workspace_sdk_dir))
  20. s1 = os.path.join(workspace_sdk_dir, 'x7sy/ForRes/drawable')
  21. x7_bg_female_pink_7_xml = os.path.join(s1, "x7_bg_female_pink_7.xml")
  22. x7_bg_female_pink_right_7 = os.path.join(s1, "x7_bg_female_pink_right_7.xml")
  23. if os.path.exists(x7_bg_female_pink_7_xml):
  24. os.remove(x7_bg_female_pink_7_xml)
  25. if os.path.exists(x7_bg_female_pink_right_7):
  26. os.remove(x7_bg_female_pink_right_7)
  27. s1 = os.path.join(workspace_sdk_dir, 'x7sy/ForRes/values/public.xml')
  28. handle_public_xml(s1)
  29. return
  30. def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  31. logging.info('game_pack_info: ' + str(channel_sdk_info))
  32. fix_android_manifest(extract_dir)
  33. if channel_sdk_info['id'] == "512":
  34. s1 = os.path.join(extract_dir, "smali_classes4", "com/netease")
  35. smali_dir = create_mutil_smali(extract_dir)
  36. s3 = os.path.join(smali_dir, "com/netease")
  37. list_package = ["androidcrashhandler","autotext","environment","godlikeshare","htprotect","neox","ngpush","ntunisdk"]
  38. if not os.path.exists(s3):
  39. distutils.dir_util.mkpath(s3)
  40. move_package(s1, s3, list_package)
  41. replace_kf_application(extract_dir)
  42. if channel_sdk_info['id'] == "323":
  43. change_smali(extract_dir)
  44. s1 = os.path.join(extract_dir, "smali", "androidx")
  45. s3 = os.path.join(extract_dir, "smali_classes3", "androidx")
  46. if not os.path.exists(s3):
  47. distutils.dir_util.mkpath(s3)
  48. move_package_one(s1, s3)
  49. s1 = os.path.join(extract_dir, "smali_classes3", "androidx")
  50. s3 = os.path.join(extract_dir, "smali_classes4", "androidx")
  51. list_package = ["a", "appcompat", "b", "c", "coordinatorlayout", "customview", "activity",
  52. "asynclayoutinflater", "browser", "cardview", "cor"]
  53. if not os.path.exists(s3):
  54. distutils.dir_util.mkpath(s3)
  55. move_package(s1, s3, list_package)
  56. if channel_sdk_info['SDKName'] == 'smallsheep':
  57. move_package_for_small_sheep(extract_dir)
  58. if channel_sdk_info['SDKName'] == 'x7sy':
  59. s1 = os.path.join(extract_dir, "smali_classes2", "com")
  60. smali_classes2_dir = create_mutil_smali(extract_dir)
  61. s3 = os.path.join(smali_classes2_dir, "com")
  62. list_package = ["a", "adjust", "alipayx", "cloudplay", "google", "heepay", "helpshift", "iapppay", "ipaynow"]
  63. if not os.path.exists(s3):
  64. distutils.dir_util.mkpath(s3)
  65. move_package(s1, s3, list_package)
  66. s1 = os.path.join(extract_dir, "smali_classes3", "androidx")
  67. smali_classes2_dir = create_mutil_smali(extract_dir)
  68. s3 = os.path.join(smali_classes2_dir, "androidx")
  69. list_package = ["core", "d", "drawerlayout", "e", "f", "fragment", "legacy", "lifecycle", "loader", "media",
  70. "multidex"]
  71. if not os.path.exists(s3):
  72. distutils.dir_util.mkpath(s3)
  73. move_package(s1, s3, list_package)
  74. if channel_sdk_info['SDKName'] == 'jolo':
  75. s1 = os.path.join(extract_dir, "smali_classes3", "androidx")
  76. s3 = os.path.join(extract_dir, "smali_classes5", "androidx")
  77. list_package = ["core", "d", "drawerlayout", "e", "f", "fragment", "legacy", "lifecycle", "loader"]
  78. if not os.path.exists(s3):
  79. distutils.dir_util.mkpath(s3)
  80. move_package(s1, s3, list_package)
  81. if channel_sdk_info['SDKName'] == 'sogou':
  82. rm_wechatpay(extract_dir)
  83. if channel_sdk_info['SDKName'] == 'xxzhushou':
  84. rm_alipay(extract_dir)
  85. if channel_sdk_info['id'] == "389":
  86. s1 = os.path.join(extract_dir, "smali", "com")
  87. smali_classes2_dir = create_mutil_smali(extract_dir)
  88. s3 = os.path.join(smali_classes2_dir, "com")
  89. list_package = ["smwl", "netease"]
  90. if not os.path.exists(s3):
  91. distutils.dir_util.mkpath(s3)
  92. move_package(s1, s3, list_package)
  93. if channel_sdk_info['id'] == "549" or channel_sdk_info['id'] == "591":
  94. s1 = os.path.join(extract_dir, "smali_classes5", "com")
  95. smali_classes2_dir = create_mutil_smali(extract_dir)
  96. s3 = os.path.join(smali_classes2_dir, "com")
  97. list_package = ["netease"]
  98. if not os.path.exists(s3):
  99. distutils.dir_util.mkpath(s3)
  100. move_package(s1, s3, list_package)
  101. if channel_sdk_info['id'] == "591" or channel_sdk_info['id'] == "549" :
  102. s1 = os.path.join(extract_dir, "smali_classes4", "com")
  103. smali_classes2_dir = create_mutil_smali(extract_dir)
  104. s3 = os.path.join(smali_classes2_dir, "com")
  105. list_package = ["netease"]
  106. if not os.path.exists(s3):
  107. distutils.dir_util.mkpath(s3)
  108. move_package(s1, s3, list_package)
  109. return
  110. def fix_android_manifest(decompile_dir):
  111. manifest_file = decompile_dir + '/AndroidManifest.xml'
  112. ET.register_namespace('android', ANDROID_NS)
  113. key_android_name = '{' + ANDROID_NS + '}authorities'
  114. target_tree = ET.parse(manifest_file)
  115. target_root = target_tree.getroot()
  116. manifest_node = target_root.get("package")
  117. application_node = target_root.find('application')
  118. if application_node is None:
  119. return
  120. if application_node is not None:
  121. activity_list_node = application_node.findall('provider')
  122. if activity_list_node is None:
  123. return
  124. for activityNode in activity_list_node:
  125. node_name_str = activityNode.attrib[key_android_name]
  126. if node_name_str == 'com.netease.onmyoji.coolpad111':
  127. activityNode.set(key_android_name, manifest_node + '111')
  128. target_tree.write(manifest_file)
  129. def create_mutil_smali(decompileDir):
  130. f_idx = 5
  131. while True:
  132. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  133. tmp = tmp.replace('\\', '/')
  134. tmp = re.sub('/+', '/', tmp)
  135. if os.path.exists(tmp):
  136. f_idx += 1
  137. else:
  138. smali_classes2_dir = tmp
  139. break
  140. # endwhile
  141. os.mkdir(smali_classes2_dir)
  142. return smali_classes2_dir
  143. def move_package(s1, s3, list_package):
  144. for d in list_package:
  145. src = os.path.join(s1, d)
  146. if os.path.exists(src):
  147. dst = os.path.join(s3, d)
  148. distutils.dir_util.copy_tree(src, dst)
  149. distutils.dir_util.remove_tree(src)
  150. def move_package_one(src, dst):
  151. if os.path.exists(src):
  152. distutils.dir_util.copy_tree(src, dst)
  153. distutils.dir_util.remove_tree(src)
  154. def rm_alipay(extract_dir):
  155. delet_file = os.path.join(extract_dir, 'smali/org/json')
  156. print delet_file
  157. logging.info('delet_file: ' + delet_file)
  158. shutil.rmtree(delet_file)
  159. def rm_wechatpay(extract_dir):
  160. delet = os.path.join(extract_dir, 'smali_classes2/com/ipaynow')
  161. print delet
  162. logging.info(delet)
  163. shutil.rmtree(delet)
  164. def change_smali(fooDir):
  165. for smali_flie in glob.glob(fooDir + "/" + "smali*/com/kunlun/platform/android/KunlunApplication.smali"):
  166. print smali_flie
  167. if os.path.isfile(smali_flie):
  168. with open(smali_flie, 'r+') as f:
  169. cont = f.read()
  170. change_str = 'invoke-virtual {p0}, Lcom/kf/framework/KFApplication;->onCreate()V'
  171. str = 'invoke-virtual {p0}, Landroid/app/Application;->onCreate()V'
  172. print len(change_str)
  173. print len(str)
  174. change_str2 = 'invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V'
  175. str2 = 'invoke-virtual {v0, p1}, Landroid/app/Application;->onConfigurationChanged(Landroid/content/res/Configuration;)V'
  176. cont = cont.replace(change_str, str)
  177. cont = cont.replace(change_str2, str2)
  178. with open(smali_flie, 'w') as tf:
  179. tf.write(cont)
  180. def intoFirstLine(target, repl, cont):
  181. # srcStr = "onBackPressed"
  182. # regexp = '.method (?:public|protected) +?'+target+"\([^.]+?\.locals +[0-9]{1,2}"
  183. regexp = '.method (?:public|protected) +?' + target + "\([^.]+?\.locals +([0-9]{1,2})"
  184. # print(regexp)
  185. reObj = re.compile(regexp)
  186. matchO = reObj.search(cont)
  187. if matchO:
  188. # print(matchO.group(0))
  189. methodHead = matchO.group(0)
  190. if matchO.group(1) == "0":
  191. methodHead = re.sub("locals +0", "locals 1", methodHead)
  192. repl = methodHead + r'\n\n ' + repl + r'\n\n'
  193. cont = reObj.sub(repl, cont)
  194. else:
  195. # todo call .super method
  196. cont += r'.method protected ' + target + r'()V\n\n .locals 0\n\n ' + repl + r'\n\n .end method\n'
  197. return cont
  198. def fixLifecycleMethod(preDir):
  199. for tfile in glob.glob(preDir + "/" + "smali*/com/supercell/titan/kunlun/GameAppKunlun.smali"):
  200. if os.path.isfile(tfile):
  201. with open(tfile, 'r+') as f:
  202. cont = f.read()
  203. changes = {
  204. "onPause": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onPause()V",
  205. "onResume": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onResume()V",
  206. "onDestroy": "invoke-static {p0}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->destroy(Landroid/content/Context;)V",
  207. "onStop": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onStop()V"
  208. }
  209. for k, v in changes.iteritems():
  210. cont = intoFirstLine(k, v, cont)
  211. f.seek(0)
  212. f.write(cont)
  213. f.close()
  214. def handle_public_xml(public_xml_path):
  215. if not os.path.exists(public_xml_path):
  216. logging.info('public_xml is null: ' + public_xml_path)
  217. return
  218. new_lines = []
  219. with open(public_xml_path, 'r+') as f:
  220. for line in f.readlines():
  221. # l = line.strip()
  222. if line.find("x7_bg_female_pink_7") > -1:
  223. continue
  224. if line.find("x7_icon_female_pink") > -1:
  225. continue
  226. if line.find("x7_bg_female_pink_right_7") > -1:
  227. continue
  228. new_lines.append(line)
  229. f.seek(0)
  230. f.truncate()
  231. f.writelines(new_lines)
  232. return
  233. def move_package_for_small_sheep(extract_dir):
  234. s1 = os.path.join(extract_dir, "smali_classes4", "com/netease")
  235. smali_classes2_dir = create_mutil_smali(extract_dir)
  236. s3 = os.path.join(smali_classes2_dir, "com/netease")
  237. list_package = ["mpay", "neox", "ngpush", "ntunisdk", "onmyoji", "pharos", "tryautotext2", "unisdk", "xyqa"]
  238. if not os.path.exists(s3):
  239. distutils.dir_util.mkpath(s3)
  240. move_package(s1, s3, list_package)
  241. lines = ['invoke-virtual {p3}, Lcom/kf/framework/KFApplication;->getApplicationInfo()Landroid/content/pm/ApplicationInfo;',
  242. 'invoke-virtual {v0}, Lcom/kf/framework/KFApplication;->onCreate()V']
  243. def replace_kf_application(extract_dir):
  244. # goal_file = glob.glob(os.path.join(extract_dir, 'smali*/com/netease/ntunisdk/unifix_hotfix_library/proxyApplication/UFProxyApplication.smali'))[0]
  245. goal_file_list = glob.glob(os.path.join(extract_dir, 'smali*/com/netease/ntunisdk/unifix_hotfix_library/proxyApplication/UFProxyApplication.smali'))
  246. if len(goal_file_list)==0:
  247. logging.info("没有UFPROXYAPPlication")
  248. return
  249. goal_file=goal_file_list[0]
  250. print(os.path.isfile(goal_file))
  251. if os.path.isfile(goal_file):
  252. logging.info('method_proceed: ' + 'goal_file')
  253. print(goal_file)
  254. with open(goal_file, 'r+') as f:
  255. cont = f.read()
  256. for line in lines:
  257. change_line = line.replace('Lcom/kf/framework/KFApplication',
  258. 'Landroid/app/Application')
  259. cont = cont.replace(line, change_line)
  260. logging.info('replace_str: ' + cont)
  261. with open(goal_file, 'w+') as tf:
  262. tf.write(cont)
  263. if __name__ == '__main__':
  264. replace_kf_application("D:\\work\\yys")