Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Text;
  6. using Mono.Cecil;
  7. using Mono.Cecil.Cil;
  8.  
  9. namespace Confuse
  10. {
  11.     class Program
  12.     {
  13.  
  14.         private static AssemblyDefinition assembly;
  15.         private static BinaryReader binaryReader;
  16.         private static uint key1;
  17.         private static uint key2;
  18.         private static uint key3;
  19.         private static uint key4;
  20.         private static uint switch1;
  21.         private static uint switch2;
  22.         private static uint switch3;
  23.         private static uint switch4;
  24.         private static uint switch5;
  25.  
  26.         private static void doConstants(AssemblyDefinition assembly)
  27.         {
  28.             getDecryptionConstants(getConstantMethod());
  29.             getSwitchConstants(getConstantMethod());
  30.             MemoryStream s = new MemoryStream();
  31.             using (DeflateStream str = new DeflateStream(getConstantResourceStream(assembly, getConstantResourceName()), CompressionMode.Decompress))
  32.             {
  33.                 byte[] dat = new byte[0x1000];
  34.                 int read = str.Read(dat, 0, 0x1000);
  35.                 do
  36.                 {
  37.                     s.Write(dat, 0, read);
  38.                     read = str.Read(dat, 0, 0x1000);
  39.                 }
  40.                 while (read != 0);
  41.             }
  42.             binaryReader = new BinaryReader(s);
  43.  
  44.             foreach (TypeDefinition type in assembly.MainModule.GetTypes())
  45.             {
  46.                 foreach (MethodDefinition method in type.Methods)
  47.                 {
  48.                     if (method.HasBody && !method.IsConstructor)
  49.                     {
  50.                         for (int j = 0; j < method.Body.Instructions.Count; j++)
  51.                         {
  52.                             Console.WriteLine("Operand: " + method.Body.Instructions[j].Operand.ToString());
  53.                             if (method.Body.Instructions[j].Operand.Equals(getConstantMethod()))
  54.                             {
  55.                                 if (method.Body.Instructions[j].Previous.Operand != null)
  56.                                 {
  57.                                     Instruction i = method.Body.Instructions[j];
  58.                                     try
  59.                                     {
  60.                                         object decrypted = decryptConstant((uint)(int)i.Previous.Operand, method.MetadataToken.ToUInt32());
  61.                                         Console.WriteLine("Constant Decrypted: " + i.Previous.Operand.ToString() + " > [" + decrypted.ToString() + "]");
  62.                                     }
  63.                                     catch (Exception e)
  64.                                     {
  65.                                         Console.WriteLine(e.Message);
  66.                                     }
  67.                                 }
  68.                             }
  69.                         }
  70.                     }
  71.                     //foreach (Instruction i in method.Body.Instructions)
  72.                     //{
  73.                     //MethodDefinition decMethod = getConstantMethod();
  74.  
  75.                     //if (!i.OpCode.Equals(OpCodes.Call))
  76.                     //    continue;
  77.  
  78.                     //if (!i.Operand.Equals(decMethod))
  79.                     //    continue;
  80.  
  81.                     //if (i.Previous.Operand == null && method.MetadataToken == null)
  82.                     //    continue;
  83.                     //string key = i.Previous.Operand.ToString();
  84.                     //int meta = (int)method.MetadataToken.ToUInt32();
  85.  
  86.                     //Console.WriteLine(method.Name + " | " + key + " :: " + meta.ToString());
  87.  
  88.                     //ILProcessor ilProc = method.Body.GetILProcessor();
  89.                     //object decrypted = decryptConstant((uint)(int)i.Previous.Operand, method.MetadataToken.ToUInt32());
  90.                     //Console.WriteLine("Constant Decrypted: " + i.Previous.Operand.ToString() + " > [" + decrypted.ToString() + "]");
  91.                     //ilProc.Replace(i.Next, ilProc.Create(OpCodes.Nop));
  92.                     //ilProc.Replace(i.Previous, ilProc.Create(OpCodes.Nop));
  93.                     //ilProc.Replace(i, ilProc.Create(OpCodes.Ldstr, decrypted.ToString()));
  94.                     //}
  95.                     //cleanMethod(method);
  96.                 }
  97.             }
  98.         }
  99.         private static object decryptConstant(uint key, uint meta)
  100.         {
  101.             object ret = null;
  102.             ret = decryptConstant(key, meta, key1, key2, key3, key4, switch1, switch2, switch3, switch4, switch5);
  103.             return ret;
  104.         }
  105.         private static object decryptConstant(uint key, uint meta, uint key1, uint key2, uint key3, uint key4, uint s1, uint s2, uint s3, uint s4, uint s5)
  106.         {
  107.             object ret = null;
  108.             uint hash = ComputeHash(meta, key4, key1, key2, key3) ^ key;
  109.  
  110.             if ((int)hash != 0)
  111.             {
  112.                 Console.WriteLine("HASH: " + hash);
  113.                 binaryReader.BaseStream.Seek((long)hash, SeekOrigin.Begin);
  114.                 byte num9 = binaryReader.ReadByte();
  115.                 byte[] bytes = binaryReader.ReadBytes(binaryReader.ReadInt32());
  116.                 Random random = new Random((int)(key4 ^ hash));
  117.                 byte[] buffer3 = new byte[bytes.Length];
  118.                 random.NextBytes(buffer3);
  119.                 BitArray array = new BitArray(bytes);
  120.                 array.Xor(new BitArray(buffer3));
  121.                 array.CopyTo(bytes, 0);
  122.                 if (num9 == s1)
  123.                 {
  124.                     ret = BitConverter.ToDouble(bytes, 0);
  125.                 }
  126.                 else
  127.                 {
  128.                     if (num9 == s2)
  129.                     {
  130.                         ret = BitConverter.ToSingle(bytes, 0);
  131.                     }
  132.                     else
  133.                     {
  134.                         if (num9 == s3)
  135.                         {
  136.                             ret = BitConverter.ToInt32(bytes, 0);
  137.                         }
  138.                         else
  139.                         {
  140.                             if (num9 == s4)
  141.                             {
  142.                                 ret = BitConverter.ToInt64(bytes, 0);
  143.                             }
  144.                             else
  145.                             {
  146.                                 if (num9 == s5)
  147.                                 {
  148.                                     ret = Encoding.UTF8.GetString(bytes);
  149.                                 }
  150.                             }
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             else
  156.             {
  157.                 return null;
  158.             }
  159.             return ret;
  160.         }
  161.         private static uint ComputeHash(uint x, uint key, uint init0, uint init1, uint init2)
  162.         {
  163.             uint h = init0 ^ x;
  164.             uint h1 = init1;
  165.             uint h2 = init2;
  166.             for (uint i = 1; i <= 64; i++)
  167.             {
  168.                 h = (h & 0x00ff) << 8 | ((h & 0xff00) >> 24);
  169.                 uint n = (h & 0xff) % 64;
  170.                 if (n >= 0 && n < 16)
  171.                 {
  172.                     h1 |= (((h & 0x0000ff00) >> 8) & ((h & 0x00ff0000) >> 16)) ^ (~h & 0x00ff);
  173.                     h2 ^= (h * i + 1) % 16;
  174.                     h += (h1 | h2) ^ key;
  175.                 }
  176.                 else if (n >= 16 && n < 32)
  177.                 {
  178.                     h1 ^= ((h & 0x00ff00ff) << 8) ^ (((h & 0x00ffff00) >> 8) | (~h & 0x0000ffff));
  179.                     h2 += (h * i) % 32;
  180.                     h |= (h1 + ~h2) & key;
  181.                 }
  182.                 else if (n >= 32 && n < 48)
  183.                 {
  184.                     h1 += ((h & 0x00ff) | ((h & 0x00ff0000) >> 16)) + (~h & 0x00ff);
  185.                     h2 -= ~(h + n) % 48;
  186.                     h ^= (h1 % h2) | key;
  187.                 }
  188.                 else if (n >= 48 && n < 64)
  189.                 {
  190.                     h1 ^= (((h & 0x00ff0000) >> 16) | ~(h & 0x0000ff)) * (~h & 0x00ff0000);
  191.                     h2 += (h ^ i - 1) % n;
  192.                     h -= ~(h1 ^ h2) + key;
  193.                 }
  194.             }
  195.             return h;
  196.         }
  197.         private static uint getKeyFromOpCode(OpCode op)
  198.         {
  199.             if (op.Equals(OpCodes.Ldc_I4_0))
  200.                 return (uint)0;
  201.             if (op.Equals(OpCodes.Ldc_I4_1))
  202.                 return (uint)1;
  203.             if (op.Equals(OpCodes.Ldc_I4_2))
  204.                 return (uint)2;
  205.             if (op.Equals(OpCodes.Ldc_I4_3))
  206.                 return (uint)3;
  207.             if (op.Equals(OpCodes.Ldc_I4_4))
  208.                 return (uint)4;
  209.             if (op.Equals(OpCodes.Ldc_I4_5))
  210.                 return (uint)5;
  211.             if (op.Equals(OpCodes.Ldc_I4_6))
  212.                 return (uint)6;
  213.             if (op.Equals(OpCodes.Ldc_I4_7))
  214.                 return (uint)7;
  215.             if (op.Equals(OpCodes.Ldc_I4_8))
  216.                 return (uint)8;
  217.             return (uint)0;
  218.         }
  219.         private static void getSwitchConstants(MethodDefinition method)
  220.         {
  221.             uint s1;
  222.             uint s2;
  223.             uint s3;
  224.             uint s4;
  225.             uint s5;
  226.             if (method.Body.Instructions[325].Operand != null)
  227.             {
  228.                 s1 = Convert.ToUInt32(method.Body.Instructions[325].Operand.ToString());
  229.             }
  230.             else
  231.             {
  232.                 s1 = getKeyFromOpCode(method.Body.Instructions[325].OpCode);
  233.             }
  234.  
  235.             if (method.Body.Instructions[334].Operand != null)
  236.             {
  237.                 s2 = Convert.ToUInt32(method.Body.Instructions[334].Operand.ToString());
  238.             }
  239.             else
  240.             {
  241.                 s2 = getKeyFromOpCode(method.Body.Instructions[334].OpCode);
  242.             }
  243.  
  244.             if (method.Body.Instructions[343].Operand != null)
  245.             {
  246.                 s3 = Convert.ToUInt32(method.Body.Instructions[343].Operand.ToString());
  247.             }
  248.             else
  249.             {
  250.                 s3 = getKeyFromOpCode(method.Body.Instructions[343].OpCode);
  251.             }
  252.  
  253.             if (method.Body.Instructions[352].Operand != null)
  254.             {
  255.                 s4 = Convert.ToUInt32(method.Body.Instructions[352].Operand.ToString());
  256.             }
  257.             else
  258.             {
  259.                 s4 = getKeyFromOpCode(method.Body.Instructions[352].OpCode);
  260.             }
  261.  
  262.             if (method.Body.Instructions[361].Operand != null)
  263.             {
  264.                 s5 = Convert.ToUInt32(method.Body.Instructions[361].Operand.ToString());
  265.             }
  266.             else
  267.             {
  268.                 s5 = getKeyFromOpCode(method.Body.Instructions[361].OpCode);
  269.             }
  270.             Console.WriteLine("Switch 1: [" + s1.ToString() + "]");
  271.             Console.WriteLine("Switch 2: [" + s2.ToString() + "]");
  272.             Console.WriteLine("Switch 3: [" + s3.ToString() + "]");
  273.             Console.WriteLine("Switch 4: [" + s4.ToString() + "]");
  274.             Console.WriteLine("Switch 5: [" + s5.ToString() + "]");
  275.         }
  276.         private static void getDecryptionConstants(MethodDefinition method)
  277.         {
  278.             uint k1 = Convert.ToUInt32(method.Body.Instructions[61].Operand.ToString());
  279.             uint k2 = Convert.ToUInt32(method.Body.Instructions[65].Operand.ToString());
  280.             uint k3 = Convert.ToUInt32(method.Body.Instructions[67].Operand.ToString());
  281.             uint k4 = Convert.ToUInt32(method.Body.Instructions[129].Operand.ToString());
  282.             //uint s1 = Convert.ToUInt32(method.Body.Instructions[325].Operand.ToString());
  283.             //uint s2 = Convert.ToUInt32(method.Body.Instructions[334].Operand.ToString());
  284.             //uint s3 = Convert.ToUInt32(method.Body.Instructions[343].Operand.ToString());
  285.             //uint s4 = Convert.ToUInt32(method.Body.Instructions[352].Operand.ToString());
  286.             //uint s5 = Convert.ToUInt32(method.Body.Instructions[361].Operand.ToString());
  287.  
  288.             key1 = k1;
  289.             key2 = k2;
  290.             key3 = k3;
  291.             key4 = k4;
  292.             //switch1 = s1;
  293.             //switch2 = s2;
  294.             //switch3 = s3;
  295.             //switch4 = s4;
  296.             //switch5 = s5;
  297.  
  298.             Console.WriteLine("key1: " + k1.ToString());
  299.             Console.WriteLine("key2: " + k2.ToString());
  300.             Console.WriteLine("key3: " + k3.ToString());
  301.             Console.WriteLine("key4: " + k4.ToString());
  302.             //Console.WriteLine("Switch 1: " + s1.ToString());
  303.             //Console.WriteLine("Switch 2: " + s2.ToString());
  304.             //Console.WriteLine("Switch 3: " + s3.ToString());
  305.             //Console.WriteLine("Switch 4: " + s4.ToString());
  306.             //Console.WriteLine("Switch 5: " + s5.ToString());
  307.         }
  308.         private static MemoryStream getConstantResourceStream(AssemblyDefinition assembly, string resName)
  309.         {
  310.             foreach (EmbeddedResource res in assembly.MainModule.Resources)
  311.             {
  312.                 if (res.Name.Equals(resName))
  313.                     return (MemoryStream)res.GetResourceStream();
  314.             }
  315.             return null;
  316.         }
  317.         private static string getConstantResourceName()
  318.         {
  319.             MethodDefinition method = getConstantMethod();
  320.             return method.Body.Instructions[1].Operand.ToString();
  321.         }
  322.         private static MethodDefinition getConstantMethod()
  323.         {
  324.             try
  325.             {
  326.                 foreach (TypeDefinition type in assembly.MainModule.GetTypes())
  327.                 {
  328.  
  329.                     if (!type.Name.Equals("<Module>"))
  330.                         continue;
  331.  
  332.                     foreach (MethodDefinition method in type.Methods)
  333.                     {
  334.                         foreach (ParameterDefinition param in method.Parameters)
  335.                         {
  336.                             if (param.ParameterType.Name.Equals("UInt32"))
  337.                                 return method;
  338.                         }
  339.                     }
  340.                 }
  341.             }
  342.             catch (Exception e)
  343.             {
  344.                 Console.WriteLine(e.Message);
  345.             }
  346.             return null;
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement