Skip to content

Instantly share code, notes, and snippets.

@zanculmarktum
Last active June 2, 2024 05:58
Show Gist options
  • Save zanculmarktum/170b94764bd9a3da31078580ccea8d7e to your computer and use it in GitHub Desktop.
Save zanculmarktum/170b94764bd9a3da31078580ccea8d7e to your computer and use it in GitHub Desktop.
Get download url from mega.nz
#!/bin/bash
# Copyright 2018, 2019, 2020 Azure Zanculmarktum
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Dependencies:
# openssl
# Usage:
# $ ./megafetch.sh https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG
# http://gfs208n103.userstorage.mega.co.nz/dl/-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890A
# file.name
# abcefghijklmnopqrstuvwxyz0123456
# 1234567890abcdef0000000000000000
# $ wget -O file.name http://gfs208n103.userstorage.mega.co.nz/dl/-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890A
# $ cat file.name | openssl enc -d -aes-128-ctr -K abcefghijklmnopqrstuvwxyz0123456 -iv 1234567890abcdef0000000000000000 > file.name.new
# $ mv -f file.name.new file.name
URL=""
if [[ $1 =~ ^https?:\/\/mega(\.co)?\.nz ]]; then
URL="$1"
fi
if [[ ! $URL ]]; then
echo "Usage: ${0##*/} url" >&2
exit 1
fi
CURL="curl -Y 1 -y 10"
missing=false
for cmd in openssl; do
if [[ ! $(command -v "$cmd" 2>&1) ]]; then
missing=true
echo "${0##*/}: $cmd: command not found" >&2
fi
done
if $missing; then
exit 1
fi
if [[ $URL =~ .*/file/[^#]*#[^#]* ]]; then
id="${URL#*file/}"; id="${id%%#*}"
key="${URL##*file/}"; key="${key##*#}"
else
id="${URL#*!}"; id="${id%%!*}"
key="${URL##*!}"
fi
raw_hex=$(echo "${key}=" | tr '\-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
hex=$(printf "%016x" \
$(( 0x${raw_hex:0:16} ^ 0x${raw_hex:32:16} )) \
$(( 0x${raw_hex:16:16} ^ 0x${raw_hex:48:16} ))
)
json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1; json="${json#"[{"}"; json="${json%"}]"}"
file_url="${json##*'"g":'}"; file_url="${file_url%%,*}"; file_url="${file_url//'"'/}"
json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1
at="${json##*'"at":'}"; at="${at%%,*}"; at="${at//'"'/}"
json=$(echo "${at}==" | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0'); json="${json#"MEGA{"}"; json="${json%"}"}"
file_name="${json##*'"n":'}"
if [[ $file_name == *,* ]]; then
file_name="${file_name%%,*}"
fi
file_name="${file_name//'"'/}"
#$CURL -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" > "$file_name"
echo "$file_url"
echo "$file_name"
echo "$hex"
echo "${raw_hex:32:16}0000000000000000"
@Torronf
Copy link

Torronf commented Apr 15, 2019

Hi, I try to replícate this versión in to PHP but the final FILE it in posible to read. can you help me to complet my proyect.

'g', 'g' => 1, 'ssl' => 0, //0, 1, 2 (default is 2) 'p' => $fileID) // File id here ); $rawPOST = json_encode($value); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiURL ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $rawPOST ); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain;charset=UTF-8')); $result=curl_exec($ch); $jsonResult = json_decode($result); $directLink = $jsonResult[0]->g; $fileSize = $jsonResult[0]->s; $video = trim($jsonResult[0]->s); $public_name = basename($video).".mp4"; echo $directLink; /* header('Content-Type: application/octet-stream'); // header("Content-Type: video/mp4"); header("Content-Disposition: attachment; filename=$public_name;"); header("Content-Length: ".$fileSize); readfile($directLink); */ ?>

@crabvk
Copy link

crabvk commented Jul 31, 2019

It's working! Thank you very much😊

@Daniyal-Javani
Copy link

not working!
./megafetch.sh 'https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG'
[-9]

8eca5a7d054e2ce9586d649f929bb3c3
59a7a29aabb2dbaf0000000000000000

@zanculmarktum
Copy link
Author

zanculmarktum commented Jan 20, 2020

./megafetch.sh 'https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG'

it's just an example link, notice the abcd1234 in https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG

try

$ ./megafetch.sh 'https://mega.nz/#!8soDySiQ!Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc'
http://gfs262n301.userstorage.mega.co.nz/dl/AOF6hkBP_lbhbQIUnJ-Hd8Lwns7ep0vbgvkZGIEvnKS7XNIPf6JfSnLiEdDDcl0md1qjaQfSHECqf3z5Pq05W8SfQLmQ3uVsnegmzl5KF0WyiKZ_jfFvHFgmiEiCS4AvBHAxK9q8VYpg0Cgh8Lfad-IWaf1TF_AiH6nSpgkInIdGYMi5YS7C7NjpUsSzLLPnEs5i87i4JhAo7ag3NKIYVk7cCsfkLl_n04qZWpuiOKxqgA8KMTFSZpmZlg
libvirt-4.5.0.tar.xz
455750c6853b7570e24870b0ba95dca0
22f5692a5da28dde0000000000000000

@vapaternina
Copy link

I don't understand that :/ I use

./megafetch.sh 'https://mega.nz/#!8soDySiQ!Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc'
http://gfs262n301.userstorage.mega.co.nz/dl/AOF6hkBP_lbhbQIUnJ-Hd8Lwns7ep0vbgvkZGIEvnKS7XNIPf6JfSnLiEdDDcl0md1qjaQfSHECqf3z5Pq05W8SfQLmQ3uVsnegmzl5KF0WyiKZ_jfFvHFgmiEiCS4AvBHAxK9q8VYpg0Cgh8Lfad-IWaf1TF_AiH6nSpgkInIdGYMi5YS7C7NjpUsSzLLPnEs5i87i4JhAo7ag3NKIYVk7cCsfkLl_n04qZWpuiOKxqgA8KMTFSZpmZlg
libvirt-4.5.0.tar.xz
455750c6853b7570e24870b0ba95dca0
22f5692a5da28dde0000000000000000

And obtain sometime like that...
But... only download a html/text file :/

@glinskicwb
Copy link

Working 11/27/2020. Thank you.

@Zubayer204
Copy link

Works like a charm!

@oneberto
Copy link

oneberto commented Apr 5, 2021

not working :(

@aejunior
Copy link

aejunior commented Apr 7, 2021

Working!

@sixtyfive
Copy link

Does it dpeend on the link perhaps?

@dausruddin
Copy link

Doesn't work

@sixtyfive
Copy link

Also all y'all check out https://megatools.megous.com/ which is still working..

@kicktv
Copy link

kicktv commented Aug 28, 2021

Is there a php version?

@nalakawula
Copy link

Amazing, it's working!
Thanks!

@Bijman
Copy link

Bijman commented Mar 20, 2022

There is an issue on macOS Big Sur with receiving proper hex and raw_hex values. Something with key value not being converted with tr, od and base64 pipes, so it results with raw_hex variable to become empty, although the key value is the same as on Linux and can download on Linux system.

BTW I used https://mega.nz/#!8soDySiQ!Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc as example of this issue.
Resulting key value on both systems is Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc with Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc= used to be converted.

Here is a part where this goes wrong:

+ key=Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc
++ echo Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc=
++ tr '\-_' +/
++ tr -d ,
++ base64 -d -i
++ od -v -An -t x1
++ tr -d '\n '
+ raw_hex=
++ printf %016x 0 0
+ hex=00000000000000000000000000000000

@Bijman
Copy link

Bijman commented Mar 20, 2022

I think that I found a very simple solution on macOS. Just add - to base64 command, so in the script should like this:

raw_hex=$(echo "${key}=" | tr '\-_' '+/' | tr -d ',' | base64 -d -i - 2>/dev/null | od -v -An -t x1 | tr -d '\n ')

Works on macOS and Linux with example https://mega.nz/#!8soDySiQ!Z6I57NiZ-K5CeE8E95ZKZyL1aSpdoo3eoDA_tE0Dlsc.

@LinenBen17
Copy link

Mega decided to change the url's, so, this method it's not working for the moment.

@hassaannoor
Copy link

I works as of 15 May, 2023

@orion486
Copy link

orion486 commented Jun 25, 2023

Works with old Mega URLs of the following two forms:

https://mega.nz/#!Wt0F1bpR!WlCGMYlOS_K23Q4wH5isOK7Oin6qsa2uyEAG3C5G3ro
https://mega.nz/file/Wt0F1bpR#WlCGMYlOS_K23Q4wH5isOK7Oin6qsa2uyEAG3C5G3ro

But not with the following forms:

https://mega.nz/folder/j9UklByL#UGECRD-W1P3gqv2na_GAmA/file/r8kA3KjI

The following CURL command would get the right JSON for the URL above, just not sure where the key is provided in this case, mega says it's "UGECRD-W1P3gqv2na_GAmA/file/r8kA3KjI" but not sure. Perhaps in the returned JSON string:

CURL -s -H 'Content-Type: application/json' -d '[{"a":"g","g":1,"ssl":1,"v":2,"n":"r8kA3KjI"}]' 'https://g.api.mega.co.nz/cs?n=j9UklByL'

All three URL forms specify the exact same file/resource.

@Rabah781
Copy link

@kxzen-x
Copy link

kxzen-x commented Dec 29, 2023

tell me to download a file not a folder show me code in python or curl whtever pls help

@daninouai
Copy link

It works great in 2024!

@kasun-97
Copy link

Yes, working great in 2024

@DarkTama
Copy link

DarkTama commented Apr 3, 2024

doesn't work in

$ ./megafetch.sh 'https://mega.nz/file/UvlSWDBS#xZq1LxAEKuMUFk1hZ077gKh3mFMJmhXJRgcCa3tADKY'
bad decrypt
38950000:error:1C80006B:Provider routines:ossl_cipher_generic_block_final:wrong final block length:../openssl-3.1.2/providers/implementations/ciphers/ciphercommon.c:429:
-3

6ded2d7c199e3f2a52114f0a1c0ef726
a8779853099a15c90000000000000000

@zanculmarktum
Copy link
Author

@DarkTama works here

$ ./megafetch.sh 'https://mega.nz/file/UvlSWDBS#xZq1LxAEKuMUFk1hZ077gKh3mFMJmhXJRgcCa3tADKY'
http://gfs206n133.userstorage.mega.co.nz/dl/UVrTogmlOlMj4wLY4QynZQ7iwpC03dfDL26mS1wijvD2e5wlAB9LPPe4XjkUohK9V3Tn0a4KMLAfFh2nvtdYTEUjxLr_y8Cb-F0PD7yiFlloXAxej2OeQ6eQo6yoMA
 Green.Book.2018.1080p.BluRay.x264.DD5.1-Pahe.in.mkv
6ded2d7c199e3f2a52114f0a1c0ef726
a8779853099a15c90000000000000000

@DarkTama
Copy link

DarkTama commented Apr 3, 2024

@DarkTama works here

$ ./megafetch.sh 'https://mega.nz/file/UvlSWDBS#xZq1LxAEKuMUFk1hZ077gKh3mFMJmhXJRgcCa3tADKY'
http://gfs206n133.userstorage.mega.co.nz/dl/UVrTogmlOlMj4wLY4QynZQ7iwpC03dfDL26mS1wijvD2e5wlAB9LPPe4XjkUohK9V3Tn0a4KMLAfFh2nvtdYTEUjxLr_y8Cb-F0PD7yiFlloXAxej2OeQ6eQo6yoMA
 Green.Book.2018.1080p.BluRay.x264.DD5.1-Pahe.in.mkv
6ded2d7c199e3f2a52114f0a1c0ef726
a8779853099a15c90000000000000000

i've been suspicious with the ISP, cause i specifically can't connect to the link or my drive, that's probably the main cause

@Mosta5a-10
Copy link

URL="https://mega.nz/file/RwkXBIDT#uyW7dSc4nvSJkNg7Y_7LKtRMZnOno75Se2ywFP62bCE"

if [[ $1 =~ ^https?://mega(.co)?.nz ]]; then
URL="$1"
fi

if [[ ! $URL ]]; then
echo "Usage: ${0##*/} url" >&2
exit 1
fi

CURL="curl -Y 1 -y 10"

missing=false
for cmd in openssl; do
if [[ ! $(command -v "$cmd" 2>&1) ]]; then
missing=true
echo "${0##*/}: $cmd: command not found" >&2
fi
done
if $missing; then
exit 1
fi

if [[ $URL =~ ./file/[^#]#[^#]* ]]; then
id="${URL#file/}"; id="${id%%#}"
key="${URL##file/}"; key="${key###}"
else
id="${URL#!}"; id="${id%%!}"
key="${URL##*!}"
fi

raw_hex=$(echo "${key}=" | tr '-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
hex=$(printf "%016x"
$(( 0x${raw_hex:0:16} ^ 0x${raw_hex:32:16} ))
$(( 0x${raw_hex:16:16} ^ 0x${raw_hex:48:16} ))
)

json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1; json="${json#"[{"}"; json="${json%"}]"}"
file_url="${json##'"g":'}"; file_url="${file_url%%,}"; file_url="${file_url//'"'/}"

json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1
at="${json##'"at":'}"; at="${at%%,}"; at="${at//'"'/}"

json=$(echo "${at}==" | tr '-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0'); json="${json#"MEGA{"}"; json="${json%"}"}"
file_name="${json##'"n":'}"
if [[ $file_name == , ]]; then
file_name="${file_name%%,
}"
fi
file_name="${file_name//'"'/}"

#$CURL -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" > "$file_name"

echo "$file_url"
echo "$file_name"
echo "$hex"
echo "${raw_hex:32:16}0000000000000000"

@kishan-kumar-ji
Copy link

URL="https://mega.nz/file/RwkXBIDT#uyW7dSc4nvSJkNg7Y_7LKtRMZnOno75Se2ywFP62bCE"

if [[ $1 =~ ^https?://mega(.co)?.nz ]]; then URL="$1" fi

if [[ ! $URL ]]; then echo "Usage: ${0##*/} url" >&2 exit 1 fi

CURL="curl -Y 1 -y 10"

missing=false for cmd in openssl; do if [[ ! $(command -v "$cmd" 2>&1) ]]; then missing=true echo "${0##*/}: $cmd: command not found" >&2 fi done if $missing; then exit 1 fi

if [[ $URL =~ ./file/[^#]#[^#]* ]]; then id="${URL#file/}"; id="${id%%#}" key="${URL##file/}"; key="${key###}" else id="${URL#!}"; id="${id%%!}" key="${URL##*!}" fi

raw_hex=$(echo "${key}=" | tr '-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ') hex=$(printf "%016x" ((0x{raw_hex:0:16} ^ 0x${raw_hex:32:16} )) ((0x{raw_hex:16:16} ^ 0x${raw_hex:48:16} )) )

json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1; json="${json#"[{"}"; json="${json%"}]"}" file_url="${json##'"g":'}"; file_url="${file_url%%,}"; file_url="${file_url//'"'/}"

json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1 at="${json##'"at":'}"; at="${at%%,}"; at="${at//'"'/}"

json=$(echo "${at}==" | tr '-' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0'); json="${json#"MEGA{"}"; json="${json%"}"}" file_name="${json##'"n":'}" if [[ $file_name == , ]]; then file_name="${file_name%%,_}" fi file_name="${file_name//'"'/}"

#$CURL -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" > "$file_name"

echo "$file_url" echo "$file_name" echo "$hex" echo "${raw_hex:32:16}0000000000000000"

make it for folders and new type link version make a repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment