/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (with key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="identityPubKey">Identity public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey, StrongNameKey identityKey, StrongNamePublicKey identityPubKey) { StrongNameKey = signatureKey; StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; StrongNamePublicKey = identityPubKey; if (module.Assembly != null) module.Assembly.UpdateOrCreateAssemblySignatureKeyAttribute(identityPubKey, identityKey, signaturePubKey); }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for normal strong name signing. /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> public void InitializeStrongNameSigning(ModuleDef module, StrongNameKey signatureKey) { StrongNameKey = signatureKey; StrongNamePublicKey = null; if (module.Assembly != null) module.Assembly.CustomAttributes.RemoveAll("System.Reflection.AssemblySignatureKeyAttribute"); }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (without key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey) { InitializeStrongNameSigning(module, signatureKey); StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; }
public RTMarker(StrongNameKey snKey) => this.snKey = snKey;
private string ProtectRT(ConfuserContext context, string fileName) { string outDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(outDir); var proj = new ConfuserProject { Seed = context.Project.Seed, Debug = context.Project.Debug }; //foreach (Rule rule in context.Project.Rules) { // var r = rule.Clone(); // r.RemoveWhere(prot => prot.Id == Parent.Id); // proj.Rules.Add(r); //} proj.Rules.Add(new Rule { new SettingItem <Protection>("anti ildasm"), new SettingItem <Protection>("ref proxy") { { "mode", "mild" }, { "typeErasure", "true" } }, new SettingItem <Protection>("rename") { { "mode", "repeating" } } }); proj.Add(new ProjectModule { Path = fileName }); proj.BaseDirectory = Path.GetDirectoryName(fileName); proj.OutputDirectory = outDir; foreach (string path in context.Project.ProbePaths) { proj.ProbePaths.Add(path); } proj.ProbePaths.Add(context.Project.BaseDirectory); StrongNameKey snKey = null; foreach (ModuleDefMD module in context.Modules) { snKey = context.Annotations.Get <StrongNameKey>(module, Marker.SNKey); if (snKey != null) { break; } } try { ConfuserEngine.Run(new ConfuserParameters { Logger = new RTLogger(context.Logger), Marker = new RTMarker(snKey), Project = proj }, context.CancellationToken).Wait(); } catch (AggregateException ex) { context.Logger.Error("Failed to protect Runtime."); throw new ConfuserException(ex); } return(Path.Combine(outDir, Path.GetFileName(fileName))); }
public PackerMarker(StrongNameKey snKey) { this.snKey = snKey; }
/// <summary> /// Protects the stub using original project settings replace the current output with the protected stub. /// </summary> /// <param name="context">The working context.</param> /// <param name="fileName">The result file name.</param> /// <param name="module">The stub module.</param> /// <param name="snKey">The strong name key.</param> /// <param name="prot">The packer protection that applies to the stub.</param> protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null) { string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { string outDir = Path.Combine(tmpDir, Path.GetRandomFileName()); Directory.CreateDirectory(tmpDir); for (int i = 0; i < context.OutputModules.Count; i++) { string path = Path.GetFullPath(Path.Combine(tmpDir, context.OutputPaths[i])); var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllBytes(path, context.OutputModules[i]); } File.WriteAllBytes(Path.Combine(tmpDir, fileName), module); var proj = new ConfuserProject(); proj.Seed = context.Project.Seed; foreach (Rule rule in context.Project.Rules) { proj.Rules.Add(rule); } proj.Add(new ProjectModule { Path = fileName }); proj.BaseDirectory = tmpDir; proj.OutputDirectory = outDir; foreach (var path in context.Project.ProbePaths) { proj.ProbePaths.Add(path); } proj.ProbePaths.Add(context.Project.BaseDirectory); PluginDiscovery discovery = null; if (prot != null) { var rule = new Rule { Preset = ProtectionPreset.None, Inherit = true, Pattern = "true" }; rule.Add(new SettingItem <Protection> { Id = prot.Id, Action = SettingItemAction.Add }); proj.Rules.Add(rule); discovery = new PackerDiscovery(prot); } try { ConfuserEngine .Run( new ConfuserParameters { Logger = new PackerLogger(context.Logger), PluginDiscovery = discovery, Marker = new PackerMarker(snKey), Project = proj, PackerInitiated = true }, context.token).Wait(); } catch (AggregateException ex) { context.Logger.Error("Failed to protect packer stub."); throw new ConfuserException(ex); } context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) }; context.OutputPaths = new[] { fileName }; } finally { try { if (Directory.Exists(tmpDir)) { Directory.Delete(tmpDir, true); } } catch (IOException ex) { context.Logger.WarnException("Failed to remove temporary files of packer.", ex); } } }
/// <summary> /// Initializes <see cref="StrongNameKey"/> and <see cref="StrongNamePublicKey"/> /// for enhanced strong name signing (with key migration). See /// http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="module">Module</param> /// <param name="signatureKey">Signature strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="identityPubKey">Identity public key</param> public void InitializeEnhancedStrongNameSigning(ModuleDef module, StrongNameKey signatureKey, StrongNamePublicKey signaturePubKey, StrongNameKey identityKey, StrongNamePublicKey identityPubKey) { StrongNameKey = signatureKey; StrongNameKey.HashAlgorithm = signaturePubKey.HashAlgorithm; StrongNamePublicKey = identityPubKey; if (module.Assembly != null) { module.Assembly.UpdateOrCreateAssemblySignatureKeyAttribute(identityPubKey, identityKey, signaturePubKey); } }
public PackerMarker(StrongNameKey snKey, StrongNamePublicKey snPubKey, bool snDelaySig, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey) { this.snKey = snKey; this.snPubKey = snPubKey; this.snDelaySig = snDelaySig; this.snSigKey = snSigKey; this.snPubSigKey = snPubSigKey; }
/// <summary> /// Protects the stub using original project settings replace the current output with the protected stub. /// </summary> /// <param name="context">The working context.</param> /// <param name="fileName">The result file name.</param> /// <param name="module">The stub module.</param> /// <param name="snKey">The strong name key.</param> /// <param name="prot">The packer protection that applies to the stub.</param> protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null) { string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); string outDir = Path.Combine(tmpDir, Path.GetRandomFileName()); Directory.CreateDirectory(tmpDir); File.WriteAllBytes(Path.Combine(tmpDir, fileName), module); var proj = new ConfuserProject(); proj.Seed = context.Project.Seed; foreach (Rule rule in context.Project.Rules) { proj.Rules.Add(rule); } proj.Add(new ProjectModule { Path = fileName }); proj.BaseDirectory = tmpDir; proj.OutputDirectory = outDir; PluginDiscovery discovery = null; if (prot != null) { var rule = new Rule { Preset = ProtectionPreset.None, Inherit = true, Pattern = "true" }; rule.Add(new SettingItem <Protection> { Id = prot.Id, Action = SettingItemAction.Add }); proj.Rules.Add(rule); discovery = new PackerDiscovery(prot); } try { ConfuserEngine.Run(new ConfuserParameters { Logger = new PackerLogger(context.Logger), PluginDiscovery = discovery, Marker = new PackerMarker(snKey), Project = proj, PackerInitiated = true }, context.token).Wait(); } catch (AggregateException ex) { context.Logger.Error("Failed to protect packer stub."); throw new ConfuserException(ex); } context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) }; context.OutputPaths = new[] { fileName }; }
public void Run() { // Create a default assembly resolver and type resolver and pass it to Load(). // If it's a .NET Core assembly, you'll need to disable GAC loading and add // .NET Core reference assembly search paths. ModuleContext modCtx = ModuleDef.CreateModuleContext(); IAssemblyResolver resolver = new CustomAssemblyResolver(references); modCtx.AssemblyResolver = resolver; module = ModuleDefMD.Load(inputFileName, modCtx); Importer importer = new Importer(module); var instantiateComponentMethod = (from t in module.GetTypes() where t.Name == "ComponentFactory" from m in t.Methods where m.Name == "InstantiateComponent" select m).Single(); var assembly = resolver.Resolve(module.GetAssemblyRefs().FirstOrDefault(x => x.Name == "Microsoft.Extensions.DependencyInjection.Abstractions"), module); var getServiceMethod = (from t in assembly.ManifestModule.GetTypes() where t.Name == "ActivatorUtilities" from m in t.Methods where m.Name == "GetServiceOrCreateInstance" && !m.HasGenericParameters select m).Single(); var firstCall = instantiateComponentMethod.Body.Instructions.First(x => x.OpCode == OpCodes.Call); instantiateComponentMethod.Body.Instructions.Insert(0, OpCodes.Ldarg_1.ToInstruction()); firstCall.Operand = importer.Import(getServiceMethod); if (module.IsILOnly) { // Create writer options var opts = new ModuleWriterOptions(module); // Open or create the strong name key var signatureKey = new StrongNameKey(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Key.snk")); // This method will initialize the required properties opts.InitializeStrongNameSigning(module, signatureKey); module.Write(outputFileName, opts); } else { // Create writer options var opts = new NativeModuleWriterOptions(module, false); // Open or create the strong name key var signatureKey = new StrongNameKey(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Key.snk")); // This method will initialize the required properties opts.InitializeStrongNameSigning(module, signatureKey); module.NativeWrite(outputFileName, opts); } }
/// <summary> /// Creates a new SignedStream with the specified parameters /// </summary> /// <param name="stream">The stream to write to (Must be writable and seekable)</param> /// <param name="strongName">The strong name to hash/sign with (can be null)</param> /// <param name="fileType">The string to use as fileType</param> public AssuredStream(Stream stream, StrongNameKey strongName, string fileType) : this(stream, new AssuredStreamCreateArgs(strongName, fileType, HashType.SHA1)) { }
public static StrongNamePublicKey GetPublicKey(this StrongNameKey key) { return(new StrongNamePublicKey(key.PublicKey)); }