Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
                        }
                    }
                }
            }
        }