private static void Main(string[] args) { var resolver = new AssemblyResolver(); resolver.EnableTypeDefCache = true; resolver.DefaultModuleContext = new ModuleContext(resolver); var module = ModuleDefMD.Load(args[0], resolver.DefaultModuleContext); if (Debug) { module.LoadPdb(); } var vr = new Virtualizer(100, Debug); vr.Initialize(ModuleDefMD.Load(args[1], resolver.DefaultModuleContext)); vr.AddModule(module); vr.ProcessMethods(module); var listener = vr.CommitModule(module); vr.CommitRuntime(); var dir = Path.GetDirectoryName(args[0]); vr.SaveRuntime(dir); module.Write(Path.Combine(dir, "Test.virtualized.exe"), new ModuleWriterOptions(module, listener)); if (Debug) { File.WriteAllBytes(Path.Combine(dir, "Test.virtualized.map"), vr.Runtime.DebugInfo); } }
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { Virtualizer vr = context.Annotations.Get <Virtualizer>(context, Fish.VirtualizerKey); ModuleDef merge = context.Annotations.Get <ModuleDef>(context, Fish.MergeKey); if (merge != null) { return; } string tmpDir = Path.GetTempPath(); string outDir = Path.Combine(tmpDir, Path.GetRandomFileName()); Directory.CreateDirectory(outDir); string rtPath = vr.SaveRuntime(outDir); if (context.Packer != null) { #if DEBUG string protRtPath = rtPath; #else var protRtPath = ProtectRT(context, rtPath); #endif context.ExternalModules.Add(File.ReadAllBytes(protRtPath)); foreach (Rule rule in context.Project.Rules) { rule.RemoveWhere(item => item.Id == this.Parent.Id); } } else { outDir = Path.GetDirectoryName(rtPath); foreach (string file in Directory.GetFiles(outDir)) { string path = Path.Combine(context.OutputDirectory, Path.GetFileName(file)); Directory.CreateDirectory(Path.GetDirectoryName(path)); File.Copy(file, path, true); } } }
public void InitializeP(ModuleDef module) { foreach (var typeDef in module.Types) { foreach (MethodDef methodDef in typeDef.Methods) { methods.Add(methodDef); } } var seed = new Random().Next(1, int.MaxValue); ModuleDef merge = module; string rtName = "KoiVM.Runtime"; ModuleDefMD rtModule = ModuleDefMD.Load("KoiVM.Runtime.dll"); rtModule.Assembly.Name = rtName; rtModule.Name = rtName + ".dll"; var vr = new Virtualizer(seed, false); vr.ExportDbgInfo = false; vr.DoStackWalk = false; vr.Initialize(rtModule); VirtualizerKey = vr; MergeKey = merge; vr.CommitRuntime(merge); var ctor = typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) }); if (methods.Count > 0) { var ca = new CustomAttribute((ICustomAttributeType)module.Import(ctor)); ca.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, vr.RuntimeModule.Assembly.Name.String)); module.Assembly.CustomAttributes.Add(ca); } MarkPhase(module); }
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { context.Logger.Log("Initializing DarksVM"); foreach (ModuleDefMD module in context.Modules) { context.Logger.LogFormat("Protecting '{0}' with DarksVM...", module.Name); } var random = context.Registry.GetService <IRandomService>(); var refProxy = context.Registry.GetService <IReferenceProxyService>(); var nameSrv = context.Registry.GetService <INameService>(); var seed = random.GetRandomGenerator(Parent.FullId).NextInt32(); string rtName = null; bool dbg = false, stackwalk = false; ModuleDef merge = null; foreach (var module in context.Modules) { if (rtName == null) { rtName = parameters.GetParameter <string>(context, module, "rtName"); } if (dbg == false) { dbg = parameters.GetParameter <bool>(context, module, "dbgInfo"); } if (stackwalk == false) { stackwalk = parameters.GetParameter <bool>(context, module, "stackwalk"); } merge = module; rtName = "Virtualization"; } rtName = rtName ?? "KoiVM.Runtime--test"; ModuleDefMD rtModule; var resStream = typeof(Virtualizer).Assembly.GetManifestResourceStream("KoiVM.Runtime.dll"); if (resStream != null) { rtModule = ModuleDefMD.Load(resStream, context.Resolver.DefaultModuleContext); } else { var rtPath = Path.Combine(koiDir, "KoiVM.Runtime.dll"); rtModule = ModuleDefMD.Load(rtPath, context.Resolver.DefaultModuleContext); } rtModule.Assembly.Name = rtName; rtModule.Name = rtName + ".dll"; var vr = new Virtualizer(seed, context.Project.Debug); vr.ExportDbgInfo = dbg; vr.DoStackWalk = stackwalk; vr.Initialize(rtModule); context.Annotations.Set(context, Fish.VirtualizerKey, vr); context.Annotations.Set(context, Fish.MergeKey, merge); if (merge != null) { var types = new List <TypeDef>(vr.RuntimeModule.GetTypes()); types.Remove(vr.RuntimeModule.GlobalType); vr.CommitRuntime(merge); foreach (var type in types) { foreach (var def in type.FindDefinitions()) { if (def is TypeDef && def != type) // nested type { continue; } nameSrv.SetCanRename(def, false); ProtectionParameters.SetParameters(context, def, new ProtectionSettings()); } } } else { vr.CommitRuntime(merge); } var ctor = typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) }); foreach (ModuleDef module in context.Modules) { var methods = new HashSet <MethodDef>(); foreach (var type in module.GetTypes()) { foreach (var method in type.Methods) { if (ProtectionParameters.GetParameters(context, method).ContainsKey(Parent)) { methods.Add(method); } } } if (methods.Count > 0) { var ca = new CustomAttribute((ICustomAttributeType)module.Import(ctor)); ca.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, vr.RuntimeModule.Assembly.Name.String)); module.Assembly.CustomAttributes.Add(ca); } foreach (var entry in new Scanner(module, methods).Scan().WithProgress(context.Logger)) { if (entry.Item2) { context.Annotations.Set(entry.Item1, Fish.ExportKey, Fish.ExportKey); } else { refProxy.ExcludeTarget(context, entry.Item1); } context.CheckCancellation(); } } }
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { Virtualizer vr = context.Annotations.Get <Virtualizer>(context, Fish.VirtualizerKey); IMarkerService marker = context.Registry.GetService <IMarkerService>(); IReferenceProxyService refProxy = context.Registry.GetService <IReferenceProxyService>(); IAntiTamperService antiTamper = context.Registry.GetService <IAntiTamperService>(); ICompressionService compression = context.Registry.GetService <ICompressionService>(); var methods = new HashSet <MethodDef>(parameters.Targets.OfType <MethodDef>()); var refRepl = new Dictionary <IMemberRef, IMemberRef>(); TypeDef oldType = context.CurrentModule.GlobalType; var newType = new TypeDefUser(oldType.Name); oldType.Namespace = "What_a_great_VM"; oldType.Name = "VM"; oldType.BaseType = context.CurrentModule.CorLibTypes.GetTypeRef("System", "Object"); context.CurrentModule.Types.Insert(0, newType); MethodDef old_cctor = oldType.FindOrCreateStaticConstructor(); MethodDef cctor = newType.FindOrCreateStaticConstructor(); old_cctor.Name = "Load"; old_cctor.IsRuntimeSpecialName = false; old_cctor.IsSpecialName = false; old_cctor.Access = MethodAttributes.PrivateScope; cctor.Body = new CilBody(true, new List <Instruction> { Instruction.Create(OpCodes.Call, old_cctor), Instruction.Create(OpCodes.Ret) }, new List <ExceptionHandler>(), new List <Local>()); marker.Mark(cctor, this.Parent); antiTamper.ExcludeMethod(context, cctor); for (int i = 0; i < oldType.Methods.Count; i++) { MethodDef nativeMethod = oldType.Methods[i]; if (nativeMethod.IsNative) { var methodStub = new MethodDefUser(nativeMethod.Name, nativeMethod.MethodSig.Clone()); methodStub.Attributes = MethodAttributes.Assembly | MethodAttributes.Static; methodStub.Body = new CilBody(); methodStub.Body.Instructions.Add(new Instruction(OpCodes.Jmp, nativeMethod)); methodStub.Body.Instructions.Add(new Instruction(OpCodes.Ret)); oldType.Methods[i] = methodStub; newType.Methods.Add(nativeMethod); refRepl[nativeMethod] = methodStub; marker.Mark(methodStub, this.Parent); antiTamper.ExcludeMethod(context, methodStub); } } compression.TryGetRuntimeDecompressor(context.CurrentModule, def => { if (def is MethodDef) { methods.Remove((MethodDef)def); } }); var toProcess = new Dictionary <ModuleDef, List <MethodDef> >(); foreach (System.Tuple <MethodDef, bool> entry in new Scanner(context.CurrentModule, methods).Scan().WithProgress(context.Logger)) { bool isExport = entry.Item2; isExport |= context.Annotations.Get <object>(entry.Item1, Fish.ExportKey) != null; isExport |= refProxy.IsTargeted(context, entry.Item1); if (!isExport) { antiTamper.ExcludeMethod(context, entry.Item1); } vr.AddMethod(entry.Item1, isExport); toProcess.AddListEntry(entry.Item1.Module, entry.Item1); context.CheckCancellation(); } context.CurrentModuleWriterListener.OnWriterEvent += new Listener { ctx = context, vr = vr, methods = toProcess, refRepl = refRepl }.OnWriterEvent; }
private static Action DeregisterVirtualizer(ConnectionFactory factory, Virtualizer processors) { return(() => { processors.Deregister(factory); }); }