生活的天平本不平衡,只有通过努力改变其偏向。

SvcHost.exe 调用的服务原理与实践

2008-05-12

1. 多个服务共享一个Svchost.exe进程利与弊

windows 系统服务分为独立进程和共享进程两种,在windows NT时只有服务器管理器SCM(Services.exe)有多个共享服务,随着系统内置服务的增加,在windows 2000中ms又把很多服务做成共享方式,由svchost.exe启动。windows 2000一般有2个svchost进程,一个是RPCSS(Remote Procedure Call)服务进程,另外一个则是由很多服务共享的一个svchost.exe。而在windows XP中,则一般有4个以上的svchost.exe服务进程,windows 2003 server中则更多,可以看出把更多的系统内置服务以共享进程方式由svchost启动是ms的一个趋势。这样做在一定程度上减少了系统资源的消耗,不 过也带来一定的不稳定因素,因为任何一个共享进程的服务因为错误退出进程就会导致整个进程中的所有服务都退出。另外就是有一点安全隐患,首先要介绍一下 svchost.exe的实现机制。

2. Svchost原理

Svchost本身只是作为服务宿主,并不实现任何服务功能,需要Svchost启动的服务以动态链接库形式实现,在安装这些服务时,把服务的可执行程序指向svchost,启动这些服务时由svchost调用相应服务的动态链接库来启动服务。

那 么svchost如何知道某一服务是由哪个动态链接库负责呢?这不是由服务的可执行程序路径中的参数部分提供的,而是服务在注册表中的参数设置的,注册表 中服务下边有一个Parameters子键其中的ServiceDll表明该服务由哪个动态链接库负责。并且所有这些服务动态链接库都必须要导出一个 ServiceMain()函数,用来处理服务任务。

例如rpcss(Remote Procedure Call)在注册表中的位置是 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs,它的参数子键Parameters里有这样一项:

“ServiceDll”=REG_EXPAND_SZ:”%SystemRoot%\system32\rpcss.dll”

当启动rpcss服务时,svchost就会调用rpcss.dll,并且执行其ServiceMain()函数执行具体服务。

既然这些服务是使用共享进程方式由svchost启动的,为什么系统中会有多个svchost进程呢?ms把这些服务分为几组,同组服务共享一个svchost进程,不同组服务使用多个svchost进程,组的区别是由服务的可执行程序后边的参数决定的。

例如rpcss在注册表中 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs 有这样一项:

“ImagePath”=REG_EXPAND_SZ:”%SystemRoot%\system32\svchost -k rpcss”

因此rpcss就属于rpcss组,这在服务管理控制台也可以看到。

svchost 的所有组和组内的所有服务都在注册表的如下位置: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost,例如windows 2000共有4组rpcss、netsvcs、wugroup、BITSgroup,其中最多的就是netsvcs=REG_MULTI_SZ: EventSystem.Ias.Iprip.Irmon.Netman.Nwsapagent.Rasauto.Rasman.Remoteaccess.SENS.Sharedaccess.Tapisrv.Ntmssvc.wzcsvc..

在启动一个svchost.exe负责的服务时,服务管理器如果遇到可执行程序内容ImagePath已经存在于服务管理器的映象库中,就不在启动第2个进程svchost,而是直接启动服务。这样就实现了多个服务共享一个svchost进程。

3. Svchost代码

现 在我们基本清楚svchost的原理了,但是要自己写一个DLL形式的服务,由svchost来启动,仅有上边的信息还有些问题不是很清楚。比如我们在导 出的ServiceMain()函数中接收的参数是ANSI还是Unicode?我们是否需要调用RegisterServiceCtrlHandler 和StartServiceCtrlDispatcher来注册服务控制及调度函数?

这些问题要通过查看svchost代码获得。下边的代码是windows 2000+ service pack 4 的svchost反汇编片段,可以看出svchost程序还是很简单的。

主 函数首先调用ProcCommandLine()对命令行进行分析,获得要启动的服务组,然后调用SvcHostOptions()查询该服务组的选项和 服务组的所有服务,并使用一个数据结构 svcTable 来保存这些服务及其服务的DLL,然后调用PrepareSvcTable() 函数创建SERVICE_TABLE_ENTRY 结构,把所有处理函数SERVICE_MAIN_FUNCTION 指向自己的一个函数FuncServiceMain(),最后调用API StartServiceCtrlDispatcher() 注册这些服务的调度函数。

代码
  1. ; =============================== Main Funcion ===========================================
  2. .text:010010B8 public start
  3. .text:010010B8 start proc near
  4. .text:010010B8 push esi
  5. .text:010010B9 push edi
  6. .text:010010BA push offset sub_1001EBA ; lpTopLevelExceptionFilter
  7. .text:010010BF xor edi, edi
  8. .text:010010C1 call ds:SetUnhandledExceptionFilter
  9. .text:010010C7 push 1 ; uMode
  10. .text:010010C9 call ds:SetErrorMode
  11. .text:010010CF call ds:GetProcessHeap
  12. .text:010010D5 push eax
  13. .text:010010D6 call sub_1001142
  14. .text:010010DB mov eax, offset dword_1003018
  15. .text:010010E0 push offset unk_1003000 ; lpCriticalSection
  16. .text:010010E5 mov dword_100301C, eax
  17. .text:010010EA mov dword_1003018, eax
  18. .text:010010EF call ds:InitializeCriticalSection
  19. .text:010010F5 call ds:GetCommandLineW
  20. .text:010010FB push eax ; lpString
  21. .text:010010FC call ProcCommandLine
  22. .text:01001101 mov esi, eax
  23. .text:01001103 test esi, esi
  24. .text:01001105 jz short lab_doservice
  25. .text:01001107 push esi
  26. .text:01001108 call SvcHostOptions
  27. .text:0100110D call PrepareSvcTable
  28. .text:01001112 mov edi, eax ; SERVICE_TABLE_ENTRY returned
  29. .text:01001114 test edi, edi
  30. .text:01001116 jz short loc_1001128
  31. .text:01001118 mov eax, [esi+10h]
  32. .text:0100111B test eax, eax
  33. .text:0100111D jz short loc_1001128
  34. .text:0100111F push dword ptr [esi+14h] ; dwCapabilities
  35. .text:01001122 push eax ; int
  36. .text:01001123 call InitializeSecurity
  37. .text:01001128
  38. .text:01001128 loc_1001128: ; CODE XREF: start+5Ej
  39. .text:01001128 ; start+65j
  40. .text:01001128 push esi ; lpMem
  41. .text:01001129 call HeapFreeMem
  42. .text:0100112E
  43. .text:0100112E lab_doservice: ; CODE XREF: start+4Dj
  44. .text:0100112E test edi, edi
  45. .text:01001130 jz ExitProgram
  46. .text:01001136 push edi ; lpServiceStartTable
  47. .text:01001137 call ds:StartServiceCtrlDispatcherW
  48. .text:0100113D jmp ExitProgram
  49. .text:0100113D start endp
  50. ; =============================== Main Funcion end ===========================================

由于svchost为该组的所有服务都注册了svchost中的一个处理函数,因此每次启动任何一个服务时,服务管理器SCM都会调用 FuncServiceMain() 这个函数。这个函数使用 svcTable 查询要启动的服务使用的DLL,调用DLL导出的ServiceMain()函数来启动服务,然后返回。

