zoukankan      html  css  js  c++  java
  • 加载windows驱动代码

    #include <WINDOWS.H>
    #include <winsvc.h>
    #include <conio.h>
    #include <stdio.h>

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

    BOOL LoadNTDriver(char* lpszDriverName, char* lpszDriverPath)
    {
     char szDriverImagePath[MAX_PATH];
     GetFullPathNameA(lpszDriverPath, MAX_PATH, szDriverImagePath, NULL);

     BOOL bRet = FALSE;

     SC_HANDLE hSvcMgr = NULL;
     SC_HANDLE hSvcDDK = NULL;

     hSvcMgr = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if (hSvcMgr == NULL)
     {
      printf("OpenSCManagerA failed : %d ", GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenSCManagerA ok... ");
     }

     hSvcDDK = CreateServiceA(hSvcMgr,
      lpszDriverName,
      lpszDriverName,
      SERVICE_ALL_ACCESS,
      SERVICE_KERNEL_DRIVER,
      SERVICE_DEMAND_START,
      SERVICE_ERROR_IGNORE,
      szDriverImagePath,
      NULL, NULL, NULL, NULL, NULL);

     DWORD dwRet;
     if (hSvcDDK == NULL)
     {
      dwRet = GetLastError();
      if (dwRet != ERROR_IO_PENDING && dwRet != ERROR_SERVICE_EXISTS)
      {
       printf("CreateServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       printf("CreateServiceA failed, %s is pending or has been existed... ", lpszDriverName);
      }

      hSvcDDK = OpenServiceA(hSvcDDK, lpszDriverName, SERVICE_ALL_ACCESS);
      if (hSvcDDK == NULL)
      {
       dwRet = GetLastError();
       printf("OpenServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       printf("OpenServiceA ok! ");
      }
     }
     else
     {
      printf("CreateServiceA ok... ");
     }

     bRet = StartServiceA(hSvcDDK, NULL, NULL);
     if (!bRet)
     {
      DWORD dwRet = GetLastError();
      if (dwRet != ERROR_IO_PENDING && dwRet != ERROR_SERVICE_EXISTS)
      {
       printf("StartServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       if (dwRet == ERROR_IO_PENDING)
       {
        printf("StartServiceA failed ERROR_IO_PENDING ");
        bRet = FALSE;
        goto BeforeLeave;
       }
       else
       {
        printf("StartServiceA failed ERROR_SERVICE_EXISTS ");
        bRet = TRUE;
        goto BeforeLeave;
       }
      }
     }
     else
     {
      printf("StartServiceA ok... ");
     }
     bRet = TRUE;
    BeforeLeave:
     if (hSvcDDK)
     {
      CloseServiceHandle(hSvcDDK);
     }
     if (hSvcMgr)
     {
      CloseServiceHandle(hSvcMgr);
     }
     return bRet;
    }

    BOOL UnloadDriver(char* lpszSvcName)
    {
     BOOL bRet = FALSE;
     SC_HANDLE hSvcMgr = NULL;
     SC_HANDLE hSvcDDK = NULL;
     SERVICE_STATUS SvcStatus;

     hSvcMgr = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if (hSvcMgr == NULL)
     {
      printf("OpenSCManagerA failed : %d ", GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenSCManagerA ok... ");
     }

     hSvcDDK = OpenServiceA(hSvcMgr, lpszSvcName, SERVICE_ALL_ACCESS);
     if (hSvcDDK == NULL)
     {
      printf("OpenServiceA %s failed : %d ", lpszSvcName,GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenServiceA %s ok... ", lpszSvcName);
     }
     if (!ControlService(hSvcDDK, SERVICE_CONTROL_STOP, &SvcStatus))
     {
      printf("ControlService failed : %d ", GetLastError());
     }
     else
     {
      printf("ControlService ok... ");
     }
     if (!DeleteService(hSvcDDK))
     {
      printf("DeleteService failed : %d ", GetLastError());
     }
     else
     {
      printf("DeleteService ok... ");
     }
     bRet = TRUE;
    BeforeLeave:
     if (hSvcDDK)
     {
      CloseServiceHandle(hSvcDDK);
     }
     if (hSvcMgr)
     {
      CloseServiceHandle(hSvcMgr);
     }
     return bRet;
    }

    int main(int argc, char** argv)
    {
     if (argc < 3)
     {
      printf("Usage : test_dev.exe load|unload ddk_svc_name sys_file_full_path ");
      return 0;
     }
     if (strcmp(argv[1], "load") == 0)
     {
      BOOL bRet = FALSE;
      bRet = LoadNTDriver(argv[2], argv[3]);
      if (!bRet)
      {
       printf("Load %s failed ", argv[2]);
       return 1;
      }
      else
      {
       printf("Load %s ok ", argv[2]);
       return 0;
      }
     }
     else if (strcmp(argv[1], "unload") == 0)
     {
      BOOL bRet = FALSE;
      bRet = UnloadDriver(argv[2]);
      if (!bRet)
      {
       printf("Unload %s failed... ", argv[2]);
       return 1;
      }
      else
      {
       printf("Unload %s ok... ", argv[2]);
       return 0;
      }
     }
    }

  • 相关阅读:
    实验一 网络侦查与网络扫描
    网络对抗作业一
    [BSidesCF 2020]Hurdles
    [BSidesCF 2019]Mixer
    安恒期末 admin
    C#编程:正则表达式验证身份证校验码-10
    分享1-3年经验的Java面试
    SpringMVC配置web.xml文件详解(列举常用的配置)
    Hibernate全套增删改查+分页
    Node.js连接mysql数据库方法
  • 原文地址:https://www.cnblogs.com/debug-me/p/6376441.html
Copyright © 2011-2022 走看看