Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed C# exe failing generation when set to PowerShell #767

Merged
merged 6 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading