コード例 #1
0
        /// <summary>
        /// Create NTTransSetQuota request for client to set quota on server. 
        /// </summary>
        /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
        /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "flags">
        /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
        /// </param>
        /// <param name = "flags2">
        /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
        /// various client and server capabilities. 
        /// </param>
        /// <param name = "fileId">the valid file id to operation on, response by server. </param>
        /// <param name = "nextEntryOffset">
        /// An offset to the start of the subsequent entry from the start of this entry, or 0 for the final entry. 
        /// </param>
        /// <param name = "changeTime">This value MUST be the time the quota was last changed, in TIME format. </param>
        /// <param name = "quotaUsed">
        /// The amount of quota, in bytes, used by this user. This field is formatted as a LARGE_INTEGER, as specified 
        ///  in [CIFS] section 2.4.2. 
        /// </param>
        /// <param name = "quotaThreshold">
        /// The quota warning limit, in bytes, for this user. This field is formatted as a LARGE_INTEGER, as specified 
        ///  in [CIFS] section 2.4.2. 
        /// </param>
        /// <param name = "quotaLimit">
        /// The quota limit, in bytes, for this user. This field is formatted as a LARGE_INTEGER, as specified in  
        /// [CIFS] section 2.4.2. 
        /// </param>
        /// <param name = "sid">
        /// The security identifier of this user. For details, see [MS-DTYP] section 2.4.2. Note that [CIFS] sections  
        /// 4.3.4, 4.3.4.7, 4.3.5, and 4.3.5.6 use Sid as the field name for a search handle. In [XOPEN-SMB], the  
        /// search handle field is called a findfirst_dirhandle or findnext_dirhandle. These are better field names 
        /// for a search handle. this param can not be null.
        /// </param>
        /// <returns>a nt transaction set quota request packet </returns>
        /// <exception cref="ArgumentNullException">sid can not be null.</exception>
        private SmbNtTransSetQuotaRequestPacket CreateNTTransSetQuotaRequest(
            ushort messageId,
            ushort sessionUid,
            ushort treeId,
            SmbHeader_Flags_Values flags,
            SmbHeader_Flags2_Values flags2,
            ushort fileId,
            uint nextEntryOffset,
            ulong changeTime,
            ulong quotaUsed,
            ulong quotaThreshold,
            ulong quotaLimit,
            byte[] sid)
        {
            if (sid == null)
            {
                throw new ArgumentNullException("sid");
            }

            SmbNtTransSetQuotaRequestPacket packet = new SmbNtTransSetQuotaRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = this.capability.MaxSetupCount;
            smbParameters.MaxParameterCount = this.capability.MaxParameterCount;
            smbParameters.MaxDataCount = this.capability.MaxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = (NtTransSubCommand)SmbNtTransSubCommand.NT_TRANSACT_SET_QUOTA;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set Nt Transaction Parameters
            NT_TRANSACT_SET_QUOTA_Request_NT_Trans_Parameters
                ntTransParameters = new NT_TRANSACT_SET_QUOTA_Request_NT_Trans_Parameters();
            ntTransParameters.Fid = fileId;

            // Set Nt Transaction Data
            NT_TRANSACT_SET_QUOTA_Request_NT_Trans_Data
                ntTransData = new NT_TRANSACT_SET_QUOTA_Request_NT_Trans_Data();
            ntTransData.NextEntryOffset = nextEntryOffset;
            ntTransData.SidLength = (uint)sid.Length;
            ntTransData.ChangeTime = changeTime;
            ntTransData.QuotaUsed = quotaUsed;
            ntTransData.QuotaThreshold = quotaThreshold;
            ntTransData.QuotaLimit = quotaLimit;
            ntTransData.Sid = sid;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #2
0
        /// <summary>
        /// to create a NtTransactSetSecurityDesc request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="securityInfoFields">ULONG Fields of security descriptor to be set</param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="securityInformation">The requested security descriptor structure. The self-relative form of
        /// a SECURITY_DESCRIPTOR is required.</param>
        /// <returns>a NtTransactSetSecurityDesc request packet</returns>
        public SmbNtTransactSetSecurityDescRequestPacket CreateNtTransactSetSecurityDescRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            ushort fid,
            NtTransactSecurityInformation securityInfoFields,
            RawSecurityDescriptor securityInformation)
        {
            SmbNtTransactSetSecurityDescRequestPacket packet = new SmbNtTransactSetSecurityDescRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_SET_SECURITY_DESC;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set NT_TransParameters
            NT_TRANSACT_SET_SECURITY_DESC_Request_NT_Trans_Parameters ntTransParameters =
                new NT_TRANSACT_SET_SECURITY_DESC_Request_NT_Trans_Parameters();
            ntTransParameters.FID = fid;
            ntTransParameters.SecurityInformation = securityInfoFields;

            // Set NT_Data
            NT_TRANSACT_SET_SECURITY_DESC_Request_NT_Trans_Data ntTransData =
                new NT_TRANSACT_SET_SECURITY_DESC_Request_NT_Trans_Data();
            ntTransData.SecurityInformation = securityInformation;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #3
0
        /// <summary>
        /// Create NTTransQueryQuota request for client to query quota on server. 
        /// </summary>
        /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
        /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "flags">
        /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
        /// </param>
        /// <param name = "flags2">
        /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
        /// various client and server capabilities. 
        /// </param>
        /// <param name = "fileId">the valid file id to operation on, response by server. </param>
        /// <param name = "isReturnSingleEntry">
        /// Indicates only a single entry is to be returned instead of filling the entire buffer. 
        /// </param>
        /// <param name = "isRestartScan">Indicates that the scan of the quota information is to be restarted. </param>
        /// <param name = "sidListLength">
        /// Supplies the length in bytes of the SidList (see below), or 0 if there is no SidList. 
        /// </param>
        /// <param name = "startSidLength">
        /// Supplies the length in bytes of the StartSid (see below), or 0 if there is no StartSid. MUST be ignored by 
        /// the receiver if SidListLength is non-zero. 
        /// </param>
        /// <param name = "startSidOffset">
        /// Supplies the offset, in bytes, to the StartSid in the Parameter buffer 
        /// </param>
        /// <returns>a nt transaction query quota request packet </returns>
        private SmbNtTransQueryQuotaRequestPacket CreateNTTransQueryQuotaRequest(
            ushort messageId,
            ushort sessionUid,
            ushort treeId,
            SmbHeader_Flags_Values flags,
            SmbHeader_Flags2_Values flags2,
            ushort fileId,
            bool isReturnSingleEntry,
            bool isRestartScan,
            int sidListLength,
            int startSidLength,
            int startSidOffset)
        {
            SmbNtTransQueryQuotaRequestPacket packet = new SmbNtTransQueryQuotaRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = this.capability.MaxSetupCount;
            smbParameters.MaxParameterCount = this.capability.MaxParameterCount;
            smbParameters.MaxDataCount = this.capability.MaxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = (NtTransSubCommand)SmbNtTransSubCommand.NT_TRANSACT_QUERY_QUOTA;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set Nt Transaction Parameters
            NT_TRANSACT_QUERY_QUOTA_Request_NT_Trans_Parameters
                ntTransParameters = new NT_TRANSACT_QUERY_QUOTA_Request_NT_Trans_Parameters();
            ntTransParameters.Fid = fileId;
            if (isReturnSingleEntry)
            {
                ntTransParameters.ReturnSingleEntry = 0x01;
            }
            if (isRestartScan)
            {
                ntTransParameters.RestartScan = 0x01;
            }
            ntTransParameters.SidListLength = (uint)sidListLength;
            ntTransParameters.StartSidLength = (uint)startSidLength;
            ntTransParameters.StartSidOffset = (uint)startSidOffset;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #4
0
        /// <summary>
        /// to create a NtTransactRenameRequest request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <returns>a NtTransactRenameRequest request packet</returns>
        public SmbNtTransactRenameRequestPacket CreateNtTransactRenameRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount)
        {
            SmbNtTransactRenameRequestPacket packet = new SmbNtTransactRenameRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_RENAME;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #5
0
        /// <summary>
        /// to create a NtTransactNotifyChange request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="filter">Specify the types of operations to monitor.</param>
        /// <param name="watchTree">BOOLEAN If all subdirectories are to be watched, then set this to TRUE; otherwise,
        /// FALSE.</param>
        /// <returns>a NtTransactNotifyChange request packet</returns>
        public SmbNtTransactNotifyChangeRequestPacket CreateNtTransactNotifyChangeRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            ushort fid,
            CompletionFilter filter,
            bool watchTree)
        {
            SmbNtTransactNotifyChangeRequestPacket packet = new SmbNtTransactNotifyChangeRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 4; // the cout of Setup is 4.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_NOTIFY_CHANGE;
            NT_TRANSACT_NOTIFY_SETUP setupStruct = new NT_TRANSACT_NOTIFY_SETUP();
            setupStruct.filter = filter;
            setupStruct.Fid = fid;
            setupStruct.WatchTree = (byte)(watchTree ? 1 : 0);
            smbParameters.Setup = CifsMessageUtils.ToTypeArray<ushort>(CifsMessageUtils.ToBytes<NT_TRANSACT_NOTIFY_SETUP
                >(setupStruct));
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #6
0
        /// <summary>
        /// to create a NtTransactIoctl request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="functionCode">Windows NT device or file system control code</param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="isFctl">This field is TRUE if the command is a file system control command and the FID is a
        /// file system control device. Otherwise, the command is a device control command and FID is an I/O
        /// device.</param>
        /// <param name="isFlags">If bit 0 is set, the command is to be applied to a share root handle. The share MUST
        /// be a Distributed File System (DFS) type</param>
        /// <param name="data">The raw bytes that are passed to the fsctl or ioctl function as the input
        /// buffer.</param>
        /// <returns>a NtTransactIoctl request packet</returns>
        public SmbNtTransactIoctlRequestPacket CreateNtTransactIoctlRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            uint functionCode,
            ushort fid,
            bool isFctl,
            byte isFlags,
            byte[] data)
        {
            if (data == null)
            {
                data = new byte[0];
            }

            SmbNtTransactIoctlRequestPacket packet = new SmbNtTransactIoctlRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 4; // the cout of Setup is 4.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_IOCTL;

            NT_TRANSACT_IOCTL_SETUP setupStruct = new NT_TRANSACT_IOCTL_SETUP();
            setupStruct.FunctionCode = functionCode;
            setupStruct.FID = fid;
            setupStruct.IsFctl = (byte)(isFctl ? 1 : 0);
            setupStruct.IsFlags = isFlags;
            smbParameters.Setup = CifsMessageUtils.ToTypeArray<ushort>(CifsMessageUtils.ToBytes<NT_TRANSACT_IOCTL_SETUP
                >(setupStruct));
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set NT_TransData
            NT_TRANSACT_IOCTL_Request_NT_Trans_Data ntTransData = new NT_TRANSACT_IOCTL_Request_NT_Trans_Data();
            ntTransData.Data = data;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }
コード例 #7
0
        /// <summary>
        /// to create a NtTransactCreate request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="ntTransParametersFlags">A 32-bit field containing a set of flags that modify the client
        /// request. Unused bits SHOULD be set to 0 by the client when sending a message and MUST be ignored when
        /// received by the server. </param>
        /// <param name="rootDirectoryFID">If nonzero, this value is the FID of an opened root directory, and the Name
        /// field MUST be handled as relative to the directory specified by this FID. If this value is zero the Name
        /// field MUST be handled as relative to the root of the share (the TID). The FID MUST have been acquired in a
        /// previous message exchange</param>
        /// <param name="desiredAccess">A 32-bit field containing standard, specific, and generic access rights</param>
        /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
        /// bytes</param>
        /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
        /// behavior flag values.</param>
        /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other processes.
        /// </param>
        /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
        /// exists or if the file is a new file and does not already exist.</param>
        /// <param name="createOptions">ULONG A 32-bit field containing flag options to use if creating the file or
        /// directory. This field MUST be set to 0 or a combination of the following possible values</param>
        /// <param name="impersonationLevel">ULONG A value that indicates what security context the server SHOULD use
        /// when executing the command on behalf of the client</param>
        /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking mode.</param>
        /// <param name="name">The name of the file; not null-terminated.  </param>
        /// <param name="securityDescriptor">The security descriptor to use when requesting access to the file</param>
        /// <param name="extendedAttributes">The list of extended attributes that SHOULD be applied to the new file.
        /// </param>
        /// <returns>a NtTransactCreate request packet</returns>
        public SmbNtTransactCreateRequestPacket CreateNtTransactCreateRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            NtTransactFlags ntTransParametersFlags,
            uint rootDirectoryFID,
            NtTransactDesiredAccess desiredAccess,
            ulong allocationSize,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            NtTransactSecurityFlags securityFlags,
            string name,
            RawSecurityDescriptor securityDescriptor,
            FILE_FULL_EA_INFORMATION[] extendedAttributes)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            if (extendedAttributes == null)
            {
                extendedAttributes = new FILE_FULL_EA_INFORMATION[0];
            }

            SmbNtTransactCreateRequestPacket packet = new SmbNtTransactCreateRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_CREATE;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set NT_TransParameters
            NT_TRANSACT_CREATE_Request_NT_Trans_Parameters ntTransParameters =
                new NT_TRANSACT_CREATE_Request_NT_Trans_Parameters();
            ntTransParameters.Flags = ntTransParametersFlags;
            ntTransParameters.RootDirectoryFID = rootDirectoryFID;
            ntTransParameters.DesiredAccess = desiredAccess;
            ntTransParameters.AllocationSize = allocationSize;
            if (createDisposition == NtTransactCreateDisposition.FILE_OPEN
                || createDisposition == NtTransactCreateDisposition.NONE
                || (extFileAttributes & SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
                == SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
            {
                ntTransParameters.AllocationSize = 0;
            }

            ntTransParameters.ExtFileAttributes = extFileAttributes;
            ntTransParameters.ShareAccess = shareAccess;
            ntTransParameters.CreateDisposition = createDisposition;
            ntTransParameters.CreateOptions = createOptions;
            ntTransParameters.SecurityDescriptorLength = securityDescriptor == null
                ? 0 : (uint)securityDescriptor.BinaryLength;
            ntTransParameters.ImpersonationLevel = impersonationLevel;
            ntTransParameters.SecurityFlags = securityFlags;

            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
            {
                ntTransParameters.Name = Encoding.Unicode.GetBytes(name);
            }
            else
            {
                ntTransParameters.Name = Encoding.ASCII.GetBytes(name);
            }
            ntTransParameters.NameLength = (uint)ntTransParameters.Name.Length;

            // The size of the preceding SmbParameters part plus Header is an odd number for all cifs messages
            // Use ntTransParameters field to judge whether needs to add one 16-bits align pad.
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE &&
                CifsMessageUtils.GetSize<NT_TRANSACT_CREATE_Request_NT_Trans_Parameters>(ntTransParameters) % twoBytesAlign == 0)
            {
                // pad 1 byte for 16-bits align:
                smbData.Pad2 = new byte[1];
            }
            else
            {
                smbData.Pad2 = new byte[0];
            }

            // Set NT_TransData
            NT_TRANSACT_CREATE_Request_NT_Trans_Data ntTransData = new NT_TRANSACT_CREATE_Request_NT_Trans_Data();
            ntTransData.ExtendedAttributes = extendedAttributes;

            for (int i = 0; i < ntTransData.ExtendedAttributes.Length; i++)
            {
                int eaNameLength = ntTransData.ExtendedAttributes[i].EaName == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaName.Length;
                int eaValueLength = ntTransData.ExtendedAttributes[i].EaValue == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaValue.Length;
                uint currentLength = (uint)(EA.FULL_EA_FIXED_SIZE + eaNameLength + eaValueLength);
                uint alignPad = (fourBytesAlign - currentLength % fourBytesAlign) % fourBytesAlign;

                if (i != ntTransData.ExtendedAttributes.Length - 1)
                {
                    currentLength += alignPad;
                    ntTransData.ExtendedAttributes[i].NextEntryOffset = currentLength;
                }
                ntTransParameters.EALength += currentLength;
            }

            ntTransData.SecurityDescriptor = securityDescriptor;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }
 /// <summary>
 /// to decode the smb data: from the general SmbData to the concrete SmbData.
 /// </summary>
 protected override void DecodeData()
 {
     this.smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();
     using (MemoryStream memoryStream = new MemoryStream(CifsMessageUtils.ToBytes<SmbData>(this.smbDataBlock)))
     {
         using (Channel channel = new Channel(null, memoryStream))
         {
             this.smbData.ByteCount = channel.Read<ushort>();
             this.smbData.Pad1 = channel.ReadBytes((int)(this.smbParameters.ParameterOffset - this.HeaderSize
                 - this.smbParameters.WordCount * 2 - SmbComTransactionPacket.SmbParametersWordCountLength
                 - SmbComTransactionPacket.SmbDataByteCountLength));
             this.smbData.NT_Trans_Parameters = channel.ReadBytes((int)this.smbParameters.ParameterCount);
             if (this.smbParameters.DataCount > 0)
             {
                 this.smbData.Pad2 = channel.ReadBytes((int)(this.smbParameters.DataOffset
                     - this.smbParameters.ParameterOffset - this.smbParameters.ParameterCount));
                 this.smbData.NT_Trans_Data = channel.ReadBytes((int)this.smbParameters.DataCount);
             }
             else
             {
                 this.smbData.Pad2 = new byte[0];
                 this.smbData.NT_Trans_Data = new byte[0];
             }
         }
         this.DecodeNtTransParameters();
         this.DecodeNtTransData();
     }
 }