Commit aa12ec18 authored by Luke Macken's avatar Luke Macken

Merge pull request #1 from kennyshen/master

Use argparse for parsing command line args, also added a prefix option for gdb
parents e3373c06 e720e770
......@@ -6,7 +6,7 @@ Injects code into a running Python process.
Requirements
~~~~~~~~~~~~
- gdb (https://www.gnu.org/s/gdb)
- gdb (https://www.gnu.org/s/gdb) (version 7.3+)
Download
~~~~~~~~
......@@ -113,6 +113,20 @@ Dumping modules, thread stacks, and forcing garbage collection
payloads/dump_modules.py
payloads/dump_stacks.py
payloads/force_garbage_collection.py
Additional installation notes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mac OS X
--------
If you don't want to override Apple's default gdb, install the latest version of gdb with a prefix (e.g. gnu)
::
$ ./configure --program-prefix=gnu
$ pyrasite <PID> payloads/reverse_python_shell.py --prefix="gnu"
Mailing List
~~~~~~~~~~~~
......
......@@ -30,12 +30,14 @@ Authors:
import os, subprocess
class CodeInjector(object):
def __init__(self, pid, filename, verbose=False):
def __init__(self, pid, filename, verbose=False, gdb_prefix=""):
self.pid = pid
self.filename = os.path.abspath(filename)
self.verbose = verbose
self.gdb_prefix = gdb_prefix
def inject(self):
gdb_cmds = [
......@@ -46,7 +48,7 @@ class CodeInjector(object):
'PyRun_SimpleString("execfile(\\"%s\\")")' % self.filename,
'PyGILState_Release($1)',
]
self._run('gdb -p %d -batch %s' % (self.pid,
self._run('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
' '.join(["-eval-command='call %s'" % cmd for cmd in gdb_cmds])))
def _run(self, cmd):
......
......@@ -16,28 +16,42 @@
# Copyright (C) 2011 Red Hat, Inc.
import os, sys
import argparse
from inject import CodeInjector
def main():
if len(sys.argv) < 3:
print("Usage: %s <pid> <filename>" % sys.argv[0])
print("\n pid:\tThe ID of the process to inject code into")
print(" filename:\tThe .py file to inject into the process\n")
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', default=None, nargs='?', help="The second argument must be a filename")
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(sys.argv[1])
pid = int(args.pid)
except ValueError:
print "Error: The first argument must be a pid"
sys.exit(2)
filename = sys.argv[2]
if not os.path.exists(filename):
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(3)
sys.exit(4)
injector = CodeInjector(pid, filename, verbose='-v' in sys.argv)
injector = CodeInjector(pid, filename, verbose=args.verbose, gdb_prefix=args.gdb_prefix)
injector.inject()
if __name__ == '__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