编译服务 DLL 以在 SvcHost.exe 中使用 g++

Compiling Service DLL for use in SvcHost.exe with g++

提问人:Stryker2k2 提问时间:5/6/2020 最后编辑:CommunityStryker2k2 更新时间:5/6/2020 访问量:818

问:

我的任务是使用 g++ 编译器创建一个由 SvcHost.exe 托管的 Windows 服务 DLL。到目前为止,我已经设置了服务,使用 ServiceMain 创建了一个 DLL,将“ServiceDLL”添加到 SYSTEM 注册表中,并将“(组名)”添加到 SOFTWARE 注册表中。

我使用了在 Microsoft - 编写 ServiceMain 函数中找到的文档,并遵循了 MSDN 目录中的每个兔子洞。我没有做的一件事是下载Visual Studio。

使用其他服务作为比较,似乎我创建的服务是正确的,注册表也是正确的。我的代码应该是正确的......所以我唯一没有遵循说明的是编译方法。

因此,我的问题:“如何编译要使用带有 g++ 的 SvcHost.exe 托管的服务 DLL?

免责声明:我知道 Microsoft 建议不要使用 SvcHost.exe,但我不负责该项目或其设计;我只是一个牡丹。

目前,我正在做以下工作来编译...

c:\> g++ -c svchostdemo.cpp
c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o

我做了以下操作来创建服务......

c:\> sc create SvcHostDemo binpath= "%SystemRoot%\System32\svchost.exe -k demo" type= share

添加/修改了以下键...

HKLM\SYSTEM\CurrentControlSet\services\SvcHostDemo\Parameters\ServiceDLL = %SystemRoot%\System32\SvcHostDemo.dll

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost\demo = SvcHostDemo

这是我启动服务时得到的......

C:\Windows>sc start SvcHostDemo

SERVICE_NAME: SvcHostDemo
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0
        PID                : 6844
        FLAGS              :

这是我查询服务时得到的......

C:\Windows>sc query SvcHostDemo

SERVICE_NAME: SvcHostDemo
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 193  (0xc1)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

DLL 代码...

#include <windows.h>

#define SVCNAME TEXT("SvcHostDemo")

SERVICE_STATUS          gSvcStatus;
SERVICE_STATUS_HANDLE   gSvcStatusHandle;
HANDLE                  ghSvcStopEvent = NULL;
LPHANDLER_FUNCTION      SvcCtrlHandler;

VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv);
VOID ReportSvcStatus( DWORD dwCurrentState,
                      DWORD dwWin32ExitCode,
                      DWORD dwWaitHint);

//
// Purpose:
//   Entry point for the service
//
// Parameters:
//   dwArgc - Number of arguments in the lpszArgv array
//   lpszArgv - Array of strings. The first string is the name of
//     the service and subsequent strings are passed by the process
//     that called the StartService function to start the service.
//
// Return value:
//   None.
//
VOID WINAPI ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
    // Register the handler function for the service

    gSvcStatusHandle = RegisterServiceCtrlHandler(
        SVCNAME,
        SvcCtrlHandler);

    if( !gSvcStatusHandle )
    {
        MessageBox(NULL, "FAIL", "FAIL", MB_OK | MB_ICONQUESTION);
        //SvcReportEvent(TEXT("RegisterServiceCtrlHandler"));
        return;
    }

    MessageBox(NULL, "PASS", "PASS", MB_OK | MB_ICONQUESTION);

    // These SERVICE_STATUS members remain as set here

    gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    gSvcStatus.dwServiceSpecificExitCode = 0;

    // Report initial status to the SCM

    ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );

    // Perform service-specific initialization and work.

    SvcInit( dwArgc, lpszArgv );
}

//
// Purpose:
//   The service code
//
// Parameters:
//   dwArgc - Number of arguments in the lpszArgv array
//   lpszArgv - Array of strings. The first string is the name of
//     the service and subsequent strings are passed by the process
//     that called the StartService function to start the service.
//
// Return value:
//   None
//
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
    // TO_DO: Declare and set any required variables.
    //   Be sure to periodically call ReportSvcStatus() with
    //   SERVICE_START_PENDING. If initialization fails, call
    //   ReportSvcStatus with SERVICE_STOPPED.

    // Create an event. The control handler function, SvcCtrlHandler,
    // signals this event when it receives the stop control code.

    ghSvcStopEvent = CreateEvent(
                         NULL,    // default security attributes
                         TRUE,    // manual reset event
                         FALSE,   // not signaled
                         NULL);   // no name

    if ( ghSvcStopEvent == NULL)
    {
        ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
        return;
    }

    // Report running status when initialization is complete.

    ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );

    // TO_DO: Perform work until service stops.

    while(1)
    {
        // Check whether to stop the service.

        WaitForSingleObject(ghSvcStopEvent, INFINITE);

        ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
        return;
    }
}

//
// Purpose:
//   Sets the current service status and reports it to the SCM.
//
// Parameters:
//   dwCurrentState - The current state (see SERVICE_STATUS)
//   dwWin32ExitCode - The system error code
//   dwWaitHint - Estimated time for pending operation,
//     in milliseconds
//
// Return value:
//   None
//
VOID ReportSvcStatus( DWORD dwCurrentState,
                      DWORD dwWin32ExitCode,
                      DWORD dwWaitHint)
{
    static DWORD dwCheckPoint = 1;

    // Fill in the SERVICE_STATUS structure.

    gSvcStatus.dwCurrentState = dwCurrentState;
    gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
    gSvcStatus.dwWaitHint = dwWaitHint;

    if (dwCurrentState == SERVICE_START_PENDING)
        gSvcStatus.dwControlsAccepted = 0;
    else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

    if ( (dwCurrentState == SERVICE_RUNNING) ||
           (dwCurrentState == SERVICE_STOPPED) )
        gSvcStatus.dwCheckPoint = 0;
    else gSvcStatus.dwCheckPoint = dwCheckPoint++;

    // Report the status of the service to the SCM.
    SetServiceStatus( gSvcStatusHandle, &gSvcStatus );
}

// TO COMPILE:
// c:\> g++ -c svchostdemo.cpp
// c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o

C++ Windows DLL 服务 svchost

评论

0赞 Remy Lebeau 5/6/2020
您是否正在导出函数以便找到它?仅仅将函数标记为 是不够的,它需要与 或 或文件一起显式导出ServiceMain()svchost.exeWINAPI__declspec(dllexport)__export.def
0赞 Stryker2k2 5/6/2020
喘息!我想你可能在做一些事情!好吧,我会修补一下......BRB的。
0赞 Stryker2k2 5/6/2020
:(没有骰子。我添加了__declspec(遵循MSDN指南)作为DllExport定义,然后用“DllExport WINAPI VOID ServiceMain(...)”标记ServiceMain(),但仍然没有。
0赞 Remy Lebeau 5/6/2020
但是,您是否验证了,例如 ,该函数被导出为公正而不是,例如?我认为您以前没有使用过DLL,对吗?dumpbin"ServiceMain""_ServiceMain@8"
0赞 Stryker2k2 5/6/2020
你的假设是正确的,我是DLL的新手。我现在有 2 周的时间来制作自己的 DLL。 感谢您建议验证代码!我刚才把它放到 Ghidra 中,它导出为“__Z11ServiceMainmPPc@8”。这是一个令人生畏的两周,但我一生中从未如此兴奋地将头撞在砖墙上......我喜欢这些东西!

答: 暂无答案