![]() ![]() I will divide this blog into multiple parts I have updated this blog after responding to a question on Reddit. ServiceSample.ChangeStartupType("NetTcpPortSharing", ServiceSample.This blog will be about how a “NotConfigured“ AppLocker policy can come back to haunt you. ServiceSample.CloseServiceHandle(scManager) ServiceSample.CloseServiceHandle(service) ![]() IntPtr service = ServiceSample.OpenService(scManager, serviceName, ServiceSample.SERVICE_CHANGE_CONFIG | ServiceSample.STANDARD_RIGHTS_WRITE) īool succeeded = ServiceSample.ChangeServiceConfig(service, ServiceSample.SERVICE_NO_CHANGE, startType, ServiceSample.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null) IntPtr scManager = ServiceSample.OpenSCManager(null, null, ServiceSample.SC_MANAGER_CONNECT) Private static bool ChangeStartupType(string serviceName, uint startType) Private static extern bool CloseServiceHandle(IntPtr hSCObject) Private static extern bool ChangeServiceConfig(IntPtr hService, uint dwServiceType, uint dwStartType, uint dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName) Private static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess) ![]() Private static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, uint dwDesiredAccess) ![]() Private const uint SERVICE_DISABLED = 0x4 Private const uint SERVICE_DEMAND_START = 0x3 Private const uint SERVICE_AUTO_START = 0x2 Private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF Private const uint STANDARD_RIGHTS_WRITE = 0x20000 Private const uint SERVICE_CHANGE_CONFIG = 0x2 Private const uint SC_MANAGER_CONNECT = 0x1 A proper implementation should use SafeHandle types to ensure proper cleanup and should add appropriate error checking. It does not contain any error handling and may leak resources. Note: This code is for demonstration purposes only. However, this requires that you first call OpenService for the specified service and that requires calling OpenSCManager beforehand (and don't forget to CloseServiceHandle when you're done!). Basically you'll want to call ChangeServiceConfig from. To some people, this is the most "pure" method, although it's more tricky to get right. ServiceSample.ChangeStartupType("NetTcpPortSharing", "Automatic") ManagementBaseObject outputParameters = serviceObject.InvokeMethod(MethodName, inputParameters, null) ManagementBaseObject inputParameters = serviceObject.GetMethodParameters(MethodName) Using (ManagementObject serviceObject = new ManagementObject(path)) ManagementPath path = new ManagementPath() namespace SampleĬonst string MethodName = "ChangeStartMode" Here we will use WMI functionality to ChangeStartMode for the service. This requires an assembly reference for. ServiceSample.ChangeStartupType("NetTcpPortSharing", "auto") Using (Process sc = Process.Start("sc.exe", arguments)) Private static bool ChangeStartupType(string serviceName, string startupType) This is similar to the solution you mention above except there is no registry hacking required. This will involve shelling out to sc.exe and changing the startup type of the service via command line arguments. Note that all of these solutions require administrative rights and must run in an elevated process. There is more than one way to do this, depending on how "pure" of a solution you want. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |