public AssemblyWriter(AssemblyDefinition assemblyDef, Stream output, CompilationOptions options) : base(output) { _options = options; _text = GetTextSection(assemblyDef); _rsrc = GetRsrcSection(); _reloc = GetRelocSection(); }
private void AddChunksToSections() { textSection.Add(ImportAddressTable, DEFAULT_IAT_ALIGNMENT); textSection.Add(ImageCor20Header, DEFAULT_COR20HEADER_ALIGNMENT); textSection.Add(strongNameSignature, DEFAULT_STRONGNAMESIG_ALIGNMENT); textSection.Add(constants, DEFAULT_CONSTANTS_ALIGNMENT); textSection.Add(methodBodies, DEFAULT_METHODBODIES_ALIGNMENT); textSection.Add(netResources, DEFAULT_NETRESOURCES_ALIGNMENT); textSection.Add(metaData, DEFAULT_METADATA_ALIGNMENT); textSection.Add(debugDirectory, DEFAULT_DEBUGDIRECTORY_ALIGNMENT); textSection.Add(ImportDirectory, DEFAULT_IMPORTDIRECTORY_ALIGNMENT); textSection.Add(StartupStub, DEFAULT_STARTUPSTUB_ALIGNMENT); if (rsrcSection != null) { rsrcSection.Add(win32Resources, DEFAULT_WIN32_RESOURCES_ALIGNMENT); } if (RelocSection != null) { RelocSection.Add(RelocDirectory, DEFAULT_RELOC_ALIGNMENT); } }
// We don't really need a reloc folder if were wharehousing the whole binary anyhow public static RelocSection ExtractRelocData(string PE, string RelocBase = null, bool NoExtraRelocFolder = true) { using (var fs = new FileStream(PE, FileMode.Open, FileAccess.Read)) { var buff = new byte[4096]; fs.Read(buff, 0, 4096); var e = Extract.IsBlockaPE(buff); if (e == null) { return(null); } e.FileName = PE; if (e.RelocSize == 0) { return(null); } RelocSection rv = new RelocSection(); int RelocPos = 0, RelocSize = 0; rv.FullPath = PE; rv.Name = Path.GetFileName(PE); rv.Is64 = e.Is64; rv.VirtualSize = e.SizeOfImage; rv.TimeStamp = e.TimeStamp; rv.OriginalBase = e.ImageBase; rv.OrigBaseOffset = (int)e.ImageBaseOffset; for (int i = 0; i < e.Sections.Count(); i++) { if (e.Sections[i].Name == ".reloc") { RelocPos = (int)e.Sections[i].RawFilePointer; RelocSize = (int)e.Sections[i].RawFileSize; break; } } if (RelocPos == 0 && RelocSize == 0) { return(null); } rv.RelocSecOffset = RelocPos; rv.RelocLength = RelocSize; var readBuffer = new byte[RelocSize]; if (RelocSize != 0) { fs.Position = RelocPos; fs.Read(readBuffer, 0, RelocSize); if (!NoExtraRelocFolder && !string.IsNullOrWhiteSpace(RelocBase) && !File.Exists(rv.FullPath)) { var relocDir = e.Is64 ? Path.Combine(RelocBase, "64") : Path.Combine(RelocBase, "32"); var sb = $"{Path.GetFileName(e.FileName)}-{e.ImageBase.ToString("X")}-{e.TimeStamp.ToString("X")}.reloc"; var outFile = Path.Combine(relocDir, sb); using (FileStream stream = new FileStream(outFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) stream.Write(readBuffer, 0, RelocSize); } } rv.RawRelocBuffer = readBuffer; return(rv); } }
public static RelocSection ExtractRelocData(Extract e, string RelocBase, byte[] buff = null, bool NoWrite = false) { if (e.RelocSize == 0) { return(null); } RelocSection rv = new RelocSection(); int RelocPos = 0, RelocSize = 0; rv.FullPath = e.FileName; rv.Name = Path.GetFileName(e.FileName); rv.Is64 = e.Is64; rv.VirtualSize = e.SizeOfImage; rv.TimeStamp = e.TimeStamp; rv.OriginalBase = e.ImageBase; rv.OrigBaseOffset = (int)e.ImageBaseOffset; for (int i = 0; i < e.Sections.Count(); i++) { if (e.Sections[i].Name == ".reloc") { RelocPos = (int)e.Sections[i].RawFilePointer; RelocSize = (int)e.Sections[i].RawFileSize; break; } } rv.RelocSecOffset = RelocPos; rv.RelocLength = RelocSize; rv.RawRelocBuffer = new ArraySegment <byte>(buff, RelocPos, RelocSize).ToArray(); return(rv); // we can use this DB of files for a solid white list #region keep whole files //var relocDir = e.Is64 ? Path.Combine(RelocBase, "64") : Path.Combine(RelocBase, "32"); //var sb = $"{Path.GetFileName(e.FileName)}-{e.ImageBase.ToString("X")}-{e.TimeStamp.ToString("X")}.reloc"; //var outFile = Path.Combine(relocDir, sb); /* * if (buff != null && buff.Length >= RelocPos + RelocSize) * { * rv.RawRelocBuffer = new ArraySegment<byte>(buff, RelocPos, RelocSize).ToArray(); * } * byte[] readBuffer; * using (var fileStream = File.OpenRead(e.FileName)) * { * for (int i = 0; i < e.Sections.Count(); i++) * { * if (e.Sections[i].Name == ".reloc") * { * RelocPos = (int)e.Sections[i].RawFilePointer; * RelocSize = (int)e.Sections[i].RawFileSize; * break; * } * } * if (RelocSize != 0) * { * readBuffer = new byte[RelocSize]; * fileStream.Position = RelocPos; * fileStream.Read(readBuffer, 0, RelocSize); * * if (!NoWrite && !File.Exists(rv.FullPath)) * { * using (FileStream stream = new FileStream(outFile, * FileMode.CreateNew, FileAccess.Write, FileShare.Read, RelocSize, true)) * stream.Write(readBuffer, 0, RelocSize); * } * } * } * return rv;*/ #endregion }