代码
  1. ; ============================== FuncServiceMain() ===========================================
  2. .text:01001504 FuncServiceMain proc near ; DATA XREF: PrepareSvcTable+44o
  3. .text:01001504
  4. .text:01001504 arg_0 = dword ptr 8
  5. .text:01001504 arg_4 = dword ptr 0Ch
  6. .text:01001504
  7. .text:01001504 push ecx
  8. .text:01001505 mov eax, [esp+arg_4]
  9. .text:01001509 push ebx
  10. .text:0100150A push ebp
  11. .text:0100150B push esi
  12. .text:0100150C mov ebx, offset unk_1003000
  13. .text:01001511 push edi
  14. .text:01001512 mov edi, [eax]
  15. .text:01001514 push ebx
  16. .text:01001515 xor ebp, ebp
  17. .text:01001517 call ds:EnterCriticalSection
  18. .text:0100151D xor esi, esi
  19. .text:0100151F cmp dwGroupSize, esi
  20. .text:01001525 jbe short loc_1001566
  21. .text:01001527 and [esp+10h], esi
  22. .text:0100152B
  23. .text:0100152B loc_100152B: ; CODE XREF: FuncServiceMain+4Aj
  24. .text:0100152B mov eax, svcTable
  25. .text:01001530 mov ecx, [esp+10h]
  26. .text:01001534 push dword ptr [eax+ecx]
  27. .text:01001537 push edi
  28. .text:01001538 call ds:lstrcmpiW
  29. .text:0100153E test eax, eax
  30. .text:01001540 jz short StartThis
  31. .text:01001542 add dword ptr [esp+10h], 0Ch
  32. .text:01001547 inc esi
  33. .text:01001548 cmp esi, dwGroupSize
  34. .text:0100154E jb short loc_100152B
  35. .text:01001550 jmp short loc_1001566
  36. .text:01001552 ; =================================================
  37. .text:01001552
  38. .text:01001552 StartThis: ; CODE XREF: FuncServiceMain+3Cj
  39. .text:01001552 mov ecx, svcTable
  40. .text:01001558 lea eax, [esi+esi*2]
  41. .text:0100155B lea eax, [ecx+eax*4]
  42. .text:0100155E push eax
  43. .text:0100155F call GetDLLServiceMain
  44. .text:01001564 mov ebp, eax ; dll ServiceMain Function address
  45. .text:01001566
  46. .text:01001566 loc_1001566: ; CODE XREF: FuncServiceMain+21j
  47. .text:01001566 ; FuncServiceMain+4Cj
  48. .text:01001566 push ebx
  49. .text:01001567 call ds:LeaveCriticalSection
  50. .text:0100156D test ebp, ebp
  51. .text:0100156F jz short loc_100157B
  52. .text:01001571 push [esp+10h+arg_4]
  53. .text:01001575 push [esp+14h+arg_0]
  54. .text:01001579 call ebp
  55. .text:0100157B
  56. .text:0100157B loc_100157B: ; CODE XREF: FuncServiceMain+6Bj
  57. .text:0100157B pop edi
  58. .text:0100157C pop esi
  59. .text:0100157D pop ebp
  60. .text:0100157E pop ebx
  61. .text:0100157F pop ecx
  62. .text:01001580 retn 8
  63. .text:01001580 FuncServiceMain endp ; sp = -8
  64. ; ============================== FuncServiceMain() end ========================================

由 于svchost已经调用了StartServiceCtrlDispatcher来服务调度函数,因此我们在实现DLL实现时就不用了,这主要是因为一 个进程只能调用一次StartServiceCtrlDispatcher API。但是需要用 RegisterServiceCtrlHandler 来注册响应控制请求的函数。最后我们的DLL接收的都是unicode字符串。

由 于这种服务启动后由svchost加载,不增加新的进程,只是svchost的一个DLL,而且一般进行审计时都不会去 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost 检查服务组是否变化,就算去检查,也不一定能发现异常,因此如果添加一个这样的DLL后门,伪装的好,是比较隐蔽的。

4. 安装服务与设置
要通过svchost调用来启动的服务,就一定要在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost下有该服务名,这可以通过如下方式来实现:
1) 添加一个新的服务组,在组里添加服务名
2) 在现有组里添加服务名
3) 直接使用现有服务组里的一个服务名,但本机没有安装的服务
4) 修改现有服务组里的现有服务,把它的ServiceDll指向自己

其 中前两种可以被正常服务使用,如使用第1种方式,启动其服务要创建新的svchost进程;第2种方式如果该组服务已经运行,安装后不能立刻启动服务,因 为svchost启动后已经把该组信息保存在内存里,并调用API StartServiceCtrlDispatcher() 为该组所有服务注册了调度处理函数,新增加的服务不能再注册调度处理函数,需要重起计算机或者该组的svchost进程。而后两种可能被后门使用,尤其是 最后一种,没有添加服务,只是改了注册表里一项设置,从服务管理控制台又看不出来,如果作为后门还是很隐蔽的。比如EventSystem服务,缺省是指 向es.dll,如果把ServiceDll改为EventSystem.dll就很难发现。

因此服务的安装除了调用CreateService()创建服务之外,还需要设置服务的ServiceDll,如果使用前2种还要设置svchost的注册表选项,在卸载时也最好删除增加的部分。

具体代码参见后边的附例(使用的是方法3)。

注: ImagePath 和ServiceDll 是ExpandString不是普通字符串。因此如果使用.reg文件安装时要注意。

5. DLL服务实现
DLL程序的编写比较简单,只要实现一个ServiceMain()函数和一个服务控制程序,在ServiceMain()函数里用RegisterServiceCtrlHandler()注册服务控制程序,并设置服务的运行状态就可以了。

