⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysource.c

📁 利用C++工具进行编程,NDIS的PASSTHRU层的驱动程序,是非常实用的程序.
💻 C
字号:
//MySource.c
#include"precomp.h"

/***************Function Declaration(Internal)***************/
//Handle CreateFile().
NTSTATUS MyDeviceCreate (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
//Handle CloseHandle().
NTSTATUS MyDeviceClose (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
//Handle ReadFile().
NTSTATUS MyDeviceRead (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
//Handle WriteFile().
NTSTATUS MyDeviceWrite (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
//Handle DeviceIoControl().
NTSTATUS MyDeviceControl (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
//Default Handle.
NTSTATUS MyDeviceDispatch (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
 
/*****************Variables Defination(Internal)**************/
static PDEVICE_OBJECT          guiDevice;//the hooked device to gui.
static WCHAR                   deviceNameBuffer[]  = L"\\Device\\MyTest";
static UNICODE_STRING          deviceNameUnicodeString;
static WCHAR                   deviceLinkBuffer[]  = L"\\DosDevices\\MyTest";
static UNICODE_STRING          deviceLinkUnicodeString;

static int iCreateDeviceFlag;//guiDevice create flag.
static KSPIN_LOCK CreateDeviceLock;
static KIRQL CreateDeviceIrql;

static NDIS_HANDLE  MyNdisDeviceHandle;
static PDRIVER_DISPATCH  MyMajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1];
        
/*******************Function Bodies***********************/
//Call In DriverEntry().
//Hook a GUI device.
//If success,return 0; else return 1.
int MyInit(IN NDIS_HANDLE  NdisWrapperHandle)
{
	NTSTATUS                ntStatus;
        
	ULONG                   i;

    DbgPrint ("MyTest--Device: entering DriverEntry\n");

	//Set Create Device Flag .
	KeInitializeSpinLock(&CreateDeviceLock);
	
	KeAcquireSpinLock(&CreateDeviceLock,&CreateDeviceIrql);
	iCreateDeviceFlag=0;
	KeReleaseSpinLock(&CreateDeviceLock,CreateDeviceIrql);

	//Filter Flag.
	KeInitializeSpinLock(&FilterLock);
	
	//Filter Item Spin Lock's Init.
	KeInitializeSpinLock(&FilterItemLock);

	// Spin Lock's Init.
	KeInitializeSpinLock(&SavedPacketEntryLock);


	//Initialize the Filter Entry.
	if( InitFilterItem() )
	{
		//Fail.
		return 1;
	}

	//Initialize the buffer of saved packet.
/*	if( InitPacketBuffer() )
	{
		//Fail.
		FreeFilterItem();

		return 1;
	}
  */  
//Create the gui Device.
	RtlInitUnicodeString (&deviceNameUnicodeString,
                          deviceNameBuffer );

    RtlInitUnicodeString (&deviceLinkUnicodeString,
				   		deviceLinkBuffer );

	//Init dispatch function.
	for(i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++ ) 
	{
		MyMajorFunction[i] = MyDeviceDispatch;
	}
	
	MyMajorFunction[IRP_MJ_CREATE]=MyDeviceCreate;
	MyMajorFunction[IRP_MJ_CLOSE]=MyDeviceClose;
	MyMajorFunction[IRP_MJ_DEVICE_CONTROL]=MyDeviceControl;
	MyMajorFunction[IRP_MJ_READ]=MyDeviceRead;
	MyMajorFunction[IRP_MJ_WRITE]=MyDeviceWrite;
	
	//Register Gui device.
	ntStatus=NdisMRegisterDevice(
				NdisWrapperHandle,
				&deviceNameUnicodeString,
				&deviceLinkUnicodeString,
				MyMajorFunction,
				&guiDevice,
				&MyNdisDeviceHandle
			);

    if(NT_SUCCESS(ntStatus)) 
	{
		DbgPrint("Create Device Success");
	
	//Create the gui Device Success!
		KeAcquireSpinLock(&CreateDeviceLock,&CreateDeviceIrql);
		iCreateDeviceFlag=1;
		KeReleaseSpinLock(&CreateDeviceLock,CreateDeviceIrql);
		
		return 0;
	}

//Fail to create GUI Device.
	DbgPrint("IOCreateDevice Fail!");

//	FreePacketBuffer();

	FreeFilterItem();
	
	return 1;
}

//Call In PtUnload().
void MyExit(void)
{
	if(iCreateDeviceFlag)
	{
		KeAcquireSpinLock(&FilterLock,&FilterIrql);
		iCreateDeviceFlag=0;
		KeReleaseSpinLock(&FilterLock,FilterIrql);
	
	//	FreePacketBuffer();
		
		FreeFilterItem();

		NdisMDeregisterDevice(MyNdisDeviceHandle);

	//Note: Error!!!	
		//RtlFreeUnicodeString(&deviceNameUnicodeString);
		//RtlFreeUnicodeString(&deviceLinkUnicodeString);
	}
}

/**************GUI Device Area****************/

//Default Handle.
NTSTATUS MyDeviceDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
/*	if(iCreateDeviceFlag)
	{
	
	}
*/	
	return STATUS_SUCCESS;
}

//Handle CreateFile().
NTSTATUS MyDeviceCreate(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
	if(iCreateDeviceFlag)
	{
		InitFilterItem();
//		ZapPacketBuffer();
	}

	return STATUS_SUCCESS;
}

//Handle CloseHandle().
NTSTATUS MyDeviceClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
	if(iCreateDeviceFlag)
	{
		FreeFilterItem();
//		ZapPacketBuffer();
	}

	return STATUS_SUCCESS;
}

//Handle ReadFile().
//Mainly Copy the packet data to gui.
NTSTATUS MyDeviceRead (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
	NTSTATUS retStatus=STATUS_SUCCESS;

	PIO_STACK_LOCATION tempIrpStack = IoGetCurrentIrpStackLocation(Irp);

	// Get call parameters
	ULONG tempReadLen = tempIrpStack->Parameters.Read.Length;


	// Check read file parameter.
	if(tempReadLen<0)
		retStatus=STATUS_INVALID_PARAMETER;
	else
	{
		//See whether have data to read.
		//Sometime I think the judge is danger:
		//		No access to PacketBufferLen!!
/*
		if( PacketBufferDataLen==0)
		{
			//No data to read.
			KeReleaseSpinLock(&PacketBufferLock,PacketBufferIrql);
			
			return STATUS_PENDING;
		}else
			ReadPacketBuffer(
				tempReadLen,
				&(Irp->IoStatus.Information),
				Irp->AssociatedIrp.SystemBuffer
			);
			*/
	}

	return retStatus;
}

//Handle WriteFile().
NTSTATUS MyDeviceWrite (IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
	//Now None Handle!

	return STATUS_SUCCESS;
}

//Handle DeviceIoControl().
NTSTATUS MyDeviceControl(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
{
	ULONG tempControlCode;
	PIO_STACK_LOCATION tempIrpStack;

	U8 *pTempFilterBuffer;
	GuiFilterItem *pTempGuiFilterItem;
	
	ULONG uTempInputBufferSize;
	ULONG uTempOutputBufferSize;
	ULONG *pTempOutputLength;

	U8 *pTempCmdParamBuffer;

	
	if(iCreateDeviceFlag)
	{
		tempIrpStack = IoGetCurrentIrpStackLocation(Irp);
		tempControlCode= tempIrpStack->Parameters.DeviceIoControl.IoControlCode;

//Filter.
		pTempGuiFilterItem=GetGuiFilterItem(Irp->AssociatedIrp.SystemBuffer);
		pTempFilterBuffer=(U8*)GetGuiFilterContext(Irp->AssociatedIrp.SystemBuffer);
		
//Cmd.
		uTempInputBufferSize = tempIrpStack->Parameters.DeviceIoControl.InputBufferLength;
		uTempOutputBufferSize = tempIrpStack->Parameters.DeviceIoControl.OutputBufferLength;

		pTempCmdParamBuffer=(U8 *)(Irp->AssociatedIrp.SystemBuffer);

		pTempOutputLength=&(Irp->IoStatus.Information);
		
		switch(tempControlCode)
		{
//Filter Item.
		case IOCTL_MYGUI_REINIT_FILTER:
			InitFilterItem();
			break;

		case IOCTL_MYGUI_ADD_FILTER:
			AddFilterItem(
				pTempGuiFilterItem->GuiFilterLen,
				pTempGuiFilterItem->GuiFilterType,
				pTempFilterBuffer
				);
			break;

		case IOCTL_MYGUI_DELETE_FILTER:
			DeleteFilterItem(
				pTempGuiFilterItem->GuiFilterLen,
				pTempGuiFilterItem->GuiFilterType,
				pTempFilterBuffer
				);
			break;
		

//Packet buffer.
		case IOCTL_MYGUI_ZERO_PACKET_BUFFER:
			ZapPacketBuffer();

			break;

		case IOCTL_MYGUI_RESIZE_PACKET_BUFFER:
			if( ReAllocatePacketBuffer( (int)( *((int*)pTempCmdParamBuffer) ) ) )
			{
				*((CmdRetFlag *)pTempCmdParamBuffer)=CmdRetFail;
			}else
			{
				*((CmdRetFlag *)pTempCmdParamBuffer)=CmdRetSuccess;
			}

			*pTempOutputLength=sizeof(CmdRetFlag);

			break;

		default:

			return STATUS_INVALID_DEVICE_REQUEST;
		}
	}

	return STATUS_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -