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

📄 netdisk.c

📁 一个普通的mount工具,能过CIFS协议来挂载盘符
💻 C
📖 第 1 页 / 共 2 页
字号:
    case IOCTL_DISK_IS_WRITABLE:
        return "IOCTL_DISK_IS_WRITABLE";
    case IOCTL_DISK_MEDIA_REMOVAL:
        return "IOCTL_DISK_MEDIA_REMOVAL";
    case IOCTL_STORAGE_MEDIA_REMOVAL:
        return "IOCTL_STORAGE_MEDIA_REMOVAL";
    case IOCTL_CDROM_READ_TOC:
        return "IOCTL_CDROM_READ_TOC";
    case IOCTL_DISK_SET_PARTITION_INFO:
        return "IOCTL_DISK_SET_PARTITION_INFO";
    case IOCTL_DISK_VERIFY:
        return "IOCTL_DISK_VERIFY";
    case IOCTL_ADD_DRIVE:
        return "IOCTL_ADD_DRIVE";
    case IOCTL_REMOVE_DRIVE:
        return "IOCTL_REMOVE_DRIVE";
    case IOCTL_INSERT:
        return "IOCTL_INSERT";
    case IOCTL_EJECT:
        return "IOCTL_EJECT";
    }

	sprintf( s_aryBuffer, "UNKOWN IOCTL:%#x", ulIoControlCode );
    return s_aryBuffer;
}

#define SECTOR_SIZE 512

NTSTATUS
OpenImageFile( 
	IN PDEVICE_OBJECT pDeviceObject, 
	IN PIRP pIrp )
{
    NTSTATUS Status;
	St_OpenImageFile * pCmd = pIrp->AssociatedIrp.SystemBuffer;
	NFS_FILEHANDLE hFile;
	LARGE_INTEGER u64FileSize;
    //PVOID pSystemBuffer = pIrp->AssociatedIrp.SystemBuffer;
    //IO_STATUS_BLOCK stIoStatus;
    //HANDLE hFile;
	//UNICODE_STRING FileName;
    /*OBJECT_ATTRIBUTES stObjectAttributes;
    struct
    {
        FILE_STANDARD_INFORMATION Info;
        WCHAR aryNameBuffer[260];
    }stFileInfo;*/
    
    P_VDiskExtension pDeviceExt = (P_VDiskExtension)
        pDeviceObject->DeviceExtension;

    /*RtlInitUnicodeString( &FileName, (PCWSTR)pSystemBuffer );
    InitializeObjectAttributes( &stObjectAttributes,
								&FileName,
								OBJ_KERNEL_HANDLE|OBJ_CASE_INSENSITIVE,
								NULL,
								NULL );

    Status = ZwCreateFile( 
        &hFile, 
        GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, 
        &stObjectAttributes,
        &stIoStatus,
        NULL,   //  AllocationSize
        FILE_ATTRIBUTE_NORMAL,
        FILE_SHARE_READ,
        FILE_OPEN,
        FILE_SYNCHRONOUS_IO_NONALERT,  //  CreateOptions
        NULL, 0  //  EaBuffer, EaBufferLength
        );

    if( !NT_SUCCESS(Status) )
    {
        return Status;
    }

    Status = ZwQueryInformationFile( 
        hFile, 
        &pIrp->IoStatus,
        &stFileInfo,
        sizeof(stFileInfo),
        FileStandardInformation );

    if( !NT_SUCCESS(Status) )
    {
        ZwClose( hFile );
        return Status;
    }*/

	hFile = NFS_OpenFile( pCmd->szIpAddress, pCmd->szRemotePath, GENERIC_READ|GENERIC_WRITE );
	if( hFile == INVALID_HANDLE_VALUE )
	{
		return STATUS_IO_DEVICE_ERROR;
	}

	if( !NFS_GetFileSize( hFile, &u64FileSize.QuadPart ) )
	{
		NFS_CloseFile( hFile );
		return STATUS_ACCESS_DENIED;
	}

	Status = STATUS_SUCCESS;

    pDeviceExt->hFile = hFile;
    pDeviceExt->FileSize = u64FileSize;//stFileInfo.Info.EndOfFile;

    return Status;
}

int TestClient(PCSTR szIpAddress, PCSTR szRemoteFile, PCSTR szLocalFile);

NTSTATUS
VDisk_DeviceControl(
	IN PDEVICE_OBJECT pDeviceObject, 
	IN PIRP pIrp
	)
{
    NTSTATUS            Status;
    PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
    ULONG ulIoControlCode =  pIrpSp->Parameters.DeviceIoControl.IoControlCode;
	ULONG ulInLength = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
	ULONG ulOutLength = pIrpSp->Parameters.DeviceIoControl.OutputBufferLength;
    PVOID pSystemBuffer = pIrp->AssociatedIrp.SystemBuffer;

    KdPrint( ( "VDisk - %s\n", GetIoCtlName(ulIoControlCode) ) );

    switch (ulIoControlCode)
    {
    case IOCTL_ADD_DRIVE:
        {
            PDEVICE_OBJECT pDiskDevice;
            UNICODE_STRING DeviceName;

            RtlInitUnicodeString(&DeviceName, L"\\Device\\Vdisk0");

            Status = IoCreateDevice( pDeviceObject->DriverObject,
                                     sizeof(St_VDiskExtension),
                                     &DeviceName,
                                     FILE_DEVICE_DISK,
                                     0,
                                     FALSE,
                                     &pDiskDevice);

            if( NT_SUCCESS(Status) )
            {
                P_VDiskExtension pDeviceExt = (P_VDiskExtension)
                    pDiskDevice->DeviceExtension;

                ObReferenceObject( pDiskDevice );
                pDiskDevice->Flags |= DO_DIRECT_IO;
                pDiskDevice->Flags &= ~DO_DEVICE_INITIALIZING;
                pDeviceExt->hFile = NULL;
                pDeviceExt->FileSize.QuadPart = 0;

                if( pSystemBuffer != NULL )
                {
                    Status = OpenImageFile( pDiskDevice, pIrp );

                    /*if( NT_SUCCESS(Status) )
                    {
                        UNICODE_STRING uniWin32NameString;

                        RtlInitUnicodeString( &uniWin32NameString, L"\\DosDevices\\F:" );
                        Status = IoCreateSymbolicLink( &uniWin32NameString, &DeviceName );
                    }*/

                    if( !NT_SUCCESS(Status) )
                    {
                        IoDeleteDevice( pDiskDevice );
                    }
                }
            }
        }

        break;
    case IOCTL_REMOVE_DRIVE:
        {
            P_VDiskExtension pDeviceExt = (P_VDiskExtension)
                pDeviceObject->DeviceExtension;
         
            //ZwClose(pDeviceExt->hFile);
			NFS_CloseFile( pDeviceExt->hFile );
            pDeviceExt->hFile = NULL;
            pDeviceExt->FileSize.QuadPart = 0;

            IoDeleteDevice( pDeviceObject );
			Status = STATUS_SUCCESS;
        }
        break;
    case IOCTL_INSERT:
        Status = OpenImageFile( pDeviceObject, pIrp );
        break;
    case IOCTL_EJECT:
        {
            P_VDiskExtension pDeviceExt = (P_VDiskExtension)
                pDeviceObject->DeviceExtension;
         
            ZwClose(pDeviceExt->hFile);
            pDeviceExt->hFile = NULL;
            pDeviceExt->FileSize.QuadPart = 0;
			Status = STATUS_SUCCESS;
        }
        break;
    case IOCTL_DISK_GET_DRIVE_GEOMETRY:
        {
            P_VDiskExtension pDeviceExt = (P_VDiskExtension)
                pDeviceObject->DeviceExtension;
            PDISK_GEOMETRY  pstDiskGeometry;

            if ( ulOutLength < sizeof(DISK_GEOMETRY) )
            {
                Status = STATUS_BUFFER_TOO_SMALL;
                pIrp->IoStatus.Information = 0;
                break;
            }

            pstDiskGeometry = (PDISK_GEOMETRY)pSystemBuffer;

            pstDiskGeometry->Cylinders.QuadPart = 
                pDeviceExt->FileSize.QuadPart / SECTOR_SIZE / 32 / 2;
            pstDiskGeometry->MediaType = RemovableMedia;//FixedMedia;
            pstDiskGeometry->TracksPerCylinder = 2;
            pstDiskGeometry->SectorsPerTrack = 32;
            pstDiskGeometry->BytesPerSector = SECTOR_SIZE;

            Status = STATUS_SUCCESS;
            pIrp->IoStatus.Information = sizeof(DISK_GEOMETRY);

            break;
        }
    case IOCTL_DISK_GET_PARTITION_INFO:
        {
            P_VDiskExtension pDeviceExt = (P_VDiskExtension)
                pDeviceObject->DeviceExtension;
            PPARTITION_INFORMATION  pstPartitionInformation;

            if ( ulOutLength < sizeof(PARTITION_INFORMATION) )
            {
                Status = STATUS_BUFFER_TOO_SMALL;
                pIrp->IoStatus.Information = 0;
                break;
            }

            pstPartitionInformation = (PPARTITION_INFORMATION)pSystemBuffer;

            pstPartitionInformation->StartingOffset.QuadPart = SECTOR_SIZE;
            pstPartitionInformation->PartitionLength.QuadPart = 
                pDeviceExt->FileSize.QuadPart - SECTOR_SIZE;
            pstPartitionInformation->HiddenSectors = 1;
            pstPartitionInformation->PartitionNumber = 0;
            pstPartitionInformation->PartitionType = 0;
            pstPartitionInformation->BootIndicator = FALSE;
            pstPartitionInformation->RecognizedPartition = FALSE;
            pstPartitionInformation->RewritePartition = FALSE;

            Status = STATUS_SUCCESS;
            pIrp->IoStatus.Information = sizeof(PARTITION_INFORMATION);

            break;
        }
	case IOCTL_DISK_SET_PARTITION_INFO:
		if( ulInLength < sizeof(SET_PARTITION_INFORMATION) )
		{
			Status = STATUS_INVALID_PARAMETER;
			pIrp->IoStatus.Information = 0;
			break;
		}
		
		Status = STATUS_SUCCESS;
		pIrp->IoStatus.Information = 0;
		
		break;
    case IOCTL_DISK_CHECK_VERIFY:
    case IOCTL_STORAGE_CHECK_VERIFY:
    case IOCTL_STORAGE_CHECK_VERIFY2:
	case IOCTL_DISK_IS_WRITABLE:
	case IOCTL_DISK_MEDIA_REMOVAL:
        Status = STATUS_SUCCESS;
        pIrp->IoStatus.Information = 0;
        break;
	case IOCTL_DISK_VERIFY:
        {
			PVERIFY_INFORMATION pVerifyInformation = 
				(PVERIFY_INFORMATION)pSystemBuffer;

            if( ulInLength < sizeof(VERIFY_INFORMATION) )
            {
                Status = STATUS_INVALID_PARAMETER;
                pIrp->IoStatus.Information = 0;
                break;
            }

            Status = STATUS_SUCCESS;
            pIrp->IoStatus.Information = pVerifyInformation->Length;
		}
		break;
	case IOCTL_TEST:
		{
			St_TestCmd * pCmd = (St_TestCmd *)pSystemBuffer;

			if( TestClient( pCmd->szIpAddress, pCmd->szRemotePath, pCmd->szLocalPath ) == 0 )
			{
				Status = STATUS_SUCCESS;
			}
		}
		break;
    default:
		DbgPrint( "!!!Unhandled Ctl: %#x\n", ulIoControlCode );
        Status = STATUS_INVALID_DEVICE_REQUEST;
    }

    pIrp->IoStatus.Status = Status;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return Status;
}

⌨️ 快捷键说明

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