#include <Windows.h> #include <iostream> #include <winsvc.h> #include <winioctl.h>
BOOL InstallDriver(const char* lpszDriverName, const char* lpszDriverPath, const char* lpszAltitude) { char szTempStr[MAX_PATH]; HKEY hKey; DWORD dwData; char szDriverImagePath[MAX_PATH];
if (NULL == lpszDriverName || NULL == lpszDriverPath) { return FALSE; }
GetFullPathName(lpszDriverPath, MAX_PATH, szDriverImagePath, NULL);
SC_HANDLE hServiceMgr = NULL; SC_HANDLE hService = NULL;
hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hServiceMgr == NULL) { CloseServiceHandle(hServiceMgr); return FALSE; }
hService = CreateService(hServiceMgr, lpszDriverName, lpszDriverName, SERVICE_ALL_ACCESS, SERVICE_FILE_SYSTEM_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szDriverImagePath, "FSFilter Activity Monitor", NULL, "FltMgr", NULL, NULL);
if (hService == NULL) { if (GetLastError() == ERROR_SERVICE_EXISTS) { CloseServiceHandle(hService); CloseServiceHandle(hServiceMgr); return TRUE; } else { CloseServiceHandle(hService); CloseServiceHandle(hServiceMgr); return FALSE; } } CloseServiceHandle(hService); CloseServiceHandle(hServiceMgr);
strcpy(szTempStr, "SYSTEM\\CurrentControlSet\\Services\\"); strcat(szTempStr, lpszDriverName); strcat(szTempStr, "\\Instances"); if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, szTempStr, 0, "", TRUE, KEY_ALL_ACCESS, NULL, &hKey, (LPDWORD)&dwData) != ERROR_SUCCESS) { return FALSE; } strcpy(szTempStr, lpszDriverName); strcat(szTempStr, " Instance"); if (RegSetValueEx(hKey, "DefaultInstance", 0, REG_SZ, (CONST BYTE*)szTempStr, (DWORD)strlen(szTempStr)) != ERROR_SUCCESS) { return FALSE; } RegFlushKey(hKey); RegCloseKey(hKey);
strcpy(szTempStr, "SYSTEM\\CurrentControlSet\\Services\\"); strcat(szTempStr, lpszDriverName); strcat(szTempStr, "\\Instances\\"); strcat(szTempStr, lpszDriverName); strcat(szTempStr, " Instance"); if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, szTempStr, 0, "", TRUE, KEY_ALL_ACCESS, NULL, &hKey, (LPDWORD)&dwData) != ERROR_SUCCESS) { return FALSE; } strcpy(szTempStr, lpszAltitude); if (RegSetValueEx(hKey, "Altitude", 0, REG_SZ, (CONST BYTE*)szTempStr, (DWORD)strlen(szTempStr)) != ERROR_SUCCESS) { return FALSE; } dwData = 0x0; if (RegSetValueEx(hKey, "Flags", 0, REG_DWORD, (CONST BYTE*)&dwData, sizeof(DWORD)) != ERROR_SUCCESS) { return FALSE; } RegFlushKey(hKey); RegCloseKey(hKey); return TRUE; }
BOOL StartDriver(const char* lpszDriverName) { SC_HANDLE schManager; SC_HANDLE schService; SERVICE_STATUS svcStatus;
if (NULL == lpszDriverName) { return FALSE; }
schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == schManager) { CloseServiceHandle(schManager); return FALSE; } schService = OpenService(schManager, lpszDriverName, SERVICE_ALL_ACCESS); if (NULL == schService) { CloseServiceHandle(schService); CloseServiceHandle(schManager); return FALSE; }
if (!StartService(schService, 0, NULL)) { CloseServiceHandle(schService); CloseServiceHandle(schManager); if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) { return TRUE; } return FALSE; }
CloseServiceHandle(schService); CloseServiceHandle(schManager);
return TRUE; }
BOOL StopDriver(const char* lpszDriverName) { SC_HANDLE schManager; SC_HANDLE schService; SERVICE_STATUS svcStatus; bool bStopped = false;
schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == schManager) { return FALSE; } schService = OpenService(schManager, lpszDriverName, SERVICE_ALL_ACCESS); if (NULL == schService) { CloseServiceHandle(schManager); return FALSE; } if (!ControlService(schService, SERVICE_CONTROL_STOP, &svcStatus) && (svcStatus.dwCurrentState != SERVICE_STOPPED)) { CloseServiceHandle(schService); CloseServiceHandle(schManager); return FALSE; }
CloseServiceHandle(schService); CloseServiceHandle(schManager);
return TRUE; }
BOOL DeleteDriver(const char* lpszDriverName) { SC_HANDLE schManager; SC_HANDLE schService; SERVICE_STATUS svcStatus;
schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == schManager) { return FALSE; } schService = OpenService(schManager, lpszDriverName, SERVICE_ALL_ACCESS); if (NULL == schService) { CloseServiceHandle(schManager); return FALSE; } ControlService(schService, SERVICE_CONTROL_STOP, &svcStatus); if (!DeleteService(schService)) { CloseServiceHandle(schService); CloseServiceHandle(schManager); return FALSE; } CloseServiceHandle(schService); CloseServiceHandle(schManager);
return TRUE; }
int main(int argc, char* argv[]) { InstallDriver("minifilter", ".\\WinDDK.sys", "225864");
while (1) { char str[20] = "\0"; printf("请输入命令: "); gets(str);
if (strcmp(str, "start") == 0) { printf("[*] 启动驱动 \n"); StartDriver("minifilter"); } if (strcmp(str, "stop") == 0) { printf("[-] 关闭驱动 \n"); StopDriver("minifilter"); } } return 0; }
|