首页
社区
课程
招聘
[旧帖] [分享]几个内核结构1 0.00雪花
发表于: 2012-7-2 11:19 983

[旧帖] [分享]几个内核结构1 0.00雪花

2012-7-2 11:19
983
几个内核结构1(学习笔记...)
        看了张帆的《Windows驱动开发技术详解.pdf》大作,学到第7章,还是稀里糊涂的,这几天,想了又想,琢磨出点味道,把这几个东西分清,
1、DDK的开发环境
2、DDK的帮助文档
3、数据存储区域
4、C/C++语言范畴                                                                (有错误谢谢指正)

内核里不能用应用层win32的API,没有WinMain(main)函数,替换成了NTSTATUS DriverEntry () ,内核驱动程序总得要存储东西吧,常量可以用宏以及微软的字符串宏RtlInitUnicodeString(),即RtlXXXXX等,不用担心内存泄露的问题,因为根本没分配内存,那变量呢,就得放到栈(或堆,堆里另论)里,或参数里(其实也是在栈里),在栈里,那就是函数里,就是我们自己编的某个函数里,再就是头文件的某个结构里,可以随时供几个文件的函数通用,而结构有自己定义的,多半就用微软的,

当我们有问题,比如,不知道,DRIVER_OBJECT结构的具体形式,首先DDK的帮助文档索引DRIVER_OBJECT一下,发现只有几个成员,帮助文档称之为,Accessible Fields(允许访问域),具体 见图1,

而全部的成员要看结构体才行,在哪呢?可以找安装目录,搜DRIVER_OBJECT,在C:\WINDDK\2600目录下,列出一堆,其实DRIVER_OBJECT结构是在DDK安装目录\2600\inc\ddk\w2k里定义的,被定义成
typedef struct _DRIVER_OBJECT{
......//(这里是一堆的成员)
}DRIVER_OBJECT;
typedef struct _DRIVER_OBJECT *PDRIVER_OBJECT; // ntndis
_DRIVER_OBJECT 就是 DRIVER_OBJECT(前有下划线是微软的DDK特有定义的结构)
注意,PDRIVER_OBJECT是指向DRIVER_OBJECT的指针。

以下几个结构,看看吧
(画图用的是Excel,将就看吧,另传上Excel文档,谁觉的不好,可以改成更好的,齐分享)
其中_DRIVER_OBJECT  、_DEVICE_OBJECT、_DRIVER_EXTENSION

