Commit c4bdfc77 authored by Luke Macken's avatar Luke Macken

Merge branch 'feature/py2.4' into develop

parents 7977ec40 a65049aa
from .inject import CodeInjector
from .inspect import ObjectInspector
from .ipc import PyrasiteIPC
from .reverse import ReverseConnection, ReversePythonConnection
__version__ = '2.0beta6' __version__ = '2.0beta6'
__all__ = ('CodeInjector', 'ObjectInspector', 'PyrasiteIPC', __all__ = ('CodeInjector', 'ObjectInspector', 'PyrasiteIPC',
'ReverseConnection', 'ReversePythonConnection') 'ReverseConnection', 'ReversePythonConnection')
...@@ -21,3 +16,8 @@ You should have received a copy of the GNU General Public License ...@@ -21,3 +16,8 @@ You should have received a copy of the GNU General Public License
along with pyrasite. If not, see <http://www.gnu.org/licenses/>.\ along with pyrasite. If not, see <http://www.gnu.org/licenses/>.\
""" """
__copyright__ = "Copyright (C) 2011, 2012 Red Hat, Inc." __copyright__ = "Copyright (C) 2011, 2012 Red Hat, Inc."
from pyrasite.inject import CodeInjector
from pyrasite.inspect import ObjectInspector
from pyrasite.ipc import PyrasiteIPC
from pyrasite.reverse import ReverseConnection, ReversePythonConnection
...@@ -52,11 +52,13 @@ class CodeInjector(object): ...@@ -52,11 +52,13 @@ class CodeInjector(object):
self.filename = os.path.abspath(filename) self.filename = os.path.abspath(filename)
gdb_cmds = [ gdb_cmds = [
'PyGILState_Ensure()', 'PyGILState_Ensure()',
# Allow payloads to import modules alongside them 'PyRun_SimpleString("'
'PyRun_SimpleString("import sys; sys.path.insert(0, \\"%s\\");")' % 'import sys; sys.path.insert(0, \\"%s\\"); '
os.path.dirname(self.filename), 'sys.path.insert(0, \\"%s\\"); '
'PyRun_SimpleString("exec(open(\\"%s\\").read())")' % 'exec(open(\\"%s\\").read())")' %
self.filename, (os.path.dirname(self.filename),
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')),
self.filename),
'PyGILState_Release($1)', 'PyGILState_Release($1)',
] ]
p = subprocess.Popen('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid, p = subprocess.Popen('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
......
...@@ -139,7 +139,7 @@ class PyrasiteIPC(object): ...@@ -139,7 +139,7 @@ class PyrasiteIPC(object):
def send(self, data): def send(self, data):
"""Send arbitrary data to the process via self.sock""" """Send arbitrary data to the process via self.sock"""
header = b'' header = ''.encode('utf-8')
data = data.encode('utf-8') data = data.encode('utf-8')
if self.reliable: if self.reliable:
header = struct.pack('<L', len(data)) header = struct.pack('<L', len(data))
...@@ -159,7 +159,7 @@ class PyrasiteIPC(object): ...@@ -159,7 +159,7 @@ class PyrasiteIPC(object):
def recv_bytes(self, n): def recv_bytes(self, n):
"""Receive n bytes from a socket""" """Receive n bytes from a socket"""
data = b'' data = ''.encode('utf-8')
while len(data) < n: while len(data) < n:
chunk = self.sock.recv(n - len(data)) chunk = self.sock.recv(n - len(data))
if not chunk: if not chunk:
......
...@@ -19,7 +19,7 @@ import os ...@@ -19,7 +19,7 @@ import os
import sys import sys
import argparse import argparse
from inject import CodeInjector import pyrasite
def main(): def main():
...@@ -58,8 +58,8 @@ def main(): ...@@ -58,8 +58,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, verbose=args.verbose, injector = pyrasite.CodeInjector(pid, verbose=args.verbose,
gdb_prefix=args.gdb_prefix) gdb_prefix=args.gdb_prefix)
injector.inject(filename) injector.inject(filename)
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
import sys import sys
import socket import socket
import traceback
import threading import threading
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
...@@ -82,8 +83,8 @@ class ReverseConnection(threading.Thread, PyrasiteIPC): ...@@ -82,8 +83,8 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
else: else:
running = self.on_command(cmd) running = self.on_command(cmd)
except Exception as e: except:
print(str(e)) traceback.print_exc()
running = False running = False
if not running: if not running:
self.close() self.close()
...@@ -102,11 +103,10 @@ class ReversePythonConnection(ReverseConnection): ...@@ -102,11 +103,10 @@ class ReversePythonConnection(ReverseConnection):
try: try:
exec(cmd) exec(cmd)
output = buffer.getvalue() output = buffer.getvalue()
except Exception as e: except:
output = str(e) output = traceback.format_exc()
finally: sys.stdout = sys.__stdout__
sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__
sys.stderr = sys.__stderr__ buffer.close()
buffer.close()
self.send(output) self.send(output)
return True return True
...@@ -20,6 +20,7 @@ import subprocess ...@@ -20,6 +20,7 @@ import subprocess
from pyrasite.inject import CodeInjector from pyrasite.inject import CodeInjector
class TestCodeInjection(unittest.TestCase): class TestCodeInjection(unittest.TestCase):
def test_injection(self): def test_injection(self):
...@@ -31,7 +32,8 @@ class TestCodeInjection(unittest.TestCase): ...@@ -31,7 +32,8 @@ class TestCodeInjection(unittest.TestCase):
ci.inject('pyrasite/payloads/helloworld.py') ci.inject('pyrasite/payloads/helloworld.py')
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
assert 'Hello World!' in stdout, "Code injection failed" assert 'Hello World!' in stdout.decode('utf-8'), \
"Code injection failed"
def test_multithreaded_injection(self): def test_multithreaded_injection(self):
cmd = [ cmd = [
...@@ -46,7 +48,9 @@ class TestCodeInjection(unittest.TestCase): ...@@ -46,7 +48,9 @@ class TestCodeInjection(unittest.TestCase):
ci.inject('pyrasite/payloads/helloworld.py') ci.inject('pyrasite/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.decode('utf-8'), \
"Multi-threaded code injection failed"
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
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