NTTransactHelper.cs 7.9 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 List<SMB1Command> GetNTTransactResponse(SMB1Header header, NTTransactRequest request, ISMBShare share, SMB1ConnectionState state)
  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);
  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 List<SMB1Command> GetNTTransactResponse(SMB1Header header, NTTransactSecondaryRequest request, ISMBShare share, SMB1ConnectionState state)
  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 new List<SMB1Command>();
  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);
  66. }
  67. }
  68. internal static List<SMB1Command> GetCompleteNTTransactResponse(SMB1Header header, NTTransactSubcommandName subcommandName, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state)
  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. return GetNTTransactResponse(responseSetup, responseParameters, responseData, state.MaxBufferSize);
  104. }
  105. private static NTTransactIOCTLResponse GetSubcommandResponse(SMB1Header header, NTTransactIOCTLRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  106. {
  107. SMB1Session session = state.GetSession(header.UID);
  108. NTTransactIOCTLResponse response = new NTTransactIOCTLResponse();
  109. if (subcommand.IsFsctl)
  110. {
  111. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  112. if (openFile == null)
  113. {
  114. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  115. return null;
  116. }
  117. int maxOutputLength = UInt16.MaxValue;
  118. byte[] output;
  119. header.Status = share.FileStore.DeviceIOControl(openFile.Handle, subcommand.FunctionCode, subcommand.Data, out output, maxOutputLength);
  120. if (header.Status != NTStatus.STATUS_SUCCESS)
  121. {
  122. return null;
  123. }
  124. response.Data = output;
  125. return response;
  126. }
  127. else
  128. {
  129. // [MS-SMB] If the IsFsctl field is set to zero, the server SHOULD fail the request with STATUS_NOT_SUPPORTED
  130. header.Status = NTStatus.STATUS_NOT_SUPPORTED;
  131. return null;
  132. }
  133. }
  134. private static List<SMB1Command> GetNTTransactResponse(byte[] responseSetup, byte[] responseParameters, byte[] responseData, int maxBufferSize)
  135. {
  136. if (NTTransactResponse.CalculateMessageSize(responseSetup.Length, responseParameters.Length, responseData.Length) <= maxBufferSize)
  137. {
  138. NTTransactResponse response = new NTTransactResponse();
  139. response.Setup = responseSetup;
  140. response.TotalParameterCount = (ushort)responseParameters.Length;
  141. response.TotalDataCount = (ushort)responseData.Length;
  142. response.TransParameters = responseParameters;
  143. response.TransData = responseData;
  144. return response;
  145. }
  146. else
  147. {
  148. throw new NotImplementedException();
  149. }
  150. }
  151. }
  152. }