找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2729|回复: 1

qq空间老贴转载-32位win下进程创建的拦截

[复制链接]

307

主题

228

回帖

7343

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5593 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
发表于 2014-2-19 19:54:13 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
32位win下进程创建的拦截
  1. 驱动部分:



  2. #include "Driver.h"
  3. #define
  4. SYSNAME "System"
  5. #define VERSIONLEN 100

  6. const WCHAR devLink[]  =
  7. L"\\??\\MyEvent";
  8. const WCHAR devName[]  =
  9. L"\\Device\\MyEvent";
  10. UNICODE_STRING   
  11. devNameUnicd;
  12. UNICODE_STRING   
  13. devLinkUnicd;
  14. PVOID     gpEventObject =
  15. NULL;            //
  16. 与应用程序通信的 Event 对象
  17. HANDLE     
  18. outBuf;
  19. BOOLEAN     BeginLog=FALSE;
  20. [/align]
  21. [p=30, 2, left]#pragma
  22. code_seg("INIT")
  23. extern "C" NTSTATUS DriverEntry(__in PDRIVER_OBJECT
  24. pDriverObject,__in PUNICODE_STRING RegistryPath)
  25. {

  26. NTSTATUS    Status;   

  27. PDEVICE_OBJECT   pDevice;

  28. KdPrint(("DriverEntry
  29. called!\n"));
  30. UNREFERENCED_PARAMETER(RegistryPath);
  31. RtlInitUnicodeString
  32. (&devNameUnicd, devName );
  33. RtlInitUnicodeString (&devLinkUnicd,
  34. devLink );

  35. Status = IoCreateDevice
  36. (pDriverObject,0,&devNameUnicd,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevice);

  37. if( !NT_SUCCESS(Status))
  38. {
  39.   KdPrint(("Can not create
  40. device.\n"));
  41.   return Status;
  42. }

  43. Status =
  44. IoCreateSymbolicLink (&devLinkUnicd, &devNameUnicd);
  45. if(
  46. !NT_SUCCESS(Status))
  47. {
  48.   KdPrint(("Cannot create
  49. link.\n"));
  50.   return Status;
  51. }


  52. pDriverObject->DriverUnload  = (PDRIVER_UNLOAD)OnUnload;

  53. pDriverObject->MajorFunction[IRP_MJ_CREATE] =

  54. pDriverObject->MajorFunction[IRP_MJ_CLOSE] =

  55. pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
  56. DeviceIoControlDispatch;

  57. Status =
  58. PsSetCreateProcessNotifyRoutine(ProcessCreateMon, FALSE);
  59. if (!NT_SUCCESS(
  60. Status ))
  61. {
  62.   
  63. KdPrint(("PsSetCreateProcessNotifyRoutine()\n"));
  64.   return Status;

  65. }

  66. if (!NT_SUCCESS( Status ))
  67. {
  68.   
  69. KdPrint(("PsSetCreateThreadNotifyRoutine()\n"));
  70.   return Status;

  71. }

  72. return STATUS_SUCCESS;
  73. }
  74. #pragma code_seg()

  75. VOID
  76. ProcessCreateMon(HANDLE hParentId,HANDLE PId,BOOLEAN bCreate)
  77. {

  78. NTSTATUS        status;

  79. HANDLE            
  80. TId;

  81. PAGED_CODE();

  82. UNREFERENCED_PARAMETER(hParentId);

  83. if(!BeginLog)
  84. {
  85.   UNREFERENCED_PARAMETER(PId);
  86.   
  87. UNREFERENCED_PARAMETER(bCreate);
  88.   return;
  89. }

  90. if ( bCreate
  91. )
  92. {
  93.   outBuf=PId;
  94.   if(gpEventObject!=NULL)
  95.    
  96. KeSetEvent((PRKEVENT)gpEventObject, 0, FALSE);
  97. }
  98. //  
  99. else//进程退出事件
  100. //  {
  101. //
  102. //  }
  103. }

  104. NTSTATUS OnUnload(
  105. IN PDRIVER_OBJECT pDriverObject )
  106. {

  107. NTSTATUS            
  108. status;
  109. KdPrint(("OnUnload called\n"));

  110. PAGED_CODE();

  111. if(gpEventObject)
  112.   ObDereferenceObject(gpEventObject);

  113. PsSetCreateProcessNotifyRoutine(ProcessCreateMon, TRUE);

  114. if(pDriverObject->DeviceObject != NULL)
  115. {
  116.   
  117. status=IoDeleteSymbolicLink( &devLinkUnicd );
  118.   if ( !NT_SUCCESS(
  119. status ) )
  120.   {
  121.    KdPrint((  "IoDeleteSymbolicLink()
  122. failed\n" ));
  123.    return status;
  124.   }
  125.   
  126. IoDeleteDevice( pDriverObject->DeviceObject );
  127. }
  128. return
  129. STATUS_SUCCESS;
  130. }

  131. NTSTATUS DeviceIoControlDispatch(IN  
  132. PDEVICE_OBJECT  DeviceObject,IN  PIRP pIrp)
  133. {
  134. PVOID
  135. inputBuffer;
  136. ULONG inputLength;
  137. PVOID outputBuffer;
  138. ULONG
  139. outputLength;
  140. OBJECT_HANDLE_INFORMATION objHandleInfo;

  141. NTSTATUS
  142. status=STATUS_SUCCESS;
  143. UNREFERENCED_PARAMETER(DeviceObject);


  144. PAGED_CODE();
  145. KdPrint(("Enter DeviceIOControl\n"));
  146. PIO_STACK_LOCATION
  147. stack=IoGetCurrentIrpStackLocation(pIrp);
  148. ULONG
  149. cbin=stack->Parameters.DeviceIoControl.InputBufferLength;
  150. ULONG
  151. cbout=stack->Parameters.DeviceIoControl.OutputBufferLength;


  152. switch(stack->MajorFunction)
  153. {
  154.   case IRP_MJ_CREATE
  155. :
  156.    KdPrint(("Call IRP_MJ_CREATE\n"));
  157.    
  158. BeginLog=TRUE;
  159.    break;

  160.   case
  161. IRP_MJ_CLOSE:
  162.    BeginLog=FALSE;
  163.    KdPrint(("Call
  164. IRP_MJ_CLOSE\n"));
  165.    break;

  166.   case
  167. IRP_MJ_DEVICE_CONTROL:
  168.    
  169. KdPrint(("IRP_MJ_DEVICE_CONTROL\n"));
  170.    
  171. inputLength=stack->Parameters.DeviceIoControl.InputBufferLength;
  172.    
  173. outputLength=stack->Parameters.DeviceIoControl.OutputBufferLength;


  174.    switch (stack->Parameters.DeviceIoControl.IoControlCode)

  175.    {
  176.     case IOCTL_PASSEVENT:   
  177. //用事件做通信
  178.      inputBuffer =
  179. pIrp->AssociatedIrp.SystemBuffer;

  180.      
  181. KdPrint(("inputBuffer:%08x\n",
  182. (HANDLE)inputBuffer));
  183.      status =
  184. ObReferenceObjectByHandle(*(HANDLE
  185. *)inputBuffer,GENERIC_ALL,NULL,KernelMode,&gpEventObject,&objHandleInfo);//获取事件句柄对应指针


  186.      
  187. if(status!=STATUS_SUCCESS)
  188.      
  189. {
  190.       KdPrint(("gethandle
  191. error!"));
  192.       break;
  193.      
  194. }
  195.      break;

  196.     case
  197. IOCTL_UNPASSEVENT:
  198.      
  199. if(gpEventObject)
  200.      
  201. {
  202.       ObDereferenceObject(gpEventObject);

  203.       
  204. gpEventObject=NULL;
  205.      }
  206.      
  207. KdPrint(("unpassevent"));
  208.      break;


  209.     case IOCTL_PASSBUF:
  210.      
  211. RtlCopyMemory(pIrp->UserBuffer, &outBuf,
  212. outputLength);
  213.      break;

  214.    
  215. default:
  216.      break;
  217.    }
  218.    
  219. break;

  220.   default:
  221.    
  222. KdPrint(("defaultcall"));
  223.    
  224. status=STATUS_INVALID_VARIANT;
  225.    break;
  226. }

  227. pIrp->IoStatus.Status=status;
  228. pIrp->IoStatus.Information=0;

  229. IoCompleteRequest(pIrp,IO_NO_INCREMENT);
  230. KdPrint(("Leave
  231. DispatchRoutine\n"));
  232. return status;
  233. }[/p]
  234. [p=30, 2, left]


  235. 应用层部分:


  236. #include <windows.h>
  237. #include <winsvc.h>  
  238. #include
  239. <conio.h>  
  240. #include <stdio.h>
  241. #include "define.h"


  242. //装载NT驱动程序
  243. BOOL LoadNTDriver(char* lpszDriverName,char*
  244. lpszDriverPath)
  245. {
  246. char szDriverImagePath[256];
  247. //得到完整的驱动路径

  248. GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);

  249. BOOL
  250. bRet = FALSE;

  251. SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
  252. SC_HANDLE
  253. hServiceDDK=NULL;//NT驱动程序的服务句柄

  254. //打开服务控制管理器
  255. hServiceMgr =
  256. OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );

  257. if( hServiceMgr ==
  258. NULL )  
  259. {
  260.   //OpenSCManager失败
  261.   printf(
  262. "OpenSCManager() Faild %d ! \n", GetLastError() );
  263.   bRet =
  264. FALSE;
  265.   goto BeforeLeave;
  266. }
  267. else
  268. {
  269.   
  270. ////OpenSCManager成功
  271.   printf( "OpenSCManager() ok ! \n" );  

  272. }

  273. //创建驱动所对应的服务
  274. hServiceDDK = CreateService( hServiceMgr,
  275.   
  276. lpszDriverName, //驱动程序的在注册表中的名字  
  277.   lpszDriverName, // 注册表驱动程序的
  278. DisplayName 值  
  279.   SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限  

  280.   SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序  
  281.   
  282. SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值  
  283.   SERVICE_ERROR_IGNORE,
  284. // 注册表驱动程序的 ErrorControl 值  
  285.   szDriverImagePath, // 注册表驱动程序的
  286. ImagePath 值  
  287.   NULL,  
  288.   NULL,  
  289.   
  290. NULL,  
  291.   NULL,  
  292.   NULL);  [/p]
  293. [p=30, 2, left] DWORD
  294. dwRtn;
  295. //判断服务是否失败
  296. if( hServiceDDK == NULL )  
  297. {  

  298.   dwRtn = GetLastError();
  299.   if( dwRtn != ERROR_IO_PENDING
  300. && dwRtn != ERROR_SERVICE_EXISTS )  
  301.   {  

  302.    //由于其他原因创建服务失败
  303.    printf( "CrateService() Faild %d
  304. ! \n", dwRtn );  
  305.    bRet = FALSE;
  306.    goto
  307. BeforeLeave;
  308.   }  
  309.   else  
  310.   
  311. {
  312.    //服务创建失败,是由于服务已经创立过
  313.    printf( "CreateService()
  314. Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );  

  315.   }

  316.   // 驱动程序已经加载,只需要打开  
  317.   hServiceDDK =
  318. OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );  
  319.   
  320. if( hServiceDDK == NULL )  
  321.   {
  322.    
  323. //如果打开服务也失败,则意味错误
  324.    dwRtn = GetLastError();  
  325.    
  326. printf( "OpenService() Faild %d ! \n", dwRtn );  
  327.    bRet =
  328. FALSE;
  329.    goto BeforeLeave;
  330.   }  
  331.   else

  332.   {
  333.    printf( "OpenService() ok ! \n" );
  334.   }

  335. }  
  336. else  
  337. {
  338.   printf( "CreateService() ok ! \n"
  339. );
  340. }

  341. //开启此项服务
  342. bRet= StartService( hServiceDDK, NULL, NULL
  343. );  
  344. if( !bRet )  
  345. {  
  346.   DWORD dwRtn =
  347. GetLastError();  
  348.   if( dwRtn != ERROR_IO_PENDING && dwRtn
  349. != ERROR_SERVICE_ALREADY_RUNNING )  
  350.   {  
  351.    
  352. printf( "StartService() Faild %d ! \n", dwRtn );  
  353.    bRet =
  354. FALSE;
  355.    goto BeforeLeave;
  356.   }  
  357.   else  

  358.   {  
  359.    if( dwRtn == ERROR_IO_PENDING )  

  360.    {  
  361.     //设备被挂住
  362.    
  363. printf( "StartService() Faild ERROR_IO_PENDING ! \n");
  364.    
  365. bRet = FALSE;
  366.     goto BeforeLeave;
  367.    }  

  368.    else  
  369.    {  
  370.    
  371. //服务已经开启
  372.     printf( "StartService() Faild
  373. ERROR_SERVICE_ALREADY_RUNNING ! \n");
  374.     bRet =
  375. TRUE;
  376.     goto BeforeLeave;
  377.    }  
  378.   
  379. }  
  380. }
  381. bRet = TRUE;
  382. //离开前关闭句柄
  383. BeforeLeave:

  384. if(hServiceDDK)
  385. {
  386.   CloseServiceHandle(hServiceDDK);
  387. }

  388. if(hServiceMgr)
  389. {
  390.   CloseServiceHandle(hServiceMgr);
  391. }

  392. return bRet;
  393. }

  394. //卸载驱动程序  
  395. BOOL UnloadNTDriver( char *
  396. szSvrName )  
  397. {
  398. BOOL bRet = FALSE;
  399. SC_HANDLE
  400. hServiceMgr=NULL;//SCM管理器的句柄
  401. SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄

  402. SERVICE_STATUS SvrSta;
  403. //打开SCM管理器
  404. hServiceMgr = OpenSCManager( NULL,
  405. NULL, SC_MANAGER_ALL_ACCESS );  
  406. if( hServiceMgr == NULL )  

  407. {
  408.   //带开SCM管理器失败
  409.   printf( "OpenSCManager() Faild %d ! \n",
  410. GetLastError() );  
  411.   bRet = FALSE;
  412.   goto BeforeLeave;

  413. }  
  414. else  
  415. {
  416.   //带开SCM管理器失败成功
  417.   printf(
  418. "OpenSCManager() ok ! \n" );  
  419. }
  420. //打开驱动所对应的服务
  421. hServiceDDK =
  422. OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );  

  423. if(
  424. hServiceDDK == NULL )  
  425. {
  426.   //打开驱动所对应的服务失败
  427.   printf(
  428. "OpenService() Faild %d ! \n", GetLastError() );  
  429.   bRet =
  430. FALSE;
  431.   goto BeforeLeave;
  432. }  
  433. else  
  434. {  

  435.   printf( "OpenService() ok ! \n" );  
  436. }  

  437. //停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。  
  438. if( !ControlService( hServiceDDK,
  439. SERVICE_CONTROL_STOP , &SvrSta ) )  
  440. {  
  441.   printf(
  442. "ControlService() Faild %d !\n", GetLastError() );  
  443. }  

  444. else  
  445. {
  446.   //打开驱动所对应的失败
  447.   printf( "ControlService() ok
  448. !\n" );  
  449. }  
  450. //动态卸载驱动程序。  
  451. if( !DeleteService(
  452. hServiceDDK ) )  
  453. {
  454.   //卸载失败
  455.   printf( "DeleteSrevice()
  456. Faild %d !\n", GetLastError() );  
  457. }  
  458. else  
  459. {  

  460.   //卸载成功
  461.   printf( "DelServer:eleteSrevice() ok !\n" );  

  462. }  
  463. bRet = TRUE;
  464. BeforeLeave:
  465. //离开前关闭打开的句柄

  466. if(hServiceDDK)
  467. {
  468.   CloseServiceHandle(hServiceDDK);
  469. }

  470. if(hServiceMgr)
  471. {
  472.   CloseServiceHandle(hServiceMgr);
  473. }

  474. return bRet;
  475. } [/p]
  476. [p=30, 2, left]int test()
  477. {
  478. HANDLE  
  479. hDevice;     
  480.     BOOL  status;

  481.     HANDLE  m_hCommEvent;
  482.    
  483. ULONG  dwReturn;
  484. HANDLE  PID;

  485.     hDevice =
  486. NULL;
  487.     m_hCommEvent = NULL;
  488.     hDevice =
  489. CreateFile(
  490. "\\\\.\\MyEvent",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,

  491.       
  492. NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  493.     if(hDevice
  494. == INVALID_HANDLE_VALUE)
  495.    
  496. {
  497.         printf("createfile
  498. wrong\n");
  499.         
  500. getchar();
  501.         return
  502. 0;
  503.     }

  504.     m_hCommEvent =
  505. CreateEvent(NULL,false,false,NULL);
  506.     printf("hEvent:%d\n",
  507. m_hCommEvent);

  508.     status
  509. =DeviceIoControl(hDevice,IOCTL_PASSEVENT,&m_hCommEvent,sizeof(m_hCommEvent),NULL,0,&dwReturn,NULL);

  510.     if( !status)
  511.    
  512. {
  513.         printf("IO wrong+%d\n",
  514. GetLastError());
  515.         
  516. getchar();
  517.         return
  518. 0;
  519.     }
  520.   
  521.     printf("[Process
  522. PID]:\n");
  523.     while(1)
  524.    
  525. {
  526.         if(getchar() == 'q')

  527.    break;
  528.         
  529. ResetEvent(m_hCommEvent);
  530.         
  531. WaitForSingleObject(m_hCommEvent,INFINITE);
  532.         
  533. status
  534. =DeviceIoControl(hDevice,IOCTL_PASSBUF,NULL,0,&PID,sizeof(HANDLE),&dwReturn,NULL);

  535.         if(
  536. !status)
  537.         
  538. {
  539.             
  540. printf("IO wrong+%d\n", GetLastError());


  541.             return
  542. 0;
  543.         }


  544.         printf("%d\n",PID);
  545.   
  546. if(!OpenProcess(PROCESS_ALL_ACCESS,FALSE,(DWORD)PID))
  547.    
  548. printf("OpenProcess Error\n");
  549.     }


  550.     status
  551. =DeviceIoControl(hDevice,IOCTL_UNPASSEVENT,NULL,0,NULL,0,&dwReturn,NULL);

  552.     if( !status)
  553.    
  554. {
  555.         printf("UNPASSEVENT wrong+%d\n",
  556. GetLastError());
  557.         
  558. getchar();
  559.         return
  560. 0;
  561.     }

  562.     status = CloseHandle(
  563. hDevice );
  564.     status =
  565. CloseHandle(m_hCommEvent);
  566.    
  567. getchar();
  568.     return 0;
  569. }

  570. void main()
  571. {

  572. //加载驱动
  573. BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);
  574. // if
  575. (!bRet)
  576. {
  577. //  printf("LoadNTDriver error\n");
  578. //  goto
  579. unload;
  580. }
  581. //加载成功

  582. printf( "press q to quit!\n" );   


  583. test();

  584. //这时候你可以通过注册表,或其他查看符号连接的软件验证。  
  585. printf(
  586. "press any to unload the driver!\n" );  
  587. getch();  


  588. unload:
  589. //卸载驱动
  590. UnloadNTDriver(DRIVER_NAME);
  591. if (!bRet)

  592. {
  593.   printf("UnloadNTDriver error\n");
  594.   return;


  595. [/p]
  596. [align=left]}

  597. }


复制代码


回复

使用道具 举报

0

主题

19

回帖

51

积分

用户组: 小·技术宅

UID
150
精华
0
威望
1 点
宅币
30 个
贡献
0 次
宅之契约
0 份
在线时间
0 小时
注册时间
2014-3-24
发表于 2014-3-24 00:58:09 | 显示全部楼层
看不懂,收下了先~!
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-29 06:01 , Processed in 0.039971 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表