From 45beda8450a89a5360d924a1baf9fa40ddcfe505 Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Fri, 17 Mar 2017 13:27:54 +0200 Subject: [PATCH] Encoded json maybe quite big, so compress it first --- pupy/pp.py | 15 ++++++++++++--- pupy/pupylib/PupyService.py | 9 +++++++-- pupy/pupylib/utils/rpyc_utils.py | 27 ++++++++++++++------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/pupy/pp.py b/pupy/pp.py index 01686c3b..71a49ba3 100755 --- a/pupy/pp.py +++ b/pupy/pp.py @@ -62,6 +62,7 @@ from network.lib.connection import PupyConnection import logging import shlex import marshal +import zlib try: # additional imports needed to package with pyinstaller @@ -95,7 +96,11 @@ def safe_obtain(proxy): if type(proxy) in [list, str, bytes, dict, set, type(None)]: return proxy conn = object.__getattribute__(proxy, "____conn__")() - return json.loads(conn.root.json_dumps(proxy)) # should prevent any code execution + return json.loads( + zlib.decompress( + conn.root.json_dumps(proxy, compressed=True) + ) + ) # should prevent any code execution def obtain(proxy): """ allows to convert netref types into python native types """ @@ -195,8 +200,12 @@ class ReverseSlaveService(Service): """imports an arbitrary module""" return __import__(name, None, None, "*") - def exposed_json_dumps(self, obj): - return json.dumps(obj) + def exposed_json_dumps(self, obj, compressed=False): + data = json.dumps(obj) + if compressed: + data = zlib.compress(data) + + return data def exposed_getconn(self): """returns the local connection instance to the other side""" diff --git a/pupy/pupylib/PupyService.py b/pupy/pupylib/PupyService.py index 9559ddf6..d87655e4 100644 --- a/pupy/pupylib/PupyService.py +++ b/pupy/pupylib/PupyService.py @@ -22,6 +22,7 @@ import ssl import logging import traceback import json +import zlib from pupylib.PupyCredentials import Credentials @@ -96,8 +97,12 @@ class PupyService(rpyc.Service): def exposed_set_modules(self, modules): self.modules=modules - def exposed_json_dumps(self, js): - return json.dumps(js) + def exposed_json_dumps(self, js, compressed=False): + data = json.dumps(js) + if compressed: + data = zlib.compress(data) + + return data class PupyBindService(PupyService): def exposed_get_password(self): diff --git a/pupy/pupylib/utils/rpyc_utils.py b/pupy/pupylib/utils/rpyc_utils.py index 002edcb5..ee44f0e1 100644 --- a/pupy/pupylib/utils/rpyc_utils.py +++ b/pupy/pupylib/utils/rpyc_utils.py @@ -1,16 +1,16 @@ -# -*- coding: UTF8 -*- +# -*- coding: utf-8 -*- # -------------------------------------------------------------- # Copyright (c) 2015, Nicolas VERDIER (contact@n1nj4.eu) # All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: -# +# # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. -# +# # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -# +# # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE # -------------------------------------------------------------- @@ -19,14 +19,16 @@ from contextlib import contextmanager from rpyc.utils.helpers import restricted import textwrap import json - +import zlib def safe_obtain(proxy): """ safe version of rpyc's rpyc.utils.classic.obtain, without using pickle. """ if type(proxy) in [list, str, bytes, dict, set, type(None)]: return proxy conn = object.__getattribute__(proxy, "____conn__")() - return json.loads(conn.root.json_dumps(proxy)) # should prevent any code execution + data = conn.root.json_dumps(proxy, compressed=True) + data = zlib.decompress(data) + return json.loads(data) # should prevent any code execution def obtain(proxy): return safe_obtain(proxy) @@ -65,7 +67,7 @@ def redirected_stdo(conn, stdout=None, stderr=None): def interact(conn): """remote interactive interpreter - + :param conn: the RPyC connection :param namespace: the namespace to use (a ``dict``) """ @@ -80,14 +82,14 @@ def interact(conn): @contextmanager def redirected_stdio(conn): r""" - Redirects the other party's ``stdin``, ``stdout`` and ``stderr`` to + Redirects the other party's ``stdin``, ``stdout`` and ``stderr`` to those of the local party, so remote IO will occur locally. Example usage:: - + with redirected_stdio(conn): conn.modules.sys.stdout.write("hello\n") # will be printed locally - + """ orig_stdin = conn.modules.sys.stdin orig_stdout = conn.modules.sys.stdout @@ -101,4 +103,3 @@ def redirected_stdio(conn): conn.modules.sys.stdin = orig_stdin conn.modules.sys.stdout = orig_stdout conn.modules.sys.stderr = orig_stderr -