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
Hide 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'
__version__
=
'2.0beta6'
__all__
=
(
'CodeInjector'
,
'ObjectInspector'
,
'PyrasiteIPC'
,
__all__
=
(
'CodeInjector'
,
'ObjectInspector'
,
'PyrasiteIPC'
,
'ReverseConnection'
,
'ReversePythonConnection'
)
'ReverseConnection'
,
'ReversePythonConnection'
)
...
@@ -21,3 +16,8 @@ You should have received a copy of the GNU General Public License
...
@@ -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/>.
\
along with pyrasite. If not, see <http://www.gnu.org/licenses/>.
\
"""
"""
__copyright__
=
"Copyright (C) 2011, 2012 Red Hat, Inc."
__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):
...
@@ -52,11 +52,13 @@ class CodeInjector(object):
self
.
filename
=
os
.
path
.
abspath
(
filename
)
self
.
filename
=
os
.
path
.
abspath
(
filename
)
gdb_cmds
=
[
gdb_cmds
=
[
'PyGILState_Ensure()'
,
'PyGILState_Ensure()'
,
# Allow payloads to import modules alongside them
'PyRun_SimpleString("'
'PyRun_SimpleString("import sys; sys.path.insert(0,
\
\
"%s
\
\
");")'
%
'import sys; sys.path.insert(0,
\
\
"%s
\
\
"); '
os
.
path
.
dirname
(
self
.
filename
),
'sys.path.insert(0,
\
\
"%s
\
\
"); '
'PyRun_SimpleString("exec(open(
\
\
"%s
\
\
").read())")'
%
'exec(open(
\
\
"%s
\
\
").read())")'
%
self
.
filename
,
(
os
.
path
.
dirname
(
self
.
filename
),
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'..'
)),
self
.
filename
),
'PyGILState_Release($1)'
,
'PyGILState_Release($1)'
,
]
]
p
=
subprocess
.
Popen
(
'%sgdb -p %d -batch %s'
%
(
self
.
gdb_prefix
,
self
.
pid
,
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):
...
@@ -139,7 +139,7 @@ class PyrasiteIPC(object):
def
send
(
self
,
data
):
def
send
(
self
,
data
):
"""Send arbitrary data to the process via self.sock"""
"""Send arbitrary data to the process via self.sock"""
header
=
b''
header
=
''
.
encode
(
'utf-8'
)
data
=
data
.
encode
(
'utf-8'
)
data
=
data
.
encode
(
'utf-8'
)
if
self
.
reliable
:
if
self
.
reliable
:
header
=
struct
.
pack
(
'<L'
,
len
(
data
))
header
=
struct
.
pack
(
'<L'
,
len
(
data
))
...
@@ -159,7 +159,7 @@ class PyrasiteIPC(object):
...
@@ -159,7 +159,7 @@ class PyrasiteIPC(object):
def
recv_bytes
(
self
,
n
):
def
recv_bytes
(
self
,
n
):
"""Receive n bytes from a socket"""
"""Receive n bytes from a socket"""
data
=
b''
data
=
''
.
encode
(
'utf-8'
)
while
len
(
data
)
<
n
:
while
len
(
data
)
<
n
:
chunk
=
self
.
sock
.
recv
(
n
-
len
(
data
))
chunk
=
self
.
sock
.
recv
(
n
-
len
(
data
))
if
not
chunk
:
if
not
chunk
:
...
...
pyrasite/main.py
View file @
c4bdfc77
...
@@ -19,7 +19,7 @@ import os
...
@@ -19,7 +19,7 @@ import os
import
sys
import
sys
import
argparse
import
argparse
from
inject
import
CodeInjector
import
pyrasite
def
main
():
def
main
():
...
@@ -58,8 +58,8 @@ def main():
...
@@ -58,8 +58,8 @@ def main():
print
(
"Error: The second argument must be a filename"
)
print
(
"Error: The second argument must be a filename"
)
sys
.
exit
(
4
)
sys
.
exit
(
4
)
injector
=
CodeInjector
(
pid
,
verbose
=
args
.
verbose
,
injector
=
pyrasite
.
CodeInjector
(
pid
,
verbose
=
args
.
verbose
,
gdb_prefix
=
args
.
gdb_prefix
)
gdb_prefix
=
args
.
gdb_prefix
)
injector
.
inject
(
filename
)
injector
.
inject
(
filename
)
...
...
pyrasite/reverse.py
View file @
c4bdfc77
...
@@ -21,6 +21,7 @@
...
@@ -21,6 +21,7 @@
import
sys
import
sys
import
socket
import
socket
import
traceback
import
threading
import
threading
if
sys
.
version_info
[
0
]
==
3
:
if
sys
.
version_info
[
0
]
==
3
:
...
@@ -82,8 +83,8 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
...
@@ -82,8 +83,8 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
else
:
else
:
running
=
self
.
on_command
(
cmd
)
running
=
self
.
on_command
(
cmd
)
except
Exception
as
e
:
except
:
print
(
str
(
e
)
)
traceback
.
print_exc
(
)
running
=
False
running
=
False
if
not
running
:
if
not
running
:
self
.
close
()
self
.
close
()
...
@@ -102,11 +103,10 @@ class ReversePythonConnection(ReverseConnection):
...
@@ -102,11 +103,10 @@ class ReversePythonConnection(ReverseConnection):
try
:
try
:
exec
(
cmd
)
exec
(
cmd
)
output
=
buffer
.
getvalue
()
output
=
buffer
.
getvalue
()
except
Exception
as
e
:
except
:
output
=
str
(
e
)
output
=
traceback
.
format_exc
()
finally
:
sys
.
stdout
=
sys
.
__stdout__
sys
.
stdout
=
sys
.
__stdout__
sys
.
stderr
=
sys
.
__stderr__
sys
.
stderr
=
sys
.
__stderr__
buffer
.
close
()
buffer
.
close
()
self
.
send
(
output
)
self
.
send
(
output
)
return
True
return
True
tests/test_code_injection.py
View file @
c4bdfc77
...
@@ -20,6 +20,7 @@ import subprocess
...
@@ -20,6 +20,7 @@ import subprocess
from
pyrasite.inject
import
CodeInjector
from
pyrasite.inject
import
CodeInjector
class
TestCodeInjection
(
unittest
.
TestCase
):
class
TestCodeInjection
(
unittest
.
TestCase
):
def
test_injection
(
self
):
def
test_injection
(
self
):
...
@@ -31,7 +32,8 @@ class TestCodeInjection(unittest.TestCase):
...
@@ -31,7 +32,8 @@ class TestCodeInjection(unittest.TestCase):
ci
.
inject
(
'pyrasite/payloads/helloworld.py'
)
ci
.
inject
(
'pyrasite/payloads/helloworld.py'
)
stdout
,
stderr
=
p
.
communicate
()
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
):
def
test_multithreaded_injection
(
self
):
cmd
=
[
cmd
=
[
...
@@ -46,7 +48,9 @@ class TestCodeInjection(unittest.TestCase):
...
@@ -46,7 +48,9 @@ class TestCodeInjection(unittest.TestCase):
ci
.
inject
(
'pyrasite/payloads/helloworld.py'
)
ci
.
inject
(
'pyrasite/payloads/helloworld.py'
)
stdout
,
stderr
=
p
.
communicate
()
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__'
:
if
__name__
==
'__main__'
:
unittest
.
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