Exemplo n.º 1
0
        public static void Erase(NativeModuleWriter writer, ModuleDefMD module)
        {
            if (writer == null || module == null)
                return;

            var sects = (IList)origSects.GetValue(writer);
            var sections = new List<Tuple<uint, uint, byte[]>>();
            var s = new MemoryStream();
            foreach (var origSect in sects) {
                var oldChunk = (BinaryReaderChunk)chunk.GetValue(origSect);
                var sectHdr = (ImageSectionHeader)peSection.GetValue(origSect);

                s.SetLength(0);
                oldChunk.WriteTo(new BinaryWriter(s));
                var buf = s.ToArray();
                var newChunk = new BinaryReaderChunk(MemoryImageStream.Create(buf), oldChunk.GetVirtualSize());
                newChunk.SetOffset(oldChunk.FileOffset, oldChunk.RVA);

                chunk.SetValue(origSect, newChunk);

                sections.Add(Tuple.Create(
                    sectHdr.PointerToRawData,
                    sectHdr.PointerToRawData + sectHdr.SizeOfRawData,
                    buf));
            }

            var md = module.MetaData;

            var row = md.TablesStream.MethodTable.Rows;
            for (uint i = 1; i <= row; i++) {
                var method = md.TablesStream.ReadMethodRow(i);
                var codeType = ((MethodImplAttributes)method.ImplFlags & MethodImplAttributes.CodeTypeMask);
                if (codeType == MethodImplAttributes.IL)
                    Erase(sections, (uint)md.PEImage.ToFileOffset((RVA)method.RVA));
            }

            var res = md.ImageCor20Header.Resources;
            if (res.Size > 0)
                Erase(sections, (uint)res.StartOffset, res.Size);

            Erase(sections, md.ImageCor20Header);
            Erase(sections, md.MetaDataHeader);
            foreach (var stream in md.AllStreams)
                Erase(sections, stream);
        }