From 6891f1b240e1fa90df8d76810bffd81a83f65e4f Mon Sep 17 00:00:00 2001 From: r4sas Date: Tue, 21 Feb 2017 00:14:56 +0300 Subject: [PATCH] fully work with files --- pbincli.py | 16 +++++------ pbincli/actions.py | 69 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/pbincli.py b/pbincli.py index a63f889..9f9c624 100755 --- a/pbincli.py +++ b/pbincli.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python3 +#! /usr/bin/env python2.7 import os import sys import argparse @@ -15,21 +15,21 @@ def main(): %(prog)s --burn --discus --expire 1day --format plaintext \\ --comment "My file" --password mypass image.txt""" ) - send_parser.add_argument("-b", "--burn", default=False, action="store_true", help="burn sent paste after reading") + send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="burn sent paste after reading") send_parser.add_argument("-c", "--comment", help="comment in quotes") - send_parser.add_argument("-d", "--discus", default=False, action="store_true", help="open discussion of sent paste") - send_parser.add_argument("-e", "--expire", default="1day", action="store", help="expiration of paste (default: 1day)") - send_parser.add_argument("-f", "--format", default="plaintext", action="store", help="format of paste (default: plaintext)") + send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="open discussion of sent paste") + send_parser.add_argument("-E", "--expire", default="1day", action="store", help="expiration of paste (default: 1day)") + send_parser.add_argument("-F", "--format", default="plaintext", action="store", choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of paste (default: plaintext)") send_parser.add_argument("-p", "--password", help="password for crypting paste") - send_parser.add_argument("-D", "--debug", default=False, action="store_true", help="enable debug") - #send_parser.add_argument("filename", help="example: image.jpg") + send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") + send_parser.add_argument("-f", "--file", help="example: image.jpg or full path to file") send_parser.set_defaults(func=pbincli.actions.send) get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance", usage=""" %(prog)s pasteid#password""" ) get_parser.add_argument("pasteinfo", help="example: aabb#cccddd") - get_parser.add_argument("-D", "--debug", default=False, action="store_true", help="enable debug") + get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") get_parser.set_defaults(func=pbincli.actions.get) # parse arguments diff --git a/pbincli/actions.py b/pbincli/actions.py index c429f31..05df244 100644 --- a/pbincli/actions.py +++ b/pbincli/actions.py @@ -1,31 +1,23 @@ """Action functions for argparser""" -import json -#import mimetypes -import os +import json, os, ntpath, sys import pbincli.actions -import sys #import pbincli.sjcl_gcm import pbincli.sjcl_simple -import pbincli.utils +from pbincli.utils import PBinCLIException, check_readable, check_writable, json_load_byteified from base64 import b64encode, b64decode from Crypto.Hash import SHA256 from pbincli.transports import privatebin from zlib import compress, decompress -def send(args): - if args.comment: - text = args.comment - else: - text = "Test!" +def path_leaf(path): + head, tail = ntpath.split(path) + return tail or ntpath.basename(head) - #check_readable(args.filename) - #with open(args.filename, "rb") as f: - # contents = f.read() - #file = b64encode(compress(contents)) +def send(args): passphrase = os.urandom(32) - if args.debug: print("Passphrase: {}".format(passphrase)) + if args.debug: print("Passphrase:\t{}".format(b64encode(passphrase))) if args.password: p = SHA256.new() p.update(args.password.encode("UTF-8")) @@ -34,10 +26,32 @@ def send(args): passphrase = b64encode(passphrase) if args.debug: print("Password:\t{}".format(passphrase)) + if args.comment: + text = b64encode(compress(args.comment)) + else: + text = b64encode(compress("Sending file to you!")) + + if args.file: + check_readable(args.file) + with open(args.file, "rb") as f: + contents = f.read() + f.close() + + if args.debug: print("Filename:\t{}".format(path_leaf(args.file))) + file = b64encode(compress(contents)) + filename = b64encode(compress(path_leaf(args.file))) + + cipherfile = pbincli.sjcl_simple.encrypt(passphrase, file) + cipherfilename = pbincli.sjcl_simple.encrypt(passphrase, filename) + """Sending text from 'data' string""" #cipher = SJCL().encrypt(b64encode(text), passphrase) cipher = pbincli.sjcl_simple.encrypt(passphrase, text) request = {'data':json.dumps(cipher, ensure_ascii=False).replace(' ',''),'expire':args.expire,'formatter':args.format,'burnafterreading':int(args.burn),'opendiscussion':int(args.discus)} + if cipherfile and cipherfilename: + request['attachment'] = json.dumps(cipherfile, ensure_ascii=False).replace(' ','') + request['attachmentname'] = json.dumps(cipherfilename, ensure_ascii=False).replace(' ','') + if args.debug: print("Request:\t{}".format(request)) result, server = privatebin().post(request) @@ -63,11 +77,26 @@ def get(args): result = json.loads(result) if result['status'] == 0: - print("Paste received!\n") - text = pbincli.utils.json_loads_byteified(result['data']) - out = pbincli.sjcl_simple.decrypt(paste[1], text) - #out = pbincli.sjcl_gcm.SJCL().decrypt(text, paste[1]) - print(out) + print("Paste received! Text inside:") + data = pbincli.utils.json_loads_byteified(result['data']) + text = pbincli.sjcl_simple.decrypt(paste[1], data) + #text = pbincli.sjcl_gcm.SJCL().decrypt(daat, paste[1]) + print(decompress(b64decode(text))) + + if 'attachment' in result and 'attachmentname' in result: + print("Found file, attached to paste. Decoding it and saving") + cipherfile = pbincli.utils.json_loads_byteified(result['attachment']) + cipherfilename = pbincli.utils.json_loads_byteified(result['attachmentname']) + attachment = pbincli.sjcl_simple.decrypt(paste[1], cipherfile) + attachmentname = pbincli.sjcl_simple.decrypt(paste[1], cipherfilename) + file = decompress(b64decode(attachment)) + filename = decompress(b64decode(attachmentname)) + if args.debug: 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']: result = privatebin().delete(paste[0], 'burnafterreading')