script.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # coding:utf8
  2. __author__ = 'dong'
  3. import os
  4. import logging
  5. import xml.dom.minidom
  6. import re
  7. import shutil
  8. import glob
  9. from xml.etree.ElementTree import ElementTree, Element
  10. import distutils.dir_util
  11. from xml.etree import ElementTree as ET
  12. import gw_file_system
  13. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  14. def script_second(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  15. logging.info('script_second: ' + str(channel_sdk_info))
  16. # 逆向渠道id
  17. public_xml = os.path.join(extract_dir, "res/values/public.xml")
  18. handle_public_xml(public_xml, "sobot_fileEndingAudio")
  19. handle_public_xml(public_xml, "sobot_fileEndingExcel")
  20. handle_public_xml(public_xml, "sobot_fileEndingImage")
  21. handle_public_xml(public_xml, "sobot_fileEndingPPT")
  22. handle_public_xml(public_xml, "sobot_fileEndingPackage")
  23. handle_public_xml(public_xml, "sobot_fileEndingPdf")
  24. handle_public_xml(public_xml, "sobot_fileEndingText")
  25. handle_public_xml(public_xml, "sobot_fileEndingVideo")
  26. handle_public_xml(public_xml, "sobot_fileEndingWord")
  27. return
  28. def handle_public_xml(public_xml_path, remove_node):
  29. if not os.path.exists(public_xml_path):
  30. logging.info('public_xml is null: ' + public_xml_path)
  31. return
  32. new_lines = []
  33. with open(public_xml_path, 'r+') as f:
  34. for line in f.readlines():
  35. # l = line.strip()
  36. if line.find(remove_node) > -1:
  37. continue
  38. new_lines.append(line)
  39. f.seek(0)
  40. f.truncate()
  41. f.writelines(new_lines)
  42. return
  43. def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  44. # s3 = decompileDir + "/smali_classes3"
  45. s1 = os.path.join(decompileDir, "smali", "com")
  46. smali_classes2_dir = create_mutil_smali(decompileDir)
  47. s3 = os.path.join(smali_classes2_dir, "com")
  48. list_package = ["yeepay", "utx", "tencent", "lilith", "lilithgame", "activeandroid", "qihoo", "android"]
  49. list_package.extend(["radical", "nostra13", "qiyukf", "dckj", "dc", "ansca"])
  50. if not os.path.exists(s3):
  51. distutils.dir_util.mkpath(s3)
  52. move_package(s1, s3, list_package)
  53. sdk_id = channelSdkInfo['id']
  54. if sdk_id == "323":
  55. modify_manifest(decompileDir, "requestLegacyExternalStorage")
  56. s1 = os.path.join(decompileDir, "smali", "androidx")
  57. if os.path.exists(s1):
  58. smali_classes2_dir = create_mutil_smali(decompileDir)
  59. s3 = os.path.join(smali_classes2_dir, "androidx")
  60. if not os.path.exists(s3):
  61. distutils.dir_util.mkpath(s3)
  62. move_package_one(s1, s3)
  63. logging.info("-----debug-----")
  64. def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  65. logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
  66. if channelSdkInfo['SDKName'] == 'reversefor360':
  67. replace_super_application(decompileDir)
  68. sdk_id = channelSdkInfo['id']
  69. if sdk_id == "234":
  70. s1 = os.path.join(decompileDir, "smali_classes4", "androidx")
  71. if os.path.exists(s1):
  72. smali_classes2_dir = create_mutil_smali(decompileDir)
  73. s3 = os.path.join(smali_classes2_dir, "androidx")
  74. list_package = ["fragment","interpolator","legacy","lifecycle","loader","localbroadcastmanager"]
  75. list_package.extend(["multidex","print","recyclerview","slidingpanelayout","swiperefreshlayout","vectordrawable","versionedparcelable","viewpager"])
  76. if not os.path.exists(s3):
  77. distutils.dir_util.mkpath(s3)
  78. move_package(s1, s3, list_package)
  79. def create_mutil_smali(decompileDir):
  80. f_idx = 2
  81. while True:
  82. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  83. tmp = tmp.replace('\\', '/')
  84. tmp = re.sub('/+', '/', tmp)
  85. if os.path.exists(tmp):
  86. f_idx += 1
  87. else:
  88. smali_classes2_dir = tmp
  89. break
  90. # endwhile
  91. os.mkdir(smali_classes2_dir)
  92. return smali_classes2_dir
  93. def move_package(s1, s3, list_package):
  94. for d in list_package:
  95. src = os.path.join(s1, d)
  96. if os.path.exists(src):
  97. dst = os.path.join(s3, d)
  98. distutils.dir_util.copy_tree(src, dst)
  99. distutils.dir_util.remove_tree(src)
  100. def move_package_one(src, dst):
  101. if os.path.exists(src):
  102. distutils.dir_util.copy_tree(src, dst)
  103. distutils.dir_util.remove_tree(src)
  104. change_map = {".super Landroid/app/Application;": ".super Lcom/kf/framework/KFApplication;",
  105. "invoke-direct {p0}, Landroid/app/Application;-><init>()V": "invoke-direct {p0}, Lcom/kf/framework/KFApplication;-><init>()V",
  106. "invoke-super {p0, p1}, Landroid/app/Application;->attachBaseContext(Landroid/content/Context;)V": "invoke-super {p0, p1}, Lcom/kf/framework/KFApplication;->attachBaseContext(Landroid/content/Context;)V",
  107. "invoke-super {p0}, Landroid/app/Application;->onCreate()V": "invoke-super {p0}, Lcom/kf/framework/KFApplication;->onCreate()V", }
  108. def replace_super_application(decompileDir):
  109. smali_files = glob.glob(
  110. os.path.join(decompileDir, 'smali*/com/radical/huangshangjixiang/qh360/CoronaApplication.smali'))
  111. if len(smali_files) == 1:
  112. game_application_file = smali_files[0];
  113. print game_application_file
  114. if os.path.isfile(game_application_file):
  115. with open(game_application_file, "r+") as f:
  116. file_str = f.read()
  117. for k, v in change_map.items():
  118. file_str = file_str.replace(k, v)
  119. with open(game_application_file, "w+") as f:
  120. f.write(file_str)
  121. def modify_manifest(decompileDir, removeKey):
  122. ET.register_namespace('android', ANDROID_NS)
  123. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  124. root_node = ET.parse(xmlparse)
  125. root = root_node.getroot()
  126. name = '{' + ANDROID_NS + '}name'
  127. authorities = '{' + ANDROID_NS + '}' + removeKey
  128. package_name = root.attrib.get('package')
  129. if package_name == None:
  130. return
  131. providers = root.findall('./application')
  132. if providers != None:
  133. for provider in providers:
  134. # providerName = provider.attrib.get(name)
  135. # if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  136. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  137. try:
  138. del provider.attrib[authorities]
  139. except:
  140. ""
  141. root_node.write(xmlparse, 'utf-8')
  142. if __name__ == '__main__':
  143. # replace_super_application("D:\work\wzdq")
  144. modify_manifest("E:\\apk\\youhua\\youhua.zip.out", "requestLegacyExternalStorage")