def main(): if len(sys.argv) != 2: print("[ pyrasite memory viewer ]\n") print("Usage: %s <pid> <objects.json>" % sys.argv[0]) print("\n pid - the running process id") print("") sys.exit(1) pid = int(sys.argv[1]) payload = abspath(join(dirname(__file__), '..', 'payloads', 'dump_memory.py')) pyrasite.inject(pid, payload) filename = '/tmp/pyrasite-%d-objects.json' % pid # Work around bug caused by meliae dumping unicode strings: # https://bugs.launchpad.net/meliae/+bug/876810 with open(filename) as sample_file, open(filename + '.tmp', 'w') as output_file: pattern = re.compile(r"(?<!\\)\\u([dD][0-9a-fA-F]{3,3})") for line in sample_file: output_file.write(pattern.sub("#S\g<1>", line)) os.rename(filename + '.tmp', filename) objects = loader.load(filename) objects.compute_referrers() PyrasiteMemoryViewer(pid=pid, objects=objects).main()
def test_injecting_into_all_interpreters(self): program = generate_program() try: for exe in interpreters(): print("sys.executable = %s" % sys.executable) print("injecting into %s" % exe) p = run_program(program, exe=exe) pyrasite.inject(p.pid, 'pyrasite/payloads/helloworld.py', verbose=True) stop_program(p) stdout, stderr = p.communicate() self.assert_output_contains(stdout, stderr, 'Hello World!') finally: os.unlink(program)
def main(): ptrace_check() parser = argparse.ArgumentParser( description='pyrasite - inject code into a running python process', epilog="For updates, visit https://github.com/lmacken/pyrasite") parser.add_argument('pid', nargs='?', help="The ID of the process to inject code into") parser.add_argument('payload', nargs='?', default='', help="The Python script to be executed inside the" " running process. Can be one of the standard" " payloads (see --list-payloads) or a filename.") parser.add_argument('-l', '--list-payloads', help='List standard payloads', default=False, action='store_const', const=True) parser.add_argument('--gdb-prefix', dest='gdb_prefix', help='GDB prefix (if specified during installation)', default="") parser.add_argument('--verbose', dest='verbose', help='Verbose mode', default=False, action='store_const', const=True) if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() if args.list_payloads: print("Available payloads:") for payload in list_payloads(): print(" %s" % payload) sys.exit() try: pid = int(args.pid) except ValueError: print("Error: The first argument must be a pid") sys.exit(2) filename = expand_payload(args.payload) if filename: if not os.path.exists(filename): print("Error: Invalid path or file doesn't exist") sys.exit(3) else: print("Error: The second argument must be a filename or a payload name") sys.exit(4) pyrasite.inject(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
def main(): ptrace_check() parser = argparse.ArgumentParser( description='pyrasite - inject code into a running python process', epilog="For updates, visit https://github.com/lmacken/pyrasite") parser.add_argument('pid', nargs='?', help="The ID of the process to inject code into") parser.add_argument('payload', nargs='?', default='', help="The Python script to be executed inside the" " running process. Can be one of the standard" " payloads (see --list-payloads) or a filname.") parser.add_argument('-l', '--list-payloads', help='List standard payloads', default=False, action='store_const', const=True) parser.add_argument('--gdb-prefix', dest='gdb_prefix', help='GDB prefix (if specified during installation)', default="") parser.add_argument('--verbose', dest='verbose', help='Verbose mode', default=False, action='store_const', const=True) if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() if args.list_payloads: print("Available payloads:") for payload in list_payloads(): print(" %s" % payload) sys.exit() try: pid = int(args.pid) except ValueError: print("Error: The first argument must be a pid") sys.exit(2) filename = expand_payload(args.payload) if filename: if not os.path.exists(filename): print("Error: Invalid path or file doesn't exist") sys.exit(3) else: print("Error: The second argument must be a filename or a payload name") sys.exit(4) pyrasite.inject(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
def test_many_payloads_into_program_with_many_threads(self): program = generate_program(threads=25) num_payloads = 25 try: for exe in interpreters(): p = run_program(program, exe=exe) for i in range(num_payloads): pyrasite.inject(p.pid, 'pyrasite/payloads/helloworld.py', verbose=True) stop_program(p) stdout, stderr = p.communicate() count = 0 for line in stdout.decode('utf-8').split('\n'): if line.strip() == 'Hello World!': count += 1 assert count == num_payloads, "Read %d hello worlds" % count finally: os.unlink(program)
def main(): if len(sys.argv) != 2: print("[ pyrasite memory viewer ]\n") print("Usage: %s <pid> <objects.json>" % sys.argv[0]) print("\n pid - the running process id") print("") sys.exit(1) pid = int(sys.argv[1]) payload = abspath(join(dirname(__file__), '..', 'payloads', 'dump_memory.py')) pyrasite.inject(pid, payload) filename = '/tmp/pyrasite-%d-objects.json' % pid objects = loader.load(filename) objects.compute_referrers() PyrasiteMemoryViewer(pid=pid, objects=objects).main()
def main(): if len(sys.argv) != 2: print("[ pyrasite memory viewer ]\n") print("Usage: %s <pid> <objects.json>" % sys.argv[0]) print("\n pid - the running process id") print("") sys.exit(1) pid = int(sys.argv[1]) payload = abspath( join(dirname(__file__), '..', 'payloads', 'dump_memory.py')) pyrasite.inject(pid, payload) filename = '/tmp/pyrasite-%d-objects.json' % pid objects = loader.load(filename) objects.compute_referrers() PyrasiteMemoryViewer(pid=pid, objects=objects).main()
def main(): ptrace_check() parser = argparse.ArgumentParser( description='pyrasite - inject code into a running python process', epilog="For updates, visit https://github.com/lmacken/pyrasite") parser.add_argument('pid', help="The ID of the process to inject code into") parser.add_argument('filename', help="The Python script to be executed inside the" " running process, also referred to as 'payload'") parser.add_argument('--gdb-prefix', dest='gdb_prefix', help='GDB prefix (if specified during installation)', default="") parser.add_argument('--verbose', dest='verbose', help='Verbose mode', default=False, action='store_const', const=True) if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() try: pid = int(args.pid) except ValueError: print("Error: The first argument must be a pid") sys.exit(2) filename = args.filename if filename: if not os.path.exists(filename): print("Error: Invalid path or file doesn't exist") sys.exit(3) else: print("Error: The second argument must be a filename") sys.exit(4) pyrasite.inject(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
def inject(self): """Inject the payload into the process.""" filename = self.create_payload() pyrasite.inject(self.pid, filename) os.unlink(filename)
''' inject variables into the running simulations, so that they finish off quicker works only with python2 ''' import time import os import subprocess import pyrasite process = subprocess.Popen( "pgrep -f no_dist_model.py", shell=True, stdout=subprocess.PIPE, ) stdout = process.communicate()[0] stdout_list = stdout.split('\n') for pid in stdout_list: print("##Injecting: ", pid) pyrasite.inject(int(pid), "inject.py", verbose=True)
def inject(self): """Inject the payload into the process.""" filename = self.create_payload() pyrasite.inject(self.pid, filename) os.unlink(filename)
def main(): ptrace_check() parser = argparse.ArgumentParser( description='pyrasite - inject code into a running python process', epilog="For updates, visit https://github.com/lmacken/pyrasite") parser.add_argument('pid', nargs='?', help="The ID of the process to inject code into") parser.add_argument('payload', nargs='?', default='', help="The Python script to be executed inside the" " running process. Can be one of the standard" " payloads (see --list-payloads) or a filname.") parser.add_argument('-l', '--list-payloads', help='List standard payloads', default=False, action='store_const', const=True) parser.add_argument('--gdb-prefix', dest='gdb_prefix', help='GDB prefix (if specified during installation)', default="") parser.add_argument('--verbose', dest='verbose', help='Verbose mode', default=False, action='store_const', const=True) parser.add_argument('--output', dest='output_type', default='procstreams', action='store', help="Set where output is to be printed. 'procstreams'" " prints output in stdout/stderr of running process" " and 'localterm' prints output in local terminal.") parser.add_argument('--ipc-timeout', dest='ipc_timeout', default=5, action='store', type=int, help="The number of seconds to wait for the injected" " code to reply over IPC before giving up.") if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() if args.list_payloads: print("Available payloads:") for payload in list_payloads(): print(" %s" % payload) sys.exit() # Make sure the output type is valid (procstreams || localterm) if args.output_type != 'procstreams' and args.output_type != 'localterm': print("Error: --output arg must be 'procstreams' or 'localterm'") sys.exit(5) try: pid = int(args.pid) except ValueError: print("Error: The first argument must be a pid") sys.exit(2) filename = expand_payload(args.payload) if filename: if not os.path.exists(filename): print("Error: Invalid path or file doesn't exist") sys.exit(3) else: print("Error: The second argument must be a filename or a payload name") sys.exit(4) if args.output_type == 'localterm': # Create new IPC connection to the process. ipc = pyrasite.PyrasiteIPC(pid, 'ReversePythonConnection', timeout=ipc_timeout) ipc.connect() print("Pyrasite Shell %s" % pyrasite.__version__) print("Connected to '%s'" % ipc.title) # Read in the payload fd = open(filename) payload = fd.read() fd.close # Run the payload, print output, close ipc connection print(ipc.cmd(payload)) ipc.close() else: pyrasite.inject(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)