龙马谷

 找回密码
 立即注册

QQ登录

只需一步,快速开始

龙马谷VIP会员办理客服QQ:82926983(如果临时会话没有收到回复,请先加QQ好友再发。)
1 [已完结] GG修改器新手入门与实战教程 31课 2 [已完结] GG修改器美化修改教程 6课 3 [已完结] GG修改器Lua脚本新手入门教程 12课
4 [已完结] 触动精灵脚本新手入门必学教程 22课 5 [已完结] 手游自动化脚本入门实战教程 9课 6 [已完结] C++射击游戏方框骨骼透视与自瞄教程 27课
7 [已完结] C++零基础UE4逆向开发FPS透视自瞄教程 29课 8 [已完结] C++零基础大漠模拟器手游自动化辅助教程 22课 9 [已完结] C++零基础开发DXF内存脚本辅助教程 32课
以下是天马阁VIP教程,本站与天马阁合作,赞助VIP可以获得天马阁对应VIP会员,名额有限! 点击进入天马阁论坛
1 [已完结] x64CE与x64dbg入门基础教程 7课 2 [已完结] x64汇编语言基础教程 16课 3 [已完结] x64辅助入门基础教程 9课
4 [已完结] C++x64内存辅助实战技术教程 149课 5 [已完结] C++x64内存检测与过检测技术教程 10课 6 [已完结] C+x64二叉树分析遍历与LUA自动登陆教程 19课
7 [已完结] C++BT功能原理与x64实战教程 29课 8 [已完结] C+FPS框透视与自瞄x64实现原理及防护思路
查看: 4984|回复: 0

VS2019驱动调用驱动源码

[复制链接]

19

主题

0

回帖

33

积分

编程入门

Rank: 1

