public string ListenerCompileGruntExecutorCode(HttpListener listener, Grunt grunt, HttpProfile profile, bool Compress = false) { Common.DotNetVersion version = Common.DotNetVersion.Net35; switch (grunt.DotNetFrameworkVersion) { case DotNetVersion.Net35: version = Common.DotNetVersion.Net35; break; case DotNetVersion.Net40: version = Common.DotNetVersion.Net40; break; case DotNetVersion.NetCore21: version = Common.DotNetVersion.NetCore21; break; } byte[] ILBytes = Compiler.Compile(new Compiler.CompilationRequest { Source = this.ListenerGetGruntExecutorCode(listener, grunt, profile), TargetDotNetVersion = version, OutputKind = OutputKind.DynamicallyLinkedLibrary, References = grunt.DotNetFrameworkVersion == DotNetVersion.Net35 ? Common.DefaultNet35References : Common.DefaultNet40References }); if (ILBytes == null || ILBytes.Length == 0) { throw new CovenantCompileGruntStagerFailedException("Compiling Grunt code failed"); } if (Compress) { ILBytes = Utilities.Compress(ILBytes); } return(Convert.ToBase64String(ILBytes)); }
public async Task <GruntTask> GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35) { string lower = name.ToLower(); GruntTask task = _context.gruntTasks .Where(T => T.Name.ToLower() == lower) // .Where(T => T.CompatibleDotNetVersions.Contains(version)) /*.Include(T => T.Options) * .Include(T => T.Author) * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary") * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary.ReferenceSourceLibraryReferenceAssemblies.ReferenceAssembly") * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary.ReferenceSourceLibraryEmbeddedResources.EmbeddedResource") * .Include("GruntTaskReferenceAssemblies.ReferenceAssembly") * .Include("GruntTaskEmbeddedResources.EmbeddedResource")*/ .AsEnumerable() .Where(T => T.CompatibleDotNetVersions.Contains(version)) .FirstOrDefault(); if (task == null) { // Probably bad performance here task = _context.gruntTasks /*.Include(T => T.Options) * .Include(T => T.Author) * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary") * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary.ReferenceSourceLibraryReferenceAssemblies.ReferenceAssembly") * .Include("GruntTaskReferenceSourceLibraries.ReferenceSourceLibrary.ReferenceSourceLibraryEmbeddedResources.EmbeddedResource") * .Include("GruntTaskReferenceAssemblies.ReferenceAssembly") * .Include("GruntTaskEmbeddedResources.EmbeddedResource")*/ .AsEnumerable() .Where(T => T.Aliases.Any(A => A.Equals(lower, StringComparison.CurrentCultureIgnoreCase))) .Where(T => T.CompatibleDotNetVersions.Contains(version)) .FirstOrDefault(); if (task == null) { throw new ControllerNotFoundException($"NotFound - GruntTask with Name: {name}"); } } return(await Task.FromResult(task)); }
public string Compile(string TaskCode, List <string> Parameters, List <string> ReferenceAssemblies, Common.DotNetVersion dotNetFrameworkVersion) { List <Compiler.Reference> references = Common.SharpSploitReferences; references.AddRange(Common.DefaultReferences); byte[] compiled = Compiler.Compile(new Compiler.CompilationRequest { Source = TaskCode, SourceDirectory = Common.CovenantSrcDirectory, ResourceDirectory = Common.CovenantResourceDirectory, ReferenceDirectory = Common.CovenantReferenceDirectory, TargetDotNetVersion = dotNetFrameworkVersion, References = references, EmbeddedResources = Common.SharpSploitEmbeddedResources, Confuse = true }); this.GruntTaskingAssembly = Convert.ToBase64String(Utilities.Compress(compiled)); foreach (string Parameter in Parameters) { this.GruntTaskingAssembly += "," + Convert.ToBase64String(Common.CovenantEncoding.GetBytes(Parameter)); } return(this.GruntTaskingAssembly); }
public string Compile(string TaskCode, List <string> Parameters, List <string> ReferenceAssemblies, List <string> ReferenceSourceLibraries, List <string> EmbeddedResources, Common.DotNetVersion dotNetFrameworkVersion) { List <Compiler.Reference> references = Common.DefaultReferences; ReferenceAssemblies.ForEach(RA => { references.AddRange( new List <Compiler.Reference> { new Compiler.Reference { File = RA, Framework = Common.DotNetVersion.Net35, Enabled = true }, new Compiler.Reference { File = RA, Framework = Common.DotNetVersion.Net40, Enabled = true } } ); }); List <Compiler.EmbeddedResource> resources = EmbeddedResources.Select(ER => { return(new Compiler.EmbeddedResource { Name = ER, File = ER, Platform = Platform.X64, Enabled = true }); }).ToList(); byte[] compiled = Compiler.Compile(new Compiler.CompilationRequest { Source = TaskCode, SourceDirectory = ReferenceSourceLibraries == null || ReferenceSourceLibraries.Count == 0 ? null : Common.CovenantSrcDirectory + Path.DirectorySeparatorChar + ReferenceSourceLibraries[0], ResourceDirectory = Common.CovenantResourceDirectory, ReferenceDirectory = Common.CovenantReferenceDirectory, TargetDotNetVersion = dotNetFrameworkVersion, References = references, EmbeddedResources = resources, Confuse = true, Optimize = !ReferenceSourceLibraries.Contains("Rubeus") // TODO: Fix optimization to work with Rubeus }); this.GruntTaskingAssembly = Convert.ToBase64String(Utilities.Compress(compiled)); foreach (string Parameter in Parameters) { this.GruntTaskingAssembly += "," + Convert.ToBase64String(Common.CovenantEncoding.GetBytes(Parameter)); } return(this.GruntTaskingAssembly); }
public Task <GruntTask> GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35) { return(_connection.InvokeAsync <GruntTask>("GetGruntTaskByName", name, version)); }
public Task <ReferenceAssembly> GetReferenceAssemblyByName(string name, Common.DotNetVersion version) { return(_connection.InvokeAsync <ReferenceAssembly>("GetReferenceAssemblyByName", name, version)); }
static void Main(string[] args) { CommandLineApplication app = new CommandLineApplication(); app.HelpOption("-? | -h | --help"); app.ThrowOnUnexpectedArgument = false; // Required arguments CommandOption OutputFileOption = app.Option( "-f | --file <OUTPUT_FILE>", "The output file to write to.", CommandOptionType.SingleValue ).IsRequired(); // Compilation-related arguments CommandOption <Common.DotNetVersion> DotNetVersionOption = app.Option <Common.DotNetVersion>( "-d | --dotnet | --dotnet-framework <DOTNET_VERSION>", "The Dotnet Framework version to target (net35 or net40).", CommandOptionType.SingleValue ); DotNetVersionOption.Validators.Add(new MustBeDotNetVersionValidator()); CommandOption OutputKindOption = app.Option( "-o | --output-kind <OUTPUT_KIND>", "The OutputKind to use (dll or exe).", CommandOptionType.SingleValue ); OutputKindOption.Validators.Add(new MustBeOutputKindValidator()); CommandOption PlatformOption = app.Option( "-p | --platform <PLATFORM>", "The Platform to use (AnyCpu, x86, or x64).", CommandOptionType.SingleValue ); PlatformOption.Validators.Add(new MustBePlatformValidator()); CommandOption OptimizationOption = app.Option( "-O | --optimization", "Use source code optimization.", CommandOptionType.NoValue ); PlatformOption.Validators.Add(new MustBePlatformValidator()); CommandOption UnsafeCompilingOption = app.Option( "-u | --unsafecompiling", "Use unsafe compiling.", CommandOptionType.NoValue ); PlatformOption.Validators.Add(new MustBePlatformValidator()); CommandOption ConfuseOption = app.Option( "-c | --confuse", "Obfuscate with ConfuserEx", CommandOptionType.NoValue ); // Source-related arguments CommandOption SourceFileOption = app.Option( "-s | --source-file <SOURCE_FILE>", "The Main source code file to compile.", CommandOptionType.SingleValue ).Accepts(v => v.ExistingFile()).IsRequired(); //CommandOption ReferenceOption = app.Option( // "-r | --references <ref1.dll,ref2.dll>", // "References used by the target project", // CommandOptionType.MultipleValue //); app.OnExecute(() => { // Check dotnet version Common.DotNetVersion TargetDotNetVersion = DotNetVersionOption.HasValue() ? DotNetVersionOption.ParsedValue : Common.DotNetVersion.Net40; bool net35 = (TargetDotNetVersion.ToString().Contains("35")) ? true : false; // Check output format OutputKind TargetOutputKind = OutputKind.ConsoleApplication; if (OutputKindOption.HasValue()) { if (OutputKindOption.Value().Contains("console", StringComparison.OrdinalIgnoreCase) || OutputKindOption.Value().Contains("exe", StringComparison.OrdinalIgnoreCase)) { TargetOutputKind = OutputKind.ConsoleApplication; } else if (OutputKindOption.Value().Contains("dll", StringComparison.OrdinalIgnoreCase) || OutputKindOption.Value().Contains("dynamicallylinkedlibrary", StringComparison.OrdinalIgnoreCase)) { TargetOutputKind = OutputKind.DynamicallyLinkedLibrary; } } else if (OutputFileOption.HasValue()) { if (OutputFileOption.Value().EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { TargetOutputKind = OutputKind.ConsoleApplication; } else if (OutputFileOption.Value().EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { TargetOutputKind = OutputKind.DynamicallyLinkedLibrary; } } // Check platform Platform TargetPlatform = Platform.AnyCpu; if (PlatformOption.HasValue()) { if (PlatformOption.Value().Equals("x86", StringComparison.OrdinalIgnoreCase)) { TargetPlatform = Platform.X86; } else if (PlatformOption.Value().Equals("x64", StringComparison.OrdinalIgnoreCase)) { TargetPlatform = Platform.X64; } else if (PlatformOption.Value().Equals("AnyCpu", StringComparison.OrdinalIgnoreCase)) { TargetPlatform = Platform.AnyCpu; } } List <Compiler.Reference> references = net35 ? Common.Net35References : Common.Net40References; string TargetRefs = Path.GetDirectoryName(SourceFileOption.Value()) + Path.DirectorySeparatorChar + "Refs"; if (File.Exists(TargetRefs)) { foreach (string Reference in File.ReadAllLines(TargetRefs)) { if (!Reference.StartsWith("#")) { references.Add(new Compiler.Reference { File = net35 ? Common.Net35Directory + Reference : Common.Net40Directory + Reference, Framework = net35 ? Common.DotNetVersion.Net35 : Common.DotNetVersion.Net40, Enabled = true }); } } } else { // Import all references foreach (string Reference in Directory.GetFiles(net35 ? Common.Net35Directory : Common.Net40Directory, "*.dll", SearchOption.AllDirectories)) { references.Add(new Compiler.Reference { File = Reference, Framework = net35 ? Common.DotNetVersion.Net35 : Common.DotNetVersion.Net40, Enabled = true }); } } // Leaving it here for opsec pros // Load default references and add optional references //List<Compiler.Reference> references = net35 ? Common.DefaultNet35References : Common.DefaultNet40References; //if(ReferenceOption.HasValue()){ // foreach(string Reference in ReferenceOption.Value().Split(',')){ // references.Add(new Compiler.Reference // { // File = net35 ? Common.Net35Directory + Reference : Common.Net40Directory + Reference, // Framework = net35 ? Common.DotNetVersion.Net35 : Common.DotNetVersion.Net40, // Enabled = true // }); // } //} // Check for Refs file in source folder //string TargetRefs = Path.GetDirectoryName(SourceFileOption.Value()) + Path.DirectorySeparatorChar + "Refs"; //Console.WriteLine(TargetRefs); //if(File.Exists(TargetRefs)){ // foreach(string Reference in File.ReadAllLines(TargetRefs)){ // references.Add(new Compiler.Reference // { // File = net35 ? Common.Net35Directory + Reference : Common.Net40Directory + Reference, // Framework = net35 ? Common.DotNetVersion.Net35 : Common.DotNetVersion.Net40, // Enabled = true // }); // } //} //Compile File.WriteAllBytes(OutputFileOption.Value(), Compiler.Compile(new Compiler.CompilationRequest { Source = File.ReadAllText(SourceFileOption.Value()), SourceFile = SourceFileOption.Value(), SourceDirectories = new[] { Path.GetDirectoryName(SourceFileOption.Value()) }, TargetDotNetVersion = net35 ? Common.DotNetVersion.Net35 : Common.DotNetVersion.Net40, OutputKind = TargetOutputKind, Platform = TargetPlatform, References = references, UnsafeCompile = UnsafeCompilingOption.HasValue(), Confuse = ConfuseOption.HasValue(), // TODO: Fix optimization to work with GhostPack Optimize = OptimizationOption.HasValue() })); }); // app.Execute(args); }
//Reference Assembly methods public async Task <ReferenceAssembly> GetReferenceAssemblyByName(string name, Common.DotNetVersion version) { ReferenceAssembly assembly = _context.referenceAssemblies.First(asm => asm.Name == name); return(assembly); }
public byte[] CompileExe(GruntTask task, Common.DotNetVersion version, OutputKind outputKind, Boolean Compress) { byte[] ILBytes = null; if (version == Common.DotNetVersion.Net35 || version == Common.DotNetVersion.Net40) { List <Compiler.Reference> references = null; switch (version) { case Common.DotNetVersion.Net35: references = Common.DefaultNet35References; break; case Common.DotNetVersion.Net40: references = Common.DefaultNet40References; break; } ILBytes = Compiler.Compile(new Compiler.CsharpFrameworkCompilationRequest { Language = task.Language, Source = task.Code, TargetDotNetVersion = version, OutputKind = outputKind, References = references }); } else if (version == Common.DotNetVersion.NetCore31) { string src = task.Code; string sanitizedName = Utilities.GetSanitizedFilename(task.Name); string dir = Common.CovenantDataDirectory + "Grunt" + Path.DirectorySeparatorChar + sanitizedName + Path.DirectorySeparatorChar; string ResultName; /*if (template.StagerCode == CodeTemplate) * { * ResultName = sanitizedName + "Stager"; * dir += sanitizedName + "Stager" + Path.DirectorySeparatorChar; * string file = sanitizedName + "Stager" + Utilities.GetExtensionForLanguage(template.Language); * File.WriteAllText(dir + file, src); * }*/ if (true) { ResultName = sanitizedName; dir += sanitizedName + Path.DirectorySeparatorChar; string file = sanitizedName + Utilities.GetExtensionForLanguage(task.Language); File.WriteAllText(dir + file, src); } ILBytes = Compiler.Compile(new Compiler.CsharpCoreCompilationRequest { ResultName = ResultName, Language = task.Language, TargetDotNetVersion = version, SourceDirectory = dir, OutputKind = outputKind, //update this to not be hardcoded RuntimeIdentifier = Compiler.RuntimeIdentifier.win_x64, UseSubprocess = true }); } if (ILBytes == null || ILBytes.Length == 0) { throw new CovenantCompileGruntStagerFailedException("Compiling Grunt code failed"); } if (Compress) { ILBytes = Utilities.Compress(ILBytes); } return(ILBytes); }