script.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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'] == "309":
  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. sdk_id = channel_sdk_info['id']
  19. if sdk_id == "512" or sdk_id=="589" or sdk_id=="597" or sdk_id=="309":
  20. modify_manifest(extract_dir)
  21. game_mode_config = os.path.join(extract_dir, "res/xml-v22/game_mode_config.xml")
  22. os.remove(game_mode_config)
  23. return
  24. def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  25. s1 = os.path.join(extract_dir, "smali_classes4", "com/netease")
  26. smali_classes2_dir = create_mutil_smali(extract_dir)
  27. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  28. list_package = ["androidcrashhandler", "download", "godlikeshare", "pharos", "unisdk", "autotext", "environment", "onmyoji", "tryautotext2", "xyqa"]
  29. if not os.path.exists(s3):
  30. distutils.dir_util.mkpath(s3)
  31. move_package(s1, s3, list_package)
  32. smali_classes2_dir = create_mutil_smali(extract_dir)
  33. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  34. list_package = ["mpay", "neox", "ngpush", "ntunisdk"]
  35. if not os.path.exists(s3):
  36. distutils.dir_util.mkpath(s3)
  37. move_package(s1, s3, list_package)
  38. if channel_sdk_info['id'] == "309":
  39. s1 = os.path.join(extract_dir, "smali_classes5", "com/netease")
  40. smali_classes2_dir = create_mutil_smali(extract_dir)
  41. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  42. list_package = ["neox", "ngpush", "onmyoji"]
  43. if not os.path.exists(s3):
  44. distutils.dir_util.mkpath(s3)
  45. move_package(s1, s3, list_package)
  46. s1 = os.path.join(extract_dir, "smali_classes5", "com/netease")
  47. smali_classes2_dir = create_mutil_smali(extract_dir)
  48. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  49. list_package = ["ntunisdk", "onmyoji", "pharos"]
  50. if not os.path.exists(s3):
  51. distutils.dir_util.mkpath(s3)
  52. move_package(s1, s3, list_package)
  53. s1 = os.path.join(extract_dir, "smali_classes5", "com/netease")
  54. smali_classes2_dir = create_mutil_smali(extract_dir)
  55. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  56. list_package = ["androidcrashhandler", "autotext", "download", "environment", "godlikeshare", "htprotect"]
  57. if not os.path.exists(s3):
  58. distutils.dir_util.mkpath(s3)
  59. move_package(s1, s3, list_package)
  60. s1 = os.path.join(extract_dir, "smali_classes5", "com/netease")
  61. smali_classes2_dir = create_mutil_smali(extract_dir)
  62. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  63. list_package = ["a","amap","autonavi","bun","duoku","github","googlecode","huawei"," mcs","mp4parser","alipayx","asus","baidu","coremedia","example","google","heytap","JavaWebsocket","meizu"]
  64. if not os.path.exists(s3):
  65. distutils.dir_util.mkpath(s3)
  66. move_package(s1, s3, list_package)
  67. s1 = os.path.join(extract_dir, "smali_classes5", "com")
  68. smali_classes2_dir = create_mutil_smali(extract_dir)
  69. s3 = os.path.join(smali_classes2_dir, "com")
  70. list_package = ["a","amap","autonavi","bun","duoku","github","googlecode","huawei"," mcs","mp4parser","alipayx","asus","baidu","coremedia","example","google","heytap","JavaWebsocket","meizu"]
  71. if not os.path.exists(s3):
  72. distutils.dir_util.mkpath(s3)
  73. move_package(s1, s3, list_package)
  74. s1 = os.path.join(extract_dir, "smali_classes5", "com")
  75. smali_classes2_dir = create_mutil_smali(extract_dir)
  76. s3 = os.path.join(smali_classes2_dir, "com")
  77. list_package = ["netease","noah","random","samsung","sina","tax","tencent","utx","vivo","xiaomi","zui"]
  78. if not os.path.exists(s3):
  79. distutils.dir_util.mkpath(s3)
  80. move_package(s1, s3, list_package)
  81. s1 = os.path.join(extract_dir, "smali_classes6", "com/netease")
  82. smali_classes2_dir = create_mutil_smali(extract_dir)
  83. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  84. list_package = ["ntunisdk"]
  85. if not os.path.exists(s3):
  86. distutils.dir_util.mkpath(s3)
  87. move_package(s1, s3, list_package)
  88. s1 = os.path.join(extract_dir, "smali_classes6", "com/netease")
  89. smali_classes2_dir = create_mutil_smali(extract_dir)
  90. s3 = os.path.join(smali_classes2_dir, "com", "netease")
  91. list_package = ["onmyoji","unisdk"]
  92. if not os.path.exists(s3):
  93. distutils.dir_util.mkpath(s3)
  94. move_package(s1, s3, list_package)
  95. def move_package(s1, s3, list_package):
  96. for d in list_package:
  97. src = os.path.join(s1, d)
  98. if os.path.exists(src):
  99. dst = os.path.join(s3, d)
  100. distutils.dir_util.copy_tree(src, dst)
  101. distutils.dir_util.remove_tree(src)
  102. def create_mutil_smali(decompileDir):
  103. f_idx = 5
  104. while True:
  105. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  106. tmp = tmp.replace('\\', '/')
  107. tmp = re.sub('/+', '/', tmp)
  108. if os.path.exists(tmp):
  109. f_idx += 1
  110. else:
  111. smali_classes2_dir = tmp
  112. break
  113. # endwhile
  114. os.mkdir(smali_classes2_dir)
  115. return smali_classes2_dir
  116. def intoFirstLine(target, repl, cont):
  117. # srcStr = "onBackPressed"
  118. # regexp = '.method (?:public|protected) +?'+target+"\([^.]+?\.locals +[0-9]{1,2}"
  119. regexp = '.method (?:public|protected) +?' + target + "\([^.]+?\.locals +([0-9]{1,2})"
  120. # print(regexp)
  121. reObj = re.compile(regexp)
  122. matchO = reObj.search(cont)
  123. if matchO:
  124. # print(matchO.group(0))
  125. methodHead = matchO.group(0)
  126. if matchO.group(1) == "0":
  127. methodHead = re.sub("locals +0", "locals 1", methodHead)
  128. repl = methodHead + r'\n\n ' + repl + r'\n\n'
  129. cont = reObj.sub(repl, cont)
  130. else:
  131. # todo call .super method
  132. cont += r'.method protected ' + target + r'()V\n\n .locals 0\n\n ' + repl + r'\n\n .end method\n'
  133. return cont
  134. def fixLifecycleMethod(preDir):
  135. for tfile in glob.glob(preDir + "/" + "smali*/com/netease/onmyoji/Client.smali"):
  136. if os.path.isfile(tfile):
  137. with open(tfile, 'r+') as f:
  138. cont = f.read()
  139. changes = {
  140. "onCreate": "invoke-static {p0}, Lcom/baidu/gamesdk/BDGameSDK;->setContext(Landroid/app/Activity;)V",
  141. }
  142. for k, v in changes.iteritems():
  143. cont = intoFirstLine(k, v, cont)
  144. f.seek(0)
  145. f.write(cont)
  146. f.close()
  147. def modify_manifest(decompileDir):
  148. ET.register_namespace('android', ANDROID_NS)
  149. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  150. root_node = ET.parse(xmlparse)
  151. root = root_node.getroot()
  152. name = '{' + ANDROID_NS + '}name'
  153. authorities = '{' + ANDROID_NS + '}foregroundServiceType'
  154. package_name = root.attrib.get('package')
  155. if package_name == None:
  156. return
  157. providers = root.findall('./application/service')
  158. if providers != None:
  159. for provider in providers:
  160. providerName = provider.attrib.get(name)
  161. if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  162. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  163. try:
  164. del provider.attrib[authorities]
  165. except:
  166. ""
  167. root_node.write(xmlparse, 'utf-8')
  168. if __name__ == '__main__':
  169. script("/tmp/ss", "/mnt/share/yanghuang/python_test/any_dir", {"id": "309"}, "", "")
  170. # hideSplash("/mnt/share/yanghuang/python_test/cr")