Created
December 3, 2021 10:07
-
-
Save DarkCoderSc/60a18484fbda7bbb2a1ec0f2b1d42cb7 to your computer and use it in GitHub Desktop.
A ported version of PowerRunAsAttached to C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*------------------------------------------------------------------------------- | |
.Developer | |
Jean - Pierre LESUEUR(@DarkCoderSc) | |
https://www.twitter.com/darkcodersc | |
https://github.com/DarkCoderSc | |
www.phrozen.io | |
[email protected] | |
PHROZEN | |
.License | |
Apache License | |
Version 2.0, January 2004 | |
http://www.apache.org/licenses/ | |
.Todo | |
- Better command line argument handling | |
Usage: SharpRunAsAttached <username> <password> <domain> | |
-------------------------------------------------------------------------------*/ | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Security; | |
namespace SharpRunAsAttached | |
{ | |
public class RunAsAttached | |
{ | |
private string Username = ""; | |
private SecureString SecurePassword = null; | |
private string Domain = ""; | |
private Process Proc = null; | |
private readonly object CriticalSection = new object(); | |
public void ReadStdThread(Process proc, bool stdErr = false) | |
{ | |
StreamReader reader = null; | |
if (stdErr) | |
{ | |
reader = proc.StandardError; | |
} | |
else | |
{ | |
reader = proc.StandardOutput; | |
} | |
char[] buffer = new char[1024]; | |
while (!proc.HasExited) | |
{ | |
int read = reader.ReadAsync(buffer, 0, buffer.Length).Result; | |
lock (this.CriticalSection) | |
{ | |
if (stdErr) { Console.ForegroundColor = ConsoleColor.Red; } | |
Console.Write(buffer, 0, read); | |
if (stdErr) { Console.ResetColor(); } | |
} | |
} | |
} | |
public RunAsAttached(string username, string password, string domain = "") | |
{ | |
this.Username = username; | |
this.Domain = domain; | |
this.SecurePassword = new NetworkCredential("", password).SecurePassword; | |
} | |
public void Spawn() | |
{ | |
ProcessStartInfo processInformation = new ProcessStartInfo(); | |
processInformation.FileName = "cmd.exe"; | |
processInformation.WorkingDirectory = Environment.GetEnvironmentVariable("SystemRoot"); | |
processInformation.UseShellExecute = false; | |
processInformation.RedirectStandardError = true; | |
processInformation.RedirectStandardOutput = true; | |
processInformation.RedirectStandardInput = true; | |
processInformation.Domain = this.Domain; | |
processInformation.UserName = this.Username; | |
processInformation.Password = this.SecurePassword; | |
processInformation.Arguments = ""; | |
processInformation.CreateNoWindow = true; | |
Console.WriteLine(processInformation.FileName); | |
this.Proc = new Process(); | |
this.Proc.StartInfo = processInformation; | |
this.Proc.Start(); | |
Thread stdOutReader = new Thread(() => this.ReadStdThread(this.Proc)); | |
Thread stdErrReader = new Thread(() => this.ReadStdThread(this.Proc, true)); | |
stdOutReader.Start(); | |
stdErrReader.Start(); | |
while (!this.Proc.HasExited) | |
{ | |
this.Proc.StandardInput.WriteLine(Console.ReadLine()); | |
} | |
Console.WriteLine("..."); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(args.Length); | |
if (args.Length < 2) | |
{ | |
throw new ArgumentException("Missing mandatory arguments. Ex: SharpRunAsAttached <username> <password>"); | |
} | |
string domain = ""; | |
if (args.Length == 3) | |
{ | |
domain = args[2]; | |
} | |
RunAsAttached runAsAttached = new RunAsAttached(args[0], args[1], domain); | |
runAsAttached.Spawn(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment