Created
June 14, 2017 09:15
-
-
Save benpturner/44310d0a842a16cfe066b3daa69b8975 to your computer and use it in GitHub Desktop.
Simple C# Service
This file contains 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
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