首页
社区
课程
招聘
[求助]用DeviceIoControl传送数据到内核,驱动获取InputBuffer的地址为0
发表于: 2014-8-5 19:54 3537

[求助]用DeviceIoControl传送数据到内核,驱动获取InputBuffer的地址为0

2014-8-5 19:54
3537
ring3:
#include <iostream>
#include <windows.h>
#include <winioctl.h>
using namespace std;
#define IOCTL_TEST CTL_CODE(\
FILE_DEVICE_UNKNOWN, \
0x800, \
METHOD_BUFFERED, \
FILE_ANY_ACCESS)
void main()
{
HANDLE hDevice =
CreateFile("\\\\.\\HelloDDK",
GENERIC_READ | GENERIC_WRITE,
0,         // share mode none
NULL,        // no security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );         // no template
cout<<GetLastError();
BYTE InputBuffer[10]={0};
BYTE OutputBuffer[10];
DWORD dwOutput;
DeviceIoControl(hDevice, IOCTL_TEST,InputBuffer, 10, OutputBuffer, 10, &dwOutput, NULL);
for (int i=0;i<(int)dwOutput;i++)
{
cout<<OutputBuffer[i];
}
cout<<dwOutput;
CloseHandle(hDevice);
}

ring0:
NTSTATUS HelloDDKDeviceIOControl(IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;
KdPrint(("Enter HelloDDKDeviceIOControl\n"));

//得到当前堆栈
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
//得到输入缓冲区大小
ULONG cbin = stack->Parameters.DeviceIoControl.InputBufferLength;
//得到输出缓冲区大小
ULONG cbout = stack->Parameters.DeviceIoControl.OutputBufferLength;
//得到IOCTL码
ULONG code = stack->Parameters.DeviceIoControl.IoControlCode;

ULONG info = 0;

switch (code)
{         // process request

case IOCTL_TEST:
{
UCHAR* UserInputBuffer = (UCHAR*)stack->Parameters.DeviceIoControl.Type3InputBuffer;
KdPrint(("UserInputBuffer:0X%0X\n",UserInputBuffer));//这里显示地址为0

//得到用户模式地址
PVOID UserOutputBuffer = pIrp->UserBuffer;

KdPrint(("UserOutputBuffer:0X%0X\n",UserOutputBuffer));
__try
{
KdPrint(("Enter __try block\n"));

//判断指针是否可读
ProbeForRead(UserInputBuffer,cbin,4);
//显示输入缓冲区内容
for (ULONG i=0;i<cbin;i++)
{
KdPrint(("%X\n",UserInputBuffer[i]));
}

//判断指针是否可写
ProbeForWrite(UserOutputBuffer,cbout,4);

memset(UserOutputBuffer,0xAA,cbout);

info = cbout;

KdPrint(("Leave __try block\n"));
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
KdPrint(("Catch the exception\n"));
KdPrint(("The program will keep going\n"));
status = STATUS_UNSUCCESSFUL;
}
         info = cbout;
break;
}

default:
status = STATUS_INVALID_VARIANT;
}

// 完成IRP
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = info;        // bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );

KdPrint(("Leave HelloDDKDeviceIOControl\n"));

return status;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 544
活跃值: (264)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
2
#define IOCTL_TEST CTL_CODE(\
FILE_DEVICE_UNKNOWN, \
0x800, \
METHOD_NEITHER, \
FILE_ANY_ACCESS)

http://book.51cto.com/art/201107/275240.htm
2014-8-5 21:32
0
雪    币: 96
活跃值: (64)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
牛逼!!简单明了
2014-8-5 21:50
0
游客
登录 | 注册 方可回帖
返回
//