另外,因为此种服务的安装除了正常的CreateService()之外,还要进行其他设置,因此最好实现安装和卸载函数。

为 了方便安装,实现的代码提供了InstallService()函数进行安装,这个函数可以接收服务名作为参数(如果不提供参数,就使用缺省的 iprip),如果要安装的服务不在svchost的netsvcs组里安装就会失败;如果要安装的服务已经存在,安装也会失败;安装成功后程序会配置服 务的ServiceDll为当前Dll。提供的UninstallService()函数,可以删除任何函数而没有进行任何检查。

为了方便使用rundll32.exe进行安装,还提供了RundllInstallA()和RundllUninstallA()分别调用InstallService()及UninstallService()。因为rundll32.exe使用的函数原型是:

C++代码
  1. void CALLBACK FunctionName(
  2. HWND hwnd, // handle to owner window
  3. HINSTANCE hinst, // instance handle for the DLL
  4. LPTSTR lpCmdLine, // string the DLL will parse
  5. int nCmdShow // show state
  6. );

对应的命令行是rundll32 DllName,FunctionName [Arguments]

DLL服务本身只是创建一个进程,该程序命令行就是启动服务时提供的第一个参数,如果未指定就使用缺省的svchostdll.exe。启动服务时如果提供第二个参数,创建的进程就是和桌面交互的。

具体代码参见后边的附例8,源代码和DLL文件请到http://www.binglesite.net下载。

C++代码
  1. //main service process function
  2. void __stdcall ServiceMain( int argc, wchar_t* argv[] );
  3. //report service stat to the service control manager
  4. int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress );
  5. //service control handler, call back by service control manager
  6. void __stdcall ServiceHandler( DWORD dwCommand );
  7. //RealService just create a process
  8. int RealService(char *cmd, int bInteract);
  9. //Install this dll as a Service host by svchost.exe, service name is given by caller
  10. int InstallService(char *name);
  11. //unInstall a Service, be CARE FOR call this to delete a service
  12. int UninstallService(char *name);
  13. //Install this dll as a Service host by svchost.exe, used by RUNDLL32.EXE to call
  14. void CALLBACK RundllInstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);
  15. //unInstall a Service used by RUNDLL32.EXE to call, be CARE FOR call this to delete a service
  16. void CALLBACK RundllUninstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);
  17. //output the debug infor into log file(or stderr if a console program call me) & DbgPrint
  18. void OutputString( char *lpFmt, … );

6. 代码使用
C:\>tlist -s
0 System Process
8 System
240 services.exe Svcs: Browser,Dhcp,dmserver,Dnscache,Eventlog,lanmanserver,lanmanworkstation, LmHosts,PlugPlay,ProtectedStorage,TrkWks,Wmi
504 svchost.exe Svcs: RpcSs
1360 svchost.exe Svcs: EventSystem,Netman,RasMan,SENS,TapiSrv

C:\>rundll32 svchostdll.dll,RundllInstall abcd
SvcHostDLL: DllMain called DLL_PROCESS_ATTACH
you specify service name not in Svchost\netsvcs, must be one of following:
- EventSystem
- Ias
- Iprip
- Irmon
- Netman
- Nwsapagent
- Rasauto
- Rasman
- Remoteaccess
- SENS
- Sharedaccess
- Tapisrv
- Ntmssvc
- wzcsvc

C:\>rundll32 svchostdll.dll,RundllInstall IPRIP
SvcHostDLL: DllMain called DLL_PROCESS_ATTACH
CreateService(IPRIP) SUCCESS. Config it
Config service IPRIP ok.

C:\>sc start iprip “cmd /k whoami” 1
NT AUTHORITY\SYSTEM

SvcHostDLL: ServiceMain(3, IPRIP) called
SvcHostDLL: RealService called ‘cmd /k whoami’ Interact
SvcHostDLL: CreateProcess(cmd /k whoami) to 640

C:\>tlist -s
0 System Process
8 System
240 services.exe Svcs: Browser,Dhcp,dmserver,Dnscache,Eventlog,lanmanserver,lanmanworkstation, LmHosts,PlugPlay,ProtectedStorage,TrkWks,Wmi
504 svchost.exe Svcs: RpcSs
640 cmd.exe Title: C:\WINNT\System32\cmd.exe
1360 svchost.exe Svcs: EventSystem,Netman,RasMan,SENS,TapiSrv,IPRIP

