mega.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mega.sh (4000B)
       ---
            1 #!/bin/sh
            2 #
            3 # TODO: support folders.
            4 #
            5 # changes vs original script:
            6 # - base64decode function (portability).
            7 # - changed bashisms to POSIX sh and awk.
            8 # - curl more private settings (User-Agent).
            9 # - enable auto decryption (to new file).
           10 # - simplifications.
           11 #
           12 # original from: https://gist.githubusercontent.com/zanculmarktum/170b94764bd9a3da31078580ccea8d7e/raw/1adfba71a69ef155c1180ab21e8fb6bead1a6c92/megafetch.sh
           13 #
           14 # Copyright 2018, 2019, 2020 Azure Zanculmarktum
           15 # All rights reserved.
           16 #
           17 # Redistribution and use of this script, with or without modification, is
           18 # permitted provided that the following conditions are met:
           19 #
           20 # 1. Redistributions of this script must retain the above copyright
           21 #    notice, this list of conditions and the following disclaimer.
           22 #
           23 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
           24 #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
           25 #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
           26 #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
           27 #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
           28 #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
           29 #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
           30 #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
           31 #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
           32 #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
           33 #
           34 # Dependencies:
           35 #  openssl/libressl
           36 #
           37 # Usage:
           38 #  $ ./mega https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG filename
           39 
           40 # match(str, regex, [flags])
           41 match() {
           42         printf '%s' "$1" | grep "-qE$3" "$2"
           43 }
           44 
           45 URL=""
           46 if match "$1" '^https?:\/\/mega(\.co)?\.nz'; then
           47         URL="$1"
           48 fi
           49 FILENAME="$2"
           50 
           51 if test -z "$URL" || test -z "$FILENAME"; then
           52         echo "Usage: $0 <url> <filename>" >&2
           53         exit 1
           54 fi
           55 
           56 CURL="curl -Y 1 -y 10 -H 'User-Agent:'"
           57 
           58 if match "$URL" '.*/file/[^#]*#[^#]*'; then
           59         id="${URL#*file/}"
           60         id="${id%%#*}"
           61         key="${URL##*file/}"
           62         key="${key##*#}"
           63 else
           64         id="${URL#*!}"
           65         id="${id%%!*}"
           66         key="${URL##*!}"
           67 fi
           68 
           69 base64decode() {
           70         # OpenBSD
           71         (echo "begin-base64 644 -";cat;echo "====") | uudecode -m -o /dev/stdout
           72         # Linux: uncomment if needed.
           73         #base64 -d -i
           74 }
           75 
           76 raw_hex=$(echo "${key}=" | \
           77         tr '\-_' '+/' | tr -d ',' | \
           78         base64decode | \
           79         od -v -An -t x1 | \
           80         tr -d '\n ')
           81 raw_hex_sub=$(echo "$raw_hex" | awk '{ print substr($0, 33, 16); }');
           82 
           83 hex=$(echo "$raw_hex" | awk '
           84 function hexdec(s) {
           85         return int("0x" s);
           86 }
           87 function xp(v, v2) {
           88         vs = ""
           89         for (i = 1; i < length(v); i += 2) {
           90                 vs = vs sprintf("%02x", xor(hexdec(substr(v, i, 2)), hexdec(substr(v2, i, 2))));
           91         }
           92         return vs;
           93 }
           94 {
           95         print xp(substr($0, 1, 16), substr($0, 33, 16)) xp(substr($0, 17, 16), substr($0, 49, 16));
           96 }
           97 ')
           98 
           99 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;
          100 json="${json#"[{"}"
          101 json="${json%"}]"}"
          102 file_url="${json##*'"g":'}"
          103 file_url="${file_url%%,*}"
          104 file_url="$(printf '%s' "$file_url" | sed 's@"@@g')"
          105 
          106 json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1
          107 at="${json##*'"at":'}"
          108 at="${at%%,*}"
          109 at="$(printf '%s' "$at" | sed 's@"@@g')"
          110 
          111 json=$(echo "${at}==" | tr '\-_' '+/' | tr -d ',' | \
          112         openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | \
          113         tr -d '\0')
          114 json="${json#"MEGA{"}"
          115 json="${json%"}"}"
          116 
          117 file_name="${json##*'"n":'}"
          118 if match "$file_name" ","; then
          119         file_name="${file_name%%,*}"
          120 fi
          121 file_name="$(printf '%s' "$file_name" | sed 's@"@@g')"
          122 
          123 echo "Response data:"
          124 echo "File url:  $file_url"
          125 echo "File name: $file_name"
          126 echo "Key (hex): $hex"
          127 echo "IV (hex):  ${raw_hex_sub}0000000000000000"
          128 
          129 # fetch raw data (encrypted).
          130 $CURL "$file_url" > "$FILENAME.enc"
          131 
          132 # decrypt
          133 openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex_sub}0000000000000000" < "$FILENAME.enc" > "$FILENAME"