Advertisement
EddyCZ

Untitled

Apr 11th, 2023
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using dnlib.DotNet;
  2. using dnlib.DotNet.Emit;
  3. using System;
  4. using System.Linq;
  5.  
  6. // Load the target assembly/module
  7. ModuleDefMD module = ModuleDefMD.Load("target.dll");
  8.  
  9. // Define a global variable to store the opaque predicate result
  10. FieldDef resultField = new FieldDefUser("result", new FieldSig(module.CorLibTypes.Boolean), FieldAttributes.Static | FieldAttributes.Private);
  11.  
  12. // Define a method to create an opaque predicate
  13. private static void CreateOpaquePredicate(MethodDef method)
  14. {
  15.     // Create a new local variable to store the result of the comparison
  16.     Local resultVar = new Local(method.Module.CorLibTypes.Boolean);
  17.     method.Body.Variables.Add(resultVar);
  18.  
  19.     // Create a new label to jump to if the comparison fails
  20.     Instruction target = Instruction.Create(OpCodes.Nop);
  21.  
  22.     // Create a new instruction to load a random value on the stack
  23.     Instruction randomValue = Instruction.Create(OpCodes.Ldc_I4, new Random().Next());
  24.  
  25.     // Create a new instruction to load another random value on the stack
  26.     Instruction anotherRandomValue = Instruction.Create(OpCodes.Ldc_I4, new Random().Next());
  27.  
  28.     // Create a new instruction to compare the two random values
  29.     Instruction compare = Instruction.Create(OpCodes.Ceq);
  30.  
  31.     // Create a new instruction to store the result of the comparison in the local variable
  32.     Instruction storeResult = Instruction.Create(OpCodes.Stloc, resultVar);
  33.  
  34.     // Create a new instruction to load the local variable on the stack
  35.     Instruction loadResult = Instruction.Create(OpCodes.Ldloc, resultVar);
  36.  
  37.     // Create a new instruction to branch to the target label if the result of the comparison is false
  38.     Instruction branchFalse = Instruction.Create(OpCodes.Brfalse, target);
  39.  
  40.     // Insert the new instructions in the method body
  41.     method.Body.Instructions.Insert(0, randomValue);
  42.     method.Body.Instructions.Insert(1, anotherRandomValue);
  43.     method.Body.Instructions.Insert(2, compare);
  44.     method.Body.Instructions.Insert(3, storeResult);
  45.     method.Body.Instructions.Insert(4, loadResult);
  46.     method.Body.Instructions.Insert(5, branchFalse);
  47.  
  48.     // Insert the target label at the end of the method body
  49.     method.Body.Instructions.Add(target);
  50.  
  51.     // Update the global variable with the result of the opaque predicate
  52.     method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, resultVar));
  53.     method.Body.Instructions.Add(Instruction.Create(OpCodes.Stsfld, resultField));
  54. }
  55.  
  56. // Iterate through all the types in the module
  57. foreach (TypeDef type in module.Types)
  58. {
  59.     // Iterate through all the methods in the type
  60.     foreach (MethodDef method in type.Methods)
  61.     {
  62.         // Create an opaque predicate in the method
  63.         CreateOpaquePredicate(method);
  64.     }
  65. }
  66.  
  67. // Iterate through all the methods in the module again
  68. foreach (TypeDef type in module.Types)
  69. {
  70.     foreach (MethodDef method in type.Methods)
  71.     {
  72.         // Create a new label to jump to if the global variable is true
  73.         Instruction target = Instruction.Create(OpCodes.Nop);
  74.  
  75.         // Create a new instruction to load the global variable on the stack
  76.         Instruction loadResult = Instruction.Create(OpCodes.Ldsfld, resultField);
  77.  
  78.         // Create a new instruction to branch to the target label if the global variable is true
  79.         Instruction branchTrue = Instruction.Create(OpCodes.Brtrue, target);
  80.  
  81.         // Insert the new instructions at the beginning of the method body
  82.         method.Body.Instructions.Insert(0, loadResult);
  83.         method.Body.Instructions.Insert(1, branchTrue);
  84.  
  85.         // Insert the target label
  86.         method.Body.Instructions.Insert(2, target);
  87.         method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
  88.     }
  89. }
  90.  
  91. // Save the obfuscated module to disk
  92. module.Write("obfuscated.dll");
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement