Esempio n. 1
0
        public static List <string> ListShares(INtFileStore namedPipeShare, ShareType?shareType)
        {
            NamedPipeHelper.BindPipe(namedPipeShare, ServerService.ServicePipeName, ServerService.ServiceInterfaceGuid, ServerService.ServiceVersion, out NtHandle? pipeHandle, out int maxTransmitFragmentSize).IsSuccessElseThrow();

            NetrShareEnumRequest shareEnumRequest = new NetrShareEnumRequest
            {
                InfoStruct = new ShareEnum
                {
                    Level = 1,
                    Info  = new ShareInfo1Container()
                },
                PreferedMaximumLength = uint.MaxValue,
                ServerName            = "*"
            };

            byte[]     data       = shareEnumRequest.GetBytes();
            RequestPDU requestPdu = new RequestPDU
            {
                Flags = PacketFlags.FirstFragment | PacketFlags.LastFragment,
                DataRepresentation =
                {
                    CharacterFormat             = CharacterFormat.ASCII,
                    ByteOrder                   = ByteOrder.LittleEndian,
                    FloatingPointRepresentation = FloatingPointRepresentation.IEEE
                },
                OpNum          = (ushort)ServerServiceOpName.NetrShareEnum,
                Data           = data,
                AllocationHint = (uint)data.Length
            };

            byte[] input = requestPdu.GetBytes();
            namedPipeShare.DeviceIOControl(pipeHandle, (uint)IoControlCode.FSCTL_PIPE_TRANSCEIVE, input, out byte[]? output, maxTransmitFragmentSize);

            if (!(RPCPDU.GetPDU(output, 0) is ResponsePDU responsePdu))
            {
                throw new NtStatusException(NTStatus.STATUS_NOT_SUPPORTED);
            }

            byte[] responseData = responsePdu.Data;
            while ((responsePdu.Flags & PacketFlags.LastFragment) == 0)
            {
                namedPipeShare.ReadFile(out output, pipeHandle, 0, maxTransmitFragmentSize);

                if (!(RPCPDU.GetPDU(output, 0) is ResponsePDU responsePdu2))
                {
                    throw new NtStatusException(NTStatus.STATUS_NOT_SUPPORTED);
                }
                responseData = ByteUtils.Concatenate(responseData, responsePdu2.Data);
            }
            namedPipeShare.CloseFile(pipeHandle);
            NetrShareEnumResponse shareEnumResponse = new NetrShareEnumResponse(responseData);

            if (shareEnumResponse.InfoStruct.Info is ShareInfo1Container shareInfo1 && shareInfo1.Entries != null)
            {
                return((from entry in shareInfo1.Entries where !shareType.HasValue || shareType.Value == entry.ShareType.ShareType select entry.NetName.Value).ToList());
            }

            throw new NtStatusException(shareEnumResponse.Result == Win32Error.ERROR_ACCESS_DENIED ? NTStatus.STATUS_ACCESS_DENIED : NTStatus.STATUS_NOT_SUPPORTED);
        }
Esempio n. 2
0
        public static FileNetworkOpenInformation?GetNetworkOpenInformation(INtFileStore fileStore, string path, SecurityContext securityContext)
        {
            fileStore.CreateFile(out NtHandle handle, out _, path, (AccessMask)FileAccessMask.FILE_READ_ATTRIBUTES, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, 0, securityContext);

            fileStore.GetFileInformation(out FileInformation? fileInfo, handle, FileInformationClass.FileNetworkOpenInformation);
            fileStore.CloseFile(handle);
            return((FileNetworkOpenInformation?)fileInfo);
        }
Esempio n. 3
0
        public static NTStatus BindPipe(INtFileStore namedPipeShare, string pipeName, Guid interfaceGuid, uint interfaceVersion, out NtHandle?pipeHandle, out int maxTransmitFragmentSize)
        {
            maxTransmitFragmentSize = 0;
            namedPipeShare.CreateFile(out pipeHandle, out _, pipeName, (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA), 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, 0, null);

            BindPDU bindPdu = new BindPDU
            {
                Flags = PacketFlags.FirstFragment | PacketFlags.LastFragment,
                DataRepresentation =
                {
                    CharacterFormat             = CharacterFormat.ASCII,
                    ByteOrder                   = ByteOrder.LittleEndian,
                    FloatingPointRepresentation = FloatingPointRepresentation.IEEE
                },
                MaxTransmitFragmentSize = 5680,
                MaxReceiveFragmentSize  = 5680
            };

            ContextElement serviceContext = new ContextElement
            {
                AbstractSyntax = new SyntaxID(interfaceGuid, interfaceVersion)
            };

            serviceContext.TransferSyntaxList.Add(new SyntaxID(RemoteServiceHelper.NDRTransferSyntaxIdentifier, RemoteServiceHelper.NDRTransferSyntaxVersion));

            bindPdu.ContextList.Add(serviceContext);

            byte[] input = bindPdu.GetBytes();
            namedPipeShare.DeviceIOControl(pipeHandle, (uint)IoControlCode.FSCTL_PIPE_TRANSCEIVE, input, out byte[]? output, 4096);

            if (!(RPCPDU.GetPDU(output, 0) is BindAckPDU bindAckPDU))
            {
                return(NTStatus.STATUS_NOT_SUPPORTED);
            }

            maxTransmitFragmentSize = bindAckPDU.MaxTransmitFragmentSize;
            return(NTStatus.STATUS_SUCCESS);
        }
Esempio n. 4
0
 public static FileNetworkOpenInformation?GetNetworkOpenInformation(INtFileStore fileStore, NtHandle handle)
 {
     fileStore.GetFileInformation(out FileInformation? fileInfo, handle, FileInformationClass.FileNetworkOpenInformation);
     return((FileNetworkOpenInformation?)fileInfo);
 }
Esempio n. 5
0
 public NTFileStoreTests(INtFileStore fileStore)
 {
     m_fileStore = fileStore;
     m_fileStore.CreateFile(out NtHandle handle, out _, TestDirName, AccessMask.GENERIC_ALL, FileAttributes.Directory, ShareAccess.Read, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_DIRECTORY_FILE, null);
     m_fileStore.CloseFile(handle);
 }