/// <summary>Executes the task.</summary> /// <param name="includePatterns">The include patterns.</param> /// <param name="excludePatterns">The exclude patterns.</param> /// <param name="keyFileName">Name of the key file.</param> /// <param name="password">The password.</param> /// <param name="force"> /// if set to <c>true</c> assemblies will be signed even if they are already signed. /// </param> public virtual void Execute( string[] includePatterns, string[] excludePatterns, string keyFileName, string password, bool force) { var keyPair = MsilUtilities.LoadKeyPair(keyFileName, password); foreach (var fileName in FindFiles(includePatterns, excludePatterns)) { var assembly = MsilUtilities.LoadAssembly(fileName); if (!MsilUtilities.IsManaged(assembly)) { Log.Warn("Assembly '{0}' is unmanaged, thus cannot be resigned", fileName); continue; } if (MsilUtilities.IsSigned(assembly)) { if (force) { Log.Warn("Assembly '{0}' was previously signed, but it going to be resigned with new key", fileName); } else { Log.Debug("Assembly '{0}' is already signed so it does not need resigning", fileName); continue; } } MsilUtilities.SaveAssembly(assembly, fileName, keyPair); } }
public void Execute( string keyFileName, string keyFilePassword, string[] includePatterns, string[] excludePatterns) { var keyPair = MsilUtilities.LoadKeyPair(keyFileName, keyFilePassword); var fileNames = FindFiles(includePatterns, excludePatterns).ToArray(); foreach (var fileName in fileNames) { var assembly = MsilUtilities.LoadAssembly(fileName); var assemblyInfo = new AssemblyInfo { FileName = fileName, Assembly = assembly, CanBeSigned = MsilUtilities.IsManaged(assembly), }; _assemblyInfos.Add(assemblyInfo); } ScanDependencies(); SignAndFixAssemblies(keyPair); }
/// <summary>Injects the DLL.</summary> /// <param name="targetAssembly">The target assembly.</param> /// <param name="sourceAssembly">The source assembly.</param> /// <param name="sourceAssemblyBytes">The source assembly bytes.</param> /// <param name="overwrite"> /// if set to <c>true</c> overwrites existing resource. /// </param> /// <returns> /// <c>true</c> if assembly has been injected. /// </returns> protected static bool InjectDll( AssemblyDefinition targetAssembly, AssemblyDefinition sourceAssembly, byte[] sourceAssemblyBytes, bool overwrite) { var flags = String.Empty; if (!MsilUtilities.IsManaged(sourceAssembly)) { flags += "u"; } if (MsilUtilities.IsPortable(sourceAssembly)) { flags += "p"; } var input = sourceAssemblyBytes; var output = DefaultCodecs.DeflateEncoder(input); if (output.Length < input.Length) { flags += "z"; } else { output = input; } var architecture = MsilUtilities.GetArchitecture(sourceAssembly); var architecturePrefix = architecture == AssemblyArchitecture.X64 ? "x64:" : architecture == AssemblyArchitecture.X86 ? "x86:" : string.Empty; var guid = Hash(architecturePrefix + sourceAssembly.FullName); var resourceName = String.Format( "asmz://{0:N}/{1}/{2}", guid, input.Length, flags); var existing = targetAssembly.MainModule.Resources .Where(r => Hash(r) == guid) .ToArray(); if (existing.Length > 0) { if (overwrite) { Log.Warn("Resource '{0}' already exists and is going to be replaced.", resourceName); foreach (var r in existing) { targetAssembly.MainModule.Resources.Remove(r); } } else { Log.Warn("Resource '{0}' already exists and will be skipped.", resourceName); return(false); } } var resource = new EmbeddedResource( resourceName, ManifestResourceAttributes.Public, output); targetAssembly.MainModule.Resources.Add(resource); return(true); }
/// <summary>Executes the task.</summary> /// <param name="libzFileName">Name of the libz file.</param> /// <param name="includePatterns">The include patterns.</param> /// <param name="excludePatterns">The exclude patterns.</param> /// <param name="codecName">Name of the codec.</param> /// <param name="safeLoad">if set to <c>true</c> 'safe load' if requested.</param> /// <param name="move">if set to <c>true</c> moves files (deletes soure files).</param> /// <param name="overwrite">if set to <c>true</c> overwrites existing resources.</param> public virtual void Execute( string libzFileName, string[] includePatterns, string[] excludePatterns, string codecName, bool safeLoad, bool move, bool overwrite) { var injectedFileNames = new List <string>(); if (string.IsNullOrEmpty(codecName)) { codecName = "deflate"; } using (var container = new LibZContainer(libzFileName, true)) { foreach (var fileName in FindFiles(includePatterns, excludePatterns)) { var assembly = MsilUtilities.LoadAssembly(fileName); if (assembly == null) { Log.Warn("Assembly from '{0}' could not be loaded", fileName); continue; } var assemblyName = assembly.Name; var managed = MsilUtilities.IsManaged(assembly); var architecture = MsilUtilities.GetArchitecture(assembly); var portable = MsilUtilities.IsPortable(assembly); var assemblyInfo = new AssemblyInfo { AssemblyName = new AssemblyName(assemblyName.FullName), AnyCPU = architecture == AssemblyArchitecture.AnyCPU, X64 = architecture == AssemblyArchitecture.X64, SafeLoad = safeLoad, Unmanaged = !managed, Portable = portable, Bytes = File.ReadAllBytes(fileName), }; Log.Info("Appending '{0}' from '{1}'", assemblyInfo.AssemblyName, fileName); container.Append( assemblyInfo, new AppendOptions { CodecName = codecName, Overwrite = overwrite, }); injectedFileNames.Add(fileName); } if (injectedFileNames.Count <= 0) { Log.Warn("No files injected: {0}", string.Join(", ", includePatterns)); } else { if (move) { foreach (var fn in injectedFileNames) { DeleteFile(fn); } } } } }