script.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  10. def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  11. logging.info('game_pack_info: ' + str(channel_sdk_info))
  12. # 逆向渠道id
  13. if channel_sdk_info['id'] == "323":
  14. fixLifecycleMethod(extract_dir)
  15. return
  16. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  17. if channel_sdk_info['SDKName'] == 'x7sy':
  18. logging.info('workspace_sdk_dir: ' + str(workspace_sdk_dir))
  19. s1 = os.path.join(workspace_sdk_dir, 'x7sy/ForRes/drawable')
  20. x7_bg_female_pink_7_xml = os.path.join(s1, "x7_bg_female_pink_7.xml")
  21. x7_bg_female_pink_right_7 = os.path.join(s1, "x7_bg_female_pink_right_7.xml")
  22. if os.path.exists(x7_bg_female_pink_7_xml):
  23. os.remove(x7_bg_female_pink_7_xml)
  24. if os.path.exists(x7_bg_female_pink_right_7):
  25. os.remove(x7_bg_female_pink_right_7)
  26. s1 = os.path.join(workspace_sdk_dir, 'x7sy/ForRes/values/public.xml')
  27. handle_public_xml(s1)
  28. return
  29. def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  30. logging.info('game_pack_info: ' + str(channel_sdk_info))
  31. replace_init_code(extract_dir)
  32. del_drawable(extract_dir)
  33. if channel_sdk_info['id'] == "323":
  34. change_smali(extract_dir)
  35. s1 = os.path.join(extract_dir, "smali", "androidx")
  36. s3 = create_mutil_smali(extract_dir)
  37. s3 = os.path.join(s3, "androidx")
  38. if not os.path.exists(s3):
  39. distutils.dir_util.mkpath(s3)
  40. move_package_one(s1, s3)
  41. s1 = os.path.join(extract_dir, "smali_classes5", "androidx")
  42. s3 = create_mutil_smali(extract_dir)
  43. s3 = os.path.join(s3, "androidx")
  44. list_package = ["fragment", "lifecycle", "media", "recyclerview", "slidingpanelayout", " transition",
  45. " versionedparcelable", "viewpager2",
  46. "legacy", " loader", "multidex", "savedstate", " swiperefreshlayout", "vectordrawable",
  47. "viewpager"]
  48. if not os.path.exists(s3):
  49. distutils.dir_util.mkpath(s3)
  50. move_package(s1, s3, list_package)
  51. if channel_sdk_info['SDKName'] == 'x7sy':
  52. s1 = os.path.join(extract_dir, "smali_classes2", "com")
  53. smali_classes2_dir = create_mutil_smali(extract_dir)
  54. s3 = os.path.join(smali_classes2_dir, "com")
  55. list_package = ["a", "adjust", "alipayx", "cloudplay", "google", "heepay", "helpshift", "iapppay", "ipaynow"]
  56. if not os.path.exists(s3):
  57. distutils.dir_util.mkpath(s3)
  58. move_package(s1, s3, list_package)
  59. s1 = os.path.join(extract_dir, "smali_classes3", "androidx")
  60. smali_classes2_dir = create_mutil_smali(extract_dir)
  61. s3 = os.path.join(smali_classes2_dir, "androidx")
  62. list_package = ["core", "d", "drawerlayout", "e", "f", "fragment", "legacy", "lifecycle", "loader", "media",
  63. "multidex"]
  64. if not os.path.exists(s3):
  65. distutils.dir_util.mkpath(s3)
  66. move_package(s1, s3, list_package)
  67. if channel_sdk_info['SDKName'] == 'ccplay':
  68. s1 = os.path.join(extract_dir, "smali_classes3", "androidx")
  69. smali_classes2_dir = create_mutil_smali(extract_dir)
  70. s3 = os.path.join(smali_classes2_dir, "androidx")
  71. list_package = ["core", "d", "drawerlayout", "e", "f", "fragment", "legacy", "lifecycle", "loader", "media",
  72. "multidex"]
  73. if not os.path.exists(s3):
  74. distutils.dir_util.mkpath(s3)
  75. move_package(s1, s3, list_package)
  76. if channel_sdk_info['SDKName'] == 'jolo':
  77. s1 = os.path.join(extract_dir, "smali_classes5", "androidx")
  78. s3 = create_mutil_smali(extract_dir)
  79. s3 = os.path.join(s3, "androidx")
  80. list_package = ["core", "d", "drawerlayout", "e", "f", "fragment", "legacy", "lifecycle", "loader"]
  81. if not os.path.exists(s3):
  82. distutils.dir_util.mkpath(s3)
  83. move_package(s1, s3, list_package)
  84. if channel_sdk_info['SDKName'] == 'sogou':
  85. rm_wechatpay(extract_dir)
  86. if channel_sdk_info['SDKName'] == 'xxzhushou':
  87. rm_alipay(extract_dir)
  88. if channel_sdk_info['id'] == "389":
  89. s1 = os.path.join(extract_dir, "smali", "com")
  90. smali_classes2_dir = create_mutil_smali(extract_dir)
  91. s3 = os.path.join(smali_classes2_dir, "com")
  92. list_package = ["smwl", "netease"]
  93. if not os.path.exists(s3):
  94. distutils.dir_util.mkpath(s3)
  95. move_package(s1, s3, list_package)
  96. return
  97. def create_mutil_smali(decompileDir):
  98. f_idx = 5
  99. while True:
  100. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  101. tmp = tmp.replace('\\', '/')
  102. tmp = re.sub('/+', '/', tmp)
  103. if os.path.exists(tmp):
  104. f_idx += 1
  105. else:
  106. smali_classes2_dir = tmp
  107. break
  108. # endwhile
  109. os.mkdir(smali_classes2_dir)
  110. return smali_classes2_dir
  111. def move_package(s1, s3, list_package):
  112. for d in list_package:
  113. src = os.path.join(s1, d)
  114. if os.path.exists(src):
  115. dst = os.path.join(s3, d)
  116. distutils.dir_util.copy_tree(src, dst)
  117. distutils.dir_util.remove_tree(src)
  118. def move_package_one(src, dst):
  119. if os.path.exists(src):
  120. distutils.dir_util.copy_tree(src, dst)
  121. distutils.dir_util.remove_tree(src)
  122. def rm_alipay(extract_dir):
  123. delet_file = os.path.join(extract_dir, 'smali/org/json')
  124. print delet_file
  125. logging.info('delet_file: ' + delet_file)
  126. shutil.rmtree(delet_file)
  127. def rm_wechatpay(extract_dir):
  128. delet = os.path.join(extract_dir, 'smali_classes2/com/ipaynow')
  129. print delet
  130. logging.info(delet)
  131. shutil.rmtree(delet)
  132. def change_smali(fooDir):
  133. for smali_flie in glob.glob(fooDir + "/" + "smali*/com/kunlun/platform/android/KunlunApplication.smali"):
  134. print smali_flie
  135. if os.path.isfile(smali_flie):
  136. with open(smali_flie, 'r+') as f:
  137. cont = f.read()
  138. change_str = 'invoke-virtual {p0}, Lcom/kf/framework/KFApplication;->onCreate()V'
  139. str = 'invoke-virtual {p0}, Landroid/app/Application;->onCreate()V'
  140. print len(change_str)
  141. print len(str)
  142. change_str2 = 'invoke-virtual {v0, p1}, Lcom/kf/framework/KFApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V'
  143. str2 = 'invoke-virtual {v0, p1}, Landroid/app/Application;->onConfigurationChanged(Landroid/content/res/Configuration;)V'
  144. cont = cont.replace(change_str, str)
  145. cont = cont.replace(change_str2, str2)
  146. with open(smali_flie, 'w') as tf:
  147. tf.write(cont)
  148. def intoFirstLine(target, repl, cont):
  149. # srcStr = "onBackPressed"
  150. # regexp = '.method (?:public|protected) +?'+target+"\([^.]+?\.locals +[0-9]{1,2}"
  151. regexp = '.method (?:public|protected) +?' + target + "\([^.]+?\.locals +([0-9]{1,2})"
  152. # print(regexp)
  153. reObj = re.compile(regexp)
  154. matchO = reObj.search(cont)
  155. if matchO:
  156. # print(matchO.group(0))
  157. methodHead = matchO.group(0)
  158. if matchO.group(1) == "0":
  159. methodHead = re.sub("locals +0", "locals 1", methodHead)
  160. repl = methodHead + r'\n\n ' + repl + r'\n\n'
  161. cont = reObj.sub(repl, cont)
  162. else:
  163. # todo call .super method
  164. cont += r'.method protected ' + target + r'()V\n\n .locals 0\n\n ' + repl + r'\n\n .end method\n'
  165. return cont
  166. def fixLifecycleMethod(preDir):
  167. for tfile in glob.glob(preDir + "/" + "smali*/com/supercell/titan/kunlun/GameAppKunlun.smali"):
  168. if os.path.isfile(tfile):
  169. with open(tfile, 'r+') as f:
  170. cont = f.read()
  171. changes = {
  172. "onPause": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onPause()V",
  173. "onResume": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onResume()V",
  174. "onDestroy": "invoke-static {p0}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->destroy(Landroid/content/Context;)V",
  175. "onStop": "invoke-static {}, Lcom/qihoo/gamecenter/sdk/matrix/Matrix;->onStop()V"
  176. }
  177. for k, v in changes.iteritems():
  178. cont = intoFirstLine(k, v, cont)
  179. f.seek(0)
  180. f.write(cont)
  181. f.close()
  182. def handle_public_xml(public_xml_path):
  183. if not os.path.exists(public_xml_path):
  184. logging.info('public_xml is null: ' + public_xml_path)
  185. return
  186. new_lines = []
  187. with open(public_xml_path, 'r+') as f:
  188. for line in f.readlines():
  189. # l = line.strip()
  190. if line.find("x7_bg_female_pink_7") > -1:
  191. continue
  192. if line.find("x7_icon_female_pink") > -1:
  193. continue
  194. if line.find("x7_bg_female_pink_right_7") > -1:
  195. continue
  196. new_lines.append(line)
  197. f.seek(0)
  198. f.truncate()
  199. f.writelines(new_lines)
  200. return
  201. def replace_init_code(decompileDir):
  202. smali_files = glob.glob(os.path.join(decompileDir, 'smali*/com/qihoo/gamecenter/sdk/matrix/Matrix.smali'))
  203. if len(smali_files) == 1:
  204. game_application_file = smali_files[0]
  205. print game_application_file
  206. if os.path.isfile(game_application_file):
  207. with open(game_application_file, "r+") as f:
  208. file_str = f.read()
  209. print(file_str)
  210. change_str = "0x82c"
  211. file_str = file_str.replace(change_str, "0x82b")
  212. with open(game_application_file, "w+") as f:
  213. f.write(file_str)
  214. def del_drawable(decompileDir):
  215. qihoo = os.path.join(decompileDir, 'res/drawable-anydpi-v26')
  216. if os.path.exists(qihoo):
  217. shutil.rmtree(qihoo)
  218. if __name__ == '__main__':
  219. replace_init_code("D:\\work\\x7sy")
  220. del_drawable("D:\\work\\xmyhs")
  221. del_drawable("D:\\work\\jl")
  222. del_drawable("D:\\work\\qql")
  223. del_drawable("D:\\work\\x7sy")