Windows中0环与3环通信

Windows中0环与3环通信,第1张

Windows中0环与3环通信(常规方式)

Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html

推荐阅读:

  1. Windows驱动学习(二)-- 驱动层&应用层通信


一、知识点讲解

1. 设备对象

  我们在开发窗口程序的时候,消息被封装成一个结构体:MSG,在内核开发时,消息被封装成另外一个结构体:IRP(I/O Request Package I/O请求包)。


  在窗口程序中,能够接收消息的只能是窗口对象(由窗口对象将消息分发给各个窗口过程)。


  在内核中,能够接收IRP消息的只能时设备对象。


    

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcoAAADfCAIAAAD5kgQXAAAgAElEQVR4Ae2dB3yURfrHZ1t2s7vplSQQ0iEEDL33IgKhioIoilgODjj0kMM/53kWDvE4xXJ4eioWUASlCIcISgmhCUJCAiRACqmk9022/2d3s8km2SybLcm+ye/96O77zjzzzDPfyf6Yd95535elVqsJNhAAARAAAVsTYNvaIfyBAAiAAAhoCEBe8XcAAiAAAnYhAHm1C1Y4BQEQAAHIK/4GQAAEQMAuBCCvdsEKpyAAAiAAecXfAAiAAAjYhQDk1S5Y4RQEQAAEIK/4GwABEAABuxCAvNoFK5yCAAiAABcIQAAEui0BlVqdU1hULZFU1tTWyaQyuaLboqANd+JxnZ34bmKRq0gU5OvDZrGspAF5tRIgioMAIwmk5+Wn3s2+nZsnk8sZ2QA7B+3E40UEBUb2DIroGWRxVSw8c8BidigIAkwkcK+07NTVxOzCIl3wykq1LE+tqlEpq4m6eysti0c4LoQtZjsFsDjuDUPXAG+vCYNig3x8LOhryKsF0FAEBBhJgE4FnLqSeDk1jUavkhLJZYUkTaUsw0OdjPQmx5MljGILh3DZfE1ubET4lKGD2ztdAHk1QhZJIND1CEhlsh/Pns/MLyAqUvu7suaCgiosNtMEqLaKR3BFgzn06Sx0ADtn7GiRs8B0EcNcyKshDeyDQNckIFModv58vKSiUlVHyn+Uy3JUXbOd9mmVU0+2x2we25l4ubo+Nm2yM187oDWjLizMM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mt+isTweV8jnu4pEdBIgomcgnRBoYdDeQ8hre4nBHgRAAATMIoCFWWZhghEIgAAItJcA5LW9xGAPAiAAAmYRgLyahQlGIAACINBeApDX9hKDPQiAAAiYRQDyahYmGIEACIBAewlAXttLDPYgAAIgYBYByKtZmGAEAiAAAu0lAHltLzHYgwAIgIBZBCCvZmGCEQiAAAi0l8D/A/dYA6tz0AjYAAAAAElFTkSuQmCC" alt="" />

2. 创建设备对象

  调用 IoCreateDevice API 来创建设备对象,其中需要传入设备名称(R3依据这个找到),需要初始化字符串。


 //创建设备名称
UNICODE_STRING Devicename;
RtlInitUnicodeString(&Devicename,L"\\Device\\MyDevice"); //创建设备
IoCreateDevice(
pDriver, //当前设备所属的驱动对象
,
&Devicename, //设备对象的名称
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&pDeviceObj //设备对象指针
);

3. 设置数据交互方式

  pDeviceObj->Flags |= DO_BUFFERED_IO (注意: I= 表示按位或的含义)

  其存在三种读写方式:

  1)缓冲区读写方式(DO_BUFFERED_IO): *** 作系统将应用程序提供缓冲区的数据复制到内核模式下的地址中。


  2)  直接方式读写(DO_DIRECT_IO): *** 作系统会将用户模式下的缓冲区锁住。


然后 *** 作系统将这段缓冲区在内核模式地址再次映射一遍。


这样,用户模式的缓冲区和内核模式的缓冲区指向的时同一区域的物理内存。


缺点就是要单独占用物理页面。


  3)其他方式读写(不设置Flags):很危险,直接读写缓冲区地址,很容易出现蓝屏并且很可能数据丢失(读取过程中挂起页置换)。


4. 创建符号链接

  //创建符号链接名称

  RtlInitUnicodeString(&SymbolicLinkName,L"\\??\\MyTestDriver");

  // 创建符号链接

  IoCreateSymbolicLink(&SymbolicLinkName,&Devicename);

  特别说明:

  1)设备名称的作用就是给内核对象用的,如果要在R3访问,必须要有符号链接。


其实就是一个别名,没有这个别名,在R3不可见


  2)在内核模式下,符号链接是以 '\??\'开头的,如果C盘就是 "\??\C:"

  3)  在用户模式下,则是以 '\\.\' 开头的,如果是C盘就是 "\\.\C:"

5. IRP与派遣函数

  如下图,在R3层面上,其由窗口对象负责将消息发送给对应的回调函数;而在内核层,将IRP发送给设备对象,由设备对象转发给对应的派遣函数。


  

6、IRP的类型

  微软文档 :IRP structure

  1)  当应用层通过CreateFile,ReadFile,WriteFile,CloseHandle等函数打开、从设备读取数据、向设备写入数据、关闭设备的时候,会时 *** 作系统产生出IRP_MJ_CREATE、IRP_MJ_READ、IRP_MJ_CLOSE等不同的IRP。


  2)其他的IRP类型

    IRP_MJ_DEVICE_CONTROL    DeviceControl函数会产生此IRP

    IRP_MJ_POWER        在 *** 作系统处理电源消息时,产生次IRP

    IRP_MJ_SHUTDOWN      关闭系统前会产生此IRP

7、派遣函数在哪里注册呢?

  其在 _DRIVER_OBJECT 函数最后一个数组中。


其派遣函数的种类及其有限(由IRP消息类型限制),可以看出一共有29种。


  关于 _DRIVER_OBJECT的介绍可以查看之前这篇博客:内核空间与内核地址

  

8. 注册派遣函数

  如下图,我们直接采取对数组赋值的形式来设置派遣函数。


  

9. 派遣函数格式

  一定要设置其返回状态 NTSTATUS,三环程序判断API是否调用成功就是根据这个状态,如果不设置,则可能会出错。


  


二、3环与0环通讯案例讲解

  下面使用驱动编写0环程序,C++编写3环程序。


3环程序负责向0环程序读取内容。


驱动代码

 #include <ntddk.h>

 // 定义设备名和符号名
#define DEVICE_NAME L"\\Device\\MTReadDevice_asajs123akdas"
#define SYM_LINK_NAME L"\\??\\MTRead_asdkasjkadsjldasss213k" // 设备创建函数
NTSTATUS DeviceCreate(PDEVICE_OBJECT Device, PIRP pIrp)
{
__asm int
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = ;
// I/O请求处理完毕
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("Create Device Success\n");
return STATUS_SUCCESS;
} // 设备读 *** 作函数
NTSTATUS DeviceRead(PDEVICE_OBJECT Device, PIRP pIrp)
{
__asm int
// 获取指向IRP的堆栈的指针
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
// 获取堆栈长度
ULONG length = stack->Parameters.Read.Length;
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = length;
// 将堆栈上的数据全设置为0xAA
memset(pIrp->AssociatedIrp.SystemBuffer, 0xBB, length);
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("Read Device Success\n");
return STATUS_SUCCESS;
} // 设备关闭函数
NTSTATUS DeviceClose(PDEVICE_OBJECT Device, PIRP pIrp)
{
__asm int
// 跟设备创建函数相同
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = ;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
DbgPrint("Close Device Success\n");
return STATUS_SUCCESS;
} // 驱动卸载函数
NTSTATUS DriverUnload(PDRIVER_OBJECT Driver)
{
NTSTATUS status;
__asm int
// 删除符号和设备
UNICODE_STRING SymLinkName;
RtlInitUnicodeString(&SymLinkName, SYM_LINK_NAME); status = (IoDeleteSymbolicLink(&SymLinkName));
DbgPrint("删除状态码:%x", status); PDRIVER_OBJECT pFirstObj = Driver->DeviceObject;
if (pFirstObj->DeviceType == FILE_DEVICE_COMPORT)
{ } IoDeleteDevice(Driver->DeviceObject);
DbgPrint("This Driver Is Unloading...\n");
return STATUS_SUCCESS;
} // 驱动入口函数
NTSTATUS DriverEntry(PDRIVER_OBJECT Driver, PUNICODE_STRING RegPath)
{
__asm int
NTSTATUS status; UNICODE_STRING DeviceName;
UNICODE_STRING SymLinkName;
// 将设备名转换为Unicode字符串
RtlInitUnicodeString(&DeviceName, DEVICE_NAME);
// 创建设备对象
PDEVICE_OBJECT pDevice = NULL;
Driver->DriverUnload = DriverUnload;
status = IoCreateDevice(Driver, , &DeviceName, FILE_DEVICE_UNKNOWN, , FALSE, &pDevice);
if (!NT_SUCCESS(status))
{
IoDeleteDevice(pDevice);
DbgPrint("Create Device Faild!\n");
return STATUS_UNSUCCESSFUL;
}
// 设置pDevice以缓冲区方式读取
pDevice->Flags = DO_BUFFERED_IO; // 将符号名转换为Unicode字符串
RtlInitUnicodeString(&SymLinkName, SYM_LINK_NAME);
// 将符号与设备关联
status = IoCreateSymbolicLink(&SymLinkName, &DeviceName);
if (!NT_SUCCESS(status))
{
DbgPrint("Create SymLink Faild!\n");
IoDeleteDevice(pDevice);
return STATUS_UNSUCCESSFUL;
} DbgPrint("Initialize Success\n"); // 注册设备创建函数、设备读函数、设备关闭函数、驱动卸载函数
Driver->MajorFunction[IRP_MJ_CREATE] = DeviceCreate;
Driver->MajorFunction[IRP_MJ_READ] = DeviceRead;
Driver->MajorFunction[IRP_MJ_CLOSE] = DeviceClose; DbgPrint("Initialize Success\n"); return STATUS_SUCCESS;
}

3环应用程序代码

 // 从驱动中读取数据.cpp : 此文件包含 "main" 函数。


程序执行将在此处开始并结束。



// #include "pch.h"
#include <stdio.h>
#include <tchar.h>
#include <Windows.h> int main()
{
// 因为驱动层已经注册符号了,那么在R3层其就可以显示出来,以"\\.\SymName" 名称显示出来 // 打开设备句柄(根据注册符号)
HANDLE hDevice = CreateFile(L"\\\\.\\MTRead_asdkasjkadsjldasss213k", GENERIC_READ | GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Failed to Obtain Device Handle!");
getchar();
getchar();
getchar();
return -;
} // 创建一个缓冲区进行读写
UCHAR buffer[];
ULONG size;
BOOL result = ReadFile(hDevice, buffer, , &size, NULL);
if (result) { // 打印到控制台
printf(" Read %d bytes :", size);
for (int i = ; i < (int)size; i++) {
printf("%02x", buffer[i]);
}
printf("\n");
} // 关闭设备句柄
CloseHandle(hDevice);
getchar();
getchar();
getchar();
getchar();
return ;
}


三、推荐阅读:

  1. Windows驱动学习(二)-- 驱动层&应用层通信

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/587953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存