Login Register






Register, Delete, and Start Service Win32API | Snippet filter_list
Author
Message
Register, Delete, and Start Service Win32API | Snippet #1
Yo.

With ven0m still in the works and me still slaving away at coding security and functionality, I found myself in the need of a service to set as the supervisor of a hypervisor. Then I realized that I needed to load said service, but didn't want to rely on SC or any other VS dependencies, so I coded my own function to assist in loading the services for me.

This most likely has improper conversions and bad habits among lots of other things, but it works.

Also, note that I use recursion in the case that it can't create the service, in my case this is because a prior service under the same name has to be deleted and is currently marked to be removed, so I just call the function again and that fixes it. THIS IS DANGEROUS, and could very easily lead to an infinite loop if there is a legitimate error with CreateService that Opening and deleting a service with the same name doesn't solve. I would recommend using  GetFileInformationByHandleEx with FILE_STANDARD_INFO and just check if it's marked for deletion, and with that, you'll be able to discern if it's the right error, or use Marshal.GetLastWin32Error, which would be much simpler.

I've now updated the code so that it re-initializes the SC manager before trying to create the service, this fixes any problems with services being marked for deletion.
Also, not that this uses my Native class, you can find it here: https://sinister.ly/Thread-Useful-Native...et-Snippet

And with that out of the way, here you go, I look forward to any criticisms, as this was made at 2 in the morning and I'm sure I fucked some stuff up.

Code:
Function InitializeService(ByVal servicename As String, ByVal path As String, ByVal createandstart As Boolean, ByVal servicetype As Object, ParamArray CommandLineArgs() As String) As IntPtr Dim displayname As String = servicename Dim quotedpath As String = """" + path + """" Dim scHandle As IntPtr = Native.OpenSCManager(Nothing, Nothing, Native.SC_MANAGER_ALL_ACCESS) If scHandle.Equals(IntPtr.Zero) _ Then Throw New Exception("Unable to open SC manager, Win32 error code: " + Marshal.GetLastWin32Error.ToString) Dim hHandle As IntPtr = Native.OpenService(scHandle, servicename, Native.SERVICE_ALL_ACCESS) If Not hHandle.Equals(IntPtr.Zero) Then Dim servicestatus As New Native.SERVICE_STATUS If Not Native.QueryServiceStatus(hHandle, servicestatus) _ Then Throw New Exception("Unable to open query service status, Win32 error code: " + Marshal.GetLastWin32Error.ToString) If servicestatus.dwCurrentState = Native.SERVICE_RUNNING Or Native.SERVICE_START_PENDING Then If Not Native.ControlService(hHandle, Native.SERVICE_CONTROL_STOP, servicestatus) _ Then Throw New Exception("Unable to control service, Win32 error code: " + Marshal.GetLastWin32Error.ToString) End If If Native.DeleteService(hHandle).Equals(IntPtr.Zero) _ Then Throw New Exception("Unable to delete service, Win32 error code: " + Marshal.GetLastWin32Error.ToString) End If Native.CloseServiceHandle(hHandle) Native.CloseServiceHandle(scHandle) scHandle = Native.OpenSCManager(Nothing, Nothing, Native.SC_MANAGER_ALL_ACCESS) If createandstart Then Dim newhHandle As IntPtr = Native.CreateService(scHandle, servicename, displayname, Native.SERVICE_ALL_ACCESS, servicetype, 'EX: Normal service: SERVICE_WIN32_OWN_PROCESS or for Kernel Driver: SERVICE_KERNEL_DRIVER Native.SERVICE_DEMAND_START, Native.SERVICE_ERROR_NORMAL, quotedpath, Nothing, 0, Nothing, Nothing, Nothing) If newhHandle.Equals(IntPtr.Zero) _ Then Throw New Exception("Unable to create service, Win32 error code: " + Marshal.GetLastWin32Error.ToString) If Native.StartService(newhHandle, If(CommandLineArgs Is Nothing, 0, CommandLineArgs.Count), If(CommandLineArgs Is Nothing, Nothing, CommandLineArgs.ToString)).Equals(IntPtr.Zero) _ Then Throw New Exception("Unable to start service, Win32 error code: " + Marshal.GetLastWin32Error.ToString) Native.CloseServiceHandle(scHandle) Return newhHandle Else Return 1 End If End Function

Reply