Skip to content

Commit

Permalink
Drop convenience APIs and split out tools (#142)
Browse files Browse the repository at this point in the history
Most use-cases will need to use some JS API that we're not bridging, so
it's better to keep our bindings simple.

It has also become clear that our CLI tools should be split out so Frida-based
tools don't have to depend on colorama, prompt_toolkit, and pygments.
  • Loading branch information
oleavr committed Jul 10, 2018
1 parent 4fcb17c commit 72899a4
Show file tree
Hide file tree
Showing 36 changed files with 81 additions and 3,225 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
*.pyc
.idea/
/*.egg-info/
/build/
/dist/
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -7,10 +7,10 @@ Python bindings for [Frida](http://www.frida.re).
To build and test your own egg, do something along the following lines:

```
set FRIDA_VERSION=10.3.1.10.gd7c36fc # from c:\temp\frida\build\tmp-windows\frida-version.h
set FRIDA_EXTENSION=c:\temp\frida\build\frida-windows\Win32-Debug\lib\python2.7\site-packages\_frida.pyd
cd C:\Temp\frida\frida-python\src\
set FRIDA_VERSION=12.0.0.10.gd7c36fc # from C:\src\frida\build\tmp-windows\frida-version.h
set FRIDA_EXTENSION=C:\src\frida\build\frida-windows\Win32-Debug\lib\python2.7\site-packages\_frida.pyd
cd C:\src\frida\frida-python\
python setup.py bdist_egg
pip uninstall frida
easy_install dist\frida-10.3.1.10.gd7c36fc-py2.7-win32.egg
easy_install dist\frida-12.0.0.10.gd7c36fc-py2.7-win32.egg
```
2 changes: 2 additions & 0 deletions examples/bytecode.py
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import frida


system_session = frida.attach(0)
bytecode = system_session.compile_script(name="bytecode-example", source="""\
'use strict';
Expand Down
7 changes: 5 additions & 2 deletions examples/child_gating.py
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import frida
from frida.application import Reactor

import threading

import frida
from frida_tools.application import Reactor


class Application(object):
def __init__(self):
self._stop_requested = threading.Event()
Expand Down
5 changes: 4 additions & 1 deletion examples/detached.py
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import frida

import sys

import frida


def on_detached():
print("on_detached")

Expand Down
5 changes: 4 additions & 1 deletion examples/inject_library/inject_blob.py
Expand Up @@ -7,9 +7,12 @@
#

from __future__ import unicode_literals, print_function
import frida

import sys

import frida


def on_uninjected(id):
print("on_uninjected id=%u" % id)

Expand Down
5 changes: 4 additions & 1 deletion examples/inject_library/inject_file.py
Expand Up @@ -7,9 +7,12 @@
#

from __future__ import unicode_literals, print_function
import frida

import sys

import frida


def on_uninjected(id):
print("on_uninjected id=%u" % id)

Expand Down
2 changes: 2 additions & 0 deletions examples/rpc.py
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import frida


session = frida.attach("Twitter")
script = session.create_script("""\
'use strict';
Expand Down
15 changes: 4 additions & 11 deletions frida-python-common.props
Expand Up @@ -17,34 +17,27 @@ mkdir $(OutDir)frida || exit 1
copy /B /Y "$(ProjectDir)src\frida\*.py" $(OutDir)frida || exit 1
echo. &gt; $(IntDir)frida-package.stamp</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)frida-package.stamp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)src\frida\core.py;$(ProjectDir)src\frida\application.py;$(ProjectDir)src\frida\lsd.py;$(ProjectDir)src\frida\ps.py;$(ProjectDir)src\frida\kill.py;$(ProjectDir)src\frida\discoverer.py;$(ProjectDir)src\frida\tracer.py;$(ProjectDir)src\frida\repl.py</AdditionalInputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)src\frida\core.py</AdditionalInputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">rmdir /S /Q $(OutDir)frida 2&gt;NUL
mkdir $(OutDir)frida || exit 1
copy /B /Y "$(ProjectDir)src\frida\*.py" $(OutDir)frida || exit 1
echo. &gt; $(IntDir)frida-package.stamp</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)frida-package.stamp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)src\frida\core.py;$(ProjectDir)src\frida\application.py;$(ProjectDir)src\frida\lsd.py;$(ProjectDir)src\frida\ps.py;$(ProjectDir)src\frida\kill.py;$(ProjectDir)src\frida\discoverer.py;$(ProjectDir)src\frida\tracer.py;$(ProjectDir)src\frida\repl.py</AdditionalInputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)src\frida\core.py</AdditionalInputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">rmdir /S /Q $(OutDir)frida 2&gt;NUL
mkdir $(OutDir)frida || exit 1
copy /B /Y "$(ProjectDir)src\frida\*.py" $(OutDir)frida || exit 1
echo. &gt; $(IntDir)frida-package.stamp</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)frida-package.stamp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)src\frida\core.py;$(ProjectDir)src\frida\application.py;$(ProjectDir)src\frida\lsd.py;$(ProjectDir)src\frida\ps.py;$(ProjectDir)src\frida\kill.py;$(ProjectDir)src\frida\discoverer.py;$(ProjectDir)src\frida\tracer.py;$(ProjectDir)src\frida\repl.py</AdditionalInputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)src\frida\core.py</AdditionalInputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">rmdir /S /Q $(OutDir)frida 2&gt;NUL
mkdir $(OutDir)frida || exit 1
copy /B /Y "$(ProjectDir)src\frida\*.py" $(OutDir)frida || exit 1
echo. &gt; $(IntDir)frida-package.stamp</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)frida-package.stamp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)src\frida\core.py;$(ProjectDir)src\frida\application.py;$(ProjectDir)src\frida\lsd.py;$(ProjectDir)src\frida\ps.py;$(ProjectDir)src\frida\kill.py;$(ProjectDir)src\frida\discoverer.py;$(ProjectDir)src\frida\tracer.py;$(ProjectDir)src\frida\repl.py</AdditionalInputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)src\frida\core.py</AdditionalInputs>
</CustomBuild>
<None Include="src\frida\core.py" />
<None Include="src\frida\application.py" />
<None Include="src\frida\lsd.py" />
<None Include="src\frida\ps.py" />
<None Include="src\frida\kill.py" />
<None Include="src\frida\discoverer.py" />
<None Include="src\frida\tracer.py" />
<None Include="src\frida\repl.py" />
</ItemGroup>

</Project>
21 changes: 0 additions & 21 deletions frida-python27.vcxproj.filters
Expand Up @@ -26,27 +26,6 @@
<None Include="src\frida\core.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\application.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\lsd.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\ps.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\kill.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\discoverer.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\tracer.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\repl.py">
<Filter>Source Files\frida</Filter>
</None>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="src\frida\__init__.py">
Expand Down
21 changes: 0 additions & 21 deletions frida-python36.vcxproj.filters
Expand Up @@ -26,27 +26,6 @@
<None Include="src\frida\core.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\application.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\lsd.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\ps.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\kill.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\discoverer.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\tracer.py">
<Filter>Source Files\frida</Filter>
</None>
<None Include="src\frida\repl.py">
<Filter>Source Files\frida</Filter>
</None>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="src\frida\__init__.py">
Expand Down
25 changes: 18 additions & 7 deletions src/frida/__init__.py → frida/__init__.py
Expand Up @@ -2,25 +2,23 @@
from __future__ import unicode_literals, print_function

import threading

try:
import _frida
except Exception as ex:
import colorama
from colorama import Back, Fore, Style
import sys
colorama.init(autoreset=True)
print("")
print("***")
if str(ex).startswith("No module named "):
print(Back.RED + Fore.WHITE + Style.BRIGHT + "Frida native extension not found" + Style.RESET_ALL)
print(Fore.WHITE + Style.BRIGHT + "Please check your PYTHONPATH." + Style.RESET_ALL)
print("Frida native extension not found")
print("Please check your PYTHONPATH.")
else:
print(Back.RED + Fore.WHITE + Style.BRIGHT + "Failed to load the Frida native extension: %s" % ex + Style.RESET_ALL)
print("Failed to load the Frida native extension: %s" % ex)
if sys.version_info[0] == 2:
current_python_version = "%d.%d" % sys.version_info[:2]
else:
current_python_version = "%d.x" % sys.version_info[0]
print(Fore.WHITE + Style.BRIGHT + "Please ensure that the extension was compiled for Python " + current_python_version + "." + Style.RESET_ALL)
print("Please ensure that the extension was compiled for Python " + current_python_version + ".")
print("***")
print("")
raise ex
Expand All @@ -44,39 +42,51 @@
ProtocolError = _frida.ProtocolError
TransportError = _frida.TransportError


def spawn(*args, **kwargs):
return get_local_device().spawn(*args, **kwargs)


def resume(target):
get_local_device().resume(target)


def kill(target):
get_local_device().kill(target)


def attach(target):
return get_local_device().attach(target)


def inject_library_file(target, path, entrypoint, data):
return get_local_device().inject_library_file(target, path, entrypoint, data)


def inject_library_blob(target, blob, entrypoint, data):
return get_local_device().inject_library_blob(target, blob, entrypoint, data)


def enumerate_devices():
return get_device_manager().enumerate_devices()


def get_local_device():
return _get_device(lambda device: device.type == 'local', timeout=0)


def get_remote_device():
return _get_device(lambda device: device.type == 'remote', timeout=0)


def get_usb_device(timeout = 0):
return _get_device(lambda device: device.type == 'usb', timeout)


def get_device(id, timeout = 0):
return _get_device(lambda device: device.id == id, timeout)


def _get_device(predicate, timeout):
mgr = get_device_manager()
def find_matching_device():
Expand All @@ -103,6 +113,7 @@ def on_devices_changed():
raise TimedOutError("timed out while waiting for device to appear")
return device


def shutdown():
get_device_manager()._impl.close()

Expand Down

0 comments on commit 72899a4

Please sign in to comment.