__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, print_function
  3. import threading
  4. try:
  5. import _frida
  6. except Exception as ex:
  7. import colorama
  8. from colorama import Back, Fore, Style
  9. import sys
  10. colorama.init(autoreset=True)
  11. print("")
  12. print("***")
  13. if str(ex).startswith("No module named "):
  14. print(Back.RED + Fore.WHITE + Style.BRIGHT + "Frida native extension not found" + Style.RESET_ALL)
  15. print(Fore.WHITE + Style.BRIGHT + "Please check your PYTHONPATH." + Style.RESET_ALL)
  16. else:
  17. print(Back.RED + Fore.WHITE + Style.BRIGHT + "Failed to load the Frida native extension: %s" % ex + Style.RESET_ALL)
  18. if sys.version_info[0] == 2:
  19. current_python_version = "%d.%d" % sys.version_info[:2]
  20. else:
  21. current_python_version = "%d.x" % sys.version_info[0]
  22. print(Fore.WHITE + Style.BRIGHT + "Please ensure that the extension was compiled for Python " + current_python_version + "." + Style.RESET_ALL)
  23. print("***")
  24. print("")
  25. raise ex
  26. __version__ = _frida.__version__
  27. FileMonitor = _frida.FileMonitor
  28. ServerNotRunningError = _frida.ServerNotRunningError
  29. ExecutableNotFoundError = _frida.ExecutableNotFoundError
  30. ExecutableNotSupportedError = _frida.ExecutableNotSupportedError
  31. ProcessNotFoundError = _frida.ProcessNotFoundError
  32. ProcessNotRespondingError = _frida.ProcessNotRespondingError
  33. InvalidArgumentError = _frida.InvalidArgumentError
  34. InvalidOperationError = _frida.InvalidOperationError
  35. PermissionDeniedError = _frida.PermissionDeniedError
  36. AddressInUseError = _frida.AddressInUseError
  37. TimedOutError = _frida.TimedOutError
  38. NotSupportedError = _frida.NotSupportedError
  39. ProtocolError = _frida.ProtocolError
  40. TransportError = _frida.TransportError
  41. def spawn(argv):
  42. return get_local_device().spawn(argv)
  43. def resume(target):
  44. get_local_device().resume(target)
  45. def kill(target):
  46. get_local_device().kill(target)
  47. def attach(target):
  48. return get_local_device().attach(target)
  49. def enumerate_devices():
  50. return get_device_manager().enumerate_devices()
  51. def get_local_device():
  52. return _get_device(lambda device: device.type == 'local', timeout=0)
  53. def get_usb_device(timeout = 0):
  54. return _get_device(lambda device: device.type == 'tether', timeout)
  55. def get_remote_device():
  56. return _get_device(lambda device: device.type == 'remote', timeout=0)
  57. def get_device(id, timeout = 0):
  58. return _get_device(lambda device: device.id == id, timeout)
  59. def _get_device(predicate, timeout):
  60. mgr = get_device_manager()
  61. def find_matching_device():
  62. usb_devices = [device for device in mgr.enumerate_devices() if predicate(device)]
  63. if len(usb_devices) > 0:
  64. return usb_devices[0]
  65. else:
  66. return None
  67. device = find_matching_device()
  68. if device is None:
  69. result = [None]
  70. event = threading.Event()
  71. def on_devices_changed():
  72. result[0] = find_matching_device()
  73. if result[0] is not None:
  74. event.set()
  75. mgr.on('changed', on_devices_changed)
  76. device = find_matching_device()
  77. if device is None:
  78. event.wait(timeout)
  79. device = result[0]
  80. mgr.off('changed', on_devices_changed)
  81. if device is None:
  82. raise TimedOutError("timed out while waiting for device to appear")
  83. return device
  84. def shutdown():
  85. get_device_manager()._impl.close()
  86. global _device_manager
  87. _device_manager = None
  88. def get_device_manager():
  89. global _device_manager
  90. if _device_manager is None:
  91. from . import core
  92. _device_manager = core.DeviceManager(_frida.DeviceManager())
  93. return _device_manager