PBinCLI/pbincli/actions.py

129 lines
5.1 KiB
Python
Raw Normal View History

2017-02-18 21:00:40 +03:00
"""Action functions for argparser"""
2017-02-21 00:14:56 +03:00
import json, os, ntpath, sys
2017-02-21 03:34:34 +03:00
import pbincli.actions, pbincli.sjcl_simple
2017-02-20 20:56:37 +03:00
from base64 import b64encode, b64decode
2017-02-19 09:40:04 +03:00
from Crypto.Hash import SHA256
2017-02-21 03:34:34 +03:00
from mimetypes import guess_type
2017-02-19 13:26:22 +03:00
from pbincli.transports import privatebin
2017-02-21 03:34:34 +03:00
from pbincli.utils import PBinCLIException, check_readable, check_writable, json_load_byteified
2017-02-20 20:56:37 +03:00
from zlib import compress, decompress
2017-02-18 21:00:40 +03:00
2017-02-20 15:13:42 +03:00
2017-02-21 00:14:56 +03:00
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
2017-02-20 20:56:37 +03:00
2017-02-20 15:13:42 +03:00
2017-02-21 00:14:56 +03:00
def send(args):
2017-02-21 05:09:38 +03:00
if args.comment:
text = args.comment
elif args.file:
text = "Sending file to you!"
else:
print("Nothing to send!")
sys.exit(1)
2017-02-21 01:27:32 +03:00
passphrase = b64encode(os.urandom(32))
2017-02-21 00:14:56 +03:00
if args.debug: print("Passphrase:\t{}".format(b64encode(passphrase)))
2017-02-19 20:15:26 +03:00
if args.password:
2017-02-19 09:40:04 +03:00
p = SHA256.new()
p.update(args.password.encode("UTF-8"))
2017-02-21 01:27:32 +03:00
password = passphrase + p.hexdigest().encode("UTF-8")
2017-02-20 15:13:42 +03:00
else:
2017-02-21 01:27:32 +03:00
password = passphrase
if args.debug: print("Password:\t{}".format(password))
2017-02-19 23:27:37 +03:00
2017-02-21 00:14:56 +03:00
if args.file:
check_readable(args.file)
with open(args.file, "rb") as f:
contents = f.read()
f.close()
2017-02-21 03:34:34 +03:00
mime = guess_type(args.file)
if args.debug: print("Filename:\t{}\nMIME-type:\t{}".format(path_leaf(args.file), mime[0]))
2017-02-21 00:14:56 +03:00
2017-02-21 03:34:34 +03:00
file = "data:" + mime[0] + ";base64," + b64encode(contents)
filename = path_leaf(args.file)
2017-02-21 00:14:56 +03:00
2017-02-21 01:27:32 +03:00
cipherfile = pbincli.sjcl_simple.encrypt(password, file)
cipherfilename = pbincli.sjcl_simple.encrypt(password, filename)
2017-02-21 00:14:56 +03:00
2017-02-20 15:13:42 +03:00
"""Sending text from 'data' string"""
2017-02-21 01:27:32 +03:00
cipher = pbincli.sjcl_simple.encrypt(password, text)
2017-02-20 20:56:37 +03:00
request = {'data':json.dumps(cipher, ensure_ascii=False).replace(' ',''),'expire':args.expire,'formatter':args.format,'burnafterreading':int(args.burn),'opendiscussion':int(args.discus)}
2017-02-21 03:34:34 +03:00
if cipherfile and cipherfilename:
request['attachment'] = json.dumps(cipherfile, ensure_ascii=False).replace(' ','')
request['attachmentname'] = json.dumps(cipherfilename, ensure_ascii=False).replace(' ','')
2017-02-21 00:14:56 +03:00
2017-02-20 15:13:42 +03:00
if args.debug: print("Request:\t{}".format(request))
result, server = privatebin().post(request)
if args.debug: print("Response:\t{}\n".format(result.decode("UTF-8")))
result = json.loads(result)
"""Standart response: {"status":0,"id":"aaabbb","url":"\/?aaabbb","deletetoken":"aaabbbccc"}"""
if result['status'] == 0:
2017-02-21 01:27:32 +03:00
print("Paste uploaded!\nPasteID:\t{}\nPassword:\t{}\nDelete token:\t{}\n\nLink:\t{}?{}#{}".format(result['id'], passphrase, result['deletetoken'], server, result['id'], passphrase))
2017-02-20 15:13:42 +03:00
else:
2017-02-20 20:56:37 +03:00
print("Something went wrong...\nError:\t{}".format(result['message']))
2017-02-20 15:13:42 +03:00
sys.exit(1)
def get(args):
paste = args.pasteinfo.split("#")
if paste[0] and paste[1]:
2017-02-21 01:27:32 +03:00
if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(paste[0], paste[1]))
if args.password:
p = SHA256.new()
p.update(args.password.encode("UTF-8"))
passphrase = paste[1] + p.hexdigest().encode("UTF-8")
else:
passphrase = paste[1]
if args.debug: print("Password:\t{}".format(passphrase))
result = privatebin().get(paste[0])
2017-02-20 15:13:42 +03:00
else:
print("PBinCLI error: Incorrect request")
sys.exit(1)
if args.debug: print("Response:\t{}\n".format(result.decode("UTF-8")))
2017-02-20 20:56:37 +03:00
2017-02-20 15:13:42 +03:00
result = json.loads(result)
if result['status'] == 0:
2017-02-21 00:14:56 +03:00
print("Paste received! Text inside:")
data = pbincli.utils.json_loads_byteified(result['data'])
2017-02-21 03:34:34 +03:00
if args.debug: print("Text:\t{}".format(data))
2017-02-21 01:27:32 +03:00
text = pbincli.sjcl_simple.decrypt(passphrase, data)
2017-02-20 18:10:19 -05:00
print(text)
2017-02-21 00:14:56 +03:00
2017-02-21 01:27:32 +03:00
check_writable("paste.txt")
with open("paste.txt", "wb") as f:
2017-02-20 18:10:19 -05:00
f.write(text)
2017-02-21 01:27:32 +03:00
f.close
2017-02-21 00:14:56 +03:00
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'])
2017-02-21 03:34:34 +03:00
if args.debug: print("Name:\t{}\nData:\t{}".format(cipherfilename, cipherfile))
attachmentf = pbincli.sjcl_simple.decrypt(passphrase, cipherfile)
2017-02-21 01:27:32 +03:00
attachmentname = pbincli.sjcl_simple.decrypt(passphrase, cipherfilename)
2017-02-21 03:34:34 +03:00
attachment = str(attachmentf.split(',', 1)[1:])
file = b64decode(attachment)
filename = attachmentname
2017-02-21 00:14:56 +03:00
if args.debug: print("Filename:\t{}\n".format(filename))
check_writable(filename)
with open(filename, "wb") as f:
f.write(file)
f.close
2017-02-20 15:13:42 +03:00
if 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
2017-02-21 01:27:32 +03:00
print("Burn afrer reading flag found. Deleting paste...")
2017-02-20 20:56:37 +03:00
result = privatebin().delete(paste[0], 'burnafterreading')
if args.debug: print("Delete response:\t{}\n".format(result.decode("UTF-8")))
2017-02-21 01:27:32 +03:00
2017-02-20 15:13:42 +03:00
else:
2017-02-20 20:56:37 +03:00
print("Something went wrong...\nError:\t{}".format(result['message']))
2017-02-20 15:13:42 +03:00
sys.exit(1)