C:\>net stop iprip
The IPRIP service was stopped successfully.

C:\>rundll32 svchostdll.dll,RundllUninstall iprip
DeleteService(IPRIP) SUCCESS.

7. 参考

Platform SDK: Tools – Rundll32
1) Inside Win32 Services, Part 2 by: Mark Russinovich, at: http://www.winnetmag.com/Articles/Index.cfm?ArticleID=8943&pg=3
2) Platform SDK: Tools – Rundll32, at: http://msdn.microsoft.com/library/en-us/tools/tools/rundll32.asp

2003/8

8. 代码

C++代码
  1. // SvcHostDLL.cpp : Demo for a service dll used by svchost.exe to host it.
  2. //
  3. // for detail comment see articles.
  4. // by bingle_at_email.com.cn
  5. //www.BingleSite.net
  6. //
  7. /* save following as a .def file to export function, only ServiceMain is needed.
  8. other used to install & uninstall service.
  9. or use /EXPORT: link option to export them.
  10. EXPORTS
  11. ServiceMain
  12. InstallService
  13. UninstallService
  14. RundllUninstallA
  15. RundllInstallA
  16. */
  17. /*
  18. To compile & link:
  19. cl /MD /GX /LD svchostdll.cpp /link advapi32.lib /DLL /base:0×71000000 /export:ServiceMain EXPORT:RundllUninstallA /EXPORT:RundllInstallA /EXPORT:InstallService /EXPORT:UninstallService
  20. */
  21. //
  22. // Articles:
  23. // 1. HOWTO Create a service dll used by svchost.exe by bingle, at: http://www.BingleSite.net/article/svchost-dll-service.html
  24. // 2. Inside Win32 Services, Part 2 by: Mark Russinovich, at: http://www.winnetmag.com/Articles/Index.cfm?ArticleID=8943&pg=3
  25. // 3. Platform SDK: Tools - Rundll32, at: http://msdn.microsoft.com/library/en-us/tools/tools/rundll32.asp
  26. #include <stdio.h>
  27. #include <time.h>
  28. #include <assert.h>
  29. #include <windows.h>
  30. #define DEFAULT_SERVICE ”IPRIP”
  31. #define MY_EXECUTE_NAME ”SvcHostDLL.exe”
  32. //main service process function
  33. void __stdcall ServiceMain( int argc, wchar_t* argv[] );
  34. //report service stat to the service control manager
  35. int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress );
  36. //service control handler, call back by service control manager
  37. void __stdcall ServiceHandler( DWORD dwCommand );
  38. //RealService just create a process
  39. int RealService(char *cmd, int bInteract);
  40. //Install this dll as a Service host by svchost.exe, service name is given by caller
  41. int InstallService(char *name);
  42. //unInstall a Service, be CARE FOR call this to delete a service
  43. int UninstallService(char *name);
  44. //Install this dll as a Service host by svchost.exe, used by RUNDLL32.EXE to call
  45. void CALLBACK RundllInstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);
  46. //unInstall a Service used by RUNDLL32.EXE to call, be CARE FOR call this to delete a service
  47. void CALLBACK RundllUninstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);
  48. //output the debug infor into log file(or stderr if a console program call me) & DbgPrint
  49. void OutputString( char *lpFmt, … );
  50. //dll module handle used to get dll path in InstallService
  51. HANDLE hDll = NULL;
  52. //Service HANDLE & STATUS used to get service state
  53. SERVICE_STATUS_HANDLE hSrv;
  54. DWORD dwCurrState;
  55. BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  56. {
  57. switch (ul_reason_for_call)
  58. {
  59. case DLL_PROCESS_ATTACH:
  60. hDll = hModule;
  61. #ifdef _DEBUG
  62. AllocConsole();
  63. OutputString(“SvcHostDLL: DllMain called DLL_PROCESS_ATTACH”);
  64. break;
  65. case DLL_THREAD_ATTACH:
  66. OutputString(“SvcHostDLL: DllMain called DLL_THREAD_ATTACH”);
  67. case DLL_THREAD_DETACH:
  68. OutputString(“SvcHostDLL: DllMain called DLL_THREAD_DETACH”);
  69. case DLL_PROCESS_DETACH:
  70. TellSCM( SERVICE_STOP_PENDING, 0, 0 );
  71. Sleep(1500);
  72. TellSCM( SERVICE_STOPPED, 0, 0 );
  73. OutputString(“SvcHostDLL: DllMain called DLL_PROCESS_DETACH”);
  74. #endif
  75. break;
  76. }
  77. return TRUE;
  78. }
  79. void __stdcall ServiceMain( int argc, wchar_t* argv[] )
  80. {
  81. // DebugBreak();
  82. char svcname[256];
  83. strncpy(svcname, (char*)argv[0], sizeof svcname); //it’s should be unicode, but if it’s ansi we do it well
  84. wcstombs(svcname, argv[0], sizeof svcname);
  85. OutputString(“SvcHostDLL: ServiceMain(%d, %s) called”, argc, svcname);
  86. hSrv = RegisterServiceCtrlHandler( svcname, (LPHANDLER_FUNCTION)ServiceHandler );
  87. if( hSrv == NULL )
  88. {
  89. OutputString(“SvcHostDLL: RegisterServiceCtrlHandler %S failed”, argv[0]);
  90. return;
  91. }else FreeConsole();
  92. TellSCM( SERVICE_START_PENDING, 0, 1 );
  93. TellSCM( SERVICE_RUNNING, 0, 0 );
  94. // call Real Service function noew
  95. if(argc > 1)
  96. strncpy(svcname, (char*)argv[1], sizeof svcname),
  97. wcstombs(svcname, argv[1], sizeof svcname);
  98. RealService(argc > 1 ? svcname : MY_EXECUTE_NAME, argc > 2 ? 1 : 0);
  99. do{
  100. Sleep(10);//not quit until receive stop command, otherwise the service will stop
  101. }while(dwCurrState != SERVICE_STOP_PENDING && dwCurrState != SERVICE_STOPPED);
  102. OutputString(“SvcHostDLL: ServiceMain done”);
  103. return;
  104. }
  105. int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress )
  106. {
  107. SERVICE_STATUS srvStatus;
  108. srvStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  109. srvStatus.dwCurrentState = dwCurrState = dwState;
  110. srvStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
  111. srvStatus.dwWin32ExitCode = dwExitCode;
  112. srvStatus.dwServiceSpecificExitCode = 0;
  113. srvStatus.dwCheckPoint = dwProgress;
  114. srvStatus.dwWaitHint = 3000;
  115. return SetServiceStatus( hSrv, &srvStatus );
  116. }
  117. void __stdcall ServiceHandler( DWORD dwCommand )
  118. {
  119. // not really necessary because the service stops quickly
  120. switch( dwCommand )
  121. {
  122. case SERVICE_CONTROL_STOP:
  123. TellSCM( SERVICE_STOP_PENDING, 0, 1 );
  124. OutputString(“SvcHostDLL: ServiceHandler called SERVICE_CONTROL_STOP”);
  125. Sleep(10);
  126. TellSCM( SERVICE_STOPPED, 0, 0 );
  127. break;
  128. case SERVICE_CONTROL_PAUSE:
  129. TellSCM( SERVICE_PAUSE_PENDING, 0, 1 );
  130. OutputString(“SvcHostDLL: ServiceHandler called SERVICE_CONTROL_PAUSE”);
  131. TellSCM( SERVICE_PAUSED, 0, 0 );
  132. break;
  133. case SERVICE_CONTROL_CONTINUE:
  134. TellSCM( SERVICE_CONTINUE_PENDING, 0, 1 );
  135. OutputString(“SvcHostDLL: ServiceHandler called SERVICE_CONTROL_CONTINUE”);
  136. TellSCM( SERVICE_RUNNING, 0, 0 );
  137. break;
  138. case SERVICE_CONTROL_INTERROGATE:
  139. OutputString(“SvcHostDLL: ServiceHandler called SERVICE_CONTROL_INTERROGATE”);
  140. TellSCM( dwCurrState, 0, 0 );
  141. break;
  142. case SERVICE_CONTROL_SHUTDOWN:
  143. OutputString(“SvcHostDLL: ServiceHandler called SERVICE_CONTROL_SHUTDOWN”);
  144. TellSCM( SERVICE_STOPPED, 0, 0 );
  145. break;
  146. }
  147. }
  148. //RealService just create a process
  149. int RealService(char *cmd, int bInteract)
  150. {
  151. OutputString(“SvcHostDLL: RealService called ’%s’ %s”, cmd, bInteract ? “Interact” : “”);
  152. STARTUPINFO si = {0};
  153. PROCESS_INFORMATION pi;
  154. si.cb = sizeof si;
  155. if(bInteract) si.lpDesktop = “WinSta0\\Default”;
  156. if(!CreateProcess(NULL, cmd, NULL, NULL, false, 0, NULL, NULL, &si, π))
  157. OutputString(“SvcHostDLL: CreateProcess(%s) error:%d”, cmd, GetLastError());
  158. else OutputString(“SvcHostDLL: CreateProcess(%s) to %d”, cmd, pi.dwProcessId);
  159. return 0;
  160. }
  161. int InstallService(char *name)
  162. {
  163. // Open a handle to the SC Manager database.
  164. int rc = 0;
  165. HKEY hkRoot = HKEY_LOCAL_MACHINE, hkParam = 0;
  166. SC_HANDLE hscm = NULL, schService = NULL;
  167. try{
  168. char buff[500];
  169. char *svcname = DEFAULT_SERVICE;
  170. if(name && name[0]) svcname = name;
  171. //query svchost setting
  172. char *ptr, *pSvchost = “SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost”;
  173. rc = RegOpenKeyEx(hkRoot, pSvchost, 0, KEY_QUERY_value, &hkRoot);
  174. if(ERROR_SUCCESS != rc)
  175. {
  176. OutputString(“RegOpenKeyEx(%s) KEY_QUERY_value error %d.”, pSvchost, rc);
  177. throw “”;
  178. }
  179. DWORD type, size = sizeof buff;
  180. rc = RegQueryvalueEx(hkRoot, “netsvcs”, 0, &type, (unsigned char*)buff, &size);
  181. RegCloseKey(hkRoot);
  182. SetLastError(rc);
  183. if(ERROR_SUCCESS != rc)
  184. throw “RegQueryvalueEx(Svchost\\netsvcs)”;
  185. for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1)
  186. if(stricmp(ptr, svcname) == 0) break;
  187. if(*ptr == 0)
  188. {
  189. OutputString(“you specify service name not in Svchost\\netsvcs, must be one of following:”);
  190. for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1)
  191. OutputString(“ - %s”, ptr);
  192. throw “”;
  193. }
  194. //install service
  195. hscm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  196. if (hscm == NULL)
  197. throw “OpenSCManager()”;
  198. char *bin = “%SystemRoot%\\System32\\svchost.exe -k netsvcs”;
  199. schService = CreateService(
  200. hscm,// SCManager database
  201. svcname,// name of service
  202. NULL,// service name to display
  203. SERVICE_ALL_ACCESS,// desired access
  204. SERVICE_WIN32_SHARE_PROCESS, // service type
  205. SERVICE_AUTO_START,// start type
  206. SERVICE_ERROR_NORMAL,// error control type
  207. bin,// service’s binary
  208. NULL,// no load ordering group
  209. NULL,// no tag identifier
  210. NULL,// no dependencies
  211. NULL,// LocalSystem account
  212. NULL);// no password
  213. if (schService == NULL)
  214. {
  215. OutputString(“CreateService(%s) error %d”, svcname, rc = GetLastError());
  216. throw “”;
  217. }
  218. OutputString(“CreateService(%s) SUCCESS. Config it”, svcname);
  219. CloseServiceHandle(schService);
  220. CloseServiceHandle(hscm);
  221. //config service
  222. hkRoot = HKEY_LOCAL_MACHINE;
  223. strncpy(buff, “SYSTEM\\CurrentControlSet\\Services\\”, sizeof buff);
  224. strncat(buff, svcname, 100);
  225. rc = RegOpenKeyEx(hkRoot, buff, 0, KEY_ALL_ACCESS, &hkRoot);
  226. if(ERROR_SUCCESS != rc)
  227. {
  228. OutputString(“RegOpenKeyEx(%s) KEY_SET_value error %d.“, svcname, rc);
  229. throw ”“;
  230. }
  231. rc = RegCreateKey(hkRoot, ”Parameters“, &hkParam);
  232. SetLastError(rc);
  233. if(ERROR_SUCCESS != rc)
  234. throw ”RegCreateKey(Parameters)“;
  235. if(!GetModuleFileName(HMODULE(hDll), buff, sizeof buff))
  236. throw ”GetModuleFileName() get dll path“;
  237. rc = RegSetvalueEx(hkParam, ”ServiceDll“, 0, REG_EXPAND_SZ, (unsigned char*)buff, strlen(buff)+1);
  238. SetLastError(rc);
  239. if(ERROR_SUCCESS != rc)
  240. throw ”RegSetvalueEx(ServiceDll)“;
  241. OutputString(“Config service %s ok.“, svcname);
  242. }catch(char *str)
  243. {
  244. if(str && str[0])
  245. {
  246. rc = GetLastError();
  247. OutputString(“%s error %d“, str, rc);
  248. }
  249. }
  250. RegCloseKey(hkRoot);
  251. RegCloseKey(hkParam);
  252. CloseServiceHandle(schService);
  253. CloseServiceHandle(hscm);
  254. return rc;
  255. }
  256. /*
  257. used to install by rundll32.exe
  258. Platform SDK: Tools - Rundll32
  259. The Run DLL utility (Rundll32.exe) included in Windows enables you to call functions exported from a 32-bit DLL. These functions must have the following syntax:
  260. */
  261. void CALLBACK RundllInstallA(
  262. HWND hwnd, // handle to owner window
  263. HINSTANCE hinst,// instance handle for the DLL
  264. char *param, // string the DLL will parse
  265. int nCmdShow// show state
  266. )
  267. {
  268. InstallService(param);
  269. }
  270. int UninstallService(char *name)
  271. {
  272. int rc = 0;
  273. SC_HANDLE schService;
  274. SC_HANDLE hscm;
  275. __try{
  276. hscm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  277. if (hscm == NULL)
  278. {
  279. OutputString(“OpenSCManager() error %d“, rc = GetLastError() );
  280. return rc;
  281. }
  282. char *svcname = DEFAULT_SERVICE;
  283. if(name && name[0]) svcname = name;
  284. schService = OpenService(hscm, svcname, DELETE);
  285. if (schService == NULL)
  286. {
  287. OutputString(“OpenService(%s) error %d“, svcname, rc = GetLastError() );
  288. return rc;
  289. }
  290. if (!DeleteService(schService) )
  291. {
  292. OutputString(“OpenService(%s) error %d“, svcname, rc = GetLastError() );
  293. return rc;
  294. }
  295. OutputString(“DeleteService(%s) SUCCESS.“, svcname);
  296. }__except(1)
  297. {
  298. OutputString(“Exception Catched 0x%X“, GetExceptionCode());
  299. }
  300. CloseServiceHandle(schService);
  301. CloseServiceHandle(hscm);
  302. return rc;
  303. }
  304. /*
  305. used to uninstall by rundll32.exe
  306. Platform SDK: Tools - Rundll32
  307. The Run DLL utility (Rundll32.exe) included in Windows enables you to call functions exported from a 32-bit DLL. These functions must have the following syntax:
  308. */
  309. void CALLBACK RundllUninstallA(
  310. HWND hwnd,// handle to owner window
  311. HINSTANCE hinst,// instance handle for the DLL
  312. char *param,// string the DLL will parse
  313. int nCmdShow// show state
  314. )
  315. {
  316. UninstallService(param);
  317. }
  318. //output the debug infor into log file & DbgPrint
  319. void OutputString( char *lpFmt, … )
  320. {
  321. char buff[1024];
  322. va_list arglist;
  323. va_start( arglist, lpFmt );
  324. _vsnprintf( buff, sizeof buff, lpFmt, arglist );
  325. va_end( arglist );
  326. DWORD len;
  327. HANDLE herr = GetStdHandle(STD_OUTPUT_HANDLE);
  328. if(herr!=INVALID_HANDLE_value)
  329. {
  330. WriteFile(herr, buff, strlen(buff), &len, NULL);
  331. WriteFile(herr, ”\r\n“, 2, &len, NULL);
  332. }else
  333. {
  334. FILE *fp = fopen(“SvcHost.DLL.log“, ”a“);
  335. if(fp)
  336. {
  337. char date[20], time[20];
  338. fprintf(fp, ”%s %s - %s\n”, _strdate(date), _strtime(time), buff);
  339. if(!stderr) fclose(fp);
  340. }
  341. }
  342. OutputDebugString(buff);
  343. }
作者:lonkil | 分类目录:编程开发 | 标签:

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>