• <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),開心的工作
            簡(jiǎn)單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
            FileMon源碼學(xué)習(xí)筆記(一)
            2008-11-24 09:37

            //----------------------------------------------------------------------
            //
            // HookDrive
            //
            // Hook the drive specified by determining which device object to
            // attach to. The algorithm used here is similar to the one used
            // internally by NT to determine which device object a file system request
            // is directed at.
            //
            //----------------------------------------------------------------------
            BOOLEAN
            HookDrive(
            ??? IN ULONG Drive,
            ??? IN PDRIVER_OBJECT DriverObject
            ??? )
            {
            ??? IO_STATUS_BLOCK???? ioStatus;
            ??? HANDLE????????????? ntFileHandle;??
            ??? OBJECT_ATTRIBUTES?? objectAttributes;
            ??? PDEVICE_OBJECT????? fileSysDevice;
            ??? PDEVICE_OBJECT????? hookDevice;
            ??? UNICODE_STRING????? fileNameUnicodeString;
            ??? PFILE_FS_ATTRIBUTE_INFORMATION fileFsAttributes;
            ??? ULONG?????????????? fileFsAttributesSize;
            ??? WCHAR?????????????? filename[] = L"
            \\DosDevices\\A:\\ ";
            ??? NTSTATUS??????????? ntStatus;
            ??? ULONG?????????????? i;
            ??? PFILE_OBJECT??????? fileObject;
            ??? PHOOK_EXTENSION???? hookExtension;
            ???
            ??? //
            ??? // Is it a legal drive letter?
            ??? //
            ??? if( Drive >= 26 ) {

            ??????? return FALSE;
            ??? }

            ??? //
            ??? // Has this drive already been hooked?
            ??? //
            ??? if( DriveHookDevices[Drive] == NULL ) {

            ??????? //
            ??????? // Frob the name to make it refer to the drive specified in the input
            ??????? // parameter.
            ??????? //
            ??????? filename[12] = (CHAR) ('A'+Drive);

            ??????? //
            ??????? // We have to figure out what device to hook - first open the volume's
            ??????? // root directory
            ??????? //
            ??????? RtlInitUnicodeString( &fileNameUnicodeString, filename );
            ??????? InitializeObjectAttributes( &objectAttributes, &fileNameUnicodeString,
            ??????????????????????????????????? OBJ_CASE_INSENSITIVE, NULL, NULL );
            ??????? ntStatus = ZwCreateFile( &ntFileHandle, SYNCHRONIZE|FILE_ANY_ACCESS,
            ???????????????????????????????? &objectAttributes, &ioStatus, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
            ???????????????????????????????? FILE_OPEN,
            ???????????????????????????????? FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE,
            ???????????????????????????????? NULL, 0 );
            ??????? if( !NT_SUCCESS( ntStatus ) ) {

            ??????????? DbgPrint(("Filemon: Could not open drive %c: %x\n", 'A'+Drive, ntStatus ));
            ??????????? return FALSE;
            ??????? }

            ??????? DbgPrint(("Filemon: opened the root directory!!! handle: %x\n", ntFileHandle));??

            ??????? //
            ??????? // Got the file handle, so now look-up the file-object it refers to
            ??????? //
            ??????? ntStatus = ObReferenceObjectByHandle( ntFileHandle, FILE_READ_DATA,
            ????????????????????????????????????????????? NULL, KernelMode, &fileObject, NULL );
            ??????? if( !NT_SUCCESS( ntStatus )) {

            ??????????? DbgPrint(("Filemon: Could not get fileobject from handle: %c\n", 'A'+Drive ));
            ??????????? ZwClose( ntFileHandle );
            ??????????? return FALSE;
            ??????? }

            ??????? //
            ??????? // Next, find out what device is associated with the file object by getting its related
            ??????? // device object
            ??????? //
            ??????? fileSysDevice = IoGetRelatedDeviceObject( fileObject );

            ??????? if( ! fileSysDevice ) {

            ??????????? DbgPrint(("Filemon: Could not get related device object: %c\n", 'A'+Drive ));
            ??????????? ObDereferenceObject( fileObject );
            ??????????? ZwClose( ntFileHandle );
            ??????????? return FALSE;
            ??????? }

            ??????? //
            ??????? // Check the device list to see if we've already attached to this particular device.
            ??????? // This can happen when more than one drive letter is being handled by the same network
            ??????? // redirecter
            ??????? //
            ??????? for( i = 0; i < 26; i++ ) {

            ??????????? if( DriveHookDevices[i] == fileSysDevice ) {

            ??????????????? //
            ??????????????? // If we're already watching it, associate this drive letter
            ??????????????? // with the others that are handled by the same network driver. This
            ??????????????? // enables us to intelligently update the hooking menus when the user
            ??????????????? // specifies that one of the group should not be watched -we mark all
            ??????????????? // of the related drives as unwatched as well
            ??????????????? //
            ??????????????? ObDereferenceObject( fileObject );
            ??????????????? ZwClose( ntFileHandle );
            ??????????????? DriveHookDevices[ Drive ] = fileSysDevice;
            ??????????????? return TRUE;
            ??????????? }
            ??????? }

            ??????? //
            ??????? // The file system's device hasn't been hooked already, so make a hooking device
            ??????? // object that will be attached to it.
            ??????? //
            ??????? ntStatus = IoCreateDevice( DriverObject,
            ?????????????????????????????????? sizeof(HOOK_EXTENSION),
            ?????????????????????????????????? NULL,
            ?????????????????????????????????? fileSysDevice->DeviceType,
            ?????????????????????????????????? 0,
            ?????????????????????????????????? FALSE,
            ?????????????????????????????????? &hookDevice );
            ??????? if( !NT_SUCCESS(ntStatus) ) {

            ??????????? DbgPrint(("Filemon: failed to create associated device: %c\n", 'A'+Drive ));??

            ??????????? ObDereferenceObject( fileObject );
            ??????????? ZwClose( ntFileHandle );

            ??????????? return FALSE;
            ??????? }

            ??????? //
            ??????? // Clear the device's init flag as per NT DDK KB article on creating device
            ??????? // objects from a dispatch routine
            ??????? //
            ??????? hookDevice->Flags &= ~DO_DEVICE_INITIALIZING;

            ??????? //
            ??????? // Setup the device extensions. The drive letter and file system object are stored
            ??????? // in the extension.
            ??????? //
            ??????? hookExtension = hookDevice->DeviceExtension;
            ??????? hookExtension->LogicalDrive = 'A'+Drive;
            ??????? hookExtension->FileSystem?? = fileSysDevice;
            ??????? hookExtension->Hooked?????? = TRUE;
            ??????? hookExtension->Type???????? = STANDARD;

            ??????? //
            ??????? // Finally, attach to the device. The second we're successfully attached, we may
            ??????? // start receiving IRPs targetted at the device we've hooked.
            ??????? //
            ??????? ntStatus = IoAttachDeviceByPointer( hookDevice, fileSysDevice );
            ??????? if( !NT_SUCCESS(ntStatus) ) {

            ??????????? //
            ??????????? // Couldn' attach for some reason
            ??????????? //
            ??????????? DbgPrint(("Filemon: Connect with Filesystem failed: %c (%x) =>%x\n",
            ????????????????????? 'A'+Drive, fileSysDevice, ntStatus ));

            ??????????? //
            ??????????? // Derefence the object and get out
            ??????????? //
            ??????????? ObDereferenceObject( fileObject );
            ??????????? ZwClose( ntFileHandle );

            ??????????? return FALSE;

            ??????? } else {

            ??????????? //
            ??????????? // Make a new drive group for the device,l if it does not have one
            ??????????? // already
            ??????????? //
            ??????????? DbgPrint(("Filemon: Successfully connected to Filesystem device %c\n", 'A'+Drive ));
            ??????? }

            ??????? //
            ??????? // Determine if this is a NTFS drive
            ??????? //
            ??????? fileFsAttributesSize = sizeof( FILE_FS_ATTRIBUTE_INFORMATION) + MAXPATHLEN;
            ??????? hookExtension->FsAttributes = (PFILE_FS_ATTRIBUTE_INFORMATION) ExAllocatePool( NonPagedPool,
            ?????????????????????????????????????????????????????????????????????????????????????? fileFsAttributesSize );
            ??????? if( hookExtension->FsAttributes &&
            ??????????? !NT_SUCCESS( IoQueryVolumeInformation( fileObject, FileFsAttributeInformation,
            ?????????????????????????????????????????????????? fileFsAttributesSize, hookExtension->FsAttributes,
            ?????????????????????????????????????????????????? &fileFsAttributesSize ))) {

            ??????????? //
            ??????????? // On failure, we just don't have attributes for this file system
            ??????????? //
            ??????????? ExFreePool( hookExtension->FsAttributes );
            ??????????? hookExtension->FsAttributes = NULL;
            ??????? }

            ??????? //
            ??????? // Close the file and update the hooked drive list by entering a
            ??????? // pointer to the hook device object in it.
            ??????? //
            ??????? ObDereferenceObject( fileObject );

            ??????? ZwClose( ntFileHandle );

            ??????? DriveHookDevices[Drive] = hookDevice;
            ???????
            ??? } else {

            ??????? hookExtension = DriveHookDevices[Drive]->DeviceExtension;
            ??????? hookExtension->Hooked = TRUE;
            ??? }
            ??? return TRUE;
            }

            以上摘自FileMon源碼,作者在獲得要hook的設(shè)備的DeviceObject的時(shí)候用了以下方式:

            ZwCreateFile——》ObReferenceObjectByHandle——》IoGetRelatedDeviceObject

            另外,這個(gè)設(shè)備對(duì)象也可以直接用一個(gè)函數(shù)獲得IoGetDeviceObjectPointer,該函數(shù)原型如下:

            NTSTATUS
            IoGetDeviceObjectPointer(
            ??? IN PUNICODE_STRING ObjectName ,
            ??? IN ACCESS_MASK DesiredAccess ,
            ??? OUT PFILE_OBJECT * FileObject ,
            ??? OUT PDEVICE_OBJECT * DeviceObject
            ??? );

            直接由名字獲得設(shè)備對(duì)象和文件對(duì)象,而該函數(shù)內(nèi)部的實(shí)現(xiàn)方式combojiang大俠也給出過c的逆向代碼如下: 逆向?yàn)閏的代碼:
            NTSTATUS
            IoGetDeviceObjectPointer(
            ???? IN PUNICODE_STRING ObjectName,
            ???? IN ACCESS_MASK DesiredAccess,
            ???? OUT PFILE_OBJECT *FileObject,
            ???? OUT PDEVICE_OBJECT *DeviceObject
            ???? )
            {
            ???? IO_STATUS_BLOCK ioStatus;
            ???? OBJECT_ATTRIBUTES objectAttributes;
            ????
            ???? //額外定義出來的棧變量。由于C與匯編的游戲規(guī)則不同。
            ???? PFILE_OBJECT fileObject;
            ???? HANDLE fileHandle;
            ???? NTSTATUS status;
            ????
            ???? InitializeObjectAttributes( &objectAttributes,
            ???????????????????????????????? ObjectName,
            ???????????????????????????????? OBJ_KERNEL_HANDLE,
            ???????????????????????????????? (HANDLE) NULL,
            ???????????????????????????????? (PSECURITY_DESCRIPTOR) NULL );
            ????????????????????????????????
            ??? status = ZwOpenFile( &fileHandle,
            ????????????????????????? DesiredAccess,
            ????????????????????????? &objectAttributes,
            ????????????????????????? &ioStatus,
            ????????????????????????? 0,
            ????????????????????????? 0x40 );

            ???? if (status >= 0)
            ???? {

            ????????? status = ObReferenceObjectByHandle( fileHandle,
            ???????????????????????????????????????????? 0,
            ???????????????????????????????????????????? IoFileObjectType,
            ???????????????????????????????????????????? 0,
            ???????????????????????????????????????????? (PVOID *) &fileObject,
            ???????????????????????????????????????????? 0 );
            ???????? if (status >= 0)
            ???????? {

            ???????????? *FileObject = fileObject;
            ???????????? *DeviceObject = IoGetRelatedDeviceObject( fileObject );
            ???????? }

            ???????? ZwClose( fileHandle );
            ???? }

            ???? return status;
            }
            與FileMon的源碼使用的方法類似,所以FileMon源碼里面應(yīng)該可以用這個(gè)函數(shù)直接替代,但是原作者沒有直接調(diào)用這個(gè)函數(shù),不知道原因是什么,難道是寫FileMon的時(shí)候還沒提供這個(gè)函數(shù),所以要自己來實(shí)現(xiàn)嗎?

            国产农村妇女毛片精品久久| 狠狠色丁香久久婷婷综合五月| 国产精品免费久久久久影院| 久久综合日本熟妇| 午夜欧美精品久久久久久久 | 久久强奷乱码老熟女| 青草久久久国产线免观| 国产精品久久成人影院| 亚洲欧美另类日本久久国产真实乱对白| 久久精品中文字幕大胸| 色成年激情久久综合| 伊人久久无码中文字幕| 人妻无码精品久久亚瑟影视 | 久久97精品久久久久久久不卡| 久久亚洲国产精品五月天婷| 国内精品九九久久久精品| 久久久午夜精品福利内容| 国产成人精品久久亚洲高清不卡 | 亚洲?V乱码久久精品蜜桃 | 国产午夜福利精品久久2021| 婷婷久久综合九色综合九七| 国产精品热久久毛片| 99999久久久久久亚洲| 久久精品国产亚洲av影院| 久久精品人人做人人爽电影| 婷婷久久五月天| 思思久久99热只有频精品66| 无码人妻久久一区二区三区蜜桃| 九九久久精品国产| 精品国产综合区久久久久久| 国产A级毛片久久久精品毛片| 91亚洲国产成人久久精品| 久久久国产精品网站| 国产精品欧美久久久久天天影视| 国产精品一久久香蕉国产线看 | 精品免费久久久久国产一区| 中文字幕一区二区三区久久网站| 99久久精品费精品国产一区二区| 久久国产高清字幕中文| a级毛片无码兔费真人久久| 久久精品国产精品亜洲毛片|