Government Security
Network Security Resources

Jump to content

Photo

Starting Services


  • Please log in to reply
1 reply to this topic

#1 tibbar

tibbar

    First Sergeant

  • Members
  • 1,423 posts

Posted 07 March 2005 - 09:50 PM

As I posted this in downloads in response to illwill's program, I thought a copy should go here...

Here's source to a simple class that adds / removes services, it would be a 2 minutes job to add this into an app (I thought I'd post this as illwill forgot to post his source )

service manager.cpp:
/*
Used to install service

Written by tibbar@governmentsecurity.org
You can use this code in your own projects, but you must give credit for
the original source...
*/


#include "service manager.h"

#pragma comment(lib,"Advapi32.lib")

CServiceManager::CServiceManager()
{

}

CServiceManager::~CServiceManager()
{

}

HANDLE CServiceManager::OpenServiceManager()
{
return OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
}


HANDLE CServiceManager::AddService(SC_HANDLE hSCManager, LPCTSTR lpServiceName,LPCTSTR lpDisplayName,LPCTSTR lpBinaryPathName)
{
HANDLE newService = CreateService (hSCManager, lpServiceName, lpDisplayName,
                                      SERVICE_ALL_ACCESS,
                                      SERVICE_WIN32_OWN_PROCESS,
                                      SERVICE_AUTO_START,
                                      SERVICE_ERROR_IGNORE,
                                      lpBinaryPathName, NULL, NULL,
                                      NULL, NULL, NULL);
return newService;
}

BOOL CServiceManager::ServiceStart(SC_HANDLE hService)
{
return StartService(hService,NULL,NULL);
}


HANDLE CServiceManager::ServiceOpen(SC_HANDLE hSCManager,LPCTSTR lpServiceName)
{
return OpenService(hSCManager, lpServiceName,DWORD SC_MANAGER_ALL_ACCESS);
}


BOOL CServiceManager::ServiceDelete(SC_HANDLE hService)
{
return DeleteService(hService);
}


BOOL CServiceManager::CreateAndStartService(LPCTSTR serviceName,LPCTSTR displayName,LPCTSTR binaryPathName)
{
// for binary path name, use format: "\"d:\\my share\\myservice.exe\""
HANDLE serviceManagerHandle = OpenServiceManager();

int x = GetLastError();
if(serviceManagerHandle == NULL){return 0;}

HANDLE serviceHandle = AddService((SC_HANDLE) serviceManagerHandle, serviceName, displayName, binaryPathName);


 x = GetLastError();
 // if we are running this for second time, then service has been marked for deletion on reboot
 // in this case we can continue without loading it

if(x==1072){return 1;}
if(serviceHandle == NULL)
{
 CloseServiceHandle((SC_HANDLE) serviceManagerHandle);
 return 0;
}

BOOL diditwork = ServiceStart((SC_HANDLE) serviceHandle);
x = GetLastError();
if(diditwork == 0)
{
 ServiceDelete((SC_HANDLE) serviceHandle);
 CloseServiceHandle((SC_HANDLE) serviceHandle);
 CloseServiceHandle((SC_HANDLE) serviceManagerHandle);
 return 0;
}

m_ServiceManagerHandle = serviceManagerHandle;
m_ServiceHandle = serviceHandle;

return 1;
}

BOOL CServiceManager::RemoveService(LPCTSTR serviceName)
{
HANDLE serviceManagerHandle = OpenServiceManager();
int x = GetLastError();
if(serviceManagerHandle == NULL){return 0;}


HANDLE serviceHandle = ServiceOpen((SC_HANDLE) serviceManagerHandle,serviceName);
x = GetLastError();
if(serviceHandle == NULL)
{
 CloseServiceHandle((SC_HANDLE) serviceManagerHandle);
 return 0;
}

ServiceDelete((SC_HANDLE) serviceHandle);
x = GetLastError();
CloseServiceHandle((SC_HANDLE) serviceHandle);
CloseServiceHandle((SC_HANDLE) serviceManagerHandle);
return 1;
}

BOOL CServiceManager::RemoveService()
{
ServiceDelete((SC_HANDLE) m_ServiceHandle);
int x = GetLastError();
CloseServiceHandle((SC_HANDLE) m_ServiceHandle);
CloseServiceHandle((SC_HANDLE) m_ServiceManagerHandle);
return 1;
}

service manager.h:
/*
Used to install kernel driver

Written by tibbar@governmentsecurity.org
You can use this code in your own projects, but you must give credit for
the original source...
*/

#include "windows.h"
#include "Winnt.h"
#include "Winsvc.h"


class CServiceManager
{
public:
	CServiceManager();
	~CServiceManager();
	BOOL CreateAndStartService(LPCTSTR serviceName,LPCTSTR displayName,LPCTSTR binaryPathName);
	BOOL RemoveService(LPCTSTR serviceName);
	BOOL RemoveService();
private:
	HANDLE OpenServiceManager();
	HANDLE AddService(SC_HANDLE hSCManager, LPCTSTR lpServiceName,LPCTSTR lpDisplayName,LPCTSTR lpBinaryPathName);
	BOOL ServiceStart(SC_HANDLE hService);
	HANDLE ServiceOpen(SC_HANDLE hSCManager,LPCTSTR lpServiceName);
	BOOL ServiceDelete(SC_HANDLE hService);
	HANDLE m_ServiceManagerHandle;
	HANDLE m_ServiceHandle;
};

If you want to read more about my security research, visit Tibbar.org

#2 DumpZ

DumpZ

    Staff Sergeant

  • Members
  • 356 posts

Posted 21 March 2005 - 12:14 AM

Look intresting i'll have a look at it thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users