Create mega.py
Browse files- tools/mega.py +122 -0
tools/mega.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
import codecs
|
| 5 |
+
import random
|
| 6 |
+
import base64
|
| 7 |
+
import struct
|
| 8 |
+
import shutil
|
| 9 |
+
import requests
|
| 10 |
+
import tempfile
|
| 11 |
+
|
| 12 |
+
from Crypto.Cipher import AES
|
| 13 |
+
from Crypto.Util import Counter
|
| 14 |
+
|
| 15 |
+
def makebyte(x):
|
| 16 |
+
return codecs.latin_1_encode(x)[0]
|
| 17 |
+
|
| 18 |
+
def a32_to_str(a):
|
| 19 |
+
return struct.pack('>%dI' % len(a), *a)
|
| 20 |
+
|
| 21 |
+
def get_chunks(size):
|
| 22 |
+
p, s = 0, 0x20000
|
| 23 |
+
|
| 24 |
+
while p + s < size:
|
| 25 |
+
yield(p, s)
|
| 26 |
+
p += s
|
| 27 |
+
|
| 28 |
+
if s < 0x100000: s += 0x20000
|
| 29 |
+
|
| 30 |
+
yield(p, size - p)
|
| 31 |
+
|
| 32 |
+
def aes_cbc_decrypt(data, key):
|
| 33 |
+
aes_cipher = AES.new(key, AES.MODE_CBC, makebyte('\0' * 16))
|
| 34 |
+
return aes_cipher.decrypt(data)
|
| 35 |
+
|
| 36 |
+
def decrypt_attr(attr, key):
|
| 37 |
+
attr = codecs.latin_1_decode(aes_cbc_decrypt(attr, a32_to_str(key)))[0].rstrip('\0')
|
| 38 |
+
return json.loads(attr[4:]) if attr[:6] == 'MEGA{"' else False
|
| 39 |
+
|
| 40 |
+
def _api_request(data):
|
| 41 |
+
sequence_num = random.randint(0, 0xFFFFFFFF)
|
| 42 |
+
params = {'id': sequence_num}
|
| 43 |
+
sequence_num += 1
|
| 44 |
+
|
| 45 |
+
if not isinstance(data, list): data = [data]
|
| 46 |
+
json_resp = json.loads(requests.post('{0}://g.api.{1}/cs'.format('https', 'mega.co.nz'), params=params, data=json.dumps(data), timeout=160).text)
|
| 47 |
+
if isinstance(json_resp, int): raise Exception(json_resp)
|
| 48 |
+
|
| 49 |
+
return json_resp[0]
|
| 50 |
+
|
| 51 |
+
def base64_url_decode(data):
|
| 52 |
+
data += '=='[(2 - len(data) * 3) % 4:]
|
| 53 |
+
|
| 54 |
+
for search, replace in (('-', '+'), ('_', '/'), (',', '')):
|
| 55 |
+
data = data.replace(search, replace)
|
| 56 |
+
|
| 57 |
+
return base64.b64decode(data)
|
| 58 |
+
|
| 59 |
+
def str_to_a32(b):
|
| 60 |
+
if isinstance(b, str): b = makebyte(b)
|
| 61 |
+
if len(b) % 4: b += b'\0' * (4 - len(b) % 4)
|
| 62 |
+
return struct.unpack('>%dI' % (len(b) / 4), b)
|
| 63 |
+
|
| 64 |
+
def base64_to_a32(s):
|
| 65 |
+
return str_to_a32(base64_url_decode(s))
|
| 66 |
+
|
| 67 |
+
def mega_download_file(file_handle, file_key, dest_path=None):
|
| 68 |
+
file_key = base64_to_a32(file_key)
|
| 69 |
+
file_data = _api_request({'a': 'g', 'g': 1, 'p': file_handle})
|
| 70 |
+
|
| 71 |
+
k = (file_key[0] ^ file_key[4], file_key[1] ^ file_key[5], file_key[2] ^ file_key[6], file_key[3] ^ file_key[7])
|
| 72 |
+
iv = file_key[4:6] + (0, 0)
|
| 73 |
+
|
| 74 |
+
if 'g' not in file_data: raise Exception
|
| 75 |
+
|
| 76 |
+
file_size = file_data['s']
|
| 77 |
+
attribs = decrypt_attr(base64_url_decode(file_data['at']), k)
|
| 78 |
+
input_file = requests.get(file_data['g'], stream=True).raw
|
| 79 |
+
|
| 80 |
+
temp_output_file = tempfile.NamedTemporaryFile(mode='w+b', prefix='megapy_', delete=False)
|
| 81 |
+
k_str = a32_to_str(k)
|
| 82 |
+
aes = AES.new(k_str, AES.MODE_CTR, counter=Counter.new(128, initial_value=((iv[0] << 32) + iv[1]) << 64))
|
| 83 |
+
|
| 84 |
+
mac_str = b'\0' * 16
|
| 85 |
+
mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str)
|
| 86 |
+
iv_str = a32_to_str([iv[0], iv[1], iv[0], iv[1]])
|
| 87 |
+
|
| 88 |
+
for _, chunk_size in get_chunks(file_size):
|
| 89 |
+
chunk = aes.decrypt(input_file.read(chunk_size))
|
| 90 |
+
temp_output_file.write(chunk)
|
| 91 |
+
|
| 92 |
+
encryptor = AES.new(k_str, AES.MODE_CBC, iv_str)
|
| 93 |
+
|
| 94 |
+
for i in range(0, len(chunk) - 16, 16):
|
| 95 |
+
block = chunk[i:i + 16]
|
| 96 |
+
encryptor.encrypt(block)
|
| 97 |
+
|
| 98 |
+
i = (i + 16) if file_size > 16 else 0
|
| 99 |
+
block = chunk[i:i + 16]
|
| 100 |
+
if len(block) % 16: block += b'\0' * (16 - (len(block) % 16))
|
| 101 |
+
|
| 102 |
+
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
|
| 103 |
+
|
| 104 |
+
file_mac = str_to_a32(mac_str)
|
| 105 |
+
temp_output_file.close()
|
| 106 |
+
|
| 107 |
+
if (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]) != file_key[6:8]: raise ValueError
|
| 108 |
+
|
| 109 |
+
file_path = os.path.join(dest_path, attribs['n'])
|
| 110 |
+
if os.path.exists(file_path): os.remove(file_path)
|
| 111 |
+
|
| 112 |
+
shutil.move(temp_output_file.name, file_path)
|
| 113 |
+
|
| 114 |
+
def mega_download_url(url, dest_path=None):
|
| 115 |
+
if '/file/' in url:
|
| 116 |
+
url = url.replace(' ', '')
|
| 117 |
+
file_id = re.findall(r'\W\w\w\w\w\w\w\w\w\W', url)[0][1:-1]
|
| 118 |
+
path = f'{file_id}!{url[re.search(file_id, url).end() + 1:]}'.split('!')
|
| 119 |
+
elif '!' in url: path = re.findall(r'/#!(.*)', url)[0].split('!')
|
| 120 |
+
else: raise Exception
|
| 121 |
+
|
| 122 |
+
return mega_download_file(path[0], path[1], dest_path)
|