_DRIVER_OBJECT(DRIVER_OBJECT) 结构的子域 PDEVICE_OBJECT DeviceObject; 是指向_DEVICE_OBJECT(DEVICE_OBJECT)整个结构体的,而_DEVICE_OBJECT结构体的子域struct _DRIVER_OBJECT *DriverObject;是指回_DRIVER_OBJECT整个结构体的,如上图;_DEVICE_OBJECT结构体的子域PVOID DeviceExtension;是指向一个我们自己定义的结构体_DEVICE_EXTENSION整个的,
我们自己定义的这个结构体_DEVICE_EXTENSION要有几个我们自己定义的几个变量,象:PDEVICE_OBJECT指回_DEVICE_OBJECT结构的指针,还有设备名,符号链接名,等,NT型驱动,这几个是必需的,WDM型驱动要多个 下一个栈设备,以构成链表,
既然是我们自己定义的,那可以有我们自己定义的一些不为人知的变量,嘿嘿,起先我也到
在DDK安装目录\2600\inc\ddk\w2k里的ntddk.h内找,死活找不到,也不想一想,多数把它定义在驱动的头文件里,*.h;就说这些,明白了这些,就有方法了,剩下的就需要时间,把它捋顺了,
在DDK安装目录\2600\inc\ddk\w2k里的ntddk.h内定义的:
typedef struct _DRIVER_OBJECT {
    CSHORT Type;
    CSHORT Size;

    //
    // The following links all of the devices created by a single driver
    // together on a list, and the Flags word provides an extensible flag
    // location for driver objects.
    //

    PDEVICE_OBJECT DeviceObject;
    ULONG Flags;

    //
    // The following section describes where the driver is loaded.  The count
    // field is used to count the number of times the driver has had its
    // registered reinitialization routine invoked.
    //

    PVOID DriverStart;
    ULONG DriverSize;
    PVOID DriverSection;
    PDRIVER_EXTENSION DriverExtension;

    //
    // The driver name field is used by the error log thread
    // determine the name of the driver that an I/O request is/was bound.
    //

    UNICODE_STRING DriverName;

    //
    // The following section is for registry support.  Thise is a pointer
    // to the path to the hardware information in the registry
    //

    PUNICODE_STRING HardwareDatabase;

    //
    // The following section contains the optional pointer to an array of
    // alternate entry points to a driver for "fast I/O" support.  Fast I/O
    // is performed by invoking the driver routine directly with separate
    // parameters, rather than using the standard IRP call mechanism.  Note
    // that these functions may only be used for synchronous I/O, and when
    // the file is cached.
    //

    PFAST_IO_DISPATCH FastIoDispatch;

    //
    // The following section describes the entry points to this particular
    // driver.  Note that the major function dispatch table must be the last
    // field in the object so that it remains extensible.
    //

    PDRIVER_INITIALIZE DriverInit;
    PDRIVER_STARTIO DriverStartIo;
    PDRIVER_UNLOAD DriverUnload;
    PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];

} DRIVER_OBJECT;
typedef struct _DRIVER_OBJECT *PDRIVER_OBJECT; // ntndis
===========================================
在DDK安装目录\2600\inc\ddk\w2k里的ntddk.h内定义的:
typedef struct _DEVICE_OBJECT {
    CSHORT Type;
    USHORT Size;
    LONG ReferenceCount;
    struct _DRIVER_OBJECT *DriverObject;
    struct _DEVICE_OBJECT *NextDevice;
    struct _DEVICE_OBJECT *AttachedDevice;
    struct _IRP *CurrentIrp;
    PIO_TIMER Timer;
    ULONG Flags;                                // See above:  DO_...
    ULONG Characteristics;                      // See ntioapi:  FILE_...
    PVPB Vpb;
    PVOID DeviceExtension;
    DEVICE_TYPE DeviceType;
    CCHAR StackSize;
    union {
        LIST_ENTRY ListEntry;
        WAIT_CONTEXT_BLOCK Wcb;
    } Queue;
    ULONG AlignmentRequirement;
    KDEVICE_QUEUE DeviceQueue;
    KDPC Dpc;

    //
    //  The following field is for exclusive use by the filesystem to keep
    //  track of the number of Fsp threads currently using the device
    //

    ULONG ActiveThreadCount;
    PSECURITY_DESCRIPTOR SecurityDescriptor;
    KEVENT DeviceLock;

    USHORT SectorSize;
    USHORT Spare1;

    struct _DEVOBJ_EXTENSION  *DeviceObjectExtension;
    PVOID  Reserved;
} DEVICE_OBJECT;
typedef struct _DEVICE_OBJECT *PDEVICE_OBJECT; // ntndis

============================================
在DDK安装目录\2600\inc\ddk\w2k里的ntddk.h内定义的:

typedef struct _DRIVER_EXTENSION {

    //
    // Back pointer to Driver Object
    //

    struct _DRIVER_OBJECT *DriverObject;

    //
    // The AddDevice entry point is called by the Plug & Play manager
    // to inform the driver when a new device instance arrives that this
    // driver must control.
    //

    PDRIVER_ADD_DEVICE AddDevice;

    //
    // The count field is used to count the number of times the driver has
    // had its registered reinitialization routine invoked.
    //

    ULONG Count;

    //
    // The service name field is used by the pnp manager to determine
    // where the driver related info is stored in the registry.
    //

    UNICODE_STRING ServiceKeyName;

    //
    // Note: any new shared fields get added here.
    //

} DRIVER_EXTENSION, *PDRIVER_EXTENSION;

=======================================

此外,还有IRP结构巨大,不列了,也是
typedef struct _IRP {
...
}IRP, *PIRP; 这种形式的,

还在继续学习中,错误请指出,非常感谢!

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

上传的附件:
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//