Skip to content

Commit

Permalink
Fixed C# exe failing generation when set to PowerShell (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cx01N authored Jan 12, 2025
1 parent b9a2d98 commit 321b5ca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed issue with C# exe and shellcode not compiling PowerShell stagers
- Fix delay/jitter adjustment in python agent (@janit0rjoe)

## [5.12.1] - 2025-01-08
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions empire/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def reset():
f"{CSHARP_DIR_BASE}/Data/Tasks/CSharp/Compiled/netcoreapp3.0"
)

file_util.clear_file_contents(
f"{CSHARP_DIR_BASE}/Data/EmbeddedResources/launcher.txt"
)

if os.path.exists(empire_config.starkiller.directory):
shutil.rmtree(empire_config.starkiller.directory)

Expand Down
43 changes: 21 additions & 22 deletions empire/server/stagers/CSharpPS.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,34 @@
using System.Management.Automation.Runspaces;
using System.IO;
using System.Reflection;
public static class Program
{
public static void Main(string[] args)
{
PowerShell ps = PowerShell.Create();
public static void Main(string[] args)
{
try
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "launcher.txt";
PowerShell ps = PowerShell.Create();
string[] names = assembly.GetManifestResourceNames();
try
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "launcher.txt";
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
{
string script = reader.ReadToEnd();
ps.AddScript(script);
}
ps.Invoke();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message.ToString());
}
string[] names = assembly.GetManifestResourceNames();
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
{
string script = reader.ReadToEnd();
ps.AddScript(script);
}
ps.Invoke();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message.ToString());
}
}
}
TaskingType: Assembly
UnsafeCompile: false
Expand Down
13 changes: 13 additions & 0 deletions empire/server/utils/file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def remove_file(path: str) -> None:
os.remove(path)


def clear_file_contents(path: str) -> None:
"""
Clears the contents of a file without deleting it.
If the file doesn't exist, it creates an empty file.
"""
try:
with open(path, "w"):
pass
log.debug(f"Cleared contents of the file: {path}")
except Exception as e:
log.error(f"Failed to clear file contents for {path}: {e}", exc_info=True)


def run_as_user(command, user=None, cwd=None):
"""
Runs a command as a specified user or the user who invoked sudo.
Expand Down

0 comments on commit 321b5ca

Please sign in to comment.