青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

S.l.e!ep.¢%

像打了激速一樣,以四倍的速度運轉,開心的工作
簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

Writing a device driver for Windows

Posted on 2009-10-30 11:04 S.l.e!ep.¢% 閱讀(599) 評論(1)  編輯 收藏 引用 所屬分類: RootKit

Writing a device driver for Windows

In order to write a device driver for windows, one needs the device driver development kit (ddk) and a c compiler.
According to this article, a device driver's maximum size is 960MB on Windows XP (100MB on NT4, 220MB on Win2K).

Setting up the environment

A proper environment must be setup. Use setenv (which ships with the ddk) to set the environment variables (and what not) to build a driver:
C:\>programme\ntddk\bin\setenv \programme\ntddk.
The argument that is given to setenv must point to the directory under which the ddk is installed.

makefile

The directory that contains the sources for the device driver must have a file called makefile and another file called sources. For a simple device driver, it is sufficient to have one single line in the makefile:
!INCLUDE $(NTMAKEENV)\makefile.def

sources

This file actually contains the names of the files to be compiled:
TARGETNAME=kamel
TARGETPATH=obj
TARGETTYPE=DRIVER

SOURCES=kamel.c writeEvent.c kamelMsg.rc

C_DEFINES=-DUNICODE -DSTRICT
kamel.c is the code for the driver itself, writeEvent.c contains a function that can be called to write messages to the system event log (see below) and kamelMsg.rc contains the strings that are written

Writing the driver

I call the driver we're going to write Kamel. In german, this will then be called Kameltreiber which is a pun german speaking people will understand. So, we're creating (according to the sources file) a file called kamel.c. The first lines contain the includes we need:
#include "ntddk.h"
#include "writeEvent.h"
#include "kamelMsg.h"
ntddk.h must always be included, writeEvent.h contains the declaration of WriteEvent (which is a function to write events, of course) and kamelMsg.h (being created by the message compiler) contains the identifiers of the strings we want to write using WriteEvent.
Each driver needs a DriverEntry function which is called when the driver is loaded:
Now, we use write the forward declarations together with the pragmas alloc_text. They indicate wheather or not the function is pageable.
#define BUFFERSIZE 1024
#define BUFFERTAG  'kmlb'

typedef struct _KAMEL_DRIVER_EXTENSION {
  char buffer[BUFFERSIZE];
} KAMEL_DRIVER_EXTENSION, *PKAMEL_DRIVER_EXTENSION;

KAMEL_DRIVER_EXTENSION* driverExtension=0;


NTSTATUS DriverEntry  (IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
NTSTATUS CreateCamel  (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS ReadCamel    (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS WriteCamel   (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS ShutdownCamel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS CleanupCamel (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS IoCtlCamel   (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
VOID     CmlUnload    (IN PDRIVER_OBJECT  DriverObject);


#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, CreateCamel) 
#pragma alloc_text(PAGE, ReadCamel) 
#pragma alloc_text(PAGE, WriteCamel) 
#pragma alloc_text(PAGE, ShutdownCamel)
#pragma alloc_text(PAGE, IoCtlCamel)
#pragma alloc_text(PAGE, CmlUnload)
#endif
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {

  UNICODE_STRING nameString, linkString;
  PDEVICE_OBJECT deviceObject;
  NTSTATUS status;

  WriteEvent(MSG_DRIVER_ENTRY,DriverObject,NULL);

  RtlInitUnicodeString(&nameString, L"\\Device\\Kamel");

  status = IoCreateDevice(
    DriverObject, 
    sizeof(65533),
    &nameString, 
    0, //FILE_DEVICE_UNKNOWN,
    0, 
    FALSE, 
    &deviceObject);

  if (!NT_SUCCESS(status))
    return status;


  deviceObject->Flags |= DO_DIRECT_IO;
  deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;


  RtlInitUnicodeString(&linkString, L"\\DosDevices\\Kamel");
  status = IoCreateSymbolicLink (&linkString, &nameString);

  if (!NT_SUCCESS(status)) {
    IoDeleteDevice (DriverObject->DeviceObject);
    return status;
  }


  DriverObject->MajorFunction[IRP_MJ_CREATE]         = CreateCamel;
  DriverObject->MajorFunction[IRP_MJ_READ]           = ReadCamel;
  DriverObject->MajorFunction[IRP_MJ_WRITE]          = WriteCamel;
  DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]       = ShutdownCamel;
  DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IoCtlCamel;
  
  DriverObject->DriverUnload=CmlUnload;

  // ExAllocatePool is obsolete and ExAllocatePoolWithTag should be used.
  driverExtension = ExAllocatePool(NonPagedPool, sizeof (KAMEL_DRIVER_EXTENSION));

  if(!driverExtension) {
    WriteEvent(MSG_NO_IOALLOCATEDRIVEROBJECTEXTENSION, DriverObject, NULL);
    return STATUS_INSUFFICIENT_RESOURCES;
  }

  RtlZeroMemory(driverExtension->buffer, BUFFERSIZE);

  RtlCopyBytes (driverExtension->buffer, "123456789012345", 16);

  return STATUS_SUCCESS;
}
DriverEntry first writes an Event (using WriteEvent, explained later) so it can be verified that DriverEntry indeed was called. Then, the actual device is created using IoCreateDevice and initialized.

Setting Up Major Functions

An Application communicates with a driver with the driver's Major Functions. These are set in the drivers array of function pointers MajorFunction.

User Visible Name for the driver

In order to create a user-visible name for the device just created, IoCreateSymbolicLink is called.

Allocating Pool Memory

The driver allocates some Pool Memory with ExAllocatePool.
By the way, Paged and Non-Paged Pool Memory sized can be adjusted with the registry keys HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\(Non)PagedPoolSize. The Value specified is the size in bytes.

Programming the Major Functions

In DriverEntry, the Major Functions IRP_MJ_CREATE, IRP_MJ_READ, IRP_MJ_WRITE, IRP_MJ_SHUTDOWN, IRP_MJ_DEVICE_CONTROL were set. Here are the actual functions they point to:

IRP_MJ_CREATE

This function is called when a file using this deivce is created. In Win32Api, Devices are opened using CreateFile which then routes in the function associated with IRP_MJ_CREATE.
NTSTATUS CreateCamel (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
  WriteEvent(MSG_CREATE,(PVOID)DeviceObject,NULL);

  IoCompleteRequest(Irp,IO_NO_INCREMENT);
  return  STATUS_SUCCESS;
}

IRP_MJ_READ

NTSTATUS ReadCamel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
  PUCHAR                      currentAddress;
  PIO_STACK_LOCATION          irpStack;

  WriteEvent(MSG_READ,DeviceObject,NULL);

  if (!driverExtension) {
    WriteEvent(MSG_DRIVEREXTISNULLINREAD,DeviceObject,NULL);
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_INSUFFICIENT_RESOURCES;
  }
  irpStack = IoGetCurrentIrpStackLocation(Irp);

  if (irpStack->MajorFunction == IRP_MJ_READ) {
    currentAddress = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

    if (!currentAddress) {
      WriteEvent(MSG_MMGETSYSTEMADDRESS,DeviceObject,NULL);
      IoCompleteRequest(Irp, IO_NO_INCREMENT);
      return STATUS_SUCCESS;
    }
    RtlMoveMemory(currentAddress, 
    driverExtension->buffer+irpStack->Parameters.Read.ByteOffset.LowPart,
    irpStack->Parameters.Read.Length);
  }
  else {
    WriteEvent(MSG_MAJORFUNC_NOT_READ,DeviceObject,NULL);
  }

  IoCompleteRequest(Irp, IO_NO_INCREMENT);
  return STATUS_SUCCESS;
}
A driver should call IoGetCurrentIrpStackLocation in its IRP function to receive a pointer to a IO_STACK_LOCATION structure.
MmGetSystemAddressForMdlSafe is a macro. It returns a virtual address to non system-space for the buffer described by the MDL.

IRP_MJ_WRITE

NTSTATUS WriteCamel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
  PUCHAR                      currentAddress;
  PIO_STACK_LOCATION          irpStack;

  if (!driverExtension) {
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_INSUFFICIENT_RESOURCES;
  }

  irpStack = IoGetCurrentIrpStackLocation(Irp);

  if (irpStack->MajorFunction == IRP_MJ_WRITE) {
    currentAddress = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

    if (!currentAddress) {
      IoCompleteRequest(Irp, IO_NO_INCREMENT);
      return STATUS_SUCCESS;
    }

    RtlMoveMemory(driverExtension->buffer+irpStack->Parameters.Write.ByteOffset.LowPart,
        currentAddress, irpStack->Parameters.Write.Length);
  }
  else {
    WriteEvent(MSG_MAJORFUNC_NOT_READ,DeviceObject,NULL);
  }

  IoCompleteRequest(Irp, IO_NO_INCREMENT);
  return STATUS_SUCCESS;
}

IRP_MJ_SHUTDOWN

NTSTATUS ShutdownCamel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
  WriteEvent(MSG_SHUTDOWN,DeviceObject,NULL);
  IoCompleteRequest(Irp, IO_NO_INCREMENT);
  return STATUS_SUCCESS;
}

IRP_MJ_DEVICE_CONTROL

NTSTATUS IoCtlCamel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
  WriteEvent(MSG_IOCTL,DeviceObject,NULL);
  IoCompleteRequest(Irp, IO_NO_INCREMENT);
  return STATUS_SUCCESS;
}

The unload function

VOID CmlUnload (IN PDRIVER_OBJECT  DriverObject) {
  UNICODE_STRING linkString;

  WriteEvent(MSG_DRIVERUNLOAD, DriverObject, NULL);
  ExFreePool(driverExtension);
  RtlInitUnicodeString (&linkString, L"\\DosDevices\\Kamel");
  IoDeleteSymbolicLink (&linkString);
  IoDeleteDevice(DriverObject->DeviceObject);
}

Writing Events from a Device Driver

It is possible to write strings from the driver into the system event box (which then can be viewed with the event viewer (eventvwr.exe). It is not straight forward however and the following steps must each be done.

The Message File

First, a message file must be created, having the suffix .mc, that contains each possible string you want to output and also assignes a unique id to these strings. A sample is given here:
MessageID    = 1
Severity     = Informational
SymbolicName = MSG_DRIVER_ENTRY
Language     = English
Driver Entry
.
MessageID    = 2
Severity     = Informational
SymbolicName = MSG_CREATE
Language     = English
Create
.
Each Entry must be followed by a single dot on its own line. In this sample, the unique Id is associated with the symbolic name MSG_DRIVER_ENTRY and the String "Driver Entry". If you take a look at DriverEntry above, you'll see that I call WriteEvent with the symbolic name MSG_DRIVER_ENTRY.
The Message File then is to be compiled with the message compiler mc: mc KamelMsg.mc on the command line. This produces a file called MessageFile.rc. KamelMsg.rc must be included in the sources file. It also creates the file KamelMsg.h which must be included to have the constants.
This is still not sufficient. Also a string entry must be created in the Registry under HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\System\<driverName>\EventMessageFile. The string must point to the .dll or .sys into which the messages were compiled, in our case: %SystemRoot%\System32\Drivers\Kamel.sys

WriteEvent

BOOLEAN WriteEvent(IN NTSTATUS ErrorCode , IN PVOID IoObject,IN PIRP Irp) {
  PIO_ERROR_LOG_PACKET Packet;
  PIO_STACK_LOCATION IrpStack;
  PWCHAR pInsertionString;
  STRING AnsiInsertString;
  UNICODE_STRING UniInsertString;

  UCHAR PacketSize;

  PacketSize = sizeof(IO_ERROR_LOG_PACKET);

  Packet = IoAllocateErrorLogEntry(IoObject,PacketSize);
  if (Packet == NULL) return FALSE;

  Packet->ErrorCode         = ErrorCode;
  Packet->UniqueErrorValue  = 0,
  Packet->RetryCount        = 0;
  Packet->SequenceNumber    = 0;
  Packet->IoControlCode     = 0;
  Packet->DumpDataSize      = 0;
  
  if (Irp!=NULL) {
     IrpStack=IoGetCurrentIrpStackLocation(Irp);
     Packet->MajorFunctionCode = IrpStack->MajorFunction;
     Packet->FinalStatus = Irp->IoStatus.Status;
  } 
  else {
     Packet->MajorFunctionCode = 0;
     Packet->FinalStatus       = 0;
  }

  IoWriteErrorLogEntry(Packet);
  return TRUE;
}

WriteEvent.h

BOOLEAN WriteEvent(IN NTSTATUS ErrorCode , IN PVOID IoObject,IN PIRP Irp); 
#pragma alloc_text(PAGE, WriteEvent)

Entries in the registry

The driver must be registred with the registry: Create a this key HKLM\System\CurrentControlSet\Services\<driverName> and add the following keys: ErrorControl, Group, Start, Tag and Type.

Feedback

# re: Writing a device driver for Windows  回復  更多評論   

2009-10-30 15:20 by 溪流
mark. thx.
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美国产视频日韩| 亚洲一级二级| 亚洲一区在线直播| 91久久中文字幕| 午夜精品一区二区三区四区| 亚洲精品一区二区三区婷婷月| 午夜视频在线观看一区二区| 在线综合欧美| 免费在线观看一区二区| 欧美影院视频| 欧美三级午夜理伦三级中视频| 免费成人av资源网| 国产一区二区福利| 亚洲性人人天天夜夜摸| 日韩亚洲欧美一区二区三区| 久久亚洲综合| 久久尤物视频| 国产综合18久久久久久| 亚洲欧美一区二区三区久久| 午夜亚洲性色视频| 国产精品日韩欧美一区| 一区二区欧美视频| 亚洲视频999| 欧美日韩成人在线| 亚洲精品视频在线| 亚洲美女视频网| 欧美精品高清视频| 91久久午夜| 一本色道久久综合狠狠躁篇怎么玩 | 日韩午夜精品| 一本大道久久精品懂色aⅴ| 美日韩精品视频| 亚洲国产mv| 99re视频这里只有精品| 欧美国产第二页| 亚洲精品国产精品国自产观看| 亚洲精品国偷自产在线99热| 欧美国产日韩视频| 日韩视频免费观看高清在线视频| 一本色道久久综合一区| 国产精品扒开腿做爽爽爽视频| 亚洲视频欧美视频| 久久国产精品一区二区| 好看的日韩av电影| 欧美a级片一区| 在线亚洲欧美视频| 久久精品视频网| 在线免费观看日本欧美| 欧美福利视频一区| 一本久道久久综合狠狠爱| 亚洲欧美成人网| 国产在线欧美日韩| 嫩草国产精品入口| 在线视频亚洲欧美| 免费观看在线综合色| 99热这里只有成人精品国产| 国产精品美女视频网站| 久久九九电影| 亚洲美女视频在线观看| 久久久久国产精品www| 亚洲人成人一区二区在线观看| 国产精品成人国产乱一区| 久久国产婷婷国产香蕉| 亚洲激情专区| 久久久噜噜噜久久中文字幕色伊伊| 亚洲激情成人| 国产精品视频久久久| 猛男gaygay欧美视频| 亚洲女人天堂成人av在线| 美女免费视频一区| 亚洲欧美日本国产有色| 亚洲黄色成人久久久| 国产女精品视频网站免费| 女生裸体视频一区二区三区| 亚洲欧美一区二区精品久久久| 亚洲国产mv| 久久美女性网| 午夜精品亚洲| 亚洲最新中文字幕| 激情久久久久久| 国产精品久久久久久超碰| 欧美大片91| 久久久久久尹人网香蕉| 午夜激情综合网| 亚洲免费电影在线观看| 欧美高清视频在线播放| 久久久久久久久伊人| 亚洲在线视频| 洋洋av久久久久久久一区| 亚洲国产精品成人综合色在线婷婷| 国产精品美腿一区在线看| 欧美破处大片在线视频| 免费观看久久久4p| 久久久久国色av免费看影院| 亚洲欧美日韩综合| 亚洲一区二区三区四区在线观看 | 亚洲大胆女人| 免费在线播放第一区高清av| 欧美一区二区三区免费在线看 | 午夜精品久久久久| 亚洲午夜久久久久久久久电影网| 亚洲黄色天堂| 欧美激情一区二区三区蜜桃视频| 久久久蜜桃精品| 欧美中文字幕在线视频| 欧美中文日韩| 欧美一区二区三区视频免费| 亚洲女性喷水在线观看一区| 亚洲少妇自拍| 亚洲午夜一区| 午夜激情一区| 欧美一区二区三区视频在线| 午夜亚洲性色福利视频| 欧美亚洲一区三区| 久久精品人人做人人爽| 欧美在线观看网址综合| 久久久999精品免费| 久久免费99精品久久久久久| 久久久久久午夜| 老司机精品视频一区二区三区| 久久一区激情| 欧美激情1区2区| 亚洲人成亚洲人成在线观看图片 | 国产精品亚洲美女av网站| 国产精品嫩草99a| 国产精一区二区三区| 国内精品一区二区三区| 亚洲承认在线| 亚洲精品久久久久久久久久久久| 亚洲精品中文字幕有码专区| 一本色道久久综合亚洲精品高清 | 国产精品99久久久久久www| 在线视频精品| 香蕉国产精品偷在线观看不卡| 先锋影音国产精品| 久久综合一区二区| 亚洲国产美女精品久久久久∴| 日韩午夜激情av| 欧美亚洲一级| 欧美.www| 欧美体内she精视频在线观看| 国产美女精品人人做人人爽| 好吊视频一区二区三区四区| 亚洲每日更新| 欧美一区精品| 欧美激情久久久久| 亚洲小视频在线| 巨胸喷奶水www久久久免费动漫| 欧美日韩在线播放一区二区| 国语自产精品视频在线看抢先版结局 | 噜噜噜久久亚洲精品国产品小说| 欧美伦理91| 国产亚洲精品久久久久婷婷瑜伽| 亚洲欧洲日韩在线| 欧美制服丝袜第一页| 亚洲全黄一级网站| 久久精品日产第一区二区| 欧美精品粉嫩高潮一区二区| 国内精品美女av在线播放| 9l国产精品久久久久麻豆| 久久久久久久欧美精品| 9色国产精品| 欧美+日本+国产+在线a∨观看| 欧美午夜一区| 最新精品在线| 久久手机免费观看| 在线视频精品一| 免费成人在线观看视频| 国产欧美日韩高清| 一区二区三区视频免费在线观看 | 久久琪琪电影院| 宅男精品视频| 欧美精品三级在线观看| 精品动漫3d一区二区三区| 亚洲一区二区久久| 亚洲国产婷婷| 男女精品视频| 国内久久精品| 欧美一区深夜视频| 一区二区三区|亚洲午夜| 欧美大胆成人| 最近中文字幕日韩精品| 免费中文日韩| 欧美一区二区三区啪啪| 国产精品免费视频xxxx| 亚洲婷婷在线| 99精品热视频只有精品10| 欧美 日韩 国产一区二区在线视频 | 在线观看亚洲a| 欧美在线日韩| 亚洲欧美日韩在线不卡| 国产精品入口麻豆原神| 亚洲综合第一| 亚洲欧美成aⅴ人在线观看| 国产精品久久久久久久久免费桃花 | 国产午夜精品一区二区三区欧美| 亚洲欧美国产日韩中文字幕| 99热免费精品| 欧美日韩亚洲高清一区二区| 亚洲一区三区电影在线观看|