Commit 8ba4f0ae authored by Luke Macken's avatar Luke Macken

Deprecate passing the payload in via the CodeInjector constructor; use the inject method instead.

parent 902e7abc
......@@ -29,22 +29,12 @@ You can also fork pyrasite on GitHub: http://github.com/lmacken/pyrasite
API
~~~
This pyrasite unit test injects a `print("Hello World!")` payload into a
process and ensures it gets printed.
::
from pyrasite.inject import CodeInjector
def test_injection(self):
cmd = 'python -c "import time; time.sleep(0.5)"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
ci = CodeInjector(p.pid, 'payloads/helloworld.py')
ci.inject()
stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Code injection failed"
ci = CodeInjector(p.pid)
ci.inject('payloads/helloworld.py')
Payloads
......
......@@ -28,18 +28,23 @@ Authors:
David Malcolm <dmalcolm@redhat.com>
"""
import os, subprocess
import os, subprocess, warnings
class CodeInjector(object):
def __init__(self, pid, filename, verbose=False, gdb_prefix=""):
def __init__(self, pid, filename=None, verbose=False, gdb_prefix=""):
self.pid = pid
self.filename = os.path.abspath(filename)
self.verbose = verbose
self.gdb_prefix = gdb_prefix
if filename:
warnings.warn('Passing the payload in via the constructor is '
'deprecated. Please pass it to the "inject" method '
'instead.')
self.filename = os.path.abspath(filename)
def inject(self):
def inject(self, filename=None):
if filename:
self.filename = os.path.abspath(filename)
gdb_cmds = [
'PyGILState_Ensure()',
# Allow payloads to import modules alongside them
......
......@@ -51,8 +51,8 @@ def main():
print "Error: The second argument must be a filename"
sys.exit(4)
injector = CodeInjector(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
injector.inject()
injector = CodeInjector(pid, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
injector.inject(filename)
if __name__ == '__main__':
main()
......@@ -25,8 +25,8 @@ class TestCodeInjection(unittest.TestCase):
cmd = 'python -c "import time; time.sleep(0.5)"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
ci = CodeInjector(p.pid, 'payloads/helloworld.py', verbose=True)
ci.inject()
ci = CodeInjector(p.pid, verbose=True)
ci.inject('payloads/helloworld.py')
stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Code injection failed"
......@@ -40,8 +40,8 @@ class TestCodeInjection(unittest.TestCase):
p = subprocess.Popen('python -c "%s"' % ';'.join(cmd),
shell=True, stdout=subprocess.PIPE)
ci = CodeInjector(p.pid, 'payloads/helloworld.py', verbose=True)
ci.inject()
ci = CodeInjector(p.pid, verbose=True)
ci.inject('payloads/helloworld.py')
stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Multi-threaded code injection failed"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment