//*********************************************************************
//利用服务控制器(SCM)加载驱动程序(该模块已经测试成功)
//*********************************************************************
BOOL SCMLoadDeviceDriver(PCHAR DrvFullPathName, //驱动程序完整路径名称
PCHAR DriverName) //name of service
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
//Open a handle to the SC Manager database
schSCManager = OpenSCManager(
NULL, //local machine
NULL, //ServicesActive database
SC_MANAGER_ALL_ACCESS); //full access rights
if (NULL == schSCManager)
{
OutputDebugString("OpenSCManager Error!!!");
return FALSE;
}
//install a service in a SCM database
schService = CreateService(
schSCManager, //SCManager database
DriverName, //name of service
DriverName, //service name to display
SERVICE_ALL_ACCESS, //desired access
SERVICE_KERNEL_DRIVER, //service type
SERVICE_DEMAND_START, //start type
SERVICE_ERROR_NORMAL, //error control type
DrvFullPathName, //path to service's binary,TEXT("c:\\boot.sys")
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (NULL == schService)
{
if (GetLastError() == ERROR_SERVICE_EXISTS)
{
//service exist
schService = OpenService(schSCManager,
DriverName, //DriverName
SERVICE_ALL_ACCESS);
if (NULL == schService)
{
OutputDebugString("OpenService Error!!!");
CloseServiceHandle(schService);
return FALSE;
}
}
else
{
OutputDebugString("CreateService Error!!!");
CloseServiceHandle(schService);
return FALSE;
}
}
//Start the driver service
if ( !StartService(schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
//An instance of the service is already running.
if ( ERROR_SERVICE_ALREADY_RUNNING == GetLastError() )
{
// no real problem
}
else
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
}
//**************************************************************************
// 通过SCM卸载驱动程序
// If the operation is successful, returns ERROR_SUCCESS. Otherwise,
// returns a system error code.
//**************************************************************************
DWORD SCMUnloadDeviceDriver(PCHAR DriverName)//Name of service
{
SC_HANDLE hSCManager; // Handle to the service control manager
SC_HANDLE hService;// Handle to the service to be stopped
SERVICE_STATUS ss;
OutputDebugString("Unloading Rootkit Driver.\n");
// Open a handle to the SC Manager database
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == hSCManager)
{
OutputDebugString("OpenSCManager Error.\n");
return GetLastError();
}
// Open a handle to the SC Manager database
hService = OpenService( hSCManager, //SCManager database
DriverName, //Name of service
SERVICE_ALL_ACCESS);
if( NULL == hService )
{
OutputDebugString("OpenService Error.");
CloseServiceHandle(hSCManager);
return FALSE;
}
// Sends a stop code to the main service.
if (!ControlService(hService, SERVICE_CONTROL_STOP, &ss))
{
OutputDebugString("warning: could not stop service");
return GetLastError();
}
// Marks the specified service for deletion from the service
// control manager database
if (!DeleteService(hService))
{
OutputDebugString("warning: could not delete service");
return FALSE;
}