Windows编写driver

1. 编译

Pspeek.cpp

#include <ntddk.h>


#define DANIEL_LIST_PROCESS 0x8001

PDRIVER_OBJECT daniel_DriverObject;
PDEVICE_OBJECT daniel_DeviceObject;


NTSTATUS daniel_DispatchCreate(
	__in PDEVICE_OBJECT DeviceObject,
	__in PIRP Irp
	)
{
	NTSTATUS status = STATUS_SUCCESS;
	PIO_STACK_LOCATION stackLocation;
	PIO_SECURITY_CONTEXT securityContext;

	stackLocation = IoGetCurrentIrpStackLocation(Irp);
	securityContext = stackLocation->Parameters.Create.SecurityContext;

	DbgPrint("###############\n");
	DbgPrint("Daniel PsPeek daniel_DispatchCreate\n");
	DbgPrint("###############\n");

	Irp->IoStatus.Status = status;
	Irp->IoStatus.Information = 0;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);

	return status;
}

NTSTATUS KphDispatchDeviceControl(
	__in PDEVICE_OBJECT DeviceObject,
	__in PIRP Irp
	)
{
	NTSTATUS status;
	PIO_STACK_LOCATION stackLocation;
	PVOID originalInput;
	ULONG inputLength;
	ULONG ioControlCode;
	KPROCESSOR_MODE accessMode;
	UCHAR capturedInput[16 * sizeof(ULONG_PTR)];
	PVOID capturedInputPointer;

	stackLocation = IoGetCurrentIrpStackLocation(Irp);
	originalInput = stackLocation->Parameters.DeviceIoControl.Type3InputBuffer;
	inputLength = stackLocation->Parameters.DeviceIoControl.InputBufferLength;
	ioControlCode = stackLocation->Parameters.DeviceIoControl.IoControlCode;
	accessMode = Irp->RequestorMode;


	// Probe and capture the input buffer.
	if (accessMode != KernelMode)
	{
		__try
		{
			ProbeForRead(originalInput, inputLength, sizeof(UCHAR));
			memcpy(capturedInput, originalInput, inputLength);
		}
		__except (EXCEPTION_EXECUTE_HANDLER)
		{
			status = GetExceptionCode();
			goto ControlEnd;
		}
	}
	else
	{
		memcpy(capturedInput, originalInput, inputLength);
	}

	capturedInputPointer = capturedInput; // avoid casting below

	switch (ioControlCode)
	{
	case DANIEL_LIST_PROCESS:
		{
			status = STATUS_SUCCESS;
		}
		break;
	default:
		status = STATUS_INVALID_DEVICE_REQUEST;
		break;
	}

ControlEnd:
	Irp->IoStatus.Status = status;
	Irp->IoStatus.Information = 0;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);

	return status;
}

VOID daniel_DriverUnload(
	__in PDRIVER_OBJECT DriverObject
	)
{
	PAGED_CODE();

	IoDeleteDevice(daniel_DeviceObject);
}


extern "C" NTSTATUS DriverEntry(
	__in PDRIVER_OBJECT DriverObject,
	__in PUNICODE_STRING RegistryPath)
{
	NTSTATUS status;
	UNICODE_STRING deviceName;
	PDEVICE_OBJECT deviceObject;

	PAGED_CODE();

	DbgPrint("###############\n");
	DbgPrint("Daniel PsPeek DriverEntry\n");
	

	DbgPrint("Current Pid: %d\n", PsGetCurrentProcessId());
	DbgPrint("###############\n");

	daniel_DriverObject = DriverObject;

	// Create the device.

	RtlInitUnicodeString(&deviceName, L"\\Device\\DanielPsPeekDriver");

	status = IoCreateDevice(
		DriverObject,
		0,
		&deviceName,
		FILE_DEVICE_UNKNOWN,
		FILE_DEVICE_SECURE_OPEN,
		FALSE,
		&deviceObject
		);

	if (!NT_SUCCESS(status))
		return status;

	daniel_DeviceObject = deviceObject;

	// Set up I/O.

	DriverObject->MajorFunction[IRP_MJ_CREATE] = daniel_DispatchCreate;
	DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = KphDispatchDeviceControl;
	DriverObject->DriverUnload = daniel_DriverUnload;

	deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

	return status;
}

  

  

 

sources

