Exemplo n.º 1
0
        public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
        {
            IO_STATUS_BLOCK ioStatusBlock;

            byte[]     buffer = new byte[4096];
            IntPtr     volumeHandle;
            FileStatus fileStatus;
            string     nativePath = @"\??\" + m_directory.FullName.Substring(0, 3);
            NTStatus   status     = CreateFile(out volumeHandle, out fileStatus, nativePath, AccessMask.GENERIC_READ, 0, (FileAttributes)0, ShareAccess.Read, CreateDisposition.FILE_OPEN, (CreateOptions)0);

            result = null;
            if (status != NTStatus.STATUS_SUCCESS)
            {
                return(status);
            }
            status = NtQueryVolumeInformationFile((IntPtr)volumeHandle, out ioStatusBlock, buffer, (uint)buffer.Length, (uint)informationClass);
            CloseFile(volumeHandle);
            if (status == NTStatus.STATUS_SUCCESS)
            {
                int numberOfBytesWritten = (int)ioStatusBlock.Information;
                buffer = ByteReader.ReadBytes(buffer, 0, numberOfBytesWritten);
                result = FileSystemInformation.GetFileSystemInformation(buffer, 0, informationClass);
            }
            return(status);
        }
Exemplo n.º 2
0
        public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
        {
            result = null;
            object     fileHandle;
            FileStatus fileStatus;
            NTStatus   status = CreateFile(out fileHandle, out fileStatus, String.Empty, (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY | (AccessMask)DirectoryAccessMask.FILE_READ_ATTRIBUTES | AccessMask.SYNCHRONIZE, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);

            if (status != NTStatus.STATUS_SUCCESS)
            {
                return(status);
            }

            status = GetFileSystemInformation(out result, fileHandle, informationClass);
            CloseFile(fileHandle);
            return(status);
        }
Exemplo n.º 3
0
        public void GetFileSystemInformation(out FileSystemInformation?result,
                                             FileSystemInformationClass informationClass)
        {
            if (!m_client.InfoLevelPassthrough)
            {
                throw new NotSupportedException("Server does not support InfoLevelPassthrough");
            }

            result = null;
            int maxOutputLength = 4096;
            Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest
            {
                FileSystemInformationClass = informationClass
            };

            Transaction2Request request = new Transaction2Request
            {
                Setup           = subcommand.GetSetup(),
                TransParameters = subcommand.GetParameters(m_client.Unicode),
                TransData       = subcommand.GetData(m_client.Unicode)
            };

            request.TotalDataCount      = (ushort)request.TransData.Length;
            request.TotalParameterCount = (ushort)request.TransParameters.Length;
            request.MaxParameterCount   = Transaction2QueryFSInformationResponse.ParametersLength;
            request.MaxDataCount        = (ushort)maxOutputLength;

            TrySendMessage(request);
            SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);

            if (reply.Header.Status != NTStatus.STATUS_SUCCESS || !(reply.Commands[0] is Transaction2Response transaction2Response))
            {
                throw new NtStatusException(reply.Header.Status);
            }

            Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(transaction2Response.TransData);

            result = subcommandResponse.GetFileSystemInformation(informationClass);
        }
Exemplo n.º 4
0
        public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
        {
            if (m_client.InfoLevelPassthrough)
            {
                result = null;
                int maxOutputLength = 4096;
                Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest();
                subcommand.FileSystemInformationClass = informationClass;

                Transaction2Request request = new Transaction2Request();
                request.Setup               = subcommand.GetSetup();
                request.TransParameters     = subcommand.GetParameters(m_client.Unicode);
                request.TransData           = subcommand.GetData(m_client.Unicode);
                request.TotalDataCount      = (ushort)request.TransData.Length;
                request.TotalParameterCount = (ushort)request.TransParameters.Length;
                request.MaxParameterCount   = Transaction2QueryFSInformationResponse.ParametersLength;
                request.MaxDataCount        = (ushort)maxOutputLength;

                TrySendMessage(request);
                SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
                if (reply != null)
                {
                    if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
                    {
                        Transaction2Response response = (Transaction2Response)reply.Commands[0];
                        Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
                        result = subcommandResponse.GetFileSystemInformation(informationClass);
                    }
                    return(reply.Header.Status);
                }
                return(NTStatus.STATUS_INVALID_SMB);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemplo n.º 5
0
 public static FileSystemInformation GetFileSystemInformation(byte[] buffer, int offset, FileSystemInformationClass informationClass)
 {
     return(informationClass switch
     {
         FileSystemInformationClass.FileFsVolumeInformation => new FileFsVolumeInformation(buffer, offset),
         FileSystemInformationClass.FileFsSizeInformation => new FileFsSizeInformation(buffer, offset),
         FileSystemInformationClass.FileFsDeviceInformation => new FileFsDeviceInformation(buffer, offset),
         FileSystemInformationClass.FileFsAttributeInformation => new FileFsAttributeInformation(buffer, offset),
         FileSystemInformationClass.FileFsControlInformation => new FileFsControlInformation(buffer, offset),
         FileSystemInformationClass.FileFsFullSizeInformation => new FileFsFullSizeInformation(buffer, offset),
         FileSystemInformationClass.FileFsObjectIdInformation => new FileFsObjectIdInformation(buffer, offset),
         FileSystemInformationClass.FileFsSectorSizeInformation => new FileFsSectorSizeInformation(buffer,
                                                                                                   offset),
         _ => throw new UnsupportedInformationLevelException()
     });
Exemplo n.º 6
0
        public void GetFileSystemInformation(out FileSystemInformation result, NtHandle handle, FileSystemInformationClass informationClass)
        {
            QueryInfoRequest request = new QueryInfoRequest
            {
                InfoType = InfoType.FileSystem,
                FileSystemInformationClass = informationClass,
                OutputBufferLength         = 4096,
                FileId = (FileID)handle
            };

            SendCommand(request);
            QueryInfoResponse queryInfoResponse = (QueryInfoResponse)WaitForCommand(request.MessageID);

            queryInfoResponse.IsSuccessElseThrow();
            result = queryInfoResponse.GetFileSystemInformation(informationClass);
        }
Exemplo n.º 7
0
 public void GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
 {
     CreateFile(out NtHandle fileHandle, out _, string.Empty, (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY | (AccessMask)DirectoryAccessMask.FILE_READ_ATTRIBUTES | AccessMask.SYNCHRONIZE, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
     GetFileSystemInformation(out result, fileHandle, informationClass);
     CloseFile(fileHandle);
 }
Exemplo n.º 8
0
 public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
 {
     result = null;
     return(NTStatus.STATUS_NOT_SUPPORTED);
 }
Exemplo n.º 9
0
        public NTStatus GetFileSystemInformation(out FileSystemInformation result, object handle, FileSystemInformationClass informationClass)
        {
            result = null;
            QueryInfoRequest request = new QueryInfoRequest();

            request.InfoType = InfoType.FileSystem;
            request.FileSystemInformationClass = informationClass;
            request.OutputBufferLength         = 4096;
            request.FileId = (FileID)handle;

            ulong       messageId = TrySendCommand(request);
            SMB2Command response  = m_client.WaitForCommand(SMB2CommandName.QueryInfo, messageId);

            if (response != null)
            {
                if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
                {
                    result = ((QueryInfoResponse)response).GetFileSystemInformation(informationClass);
                }
                return(response.Header.Status);
            }

            return(NTStatus.STATUS_INVALID_SMB);
        }
 /// <remarks>
 /// Support for pass-through Information Levels must be enabled.
 /// </remarks>
 public void SetFileSystemInformation(FileSystemInformation information)
 {
     FileSystemInformationClass = information.FileSystemInformationClass;
     InformationBytes           = information.GetBytes();
 }
 /// <remarks>
 /// Support for pass-through Information Levels must be enabled.
 /// </remarks>
 public FileSystemInformation GetFileSystemInformation(FileSystemInformationClass informationClass)
 {
     return(FileSystemInformation.GetFileSystemInformation(InformationBytes, 0, informationClass));
 }
Exemplo n.º 12
0
 public FileSystemInformation GetFileSystemInformation(FileSystemInformationClass informationClass)
 {
     return(FileSystemInformation.GetFileSystemInformation(OutputBuffer, 0, informationClass));
 }
Exemplo n.º 13
0
 public Task <(NTStatus status, FileSystemInformation result)> GetFileSystemInformationAsync(FileSystemInformationClass informationClass, CancellationToken cancellationToken)
 {
     return(Task.FromResult <(NTStatus, FileSystemInformation)>((NTStatus.STATUS_NOT_SUPPORTED, null)));
 }
        public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
        {
            switch (informationClass)
            {
            case FileSystemInformationClass.FileFsVolumeInformation:
            {
                FileFsVolumeInformation information = new FileFsVolumeInformation();
                information.SupportsObjects = false;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsSizeInformation:
            {
                FileFsSizeInformation information = new FileFsSizeInformation();
                information.TotalAllocationUnits     = m_fileSystem.Size / ClusterSize;
                information.AvailableAllocationUnits = m_fileSystem.FreeSpace / ClusterSize;
                information.SectorsPerAllocationUnit = ClusterSize / BytesPerSector;
                information.BytesPerSector           = BytesPerSector;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsDeviceInformation:
            {
                FileFsDeviceInformation information = new FileFsDeviceInformation();
                information.DeviceType      = DeviceType.Disk;
                information.Characteristics = DeviceCharacteristics.IsMounted;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsAttributeInformation:
            {
                FileFsAttributeInformation information = new FileFsAttributeInformation();
                information.FileSystemAttributes       = FileSystemAttributes.UnicodeOnDisk;
                information.MaximumComponentNameLength = 255;
                information.FileSystemName             = m_fileSystem.Name;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsControlInformation:
            {
                FileFsControlInformation information = new FileFsControlInformation();
                information.FileSystemControlFlags = FileSystemControlFlags.ContentIndexingDisabled;
                information.DefaultQuotaThreshold  = UInt64.MaxValue;
                information.DefaultQuotaLimit      = UInt64.MaxValue;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsFullSizeInformation:
            {
                FileFsFullSizeInformation information = new FileFsFullSizeInformation();
                information.TotalAllocationUnits           = m_fileSystem.Size / ClusterSize;
                information.CallerAvailableAllocationUnits = m_fileSystem.FreeSpace / ClusterSize;
                information.ActualAvailableAllocationUnits = m_fileSystem.FreeSpace / ClusterSize;
                information.SectorsPerAllocationUnit       = ClusterSize / BytesPerSector;
                information.BytesPerSector = BytesPerSector;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            case FileSystemInformationClass.FileFsObjectIdInformation:
            {
                result = null;
                // STATUS_INVALID_PARAMETER is returned when the file system does not implement object IDs
                // See: https://msdn.microsoft.com/en-us/library/cc232106.aspx
                return(NTStatus.STATUS_INVALID_PARAMETER);
            }

            case FileSystemInformationClass.FileFsSectorSizeInformation:
            {
                FileFsSectorSizeInformation information = new FileFsSectorSizeInformation();
                information.LogicalBytesPerSector = BytesPerSector;
                information.PhysicalBytesPerSectorForAtomicity   = BytesPerSector;
                information.PhysicalBytesPerSectorForPerformance = BytesPerSector;
                information.FileSystemEffectivePhysicalBytesPerSectorForAtomicity = BytesPerSector;
                information.ByteOffsetForSectorAlignment    = 0;
                information.ByteOffsetForPartitionAlignment = 0;
                result = information;
                return(NTStatus.STATUS_SUCCESS);
            }

            default:
            {
                result = null;
                return(NTStatus.STATUS_INVALID_INFO_CLASS);
            }
            }
        }
Exemplo n.º 15
0
        public static FileSystemInformation GetFileSystemInformation(byte[] buffer, int offset, FileSystemInformationClass informationClass)
        {
            switch (informationClass)
            {
            case FileSystemInformationClass.FileFsVolumeInformation:
                return(new FileFsVolumeInformation(buffer, offset));

            case FileSystemInformationClass.FileFsSizeInformation:
                return(new FileFsSizeInformation(buffer, offset));

            case FileSystemInformationClass.FileFsDeviceInformation:
                return(new FileFsDeviceInformation(buffer, offset));

            case FileSystemInformationClass.FileFsAttributeInformation:
                return(new FileFsAttributeInformation(buffer, offset));

            case FileSystemInformationClass.FileFsControlInformation:
                return(new FileFsControlInformation(buffer, offset));

            case FileSystemInformationClass.FileFsFullSizeInformation:
                return(new FileFsFullSizeInformation(buffer, offset));

            case FileSystemInformationClass.FileFsObjectIdInformation:
                return(new FileFsObjectIdInformation(buffer, offset));

            case FileSystemInformationClass.FileFsSectorSizeInformation:
                return(new FileFsSectorSizeInformation(buffer, offset));

            default:
                throw new UnsupportedInformationLevelException();
            }
        }
Exemplo n.º 16
0
        public async Task <(NTStatus status, FileSystemInformation result)> GetFileSystemInformationAsync(object handle, FileSystemInformationClass informationClass, CancellationToken cancellationToken)
        {
            QueryInfoRequest request = new QueryInfoRequest();

            request.InfoType = InfoType.FileSystem;
            request.FileSystemInformationClass = informationClass;
            request.OutputBufferLength         = 4096;
            request.FileId = (FileID)handle;

            await TrySendCommandAsync(request, cancellationToken);

            SMB2Command response = m_client.WaitForCommand(SMB2CommandName.QueryInfo);

            if (response != null)
            {
                FileSystemInformation result = null;
                if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
                {
                    result = ((QueryInfoResponse)response).GetFileSystemInformation(informationClass);
                }
                return(response.Header.Status, result);
            }

            return(NTStatus.STATUS_INVALID_SMB, null);
        }
Exemplo n.º 17
0
        public async Task <(NTStatus status, FileSystemInformation result)> GetFileSystemInformationAsync(FileSystemInformationClass informationClass, CancellationToken cancellationToken)
        {
            FileSystemInformation result = null;

            var(status, fileHandle, _) = await CreateFile(String.Empty, (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY | (AccessMask)DirectoryAccessMask.FILE_READ_ATTRIBUTES | AccessMask.SYNCHRONIZE, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null, cancellationToken);

            if (status != NTStatus.STATUS_SUCCESS)
            {
                return(status, result);
            }

            var(aStatus, aResult) = await GetFileSystemInformationAsync(fileHandle, informationClass, cancellationToken);
            await CloseFileAsync(fileHandle, cancellationToken);

            return(aStatus, aResult);
        }