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

📄 comspy.c

📁 串口过滤驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
/*--         
Copyright (c) 1998, 1999  Microsoft Corporation

Module Name:

    ComSpy.c

Abstract:

Environment:

    Kernel mode only.

Notes:


--*/

#include "ComSpy.h"

const WCHAR NameBuffer[] = L"\\Device\\ComSpy";
const WCHAR DOSNameBuffer[] = L"\\DosDevices\\ComSpy";

#define FILE_DEVICE_COMPORT 0x0000f000


#define		DEV_EXT_ATTACHED								(0x00000001)

//filter device object
LIST_ENTRY g_data_lst;			//DATA		队列
LIST_ENTRY g_req_lst;			//读IRP  	队列
KSPIN_LOCK g_req_splock;		//读		同步

ULONG	   g_szCount = 0;	
ULONG	   g_bStartMon = 0;	


//KSPIN_LOCK	g_cancl_splock;

PVOID	gpEventObject = NULL;

PVOID SystemVirtualAddress = NULL ;

#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
//#pragma alloc_text (PAGE, ComSpy_Close)
//#pragma alloc_text (PAGE, ComSpy_Create)
//#pragma alloc_text (PAGE, ComSpy_Read)
//#pragma alloc_text (PAGE, ComSpy_Write)
//#pragma alloc_text (PAGE, ComSpy_Unload)
#endif


NTSTATUS
DriverEntry ( IN  PDRIVER_OBJECT  DriverObject, IN  PUNICODE_STRING RegistryPath )
{

//  UNREFERENCED_PARAMETER (RegistryPath);

   	NTSTATUS NtStatus = STATUS_SUCCESS;
    ULONG uiIndex = 0;
    
    PDEVICE_OBJECT pDeviceObject = NULL, pFilteredDevice = NULL;
    UNICODE_STRING usDeviceToFilter;

	PDEVICE_OBJECT			pTargetDeviceObject = NULL;
	PFILE_OBJECT			pTargetFileObject	= NULL;
	PDEVICE_EXTENSION		pDevExt;

	DbgPrint("DriverEntry Called \n");
	DbgPrint(("ComSpy.SYS: entering DriverEntry\n"));

	///////////////////////////////////////////////////////////////////
    for(uiIndex = 0; uiIndex < IRP_MJ_MAXIMUM_FUNCTION; uiIndex++)
         DriverObject->MajorFunction[uiIndex] = ComSpy_DispatchPassThrough;

    DriverObject->MajorFunction[IRP_MJ_CLOSE]             = ComSpy_Close;
    DriverObject->MajorFunction[IRP_MJ_CREATE]            = ComSpy_Create;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]    = ComSpy_IoCtl;
    DriverObject->MajorFunction[IRP_MJ_READ]              = ComSpy_Read;
    DriverObject->MajorFunction[IRP_MJ_WRITE]             = ComSpy_Write;
	DriverObject->MajorFunction[IRP_MJ_POWER]			  = ComSpy_Power; 
	DriverObject->MajorFunction[IRP_MJ_PNP]				  = ComSpy_PnP; 
    DriverObject->DriverUnload =  ComSpy_Unload;

	////////////////////////////////////////////////////////////
    RtlInitUnicodeString(&usDeviceToFilter, L"\\??\\COM1");
	
	NtStatus = IoGetDeviceObjectPointer(
			   IN	&usDeviceToFilter,
			   IN	FILE_ALL_ACCESS,
			   OUT	&pTargetFileObject,   
			   OUT	&pTargetDeviceObject
			   );
	if( !NT_SUCCESS(NtStatus) )
	{
		DbgPrint(("ComSpy.SYS:: Couldn't Get the Device Object\n"));
		pTargetFileObject	= NULL;
		pTargetDeviceObject = NULL;
		
		return( NtStatus );
	}

	DbgPrint("IoGetDeviceObjectPointer ok!\n");

	//////////////////////////////////////////////////////////////////////////

 	NtStatus = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), NULL, 
								pTargetDeviceObject->DeviceType, 
								pTargetDeviceObject->Characteristics, 
								FALSE, &pDeviceObject);

	if( !NT_SUCCESS(NtStatus) ) 
	{
        DbgPrint(("ComSpy.SYS: ComSpy failed to create device!\n"));

        RtlFreeUnicodeString( &usDeviceToFilter );

		ObDereferenceObject( pTargetFileObject );
		pTargetFileObject = NULL;

        return STATUS_SUCCESS;

	}
	
	pDevExt=ExAllocatePool(NonPagedPool, sizeof( PDEVICE_EXTENSION ) );
	(PDEVICE_EXTENSION )( pDeviceObject->DeviceExtension )= pDevExt  ;

	pDevExt->pFilterDeviceObject = pDeviceObject;
	pDevExt->TargetDeviceObject	= pTargetDeviceObject;

 	DbgPrint(("IoCreateDevice: Create Device \n"));

	NtStatus = IoAttachDeviceByPointer(pDeviceObject,pTargetDeviceObject);

	if( !NT_SUCCESS(NtStatus) )
	{
			DbgPrint(("ComSpy_Attach: Couldn't attach to COM Device Object\n"));

			IoDeleteDevice( pDeviceObject );
			pDeviceObject = NULL;
			
			ObDereferenceObject( pTargetFileObject );
			
			pTargetFileObject	= NULL;
			pTargetDeviceObject = NULL;

			return( NtStatus );
	}

 	DbgPrint(("IoAttachDeviceToDeviceStack: Attach Device OK \n"));

	///////////////////////////////////
	DbgPrint(("ComSpy.SYS: Attach Device\n"));

   	pDeviceObject->DeviceType = pTargetDeviceObject->DeviceType; 
	pDeviceObject->Characteristics = pTargetDeviceObject->Characteristics; 
	pDeviceObject->Flags |= (  ( DO_BUFFERED_IO ) ); 
  
	///////////////////////////////////////////////////////////////////

	DbgPrint(("ComSpy.SYS: Before Dereference TargetFileObject \n"));

	ObDereferenceObject( pTargetFileObject );
	
	pTargetFileObject = NULL;

	///////////////////////////////////////////////////////////////////

  	NtStatus=Add_IoControlDevice(DriverObject,RegistryPath);

	InitializeListHead( &g_data_lst );
	KeInitializeSpinLock( &g_req_splock );

//	KeInitializeSpinLock( &g_cancl_splock );
	

	DbgPrint(("ComSpy.SYS: Leaving DriverEntry\n"));

	return NtStatus;

}


VOID
ComSpy_Unload( IN PDRIVER_OBJECT DriverObject )
{
	BOOLEAN					NoRequestsOutstanding = FALSE;
	UNICODE_STRING          uniDOSString;
	PDEVICE_OBJECT	pFirstObj;
	PDEVICE_OBJECT	pNextObj;

 	PDEVICE_EXTENSION 		pExt;
 	PZT_DEVICE_EXTENSION	pDevExt;
	PIO_REQ				pReq;
  	PLIST_ENTRY			link;
    
	DbgPrint("ComSpy_Unload Called \r\n");
	
	pFirstObj=DriverObject->DeviceObject;
  
	if(pFirstObj->DeviceType==FILE_DEVICE_COMPORT)
	{
		pDevExt=(PZT_DEVICE_EXTENSION)pFirstObj->DeviceExtension;
		RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);
		IoDeleteSymbolicLink (&uniDOSString);

		/////////////////////////////////////////
		if(pDevExt->UserVirtualAddress) 
		{ 
			MmUnmapLockedPages(pDevExt->UserVirtualAddress, pDevExt->MyMdl); 
			pDevExt->UserVirtualAddress = NULL; 
		} 

		if(pDevExt->MyMdl) 
		{ 
			IoFreeMdl(pDevExt->MyMdl); 
			pDevExt->MyMdl = NULL; 
		} 

		if(pDevExt->SystemVirtualAddress) 
		{ 
			ExFreePool(pDevExt->SystemVirtualAddress); 
			pDevExt->SystemVirtualAddress = NULL; 
		} 
		/////////////////////////////////////////
        while (link =  ExInterlockedRemoveHeadList(&g_data_lst, &g_req_splock))
        {
            pReq=  CONTAINING_RECORD(link,IO_REQ,entry);

  			ExFreePool(pReq->pData); 
			ExFreePool(pReq); 
		}
		DbgPrint("ComSpy_Unload IoCtrl First Unload \r\n");
		
		/////////////////////////////////////////
		pNextObj=pFirstObj->NextDevice;
		IoDeleteDevice(pFirstObj);
	  
		pExt= (PDEVICE_EXTENSION)pNextObj->DeviceExtension;
		IoDetachDevice( pExt->TargetDeviceObject );
		IoDeleteDevice(pExt->pFilterDeviceObject);
		
		ExFreePool(pExt);


	}

	DbgPrint("ComSpy_Unload end \r\n");
}

