87linux | 15 points
./mega-dl.sh "FULL_MEGA_URL" "OUTPUT_FILE_NAME"
#!/bin/bash
if [[ $# != 2 ]]; then
echo "usage: mega-dl.sh <url> <output>"
exit 1
fi
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"
response=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://eu.api.mega.co.nz/cs`
echo "$response"
new_url=`echo "$response" | 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"
This would pretty much only be useful if you want to download things without a web browser, but if you're a better bash hacker than me, you might be able to continue an incomplete download. Good luck with that.
EDIT: Fixed to support spaces in the output file name
Top! Might try and knock together a web front end for this - dump in your mega link and it downloads it to a defined location.
How does this work with a folder of files?
Very interesting and helpful! Did you find this out by yourself, analysing the source code?
EDIT: Another Question: On the new_url=
line it doesn't seem to work for me. Downloading the data from that location with this POST content, gives me this:
[{"s":294074232,"at":"-fVDa5pBVny1d_NDpV3G7dDe_yg4IGqj6-Mg1733Sb1ox64OWCi-92o7TlG3KGg56bQD0tj2us4Wz8BV4KW7Qg"}]
and then running awk -F '"' '{print $10}'
on it just gives an empty string, since there are just six double quotes.
Strange. Which file are you downloading? It should have given you one more JSON field called "g".
I just took an arbitrary chosen link from this subreddit, I'm pretty sure it was [Tv Show] Hell On Wheels Season 4 Episode 1.
Strangely enough, now that I tried it again, I do indeed get the g
parameter.
Did you find this procedure out all by yourself, analysing the source code? Because this is the first page I find about how the Mega web interface downloads and decrypts a file.
EDIT: Formatting
EDIT2: I've tried it anew, and now I don't get the g
paramter anymore, but instead an additional e
parameter with value -18
. Seems like it doesn't always work.
[-] Bludgeon_4_Bacon | 3 points | Aug 10 2014 19:31:03
Can you explain what this is and the benefits of using it? Total noob question
permalink
[-] 87linux | 3 points | Aug 10 2014 23:26:05
It removes the requirement of needing a web browser to download mega files. So you could download them on a raspberry pi or some other headless server.
permalink
[-] Bludgeon_4_Bacon | 1 points | Aug 10 2014 23:56:10
ahh ok thanks!
permalink