Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pyrasite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
pyrasite
Commits
8ba4f0ae
Commit
8ba4f0ae
authored
Feb 11, 2012
by
Luke Macken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deprecate passing the payload in via the CodeInjector constructor; use the inject method instead.
parent
902e7abc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
23 deletions
+18
-23
README.rst
README.rst
+2
-12
pyrasite/inject.py
pyrasite/inject.py
+10
-5
pyrasite/main.py
pyrasite/main.py
+2
-2
tests/test_code_injection.py
tests/test_code_injection.py
+4
-4
No files found.
README.rst
View file @
8ba4f0ae
...
...
@@ -29,22 +29,12 @@ You can also fork pyrasite on GitHub: http://github.com/lmacken/pyrasite
API
~~~
This pyrasite unit test injects a `print("Hello World!")` payload into a
process and ensures it gets printed.
::
from pyrasite.inject import CodeInjector
def test_injection(self):
cmd = 'python -c "import time; time.sleep(0.5)"'
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"
ci = CodeInjector(p.pid)
ci.inject('payloads/helloworld.py')
Payloads
...
...
pyrasite/inject.py
View file @
8ba4f0ae
...
...
@@ -28,18 +28,23 @@ Authors:
David Malcolm <dmalcolm@redhat.com>
"""
import
os
,
subprocess
import
os
,
subprocess
,
warnings
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
.
filename
=
os
.
path
.
abspath
(
filename
)
self
.
verbose
=
verbose
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
=
[
'PyGILState_Ensure()'
,
# Allow payloads to import modules alongside them
...
...
pyrasite/main.py
View file @
8ba4f0ae
...
...
@@ -51,8 +51,8 @@ def main():
print
"Error: The second argument must be a filename"
sys
.
exit
(
4
)
injector
=
CodeInjector
(
pid
,
filename
,
verbose
=
args
.
verbose
,
gdb_prefix
=
args
.
gdb_prefix
)
injector
.
inject
()
injector
=
CodeInjector
(
pid
,
verbose
=
args
.
verbose
,
gdb_prefix
=
args
.
gdb_prefix
)
injector
.
inject
(
filename
)
if
__name__
==
'__main__'
:
main
()
tests/test_code_injection.py
View file @
8ba4f0ae
...
...
@@ -25,8 +25,8 @@ class TestCodeInjection(unittest.TestCase):
cmd
=
'python -c "import time; time.sleep(0.5)"'
p
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
)
ci
=
CodeInjector
(
p
.
pid
,
'payloads/helloworld.py'
,
verbose
=
True
)
ci
.
inject
()
ci
=
CodeInjector
(
p
.
pid
,
verbose
=
True
)
ci
.
inject
(
'payloads/helloworld.py'
)
stdout
,
stderr
=
p
.
communicate
()
assert
'Hello World!'
in
stdout
,
"Code injection failed"
...
...
@@ -40,8 +40,8 @@ class TestCodeInjection(unittest.TestCase):
p
=
subprocess
.
Popen
(
'python -c "%s"'
%
';'
.
join
(
cmd
),
shell
=
True
,
stdout
=
subprocess
.
PIPE
)
ci
=
CodeInjector
(
p
.
pid
,
'payloads/helloworld.py'
,
verbose
=
True
)
ci
.
inject
()
ci
=
CodeInjector
(
p
.
pid
,
verbose
=
True
)
ci
.
inject
(
'payloads/helloworld.py'
)
stdout
,
stderr
=
p
.
communicate
()
assert
'Hello World!'
in
stdout
,
"Multi-threaded code injection failed"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment