• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開心的工作
            簡單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
            FileMon源碼學(xué)習(xí)筆記(二)
            2008-11-24 10:41

            FileMon源碼中另一個(gè)比較疑惑的地方,F(xiàn)ileMon創(chuàng)建了兩類設(shè)備,一個(gè)是用于和ring3通信的GUI設(shè)備,另一個(gè)是hook的過濾設(shè)備,但在代碼中,當(dāng)收到發(fā)向GUI設(shè)備的IRP_MJ_DEVICE_CONTROL時(shí),代碼竟是去調(diào)用屬于hook設(shè)備的功能函數(shù),而在這個(gè)功能函數(shù)里面通過條件判斷是否是GUI設(shè)備來分別處理,而對(duì)于發(fā)給GUI設(shè)備的其他IRP都在直接在GUI的處理函數(shù)中直接處理的,不知道作者這樣寫是否有什么深層的含義,不過對(duì)于我這種初學(xué)者來說,這樣的寫法倒是容易引起混亂,還是不同設(shè)備的功能函數(shù),分開來寫好一點(diǎn)。下面附上相關(guān)代碼:

            //=========================================================

            //GUI設(shè)備的功能函數(shù),注意IRP_MJ_DEVICE_CONTROL的實(shí)現(xiàn)

            //=========================================================

            NTSTATUS
            FilemonDeviceRoutine(
            ??? IN PDEVICE_OBJECT DeviceObject,
            ??? IN PIRP Irp
            ??? )
            {
            ??? PIO_STACK_LOCATION irpStack;
            ??? PVOID?????????????? inputBuffer;
            ??? PVOID?????????????? outputBuffer;
            ??? ULONG?????????????? inputBufferLength;
            ??? ULONG?????????????? outputBufferLength;
            ??? ULONG?????????????? ioControlCode;

            ??? //
            ??? // Go ahead and set the request up as successful
            ??? //
            ??? Irp->IoStatus.Status????? = STATUS_SUCCESS;
            ??? Irp->IoStatus.Information = 0;

            ??? //
            ??? // Get a pointer to the current location in the Irp. This is where
            ??? // the function codes and parameters are located.
            ??? //
            ??? irpStack = IoGetCurrentIrpStackLocation (Irp);

            ??? //
            ??? // Get the pointer to the input/output buffer and its length
            ??? //
            ??? inputBuffer??????? = Irp->AssociatedIrp.SystemBuffer;
            ??? inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
            ??? outputBuffer?????? = Irp->AssociatedIrp.SystemBuffer;
            ??? outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
            ??? ioControlCode????? = irpStack->Parameters.DeviceIoControl.IoControlCode;

            ??? switch (irpStack->MajorFunction) {
            ??? case IRP_MJ_CREATE:

            ??????? DbgPrint(("Filemon: IRP_MJ_CREATE\n"));

            ??????? //
            ??????? // Start the sequence number at 0
            ??????? //
            ??????? Sequence = 0;
            ??????? break;

            ??? case IRP_MJ_CLOSE:

            ??????? DbgPrint(("Filemon: IRP_MJ_CLOSE\n"));

            ??????? //
            ??????? // A GUI is closing communication
            ??????? //
            ??????? FilterOn = FALSE;

            ??????? //
            ??????? // If the GUI has no more references to us, reset the output
            ??????? // buffers and hash table.
            ??????? //
            ??????? FilemonResetLog();
            ??????? FilemonHashCleanup();

            ??????? //
            ??????? // Stop capturing drives
            ??????? //
            ??????? HookDriveSet( 0, DeviceObject->DriverObject );
            ??????? UnhookSpecialFs( NPFS );
            ??????? UnhookSpecialFs( MSFS );
            ??????? break;

            ??? case IRP_MJ_DEVICE_CONTROL:

            ??????? //
            ??????? // This path will never execute because we have registered a
            ??????? // fast I/O path for device control. That means that the fast I/O entry
            ??????? // point will ALWAYS be called for Device Control operations
            ??????? //
            ??????? DbgPrint (("Filemon: IRP_MJ_DEVICE_CONTROL\n"));

            ??????? //
            ??????? // Get output buffer if its passed as an MDL
            ??????? //
            ??????? if( Irp->MdlAddress ) {

            ??????????? outputBuffer = MmGetSystemAddressForMdl( Irp->MdlAddress );
            ??????? }

            ??????? //
            ??????? // Its a request from the GUI. Simply call our fast handler.
            ??????? //
            ??????? FilemonFastIoDeviceControl( irpStack->FileObject, TRUE,
            ??????????????????????????????????? inputBuffer, inputBufferLength,
            ??????????????????????????????????? outputBuffer, outputBufferLength,
            ??????????????????????????????????? ioControlCode, &Irp->IoStatus, DeviceObject );
            ??????? break;
            ??? }

            ??? //
            ??? // Complete the IRP
            ??? //
            ??? IoCompleteRequest( Irp, IO_NO_INCREMENT );
            ??? return STATUS_SUCCESS;??
            }

            //=========================================================

            //hook設(shè)備的功能函數(shù),在里面夾雜了處理GUI設(shè)備的IRP_MJ_DEVICE_CONTROL的代碼

            //=========================================================
            BOOLEAN
            FilemonFastIoDeviceControl(
            ??? IN PFILE_OBJECT FileObject,
            ??? IN BOOLEAN Wait,
            ??? IN PVOID InputBuffer,
            ??? IN ULONG InputBufferLength,
            ??? OUT PVOID OutputBuffer,
            ??? IN ULONG OutputBufferLength,
            ??? IN ULONG IoControlCode,
            ??? OUT PIO_STATUS_BLOCK IoStatus,
            ??? IN PDEVICE_OBJECT DeviceObject
            ??? )
            {
            ??? BOOLEAN???????????? retval = FALSE;
            ??? BOOLEAN???????????? logMutexReleased;
            ??? PHOOK_EXTENSION???? hookExt;
            ??? PLOG_BUF??????????? oldLog, savedCurrentLog;
            ??? CHAR??????????????? fullPathName[MAXPATHLEN], name[PROCNAMELEN], errorBuf[ERRORLEN];
            ??? KIRQL?????????????? oldirql;
            ??? LARGE_INTEGER?????? timeStampStart, timeStampComplete, timeResult;
            ??? LARGE_INTEGER?????? dateTime;

            ??? hookExt = DeviceObject->DeviceExtension;
            ??? if( hookExt->Type == GUIINTERFACE ) {

            ??????? //
            ??????? // Its a message from our GUI!
            ??????? //
            ??????? IoStatus->Status????? = STATUS_SUCCESS; // Assume success
            ??????? IoStatus->Information = 0;????? // Assume nothing returned

            ??????? switch ( IoControlCode ) {

            ??????? case IOCTL_FILEMON_VERSION:

            ??????????? //
            ??????????? // Version #
            ??????????? //
            ??????????? if( OutputBufferLength >= sizeof(ULONG)) {

            ??????????????? *(ULONG *)OutputBuffer = FILEMONVERSION;
            ??????????????? IoStatus->Information = sizeof(ULONG);

            ??????????? } else {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????? }???????????
            ??????????? break;

            ??????? case IOCTL_FILEMON_SETDRIVES:

            ??????????? //
            ??????????? // Hook and/or unhook drives
            ??????????? //
            ??????????? DbgPrint (("Filemon: set drives\n"));

            ??????????? if( InputBufferLength >= sizeof(ULONG) &&
            ???????????????? OutputBufferLength >= sizeof(ULONG)) {

            ??????????????? *(ULONG *)OutputBuffer = HookDriveSet( *(ULONG *)InputBuffer, DeviceObject->DriverObject );
            ??????????????? IoStatus->Information = sizeof(ULONG);

            ??????????? } else {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????? }
            ??????????? break;

            ??????? case IOCTL_FILEMON_HOOKSPECIAL:

            ??????????? if( InputBufferLength >= sizeof(FILE_SYSTEM_TYPE )) {

            ??????????????? if( !HookSpecialFs( DeviceObject->DriverObject, *(PFILE_SYSTEM_TYPE) InputBuffer )) {
            ???????????????
            ??????????????????? IoStatus->Status = STATUS_UNSUCCESSFUL;
            ??????????????? }
            ??????????? } else {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????? }
            ??????????? break;

            ??????? case IOCTL_FILEMON_UNHOOKSPECIAL:

            ??????????? if( InputBufferLength >= sizeof(FILE_SYSTEM_TYPE )) {

            ??????????????? UnhookSpecialFs( *(PFILE_SYSTEM_TYPE) InputBuffer );

            ??????????? } else {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????? }
            ??????????? break;

            ??????? case IOCTL_FILEMON_STOPFILTER:
            ???????????
            ??????????? //
            ??????????? // Turn off logging
            ??????????? //
            ??????????? DbgPrint(("Filemon: stop logging\n"));
            ??????????? FilterOn = FALSE;
            ??????????? break;

            ??????? case IOCTL_FILEMON_STARTFILTER:
            ?????????
            ??????????? //
            ??????????? // Turn on logging
            ??????????? //
            ??????????? DbgPrint(("Filemon: start logging\n"));
            ??????????? FilterOn = TRUE;
            ??????????? break;

            ??????? case IOCTL_FILEMON_SETFILTER:

            ??????????? //
            ??????????? // Gui is updating the filter functions
            ??????????? //
            ??????????? DbgPrint(("Filemon: set filter\n"));

            ??????????? if( InputBufferLength >= sizeof(FILTER) ) {

            ??????????????? FilterDef = *(PFILTER) InputBuffer;
            ??????????????? FilemonUpdateFilters();

            ??????????? } else {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????? }
            ??????????? break;

            ??????? case IOCTL_FILEMON_UNLOADQUERY:
            #if DBG
            ??????????? //
            ??????????? // Is it possible to unload?
            ??????????? //
            ??????????? KeAcquireSpinLock( &CountMutex, &oldirql );
            ??????????? IoStatus->Information = OutstandingIRPCount;

            ??????????? //
            ??????????? // Any outstanding Irps?
            ??????????? //
            ??????????? if( !OutstandingIRPCount ) {

            ??????????????? //
            ??????????????? // Nope, so don't process anymore
            ??????????????? //
            ??????????????? UnloadInProgress = TRUE;

            ??????????????? KeReleaseSpinLock( &CountMutex, oldirql );

            ??????????????? //
            ??????????????? // Stop capturing drives
            ??????????????? //
            ??????????????? HookDriveSet( 0, DeviceObject->DriverObject );
            ??????????????? UnhookSpecialFs( NPFS );
            ??????????????? UnhookSpecialFs( MSFS );

            ??????????????? //
            ??????????????? // Detach from all devices
            ??????????????? //
            ??????????????? UnloadDetach();

            ??????????? } else {

            ??????????????? KeReleaseSpinLock( &CountMutex, oldirql );
            ??????????? }
            #else // DBG
            ??????????? IoStatus->Information = 1;
            #endif // DBG
            ??????????? break;

            ??????? case IOCTL_FILEMON_ZEROSTATS:

            ??????????? //
            ??????????? // Reset all output buffers
            ??????????? //
            ??????????? DbgPrint (("Filemon: zero stats\n"));

            ??????????? ExAcquireFastMutex( &LogMutex );

            ??????????? while( CurrentLog->Next ) {

            ??????????????? //
            ??????????????? // Free all but the first output buffer
            ??????????????? //
            ??????????????? oldLog = CurrentLog->Next;
            ??????????????? CurrentLog->Next = oldLog->Next;

            ??????????????? ExFreePool( oldLog );
            ??????????????? NumLog--;
            ??????????? }

            ??????????? //
            ??????????? // Set the output pointer to the start of the output buffer
            ??????????? //
            ??????????? CurrentLog->Len = 0;
            ??????????? Sequence = 0;

            ??????????? ExReleaseFastMutex( &LogMutex );
            ??????????? break;

            ??????? case IOCTL_FILEMON_GETSTATS:

            ??????????? //
            ??????????? // Copy the oldest output buffer to the caller
            ??????????? //
            ??????????? DbgPrint (("Filemon: get stats\n"));

            ??? //
            ??????????? // If the output buffer is too large to fit into the caller's buffer
            ??????????? //
            ??????????? if( LOGBUFSIZE > OutputBufferLength ) {

            ??????????????? IoStatus->Status = STATUS_BUFFER_TOO_SMALL;
            ??????????????? return FALSE;
            ??????????? }

            ??????????? //
            ??????????? // Probe the output buffer
            ??????????? //
            ??????????? try {????????????????

            ??????????????? ProbeForWrite( OutputBuffer,
            ?????????????????????????????? OutputBufferLength,
            ?????????????????????????????? sizeof( UCHAR ));

            ??????????? } except( EXCEPTION_EXECUTE_HANDLER ) {

            ??????????????? IoStatus->Status = STATUS_INVALID_PARAMETER;
            ??????????????? return FALSE;
            ??????????? }???????????

            ??????????? //
            ??????????? // We're okay, lock the buffer pool
            ??????????? //
            ??????????? ExAcquireFastMutex( &LogMutex );
            ??????????? if( CurrentLog->Len || CurrentLog->Next ) {

            ??????????????? //
            ??????????????? // Start output to a new output buffer
            ??????????????? //
            ??????????????? FilemonAllocateLog();

            ??????????????? //
            ??????????????? // Fetch the oldest to give to user
            ??????????????? //
            ??????????????? oldLog = FilemonGetOldestLog();

            ??????????????? if( oldLog != CurrentLog ) {

            ??????????????????? logMutexReleased = TRUE;
            ??????????????????? ExReleaseFastMutex( &LogMutex );

            ??????????????? } else {

            ??????????????????? logMutexReleased = FALSE;
            ??????????????? }

            ??????????????? //
            ??????????????? // Copy it to the caller's buffer
            ??????????????? //
            ??????????????? memcpy( OutputBuffer, oldLog->Data, oldLog->Len );

            ??????????????? //
            ??????????????? // Return length of copied info
            ??????????????? //
            ??????????????? IoStatus->Information = oldLog->Len;

            ??????????????? //
            ??????????????? // Deallocate buffer - unless its the last one
            ??????????????? //
            ??????????????? if( logMutexReleased ) {
            ???????????????????
            ??????????????????? ExFreePool( oldLog );

            ??????????????? } else {

            ??????????????????? CurrentLog->Len = 0;
            ??????????????????? ExReleaseFastMutex( &LogMutex );???????????????????
            ??????????????? }

            ??????????? } else {

            ??????????????? //
            ??????????????? // There is no unread data
            ??????????????? //
            ??????????????? ExReleaseFastMutex( &LogMutex );
            ???? IoStatus->Information = 0;
            ??????????? }
            ??????????? break;

            ??????? default:

            ??????????? //
            ??????????? // Unknown control
            ??????????? //
            ??????????? DbgPrint (("Filemon: unknown IRP_MJ_DEVICE_CONTROL\n"));
            ??????????? IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST;
            ??????????? break;
            ??????? }

            ??????? retval = TRUE;

            ??? } else {

            ??????? //
            ??????? // Its a call for a file system, so pass it through
            ??????? //
            ??????? if( FASTIOPRESENT( hookExt, FastIoDeviceControl ) ) {
            ???????
            ??????????? FilemonGetFullPath( FALSE, FileObject, hookExt, fullPathName );
            ??????????? TIMESTAMPSTART();

            ??????????? retval = hookExt->FileSystem->DriverObject->FastIoDispatch->FastIoDeviceControl(
            ??????????????? FileObject, Wait, InputBuffer, InputBufferLength, OutputBuffer,
            ??????????????? OutputBufferLength, IoControlCode, IoStatus, hookExt->FileSystem );

            ??????????? if(hookExt->Hooked) {

            ??????????????? TIMESTAMPSTOP();
            ??????????????? LogRecord( TRUE, NULL, &dateTime, &timeResult,
            ?????????????????????????? "%s\tFASTIO_DEVICE_CONTROL\t%s\tIOCTL: 0x%X\t%s",
            ?????????????????????????? FilemonGetProcess( name ), fullPathName,
            ?????????????????????????? IoControlCode,
            ?????????????????????????? retval ? ErrorString( IoStatus->Status, errorBuf ) : "FAILURE" );
            ??????????? }
            ??????? }
            ??? }

            ??? return retval;
            }

            国产午夜福利精品久久2021| 国产69精品久久久久9999| 新狼窝色AV性久久久久久| 91精品国产9l久久久久| 久久久久国产日韩精品网站| 久久精品国产亚洲AV香蕉| 亚洲精品高清久久| 久久久久亚洲AV成人网人人网站| 久久久久亚洲Av无码专| 久久精品二区| 成人资源影音先锋久久资源网| 国产精品永久久久久久久久久| 久久精品国产亚洲AV久| 国产农村妇女毛片精品久久| 99久久精品免费看国产一区二区三区 | 久久九九亚洲精品| 要久久爱在线免费观看| 9999国产精品欧美久久久久久| 国产精品一区二区久久精品涩爱| 久久精品草草草| 久久久精品2019免费观看| 午夜精品久久久久| 久久青青草原亚洲av无码| 久久精品一区二区三区不卡| 人妻精品久久久久中文字幕69 | 免费观看成人久久网免费观看| 99精品久久精品一区二区| 色综合久久中文字幕综合网| 久久久91精品国产一区二区三区| 亚洲国产另类久久久精品小说 | 久久精品国产精品亚洲毛片| 久久久久久久久久久| 香蕉久久夜色精品国产2020| 久久久久亚洲av毛片大| 久久久久亚洲精品天堂久久久久久| 亚洲国产精品久久久久婷婷老年 | 天天久久狠狠色综合| 国产精品久久久久久| 狠狠色丁香久久婷婷综| 国内精品久久久久| 九九热久久免费视频|