private static void performDiskPatch(PEFile file, MethodDef def, int patternIndex, int fileAddress) { CopyIfNecessary(); // read the int, shift off the table number int fieldNum = BitConverter.ToInt32(def.Method.Code, patternIndex + 6) << 8 >> 8; using (FileStream sw = new FileStream(LOADED_FILE, FileMode.Open, FileAccess.Write, FileShare.Read)) { // change the push 1 to a push 0 int RVA = def.GetByteOffset(file, patternIndex) + 10; byte[] data = new byte[] { 0x16 }; sw.Position = file.FindSectionForRVA(RVA).CalculateFileOffset(RVA); sw.Write(data, 0, data.Length); // write the new location to the metadata folder data = new byte[] { getByte(fileAddress, 0), getByte(fileAddress, 1), getByte(fileAddress, 2), getByte(fileAddress, 3) }; FieldDef fd = file.GetField(fieldNum); RVA = file.FindSectionForFileOffset((int)fd.MetaDataFileLocation) .CalculateRVA((int)fd.MetaDataFileLocation); sw.Position = file.FindSectionForRVA(RVA).CalculateFileOffset(RVA); sw.Write(data, 0, data.Length); // write the string to an empty file location RVA = fileAddress; data = Encoding.Unicode.GetBytes(FileManager.MergedPath); sw.Position = file.FindSectionForRVA(RVA).CalculateFileOffset(RVA); sw.Write(data, 0, data.Length); } }
public CLRDirectory(FileStream fs, PEFile parent) { // Find the .NET metadata and load it PEDataDirectory clrDir = parent.OptionalHeader.DataDirectories.FirstOrDefault( p => p.DirectoryName == PEOptionalHeader.CLR_RUNTIME_HEADER); PESection section = parent.FindSectionForRVA(clrDir.VirtualAddress); MetaDataOffset = section.CalculateFileOffset(clrDir.VirtualAddress); // Skip to the start of the structure fs.Seek(MetaDataOffset, SeekOrigin.Begin); fs.Seek(8, SeekOrigin.Current); MetDataDirectory = new PEDataDirectory(fs, "MetaData"); MetaDataHeaderRVF = parent.FindSectionForRVA(MetDataDirectory.VirtualAddress) .CalculateFileOffset(MetDataDirectory.VirtualAddress); fs.Seek(MetaDataHeaderRVF, SeekOrigin.Begin); // load the metadata itself MetaDataHeader = new MetaDataHeader(fs); MetaDataStreams = new List <MetaDataStream>(); for (int i = 0; i < MetaDataHeader.NumberOfStreams; i++) { MetaDataStreams.Add(new MetaDataStream(fs)); } // we should now be aligned at the start of the first section foreach (MetaDataStream ms in MetaDataStreams) { fs.Seek(ms.Offset + MetaDataHeaderRVF, SeekOrigin.Begin); if (ms.Name == "#~") { ms.Data = new PoundTildeStream(fs); Tables = ms; } else { if (ms.Name == "#Strings") { Strings = ms; } ms.Data = new byte[ms.Size]; fs.Read((byte[])ms.Data, 0, ms.Size); } } ((PoundTildeStream)Tables.Data).FillMethods(fs, Strings, parent); }
public CLRDirectory(FileStream fs, PEFile parent) { // Find the .NET metadata and load it PEDataDirectory clrDir = parent.OptionalHeader.DataDirectories.FirstOrDefault( p => p.DirectoryName == PEOptionalHeader.CLR_RUNTIME_HEADER); PESection section = parent.FindSectionForRVA(clrDir.VirtualAddress); MetaDataOffset = section.CalculateFileOffset(clrDir.VirtualAddress); // Skip to the start of the structure fs.Seek(MetaDataOffset, SeekOrigin.Begin); fs.Seek(8, SeekOrigin.Current); MetDataDirectory = new PEDataDirectory(fs, "MetaData"); MetaDataHeaderRVF = parent.FindSectionForRVA(MetDataDirectory.VirtualAddress) .CalculateFileOffset(MetDataDirectory.VirtualAddress); fs.Seek(MetaDataHeaderRVF,SeekOrigin.Begin); // load the metadata itself MetaDataHeader = new MetaDataHeader(fs); MetaDataStreams = new List<MetaDataStream>(); for(int i =0; i< MetaDataHeader.NumberOfStreams; i++) { MetaDataStreams.Add(new MetaDataStream(fs)); } // we should now be aligned at the start of the first section foreach (MetaDataStream ms in MetaDataStreams) { fs.Seek(ms.Offset + MetaDataHeaderRVF, SeekOrigin.Begin); if (ms.Name == "#~") { ms.Data = new PoundTildeStream(fs); Tables = ms; } else { if (ms.Name == "#Strings") Strings = ms; ms.Data = new byte[ms.Size]; fs.Read((byte[])ms.Data, 0, ms.Size); } } ((PoundTildeStream)Tables.Data).FillMethods(fs, Strings, parent); }
public void Populate(FileStream fs, MetaDataStream Strings, PEFile file) { this.Name = Utils.ReadString((byte[])Strings.Data, NameIndex); if (RVA != 0) { int offset = file.FindSectionForRVA(RVA).CalculateFileOffset(RVA); fs.Seek(offset, SeekOrigin.Begin); Method = new Method(fs); } }