内核编程工具集安装:
1:下载安装WDK
2:编写程序(DrvierEntry必选),编译
3:利用srvinstw.exe等工具安装服务,用net start启动服务
4:使用WinDbg,利用串口和虚拟机通信,将目标系统设置为调试模式启动,这样就可以调试系统了。需要设置WinDbg的内核符号表路径,这样可以实现源码级内核调试。
特殊宏:
IN,OUT:输入输出
__in_bcount(Length):输入,并且长度为Length
#pragma alloc_text(INIT, DriverEntry):指定代码位置
设备绑定API:
NTSTATUS IoAttachDevice(
IN PDEVICE_OBJECT SourceDevice,
IN PUNICODE_STRING TargetDevice,
OUT PDEVICE_OBJECT *AttachedDevice
);
NTSTATUS IoAttachDeviceToDeviceStackSafe(
IN PDEVICE_OBJECT SourceDevice,
IN PDEVICE_OBJECT TargetDevice,
IN OUT PDEVICE_OBJECT *AttachedToDeviceObject
);
PDEVICE_OBJECT IoAttachDeviceToDeviceStack(
IN PDEVICE_OBJECT SourceDevice,
IN PDEVICE_OBJECT TargetDevice
);
生成设备:
NTSTATUS IoCreateDevice(
IN PDRIVER_OBJECT DriverObject,
IN ULONG DeviceExtensionSize,
IN PUNICODE_STRING DeviceName,
IN DEVICE_TYPE DeviceType,
IN ULONG DeviceCharacteristics,
IN BOOLEAN Exclusive,
OUT PDEVICE_OBJECT *DeviceObject
);
从名字获取设备对象:
NTSTATUS IoGetDeviceObjectPointer(
IN PUNICODE_STRING ObjectName,
IN ACCESS_MASK DesiredAccess,
OUT PFILE_OBJECT *FileObject,
OUT PDEVICE_OBJECT *DeviceObject
);
键盘按键信息是由csrss.exe!RawInputThread线程负责提取后发送给当前窗体的输入队列的。
PS/2键盘的设备栈是Kbdclass/i8042prt/ACPI
根据名字获取对象指针:
NTSTATUS ObReferenceObjectByName(
PUNICODE_STRING ObjectName,
ULONG Attributes,
PACCESS_STATE AccessState,
ACCESS_MASK DesiredAccess,
POBJECT_TYPE ObjectType,
KPROCESSOR_MODE AccessMode,
PVOID ParseContext,
PVOID *Object
);
过滤键盘操作可以使用过滤Kbdclass驱动。首先获取驱动对象,然后遍历器所有设备对象,将其绑定后过滤。
还可以通过Hook类驱动对象的分发函数实现。
还可以通过修改端口驱动调用的类驱动函数地址。
还可以通过修改键盘中断的IDT。
还可以通过IOAPIC重定向键盘中断。
文件访问的驱动结构:文件系统驱动/卷驱动/磁盘驱动,文件系统设备包含一个控制设备和一个卷设备。
创建通信端口:
NTSTATUS FltCreateCommunicationPort(
IN PFLT_FILTER Filter,
OUT PFLT_PORT *ServerPort,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN PVOID ServerPortCookie OPTIONAL,
IN PFLT_CONNECT_NOTIFY ConnectNotifyCallback,
IN PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback,
IN PFLT_MESSAGE_NOTIFY MessageNotifyCallback,
IN LONG MaxConnections
);
连接通信端口:
HRESULT WINAPI FilterConnectCommunicationPort(
IN LPCWSTR lpPortName,
IN DWORD dwOptions,
IN LPVOID lpContext OPTIONAL,
IN DWORD dwSizeOfContext,
IN LPSECURITY_ATTRIBUTES lpSecurityAttributes OPTIONAL,
OUT HANDLE *hPort
);
通过通信端口发送数据:
HRESULT WINAPI FilterSendMessage(
__in HANDLE hPort,
__in_bcount LPVOID lpInBuffer,
__in DWORD dwInBufferSize,
__out_bcount_part_opt LPVOID lpOutBuffer,
__in DWORD dwOutBufferSize,
__out LPDWORD lpBytesReturned
);
网络驱动结构:TDI接口驱动/NDIS网络驱动
NDIS网络驱动结构:协议驱动/中间层驱动/小端口驱动