TARGETNAME=Pspeek
TARGETPATH=obj
TARGETTYPE=DRIVER

SOURCES=Pspeek.cpp

  

 mk.bat

set setenv_script=D:\WinDDK\7600.16385.1\bin\setenv.bat
set ddk_path=D:\WinDDK\7600.16385.1\
set config=chk
set platform=x86
set os=WXP

%setenv_script% %ddk_path% %config% %platform% %os% && H: && cd %cd% && build

  

2. 加载

ld.bat

sc stop Pspeek
sc delete Pspeek
copy /y "F:\pspeek.sys" "C:\WINDOWS\system32\pspeek.sys"
sc create Pspeek binPath= "C:\WINDOWS\system32\pspeek.sys" type= kernel start= auto error= ignore DisplayName= "Daniel Process Peek Driver"
sc start Pspeek

  

在Windows 7下,需要使用"管理员权限"才能执行上述脚本。

3. 枚举进程列表

void GatherProcessListByEPROCESS()
{
	HANDLE pid = PsGetCurrentProcessId();
	DbgPrint("Current Pid: %d\n", pid);
	PEPROCESS eprocess;
	PsLookupProcessByProcessId(pid, &eprocess);

	DbgPrint("_EPROCESS: 0x%08x\n", eprocess);

	_LIST_ENTRY active_process_node = {0,0};
	memcpy(&active_process_node, (CHAR*)eprocess + 0x88, 8);

	DbgPrint("Active Process List Node: [0x%08x, 0x%08x]\n", active_process_node.Blink, active_process_node.Flink);

	DbgPrint("VirtualSize: 0x%08x \n", *(ULONG*)((CHAR*)eprocess + 0xb0));
}

  

 上面代码与WinDbg的验证一致,因此Windows下获取内核相关的数据与Linux并无太大差别。

 

 

void PProcess(char* eprocess)
{
	DbgPrint("%16s: 0x%08x\n", "_EPROCESS", eprocess);
	DbgPrint("%16s: %s\n", "ImageName", eprocess + 0x174);

	DbgPrint("\n");
}

void GatherProcessListByEPROCESS()
{
	HANDLE pid = PsGetCurrentProcessId();
	DbgPrint("Current Pid: %d\n", pid);

	PEPROCESS eprocess;
	PsLookupProcessByProcessId(pid, &eprocess);

	_LIST_ENTRY active_process_node = {0,0};
	memcpy(&active_process_node, (CHAR*)eprocess + 0x88, 8);

	_LIST_ENTRY* head = active_process_node.Flink;

	PProcess((char*)eprocess);
	for (_LIST_ENTRY* cur=head;
		cur->Flink != head;
		cur = cur->Flink)
	{
		PProcess((char*)cur - 0x88);
	}
}

  

  

转载于:https://www.cnblogs.com/long123king/p/3860830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Windows Application Driver是一个用于测试Windows桌面应用程序的驱动软件。它提供了一种使用Python编写自动化测试脚本来测试和控制Windows桌面应用程序的方法。 Python Windows Application DriverWindows操作系统上的一个WebDriver。它通过实现Windows屏幕上的UI Automation接口来和Windows桌面应用程序进行交互。通过它,我们可以使用Python编写自动化测试脚本来模拟用户与Windows应用程序的交互行为,比如点击按钮、输入文字等。 Python Windows Application Driver的安装非常简单。首先,我们需要安装Python,并确保已安装pip包管理器。然后,通过运行pip install windows-app-driver命令来安装Python Windows Application Driver。 一旦安装完成,我们就可以使用Python编写测试脚本了。首先,我们需要导入pywinauto库来实现Windows应用程序的自动化操作。然后,我们可以使用pywinauto库中的一些方法来定位和操作Windows应用程序的各个元素,比如查找按钮、输入框等。 使用Python Windows Application Driver进行自动化测试有很多好处。首先,它允许我们使用Python这个流行的编程语言来编写测试脚本,这样可以使用大量的Python库来辅助测试。其次,它提供了一种简单且灵活的方式来与Windows桌面应用程序进行交互,使得自动化测试变得更加容易和高效。 总之,Python Windows Application Driver是一个功能强大且易于使用的工具,可以帮助我们在Windows操作系统上进行桌面应用程序的自动化测试。它为Python开发者提供了一种简单且灵活的方式来与Windows应用程序进行交互,从而提高测试效率和质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值