Beispiel #1
0
        public static void WritePortableExecutable(PortableExecutableInfo peInfo, Stream stream)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            peInfo.WritePortableExecutable(writer);
            writer.Close();
        }
Beispiel #2
0
        public static void WritePortableExecutable(PortableExecutableInfo peInfo, string path)
        {
            FileSystemUtils.ClearReadOnlyAttribute(path);
            FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);

            WritePortableExecutable(peInfo, stream);
            stream.Close();
        }
Beispiel #3
0
        public static List <string> GetDependencies(string path)
        {
            List <string>          result = new List <string>();
            PortableExecutableInfo peInfo = new PortableExecutableInfo(path);

            ImportDirectory dir = peInfo.ImportDirectory;

            if (dir != null)
            {
                BinaryReader reader = GetBinaryReader(path);
                foreach (ImageImportDescriptor desc in dir.Descriptors)
                {
                    uint fileNameOffset = peInfo.GetOffsetFromRVA(desc.NameRVA);
                    reader.BaseStream.Seek(fileNameOffset, SeekOrigin.Begin);
                    string fileName = BinaryReaderUtils.ReadNullTerminatedAsciiString(reader);
                    result.Add(fileName);
                }
                reader.Close();
            }
            return(result);
        }
Beispiel #4
0
        public static void RenameDependencyFileName(string filePath, string oldFileName, string newFileName)
        {
            if (oldFileName.Length != newFileName.Length)
            {
                throw new NotImplementedException("when renaming dependencies, old file name must have the same size as new file name");
            }

            PortableExecutableInfo peInfo = new PortableExecutableInfo(filePath);
            uint            oldNameRVA    = 0;
            PESectionHeader header        = null;
            int             sectionIndex  = -1;

            foreach (ImageImportDescriptor descriptor in peInfo.ImportDirectory.Descriptors)
            {
                uint nameRVA = descriptor.NameRVA;
                header = peInfo.FindSectionByRVA(nameRVA);
                if (header != null)
                {
                    sectionIndex = peInfo.SectionHeaders.IndexOf(header);

                    uint fileNameAddressInSection = peInfo.GetAddressInSectionFromRVA(header, nameRVA);

                    string fileName = PortableExecutableUtils.ReadNullTerminatedAsciiString(peInfo.Sections[sectionIndex], fileNameAddressInSection);
                    if (fileName.Equals(oldFileName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        oldNameRVA = nameRVA;
                    }
                }
            }

            if (oldNameRVA > 0)
            {
                byte[] newFileNameAsciiBytes = ASCIIEncoding.ASCII.GetBytes(newFileName);
                uint   addressInSection      = peInfo.GetAddressInSectionFromRVA(header, oldNameRVA);
                byte[] section = peInfo.Sections[sectionIndex];
                Buffer.BlockCopy(newFileNameAsciiBytes, 0, section, (int)addressInSection, newFileNameAsciiBytes.Length);
            }
            PortableExecutableInfo.WritePortableExecutable(peInfo, filePath);
        }