#include "Ring0.h"
//驱动在卸载的时候,经过这里,我们可以在这里释放一些资源。
//断开通话
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING DeviceName;
UNICODE_STRING DosDeviceName;
//删除符号链接
RtlInitUnicodeString(&DosDeviceName, DOSDEVICE);
IoDeleteSymbolicLink(&DosDeviceName );
if( DriverDeviceObject != NULL )
IoDeleteDevice( DriverDeviceObject );
DbgPrint("驱动卸载成功!\r\n");
}
//函数派遣的IRP,如果我们不需要用到这里通信。
NTSTATUS IODispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
//完成例程,表示调用者已经完成了对指定请求的所有处理操作。
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
//其实就是win32DLL里面的入口
//BOOL WINAPI DllMain(hinstDLL,fdwReason,lpvReserved);
//DRIVER_OBJECT其实是一个结构体
/*
lkd> dt_DRIVER_OBJECT
nt!_DRIVER_OBJECT
+0x000 Type : Int2B
+0x002 Size : Int2B
+0x004 DeviceObject : Ptr32 _DEVICE_OBJECT
+0x008 Flags : Uint4B
+0x00c DriverStart : Ptr32 Void //就是驱动在内存中的开始地址,也可以把它当成是一个pe文件的头开始
+0x010 DriverSize : Uint4B //驱动的大小
+0x014 DriverSection : Ptr32 Void
+0x018 DriverExtension : Ptr32 _DRIVER_EXTENSION
+0x01c DriverName : _UNICODE_STRING
+0x024 HardwareDatabase : Ptr32 _UNICODE_STRING
+0x028 FastIoDispatch : Ptr32 _FAST_IO_DISPATCH
+0x02c DriverInit : Ptr32 long
+0x030 DriverStartIo : Ptr32 void
+0x034 DriverUnload : Ptr32 void //驱动的卸载例程
+0x038 MajorFunction : [28] Ptr32 long
lkd> dt_UNICODE_STRING
nt!_UNICODE_STRING
+0x000 Length : Uint2B //这个是文本的长度
+0x002 MaximumLength : Uint2B //最大长度
+0x004 Buffer : Ptr32 Uint2B //这个是文本的内容 UNICODE
*/
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject,PUNICODE_STRING theRegistryPath)
{
UNICODE_STRING DeviceName;
UNICODE_STRING DosDeviceName;
NTSTATUS status;
//初始化驱动符号名
//UNICODE_STRING是一个结构体,类似win32里面的结构体,比如SYSTEMTIME
//在Win系统下,内核的结构体都是可以通过windbg或者wrk获得。
/*
lkd> dt_unicode_string
nt!_UNICODE_STRING
+0x000 Length : Uint2B //文本长度
+0x002 MaximumLength : Uint2B //最大长度
+0x004 Buffer : Ptr32 Uint2B //文本内容,是unicode类型,即WCHAR
*/
RtlInitUnicodeString(&DeviceName, DEVICE);
RtlInitUnicodeString(&DosDeviceName, DOSDEVICE);
//如果要跟用户层通信,我们需要创建一个设备
//如果我们要跟别人通话,是不是需要一部电话在手里?这个函数就相当于这部电话
status = IoCreateDevice(
DriverObject, // ptr to caller object
0, // extension device allocated byte number
&DeviceName, // device name
FILE_DEVICE_UNKNOWN,
0, // no special caracteristics
FALSE, // we can open many handles in same time
&DriverDeviceObject); // [OUT] ptr to the created object
if (!NT_SUCCESS(status)){
return STATUS_NO_SUCH_DEVICE;
}
//同样我们也需要一个符号链接,不然会影响到驱动和应用层的通信
//我们还需要一个移动卡
status = IoCreateSymbolicLink(&DosDeviceName,&DeviceName);
if( !NT_SUCCESS(status)){
IoDeleteDevice( DriverDeviceObject );
return STATUS_NO_SUCH_DEVICE;
}
//驱动通信例程
//这是驱动的卸载例程,类似于DLL里面的DLL_PROCESS_DETACH,要卸载这个驱动,就要设置卸载例程
DriverObject->DriverUnload = DriverUnload;
//IRP_MJ_CREATE,响应的是应用层函数CreateFile,应用层调用这个函数就会进入这个例程
DriverObject->MajorFunction[IRP_MJ_CREATE] = IODispatch; //CreateFile();
<pre class="cpp" name="code">#include "Ring0.h"
//驱动在卸载的时候,经过这里,我们可以在这里释放一些资源。
//断开通话
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING DeviceName;
UNICODE_STRING DosDeviceName;
//删除符号链接
RtlInitUnicodeString(&DosDeviceName, DOSDEVICE);
IoDeleteSymbolicLink(&DosDeviceName );
if( DriverDeviceObject != NULL )
IoDeleteDevice( DriverDeviceObject );
DbgPrint("驱动卸载成功!\r\n");
}
//函数派遣的IRP,如果我们不需要用到这里通信。
NTSTATUS IODispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
//完成例程,表示调用者已经完成了对指定请求的所有处理操作。
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
//其实就是win32DLL里面的入口
//BOOL WINAPI DllMain(hinstDLL,fdwReason,lpvReserved);
//DRIVER_OBJECT其实是一个结构体
/*
lkd> dt_DRIVER_OBJECT
nt!_DRIVER_OBJECT
+0x000 Type : Int2B
+0x002 Size : Int2B
+0x004 DeviceObject : Ptr32 _DEVICE_OBJECT
+0x008 Flags : Uint4B
+0x00c DriverStart : Ptr32 Void //就是驱动在内存中的开始地址,也可以把它当成是一个pe文件的头开始
+0x010 DriverSize : Uint4B //驱动的大小
+0x014 DriverSection : Ptr32 Void
+0x018 DriverExtension : Ptr32 _DRIVER_EXTENSION
+0x01c DriverName : _UNICODE_STRING
+0x024 HardwareDatabase : Ptr32 _UNICODE_STRING
+0x028 FastIoDispatch : Ptr32 _FAST_IO_DISPATCH
+0x02c DriverInit : Ptr32 long
+0x030 DriverStartIo : Ptr32 void
+0x034 DriverUnload : Ptr32 void //驱动的卸载例程
+0x038 MajorFunction : [28] Ptr32 long
lkd> dt_UNICODE_STRING
nt!_UNICODE_STRING
+0x000 Length : Uint2B //这个是文本的长度
+0x002 MaximumLength : Uint2B //最大长度
+0x004 Buffer : Ptr32 Uint2B //这个是文本的内容 UNICODE
*/
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject,PUNICODE_STRING theRegistryPath)
{
UNICODE_STRING DeviceName;
UNICODE_STRING DosDeviceName;
NTSTATUS status;
//初始化驱动符号名
//UNICODE_STRING是一个结构体,类似win32里面的结构体,比如SYSTEMTIME
//在Win系统下,内核的结构体都是可以通过windbg或者wrk获得。
/*
lkd> dt_unicode_string
nt!_UNICODE_STRING
+0x000 Length : Uint2B //文本长度
+0x002 MaximumLength : Uint2B //最大长度
+0x004 Buffer : Ptr32 Uint2B //文本内容,是unicode类型,即WCHAR
*/
RtlInitUnicodeString(&DeviceName, DEVICE);
RtlInitUnicodeString(&DosDeviceName, DOSDEVICE);
//如果要跟用户层通信,我们需要创建一个设备
//如果我们要跟别人通话,是不是需要一部电话在手里?这个函数就相当于这部电话
status = IoCreateDevice(
DriverObject, // ptr to caller object
0, // extension device allocated byte number
&DeviceName, // device name
FILE_DEVICE_UNKNOWN,
0, // no special caracteristics
FALSE, // we can open many handles in same time
&DriverDeviceObject); // [OUT] ptr to the created object
if (!NT_SUCCESS(status)){
return STATUS_NO_SUCH_DEVICE;
}
//同样我们也需要一个符号链接,不然会影响到驱动和应用层的通信
//我们还需要一个移动卡
status = IoCreateSymbolicLink(&DosDeviceName,&DeviceName);
if( !NT_SUCCESS(status)){
IoDeleteDevice( DriverDeviceObject );
return STATUS_NO_SUCH_DEVICE;
}
//驱动通信例程
//这是驱动的卸载例程,类似于DLL里面的DLL_PROCESS_DETACH,要卸载这个驱动,就要设置卸载例程
DriverObject->DriverUnload = DriverUnload;
//IRP_MJ_CREATE,响应的是应用层函数CreateFile,应用层调用这个函数就会进入这个例程
DriverObject->MajorFunction[IRP_MJ_CREATE] = IODispatch; //CreateFile();
//下面的分别对应CloseHandle ReadFile WriteFile
DriverObject->MajorFunction[IRP_MJ_CLOSE] = IODispatch; //CloseHandle() //你可以把这些想象成是不同的电话号码
DriverObject->MajorFunction[IRP_MJ_READ] = IODispatch; //ReadFile()
DriverObject->MajorFunction[IRP_MJ_WRITE] = IODispatch; //WriteFile()
//一般我们跟应用层通信,都是通过IRP_MJ_DEVICE_CONTROL例程,这个例程对应的是应用层下的DeviceIoControl
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IODispatch; //DeviceIoControl()
//像所有入门程序一样,我们可以打印出Hello World证明我们的驱动加载成功
DbgPrint("Hello World\r\n");
return STATUS_SUCCESS;
}
//下面的分别对应CloseHandle ReadFile WriteFileDriverObject->MajorFunction[IRP_MJ_CLOSE] = IODispatch; //CloseHandle() //你可以把这些想象成是不同的电话号码DriverObject->MajorFunction[IRP_MJ_READ] = IODispatch; //ReadFile()DriverObject->MajorFunction[IRP_MJ_WRITE] = IODispatch; //WriteFile()//一般我们跟应用层通信,都是通过IRP_MJ_DEVICE_CONTROL例程,这个例程对应的是应用层下的DeviceIoControlDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IODispatch; //DeviceIoControl()//像所有入门程序一样,我们可以打印出Hello World证明我们的驱动加载成功DbgPrint("Hello World\r\n");return STATUS_SUCCESS;}