-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.py
93 lines (78 loc) · 3.14 KB
/
encoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""encoding.py - Class for encoding/decoding operations.
Calling this Subcipher is rather aspirational :P
Todo:
* Leverage compression for large paylaods (smaz for text,
gzip for binaries for ex.)
"""
import html
import json
from os import unsetenv
import sys
import base64
from pathlib import Path
import mimetypes
from hashlib import blake2b
import re
__author__ = '@sourcefrenchy'
__copyright__ = 'none'
__email__ = 'jmamblat@icloud.com'
__status__ = 'PROTOTYPE'
class Subcipher(object):
"""Encoding/Decoding operations."""
def __init__(self, spot):
"""Constructor."""
self.spotipy = spot
def encode_payload(self, input_file):
"""Encoding text."""
try:
plaintext = Path(input_file).read_bytes()
except OSError as err:
print("[!] Cannot read file: {}".format(err))
sys.exit(0)
h = blake2b(digest_size=20)
h.update(plaintext)
print("[*] checksum plaintext {}".format(h.hexdigest()))
# if mimetypes.guess_type(input_file) == (None, None): # check if text or binary
# print("\n[D] TXT PAYLOAD BEFORE JSON=|{}|\n".format(base64.b64encode(plaintext).decode('utf-8')))
# b64data = base64.b64encode(plaintext)
# b64datadec = b64data.decode('utf-8')
# else:
# print("\n[D] BIN PAYLOAD BEFORE JSON=|{}|\n".format(base64.encodebytes(plaintext).decode('utf-8')))
# b64data = base64.encodebytes(plaintext)
# b64datadec = b64data.decode('utf-8')
b64data = base64.b64encode(plaintext)
b64datadec = b64data.decode('utf-8')
return json.dumps(b64datadec)
def decode_payload(self, payload):
"""Decoding text. Don't forget unescape first."""
def decode_base64(data, altchars=b'+/'):
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
"""
data = re.sub(rb'[^a-zA-Z0-9%s]+' % altchars, b'', data) # normalize
missing_padding = len(data) % 4
if missing_padding:
data += b'='* (4 - missing_padding)
return base64.b64decode(data, altchars)
try:
data = html.unescape(payload)#.replace("\\n", "\n")[:-2]
except Exception:
print("[!] Failure // type(data)={} data=|{}|\nEXCEPTIONS --> {}".format(type(data), data, Exception))
sys.exit(0)
try:
b64datenc = json.loads(data)
except Exception:
print("[D] json.loads fail dataretrieved=|{}|".format(data))
sys.exit(0)
b64data = b64datenc.encode('utf-8')
# try: # check if text or binary
# plaintext = base64.decodebytes(b64data)
plaintext = decode_base64(b64data)
# except:
# plaintext = base64.decodebytes(b64data)
# print("[D] PAYLOAD b64data=|{}|".format(b64data))
h = blake2b(digest_size=20)
h.update(plaintext)
print("[*] checksum payload {}".format(h.hexdigest()))
return plaintext