cocus

tcopp min decrypter unpacker

Oct 15th, 2018
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class Program
  12.     {
  13.         static int getPackageCountFromRawBinary(byte[] internalData)
  14.         {
  15.             byte[] numArray = new byte[2];
  16.             Array.Copy((Array)internalData, 20, (Array)numArray, 0, 2);
  17.             return (int)BitConverter.ToUInt16(numArray, 0);
  18.         }
  19.  
  20.         static string calculateMD5ofByteArray(byte[] byte_0)
  21.         {
  22.             return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(byte_0)).Replace("-", "");
  23.         }
  24.  
  25.         static bool tcopp_sanity_check(byte[] tcopp)
  26.         {
  27.             string[] md5hashes = new string[16]
  28.             {
  29.               "13F817878913B3A22452D6274BC24D99",
  30.               "1AD1DBA1544F722D74729862BECE1726",
  31.               "940F2CC79DF835D5C9C18F860A297128",
  32.               "667D549AAF7112ED6BA41C278D587D02",
  33.               "09168B35EB7AD2D80EEAB9669608338A",
  34.               "3CE89D4D2CCA98645C573CDD5983AB6D",
  35.               "D82C24AF79C34439551E20C4FAB7B79E",
  36.               "D0EBAAF957F88A4BBD6314A7AF080759",
  37.               "0AE2781B988852DA5566E169F8DDBA30",
  38.               "FEEBC8437D791C98C413E02118AC33A0",
  39.               "26FE20E48A6E3D7E50F1882AB2727CEB",
  40.               "E2EA82BAC16FC70ADA2AF3283CA67F59",
  41.               "E5B7934744C928BC119B891CB9F8BC0A",
  42.               "0271C2AA4224CB140B8730FDD1DCDBAB",
  43.               "B4D8D2BFBE7A766B214D7B8857741A29",
  44.               "997F8E5876C4DBB5B6FD787C2B131E09"
  45.             };
  46.  
  47.             Console.WriteLine($" => Length = {tcopp.Length}");
  48.             Console.WriteLine($" => Type = {tcopp[0].ToString()}");
  49.  
  50.             if ((tcopp.Length != 266) &&
  51.                 (tcopp.Length != 541))
  52.             {
  53.                 Console.WriteLine($" => tcopp size not valid (size = {tcopp.Length})");
  54.                 return false;
  55.             }
  56.  
  57.             if ((tcopp[0] != 4) &&
  58.                 (tcopp[0] != 2))
  59.             {
  60.                 Console.WriteLine($" => header type not valid (type = {tcopp[0].ToString()})");
  61.                 return false;
  62.             }
  63.  
  64.             if (((tcopp.Length == 266) && (tcopp[0] != (byte)2)) ||
  65.                 ((tcopp.Length == 541) && (tcopp[0] != (byte)4)))
  66.             {
  67.                 Console.WriteLine($" => header doesn't match tcopp size (size = {tcopp.Length}, header = {tcopp[0].ToString()})");
  68.                 return false;
  69.             }
  70.  
  71.             byte[] magicConstantData = new byte[284];
  72.             Array.Copy(tcopp,                   /* Copy from this array */
  73.                        1,                       /* Copy from this offset */
  74.                        magicConstantData,       /* Copy to this array */
  75.                        0,                       /* Copy to this offset */
  76.                        magicConstantData.Length /* Max size of the target array */
  77.                        );
  78.  
  79.             string magicConstantDataMD5 = calculateMD5ofByteArray(magicConstantData);
  80.             Console.WriteLine($" => MagicConstantDataMD5 = {magicConstantDataMD5}");
  81.  
  82.             if (Array.IndexOf(md5hashes, magicConstantDataMD5) < 0)
  83.             {
  84.                 Console.WriteLine($" => MD5 not in hardcoded md5 list (MD5 = {magicConstantDataMD5})");
  85.                 return false;
  86.             }
  87.  
  88.             return true;
  89.         }
  90.  
  91.         static void Main(string[] args)
  92.         {
  93.             if (args.Length == 0)
  94.             {
  95.                 Console.WriteLine("Invoke this app with a .min file!");
  96.                 return;
  97.             }
  98.             string file = args[0];
  99.  
  100.             try
  101.             {
  102.                 byte[] rawMinData = File.ReadAllBytes(file);
  103.  
  104.                 byte[] rawDataWithoutHeader = new byte[rawMinData.Length - 22];
  105.                 Array.Copy((Array)rawMinData, 22, (Array)rawDataWithoutHeader, 0, rawMinData.Length - 22);
  106.  
  107.                 byte[] internalData;
  108.                 Dictionary<string, byte[]> dictionary_0;
  109.  
  110.                 RijndaelManaged rijndaelManaged = new RijndaelManaged();
  111.                 rijndaelManaged.Key = Encoding.UTF8.GetBytes("0619431931167B5466CA4256A2124504");
  112.                 rijndaelManaged.IV = Encoding.UTF8.GetBytes("3EEACFF1842BEB65");
  113.                 rijndaelManaged.Mode = CipherMode.CBC;
  114.                 ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
  115.                 MemoryStream memoryStream = new MemoryStream();
  116.                 CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Write);
  117.                 cryptoStream.Write(rawDataWithoutHeader, 0, rawDataWithoutHeader.Length);
  118.                 cryptoStream.FlushFinalBlock();
  119.                 File.WriteAllBytes($"{file}_decrypted_raw.bin", memoryStream.ToArray());
  120.  
  121.                 internalData = memoryStream.ToArray();
  122.                 dictionary_0 = new Dictionary<string, byte[]>();
  123.                 Console.WriteLine($"Package Count = {getPackageCountFromRawBinary(internalData)}");
  124.  
  125.                 if (getPackageCountFromRawBinary(internalData) == 0)
  126.                 {
  127.                     return;
  128.                 }
  129.  
  130.                 for (int index = 0; index < getPackageCountFromRawBinary(internalData); ++index)
  131.                 {
  132.                     /* Header of package */
  133.                     int sourceIndex1 = 22 + 20 * index + 541 * index;
  134.                     byte[] bytes = new byte[20];
  135.                     Array.Copy((Array)internalData, sourceIndex1, (Array)bytes, 0, 20);
  136.                     string pkgID = Encoding.ASCII.GetString(bytes).ToUpper();
  137.  
  138.                     /* Data of package */
  139.                     int sourceIndex2 = 22 + 20 * index + 541 * index + 20;
  140.                     byte[] pkgData = new byte[541];
  141.                     Array.Copy((Array)internalData, sourceIndex2, (Array)pkgData, 0, 541);
  142.  
  143.                     dictionary_0.Add(pkgID, pkgData);
  144.                     Console.WriteLine($"  #{index}, hashed_pkey_target = {pkgID}");
  145.                     File.WriteAllBytes($"{file}_decrypted_id_{index}_pkg_{pkgID}.bin", pkgData);
  146.                     //tcopp_sanity_check(numArray);
  147.                 }
  148.  
  149.                 if (getPackageCountFromRawBinary(internalData) == dictionary_0.Count)
  150.                 {
  151.                     Console.WriteLine("Count = PackageCount => Tudo bom, tudo legal");
  152.                     return;
  153.                 }
  154.                 Console.WriteLine("Count != PackageCount => invalid decrypted packages count!");
  155.                 return;
  156.             }
  157.             catch (Exception ex)
  158.             {
  159.                 Console.WriteLine($"Exception!\nInfo: {ex.ToString()}");
  160.             }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment