public static string Disassemble(string sourceFileName, string outputFile, AssemblerOptions asmOptions) { if (asmOptions.HasFlag(AssemblerOptions.UseOwnDisassembler)) { using (ModuleDefinition module = ModuleDefinition.ReadModule(sourceFileName)) using (var writer = new StreamWriter(outputFile)) { module.Name = Path.GetFileNameWithoutExtension(outputFile); var output = new PlainTextOutput(writer); ReflectionDisassembler rd = new ReflectionDisassembler(output, CancellationToken.None); rd.DetectControlStructure = false; rd.WriteAssemblyReferences(module); if (module.Assembly != null) { rd.WriteAssemblyHeader(module.Assembly); } output.WriteLine(); rd.WriteModuleHeader(module, skipMVID: true); output.WriteLine(); rd.WriteModuleContents(module); } return(outputFile); } string ildasmPath = SdkUtility.GetSdkPath("ildasm.exe"); ProcessStartInfo info = new ProcessStartInfo(ildasmPath); info.Arguments = $"/nobar /utf8 /out=\"{outputFile}\" \"{sourceFileName}\""; info.RedirectStandardError = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process process = Process.Start(info); var outputTask = process.StandardOutput.ReadToEndAsync(); var errorTask = process.StandardError.ReadToEndAsync(); Task.WaitAll(outputTask, errorTask); process.WaitForExit(); Console.WriteLine("output: " + outputTask.Result); Console.WriteLine("errors: " + errorTask.Result); // Unlike the .imagebase directive (which is a fixed value when compiling with /deterministic), // the image base comment still varies... ildasm putting a random number here? string il = File.ReadAllText(outputFile); il = Regex.Replace(il, @"^// Image base: 0x[0-9A-F]+\r?\n", "", RegexOptions.Multiline); // and while we're at it, also remove the MVID il = Regex.Replace(il, @"^// MVID: \{[0-9A-F-]+\}\r?\n", "", RegexOptions.Multiline); // and the ildasm version info (varies from system to system) il = Regex.Replace(il, @"^// +Microsoft .* Disassembler\. +Version.*\r?\n", "", RegexOptions.Multiline); // copyright header "All rights reserved" is dependent on system language il = Regex.Replace(il, @"^// +Copyright .* Microsoft.*\r?\n", "", RegexOptions.Multiline); // filename may contain full path il = Regex.Replace(il, @"^// WARNING: Created Win32 resource file.*\r?\n", "", RegexOptions.Multiline); File.WriteAllText(outputFile, il); return(outputFile); }
public static async Task SignAssembly(string assemblyPath, string keyFilePath) { string snPath = SdkUtility.GetSdkPath("sn.exe"); var command = Cli.Wrap(snPath) .WithArguments($"-R \"{assemblyPath}\" \"{keyFilePath}\"") .WithValidation(CommandResultValidation.None); var result = await command.ExecuteBufferedAsync().ConfigureAwait(false); Assert.AreEqual(0, result.ExitCode, "sn failed"); Console.WriteLine("output: " + result.StandardOutput); Console.WriteLine("errors: " + result.StandardError); }
public static string Disassemble(string sourceFileName, string outputFile, AssemblerOptions asmOptions) { if (asmOptions.HasFlag(AssemblerOptions.UseOwnDisassembler)) { using (ModuleDefinition module = ModuleDefinition.ReadModule(sourceFileName)) using (var writer = new StreamWriter(outputFile)) { module.Name = Path.GetFileNameWithoutExtension(outputFile); var output = new PlainTextOutput(writer); ReflectionDisassembler rd = new ReflectionDisassembler(output, CancellationToken.None); rd.DetectControlStructure = false; rd.WriteAssemblyReferences(module); if (module.Assembly != null) { rd.WriteAssemblyHeader(module.Assembly); } output.WriteLine(); rd.WriteModuleHeader(module); output.WriteLine(); rd.WriteModuleContents(module); } return(outputFile); } string ildasmPath = SdkUtility.GetSdkPath("ildasm.exe"); ProcessStartInfo info = new ProcessStartInfo(ildasmPath); info.Arguments = $"/out=\"{outputFile}\" \"{sourceFileName}\""; info.RedirectStandardError = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process process = Process.Start(info); var outputTask = process.StandardOutput.ReadToEndAsync(); var errorTask = process.StandardError.ReadToEndAsync(); Task.WaitAll(outputTask, errorTask); process.WaitForExit(); Console.WriteLine("output: " + outputTask.Result); Console.WriteLine("errors: " + errorTask.Result); return(outputFile); }
public static void SignAssembly(string assemblyPath, string keyFilePath) { string snPath = SdkUtility.GetSdkPath("sn.exe"); ProcessStartInfo info = new ProcessStartInfo(snPath); info.Arguments = $"-R \"{assemblyPath}\" \"{keyFilePath}\""; info.RedirectStandardError = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process process = Process.Start(info); var outputTask = process.StandardOutput.ReadToEndAsync(); var errorTask = process.StandardError.ReadToEndAsync(); Task.WaitAll(outputTask, errorTask); process.WaitForExit(); Console.WriteLine("output: " + outputTask.Result); Console.WriteLine("errors: " + errorTask.Result); }