Skip to content

Instantly share code, notes, and snippets.

@maboloshi
Forked from KenMacD/mega-dl.sh
Last active March 15, 2019 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maboloshi/77cb8e5d0ba83b82f7cc4ace506068da to your computer and use it in GitHub Desktop.
Save maboloshi/77cb8e5d0ba83b82f7cc4ace506068da to your computer and use it in GitHub Desktop.
[Download MEGA files from command-line] `./mega-dl.sh '<URL>' <file name>` (Note the single quotes around the URL, otherwise bash complains "event not found") #mega #shell
#!/bin/bash
# 原作者: t0n1
# 来自: http://hacktracking.blogspot.ca/2013/07/download-mega-files-from-command-line.html
#
# 修改: maboloshi
# <file_url>: https://mega.nz/#!<id>!<decyption Key>
url=$1
out_file="$2"
id=`echo $url | sed 's/.*#!\(.*\)!\(.*\)/\1/'`
key=`echo $url | sed 's/.*#!\(.*\)!\(.*\)/\2/;s/-/+/g;s/_/\//g;s/,//g'`
if [ "$(uname -s)" = "Darwin" ]; then
# mac
b64_hex_key=`echo -n $key | base64 --decode --ignore-garbage 2> /dev/null | od -v -An -t x1 | tr -d '\n '`
else
b64_hex_key=`echo -n $key | base64 --decode --ignore-garbage 2> /dev/null | xxd -p | tr -d '\n'`
fi
hex_key[0]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
hex_key[1]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
hex_key=`printf "%x" ${hex_key[*]}`
hex_iv="${b64_hex_key:32:16}0000000000000000"
new_url=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://eu.api.mega.co.nz/cs | sed 's/.*"g":"\(.*\)",.*/\1/'`
curl --fail "$new_url" | openssl enc -d -aes-128-ctr -K "$hex_key" -iv "$hex_iv" -out $out_file
#!/bin/bash
# 原作者: t0n1
# 来自: http://hacktracking.blogspot.ca/2013/07/download-mega-files-from-command-line.html
#
# 另参考: https://raw.githubusercontent.com/tonikelope/megadown/master/megadown
# 修改: maboloshi
# <file_url>: https://mega.nz/#!<id>!<decyption Key>
url=${1?Error: no url given!!!}
out_path=${2:-./}
# 默认输出路径为当前
id=`echo $url | sed 's/.*#!\(.*\)!\(.*\)/\1/'`
key=`echo $url | sed 's/.*#!\(.*\)!\(.*\)/\2/;s/-/+/g;s/_/\//g;s/,//g;'`===
# -替换为+ _替换为/ 删除,
# 傻瓜式补足末尾标记=
# macOS `base64` 不会自动补足末尾标记= 导致解密错误
# 且多余的= 不会影响`base64`解码
# 但是`openssl enc -base64` 比较严格,末尾标记=不足和多余 直接返回空值
# 可使用以下函数,进行预处理
# 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"
while [ "$key" = "" ]
do
read -p "Enter decyption Key:" key
done
b64_hex_key=`echo -n $key | base64 --decode 2> /dev/null | od -v -An -t x1 | tr -d '\n '`
hex_key[1]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
hex_key[2]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
hex_key=`printf "%x" ${hex_key[*]}`
hex_iv="${b64_hex_key:32:16}0000000000000000"
hex_public_iv=00000000000000000000000000000000
mega_res_json=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://g.api.mega.co.nz/cs`
file_metadata=`echo $mega_res_json | sed 's/.*"at":"\(.*\)",".*/\1/;s/-/+/g;s/_/\//g;s/,//g' | openssl enc -a -A -d -aes-128-cbc -K "$hex_key" -iv "$hex_public_iv" -nopad | tr -d '\0'`
file_url=`echo $mega_res_json | sed 's/.*"g":"\(.*\)".*/\1/'`
file_size=`echo $mega_res_json | sed 's/.*"s":\([0-9].*\),"at".*/\1/' | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }'`
file_name=`echo $file_metadata | sed 's/.*"n":"\(.*\)",.*/\1/'`
printf "Downloading %s [size: %s]...\n" "$file_name" "$file_size"
curl --progress-bar --fail "$file_url" | openssl enc -d -aes-128-ctr -K "$hex_key" -iv "$hex_iv" -out "$out_path$file_name"
#!/bin/bash
# 原作者: t0n1
# 来自: http://hacktracking.blogspot.ca/2013/07/download-mega-files-from-command-line.html
#
# 另参考: https://raw.githubusercontent.com/tonikelope/megadown/master/megadown
# 修改: maboloshi
# <file_url>: https://mega.nz/#!<id>!<decyption Key>
url=${1?Error: no url given!!!}
out_path=${2:-./}
# 默认输出路径为当前
id=`echo $url | awk -F '!' '{print $2}'`
key=`echo $url | awk -F '!' '{print $3}'| tr '\-_' '+/' | tr -d ','`===
# -替换为+ _替换为/ 删除,
# 傻瓜式补足末尾标记=
# macOS `base64` 不会自动补足末尾标记= 导致解密错误
# 且多余的= 不会影响`base64`解码
# 但是`openssl enc -base64` 比较严格,末尾标记=不足和多余 直接返回空值
# 可使用以下函数,进行预处理
# 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"
while [ "$key" = "" ]
do
read -p "Enter decyption Key:" key
done
b64_hex_key=`echo -n $key | base64 --decode 2> /dev/null | od -v -An -t x1 | tr -d '\n '`
hex_key[1]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
hex_key[2]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
hex_key=`printf "%x" ${hex_key[*]}`
hex_iv="${b64_hex_key:32:16}0000000000000000"
hex_public_iv=00000000000000000000000000000000
mega_res_json=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://g.api.mega.co.nz/cs`
file_metadata=`echo $mega_res_json | awk -F '"' '{print $6}' | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex_key" -iv "$hex_public_iv" -nopad | tr -d '\0'`
file_url=`echo $mega_res_json | awk -F '"' '{print $12}'`
file_size=`echo $mega_res_json | sed 's/.*"s":\([0-9].*\),"at".*/\1/' | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }'`
file_name=`echo $file_metadata | awk -F '"' '{print $4}'`
printf "Downloading %s [size: %s]...\n" "$file_name" "$file_size"
curl --progress-bar --fail "$file_url" | openssl enc -d -aes-128-ctr -K "$hex_key" -iv "$hex_iv" -out "$out_path$file_name"
#!/bin/bash
# 原作者: t0n1
# 来自: http://hacktracking.blogspot.ca/2013/07/download-mega-files-from-command-line.html
#
# 另参考: https://raw.githubusercontent.com/tonikelope/megadown/master/megadown
# 修改: maboloshi
# <file_url>: https://mega.nz/#!<id>!<decyption Key>
url=${1?Error: no url given!!!}
out_path=${2:-./}
# 默认输出路径为当前
id=`echo $url | awk -F '!' '{print $2}'`
key=`echo $url | awk -F '!' '{print $3}'| tr '\-_' '+/' | tr -d ','`===
# -替换为+ _替换为/ 删除,
# 傻瓜式补足末尾标记=
# macOS `base64` 不会自动补足末尾标记= 导致解密错误
# 且多余的= 不会影响`base64`解码
# 但是`openssl enc -base64` 比较严格,末尾标记=不足和多余 直接返回空值
# 可使用以下函数,进行预处理
# 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"
while [ "$key" = "" ]
do
read -p "Enter decyption Key:" key
done
b64_hex_key=`echo -n $key | base64 --decode 2> /dev/null | od -v -An -t x1 | tr -d '\n '`
hex_key[1]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
hex_key[2]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
hex_key=`printf "%x" ${hex_key[*]}`
hex_iv="${b64_hex_key:32:16}0000000000000000"
hex_public_iv=00000000000000000000000000000000
mega_res_json=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://g.api.mega.co.nz/cs`
file_metadata=`echo $mega_res_json| jq -r .[0].at | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex_key" -iv "$hex_public_iv" -nopad | tr -d '\0'`
file_url=`echo $mega_res_json| jq -r .[0].g`
file_size=`echo $mega_res_json| jq -r .[0].s | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }'`
file_name=`echo $file_metadata | awk -F '"' '{print $4}'`
printf "Downloading %s [size: %s]...\n" "$file_name" "$file_size"
curl --progress-bar --fail "$file_url" | openssl enc -d -aes-128-ctr -K "$hex_key" -iv "$hex_iv" -out "$out_path$file_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment