script.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. import gw_file_system
  12. import gw_data_center
  13. import gw_apk_tool
  14. def script_init(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  15. return
  16. def script_first(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  17. return
  18. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  19. sdk_id = channel_sdk_info['id']
  20. if sdk_id == "597":
  21. game_mode_config = os.path.join(extract_dir, "res/xml-v22/game_mode_config.xml")
  22. os.remove(game_mode_config)
  23. def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  24. return
  25. def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  26. sdk_id = channelSdkInfo['id']
  27. if sdk_id == "597" or sdk_id == "591" :
  28. s1 = os.path.join(decompileDir, "smali_classes4")
  29. if os.path.exists(s1):
  30. smali_classes2_dir = create_mutil_smali(decompileDir)
  31. s3 = smali_classes2_dir
  32. if not os.path.exists(s3):
  33. distutils.dir_util.mkpath(s3)
  34. move_package(s1, s3, ["androidx", "android"])
  35. s1 = os.path.join(decompileDir, "smali_classes4", "com")
  36. if os.path.exists(s1):
  37. smali_classes2_dir = create_mutil_smali(decompileDir)
  38. s3 = os.path.join(smali_classes2_dir, "com")
  39. if not os.path.exists(s3):
  40. distutils.dir_util.mkpath(s3)
  41. move_package_one(s1, s3)
  42. s1 = os.path.join(decompileDir, "smali_classes5","com","netease")
  43. if os.path.exists(s1):
  44. smali_classes2_dir = create_mutil_smali(decompileDir)
  45. s3 = os.path.join(smali_classes2_dir,"com","netease")
  46. if not os.path.exists(s3):
  47. distutils.dir_util.mkpath(s3)
  48. move_package(s1, s3, ["mpay", "ngpush", "ntunisdk"])
  49. s1 = os.path.join(decompileDir, "smali_classes8","com","netease")
  50. if os.path.exists(s1):
  51. smali_classes2_dir = create_mutil_smali(decompileDir)
  52. s3 = os.path.join(smali_classes2_dir,"com","netease")
  53. if not os.path.exists(s3):
  54. distutils.dir_util.mkpath(s3)
  55. move_package(s1, s3, ["party", "unisdk"])
  56. s1 = os.path.join(decompileDir, "smali_classes8","com","netease")
  57. if os.path.exists(s1):
  58. smali_classes2_dir = create_mutil_smali(decompileDir)
  59. s3 = os.path.join(smali_classes2_dir,"com","netease")
  60. if not os.path.exists(s3):
  61. distutils.dir_util.mkpath(s3)
  62. move_package(s1, s3, ["party", "unisdk"])
  63. s1 = os.path.join(decompileDir, "smali_classes5","com")
  64. if os.path.exists(s1):
  65. smali_classes2_dir = create_mutil_smali(decompileDir)
  66. s3 = os.path.join(smali_classes2_dir,"com")
  67. if not os.path.exists(s3):
  68. distutils.dir_util.mkpath(s3)
  69. move_package(s1, s3, ["netease"])
  70. s1 = os.path.join(decompileDir, "smali_classes6/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 = ["documentfile","drawerlayout","fragment","interpolator","legacy","lifecycle","loader","localbroadcastmanager","multidex"]
  75. if not os.path.exists(s3):
  76. distutils.dir_util.mkpath(s3)
  77. move_package(s1, s3, list_package)
  78. s1 = os.path.join(decompileDir, "smali_classes7/com")
  79. if os.path.exists(s1):
  80. smali_classes2_dir = create_mutil_smali(decompileDir)
  81. s3 = os.path.join(smali_classes2_dir, "com")
  82. list_package = ["lzy","meizu","mp4parser","netease","tencent","xiaomi","yulong"]
  83. if not os.path.exists(s3):
  84. distutils.dir_util.mkpath(s3)
  85. move_package(s1, s3, list_package)
  86. s1 = os.path.join(decompileDir, "smali_classes8/com/netease/ntunisdk")
  87. if os.path.exists(s1):
  88. smali_classes2_dir = create_mutil_smali(decompileDir)
  89. s3 = os.path.join(smali_classes2_dir, "com/netease/ntunisdk")
  90. list_package = ["modules","ngpush","okhttp","permissionkit","piclib","protocollib","receiver","ui"," zxing","ngdetect","ngshare","okio"," picedit"," protocolapp","qrcode","shortcuts","util"]
  91. if not os.path.exists(s3):
  92. distutils.dir_util.mkpath(s3)
  93. move_package(s1, s3, list_package)
  94. s1 = os.path.join(decompileDir, "smali_classes6")
  95. if os.path.exists(s1):
  96. smali_classes2_dir = create_mutil_smali(decompileDir)
  97. s3 = os.path.join(smali_classes2_dir)
  98. list_package = ["androidx"]
  99. if not os.path.exists(s3):
  100. distutils.dir_util.mkpath(s3)
  101. move_package(s1, s3, list_package)
  102. s1 = os.path.join(decompileDir, "smali_classes8")
  103. if os.path.exists(s1):
  104. smali_classes2_dir = create_mutil_smali(decompileDir)
  105. s3 = os.path.join(smali_classes2_dir)
  106. list_package = ["androidx"]
  107. if not os.path.exists(s3):
  108. distutils.dir_util.mkpath(s3)
  109. move_package(s1, s3, list_package)
  110. s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
  111. if os.path.exists(s1):
  112. smali_classes2_dir = create_mutil_smali(decompileDir)
  113. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  114. list_package = ["ui","unifix","util","zxing"]
  115. if not os.path.exists(s3):
  116. distutils.dir_util.mkpath(s3)
  117. move_package(s1, s3, list_package)
  118. s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
  119. if os.path.exists(s1):
  120. smali_classes2_dir = create_mutil_smali(decompileDir)
  121. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  122. list_package = ["application","ccmoment","clipboard","cpubound","download","external","httpdns","langutil","lbscommon","base","child","coolpad_sdk","cutout","extend","gamemode","imageutil","lbs"]
  123. if not os.path.exists(s3):
  124. distutils.dir_util.mkpath(s3)
  125. move_package(s1, s3, list_package)
  126. s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
  127. if os.path.exists(s1):
  128. smali_classes2_dir = create_mutil_smali(decompileDir)
  129. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  130. list_package = ["modules","ngdetect","ngpush","ngshare","okio"]
  131. if not os.path.exists(s3):
  132. distutils.dir_util.mkpath(s3)
  133. move_package(s1, s3, list_package)
  134. s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
  135. if os.path.exists(s1):
  136. smali_classes2_dir = create_mutil_smali(decompileDir)
  137. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  138. list_package = ["permissionkit","piclib","protocolapp","qrcode","sdkpersonalinfolist","picedit","placeholdertest","protocollib","receiver","shortcuts"]
  139. if not os.path.exists(s3):
  140. distutils.dir_util.mkpath(s3)
  141. move_package(s1, s3, list_package)
  142. s1 = os.path.join(decompileDir, "smali_classes9","com/netease")
  143. if os.path.exists(s1):
  144. smali_classes2_dir = create_mutil_smali(decompileDir)
  145. s3 = os.path.join(smali_classes2_dir,"com/netease")
  146. list_package = ["ntunisdk"]
  147. if not os.path.exists(s3):
  148. distutils.dir_util.mkpath(s3)
  149. move_package(s1, s3, list_package)
  150. s1 = os.path.join(decompileDir, "smali_classes9","com")
  151. if os.path.exists(s1):
  152. smali_classes2_dir = create_mutil_smali(decompileDir)
  153. s3 = os.path.join(smali_classes2_dir,"com")
  154. list_package = ["bytedance"]
  155. if not os.path.exists(s3):
  156. distutils.dir_util.mkpath(s3)
  157. move_package(s1, s3, list_package)
  158. s1 = os.path.join(decompileDir, "smali_classes3")
  159. if os.path.exists(s1):
  160. smali_classes2_dir = create_mutil_smali(decompileDir)
  161. s3 = os.path.join(smali_classes2_dir)
  162. list_package = ["androidx"]
  163. if not os.path.exists(s3):
  164. distutils.dir_util.mkpath(s3)
  165. move_package(s1, s3, list_package)
  166. s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
  167. if os.path.exists(s1):
  168. smali_classes2_dir = create_mutil_smali(decompileDir)
  169. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  170. list_package = ["application","ccmoment","clipboard","cpubound","download","external","httpdns","langutil","lbscommon","base","child","coolpad_sdk","cutout","extend","gamemode","imageutil","lbs"]
  171. if not os.path.exists(s3):
  172. distutils.dir_util.mkpath(s3)
  173. move_package(s1, s3, list_package)
  174. s1 = os.path.join(decompileDir, "smali_classes6","com/netease/ntunisdk")
  175. if os.path.exists(s1):
  176. smali_classes2_dir = create_mutil_smali(decompileDir)
  177. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  178. list_package = ["modules","ngdetect","ngpush","ngshare","okio"]
  179. if not os.path.exists(s3):
  180. distutils.dir_util.mkpath(s3)
  181. move_package(s1, s3, list_package)
  182. s1 = os.path.join(decompileDir, "smali_classes6","com/netease/ntunisdk")
  183. if os.path.exists(s1):
  184. smali_classes2_dir = create_mutil_smali(decompileDir)
  185. s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
  186. list_package = ["permissionkit","piclib","protocolapp","qrcode","sdkpersonalinfolist","picedit","placeholdertest","protocollib","receiver","shortcuts"]
  187. if not os.path.exists(s3):
  188. distutils.dir_util.mkpath(s3)
  189. move_package(s1, s3, list_package)
  190. s1 = os.path.join(decompileDir, "smali_classes6","com/netease")
  191. if os.path.exists(s1):
  192. smali_classes2_dir = create_mutil_smali(decompileDir)
  193. s3 = os.path.join(smali_classes2_dir,"com/netease")
  194. list_package = ["ntunisdk"]
  195. if not os.path.exists(s3):
  196. distutils.dir_util.mkpath(s3)
  197. move_package(s1, s3, list_package)
  198. s1 = os.path.join(decompileDir, "smali_classes7","androidx")
  199. if os.path.exists(s1):
  200. smali_classes2_dir = create_mutil_smali(decompileDir)
  201. s3 = os.path.join(smali_classes2_dir,"androidx")
  202. list_package = ["activity","appcompat","asynclayoutinflater","collection","core","customview","drawerlayout","annotation","arch","cardview","coordinatorlayout","cursoradapter","documentfile","interpolator"]
  203. if not os.path.exists(s3):
  204. distutils.dir_util.mkpath(s3)
  205. move_package(s1, s3, list_package)
  206. s1 = os.path.join(decompileDir, "smali_classes8","com")
  207. if os.path.exists(s1):
  208. smali_classes2_dir = create_mutil_smali(decompileDir)
  209. s3 = os.path.join(smali_classes2_dir,"com")
  210. list_package = ["googlecode","jakewharton","lzy","meizu","mp4parser","netease","tencent","xiaomi","yulong"]
  211. if not os.path.exists(s3):
  212. distutils.dir_util.mkpath(s3)
  213. move_package(s1, s3, list_package)
  214. if sdk_id == "549" or sdk_id=="591":
  215. s1 = os.path.join(decompileDir, "smali_classes7", "com")
  216. if os.path.exists(s1):
  217. smali_classes2_dir = create_mutil_smali(decompileDir)
  218. s3 = os.path.join(smali_classes2_dir, "com")
  219. if not os.path.exists(s3):
  220. distutils.dir_util.mkpath(s3)
  221. move_package(s1, s3, ["netease"])
  222. # s1 = os.path.join(decompileDir, "smali_classes8", "com")
  223. # if os.path.exists(s1):
  224. # smali_classes2_dir = create_mutil_smali(decompileDir)
  225. # s3 = os.path.join(smali_classes2_dir, "com")
  226. # if not os.path.exists(s3):
  227. # distutils.dir_util.mkpath(s3)
  228. # move_package(s1, s3, ["netease"])
  229. if sdk_id=="597":
  230. ""
  231. else:
  232. move_smallsheep(decompileDir)
  233. return
  234. def move_smallsheep(decompileDir):
  235. s1 = os.path.join(decompileDir, "smali_classes13/com")
  236. smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
  237. s3 = os.path.join(decompileDir, smali_classes_new, "com")
  238. list_package = ["tencent","xiaomi","yulong"]
  239. if not os.path.exists(s3):
  240. distutils.dir_util.mkpath(s3)
  241. gw_apk_tool.move_package(s1, s3, list_package)
  242. s1 = os.path.join(decompileDir, "smali_classes13/com/netease")
  243. smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
  244. s3 = os.path.join(decompileDir, smali_classes_new, "com/netease")
  245. list_package = ["androidcrashhandler","blqx","cloud","gl","inner","mpay","ngpush","ntunisdk","aoe","cc","download","godlikeshare","mc","ngdetect","ntsharesdk","pharos"]
  246. if not os.path.exists(s3):
  247. distutils.dir_util.mkpath(s3)
  248. gw_apk_tool.move_package(s1, s3, list_package)
  249. def create_mutil_smali(decompileDir):
  250. f_idx = 2
  251. while True:
  252. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  253. tmp = tmp.replace('\\', '/')
  254. tmp = re.sub('/+', '/', tmp)
  255. if os.path.exists(tmp):
  256. f_idx += 1
  257. else:
  258. smali_classes2_dir = tmp
  259. break
  260. # endwhile
  261. os.mkdir(smali_classes2_dir)
  262. return smali_classes2_dir
  263. def move_package(s1, s3, list_package):
  264. for d in list_package:
  265. src = os.path.join(s1, d)
  266. if os.path.exists(src):
  267. dst = os.path.join(s3, d)
  268. distutils.dir_util.copy_tree(src, dst)
  269. distutils.dir_util.remove_tree(src)
  270. def is_reverse_qihoo(file):
  271. with open(file, 'r+') as f:
  272. file_str = f.read()
  273. if 'KFSDK' in file_str:
  274. return True
  275. else:
  276. return False
  277. def replace_init_code(decompileDir):
  278. smali_files = glob.glob(os.path.join(decompileDir, 'smali*/com/qihoo/'))
  279. if len(smali_files) >= 1:
  280. iter_smali = iter(smali_files)
  281. while True:
  282. try:
  283. qihoo_file_path = iter_smali.next()
  284. file = os.path.join(qihoo_file_path, 'gamecenter/sdk/matrix/Matrix.smali')
  285. if os.path.exists(file):
  286. logging.info('Matrix exits in %s' % qihoo_file_path)
  287. if is_reverse_qihoo(file):
  288. with open(file, 'r+') as f:
  289. cont = f.read()
  290. change_str = '0x82c'
  291. str = '0x82b'
  292. cont = cont.replace(change_str, str)
  293. logging.info('replace_str: ' + cont)
  294. with open(file, 'w+') as tf:
  295. tf.write(cont)
  296. except StopIteration:
  297. break
  298. def replace_application(extract_dir):
  299. goal_file = os.path.join(extract_dir, 'smali_classes3/com/lilithgame/sgame/wxapi/QihooApplication.smali')
  300. print(os.path.isfile(goal_file))
  301. if os.path.isfile(goal_file):
  302. logging.info('method_proceed: ' + 'goal_file')
  303. print(goal_file)
  304. with open(goal_file, 'r+') as f:
  305. cont = f.read()
  306. change_str = 'Landroid/support/multidex/MultiDexApplication'
  307. str = 'Lcom/kf/framework/KFApplication'
  308. cont = cont.replace(change_str, str)
  309. logging.info('replace_str: ' + cont)
  310. with open(goal_file, 'w+') as tf:
  311. tf.write(cont)
  312. def move_package(s1, s3, list_package):
  313. for d in list_package:
  314. src = os.path.join(s1, d)
  315. if os.path.exists(src):
  316. dst = os.path.join(s3, d)
  317. distutils.dir_util.copy_tree(src, dst)
  318. distutils.dir_util.remove_tree(src)
  319. def move_package_one(src, dst):
  320. if os.path.exists(src):
  321. distutils.dir_util.copy_tree(src, dst)
  322. distutils.dir_util.remove_tree(src)
  323. if __name__ == '__main__':
  324. replace_init_code("D:\work\lyjymb")