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

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.¢% 閱讀(594) 評論(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>
            欧美一区二区黄色| 在线欧美三区| 午夜视频一区在线观看| 亚洲永久免费观看| 国产主播一区| 男人的天堂亚洲在线| 免费看精品久久片| 夜夜爽www精品| 中文网丁香综合网| 国产亚洲精品aa午夜观看| 久久久夜夜夜| 欧美国产乱视频| 亚洲一区二区三区免费观看| 午夜视频久久久| 亚洲黄色毛片| 99精品热视频只有精品10| 国产嫩草影院久久久久 | 欧美精品播放| 亚洲欧美日韩另类| 久久久精品2019中文字幕神马| 亚洲清纯自拍| 午夜日韩激情| 一区二区国产在线观看| 欧美自拍偷拍| 亚洲午夜电影| 美女主播精品视频一二三四| 亚洲欧美第一页| 久久久伊人欧美| 制服丝袜亚洲播放| 欧美资源在线观看| 宅男噜噜噜66一区二区| 欧美一区=区| 夜夜嗨av一区二区三区中文字幕 | 久久精品一区二区三区不卡牛牛 | 欧美精品一区二区久久婷婷| 久久gogo国模啪啪人体图| 欧美国产三级| 欧美不卡福利| 国内精品**久久毛片app| 一区二区91| 一本色道久久加勒比88综合| 久久久久久久综合| 欧美综合激情网| 国产精品多人| 亚洲激情在线视频| 国产综合久久久久影院| 亚洲天堂网在线观看| 亚洲伦理在线观看| 蜜桃av噜噜一区| 久久久999精品免费| 国产精品日韩在线播放| 日韩午夜在线电影| 夜夜嗨av一区二区三区网站四季av| 久久久久久久久综合| 久久夜色精品| 国产在线播放一区二区三区| 亚洲视频在线观看免费| 亚洲午夜在线| 国产精品v片在线观看不卡| 夜夜爽www精品| 亚洲男女自偷自拍| 国产精品一区一区三区| 欧美亚洲综合另类| 久久久久久久久久久一区| 黄色资源网久久资源365| 久久大逼视频| 欧美jizzhd精品欧美巨大免费| 韩国欧美一区| 免费久久精品视频| 91久久精品国产91性色tv| 日韩一级大片在线| 国产精品国产成人国产三级| 亚洲一级网站| 久久免费视频在线| 亚洲黄网站在线观看| 欧美美女bbbb| 日韩午夜av| 欧美在线一区二区三区| 有码中文亚洲精品| 欧美福利视频| 亚洲一区二区在线看| 欧美一区激情视频在线观看| 国产伊人精品| 欧美成人小视频| 在线综合亚洲欧美在线视频| 亚洲欧美日韩中文在线制服| 国产一区二区三区视频在线观看| 久久精品视频免费观看| 亚洲国产精品久久91精品| 亚洲色在线视频| 国产女人精品视频| 免费成人av| 亚洲视频免费观看| 久久久久9999亚洲精品| 亚洲黄色一区| 国产亚洲精品久久久久久| 欧美大片一区二区| 亚洲欧美网站| 亚洲日本va午夜在线影院| 久久精品99久久香蕉国产色戒 | 黑人巨大精品欧美一区二区| 欧美va天堂| 亚洲欧美日韩第一区| 亚洲高清影视| 亚洲欧美区自拍先锋| 1024成人| 国产欧美va欧美va香蕉在| 欧美大片va欧美在线播放| 亚洲欧美视频在线观看| 亚洲精品久久久久中文字幕欢迎你 | 一本久道久久综合狠狠爱| 国产三级欧美三级日产三级99| 欧美国产日韩xxxxx| 久久精品日韩欧美| 亚洲视频欧美视频| 亚洲国产成人久久综合一区| 久久高清国产| 亚洲免费在线精品一区| 亚洲区一区二| 国产综合自拍| 国产女主播在线一区二区| 欧美视频亚洲视频| 欧美福利小视频| 久久久人成影片一区二区三区| 亚洲一区三区视频在线观看| 亚洲激情电影在线| 欧美激情 亚洲a∨综合| 噜噜噜在线观看免费视频日韩 | 在线视频成人| 黄色成人小视频| 狠狠v欧美v日韩v亚洲ⅴ| 国产日韩在线看片| 国产精品性做久久久久久| 国产精品入口夜色视频大尺度| 国产精品成人av性教育| 欧美久久久久久久久久| 欧美日韩www| 欧美日韩国产小视频| 欧美精品麻豆| 欧美日韩福利在线观看| 欧美日韩亚洲一区二| 国产精品99一区二区| 欧美日韩国产美女| 欧美性做爰猛烈叫床潮| 国产精品一区=区| 国产一区二区三区精品欧美日韩一区二区三区 | 亚洲精品麻豆| 在线午夜精品自拍| 亚洲欧美日韩精品久久亚洲区| 午夜精品久久久久久久久久久久| 午夜欧美大尺度福利影院在线看| 久久成人精品电影| 乱码第一页成人| 欧美另类专区| 国产精品影音先锋| 激情欧美日韩一区| 亚洲欧洲一区二区三区在线观看| 亚洲美女电影在线| 亚洲女ⅴideoshd黑人| 久久久久在线观看| 欧美激情精品久久久六区热门 | 一区二区三区视频在线观看 | 一本大道久久a久久精二百| 亚洲中字黄色| 久久亚洲精品视频| 欧美视频在线观看一区二区| 国产日韩一区二区三区在线| 亚洲成人自拍视频| 亚洲影院免费观看| 免费试看一区| 一区二区高清在线| 久久久久国色av免费观看性色| 欧美精品在线极品| 国产一区二区三区丝袜| 亚洲精品一区二区三区在线观看| 亚洲男人av电影| 亚洲成人中文| 夜夜嗨av一区二区三区免费区 | 久久视频在线视频| 亚洲精品在线观看视频| 国产精品一区毛片| 亚洲经典在线| 久久久97精品| 女生裸体视频一区二区三区| 在线视频日本亚洲性| 久久夜精品va视频免费观看| 国产精品免费看片| 亚洲免费黄色| 欧美成年人网| 欧美在线日韩| 国产精品久久久久久久久久久久久| 精品88久久久久88久久久| 亚洲免费在线精品一区| 亚洲激情精品| 米奇777在线欧美播放| 狠狠色丁香婷婷综合久久片| 午夜精品久久久久久久蜜桃app| 亚洲国产日韩欧美| 美女黄色成人网| 激情文学一区|