Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using dnlib.DotNet;
  2. using dnlib.DotNet.Emit;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. namespace Unchanger.Obfuscation
  7. {
  8.     public static class Integrity
  9.     {
  10.         public static void Protect(AssemblyDef assembly)
  11.         {
  12.             foreach (ModuleDef module in assembly.Modules)
  13.             {
  14.                 Protect(module);
  15.             }
  16.         }
  17.  
  18.         public static void Protect(ModuleDef module)
  19.         {
  20.             foreach (TypeDef type in module.Types)
  21.             {
  22.                 Protect(type);
  23.             }
  24.         }
  25.  
  26.         public static void Protect(TypeDef type)
  27.         {
  28.             if (type.IsRuntimeSpecialName || type.IsWindowsRuntime)
  29.                 return;
  30.  
  31.             Importer importer = new Importer(type.Module);
  32.             ITypeDefOrRef intRef = importer.Import(typeof(Int32));
  33.             FieldDef fieldInjectedArray = new FieldDefUser("bla", new FieldSig(intRef.ToTypeSig()), FieldAttributes.Static | FieldAttributes.Public);
  34.             type.Fields.Add(fieldInjectedArray);
  35.  
  36.             int total = 0;
  37.             foreach (MethodDef method in type.Methods)
  38.             {
  39.                 Protect(method, fieldInjectedArray,ref  total);
  40.             }
  41.         }
  42.  
  43.         public static int Protect(MethodDef method, FieldDef field, ref int total)
  44.         {
  45.             if (method == null)
  46.                 return 0;
  47.  
  48.             if (!method.HasBody)
  49.                 return 0;
  50.  
  51.             if (!method.Body.HasInstructions)
  52.                 return 0;
  53.  
  54.             // todo: we can't possibly know the order of the methods executed, so I guess we'll have to
  55.             // use a separate integer for each method.....
  56.  
  57.             IList<Instruction> instructions = method.Body.Instructions;
  58.             for (int i = 0; i < instructions.Count; i++)
  59.             {
  60.                 if (instructions[i].OpCode == OpCodes.Call)
  61.                 {
  62.                     MethodDef target = instructions[i].Operand as MethodDef;
  63.                     if (target != method)
  64.                     {
  65.                         // todo: Check if any changes were made, because the number may really be 0
  66.                         int newValue = Protect(target, field, ref total);
  67.                         if (newValue != 0)
  68.                             total = newValue;
  69.                     }
  70.                 }
  71.                 else if (instructions[i].OpCode == OpCodes.Ldc_I4)
  72.                 {
  73.                     int value = (int)instructions[i].Operand;
  74.                     int difference = total - value;
  75.  
  76.                     instructions[i] = new Instruction(OpCodes.Ldsfld, field);
  77.  
  78.                     // make sure we cant go out of bounds, "clamp" at 0
  79.                     int newIndex = 0;
  80.                     if (i == 0)
  81.                     {
  82.                         newIndex = 0;
  83.                     }
  84.                     else
  85.                     {
  86.                         newIndex = i - 1;
  87.                     }
  88.  
  89.                     instructions.Insert(newIndex, new Instruction(OpCodes.Stsfld, field));
  90.                     if (difference < 0)
  91.                     {
  92.                         instructions.Insert(newIndex, new Instruction(OpCodes.Add));
  93.                     }
  94.                     else
  95.                     {
  96.                         instructions.Insert(newIndex, new Instruction(OpCodes.Sub));
  97.                     }
  98.                     instructions.Insert(newIndex, new Instruction(OpCodes.Ldc_I4, Math.Abs(difference)));
  99.                     instructions.Insert(newIndex, new Instruction(OpCodes.Ldsfld, field));
  100.  
  101.                     total = value;
  102.  
  103.                     i += 4; // collection size changed
  104.                 }
  105.             }
  106.  
  107.             return total;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement