SMB1FileStore, SMB2FileStore: Return STATUS_IO_TIMEOUT instead of STATUS_INVALID_SMB when server does not reply and there is no protocol violation

This commit is contained in:
Tal Aloni 2024-06-12 12:27:26 +03:00
parent d8b1791212
commit 7ecc9e667a
2 changed files with 54 additions and 54 deletions

View file

@ -36,7 +36,7 @@ namespace SMBLibrary.Client
request.ImpersonationLevel = ImpersonationLevel.Impersonation;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_CREATE_ANDX);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_CREATE_ANDX, out bool connectionTerminated);
if (reply != null)
{
if (reply.Commands[0] is NTCreateAndXResponse)
@ -51,7 +51,7 @@ namespace SMBLibrary.Client
return reply.Header.Status;
}
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus CloseFile(object handle)
@ -59,12 +59,12 @@ namespace SMBLibrary.Client
CloseRequest request = new CloseRequest();
request.FID = (ushort)handle;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_CLOSE);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_CLOSE, out bool connectionTerminated);
if (reply != null)
{
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
@ -76,7 +76,7 @@ namespace SMBLibrary.Client
request.MaxCountLarge = (uint)maxCount;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_READ_ANDX);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_READ_ANDX, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is ReadAndXResponse)
@ -85,7 +85,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
@ -97,7 +97,7 @@ namespace SMBLibrary.Client
request.Data = data;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_WRITE_ANDX);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_WRITE_ANDX, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is WriteAndXResponse)
@ -106,7 +106,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus FlushFileBuffers(object handle)
@ -150,7 +150,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
@ -180,10 +180,10 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out connectionTerminated);
if (reply == null)
{
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
else if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
{
@ -201,7 +201,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
@ -224,7 +224,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
@ -244,7 +244,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
else
{
@ -277,7 +277,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
@ -288,7 +288,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetFileInformation(object handle, FileInformation information)
@ -319,12 +319,12 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
else
{
@ -349,12 +349,12 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
@ -376,7 +376,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
@ -387,7 +387,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
else
{
@ -412,7 +412,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (ushort)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
@ -423,7 +423,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetFileSystemInformation(FileSystemInformation information)
@ -450,7 +450,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (uint)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
@ -461,7 +461,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
@ -504,7 +504,7 @@ namespace SMBLibrary.Client
request.MaxDataCount = (uint)maxOutputLength;
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
@ -515,7 +515,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus FsCtlPipeTranscieve(object handle, byte[] input, out byte[] output, int maxOutputLength)
@ -536,7 +536,7 @@ namespace SMBLibrary.Client
request.Name = @"\PIPE\";
TrySendMessage(request);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION);
SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION, out bool connectionTerminated);
if (reply != null)
{
if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is TransactionResponse)
@ -547,7 +547,7 @@ namespace SMBLibrary.Client
}
return reply.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus Disconnect()

View file

@ -40,7 +40,7 @@ namespace SMBLibrary.Client
request.ImpersonationLevel = ImpersonationLevel.Impersonation;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is CreateResponse)
@ -52,7 +52,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus CloseFile(object handle)
@ -60,13 +60,13 @@ namespace SMBLibrary.Client
CloseRequest request = new CloseRequest();
request.FileId = (FileID)handle;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
@ -79,7 +79,7 @@ namespace SMBLibrary.Client
request.ReadLength = (uint)maxCount;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is ReadResponse)
@ -89,7 +89,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
@ -102,7 +102,7 @@ namespace SMBLibrary.Client
request.Data = data;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is WriteResponse)
@ -112,7 +112,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus FlushFileBuffers(object handle)
@ -121,7 +121,7 @@ namespace SMBLibrary.Client
request.FileId = (FileID) handle;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is FlushResponse)
@ -130,7 +130,7 @@ namespace SMBLibrary.Client
}
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus LockFile(object handle, long byteOffset, long length, bool exclusiveLock)
@ -155,7 +155,7 @@ namespace SMBLibrary.Client
request.FileName = fileName;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
while (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryDirectoryResponse)
@ -164,16 +164,16 @@ namespace SMBLibrary.Client
result.AddRange(page);
request.Reopen = false;
TrySendCommand(request);
response = m_client.WaitForCommand(request.MessageID);
response = m_client.WaitForCommand(request.MessageID, out connectionTerminated);
if (response == null)
{
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
}
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
@ -186,7 +186,7 @@ namespace SMBLibrary.Client
request.FileId = (FileID)handle;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
@ -196,7 +196,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetFileInformation(object handle, FileInformation information)
@ -208,13 +208,13 @@ namespace SMBLibrary.Client
request.SetFileInformation(information);
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
@ -243,7 +243,7 @@ namespace SMBLibrary.Client
request.FileId = (FileID)handle;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
@ -253,7 +253,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetFileSystemInformation(FileSystemInformation information)
@ -271,7 +271,7 @@ namespace SMBLibrary.Client
request.FileId = (FileID)handle;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
@ -281,7 +281,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
@ -310,7 +310,7 @@ namespace SMBLibrary.Client
request.Input = input;
request.MaxOutputResponse = (uint)maxOutputLength;
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(request.MessageID);
SMB2Command response = m_client.WaitForCommand(request.MessageID, out bool connectionTerminated);
if (response != null)
{
if ((response.Header.Status == NTStatus.STATUS_SUCCESS || response.Header.Status == NTStatus.STATUS_BUFFER_OVERFLOW) && response is IOCtlResponse)
@ -320,7 +320,7 @@ namespace SMBLibrary.Client
return response.Header.Status;
}
return NTStatus.STATUS_INVALID_SMB;
return connectionTerminated ? NTStatus.STATUS_INVALID_SMB : NTStatus.STATUS_IO_TIMEOUT;
}
public NTStatus Disconnect()