/// <summary> /// Get lease context according to leasekey and leasestate /// </summary> private Smb2CreateContextRequest[] GetLeaseContext( OplockLeaseType oplockLeaseType, Guid leaseKey, LeaseStateValues leaseState) { Smb2CreateContextRequest[] leaseContext = new Smb2CreateContextRequest[] { }; if (oplockLeaseType == OplockLeaseType.LeaseV1) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_REQUEST_LEASE); leaseContext = leaseContext.Append( new Smb2CreateRequestLease { LeaseKey = leaseKey, LeaseState = leaseState }); } if (oplockLeaseType == OplockLeaseType.LeaseV2) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_REQUEST_LEASE_V2); leaseContext = leaseContext.Append( new Smb2CreateRequestLeaseV2 { LeaseKey = leaseKey, LeaseState = leaseState }); } return(leaseContext); }
/// <summary> /// Get handle context for PrepareOpen /// </summary> private Smb2CreateContextRequest[] GetPrepareOpenHandleContext( ModelHandleType modelHandleType) { Smb2CreateContextRequest[] handleContext = new Smb2CreateContextRequest[] { }; switch (modelHandleType) { case ModelHandleType.DurableHandleV1: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST); createGuid = Guid.Empty; handleContext = handleContext.Append(new Smb2CreateDurableHandleRequest { DurableRequest = createGuid }); return(handleContext); } case ModelHandleType.DurableHandleV2: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); createGuid = Guid.NewGuid(); handleContext = handleContext.Append(new Smb2CreateDurableHandleRequestV2 { CreateGuid = createGuid }); return(handleContext); } case ModelHandleType.PersistentHandle: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); createGuid = Guid.NewGuid(); handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = createGuid, Flags = CREATE_DURABLE_HANDLE_REQUEST_V2_Flags.DHANDLE_FLAG_PERSISTENT, }); return(handleContext); } default: throw new InvalidOperationException("Unsupported handle type"); } }
/// <summary> /// Get create context for OpenFile /// </summary> private Smb2CreateContextRequest[] GetOpenFileHandleContext( DurableV1RequestContext durableV1RequestContext, DurableV2RequestContext durableV2RequestContext, DurableV1ReconnectContext durableV1ReconnectContext, DurableV2ReconnectContext durableV2ReconnectContext, bool isSameCreateGuid) { Smb2CreateContextRequest[] handleContext = new Smb2CreateContextRequest[] { }; if (durableV1RequestContext == DurableV1RequestContext.DurableV1RequestContextExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST); handleContext = handleContext.Append( new Smb2CreateDurableHandleRequest { DurableRequest = isSameCreateGuid ? Guid.Empty : Guid.NewGuid(), }); } if (durableV2RequestContext != DurableV2RequestContext.DurableV2RequestContextNotExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); if (durableV2RequestContext == DurableV2RequestContext.DurableV2RequestContextExistWithoutPersistent) { handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), }); } else { handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), Flags = CREATE_DURABLE_HANDLE_REQUEST_V2_Flags.DHANDLE_FLAG_PERSISTENT, }); } } if (durableV1ReconnectContext == DurableV1ReconnectContext.DurableV1ReconnectContextExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_RECONNECT); handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnect { Data = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } if (durableV2ReconnectContext != DurableV2ReconnectContext.DurableV2ReconnectContextNotExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2); if (durableV2ReconnectContext == DurableV2ReconnectContext.DurableV2ReconnectContextExistWithoutPersistent) { handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnectV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), FileId = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } else { handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnectV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), Flags = CREATE_DURABLE_HANDLE_RECONNECT_V2_Flags.DHANDLE_FLAG_PERSISTENT, FileId = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } } return(handleContext); }
/// <summary> /// Get handle context for PrepareOpen /// </summary> private Smb2CreateContextRequest[] GetPrepareOpenHandleContext( ModelHandleType modelHandleType) { Smb2CreateContextRequest[] handleContext = new Smb2CreateContextRequest[] { }; switch (modelHandleType) { case ModelHandleType.DurableHandleV1: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST); createGuid = Guid.Empty; handleContext = handleContext.Append(new Smb2CreateDurableHandleRequest { DurableRequest = createGuid }); return handleContext; } case ModelHandleType.DurableHandleV2: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); createGuid = Guid.NewGuid(); handleContext = handleContext.Append(new Smb2CreateDurableHandleRequestV2 { CreateGuid = createGuid }); return handleContext; } case ModelHandleType.PersistentHandle: { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); createGuid = Guid.NewGuid(); handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = createGuid, Flags = CREATE_DURABLE_HANDLE_REQUEST_V2_Flags.DHANDLE_FLAG_PERSISTENT, }); return handleContext; } default: throw new InvalidOperationException("Unsupported handle type"); } }
/// <summary> /// Get create context for OpenFile /// </summary> private Smb2CreateContextRequest[] GetOpenFileHandleContext( DurableV1RequestContext durableV1RequestContext, DurableV2RequestContext durableV2RequestContext, DurableV1ReconnectContext durableV1ReconnectContext, DurableV2ReconnectContext durableV2ReconnectContext, bool isSameCreateGuid) { Smb2CreateContextRequest[] handleContext = new Smb2CreateContextRequest[] { }; if (durableV1RequestContext == DurableV1RequestContext.DurableV1RequestContextExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST); handleContext = handleContext.Append( new Smb2CreateDurableHandleRequest { DurableRequest = isSameCreateGuid ? Guid.Empty : Guid.NewGuid(), }); } if (durableV2RequestContext != DurableV2RequestContext.DurableV2RequestContextNotExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2); if (durableV2RequestContext == DurableV2RequestContext.DurableV2RequestContextExistWithoutPersistent) { handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), }); } else { handleContext = handleContext.Append( new Smb2CreateDurableHandleRequestV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), Flags = CREATE_DURABLE_HANDLE_REQUEST_V2_Flags.DHANDLE_FLAG_PERSISTENT, }); } } if (durableV1ReconnectContext == DurableV1ReconnectContext.DurableV1ReconnectContextExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_RECONNECT); handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnect { Data = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } if (durableV2ReconnectContext != DurableV2ReconnectContext.DurableV2ReconnectContextNotExist) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2); if (durableV2ReconnectContext == DurableV2ReconnectContext.DurableV2ReconnectContextExistWithoutPersistent) { handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnectV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), FileId = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } else { handleContext = handleContext.Append( new Smb2CreateDurableHandleReconnectV2 { CreateGuid = isSameCreateGuid ? createGuid : Guid.NewGuid(), Flags = CREATE_DURABLE_HANDLE_RECONNECT_V2_Flags.DHANDLE_FLAG_PERSISTENT, FileId = new FILEID { Persistent = fileIdBeforDisconnection.Persistent } }); } } return handleContext; }
/// <summary> /// Get lease context according to leasekey and leasestate /// </summary> private Smb2CreateContextRequest[] GetLeaseContext( OplockLeaseType oplockLeaseType, Guid leaseKey, LeaseStateValues leaseState) { Smb2CreateContextRequest[] leaseContext = new Smb2CreateContextRequest[] { }; if (oplockLeaseType == OplockLeaseType.LeaseV1) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_REQUEST_LEASE); leaseContext = leaseContext.Append( new Smb2CreateRequestLease { LeaseKey = leaseKey, LeaseState = leaseState }); } if (oplockLeaseType == OplockLeaseType.LeaseV2) { testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_REQUEST_LEASE_V2); leaseContext = leaseContext.Append( new Smb2CreateRequestLeaseV2 { LeaseKey = leaseKey, LeaseState = leaseState }); } return leaseContext; }
/// <summary> /// Concat two contexts arrays /// </summary> private Smb2CreateContextRequest[] ConcatContexts( Smb2CreateContextRequest[] firstContext, Smb2CreateContextRequest[] secondContext) { foreach (var context in secondContext) { firstContext = firstContext.Append(context); } return firstContext; }
public void CreateRequest( CreateFileNameType fileNameType, CreateOptionsFileOpenReparsePointType fileOpenReparsePointType, CreateOptionsFileDeleteOnCloseType fileDeleteOnCloseType, CreateContextType contextType, ImpersonationLevelType impersonationType, CreateFileType fileType) { #region Header Packet_Header_Flags_Values headerFlag = testConfig.SendSignedRequest ? Packet_Header_Flags_Values.FLAGS_SIGNED : Packet_Header_Flags_Values.NONE; #endregion #region File Name string fileName = GetFileName(fileNameType); #endregion #region CreateOptions CreateOptions_Values createOptions = fileType == CreateFileType.DirectoryFile ? CreateOptions_Values.FILE_DIRECTORY_FILE : CreateOptions_Values.FILE_NON_DIRECTORY_FILE; if (fileOpenReparsePointType == CreateOptionsFileOpenReparsePointType.FileOpenReparsePointSet) { createOptions |= CreateOptions_Values.FILE_OPEN_REPARSE_POINT; } if (fileDeleteOnCloseType == CreateOptionsFileDeleteOnCloseType.FileDeteteOnCloseSet) { createOptions |= CreateOptions_Values.FILE_DELETE_ON_CLOSE; } #endregion #region CreateContexts Smb2CreateContextRequest[] createContexts = new Smb2CreateContextRequest[] { }; Create_ContextType = contextType; switch (contextType) { case CreateContextType.NoCreateContext: break; case CreateContextType.InvalidCreateContext: case CreateContextType.InvalidCreateContextSize: testClient.BeforeSendingPacket(ReplacePacketByInvalidCreateContext); break; case CreateContextType.ValidCreateContext: testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_QUERY_ON_DISK_ID); createContexts = createContexts.Append(new Smb2CreateQueryOnDiskId()); break; default: throw new ArgumentException("contextType"); } #endregion #region ImpersonationLevel ImpersonationLevel_Values impersonation = ImpersonationLevel_Values.Impersonation; if (impersonationType == ImpersonationLevelType.InvalidImpersonationLevel) { impersonation = (ImpersonationLevel_Values)0x00000004; //Non-existed impersonation level } #endregion #region DesiredAccess AccessMask accessMask = AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE | AccessMask.DELETE; #endregion Smb2CreateContextResponse[] contextResponse; uint status = testClient.Create( treeId, fileName, createOptions, headerFlag, out fileID, out contextResponse, createContexts: createContexts, accessMask: accessMask, checker: (header, response) => { }, impersonationLevel: impersonation); CreateResponse((ModelSmb2Status)status, createCloseConfig); }
public void CreateRequest( CreateFileNameType fileNameType, CreateOptionsFileOpenReparsePointType fileOpenReparsePointType, CreateOptionsFileDeleteOnCloseType fileDeleteOnCloseType, CreateContextType contextType, ImpersonationLevelType impersonationType, CreateFileType fileType) { #region Header Packet_Header_Flags_Values headerFlag = testConfig.SendSignedRequest ? Packet_Header_Flags_Values.FLAGS_SIGNED : Packet_Header_Flags_Values.NONE; #endregion #region File Name string fileName = GetFileName(fileNameType); #endregion #region CreateOptions CreateOptions_Values createOptions = fileType == CreateFileType.DirectoryFile ? CreateOptions_Values.FILE_DIRECTORY_FILE : CreateOptions_Values.FILE_NON_DIRECTORY_FILE; if (fileOpenReparsePointType == CreateOptionsFileOpenReparsePointType.FileOpenReparsePointSet) createOptions |= CreateOptions_Values.FILE_OPEN_REPARSE_POINT; if (fileDeleteOnCloseType == CreateOptionsFileDeleteOnCloseType.FileDeteteOnCloseSet) createOptions |= CreateOptions_Values.FILE_DELETE_ON_CLOSE; #endregion #region CreateContexts Smb2CreateContextRequest[] createContexts = new Smb2CreateContextRequest[] { }; Create_ContextType = contextType; switch (contextType) { case CreateContextType.NoCreateContext: break; case CreateContextType.InvalidCreateContext: case CreateContextType.InvalidCreateContextSize: testClient.BeforeSendingPacket(ReplacePacketByInvalidCreateContext); break; case CreateContextType.ValidCreateContext: testConfig.CheckCreateContext(CreateContextTypeValue.SMB2_CREATE_QUERY_ON_DISK_ID); createContexts = createContexts.Append(new Smb2CreateQueryOnDiskId()); break; default: throw new ArgumentException("contextType"); } #endregion #region ImpersonationLevel ImpersonationLevel_Values impersonation = ImpersonationLevel_Values.Impersonation; if (impersonationType == ImpersonationLevelType.InvalidImpersonationLevel) { impersonation = (ImpersonationLevel_Values)0x00000004; //Non-existed impersonation level } #endregion #region DesiredAccess AccessMask accessMask = AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE | AccessMask.DELETE; #endregion Smb2CreateContextResponse[] contextResponse; uint status = testClient.Create( treeId, fileName, createOptions, headerFlag, out fileID, out contextResponse, createContexts: createContexts, accessMask: accessMask, checker: (header, response) => { }, impersonationLevel: impersonation); CreateResponse((ModelSmb2Status)status, createCloseConfig); }