NTSTATUS ComSpy_Read(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{

	PIO_STACK_LOCATION		IrpStack;
//	PIO_STACK_LOCATION		NextIrpStack;
	PDEVICE_EXTENSION pExt;
	
    NTSTATUS NtStatus = STATUS_SUCCESS;
    
    DbgPrint("ComSpy_Read Called \r\n");

	if(DeviceObject->DeviceType==FILE_DEVICE_COMPORT)
	{
	   return IOCtrl_Read(DeviceObject,Irp);
	}

    
    pExt = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    IrpStack = IoGetCurrentIrpStackLocation(Irp);

//    IoSkipCurrentIrpStackLocation(Irp);
    IoCopyCurrentIrpStackLocationToNext(Irp);
    IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) ReadCompletion, NULL, TRUE, TRUE, TRUE);	//care about the result

    NtStatus = IoCallDriver(pExt->TargetDeviceObject, Irp);

    DbgPrint("ComSpy_Read Exit 0x%0x \r\n", NtStatus);

	return NtStatus;

}

NTSTATUS ComSpy_Write(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{

	PIO_STACK_LOCATION		IrpStack;
//	PIO_STACK_LOCATION		NextIrpStack;
	PDEVICE_EXTENSION pExt;
	
    NTSTATUS NtStatus = STATUS_SUCCESS;
    
    DbgPrint("ComSpy_Write Called \r\n");

	if(DeviceObject->DeviceType==FILE_DEVICE_COMPORT)
	{
	   return IOCtrl_Write(DeviceObject,Irp);
	}

    
    pExt = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    IrpStack = IoGetCurrentIrpStackLocation(Irp);

//    IoSkipCurrentIrpStackLocation(Irp);
    IoCopyCurrentIrpStackLocationToNext(Irp);
    IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) WriteCompletion, NULL, TRUE, TRUE, TRUE);
   

    NtStatus = IoCallDriver(pExt->TargetDeviceObject, Irp);

    DbgPrint("ComSpy_Write Exit 0x%0x \r\n", NtStatus);

	return NtStatus;

}

NTSTATUS ComSpy_IoCtl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{

	PIO_STACK_LOCATION		IrpStack;
//	PIO_STACK_LOCATION		NextIrpStack;
	PDEVICE_EXTENSION pExt;
	
    NTSTATUS NtStatus = STATUS_SUCCESS;
    
 //   DbgPrint("ComSpy_IoCtl Called \r\n");

	if(DeviceObject->DeviceType==FILE_DEVICE_COMPORT)
	{
	   return IOCtrl_IoCtl(DeviceObject,Irp);
	}

    
    pExt = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    IrpStack = IoGetCurrentIrpStackLocation(Irp);

    switch(IrpStack->Parameters.DeviceIoControl.IoControlCode)
    {
           case IOCTL_SERIAL_SET_BAUD_RATE:
    					DbgPrint("ComSpy_IoCtl Set Baud Rate \r\n");
           		break;
           case IOCTL_SERIAL_SET_LINE_CONTROL:
    					DbgPrint("ComSpy_IoCtl Set Line Ctl \r\n");
            	break;
         default:
 //   					DbgPrint("ComSpy_IoCtl IoControlCode  %x \r\n", IrpStack->Parameters.DeviceIoControl.IoControlCode);
    					break;

    }
//    IoSkipCurrentIrpStackLocation(Irp);
    IoCopyCurrentIrpStackLocationToNext(Irp);
    IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) IOCompletion, NULL, TRUE, TRUE, TRUE);
   

    NtStatus = IoCallDriver(pExt->TargetDeviceObject, Irp);

//    DbgPrint("- ComSpy_IoCtl Exit 0x%0x \r\n", NtStatus);

	return NtStatus;

}


NTSTATUS ComSpy_Create( IN PDEVICE_OBJECT    DeviceObject,  IN PIRP  Irp )
{
	PIO_STACK_LOCATION		IrpStack;
    NTSTATUS status = STATUS_SUCCESS;
	PDEVICE_EXTENSION	pExt; 

	DbgPrint(("+ Enter ComSpy Create routine\n"));

	if(DeviceObject->DeviceType==FILE_DEVICE_COMPORT)
	{
	   return IOCtrl_CreateClose(DeviceObject,Irp);
	}

	pExt = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  
    IrpStack = IoGetCurrentIrpStackLocation(Irp);

	IoSkipCurrentIrpStackLocation (Irp);				//pass the IRP to another driver
//	IoCopyCurrentIrpStackLocationToNext(Irp);
//  IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) DefaultCompletion, pExt, TRUE, TRUE, TRUE);

	status = IoCallDriver (pExt->TargetDeviceObject, Irp);

    DbgPrint("- ComSpy_Create Exit 0x%0x \r\n", status);

⌨️ 快捷键说明

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