示例#1
0
    def _decryptV1(self):
        from sjcl import SJCL
        from json import loads as json_decode

        password = self.__preparePassKey()
        cipher_text = json_decode(self._data['data'])
        if self._debug: print("Text:\t{}\n".format(cipher_text))

        text = SJCL().decrypt(cipher_text, password)

        if len(text):
            if self._debug: print("Decoded Text:\t{}\n".format(text))
            self._text = self.__decompress(text.decode())

        if 'attachment' in self._data and 'attachmentname' in self._data:
            cipherfile = json_decode(self._data['attachment'])
            cipherfilename = json_decode(self._data['attachmentname'])

            if self._debug:
                print("Name:\t{}\nData:\t{}".format(cipherfilename,
                                                    cipherfile))

            attachment = SJCL().decrypt(cipherfile, password)
            attachmentname = SJCL().decrypt(cipherfilename, password)

            self._attachment = self.__decompress(
                attachment.decode('utf-8')).decode('utf-8')
            self._attachment_name = self.__decompress(
                attachmentname.decode('utf-8')).decode('utf-8')
示例#2
0
    def decrypt(self):
        from json import loads as json_decode

        if self._version == 2:
            iv = b64decode(self._data['adata'][0][0])
            salt = b64decode(self._data['adata'][0][1])
            key = self.__deriveKey(salt)

            # Get compression type from received paste
            self._compression = self._data['adata'][0][7]

            cipher = self.__initializeCipher(key, iv, self._data['adata'])
            # Cut the cipher text into message and tag
            cipher_text_tag = b64decode(self._data['ct'])
            cipher_text = cipher_text_tag[:-CIPHER_TAG_BYTES]
            cipher_tag = cipher_text_tag[-CIPHER_TAG_BYTES:]
            cipher_message = json_decode(
                self.__decompress(
                    cipher.decrypt_and_verify(cipher_text,
                                              cipher_tag)).decode())

            self._text = cipher_message['paste'].encode()
            if 'attachment' in cipher_message and 'attachment_name' in cipher_message:
                self._attachment = cipher_message['attachment']
                self._attachment_name = cipher_message['attachment_name']
        else:
            from hashlib import sha256
            from sjcl import SJCL

            if self._password:
                digest = sha256(self._password.encode("UTF-8")).hexdigest()
                password = b64encode(self._key) + digest.encode("UTF-8")
            else:
                password = b64encode(self._key)

            cipher_text = json_decode(self._data['data'])

            if self._debug: print("Text:\t{}\n".format(data))

            text = SJCL().decrypt(cipher_text, password)

            if len(text):
                self._text = self.__decompress(text.decode())

            if 'attachment' in self._data and 'attachmentname' in self._data:
                cipherfile = json_decode(self._data['attachment'])
                cipherfilename = json_decode(self._data['attachmentname'])

                if self._debug:
                    print("Name:\t{}\nData:\t{}".format(
                        cipherfilename, cipherfile))

                attachment = SJCL().decrypt(cipherfile, password)
                attachmentname = SJCL().decrypt(cipherfilename, password)

                self._attachment = self.__decompress(
                    attachment.decode('utf-8')).decode('utf-8')
                self._attachment_name = self.__decompress(
                    attachmentname.decode('utf-8')).decode('utf-8')
示例#3
0
def get(args, api_client):
    pasteid, passphrase = args.pasteinfo.split("#")

    if pasteid and passphrase:
        if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase))

        if args.password:
            digest = hashlib.sha256(args.password.encode("UTF-8")).hexdigest()
            password = passphrase + digest.encode("UTF-8")
        else:
            password = passphrase

        if args.debug: print("Password:\t{}".format(password))

        result = api_client.get(pasteid)
    else:
        print("PBinCLI error: Incorrect request")
        sys.exit(1)

    if args.debug: print("Response:\t{}\n".format(result))

    try:
        result = json.loads(result)
    except ValueError as e:
        print("PBinCLI Error: {}".format(e))
        sys.exit(1)

    if 'status' in result and not result['status']:
        print("Paste received! Text inside:")
        data = json.loads(result['data'])

        if args.debug: print("Text:\t{}\n".format(data))

        text = SJCL().decrypt(data, password)
        print("{}\n".format(decompress(text.decode())))

        check_writable("paste.txt")
        with open("paste.txt", "wb") as f:
            f.write(decompress(text.decode()))
            f.close

        if 'attachment' in result and 'attachmentname' in result:
            print("Found file, attached to paste. Decoding it and saving")

            cipherfile = json.loads(result['attachment']) 
            cipherfilename = json.loads(result['attachmentname'])

            if args.debug: print("Name:\t{}\nData:\t{}".format(cipherfilename, cipherfile))

            attachmentf = SJCL().decrypt(cipherfile, password)
            attachmentname = SJCL().decrypt(cipherfilename, password)

            attachment = decompress(attachmentf.decode('utf-8')).decode('utf-8').split(',', 1)[1]
            file = b64decode(attachment)
            filename = decompress(attachmentname.decode('utf-8')).decode('utf-8')

            print("Filename:\t{}\n".format(filename))

            check_writable(filename)
            with open(filename, "wb") as f:
                f.write(file)
                f.close

        if 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
            print("Burn afrer reading flag found. Deleting paste...")
            result = api_client.delete(pasteid, 'burnafterreading')

            if args.debug: print("Delete response:\t{}\n".format(result))

            try:
                result = json.loads(result)
            except ValueError as e:
                print("PBinCLI Error: {}".format(e))
                sys.exit(1)

            if 'status' in result and not result['status']:
                print("Paste successfully deleted!")
            elif 'status' in result and result['status']:
                print("Something went wrong...\nError:\t\t{}".format(result['message']))
                sys.exit(1)
            else:
                print("Something went wrong...\nError: Empty response.")
                sys.exit(1)

    elif 'status' in result and result['status']:
        print("Something went wrong...\nError:\t\t{}".format(result['message']))
        sys.exit(1)
    else:
        print("Something went wrong...\nError: Empty response.")
        sys.exit(1)
示例#4
0
                #     def __str__(self):
                #         return json.dumps(self)
                # text = mydict(text)
                for key in text:
                    if key == 'salt' or key == 'ct' or key == 'iv':
                        text[key] = text[key].encode('ascii')
                # print(text)
                # text = {"salt": "1sMrwmhD9uo=", "iter": 10000, "ks": 128, "ct": "IY3k1LcEUSQ=", "iv": "JReeuzEISWe1PjLzNCLMXw==", "cipher": "aes", "mode": "ccm", "adata": "", "v": 1, "ts": 64}
                dec = SJCL().decrypt(text, 'shared_secret')
                # dec = dec.decode('ascii')
                # dec = base64.b64decode(dec)
                # dec = base64.decodebytes(dec)
                # dec = ('data:text/plain;base64,') + dec
                dec = base64.b64decode(dec)
                # dec = base64.b64decode('data:image/jpeg;base64,') + dec
                dec = dec.decode('ascii')
                # prepend_info = 'data:image/jpeg;base64'
                # dec = base64.decodebytes(dec)
                #output file
                # print(dec)
                f.write(dec)
                f.close()
                print(green + "done" + end_c)
            except () as e:
                print(red + "Error connecting: {}".format(e) + end_c)

except requests.exceptions.HTTPError as e:
    print(red + 'HTTP error: {}'.format(e) + end_c)
except requests.exceptions.RequestException as e:
    print(red + 'Connection Error: {}'.format(e) + end_c)
    client.close()