static uint UpdateTable(PEFile pe, uint tableRva, int entryPointerOffset, int entrySize, List <uint> offsets, uint?newSectionRva) { var tableSection = pe.GetSectionContainingRva(tableRva); var tableData = ((tableSection.Contents as VirtualSegment).PhysicalContents as DataSegment).Data; using MemoryStream tableMs = new MemoryStream(tableData); tableMs.Seek(tableRva - tableSection.Rva, SeekOrigin.Begin); BinaryWriter bw = new BinaryWriter(tableMs); uint dataBaseVa; if (newSectionRva.HasValue) { dataBaseVa = (uint)(pe.OptionalHeader.ImageBase + newSectionRva.Value); } else { // Get first string offset tableMs.Seek(entryPointerOffset, SeekOrigin.Current); BinaryReader br = new BinaryReader(tableMs); dataBaseVa = br.ReadUInt32(); tableMs.Seek(-(entryPointerOffset + 4), SeekOrigin.Current); } foreach (var offset in offsets) { tableMs.Seek(entryPointerOffset, SeekOrigin.Current); bw.Write(dataBaseVa + offset); tableMs.Seek(entrySize - entryPointerOffset - 4, SeekOrigin.Current); } tableMs.Flush(); return((uint)(dataBaseVa - pe.OptionalHeader.ImageBase)); }
static void DumpStrings(PEFile pe, uint tableRva, int numEntries, int entryPointerOffset, int entrySize, StreamWriter sw) { var tableSection = pe.GetSectionContainingRva(tableRva); var tableData = ((tableSection.Contents as VirtualSegment).PhysicalContents as DataSegment).Data; using MemoryStream tableMs = new MemoryStream(tableData); tableMs.Seek(tableRva - tableSection.Rva, SeekOrigin.Begin); BinaryReader br = new BinaryReader(tableMs); for (int i = 0; i < numEntries; ++i) { var tablePos = tableMs.Position; tableMs.Seek(entryPointerOffset, SeekOrigin.Current); uint stringVa = br.ReadUInt32(); tableMs.Seek((long)(stringVa - pe.OptionalHeader.ImageBase - tableSection.Rva), SeekOrigin.Begin); List <byte> stringBytes = new List <byte>(); byte b; while ((b = br.ReadByte()) != 0) { stringBytes.Add(b); } sw.WriteLine(shiftJisEncoding.GetString(stringBytes.ToArray())); tableMs.Seek(tablePos + entrySize, SeekOrigin.Begin); } }