NTTransactHelper.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. public class NTTransactHelper
  15. {
  16. /// <summary>
  17. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  18. /// </summary>
  19. internal static SMB1Command GetNTTransactResponse(SMB1Header header, NTTransactRequest request, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  20. {
  21. if (request.TransParameters.Length < request.TotalParameterCount ||
  22. request.TransData.Length < request.TotalDataCount)
  23. {
  24. ProcessStateObject processState = state.ObtainProcessState(header.PID);
  25. // A secondary transaction request is pending
  26. processState.SubcommandID = (ushort)request.Function;
  27. processState.TransactionSetup = request.Setup;
  28. processState.TransactionParameters = new byte[request.TotalParameterCount];
  29. processState.TransactionData = new byte[request.TotalDataCount];
  30. ByteWriter.WriteBytes(processState.TransactionParameters, 0, request.TransParameters);
  31. ByteWriter.WriteBytes(processState.TransactionData, 0, request.TransData);
  32. processState.TransactionParametersReceived += request.TransParameters.Length;
  33. processState.TransactionDataReceived += request.TransData.Length;
  34. return new NTTransactInterimResponse();
  35. }
  36. else
  37. {
  38. // We have a complete command
  39. return GetCompleteNTTransactResponse(header, request.Function, request.Setup, request.TransParameters, request.TransData, share, state, sendQueue);
  40. }
  41. }
  42. /// <summary>
  43. /// There are no secondary response messages.
  44. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  45. /// </summary>
  46. internal static SMB1Command GetNTTransactResponse(SMB1Header header, NTTransactSecondaryRequest request, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  47. {
  48. ProcessStateObject processState = state.GetProcessState(header.PID);
  49. if (processState == null)
  50. {
  51. throw new InvalidRequestException();
  52. }
  53. ByteWriter.WriteBytes(processState.TransactionParameters, (int)request.ParameterDisplacement, request.TransParameters);
  54. ByteWriter.WriteBytes(processState.TransactionData, (int)request.DataDisplacement, request.TransData);
  55. processState.TransactionParametersReceived += request.TransParameters.Length;
  56. processState.TransactionDataReceived += request.TransData.Length;
  57. if (processState.TransactionParametersReceived < processState.TransactionParameters.Length ||
  58. processState.TransactionDataReceived < processState.TransactionData.Length)
  59. {
  60. return null;
  61. }
  62. else
  63. {
  64. // We have a complete command
  65. return GetCompleteNTTransactResponse(header, (NTTransactSubcommandName)processState.SubcommandID, processState.TransactionSetup, processState.TransactionParameters, processState.TransactionData, share, state, sendQueue);
  66. }
  67. }
  68. internal static SMB1Command GetCompleteNTTransactResponse(SMB1Header header, NTTransactSubcommandName subcommandName, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  69. {
  70. NTTransactSubcommand subcommand = NTTransactSubcommand.GetSubcommandRequest(subcommandName, requestSetup, requestParameters, requestData, header.UnicodeFlag);
  71. NTTransactSubcommand subcommandResponse = null;
  72. if (subcommand is NTTransactCreateRequest)
  73. {
  74. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  75. }
  76. else if (subcommand is NTTransactIOCTLRequest)
  77. {
  78. subcommandResponse = GetSubcommandResponse(header, (NTTransactIOCTLRequest)subcommand, share, state);
  79. }
  80. else if (subcommand is NTTransactSetSecurityDescriptor)
  81. {
  82. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  83. }
  84. else if (subcommand is NTTransactNotifyChangeRequest)
  85. {
  86. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  87. }
  88. else if (subcommand is NTTransactQuerySecurityDescriptorRequest)
  89. {
  90. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  91. }
  92. else
  93. {
  94. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  95. }
  96. if (header.Status != NTStatus.STATUS_SUCCESS)
  97. {
  98. return new ErrorResponse(CommandName.SMB_COM_NT_TRANSACT);
  99. }
  100. byte[] responseSetup = subcommandResponse.GetSetup();
  101. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  102. byte[] responseData = subcommandResponse.GetData();
  103. NTTransactResponse response = new NTTransactResponse();
  104. PrepareResponse(response, responseSetup, responseParameters, responseData, state.MaxBufferSize, sendQueue);
  105. return response;
  106. }
  107. private static NTTransactIOCTLResponse GetSubcommandResponse(SMB1Header header, NTTransactIOCTLRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  108. {
  109. SMB1Session session = state.GetSession(header.UID);
  110. NTTransactIOCTLResponse response = new NTTransactIOCTLResponse();
  111. if (subcommand.IsFsctl)
  112. {
  113. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  114. if (openFile == null)
  115. {
  116. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  117. return null;
  118. }
  119. int maxOutputLength = UInt16.MaxValue;
  120. byte[] output;
  121. header.Status = share.FileStore.DeviceIOControl(openFile.Handle, subcommand.FunctionCode, subcommand.Data, out output, maxOutputLength);
  122. if (header.Status != NTStatus.STATUS_SUCCESS)
  123. {
  124. return null;
  125. }
  126. response.Data = output;
  127. return response;
  128. }
  129. else
  130. {
  131. // [MS-SMB] If the IsFsctl field is set to zero, the server SHOULD fail the request with STATUS_NOT_SUPPORTED
  132. header.Status = NTStatus.STATUS_NOT_SUPPORTED;
  133. return null;
  134. }
  135. }
  136. private static void PrepareResponse(NTTransactResponse response, byte[] responseSetup, byte[] responseParameters, byte[] responseData, int maxBufferSize, List<SMB1Command> sendQueue)
  137. {
  138. if (NTTransactResponse.CalculateMessageSize(responseSetup.Length, responseParameters.Length, responseData.Length) <= maxBufferSize)
  139. {
  140. response.Setup = responseSetup;
  141. response.TotalParameterCount = (ushort)responseParameters.Length;
  142. response.TotalDataCount = (ushort)responseData.Length;
  143. response.TransParameters = responseParameters;
  144. response.TransData = responseData;
  145. }
  146. else
  147. {
  148. throw new NotImplementedException();
  149. }
  150. }
  151. }
  152. }