龙马币
144


  1. #pragma once
  2. //DriverA
  3. #include <ntddk.h>

  4. //创建自定义设备扩展
  5. typedef struct _DEVICE_EXTENTION
  6. {
  7.     PDEVICE_OBJECT pDeviceObject;
  8.     UNICODE_STRING uszDeviceName;
  9.     UNICODE_STRING uszSymbolName;
  10.     PIRP pCurrentIrp;
  11.     KTIMER kTimer;
  12.     KDPC kDpc;
  13. }DEVICE_EXTENTION, *PDEVICE_EXTENTION;

  14. //驱动入口
  15. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName);
  16. //驱动卸载
  17. VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject);
  18. //创建设备对象 为了交互数据
  19. NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject);
  20. //普通回调
  21. NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
  22. //读取回调 将当前要处理的IRP挂起 3秒后触发DPC回调结束IRP 同步
  23. NTSTATUS DispatchRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);

  24. #include "DriverA.h"

  25. //DPC回调
  26. VOID DeferTimer(
  27.     _In_     struct _KDPC *Dpc,
  28.     _In_opt_ PVOID        DeferredContext,
  29.     _In_opt_ PVOID        SystemArgument1,
  30.     _In_opt_ PVOID        SystemArgument2
  31. )
  32. {
  33.     PDEVICE_EXTENTION pDeviceExtention = NULL;
  34.     PDEVICE_OBJECT pDeviceObject = (PDEVICE_OBJECT)DeferredContext;
  35.     pDeviceExtention = (PDEVICE_EXTENTION)pDeviceObject->DeviceExtension;
  36.     PIRP pCurrentIrp = pDeviceExtention->pCurrentIrp;
  37.     KdPrint(("DriverA\n"));
  38.     pCurrentIrp->IoStatus.Information = 0;
  39.     pCurrentIrp->IoStatus.Status = STATUS_SUCCESS;
  40.     IoCompleteRequest(pCurrentIrp, IO_NO_INCREMENT);
  41. }

  42. //驱动卸载
  43. VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject)
  44. {
  45.     PDEVICE_EXTENTION pDeviceExtention = NULL;
  46.     PDEVICE_OBJECT pFirstDeviceObject = NULL;
  47.     pFirstDeviceObject = pDriverObject->DeviceObject;
  48.     ASSERT(pFirstDeviceObject != NULL);
  49.     pDeviceExtention = (PDEVICE_EXTENTION)(pFirstDeviceObject->DeviceExtension);
  50.     KeCancelTimer(&pDeviceExtention->kTimer);
  51.     pDeviceExtention = (PDEVICE_EXTENTION)pFirstDeviceObject->DeviceExtension;
  52.     IoDeleteSymbolicLink(&pDeviceExtention->uszSymbolName);
  53.     IoDeleteDevice(pDeviceExtention->pDeviceObject);
  54. }

  55. //创建设备对象 为了交互数据
  56. NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject)
  57. {
  58.     NTSTATUS ntStatus = STATUS_SUCCESS;
  59.     UNICODE_STRING uszDeviceName = RTL_CONSTANT_STRING(L"\\Device\\DriverA");
  60.     UNICODE_STRING uszSymbolName = RTL_CONSTANT_STRING(L"\\??\\DriverASymbol");
  61.     PDEVICE_OBJECT pDeviceObject = NULL;
  62.     PDEVICE_EXTENTION pDeviceExtention = NULL;

  63.     ntStatus = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENTION), &uszDeviceName,
  64.         FILE_DEVICE_UNKNOWN, 0, TRUE, &pDeviceObject);
  65.     if (!NT_SUCCESS(ntStatus))
  66.     {
  67.         KdPrint(("IoCreateDevice 错误:%x\n", ntStatus));
  68.         return ntStatus;
  69.     }
  70.     ntStatus = IoCreateSymbolicLink(&uszSymbolName, &uszDeviceName);
  71.     if (!NT_SUCCESS(ntStatus))
  72.     {
  73.         KdPrint(("IoCreateSymbolicLink 错误:%x\n", ntStatus));
  74.         return ntStatus;
  75.     }
  76.     pDeviceExtention = (PDEVICE_EXTENTION)(pDeviceObject->DeviceExtension);
  77.     //初始化时间对象
  78.     KeInitializeTimer(&pDeviceExtention->kTimer);
  79.     //初始化DPC对象
  80.     KeInitializeDpc(&pDeviceExtention->kDpc, DeferTimer, (PVOID)pDeviceObject);
  81.     pDeviceExtention->pDeviceObject = pDeviceObject;
  82.     pDeviceExtention->uszDeviceName = uszDeviceName;
  83.     pDeviceExtention->uszSymbolName = uszSymbolName;
  84.     pDeviceObject->Flags |= DO_DIRECT_IO;

  85.     return ntStatus;
  86. }

  87. //普通回调
  88. NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
  89. {
  90.     NTSTATUS ntStatus = STATUS_SUCCESS;
  91.     pIrp->IoStatus.Information = 0;
  92.     pIrp->IoStatus.Status = ntStatus;
  93.     IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  94.     return ntStatus;
  95. }

  96. //读取回调 将当前要处理的IRP挂起 3秒后触发DPC回调结束IRP 同步
  97. NTSTATUS DispatchRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
  98. {
  99.     PDEVICE_EXTENTION pDeviceExtention = NULL;

  100.     IoMarkIrpPending(pIrp);

  101.     pDeviceExtention = (PDEVICE_EXTENTION)(pDeviceObject->DeviceExtension);
  102.     pDeviceExtention->pCurrentIrp = pIrp;
  103.     LARGE_INTEGER aa = { 0 };
  104.     aa.QuadPart = -30000000;
  105.     //启动定时器 3秒后进入DPC回调
  106.     KeSetTimer(
  107.         &pDeviceExtention->kTimer,
  108.         aa,
  109.         &pDeviceExtention->kDpc);
  110.     return STATUS_PENDING;
  111. }

  112. //驱动入口
  113. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName)
  114. {
  115.     NTSTATUS ntStatus = STATUS_SUCCESS;
  116.     pDriverObject->DriverUnload = MyDriverUnload;
  117.     CreateDevice(pDriverObject);
  118.     for (ULONG64 uCount = 0; uCount < IRP_MJ_MAXIMUM_FUNCTION; uCount++)
  119.     {
  120.         pDriverObject->MajorFunction[uCount] = DispatchGeneral;
  121.     }
  122.     pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
  123.     return ntStatus;
  124. }

  125. #include <ntddk.h>
  126. //DriverB
  127. VOID DriverUnLoad(PDRIVER_OBJECT pDriverObj)
  128. {
  129.     KdPrint(("Driver unload\n"));
  130. }

  131. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
  132. {
  133.     KdPrint(("Driver load\n"));
  134.     pDriverObj->DriverUnload = DriverUnLoad;
  135.     HANDLE hDeivce = NULL;
  136.     OBJECT_ATTRIBUTES objAttr = { 0 };
  137.     IO_STATUS_BLOCK ioStack = { 0 };
  138.     NTSTATUS ntStatus = 0;
  139.     UNICODE_STRING uszDeviceName = RTL_CONSTANT_STRING(L"\\Device\\DriverA");
  140.     InitializeObjectAttributes(&objAttr, &uszDeviceName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  141.     ntStatus = ZwCreateFile(&hDeivce, GENERIC_ALL, &objAttr, &ioStack, 0, FILE_ATTRIBUTE_NORMAL, FILE_READ_ACCESS, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_ALERT, NULL, 0);
  142.     if (!NT_SUCCESS(ntStatus))
  143.     {
  144.         KdPrint(("ZwCreateFile 错误\n"));
  145.         return ntStatus;
  146.     }
  147.     ntStatus = ZwReadFile(hDeivce, NULL, NULL, NULL, &ioStack, NULL, 0, 0, NULL);
  148.     if (!NT_SUCCESS(ntStatus))
  149.     {
  150.         KdPrint(("ZwReadFile 错误\n"));
  151.         ZwClose(hDeivce);
  152.         return ntStatus;
  153.     }
  154.     ZwClose(hDeivce);
  155.     KdPrint(("DriverB\n"));
  156.     return STATUS_SUCCESS;
  157. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

龙马谷| C/C++辅助教程| 安卓逆向安全| 论坛导航| 免责申明|Archiver|
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表龙马谷立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2018-2021 All Right Reserved.
在线客服
快速回复 返回顶部 返回列表