-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmegadown
executable file
·501 lines (334 loc) · 12 KB
/
megadown
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/bin/bash
VERSION="2.0"
HERE=$(dirname "$0")
SCRIPT=$(readlink -f "$0")
BASH_MIN_VER="3"
MEGA_API_URL="https://g.api.mega.co.nz"
OPENSSL_AES_CTR_128_DEC="openssl enc -d -aes-128-ctr"
OPENSSL_AES_CBC_128_DEC="openssl enc -a -A -d -aes-128-cbc"
OPENSSL_AES_CBC_256_DEC="openssl enc -a -A -d -aes-256-cbc"
OPENSSL_MD5="openssl md5"
if [ ! -d ".megadown" ]; then
mkdir ".megadown"
fi
# 1:message_error
function showError {
echo -e "\n$1\n" 1>&2
exit 1
}
function showHelp {
echo -e "\nmegadown $VERSION - https://github.com/masterofthesith/megadown"
echo -e "\ncli downloader for mega.nz and megacrypter"
echo -e "\nSingle url mode: megadown [OPTION]... 'URL'\n"
echo -e "\tOptions:"
echo -e "\t-o,\t--output FILE_NAME Store file with this name."
echo -e "\t-s,\t--speed SPEED Download speed limit (integer values: 500B, K, 2M)."
echo -e "\t-p,\t--password PASSWORD Password for MegaCrypter links."
echo -e "\t-q,\t--quiet Quiet mode."
echo -e "\t-m,\t--metadata Prints file metadata in JSON format and exits."
echo -e "\n\nMulti url mode: megadown [OPTION]... -l|--list FILE\n"
echo -e "\tOptions:"
echo -e "\t-s,\t--speed SPEED Download speed limit (integer values: 500B, 500K, 2M)."
echo -e "\t-p,\t--password PASSWORD Password for MegaCrypter links (same for every link in a list)."
echo -e "\t-q,\t--quiet Quiet mode."
echo -e "\t-m,\t--metadata Prints file metadata in JSON format and exits."
echo -e "\tFile line format: URL [optional_file_name]\n"
}
function check_deps {
local dep_error=0
if [ -n "$(command -v curl 2>&1)" ]; then
DL_COM="curl --fail -s"
DL_COM_PDATA="--data"
elif [ -n "$(command -v wget 2>&1)" ]; then
DL_COM="wget -q -O -"
DL_COM_PDATA="--post-data"
else
echo "wget OR curl is required and it's not installed"
dep_error=1
fi
for i in openssl pv jq; do
if [ -z "$(command -v "$i" 2>&1)" ]; then
echo "[$i] is required and it's not installed"
dep_error=1
else
case "$i" in
openssl)
openssl_sup=$(openssl enc -ciphers 2>&1)
for i in "aes-128-ctr" "aes-128-cbc" "aes-256-cbc"; do
if [ -z "$(echo -n "$openssl_sup" | grep -o "$i" | head -n1)" ]; then
echo "Your openssl binary does not support ${i}"
dep_error=1
fi
done
;;
esac
fi
done
if [ -z "$(command -v python 2>&1)" ]; then
echo "WARNING: python is required for MegaCrypter password protected links and it's not installed."
fi
if [[ "$(echo -n "$BASH_VERSION" | grep -o -E "[0-9]+" | head -n1)" < "$BASH_MIN_VER" ]]; then
echo "bash >= ${BASH_MIN_VER} is required"
dep_error=1
fi
if [ $dep_error -ne 0 ]; then
showError "ERROR: there are dependencies not present!"
fi
}
# 2:url
function urldecode {
: "${*//+/ }"; echo -e "${_//%/\\x}";
}
# 1:b64_encoded_string
function urlb64_to_b64 {
local b64=$(echo -n "$1" | tr '\-_' '+/' | tr -d ',')
local pad=$(((4-${#1}%4)%4))
for i in $(seq 1 $pad); do
b64="${b64}="
done
echo -n "$b64"
}
# 1:mega://enc link
function decrypt_md_link {
local data=$(regex_imatch "^.*?mega:\/\/enc[0-9]*?\?([a-z0-9_,-]+).*?$" "$1" 1)
local iv="79F10A01844A0B27FF5B2D4E0ED3163E"
if [ $(echo -n "$1" | grep 'mega://enc?') ]; then
key="6B316F36416C2D316B7A3F217A30357958585858585858585858585858585858"
elif [ $(echo -n "$1" | grep 'mega://enc2?') ];then
key="ED1F4C200B35139806B260563B3D3876F011B4750F3A1A4A5EFD0BBE67554B44"
fi
echo -n "https://mega.nz/#"$(echo -n "$(urlb64_to_b64 "$data")" | $OPENSSL_AES_CBC_256_DEC -K "$key" -iv "$iv")
}
# 1:hex_raw_key
function hrk2hk {
declare -A hk
hk[0]=$(( 0x${1:0:16} ^ 0x${1:32:16} ))
hk[1]=$(( 0x${1:16:16} ^ 0x${1:48:16} ))
printf "%016x%016x" ${hk[0]} ${hk[1]}
}
# 1:link
function get_mc_link_info {
local MC_API_URL=$(echo -n "$1" | grep -i -E -o 'https?://[^/]+')"/api"
local download_exit_code=1
local info_link=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "{\"m\":\"info\", \"link\":\"$1\"}" "$MC_API_URL")
download_exit_code=$?
if [ "$download_exit_code" -ne 0 ]; then
echo -e "ERROR: Oooops, something went bad. EXIT CODE (${download_exit_code})"
return 1
fi
if [ $(echo $info_link | grep '"error"') ]; then
local error_code=$(echo "$info_link" | jq -r .error)
echo -e "MEGACRYPTER ERROR $error_code"
return 1
fi
local expire=$(echo "$info_link" | jq -r .expire)
if [ "$expire" != "false" ]; then
IFS='#' read -a array <<< "$expire"
local no_exp_token=${array[1]}
else
local no_exp_token="$expire"
fi
local file_name=$(echo "$info_link" | jq -r .name | base64 -w 0 -i 2>/dev/null)
local path=$(echo "$info_link" | jq -r .path)
if [ "$path" != "false" ]; then
path=$(echo -n "$path" | base64 -w 0 -i 2>/dev/null)
fi
local mc_pass=$(echo "$info_link" | jq -r .pass)
local file_size=$(echo "$info_link" | jq -r .size)
local key=$(echo "$info_link" | jq -r .key)
echo -n "${file_name}@${path}@${file_size}@${mc_pass}@${key}@${no_exp_token}"
}
# 1:file_name 2:file_size 3:formatted_file_size [4:md5_mclink]
function check_file_exists {
if [ -f "$1" ]; then
local actual_size=$(stat -c %s "$1")
if [ "$actual_size" == "$2" ]; then
if [ -n "$4" ] && [ -f ".megadown/${4}" ]; then
rm ".megadown/${4}"
fi
showError "WARNING: File $1 exists. Download aborted!"
fi
DL_MSG="\nFile $1 exists but with different size (${2} vs ${actual_size} bytes). Downloading [${3}] ...\n"
else
DL_MSG="\nDownloading $1 [${3}] ...\n"
fi
}
# 1:file_size
function format_file_size {
if [ "$1" -ge 1073741824 ]; then
local file_size_f=$(awk "BEGIN { rounded = sprintf(\"%.1f\", ${1}/1073741824); print rounded }")" GB"
elif [ "$1" -ge 1048576 ];then
local file_size_f=$(awk "BEGIN { rounded = sprintf(\"%.1f\", ${1}/1048576); print rounded }")" MB"
else
local file_size_f="${1} bytes"
fi
echo -ne "$file_size_f"
}
# 1:password 2:salt 3:iterations
function mc_pbkdf2 {
echo -e "import sys,hashlib,base64\nprint(base64.b64encode(hashlib.pbkdf2_hmac('sha256', b'${1}', base64.b64decode(b'${2}'), ${3})).decode())" | python
}
# 1:mc_pass_info 2:pass_to_check
function mc_pass_check {
IFS='#' read -a array <<< "$1"
local iter_log2=${array[0]}
local key_check=${array[1]}
local salt=${array[2]}
local iv=${array[3]}
local mc_pass_hash=$(mc_pbkdf2 "$password" "$salt" $((2**$iter_log2)))
mc_pass_hash=$(echo -n "$mc_pass_hash" | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
iv=$(echo -n "$iv" | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
if [ "$(echo -n "$key_check" | $OPENSSL_AES_CBC_256_DEC -K "$mc_pass_hash" -iv "$iv" 2>/dev/null | od -v -An -t x1 | tr -d '\n ')" != "$mc_pass_hash" ]; then
echo -n "0"
else
echo -n "${mc_pass_hash}#${iv}"
fi
}
#1:string
function trim {
if [[ "$1" =~ \ *([^ ]|[^ ].*[^ ])\ * ]]; then
echo -n "${BASH_REMATCH[1]}"
fi
}
#1:pattern 2:subject 3:group
function regex_match {
if [[ "$2" =~ $1 ]]; then
echo -n "${BASH_REMATCH[$3]}"
fi
}
#1:pattern 2:subject 3:group
function regex_imatch {
shopt -s nocasematch
if [[ "$2" =~ $1 ]]; then
echo -n "${BASH_REMATCH[$3]}"
fi
shopt -u nocasematch
}
#MAIN STARTS HERE:
check_deps
if [ -z "$1" ]; then
showHelp
exit 1
fi
eval set -- "$(getopt -o "l:p:k:o:s:qm" -l "list:,password:,key:,output:,speed:,quiet,metadata" -n ${0} -- "$@")"
while true; do
case "$1" in
-l|--list) list="$2"; shift 2;;
-p|--password) password="$2"; shift 2;;
-o|--output) output="$2"; shift 2;;
-s|--speed) speed="$2"; shift 2;;
-q|--quiet) quiet=true; shift 1;;
-m|--metadata) metadata=true; shift 1;;
--) shift; break;;
*)
showHelp
exit 1;;
esac
done
yen=$(echo $1 | sed 's|#|!|g' | sed 's|file/|#!|g')
p1=$(trim $(urldecode "$yen"))
if [[ "$p1" =~ ^http ]] || [[ "$p1" =~ ^mega:// ]]; then
link="$p1"
fi
if [ -z "$link" ]; then
if [ -z "$list" ]; then
showHelp
showError "ERROR: MEGA/MC link or --list parameter is required"
elif [ ! -f "$list" ]; then
showHelp
showError "ERROR: list file ${list} not found"
fi
if [ ! $quiet ]; then
echo -ne "\n(Pre)reading mc links info..."
fi
link_count=0
while IFS='' read -r line || [ -n "$line" ]; do
if [ -n "$line" ] && ! [ $(echo -n "$line" | grep -E -o 'mega://enc') ];then
link=$(regex_imatch "^.*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
if [ $(echo -n "$link" | grep -E -o 'https?://[^/]+/!') ]; then
md5=$(echo -n "$link" | $OPENSSL_MD5 | grep -E -o '[0-9a-f]{32}')
if [ ! -f ".megadown/${md5}" ];then
mc_link_info=$(get_mc_link_info "$link")
if ! [ "$?" -eq 1 ];then
echo -n "$mc_link_info" >> ".megadown/${md5}"
fi
fi
link_count=$((link_count + 1))
fi
fi
done < "$list"
echo -ne " OK(${link_count} MC links found)\n"
while IFS='' read -r line || [ -n "$line" ]; do
if [ -n "$line" ];then
if [ $(echo -n "$line" | grep -E -o 'mega://enc') ]; then
link=$(regex_imatch "^.*?(mega:\/\/enc\d*?\?[a-z0-9_-]+).*$" "$line" 1)
output=$(regex_imatch "^.*?mega:\/\/enc\d*?\?[a-z0-9_-]+(.*)$" "$line" 1 1)
elif [ $(echo -n "$line" | grep -E -o 'https?://') ]; then
link=$(regex_imatch ".*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
output=$(regex_imatch "^.*?https?\:\/\/[^\/]+\/[#!0-9a-z_-]+(.*)$" "$line" 1 1)
else
continue
fi
$SCRIPT "$link" --output="$output" --password="$password" --speed="$speed"
fi
done < "$list"
exit 0
fi
if [ $(echo -n "$link" | grep -E -o 'mega://enc') ]; then
link=$(decrypt_md_link "$link")
fi
if [ ! $quiet ]; then
echo -e "\nReading link metadata..."
fi
if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz') ]; then
#MEGA.CO.NZ LINK
file_id=$(regex_match "^.*\/#.*?!(.+)!.*$" "$link" 1)
file_key=$(regex_match "^.*\/#.*?!.+!(.+)$" "$link" 1)
hex_raw_key=$(echo -n $(urlb64_to_b64 "$file_key") | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz/#!') ]; then
mega_req_json="[{\"a\":\"g\", \"p\":\"${file_id}\"}]"
mega_req_url="${MEGA_API_URL}/cs?id=&ak="
elif [ $(echo -n "$link" | grep -E -o -i 'mega(\.co)?\.nz/#N!') ]; then
mega_req_json="[{\"a\":\"g\", \"n\":\"${file_id}\"}]"
folder_id=$(regex_match "###n\=(.+)$" "$link" 1)
mega_req_url="${MEGA_API_URL}/cs?id=&ak=&n=${folder_id}"
fi
mega_res_json=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "$mega_req_json" "$mega_req_url")
download_exit_code=$?
if [ "$download_exit_code" -ne 0 ]; then
showError "Oooops, something went bad. EXIT CODE (${download_exit_code})"
fi
if [ $(echo -n "$mega_res_json" | grep -E -o '\[ *\-[0-9]+ *\]') ]; then
showError "MEGA ERROR $(echo -n "$mega_res_json" | grep -E -o '\-[0-9]+')"
fi
file_size=$(echo "$mega_res_json" | jq -r .[0].s)
at=$(echo "$mega_res_json" | jq -r .[0].at)
hex_key=$(hrk2hk "$hex_raw_key")
at_dec_json=$(echo -n $(urlb64_to_b64 "$at") | $OPENSSL_AES_CBC_128_DEC -K "$hex_key" -iv "00000000000000000000000000000000" -nopad | tr -d '\0')
if [ ! $(echo -n "$at_dec_json" | grep -E -o 'MEGA') ]; then
showError "MEGA bad link"
fi
if [ -z "$output" ]; then
file_name=$(echo -n "$at_dec_json" | grep -E -o '\{.+\}' | jq -r .n)
else
file_name="$output"
fi
if [ $metadata ]; then
echo "{\"file_name\" : \"${file_name}\", \"file_size\" : ${file_size}}"
exit 0
fi
check_file_exists "$file_name" "$file_size" "$(format_file_size "$file_size")"
if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz/#!') ]; then
mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"p\":\"$file_id\"}]"
elif [ $(echo -n "$link" | grep -E -o -i 'mega(\.co)?\.nz/#N!') ]; then
mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"n\":\"$file_id\"}]"
fi
mega_res_json=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "$mega_req_json" "$mega_req_url")
download_exit_code=$?
if [ "$download_exit_code" -ne 0 ]; then
showError "Oooops, something went bad. EXIT CODE (${download_exit_code})"
fi
dl_temp_url=$(echo "$mega_res_json" | jq -r .[0].g)
echo "{\"file_name\" : \"${file_name}\", \"url\" : \"${dl_temp_url}\"}"
fi
exit 0