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
c4bdfc77
Commit
c4bdfc77
authored
Mar 18, 2012
by
Luke Macken
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/py2.4' into develop
parents
7977ec40
a65049aa
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
31 additions
and
25 deletions
+31
-25
pyrasite/__init__.py
pyrasite/__init__.py
+5
-5
pyrasite/inject.py
pyrasite/inject.py
+7
-5
pyrasite/ipc.py
pyrasite/ipc.py
+2
-2
pyrasite/main.py
pyrasite/main.py
+3
-3
pyrasite/reverse.py
pyrasite/reverse.py
+8
-8
tests/test_code_injection.py
tests/test_code_injection.py
+6
-2
No files found.
pyrasite/__init__.py
View file @
c4bdfc77
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
pyrasite/inject.py
View file @
c4bdfc77
...
...
@@ -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
,
...
...
pyrasite/ipc.py
View file @
c4bdfc77
...
...
@@ -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
:
...
...
pyrasite/main.py
View file @
c4bdfc77
...
...
@@ -19,7 +19,7 @@ import os
import
sys
import
argparse
from
inject
import
CodeInjector
import
pyrasite
def
main
():
...
...
@@ -58,7 +58,7 @@ def main():
print
(
"Error: The second argument must be a filename"
)
sys
.
exit
(
4
)
injector
=
CodeInjector
(
pid
,
verbose
=
args
.
verbose
,
injector
=
pyrasite
.
CodeInjector
(
pid
,
verbose
=
args
.
verbose
,
gdb_prefix
=
args
.
gdb_prefix
)
injector
.
inject
(
filename
)
...
...
pyrasite/reverse.py
View file @
c4bdfc77
...
...
@@ -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,9 +103,8 @@ class ReversePythonConnection(ReverseConnection):
try
:
exec
(
cmd
)
output
=
buffer
.
getvalue
()
except
Exception
as
e
:
output
=
str
(
e
)
finally
:
except
:
output
=
traceback
.
format_exc
()
sys
.
stdout
=
sys
.
__stdout__
sys
.
stderr
=
sys
.
__stderr__
buffer
.
close
()
...
...
tests/test_code_injection.py
View file @
c4bdfc77
...
...
@@ -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
()
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