Skip to content

Instantly share code, notes, and snippets.

@benpturner
Created June 14, 2017 09:15
Show Gist options
  • Save benpturner/44310d0a842a16cfe066b3daa69b8975 to your computer and use it in GitHub Desktop.
Save benpturner/44310d0a842a16cfe066b3daa69b8975 to your computer and use it in GitHub Desktop.
Simple C# Service
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace RedTeamingService
{
public partial class SystemService : ServiceBase
{
public static int pid = 0;
public SystemService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe");
psi.Arguments = @"-e cwB0AGEAcgB0AC0AcAByAG8AYwBlAHMAcwAgAGMAYQBsAGMALgBlAHgAZQA=";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
System.Diagnostics.Process processone;
processone = System.Diagnostics.Process.Start(psi);
pid = processone.Id;
}
catch
{
// do something
}
}
protected override void OnStop()
{
try
{
Process.GetProcessById(pid).Kill();
}
catch (Exception ex)
{
// do nothing
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment