1
1
mirror of https://github.com/r4sas/PBinCLI synced 2025-08-31 16:12:02 +00:00

fully work with files

This commit is contained in:
R4SAS 2017-02-21 00:14:56 +03:00
parent e986df6413
commit 6891f1b240
2 changed files with 58 additions and 29 deletions

View File

@ -1,4 +1,4 @@
#! /usr/bin/env python3 #! /usr/bin/env python2.7
import os import os
import sys import sys
import argparse import argparse
@ -15,21 +15,21 @@ def main():
%(prog)s --burn --discus --expire 1day --format plaintext \\ %(prog)s --burn --discus --expire 1day --format plaintext \\
--comment "My file" --password mypass image.txt""" --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("-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("-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("-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("-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("-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("-d", "--debug", default=False, action="store_true", help="enable debug")
#send_parser.add_argument("filename", help="example: image.jpg") send_parser.add_argument("-f", "--file", help="example: image.jpg or full path to file")
send_parser.set_defaults(func=pbincli.actions.send) send_parser.set_defaults(func=pbincli.actions.send)
get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance", usage=""" get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance", usage="""
%(prog)s pasteid#password""" %(prog)s pasteid#password"""
) )
get_parser.add_argument("pasteinfo", help="example: aabb#cccddd") 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) get_parser.set_defaults(func=pbincli.actions.get)
# parse arguments # parse arguments

View File

@ -1,31 +1,23 @@
"""Action functions for argparser""" """Action functions for argparser"""
import json import json, os, ntpath, sys
#import mimetypes
import os
import pbincli.actions import pbincli.actions
import sys
#import pbincli.sjcl_gcm #import pbincli.sjcl_gcm
import pbincli.sjcl_simple 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 base64 import b64encode, b64decode
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
from pbincli.transports import privatebin from pbincli.transports import privatebin
from zlib import compress, decompress from zlib import compress, decompress
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def send(args): def send(args):
if args.comment:
text = args.comment
else:
text = "Test!"
#check_readable(args.filename)
#with open(args.filename, "rb") as f:
# contents = f.read()
#file = b64encode(compress(contents))
passphrase = os.urandom(32) passphrase = os.urandom(32)
if args.debug: print("Passphrase: {}".format(passphrase)) if args.debug: print("Passphrase:\t{}".format(b64encode(passphrase)))
if args.password: if args.password:
p = SHA256.new() p = SHA256.new()
p.update(args.password.encode("UTF-8")) p.update(args.password.encode("UTF-8"))
@ -34,10 +26,32 @@ def send(args):
passphrase = b64encode(passphrase) passphrase = b64encode(passphrase)
if args.debug: print("Password:\t{}".format(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""" """Sending text from 'data' string"""
#cipher = SJCL().encrypt(b64encode(text), passphrase) #cipher = SJCL().encrypt(b64encode(text), passphrase)
cipher = pbincli.sjcl_simple.encrypt(passphrase, text) 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)} 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)) if args.debug: print("Request:\t{}".format(request))
result, server = privatebin().post(request) result, server = privatebin().post(request)
@ -63,11 +77,26 @@ def get(args):
result = json.loads(result) result = json.loads(result)
if result['status'] == 0: if result['status'] == 0:
print("Paste received!\n") print("Paste received! Text inside:")
text = pbincli.utils.json_loads_byteified(result['data']) data = pbincli.utils.json_loads_byteified(result['data'])
out = pbincli.sjcl_simple.decrypt(paste[1], text) text = pbincli.sjcl_simple.decrypt(paste[1], data)
#out = pbincli.sjcl_gcm.SJCL().decrypt(text, paste[1]) #text = pbincli.sjcl_gcm.SJCL().decrypt(daat, paste[1])
print(out) 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']: if 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
result = privatebin().delete(paste[0], 'burnafterreading') result = privatebin().delete(paste[0], 'burnafterreading')