I'm trying to work out the internals of how a Windows process starts and maintains communication with services.exe
. This is on Windows 8 x64, but if you have tips for Windows 7 that is fine too.
So far I figure out services.exe
does something approximately like this:
PROCESS_INFORMATION pi;
TCHAR szExe[] = _T("C:\\Program Files\\TestProgram\\myWindowsService.exe");
PROCESS_INFORMATION process_information = {0};
HKEY hOpen;
DWORD dwNumber = 0;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
STARTUPINFOEX startup_info;
SIZE_T attribute_list_size = 0;
ZeroMemory(&startup_info, sizeof(STARTUPINFOEX));
// can see EXTENDED_STARTUPINFO_PRESENT is used, but couldn't figure out if any/what attributes are added
BOOL status = InitializeProcThreadAttributeList(nullptr, 0, 0, &attribute_list_size);
PPROC_THREAD_ATTRIBUTE_LIST attribute_list = (PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, attribute_list_size);
startup_info.StartupInfo.cb = sizeof(STARTUPINFOEX);
startup_info.lpAttributeList = attribute_list;
startup_info.StartupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK;
startup_info.StartupInfo.wShowWindow= SW_HIDE;
if(CreateProcess(
NULL,
szExe,
NULL,
NULL,
FALSE,
CREATE_SUSPENDED |
CREATE_UNICODE_ENVIRONMENT |
DETACHED_PROCESS |
EXTENDED_STARTUPINFO_PRESENT,
NULL,
NULL,
&startup_info.StartupInfo,
&pi))
{
HANDLE hEvent;
hEvent=CreateEvent(NULL,FALSE,FALSE,NULL); // I traced this call during service sstartup; no idea what purpose it serves?
ResumeThread(pi.hThread);
Now my question is, how does the actual "Start" get communicated to the service? I know the service itself does something like this:
- main entry point (à la console program)
- Call
advapi!StartServiceCtrlDispatcher
- Goes to
sechost!StartServiceCtrlDispatcher
- This jumps into
sechost!QueryServiceDynamicInformation
I'm trying to figure out what method in services.exe
is used to hook into this start process. Ideally I want to be able to write a PoC code that can "launch" a simple Windows service and get it to start, without it being registered as a Windows Service, i.e. wrapped inside a "stand alone service control manager". I'm looking for some tips of what best to look for next.
There is also a reference to services.exe
in \\pipe\ntsvcs
. The Wikipedia article about SCM refers to \Pipe\Net\NtControlPipeX
being created, but as far as I can tell, that is in Windows 2000 (maybe XP) but I can't see this happening on Windows 8.