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 ...@@ -29,22 +29,12 @@ You can also fork pyrasite on GitHub: http://github.com/lmacken/pyrasite
API API
~~~ ~~~
This pyrasite unit test injects a `print("Hello World!")` payload into a
process and ensures it gets printed.
:: ::
from pyrasite.inject import CodeInjector from pyrasite.inject import CodeInjector
def test_injection(self): ci = CodeInjector(p.pid)
cmd = 'python -c "import time; time.sleep(0.5)"' ci.inject('payloads/helloworld.py')
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"
Payloads Payloads
......
...@@ -28,18 +28,23 @@ Authors: ...@@ -28,18 +28,23 @@ Authors:
David Malcolm <dmalcolm@redhat.com> David Malcolm <dmalcolm@redhat.com>
""" """
import os, subprocess import os, subprocess, warnings
class CodeInjector(object): 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.pid = pid
self.filename = os.path.abspath(filename)
self.verbose = verbose self.verbose = verbose
self.gdb_prefix = gdb_prefix 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 = [ gdb_cmds = [
'PyGILState_Ensure()', 'PyGILState_Ensure()',
# Allow payloads to import modules alongside them # Allow payloads to import modules alongside them
......
...@@ -51,8 +51,8 @@ def main(): ...@@ -51,8 +51,8 @@ def main():
print "Error: The second argument must be a filename" print "Error: The second argument must be a filename"
sys.exit(4) sys.exit(4)
injector = CodeInjector(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix) injector = CodeInjector(pid, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
injector.inject() injector.inject(filename)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -25,8 +25,8 @@ class TestCodeInjection(unittest.TestCase): ...@@ -25,8 +25,8 @@ class TestCodeInjection(unittest.TestCase):
cmd = 'python -c "import time; time.sleep(0.5)"' cmd = 'python -c "import time; time.sleep(0.5)"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
ci = CodeInjector(p.pid, 'payloads/helloworld.py', verbose=True) ci = CodeInjector(p.pid, verbose=True)
ci.inject() ci.inject('payloads/helloworld.py')
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Code injection failed" assert 'Hello World!' in stdout, "Code injection failed"
...@@ -40,8 +40,8 @@ class TestCodeInjection(unittest.TestCase): ...@@ -40,8 +40,8 @@ class TestCodeInjection(unittest.TestCase):
p = subprocess.Popen('python -c "%s"' % ';'.join(cmd), p = subprocess.Popen('python -c "%s"' % ';'.join(cmd),
shell=True, stdout=subprocess.PIPE) shell=True, stdout=subprocess.PIPE)
ci = CodeInjector(p.pid, 'payloads/helloworld.py', verbose=True) ci = CodeInjector(p.pid, verbose=True)
ci.inject() ci.inject('payloads/helloworld.py')
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Multi-threaded code injection failed" 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