Ejemplo n.º 1
0
        public void KDF()
        {
            // Decrypt packet salt
            if (ecmID != prevEcmID)
            {
                byte[] saltHmacKey = sha1.ComputeHash(F4FOldMethod.AppendBuf(sessionKey, packetIV));
                if (debug)
                {
                    Program.DebugLog("SaltHmacKey  : " + Hexlify(saltHmacKey));
                }
                shaSalt.Key = saltHmacKey;
                saltAesKey  = F4FOldMethod.BlockCopy(shaSalt.ComputeHash(hmacData1), 0, 16);
                if (debug)
                {
                    Program.DebugLog("SaltAesKey   : " + Hexlify(saltAesKey));
                }
                prevEcmID = ecmID;
            }
            if (debug)
            {
                Program.DebugLog("EncryptedSalt: " + Hexlify(packetSalt));
            }

            byte[] decryptedSalt = AesDecrypt(packetSalt, saltAesKey, packetIV);
            if (decryptedSalt == null)
            {
                Program.Quit("<c:Red>Error ocurred while decription salt of fagment.");
            }
            if (debug)
            {
                Program.DebugLog("DecryptedSalt: " + Hexlify(decryptedSalt));
            }
            decryptBytes = F4FOldMethod.ReadInt32(ref decryptedSalt, 0);
            if (debug)
            {
                Program.DebugLog("DecryptBytes : " + decryptBytes);
            }
            byte[] decryptedSalt2 = F4FOldMethod.BlockCopy(decryptedSalt, 4, 16);
            if (debug)
            {
                Program.DebugLog("DecryptedSalt: " + Hexlify(decryptedSalt2));
            }
            // Generate final packet decryption key
            byte[] finalHmacKey = sha1.ComputeHash(decryptedSalt2);
            if (debug)
            {
                Program.DebugLog("FinalHmacKey : " + Hexlify(finalHmacKey));
            }
            finalsha.Key = finalHmacKey;
            packetKey    = F4FOldMethod.BlockCopy(finalsha.ComputeHash(hmacData2), 0, 16);
            if (debug)
            {
                Program.DebugLog("PacketKey    : " + Hexlify(packetKey));
            }
        }
Ejemplo n.º 2
0
        public byte[] Decrypt(byte[] data, long pos, string baseUrl, string auth)
        {
            if (debug)
            {
                Program.DebugLog("\n----- Akamai Decryption Start -----");
            }
            byte[] decryptedData = new byte[0];
            // Parse packet header
            using (MemoryStream ms = new MemoryStream(data)) {
                ms.Position = pos;
                using (HDSBinaryReader br = new HDSBinaryReader(ms)) {
                    int b = br.ReadByte();
                    ecmVersion = (b >> 4);
                    if (ecmVersion != 11)
                    {
                        ecmVersion = b;
                    }
                    ecmID          = br.ReadInt32();
                    ecmTimestamp   = (uint)br.ReadInt32();
                    kdfVersion     = br.ReadInt16();
                    dccAccReserved = br.ReadByte();

                    if (debug)
                    {
                        Program.DebugLog("ECM Version  : " + ecmVersion + ", ECM ID: " + ecmID + ", ECM Timestamp: " + ecmTimestamp + ", KDF Version: " + kdfVersion + ", DccAccReserved: " + dccAccReserved);
                    }
                    b = br.ReadByte();

                    bool iv  = ((b & 2) > 0);
                    bool key = ((b & 4) > 0);

                    if (iv)
                    {
                        packetIV = br.ReadBytes(16);
                        if (debug)
                        {
                            Program.DebugLog("PacketIV     : " + Hexlify(packetIV));
                        }
                    }

                    if (key)
                    {
                        sessionKeyUrl = br.ReadString();
                        if (debug)
                        {
                            Program.DebugLog("SessionKeyUrl: " + sessionKeyUrl);
                        }
                        string keyPath = sessionKeyUrl.Substring(sessionKeyUrl.LastIndexOf('/'));
                        string keyUrl  = HTTP.JoinUrl(baseUrl, keyPath) + auth;

                        // Download key file if required
                        if (sessionKeyUrl != lastKeyUrl)
                        {
                            if ((baseUrl.Length == 0) && (sessionKey.Length == 0))
                            {
                                if (debug)
                                {
                                    Program.DebugLog("Unable to download session key without manifest url. you must specify it manually using 'adkey' switch.");
                                }
                            }
                            else
                            {
                                if (baseUrl.Length > 0)
                                {
                                    if (debug)
                                    {
                                        Program.DebugLog("Downloading new session key from " + keyUrl);
                                    }
                                    byte[] downloadedData = HTTP.TryGETData(keyUrl, out int retCode, out string status);
                                    if (retCode == 200)
                                    {
                                        sessionID  = "_" + keyPath.Substring("/key_".Length);
                                        sessionKey = downloadedData;
                                    }
                                    else
                                    {
                                        if (debug)
                                        {
                                            Program.DebugLog("Failed to download new session key, Status: " + status + " (" + retCode + ")");
                                        }
                                        sessionID = "";
                                    }
                                }
                            }
                            lastKeyUrl = sessionKeyUrl;
                            if (sessionKey == null || sessionKey.Length == 0)
                            {
                                Program.Quit("Failed to download akamai session decryption key");
                            }
                        }
                    }

                    if (debug)
                    {
                        Program.DebugLog("SessionKey   : " + Hexlify(sessionKey));
                    }

                    if (sessionKey == null || sessionKey.Length < 1)
                    {
                        Program.Quit("ERROR: Fragments can't be decrypted properly without corresponding session key.");
                    }

                    byte reserved; byte[] reservedBlock1, reservedBlock2, encryptedData, lastBlockData;

                    reserved       = br.ReadByte();
                    packetSalt     = br.ReadBytes(32);
                    reservedBlock1 = br.ReadBytes(20);
                    reservedBlock2 = br.ReadBytes(20);
                    if (debug)
                    {
                        Program.DebugLog("ReservedByte : " + reserved + ", ReservedBlock1: " + Hexlify(reservedBlock1) + ", ReservedBlock2: " + Hexlify(reservedBlock2));
                    }

                    // Generate packet decryption key
                    KDF();

                    // Decrypt packet data
                    encryptedData = br.ReadBytes((int)decryptBytes);
                    lastBlockData = br.ReadToEnd();
                    if (decryptBytes > 0)
                    {
                        decryptedData = AesDecrypt(encryptedData, packetKey, packetIV);
                    }
                    decryptedData = F4FOldMethod.AppendBuf(decryptedData, lastBlockData);
                    if (debug)
                    {
                        Program.DebugLog("EncryptedData: " + Hexlify(encryptedData, 64));
                        Program.DebugLog("DecryptedData: " + Hexlify(decryptedData, 64));
                        Program.DebugLog("----- Akamai Decryption End -----\n");
                    }
                } // using (HDSBinaryReader br = new HDSBinaryReader(ms))
            }     // using (MemoryStream ms = new MemoryStream(data))

            return(decryptedData);
        }