frida_demo.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2022/9/30 15:36
  3. # @Author : Clown
  4. # @File : frida_demo.py
  5. # @Software : PyCharm
  6. import frida
  7. import pprint
  8. import frida_dexdump
  9. import sys
  10. if 1==0:
  11. def on_message(message, data):
  12. print ("[on_message] message:", message, "data:", data)
  13. rdev = frida.get_usb_device ()
  14. process = rdev.enumerate_processes () # 获取手机所有进程
  15. pprint.pprint (process)
  16. session = rdev.attach ("美团") # frida-ps -U 查看到的app名字
  17. print (session)
  18. script = session.create_script ("""
  19. rpc.exports.enumerateModules=function(){
  20. return Process.enumerateModules();
  21. };
  22. """)
  23. script.on ("message", on_message)
  24. script.load ()
  25. # 获取进程使用的所有模块
  26. pprint.pprint ([m["name"] for m in script.exports.enumerate_modules ()])
  27. if 1==1:
  28. jscode = """
  29. Java.perform(function () {
  30. var SwitchConfig = Java.use('mtopsdk.mtop.global.SwitchConfig');
  31. SwitchConfig.isGlobalSpdySwitchOpen.overload().implementation = function(){
  32. var ret = this.isGlobalSpdySwitchOpen.apply(this, arguments);
  33. console.log("isGlobalSpdySwitchOpenl "+ret)
  34. return false
  35. }
  36. })
  37. """
  38. def on_message(message, data):
  39. if message['type'] == 'send':
  40. print ("[*] {0}".format (message['payload']))
  41. else:
  42. print (message)
  43. process = frida.get_usb_device ().attach ('美团')
  44. script = process.create_script (jscode)
  45. script.on ('message', on_message)
  46. script.load ()
  47. sys.stdin.read ()
  48. if 1 == 0:
  49. import os
  50. import zipfile
  51. import argparse
  52. def rename_class(path):
  53. files = os.listdir (path)
  54. dex_index = 0
  55. if path.endswith ('/'):
  56. path = path[:-1]
  57. print (path)
  58. for i in range (len (files)):
  59. if files[i].endswith ('.dex'):
  60. old_name = path + '/' + files[i]
  61. if dex_index == 0:
  62. new_name = path + '/' + 'classes.dex'
  63. else:
  64. new_name = path + '/' + 'classes%d.dex' % dex_index
  65. dex_index += 1
  66. if os.path.exists (new_name):
  67. continue
  68. os.rename (old_name, new_name)
  69. print ('[*] 重命名完毕')
  70. def extract_META_INF_from_apk(apk_path, target_path):
  71. r = zipfile.is_zipfile (apk_path)
  72. if r:
  73. fz = zipfile.ZipFile (apk_path, 'r')
  74. for file in fz.namelist ():
  75. if file.startswith ('META-INF'):
  76. fz.extract (file, target_path)
  77. else:
  78. print ('[-] %s 不是一个APK文件' % apk_path)
  79. def zip_dir(dirname, zipfilename):
  80. filelist = []
  81. if os.path.isfile (dirname):
  82. if dirname.endswith ('.dex'):
  83. filelist.append (dirname)
  84. else:
  85. for root, dirs, files in os.walk (dirname):
  86. for dir in dirs:
  87. # if dir == 'META-INF':
  88. # print('dir:', os.path.join(root, dir))
  89. filelist.append (os.path.join (root, dir))
  90. for name in files:
  91. # print('file:', os.path.join(root, name))
  92. filelist.append (os.path.join (root, name))
  93. z = zipfile.ZipFile (zipfilename, 'w', zipfile.ZIP_DEFLATED)
  94. for tar in filelist:
  95. arcname = tar[len (dirname):]
  96. if ('META-INF' in arcname or arcname.endswith ('.dex')) and '.DS_Store' not in arcname:
  97. # print(tar + " -->rar: " + arcname)
  98. z.write (tar, arcname)
  99. print ('[*] APK打包成功,你可以拖入APK进行分析啦!')
  100. z.close ()
  101. if __name__ == '__main__':
  102. args = {
  103. 'dex_path': '脱壳后dex路径',
  104. 'apk_path': '原始带壳apk路径',
  105. 'output': '脱壳后apk路径'
  106. }
  107. rename_class (args['dex_path'])
  108. extract_META_INF_from_apk (args['apk_path'], args['dex_path'])
  109. zip_dir (args['dex_path'], args['output'])