public TypeDefTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
void AddImportScopeIndexes(Dictionary <uint, BlobDataInfoPosition> dict, TablesHeap tables, Table table, int column) { var mdTable = tables.MDTables[(int)table]; if (mdTable.Rows == 0) { return; } var heapStart = Span.Span.Start; var heapEnd = Span.Span.End; var buffer = Span.Buffer; var recPos = mdTable.Span.Start; var rows = mdTable.Rows; var colInfo = mdTable.Columns[column]; uint recSize = mdTable.RowSize; bool bigBlob = colInfo.Size == 4; for (uint rid = 1; rid <= rows; rid++, recPos += recSize) { uint offs = bigBlob ? buffer.ReadUInt32(recPos + colInfo.Offset) : buffer.ReadUInt16(recPos + colInfo.Offset); if (offs == 0) { continue; } var pos = heapStart + offs; int len = ReadCompressedUInt32(ref pos) ?? -1; if (len <= 0) { continue; } var end = HexPosition.Min(heapEnd, pos + len); while (pos < end) { var kind = ReadCompressedUInt32(ref pos); if (kind is null || pos > end) { break; } var flags = GetImportFlags(kind.Value); Debug.Assert(flags != 0); if (flags == 0) { break; } if ((flags & ImportFlags.Alias) != 0) { var valueTmp = ReadCompressedUInt32(ref pos); if (valueTmp is null || pos > end) { break; } var value = valueTmp.Value; if (value != 0 && !dict.ContainsKey((uint)value)) { var namePos = heapStart + value; if (namePos < heapEnd) { dict[(uint)value] = new BlobDataInfoPosition(namePos, BlobDataKind.Utf8Name); } } } if ((flags & ImportFlags.TargetAssembly) != 0) { var valueTmp = ReadCompressedUInt32(ref pos); if (valueTmp is null || pos > end) { break; } } if ((flags & ImportFlags.TargetNamespace) != 0) { var valueTmp = ReadCompressedUInt32(ref pos); if (valueTmp is null || pos > end) { break; } var value = valueTmp.Value; if (value != 0 && !dict.ContainsKey((uint)value)) { var namePos = heapStart + value; if (namePos < heapEnd) { dict[(uint)value] = new BlobDataInfoPosition(namePos, BlobDataKind.Utf8Name); } } } if ((flags & ImportFlags.TargetType) != 0) { var valueTmp = ReadCompressedUInt32(ref pos); if (valueTmp is null || pos > end) { break; } } } } }
void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDBeginAddResources) { ctx.Context.CheckCancellation(); ctx.Context.Logger.Log("Cryting resources..."); bool hasPacker = ctx.Context.Packer != null; List <EmbeddedResource> resources = ctx.Module.Resources.OfType <EmbeddedResource>().ToList(); if (!hasPacker) { ctx.Module.Resources.RemoveWhere(res => res is EmbeddedResource); } // move resources string asmName = ctx.Name.RandomName(); PublicKey pubKey = null; if (writer.TheOptions.StrongNameKey != null) { pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey); } var assembly = new AssemblyDefUser(asmName, new Version(0, 0), pubKey); assembly.Modules.Add(new ModuleDefUser(asmName + ".dll")); ModuleDef module = assembly.ManifestModule; assembly.ManifestModule.Kind = ModuleKind.Dll; var asmRef = new AssemblyRefUser(module.Assembly); if (!hasPacker) { foreach (EmbeddedResource res in resources) { res.Attributes = ManifestResourceAttributes.Public; module.Resources.Add(res); ctx.Module.Resources.Add(new AssemblyLinkedResource(res.Name, asmRef, res.Attributes)); } } byte[] moduleBuff; using (var ms = new MemoryStream()) { module.Write(ms, new ModuleWriterOptions { StrongNameKey = writer.TheOptions.StrongNameKey }); moduleBuff = ms.ToArray(); } // compress moduleBuff = ctx.Context.Registry.GetService <ICompressionService>().Compress( moduleBuff, progress => ctx.Context.Logger.Progress((int)(progress * 10000), 10000)); ctx.Context.Logger.EndProgress(); ctx.Context.CheckCancellation(); uint compressedLen = (uint)(moduleBuff.Length + 3) / 4; compressedLen = (compressedLen + 0xfu) & ~0xfu; var compressedBuff = new uint[compressedLen]; Buffer.BlockCopy(moduleBuff, 0, compressedBuff, 0, moduleBuff.Length); Debug.Assert(compressedLen % 0x10 == 0); // encrypt uint keySeed = ctx.Random.NextUInt32() | 0x10; var key = new uint[0x10]; uint state = keySeed; for (int i = 0; i < 0x10; i++) { state ^= state >> 13; state ^= state << 25; state ^= state >> 27; key[i] = state; } var encryptedBuffer = new byte[compressedBuff.Length * 4]; int buffIndex = 0; while (buffIndex < compressedBuff.Length) { uint[] enc = ctx.ModeHandler.Encrypt(compressedBuff, buffIndex, key); for (int j = 0; j < 0x10; j++) { key[j] ^= compressedBuff[buffIndex + j]; } Buffer.BlockCopy(enc, 0, encryptedBuffer, buffIndex * 4, 0x40); buffIndex += 0x10; } Debug.Assert(buffIndex == compressedBuff.Length); var size = (uint)encryptedBuffer.Length; TablesHeap tblHeap = writer.MetaData.TablesHeap; tblHeap.ClassLayoutTable[writer.MetaData.GetClassLayoutRid(ctx.DataType)].ClassSize = size; tblHeap.FieldTable[writer.MetaData.GetRid(ctx.DataField)].Flags |= (ushort)FieldAttributes.HasFieldRVA; encryptedResource = writer.Constants.Add(new ByteArrayChunk(encryptedBuffer), 8); // inject key values MutationHelper.InjectKeys(ctx.InitMethod, new[] { 0, 1 }, new[] { (int)(size / 4), (int)(keySeed) }); } else if (e.WriterEvent == ModuleWriterEvent.EndCalculateRvasAndFileOffsets) { TablesHeap tblHeap = writer.MetaData.TablesHeap; tblHeap.FieldRVATable[writer.MetaData.GetFieldRVARid(ctx.DataField)].RVA = (uint)encryptedResource.RVA; } }
DotNetHeap[] CreateENCHeaps() { var list = new List <DotNetHeap>(); StringsHeap stringsHeap = null; USHeap usHeap = null; BlobHeap blobHeap = null; GUIDHeap guidHeap = null; TablesHeap tablesHeap = null; foreach (var ssh in storageStreamHeaders) { var span = new HexBufferSpan(file.Buffer, ssh.DataSpan); switch (ssh.Name.ToUpperInvariant()) { case "#STRINGS": if (stringsHeap == null) { stringsHeap = new StringsHeapImpl(span); list.Add(stringsHeap); continue; } break; case "#US": if (usHeap == null) { usHeap = new USHeapImpl(span); list.Add(usHeap); continue; } break; case "#BLOB": if (blobHeap == null) { blobHeap = new BlobHeapImpl(span); list.Add(blobHeap); continue; } break; case "#GUID": if (guidHeap == null) { guidHeap = new GUIDHeapImpl(span); list.Add(guidHeap); continue; } break; case "#~": // Only if #Schema is used case "#-": if (tablesHeap == null && span.Length >= TablesHeapImpl.MinimumSize) { tablesHeap = new TablesHeapImpl(span, TablesHeapType.ENC); list.Add(tablesHeap); continue; } break; } list.Add(new UnknownHeapImpl(span)); } return(list.ToArray()); }
DotNetHeap[] CreateCompressedHeaps() { var list = new List <DotNetHeap>(); StringsHeap stringsHeap = null; USHeap usHeap = null; BlobHeap blobHeap = null; GUIDHeap guidHeap = null; TablesHeap tablesHeap = null; PdbHeap pdbHeap = null; for (int i = storageStreamHeaders.Length - 1; i >= 0; i--) { var ssh = storageStreamHeaders[i]; var span = new HexBufferSpan(file.Buffer, ssh.DataSpan); switch (ssh.Name) { case "#Strings": if (stringsHeap == null) { stringsHeap = new StringsHeapImpl(span); list.Add(stringsHeap); continue; } break; case "#US": if (usHeap == null) { usHeap = new USHeapImpl(span); list.Add(usHeap); continue; } break; case "#Blob": if (blobHeap == null) { blobHeap = new BlobHeapImpl(span); list.Add(blobHeap); continue; } break; case "#GUID": if (guidHeap == null) { guidHeap = new GUIDHeapImpl(span); list.Add(guidHeap); continue; } break; case "#~": if (tablesHeap == null && span.Length >= TablesHeapImpl.MinimumSize) { tablesHeap = new TablesHeapImpl(span, TablesHeapType.Compressed); list.Add(tablesHeap); continue; } break; case "#!": list.Add(new HotHeapImpl(span)); continue; case "#Pdb": if (pdbHeap == null && span.Length >= PdbHeapImpl.MinimumSize) { pdbHeap = new PdbHeapImpl(span); list.Add(pdbHeap); continue; } break; } list.Add(new UnknownHeapImpl(span)); } list.Reverse(); return(list.ToArray()); }
public PropertyTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public MethodSemanticsTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public GenericParamTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public LocalVariableTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public ExportedTypeTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public ManifestResourceTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public AssemblyRefTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public FieldRVATableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public ImplMapTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public DeclSecurityTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public TablesStreamVM(HexBuffer buffer, TablesHeap tablesHeap, MetaDataTableVM[] metaDataTables) : base(tablesHeap.HeaderSpan) { Debug.Assert(metaDataTables.Length == 0x40); this.metaDataTables = metaDataTables; Name = tablesHeap.Header.Name; M_ulReservedVM = new UInt32HexField(tablesHeap.Header.Reserved); M_majorVM = new ByteHexField(tablesHeap.Header.MajorVersion, true); M_minorVM = new ByteHexField(tablesHeap.Header.MinorVersion, true); M_heapsVM = new ByteFlagsHexField(tablesHeap.Header.Flags); M_heapsVM.Add(new BooleanHexBitField("BigStrings", 0)); M_heapsVM.Add(new BooleanHexBitField("BigGUID", 1)); M_heapsVM.Add(new BooleanHexBitField("BigBlob", 2)); M_heapsVM.Add(new BooleanHexBitField("Padding", 3)); M_heapsVM.Add(new BooleanHexBitField("Reserved", 4)); M_heapsVM.Add(new BooleanHexBitField("DeltaOnly", 5)); M_heapsVM.Add(new BooleanHexBitField("ExtraData", 6)); M_heapsVM.Add(new BooleanHexBitField("HasDelete", 7)); M_ridVM = new ByteHexField(tablesHeap.Header.Log2Rid); M_maskvalidVM = new UInt64FlagsHexField(tablesHeap.Header.ValidMask); AddTableFlags(M_maskvalidVM); M_sortedVM = new UInt64FlagsHexField(tablesHeap.Header.SortedMask); AddTableFlags(M_sortedVM); var list = new List <HexField> { M_ulReservedVM, M_majorVM, M_minorVM, M_heapsVM, M_ridVM, M_maskvalidVM, M_sortedVM, }; rowsVM = new UInt32HexField[64]; ulong valid = tablesHeap.ValidMask; for (int i = 0, rowIndex = 0; i < rowsVM.Length; i++) { UInt32HexField field; if ((valid & 1) != 0 && rowIndex < tablesHeap.Header.Rows.Data.FieldCount) { var row = tablesHeap.Header.Rows.Data[rowIndex++].Data; field = new UInt32HexField(row, tablesHeap.Header.Rows.Name + "[" + i.ToString("X2") + "]"); list.Add(field); } else { field = UInt32HexField.TryCreate(null); } rowsVM[i] = field; valid >>= 1; } M_ulExtraVM = UInt32HexField.TryCreate(tablesHeap.Header.ExtraData); M_ulExtraVM.IsVisible = tablesHeap.Header.HasExtraData; if (tablesHeap.Header.HasExtraData) { list.Add(M_ulExtraVM); } hexFields = list.ToArray(); }
public EventTableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) : base(tablesHeap, mdTable) { }
public virtual void VisitTablesHeap(TablesHeap heap) { }
public TableRecordDataFactory(TablesHeap tablesHeap, MDTable mdTable) { TablesHeap = tablesHeap ?? throw new ArgumentNullException(nameof(tablesHeap)); MDTable = mdTable ?? throw new ArgumentNullException(nameof(mdTable)); }