1
1
mirror of https://github.com/r4sas/PBinCLI synced 2025-01-09 06:18:00 +00:00
PBinCLI/pbincli/actions.py

163 lines
5.1 KiB
Python
Raw Normal View History

2019-06-02 14:04:38 +00:00
from pbincli.format import Paste
from pbincli.utils import PBinCLIError
2018-04-04 02:34:05 +00:00
def send(args, api_client, settings=None):
from pbincli.api import Shortener
if args.short:
shortener = Shortener(settings)
if not args.notext:
if args.text:
text = args.text
elif args.stdin:
text = args.stdin.read()
elif not args.file:
PBinCLIError("Nothing to send!")
else:
text = ""
2017-02-21 02:09:38 +00:00
2019-06-02 14:04:38 +00:00
paste = Paste(args.debug)
# get from server supported paste format version and update object
version = api_client.getVersion()
paste.setVersion(version)
# set compression type, works only on v2 pastes
if version == 2:
paste.setCompression(args.compression)
# add text in paste (if it provided)
2019-06-02 14:04:38 +00:00
paste.setText(text)
2018-02-12 14:28:18 +00:00
# If we set PASSWORD variable
2017-02-19 17:15:26 +00:00
if args.password:
2019-06-02 14:04:38 +00:00
paste.setPassword(args.password)
2018-02-12 14:28:18 +00:00
# If we set FILE variable
2017-02-20 21:14:56 +00:00
if args.file:
2019-06-02 14:04:38 +00:00
paste.setAttachment(args.file)
2019-06-02 14:04:38 +00:00
paste.encrypt(
formatter = args.format,
burnafterreading = args.burn,
discussion = args.discus,
expiration = args.expire)
2017-02-20 21:14:56 +00:00
2019-06-02 14:04:38 +00:00
request = paste.getJSON()
2017-02-20 21:14:56 +00:00
if args.debug:
print("Passphrase:\t{}".format(paste.getHash()))
print("Request:\t{}".format(request))
2017-02-20 12:13:42 +00:00
2018-02-12 14:28:18 +00:00
# If we use dry option, exit now
2019-06-02 14:04:38 +00:00
if args.dry: exit(0)
2018-02-12 14:28:18 +00:00
2018-05-23 08:14:35 +00:00
result = api_client.post(request)
if args.debug: print("Response:\t{}\n".format(result))
2019-06-21 11:59:23 +00:00
# Paste was sent. Checking for returned status code
if not result['status']: # return code is zero
2019-06-02 14:04:38 +00:00
passphrase = paste.getHash()
print("Paste uploaded!\nPasteID:\t{}\nPassword:\t{}\nDelete token:\t{}\n\nLink:\t\t{}?{}#{}".format(
result['id'],
passphrase,
result['deletetoken'],
settings['server'],
2019-06-02 14:04:38 +00:00
result['id'],
passphrase))
2019-06-21 11:59:23 +00:00
elif result['status']: # return code is other then zero
PBinCLIError("Something went wrong...\nError:\t\t{}".format(result['message']))
2019-06-21 11:59:23 +00:00
else: # or here no status field in response or it is empty
PBinCLIError("Something went wrong...\nError: Empty response.")
2017-02-20 12:13:42 +00:00
if args.short:
print("\nQuerying URL shortening service...")
shortener.getlink("{}?{}#{}".format(
settings['server'],
result['id'],
passphrase))
2017-02-20 12:13:42 +00:00
def get(args, api_client, settings=None):
2019-06-21 14:35:45 +00:00
from pbincli.utils import check_writable, json_encode
2019-06-02 14:04:38 +00:00
try:
pasteid, passphrase = args.pasteinfo.split("#")
except ValueError:
PBinCLIError("Provided info hasn't contain valid PasteID#Passphrase string")
2019-06-02 14:04:38 +00:00
if not (pasteid and passphrase):
PBinCLIError("Incorrect request")
2019-06-02 14:04:38 +00:00
if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase))
2017-02-20 22:27:32 +00:00
paste = Paste(args.debug)
2017-02-20 22:27:32 +00:00
2019-06-02 14:04:38 +00:00
if args.password:
paste.setPassword(args.password)
2019-06-21 14:35:45 +00:00
if args.debug: print("Password:\t{}".format(args.password))
2017-02-20 22:27:32 +00:00
2019-06-02 14:04:38 +00:00
result = api_client.get(pasteid)
if args.debug: print("Response:\t{}\n".format(result))
2017-02-20 17:56:37 +00:00
2019-06-21 11:59:23 +00:00
# Paste was received. Checking received status code
if not result['status']: # return code is zero
2019-06-02 14:04:38 +00:00
print("Paste received!")
2019-06-02 14:04:38 +00:00
version = result['v'] if 'v' in result else 1
paste.setVersion(version)
2019-06-02 14:04:38 +00:00
if version == 2:
if args.debug: print("Authentication data:\t{}".format(result['adata']))
2019-06-02 14:04:38 +00:00
paste.setHash(passphrase)
paste.loadJSON(result)
paste.decrypt()
text = paste.getText()
2017-02-20 21:14:56 +00:00
if args.debug: print("Decoded text size: {}\n".format(len(text)))
if len(text):
2019-06-02 14:04:38 +00:00
if args.debug: print("{}\n".format(text.decode()))
filename = "paste-" + pasteid + ".txt"
print("Found text in paste. Saving it to {}".format(filename))
2019-06-02 14:04:38 +00:00
check_writable(filename)
with open(filename, "wb") as f:
f.write(text)
2019-06-21 13:12:17 +00:00
f.close()
2017-02-20 22:27:32 +00:00
2019-06-02 14:04:38 +00:00
attachment, attachment_name = paste.getAttachment()
2019-06-02 14:04:38 +00:00
if attachment:
print("Found file, attached to paste. Saving it to {}\n".format(attachment_name))
2019-06-02 14:04:38 +00:00
check_writable(attachment_name)
with open(attachment_name, "wb") as f:
f.write(attachment)
2019-06-21 13:12:17 +00:00
f.close()
2017-02-20 12:13:42 +00:00
2019-06-02 14:04:38 +00:00
if version == 1 and 'meta' in result and 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
2017-02-20 22:27:32 +00:00
print("Burn afrer reading flag found. Deleting paste...")
2019-06-19 10:58:41 +00:00
api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':'burnafterreading'}))
2019-06-21 11:59:23 +00:00
elif result['status']: # return code is other then zero
PBinCLIError("Something went wrong...\nError:\t\t{}".format(result['message']))
2019-06-21 11:59:23 +00:00
else: # or here no status field in response or it is empty
PBinCLIError("Something went wrong...\nError: Empty response.")
def delete(args, api_client, settings=None):
2019-06-02 14:04:38 +00:00
from pbincli.utils import json_encode
pasteid = args.paste
token = args.token
if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token))
2019-06-19 10:58:41 +00:00
api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':token}))