private void AssembleStub() { try { string intermediateOutputFileName = GetIntermediateBinaryPath(Project.VersionInfo.OriginalFilename.ToNullIfEmpty() ?? Path.GetFileName(OutputFileName)); string manifestFileName; if (Project.Manifest.Template != null) { manifestFileName = GetIntermediateSourcePath(@"Resources\" + Project.Manifest.Template.GetDescription() + ".manifest"); } else if (Project.Manifest.Path != null) { manifestFileName = GetIntermediateSourcePath(@"Resources\" + Path.GetFileNameWithoutExtension(Project.Manifest.Path) + ".manifest"); File.Copy(Project.Manifest.Path, manifestFileName); } else { manifestFileName = null; } string iconFileName; if (Project.Stub.IconPath != null) { iconFileName = GetIntermediateSourcePath(@"Resources\icon.ico"); Icon icon = IconExtractor.FromFile(Project.Stub.IconPath); if (icon == null) { throw new ErrorException("Could not read icon from file '" + Path.GetFileName(Project.Stub.IconPath) + "'."); } icon.Save(iconFileName); } else { iconFileName = null; } bool success = Helper.DotNetCompile ( StubSourceCodeFiles.Select(file => GetIntermediateSourcePath(file)).ToArray(), new[] { "mscorlib.dll", "System.dll", "System.Core.dll", "System.Windows.Forms.dll" }, new[] { GetIntermediateSourcePath(@"Resources\" + StubResourcesFileName) }, manifestFileName, iconFileName, intermediateOutputFileName, Is64Bit, out string[] errors ); if (success) { Helper.AppendEofData(intermediateOutputFileName); File.Copy(intermediateOutputFileName, OutputFileName, true); } else { Errors.AddRange(errors.Select(error => new Error(ErrorSource.Assembly, ErrorSeverity.Error, error))); } } catch (ErrorException ex) { Errors.Add(ErrorSource.Assembly, ErrorSeverity.Error, ex.Message, ex.Details); } catch (Exception ex) { Errors.Add(ErrorSource.Assembly, ErrorSeverity.Error, "Unhandled " + ex.GetType() + " while assembling stub.", ex.GetFullStackTrace()); } }
private void CompileStub() { try { // Encrypt stage2 into Stage2Shellcode.inc using (AssemblyStream assembly = new AssemblyStream(GetIntermediateSourcePath("Stage2Shellcode.inc"))) { Helper.EncryptData ( GetIntermediateBinaryPath("stage2.shellcode"), GetIntermediateBinaryPath("stage2.shellcode_encrypted"), Project.Stub.Padding, out uint key, out uint paddingMask, out int paddingByteCount ); assembly.EmitConstant("Stage2Size", new FileInfo(GetIntermediateBinaryPath("stage2.shellcode")).Length.ToString()); assembly.EmitConstant("Stage2Key", "0x" + key.ToString("x8")); assembly.EmitConstant("Stage2PaddingMask", "0x" + paddingMask.ToString("x8")); assembly.EmitConstant("Stage2PaddingByteCount", paddingByteCount.ToString()); assembly.EmitFileData("Stage2Shellcode", @"..\bin\stage2.shellcode_encrypted"); } // Compile stub string[] stubLines = File.ReadAllLines(GetIntermediateSourcePath("Stub.asm")); using (AssemblyStream assembly = new AssemblyStream(GetIntermediateSourcePath("Stub.asm"))) { foreach (string line in stubLines) { if (line.Trim() == ";{RSRC}") { assembly.Indent = 0; bool hasVersionInfo = !Project.VersionInfo.IsEmpty; bool hasManifest = Project.Manifest.Template != null || Project.Manifest.Path != null; bool hasIcon = Project.Stub.IconPath != null; List <string> directory = new List <string>(); if (hasVersionInfo) { directory.Add("RT_VERSION, VersionInfo"); } if (hasManifest) { directory.Add("RT_MANIFEST, Manifest"); } if (hasIcon) { directory.AddRange(new[] { "RT_ICON, Icons", "RT_GROUP_ICON, GroupIcon" }); } if (directory.Any()) { assembly.WriteLine("section '.rsrc' resource data readable"); assembly.Indent = 4; assembly.EmitDefinition("directory", directory); if (hasVersionInfo) { assembly.EmitDefinition("resource VersionInfo,", "1, LANG_NEUTRAL, VersionInfoData"); } if (hasManifest) { assembly.EmitDefinition("resource Manifest,", "1, LANG_NEUTRAL, ManifestData"); } if (hasIcon) { Icon[] icon = IconExtractor.FromFile(Project.Stub.IconPath)?.Split(); if (icon == null) { throw new ErrorException("Could not read icon from file '" + Path.GetFileName(Project.Stub.IconPath) + "'."); } for (int i = 0; i < icon.Length; i++) { icon[i].Save(GetIntermediateSourcePath(@"Resources\icon-" + (i + 1) + ".ico")); } assembly.EmitDefinition("resource Icons,", Enumerable.Range(1, icon.Length).Select(i => i + ", LANG_NEUTRAL, IconData" + i)); assembly.EmitDefinition("resource GroupIcon,", "1, LANG_NEUTRAL, GroupIconData"); assembly.EmitDefinition("icon GroupIconData,", Enumerable.Range(1, icon.Length).Select(i => "IconData" + i + @", 'Resources\icon-" + i + ".ico'")); } if (hasVersionInfo) { assembly.EmitDefinition ( "versioninfo VersionInfoData,", "VOS__WINDOWS32, VFT_APP, VFT2_UNKNOWN, LANG_ENGLISH+SUBLANG_DEFAULT, 0", "'FileDescription', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.FileDescription) + "'", "'ProductName', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.ProductName) + "'", "'FileVersion', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.FileVersion) + "'", "'ProductVersion', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.ProductVersion) + "'", "'LegalCopyright', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.Copyright) + "'", "'OriginalFilename', '" + Helper.FasmEscapeDefinitionString(Project.VersionInfo.OriginalFilename) + "'" ); } if (hasManifest) { string manifestFileName; if (Project.Manifest.Template != null) { manifestFileName = Project.Manifest.Template.GetDescription(); } else if (Project.Manifest.Path != null) { manifestFileName = Path.GetFileNameWithoutExtension(Project.Manifest.Path); File.Copy(Project.Manifest.Path, GetIntermediateSourcePath(@"Resources\" + manifestFileName + ".manifest")); } else { throw new InvalidOperationException(); } assembly.WriteLine("\tresdata ManifestData"); assembly.WriteLine("\t\tfile 'Resources\\" + manifestFileName + ".manifest'"); assembly.WriteLine("\tendres"); } } } else { assembly.Indent = 0; assembly.WriteLine(line); } } } // Obfuscate code AssemblyObfuscator obfuscator = new AssemblyObfuscator(Path.Combine(IntermediateDirectorySource, "Obfuscator")); obfuscator.ObfuscateFile(GetIntermediateSourcePath("Stub.asm")); obfuscator.ObfuscateFile(GetIntermediateSourcePath("Emulator.asm")); } catch (ErrorException ex) { Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, ex.Message, ex.Details); } catch (Exception ex) { Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, "Unhandled " + ex.GetType() + " while compiling stub.", ex.GetFullStackTrace()); } }