Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using dnlib.DotNet;
- using dnlib.DotNet.Emit;
- using System;
- using System.Linq;
- // Load the target assembly/module
- ModuleDefMD module = ModuleDefMD.Load("target.dll");
- // Define a global variable to store the opaque predicate result
- FieldDef resultField = new FieldDefUser("result", new FieldSig(module.CorLibTypes.Boolean), FieldAttributes.Static | FieldAttributes.Private);
- // Define a method to create an opaque predicate
- private static void CreateOpaquePredicate(MethodDef method)
- {
- // Create a new local variable to store the result of the comparison
- Local resultVar = new Local(method.Module.CorLibTypes.Boolean);
- method.Body.Variables.Add(resultVar);
- // Create a new label to jump to if the comparison fails
- Instruction target = Instruction.Create(OpCodes.Nop);
- // Create a new instruction to load a random value on the stack
- Instruction randomValue = Instruction.Create(OpCodes.Ldc_I4, new Random().Next());
- // Create a new instruction to load another random value on the stack
- Instruction anotherRandomValue = Instruction.Create(OpCodes.Ldc_I4, new Random().Next());
- // Create a new instruction to compare the two random values
- Instruction compare = Instruction.Create(OpCodes.Ceq);
- // Create a new instruction to store the result of the comparison in the local variable
- Instruction storeResult = Instruction.Create(OpCodes.Stloc, resultVar);
- // Create a new instruction to load the local variable on the stack
- Instruction loadResult = Instruction.Create(OpCodes.Ldloc, resultVar);
- // Create a new instruction to branch to the target label if the result of the comparison is false
- Instruction branchFalse = Instruction.Create(OpCodes.Brfalse, target);
- // Insert the new instructions in the method body
- method.Body.Instructions.Insert(0, randomValue);
- method.Body.Instructions.Insert(1, anotherRandomValue);
- method.Body.Instructions.Insert(2, compare);
- method.Body.Instructions.Insert(3, storeResult);
- method.Body.Instructions.Insert(4, loadResult);
- method.Body.Instructions.Insert(5, branchFalse);
- // Insert the target label at the end of the method body
- method.Body.Instructions.Add(target);
- // Update the global variable with the result of the opaque predicate
- method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, resultVar));
- method.Body.Instructions.Add(Instruction.Create(OpCodes.Stsfld, resultField));
- }
- // Iterate through all the types in the module
- foreach (TypeDef type in module.Types)
- {
- // Iterate through all the methods in the type
- foreach (MethodDef method in type.Methods)
- {
- // Create an opaque predicate in the method
- CreateOpaquePredicate(method);
- }
- }
- // Iterate through all the methods in the module again
- foreach (TypeDef type in module.Types)
- {
- foreach (MethodDef method in type.Methods)
- {
- // Create a new label to jump to if the global variable is true
- Instruction target = Instruction.Create(OpCodes.Nop);
- // Create a new instruction to load the global variable on the stack
- Instruction loadResult = Instruction.Create(OpCodes.Ldsfld, resultField);
- // Create a new instruction to branch to the target label if the global variable is true
- Instruction branchTrue = Instruction.Create(OpCodes.Brtrue, target);
- // Insert the new instructions at the beginning of the method body
- method.Body.Instructions.Insert(0, loadResult);
- method.Body.Instructions.Insert(1, branchTrue);
- // Insert the target label
- method.Body.Instructions.Insert(2, target);
- method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
- }
- }
- // Save the obfuscated module to disk
- module.Write("obfuscated.dll");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement