# Download MEGA files from command-line


# cat mega-dl
#!/bin/bash

url=$1
enc_file=$2.enc
out_file=$2

 id=`echo $url | awk -F '!' '{print $2}'`
key=`echo $url | awk -F '!' '{print $3}' | sed -e 's/-/+/g' -e 's/_/\//g' -e 's/,//g'`
b64_hex_key=`echo -n $key | base64 --decode --ignore-garbage 2> /dev/null | xxd -p | tr -d '\n'`
key[0]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
key[1]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
key=`printf "%x" ${key[*]}`
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 | awk -F '"' '{print $10}'`
curl --output $enc_file $new_url
openssl enc -d -aes-128-ctr -K $key -iv $iv -in $enc_file -out $out_file
rm -f $enc_file
# url='https://mega.co.nz/#!HFsiWYrJ!VkovRDl9kJkIMDBkiRlw-E9OGOkcD1DbNqs3d-CGNf8'
# ./mega-dl $url toulouse.mp4
# file toulouse.mp4
toulouse.mp4: ISO Media, MPEG v4 system, version 2

18 comments:

Anonymous said...

Hi,

I have a problem, my opensssl don't have aes-128-ctr in optien (it don't work) so it download the file and it never "decrypt it' so how can i do ?

It is an upgrade of the open-ssl program or what ?

Thank you.

Anonymous said...

Hi,

I have a problem, my opensssl don't have aes-128-ctr in option (it don't work) so it download the file and it never "decrypt it' so how can i do ?

It is an upgrade of the open-ssl program or what ?

Thank you.

Anonymous said...

Worked great, thank you!

Anonymous said...

awesome thank you!

Anonymous said...

thanks.

on a related note, I wrote a tool that allows to mount a MEGA account as a fuse filesystem:
https://github.com/matteoserva/MegaFuse

Anonymous said...

they changed it to -aes-256-ctr. So you might want to edit that.
At least it didn't work with AES-128-CTR.

Anonymous said...

Works great, but I have a question:

Let's assume I have a link to a file on mega and want the file to be saved with the very same name as uploaded on mega... How can I query the original file name?

Opening the link in my browser shows me the original file name, but how can I query that from the script?

Thanks for helpful input

Hepha Cheng said...

how can download mega public folder ?
thx

Leon S said...

Hey great script, found a small bug where it wouldn't create the right key though, it's a simple fix:

Change
key=`printf "%x" ${key[*]}`
to
key=`printf "%016x" ${key[*]}`

With this fix it makes keys that start with a hex char < 0x10 have leading zeroes

I've also made a version where it gets the filename from MEGA, so now you only need to give the MEGA URL
http://pastebin.com/dj1KcKKS
Had to do some weird things with temp files because OpenSSL is picky but it works great for batch jobs :D

Yohanes Pebryanto said...

Hello, i confed here. what must i do ? save this command to *.sh ? or what ? please gift me tutorial.

DinJo said...

The script fails when the video has spaces in it

Camilo Martin said...

Why don't people quote their scripts' variables?

Just quote the variables that might ever contain spaces or other characters. Heck, even echo supports arguments. Just... always quote, if unsure. See, "$var" is not so hard.

Anonymous said...

Camilo Martino.... STOP WHINING!

Derek said...

This stopped working due to a Mega API update.
Here's the updated version: http://pastebin.com/xFD2qXbL

Anonymous said...

Thanks, I was just about to point out that the URL is the $12th quote-separated field, not the $10th in the original script, and you were way ahead of me.

Anonymous said...

Does anyone have a problem where this has download speeds at around 100kb instead of 6MB in the recent months?
BTW, why not just grep the http string? Then no matter which field is it, it will work as intended.

Anonymous said...

This is great, really! Thank you.
How about folder URLs? would it be possible to adapt this to download a single file or dir in folder, or the entire folder?

Frank said...

Beyond awesomw stuff!!!
Thanks a lot...
Some corrections tho
Comments on lines 21 and 28
http://pastebin.com/JNZ0VUpi