Inject code into running Python processes

Overview

http://pyrasite.com/logo.png

pyrasite

https://api.travis-ci.org/lmacken/pyrasite.png?branch=develop https://coveralls.io/repos/lmacken/pyrasite/badge.png?branch=develop https://pypip.in/v/pyrasite/badge.png https://pypip.in/d/pyrasite/badge.png

Tools for injecting arbitrary code into running Python processes.

homepage: http://pyrasite.com
documentation: http://pyrasite.rtfd.org
download: http://pypi.python.org/pypi/pyrasite
source: http://github.com/lmacken/pyrasite
screenshots: http://readthedocs.org/docs/pyrasite/en/latest/GUI.html
mailing list: https://fedorahosted.org/mailman/listinfo/pyrasite
jenkins: http://ci.csh.rit.edu/view/Pyrasite
irc: #pyrasite on Freenode

Requirements

  • gdb (version 7.3+ (or RHEL5+))

On OS X you will need to have a codesigned gdb - see https://sourceware.org/gdb/wiki/BuildingOnDarwin if you get errors while running with --verbose which mention codesigning.

Compatiblity

Pyrasite works with Python 2.4 and newer. Injection works between versions as well, so you can run Pyrasite under Python 3 and inject into 2, and vice versa.

pyrasite-gui

The graphical interface can be found here: https://github.com/lmacken/pyrasite-gui

http://lewk.org/img/pyrasite/pyrasite-info-thumb.png

Authors

Created by Luke Macken with the help of David Malcolm and many other contributors. Logo by Adam Saunders.

Licenses

Code

https://www.gnu.org/graphics/gplv3-127x51.png

Logo

https://creativecommons.org/images/deed/nolaw.png
Comments
  • reverse_python_shell.py showing '[Errno 111] Connection refused' in other process

    reverse_python_shell.py showing '[Errno 111] Connection refused' in other process

    When injecting the reverse_python_shell.py payload on an Ubuntu 11.10 system (after doing the echo 0 to ptrace_scope), the process I am connecting to (a python interpreter sitting at the shell) shows the following error:

    [Errno 111] Connection refused

    No error is reported from the pyrasite process. Tests with the hello_world.py payload work, though.

    opened by seibert 4
  • Problem with 'python setup.py develop'

    Problem with 'python setup.py develop'

    (pyrasite)threebean@marat ~/devel/pyrasite (git:develop:0)
     % python setup.py develop
    running develop
    running egg_info
    writing requirements to pyrasite.egg-info/requires.txt
    writing pyrasite.egg-info/PKG-INFO
    writing top-level names to pyrasite.egg-info/top_level.txt
    writing dependency_links to pyrasite.egg-info/dependency_links.txt
    writing entry points to pyrasite.egg-info/entry_points.txt
    reading manifest file 'pyrasite.egg-info/SOURCES.txt'
    writing manifest file 'pyrasite.egg-info/SOURCES.txt'
    running build_ext
    Creating /home/threebean/.virtualenvs/pyrasite/lib/python2.7/site-packages/pyrasite.egg-link (link to .)
    pyrasite 1.1 is already the active version in easy-install.pth
    Installing pyrasite-gui script to /home/threebean/.virtualenvs/pyrasite/bin
    Installing pyrasite-memory-viewer script to /home/threebean/.virtualenvs/pyrasite/bin
    Installing pyrasite script to /home/threebean/.virtualenvs/pyrasite/bin
    
    Installed /home/threebean/devel/pyrasite
    Processing dependencies for pyrasite==1.1
    Searching for meliae
    Reading http://pypi.python.org/simple/meliae/
    Reading https://launchpad.net/meliae
    Reading https://launchpad.net/meliae/+download
    Best match: meliae 0.4.0
    Downloading http://launchpad.net/meliae/trunk/0.4/+download/meliae-0.4.0.tar.gz
    Processing meliae-0.4.0.tar.gz
    Running meliae-0.4.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-IuIszE/meliae-0.4.0/egg-dist-tmp-9LFMD7
    We require Cython to be installed.
    No eggs found in /tmp/easy_install-IuIszE/meliae-0.4.0/egg-dist-tmp-9LFMD7 (setup script problem?)
    error: Could not find required distribution meliae
    
    opened by ralphbean 4
  • Add the ability to control the IPC timeout.

    Add the ability to control the IPC timeout.

    With a hardcoded 5-second timeout, it's impossible to run commands with pyrasite-shell that take more than 5 seconds. I was bitten by this recently when trying to get heap dumps from a large process, but it can be reproduced easily with time.sleep:

    $ pyrasite-shell 11166
    Pyrasite Shell 2.0
    Connected to 'python'
    Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (DistantInteractiveConsole)
    
    >>> import time
    >>> time.sleep(10)
    
    Traceback (most recent call last):
      File "/home/jakeschmidt/pyrasite/env/bin/pyrasite-shell", line 9, in <module>
        load_entry_point('pyrasite==2.0', 'console_scripts', 'pyrasite-shell')()
      File "/home/jakeschmidt/pyrasite/env/local/lib/python2.7/site-packages/pyrasite-2.0-py2.7.egg/pyrasite/tools/shell.py", line 70, in shell
        payload = ipc.recv()
      File "/home/jakeschmidt/pyrasite/env/local/lib/python2.7/site-packages/pyrasite-2.0-py2.7.egg/pyrasite/ipc.py", line 190, in recv
        header_data = self.recv_bytes(4)
      File "/home/jakeschmidt/pyrasite/env/local/lib/python2.7/site-packages/pyrasite-2.0-py2.7.egg/pyrasite/ipc.py", line 203, in recv_bytes
        chunk = self.sock.recv(n - len(data))
    socket.timeout: timed out
    
    opened by TheJakeSchmidt 3
  • No response when using pyrasite-shell

    No response when using pyrasite-shell

    Hey Luke/Pyrasite crew, I'm trying to attach a shell to a twisted process, and all i'm getting is a hung shell. So I SIGINT and rerun with a --verbose, like: pyrasite --verbose 14129 dump_modules.py and I see:

    [Thread debugging using libthread_db enabled]
    [New Thread 0x7fd91ed28700 (LWP 15516)]
    [New Thread 0x7fd91f529700 (LWP 15515)]
    [New Thread 0x7fd91fd2a700 (LWP 15514)]
    [New Thread 0x7fd924a8b700 (LWP 14332)]
    [New Thread 0x7fd92528c700 (LWP 14133)]
    [New Thread 0x7fd925a8d700 (LWP 14130)]
    0x00007fd92a82a163 in epoll_wait () from /lib/libc.so.6
    $1 = PyGILState_UNLOCKED
    $2 = 0
    
    
    warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff075fe000
    

    Waiting at an epoll, as one would expect from a Twisted process running on Debian:

    # uname -a
    Linux graybles 2.6.32-042stab079.6 #1 SMP Mon Aug 26 19:47:50 MSK 2013 x86_64 GNU/Linux
    

    Also, I'm using the pip-installable versions of all relevant libraries. Let me know if there's anything else I can provide. And of course, thanks for making pyrasite as it is very awesome. :)

    opened by mahmoud 3
  • Pyrasite freezes everytime I run

    Pyrasite freezes everytime I run

    I installed in archlinux using the PKGBUILD in the pkgs/arch folder. If I run pyrasite-gui, the gui freezes when I click in any program on the left list. Basically the same thing happens if I run using pyrasite-shell - I get no output at all and I've to kill the process. Using just pyrasite nothing happens.

    opened by sollidsnake 3
  • pyrasite-memory-viewer does not work

    pyrasite-memory-viewer does not work

    I cloned master, easy_install'ed it into ~/py-lib, tried pyrasite-memory-viewer somepid and got this:

    ImportError: No module named urwid
    

    After easy_installing urwid I re-ran that command and got

    Traceback (most recent call last):
      File "/home/mg/bin/pyrasite-memory-viewer", line 9, in <module>
        load_entry_point('pyrasite==2.0beta9', 'console_scripts', 'pyrasite-memory-viewer')()
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 305, in load_entry_point
        return get_distribution(dist).load_entry_point(group, name)
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2245, in load_entry_point
        return ep.load()
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1955, in load
        entry = __import__(self.module_name, globals(),globals(), ['__name__'])
      File "/home/mg/py-lib/pyrasite-2.0beta9-py2.7.egg/pyrasite/tools/memory_viewer.py", line 34, in <module>
        from pyrasite.inspect import ObjectInspector
    ImportError: No module named inspect
    
    opened by mgedmin 3
  • Inject/UnInject some code in anywhere

    Inject/UnInject some code in anywhere

    Hi pyrasite can inject some code anywhere?
    like Btrace (https://kenai.com/projects/btrace/pages/UserGuide) for example , I have a function like:

         def index(request):
             for k,v in request.iteritems():
                  #inject code there, like: import pdb;pdb.set_trace()
                  print(k,v)
    

    when debug done, remove injected code.

    Tks

    RFE 
    opened by alex8224 2
  • [Mac OSX]Use the pyrasite always remain on ipc.connect

    [Mac OSX]Use the pyrasite always remain on ipc.connect

    (g)dreampuf@HX:~/hub/fortest/hello$ pyrasite-shell 35548
    ^CTraceback (most recent call last):
      File "/Users/dreampuf/.virtualenvs/g/bin/pyrasite-shell", line 8, in <module>
        load_entry_point('pyrasite==2.0', 'console_scripts', 'pyrasite-shell')()
      File "/Users/dreampuf/.virtualenvs/g/lib/python2.7/site-packages/pyrasite/tools/shell.py", line 30, in shell
        ipc.connect()
      File "/Users/dreampuf/.virtualenvs/g/lib/python2.7/site-packages/pyrasite/ipc.py", line 95, in connect
        self.wait()
      File "/Users/dreampuf/.virtualenvs/g/lib/python2.7/site-packages/pyrasite/ipc.py", line 151, in wait
        (clientsocket, address) = self.server_sock.accept()
      File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 202, in accept
        sock, addr = self._sock.accept()
    KeyboardInterrupt
    

    Did I need the special code insert the remote process? or other version of gdb (I could not find the version 7.2 on homebrew)

    (g)dreampuf@HX:~/hub/fortest/hello$ gdb --version
    GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Thu Nov 15 10:42:43 UTC 2012)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin".
    
    opened by dreampuf 2
  • Handle PyGILState_LOCKED race conditions.

    Handle PyGILState_LOCKED race conditions.

    ======================================================================
    FAIL: test_multithreaded_injection (tests.test_code_injection.TestCodeInjection)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/lmacken/code/github.com/pyrasite/tests/test_code_injection.py", line 50, in test_multithreaded_injection
        "Multi-threaded code injection failed"
    AssertionError: AssertionError: Multi-threaded code injection failed
    -------------------- >> begin captured stdout << ---------------------
    b'[Thread debugging using libthread_db enabled]\nUsing host libthread_db library "/lib64/libthread_db.so.1".\nPyEval_EvalF
    rameEx (f=<optimized out>, throwflag=<optimized out>) at /usr/src/debug/Python-2.7.2/Python/ceval.c:2966\n2966\t
      while (STACK_LEVEL() > b->b_level) {\n$1 = PyGILState_LOCKED\n$2 = -1\n'
    
    opened by lmacken 2
  • Local term output

    Local term output

    As discussed in IRC it would be desirable to have output from the injected python commands to be piped back and printed out on the local terminal. This commit is an attempt at adding that functionality into the pyrasite executable (main.py).

    opened by dustymabe 1
  • Add support for tox (http://tox.testrun.org/) and Travis CI (http://travis-ci.org/)

    Add support for tox (http://tox.testrun.org/) and Travis CI (http://travis-ci.org/)

    Successful Travis CI run: http://travis-ci.org/#!/msabramo/pyrasite/jobs/1690081

    To set up Travis CI for your own repo, see http://about.travis-ci.org/docs/user/getting-started/

    Output of tox:

    marca@ubuntu:~/dev/git-repos/pyrasite$ sudo tox 2>&1 | tee tox.out
    ...
    ___________________________________ summary ____________________________________
      py26: commands succeeded
      py27: commands succeeded
      congratulations :)
    
    opened by msabramo 1
  • pyrasite doesn`t work in some docker image

    pyrasite doesn`t work in some docker image

    when i execute the command in this docker images docker pull yumoshangguan/centos7.7.1908-gcc8.3.0-python3.6-qt5.14.0:v0.0.03-debug-for-python3

    pip3 install pyrasite

    it can be installed successfully, but i cannot find the pyrasite, pyrasite-shell, pyrasite-memroy-viewer in /usr/bin, so i cannot execute the command pyrasite or pyrasite-shell ...

    not all images occur the problem

    opened by pbl357 0
  • KeyError: 'address 139644515901800 not present'

    KeyError: 'address 139644515901800 not present'

    (new_sdk) [root@host-01 test]# ps -aux|grep 19373
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    xxxx      19373 61.3 12.9 22286416 17040500 ?   Sl   06:03 441:10 /opt/.pyenv/versions/new_sdk/bin/python controller.py
    
    
    (new_sdk) [root@host-01 test]# pyrasite-memory-viewer 19373
    loaded line 13598, 13599 objs,   1.6 /   1.6 MiB read in 0.2s        
    Traceback (most recent call last):sed        0    
      File "/opt/.pyenv/versions/new_sdk/bin/pyrasite-memory-viewer", line 11, in <module>
        load_entry_point('pyrasite==2.0', 'console_scripts', 'pyrasite-memory-viewer')()
      File "/opt/.pyenv/versions/new_sdk/lib/python3.6/site-packages/pyrasite/tools/memory_viewer.py", line 150, in main
        objects = loader.load(filename)
      File "/opt/.pyenv/versions/new_sdk/lib/python3.6/site-packages/meliae/loader.py", line 562, in load
        if not manager.collapse_instance_dicts():
      File "/opt/.pyenv/versions/new_sdk/lib/python3.6/site-packages/meliae/loader.py", line 432, in collapse_instance_dicts
        obj_1, obj_2 = obj
      File "meliae/_loader.pyx", line 481, in meliae._loader._MemObjectProxy.__getitem__
      File "meliae/_loader.pyx", line 477, in meliae._loader._MemObjectProxy.__getitem__
      File "meliae/_loader.pyx", line 767, in meliae._loader.MemObjectCollection.__getitem__
    KeyError: 'address 139644515901800 not present'
    
    opened by acracker 0
  • docs: fix simple typo, outpu -> output

    docs: fix simple typo, outpu -> output

    There is a small typo in pyrasite/tools/memory_viewer.py.

    Should read output rather than outpu.

    Semi-automated pull request generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    opened by timgates42 0
  • Can it work with Pyinstaller (Linux)?

    Can it work with Pyinstaller (Linux)?

    I tried writing a very simple Python program (using Ubuntu, Python 2.7) and I can apply pyrasite-shell without problem on it. However, when I use pyinstaller (with the -F option for simple file) and then try to apply pyrasite-shell on that process, this produces a segmentation fault - it looks like a GIL issue. Is there any way around this?

    opened by sergejkl 0
  • pyrasite-shell causes segmentation fault in Python 3

    pyrasite-shell causes segmentation fault in Python 3

    Using $pyrasite-shell <PID> in python 3 causes the running process(<PID>) to face a segmentation fault. The only exception is when both the running process andpyrasite-shell are executed with sudo. Any solutions or workaround?

    opened by mehrdad-h 2
Owner
Luke Macken
Python, Machine Learning, Data Science, Quant Finance, Algo Trading, Linux, Security.
Luke Macken
Monitor Memory usage of Python code

Memory Profiler This is a python module for monitoring memory consumption of a process as well as line-by-line analysis of memory consumption for pyth

Fabian Pedregosa 80 Nov 18, 2022
VizTracer is a low-overhead logging/debugging/profiling tool that can trace and visualize your python code execution.

VizTracer is a low-overhead logging/debugging/profiling tool that can trace and visualize your python code execution.

null 2.8k Jan 8, 2023
Visual Interaction with Code - A portable visual debugger for python

VIC Visual Interaction with Code A simple tool for debugging and interacting with running python code. This tool is designed to make it easy to inspec

Nathan Blank 1 Nov 16, 2021
Hunter is a flexible code tracing toolkit.

Overview docs tests package Hunter is a flexible code tracing toolkit, not for measuring coverage, but for debugging, logging, inspection and other ne

Ionel Cristian Mărieș 705 Dec 8, 2022
The official code of LM-Debugger, an interactive tool for inspection and intervention in transformer-based language models.

LM-Debugger is an open-source interactive tool for inspection and intervention in transformer-based language models. This repository includes the code

Mor Geva 110 Dec 28, 2022
pdb++, a drop-in replacement for pdb (the Python debugger)

pdb++, a drop-in replacement for pdb What is it? This module is an extension of the pdb module of the standard library. It is meant to be fully compat

null 1k Dec 24, 2022
Full-screen console debugger for Python

PuDB: a console-based visual debugger for Python Its goal is to provide all the niceties of modern GUI-based debuggers in a more lightweight and keybo

Andreas Klöckner 2.6k Jan 1, 2023
Trace any Python program, anywhere!

lptrace lptrace is strace for Python programs. It lets you see in real-time what functions a Python program is running. It's particularly useful to de

Karim Hamidou 687 Nov 20, 2022
Debugging manhole for python applications.

Overview docs tests package Manhole is in-process service that will accept unix domain socket connections and present the stacktraces for all threads

Ionel Cristian Mărieș 332 Dec 7, 2022
(OLD REPO) Line-by-line profiling for Python - Current repo ->

line_profiler and kernprof line_profiler is a module for doing line-by-line profiling of functions. kernprof is a convenient script for running either

Robert Kern 3.6k Jan 6, 2023
Sampling profiler for Python programs

py-spy: Sampling profiler for Python programs py-spy is a sampling profiler for Python programs. It lets you visualize what your Python program is spe

Ben Frederickson 9.5k Jan 8, 2023
🔥 Pyflame: A Ptracing Profiler For Python. This project is deprecated and not maintained.

Pyflame: A Ptracing Profiler For Python (This project is deprecated and not maintained.) Pyflame is a high performance profiling tool that generates f

Uber Archive 3k Jan 7, 2023
Visual profiler for Python

vprof vprof is a Python package providing rich and interactive visualizations for various Python program characteristics such as running time and memo

Nick Volynets 3.9k Jan 1, 2023
Parsing ELF and DWARF in Python

pyelftools pyelftools is a pure-Python library for parsing and analyzing ELF files and DWARF debugging information. See the User's guide for more deta

Eli Bendersky 1.6k Jan 4, 2023
pdb++, a drop-in replacement for pdb (the Python debugger)

pdb++, a drop-in replacement for pdb What is it? This module is an extension of the pdb module of the standard library. It is meant to be fully compat

null 1k Jan 2, 2023
Run-time type checker for Python

This library provides run-time type checking for functions defined with PEP 484 argument (and return) type annotations. Four principal ways to do type

Alex Grönholm 1.1k Jan 5, 2023
Graphical Python debugger which lets you easily view the values of all evaluated expressions

birdseye birdseye is a Python debugger which records the values of expressions in a function call and lets you easily view them after the function exi

Alex Hall 1.5k Dec 24, 2022
A powerful set of Python debugging tools, based on PySnooper

snoop snoop is a powerful set of Python debugging tools. It's primarily meant to be a more featureful and refined version of PySnooper. It also includ

Alex Hall 874 Jan 8, 2023
Cyberbrain: Python debugging, redefined.

Cyberbrain1(电子脑) aims to free programmers from debugging.

laike9m 2.3k Jan 7, 2023