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'
__all__ = ('CodeInjector', 'ObjectInspector', 'PyrasiteIPC',
'ReverseConnection', 'ReversePythonConnection')
......@@ -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/>.\
"""
__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):
self.filename = os.path.abspath(filename)
gdb_cmds = [
'PyGILState_Ensure()',
# Allow payloads to import modules alongside them
'PyRun_SimpleString("import sys; sys.path.insert(0, \\"%s\\");")' %
os.path.dirname(self.filename),
'PyRun_SimpleString("exec(open(\\"%s\\").read())")' %
self.filename,
'PyRun_SimpleString("'
'import sys; sys.path.insert(0, \\"%s\\"); '
'sys.path.insert(0, \\"%s\\"); '
'exec(open(\\"%s\\").read())")' %
(os.path.dirname(self.filename),
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')),
self.filename),
'PyGILState_Release($1)',
]
p = subprocess.Popen('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
......
......@@ -139,7 +139,7 @@ class PyrasiteIPC(object):
def send(self, data):
"""Send arbitrary data to the process via self.sock"""
header = b''
header = ''.encode('utf-8')
data = data.encode('utf-8')
if self.reliable:
header = struct.pack('<L', len(data))
......@@ -159,7 +159,7 @@ class PyrasiteIPC(object):
def recv_bytes(self, n):
"""Receive n bytes from a socket"""
data = b''
data = ''.encode('utf-8')
while len(data) < n:
chunk = self.sock.recv(n - len(data))
if not chunk:
......
......@@ -19,7 +19,7 @@ import os
import sys
import argparse
from inject import CodeInjector
import pyrasite
def main():
......@@ -58,8 +58,8 @@ def main():
print("Error: The second argument must be a filename")
sys.exit(4)
injector = CodeInjector(pid, verbose=args.verbose,
gdb_prefix=args.gdb_prefix)
injector = pyrasite.CodeInjector(pid, verbose=args.verbose,
gdb_prefix=args.gdb_prefix)
injector.inject(filename)
......
......@@ -21,6 +21,7 @@
import sys
import socket
import traceback
import threading
if sys.version_info[0] == 3:
......@@ -82,8 +83,8 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
else:
running = self.on_command(cmd)
except Exception as e:
print(str(e))
except:
traceback.print_exc()
running = False
if not running:
self.close()
......@@ -102,11 +103,10 @@ class ReversePythonConnection(ReverseConnection):
try:
exec(cmd)
output = buffer.getvalue()
except Exception as e:
output = str(e)
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
buffer.close()
except:
output = traceback.format_exc()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
buffer.close()
self.send(output)
return True
......@@ -20,6 +20,7 @@ import subprocess
from pyrasite.inject import CodeInjector
class TestCodeInjection(unittest.TestCase):
def test_injection(self):
......@@ -31,7 +32,8 @@ class TestCodeInjection(unittest.TestCase):
ci.inject('pyrasite/payloads/helloworld.py')
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):
cmd = [
......@@ -46,7 +48,9 @@ class TestCodeInjection(unittest.TestCase):
ci.inject('pyrasite/payloads/helloworld.py')
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__':
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