TransactionHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Copyright (C) 2014-2019 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.IO;
  10. using SMBLibrary.SMB1;
  11. using SMBLibrary.RPC;
  12. using SMBLibrary.Services;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. internal class TransactionHelper
  17. {
  18. /// <summary>
  19. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  20. /// The server MUST respond to the transaction request as a whole.
  21. /// </summary>
  22. internal static List<SMB1Command> GetTransactionResponse(SMB1Header header, TransactionRequest request, ISMBShare share, SMB1ConnectionState state)
  23. {
  24. if (request.TransParameters.Length < request.TotalParameterCount ||
  25. request.TransData.Length < request.TotalDataCount)
  26. {
  27. // A secondary transaction request is pending
  28. ProcessStateObject processState = state.CreateProcessState(header.PID);
  29. processState.MaxParameterCount = request.MaxParameterCount;
  30. processState.MaxDataCount = request.MaxDataCount;
  31. processState.Timeout = request.Timeout;
  32. processState.Name = request.Name;
  33. processState.TransactionSetup = request.Setup;
  34. processState.TransactionParameters = new byte[request.TotalParameterCount];
  35. processState.TransactionData = new byte[request.TotalDataCount];
  36. ByteWriter.WriteBytes(processState.TransactionParameters, 0, request.TransParameters);
  37. ByteWriter.WriteBytes(processState.TransactionData, 0, request.TransData);
  38. processState.TransactionParametersReceived += request.TransParameters.Length;
  39. processState.TransactionDataReceived += request.TransData.Length;
  40. if (request is Transaction2Request)
  41. {
  42. return new Transaction2InterimResponse();
  43. }
  44. else
  45. {
  46. return new TransactionInterimResponse();
  47. }
  48. }
  49. else
  50. {
  51. // We have a complete command
  52. if (request is Transaction2Request)
  53. {
  54. return GetCompleteTransaction2Response(header, request.MaxDataCount, request.Setup, request.TransParameters, request.TransData, share, state);
  55. }
  56. else
  57. {
  58. return GetCompleteTransactionResponse(header, request.MaxDataCount, request.Timeout, request.Name, request.Setup, request.TransParameters, request.TransData, share, state);
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// There are no secondary response messages.
  64. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  65. /// The server MUST respond to the transaction request as a whole.
  66. /// </summary>
  67. internal static List<SMB1Command> GetTransactionResponse(SMB1Header header, TransactionSecondaryRequest request, ISMBShare share, SMB1ConnectionState state)
  68. {
  69. ProcessStateObject processState = state.GetProcessState(header.PID);
  70. if (processState == null)
  71. {
  72. throw new InvalidDataException();
  73. }
  74. ByteWriter.WriteBytes(processState.TransactionParameters, request.ParameterDisplacement, request.TransParameters);
  75. ByteWriter.WriteBytes(processState.TransactionData, request.DataDisplacement, request.TransData);
  76. processState.TransactionParametersReceived += request.TransParameters.Length;
  77. processState.TransactionDataReceived += request.TransData.Length;
  78. if (processState.TransactionParametersReceived < processState.TransactionParameters.Length ||
  79. processState.TransactionDataReceived < processState.TransactionData.Length)
  80. {
  81. return new List<SMB1Command>();
  82. }
  83. else
  84. {
  85. // We have a complete command
  86. state.RemoveProcessState(header.PID);
  87. if (request is Transaction2SecondaryRequest)
  88. {
  89. return GetCompleteTransaction2Response(header, processState.MaxDataCount, processState.TransactionSetup, processState.TransactionParameters, processState.TransactionData, share, state);
  90. }
  91. else
  92. {
  93. return GetCompleteTransactionResponse(header, processState.MaxDataCount, processState.Timeout, processState.Name, processState.TransactionSetup, processState.TransactionParameters, processState.TransactionData, share, state);
  94. }
  95. }
  96. }
  97. internal static List<SMB1Command> GetCompleteTransactionResponse(SMB1Header header, uint maxDataCount, uint timeout, string name, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state)
  98. {
  99. if (String.Equals(name, @"\PIPE\lanman", StringComparison.OrdinalIgnoreCase))
  100. {
  101. // [MS-RAP] Remote Administration Protocol request
  102. state.LogToServer(Severity.Debug, "Remote Administration Protocol requests are not implemented");
  103. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  104. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  105. }
  106. TransactionSubcommand subcommand;
  107. try
  108. {
  109. subcommand = TransactionSubcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  110. }
  111. catch
  112. {
  113. header.Status = NTStatus.STATUS_INVALID_SMB;
  114. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  115. }
  116. state.LogToServer(Severity.Verbose, "Received complete SMB_COM_TRANSACTION subcommand: {0}", subcommand.SubcommandName);
  117. TransactionSubcommand subcommandResponse = null;
  118. if (subcommand is TransactionSetNamedPipeStateRequest)
  119. {
  120. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  121. }
  122. else if (subcommand is TransactionRawReadNamedPipeRequest)
  123. {
  124. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  125. }
  126. else if (subcommand is TransactionQueryNamedPipeStateRequest)
  127. {
  128. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  129. }
  130. else if (subcommand is TransactionQueryNamedPipeInfoRequest)
  131. {
  132. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  133. }
  134. else if (subcommand is TransactionPeekNamedPipeRequest)
  135. {
  136. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  137. }
  138. else if (subcommand is TransactionTransactNamedPipeRequest)
  139. {
  140. subcommandResponse = TransactionSubcommandHelper.GetSubcommandResponse(header, maxDataCount, (TransactionTransactNamedPipeRequest)subcommand, share, state);
  141. }
  142. else if (subcommand is TransactionRawWriteNamedPipeRequest)
  143. {
  144. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  145. }
  146. else if (subcommand is TransactionReadNamedPipeRequest)
  147. {
  148. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  149. }
  150. else if (subcommand is TransactionWriteNamedPipeRequest)
  151. {
  152. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  153. }
  154. else if (subcommand is TransactionWaitNamedPipeRequest)
  155. {
  156. TransactionSubcommandHelper.ProcessSubcommand(header, timeout, name, (TransactionWaitNamedPipeRequest)subcommand, share, state);
  157. }
  158. else if (subcommand is TransactionCallNamedPipeRequest)
  159. {
  160. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  161. }
  162. else
  163. {
  164. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  165. }
  166. if (header.Status != NTStatus.STATUS_SUCCESS && (header.Status != NTStatus.STATUS_BUFFER_OVERFLOW || subcommandResponse == null))
  167. {
  168. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  169. }
  170. byte[] responseSetup = subcommandResponse.GetSetup();
  171. byte[] responseParameters = subcommandResponse.GetParameters();
  172. byte[] responseData = subcommandResponse.GetData(header.UnicodeFlag);
  173. return GetTransactionResponse(false, responseSetup, responseParameters, responseData, state.MaxBufferSize);
  174. }
  175. internal static List<SMB1Command> GetCompleteTransaction2Response(SMB1Header header, uint maxDataCount, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state)
  176. {
  177. Transaction2Subcommand subcommand;
  178. try
  179. {
  180. subcommand = Transaction2Subcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  181. }
  182. catch
  183. {
  184. header.Status = NTStatus.STATUS_INVALID_SMB;
  185. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  186. }
  187. state.LogToServer(Severity.Verbose, "Received complete SMB_COM_TRANSACTION2 subcommand: {0}", subcommand.SubcommandName);
  188. Transaction2Subcommand subcommandResponse = null;
  189. if (subcommand is Transaction2FindFirst2Request)
  190. {
  191. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, maxDataCount, (Transaction2FindFirst2Request)subcommand, share, state);
  192. }
  193. else if (subcommand is Transaction2FindNext2Request)
  194. {
  195. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, maxDataCount, (Transaction2FindNext2Request)subcommand, share, state);
  196. }
  197. else if (subcommand is Transaction2QueryFSInformationRequest)
  198. {
  199. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, maxDataCount, (Transaction2QueryFSInformationRequest)subcommand, share, state);
  200. }
  201. else if (subcommand is Transaction2SetFSInformationRequest)
  202. {
  203. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2SetFSInformationRequest)subcommand, share, state);
  204. }
  205. else if (subcommand is Transaction2QueryPathInformationRequest)
  206. {
  207. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, maxDataCount, (Transaction2QueryPathInformationRequest)subcommand, share, state);
  208. }
  209. else if (subcommand is Transaction2SetPathInformationRequest)
  210. {
  211. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  212. }
  213. else if (subcommand is Transaction2QueryFileInformationRequest)
  214. {
  215. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, maxDataCount, (Transaction2QueryFileInformationRequest)subcommand, share, state);
  216. }
  217. else if (subcommand is Transaction2SetFileInformationRequest)
  218. {
  219. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2SetFileInformationRequest)subcommand, share, state);
  220. }
  221. else if (subcommand is Transaction2CreateDirectoryRequest)
  222. {
  223. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  224. }
  225. else if (subcommand is Transaction2GetDfsReferralRequest)
  226. {
  227. header.Status = NTStatus.STATUS_NO_SUCH_DEVICE;
  228. }
  229. else
  230. {
  231. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  232. }
  233. if (header.Status != NTStatus.STATUS_SUCCESS && (header.Status != NTStatus.STATUS_BUFFER_OVERFLOW || subcommandResponse == null))
  234. {
  235. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  236. }
  237. byte[] responseSetup = subcommandResponse.GetSetup();
  238. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  239. byte[] responseData = subcommandResponse.GetData(header.UnicodeFlag);
  240. return GetTransactionResponse(true, responseSetup, responseParameters, responseData, state.MaxBufferSize);
  241. }
  242. internal static List<SMB1Command> GetTransactionResponse(bool transaction2Response, byte[] responseSetup, byte[] responseParameters, byte[] responseData, int maxBufferSize)
  243. {
  244. List<SMB1Command> result = new List<SMB1Command>();
  245. TransactionResponse response;
  246. if (transaction2Response)
  247. {
  248. response = new Transaction2Response();
  249. }
  250. else
  251. {
  252. response = new TransactionResponse();
  253. }
  254. result.Add(response);
  255. int responseSize = TransactionResponse.CalculateMessageSize(responseSetup.Length, responseParameters.Length, responseData.Length);
  256. if (responseSize <= maxBufferSize)
  257. {
  258. response.Setup = responseSetup;
  259. response.TotalParameterCount = (ushort)responseParameters.Length;
  260. response.TotalDataCount = (ushort)responseData.Length;
  261. response.TransParameters = responseParameters;
  262. response.TransData = responseData;
  263. }
  264. else
  265. {
  266. int currentDataLength = maxBufferSize - (responseSize - responseData.Length);
  267. byte[] buffer = new byte[currentDataLength];
  268. Array.Copy(responseData, 0, buffer, 0, currentDataLength);
  269. response.Setup = responseSetup;
  270. response.TotalParameterCount = (ushort)responseParameters.Length;
  271. response.TotalDataCount = (ushort)responseData.Length;
  272. response.TransParameters = responseParameters;
  273. response.TransData = buffer;
  274. int dataBytesLeftToSend = responseData.Length - currentDataLength;
  275. while (dataBytesLeftToSend > 0)
  276. {
  277. TransactionResponse additionalResponse;
  278. if (transaction2Response)
  279. {
  280. additionalResponse = new Transaction2Response();
  281. }
  282. else
  283. {
  284. additionalResponse = new TransactionResponse();
  285. }
  286. currentDataLength = dataBytesLeftToSend;
  287. responseSize = TransactionResponse.CalculateMessageSize(0, 0, dataBytesLeftToSend);
  288. if (responseSize > maxBufferSize)
  289. {
  290. currentDataLength = maxBufferSize - (responseSize - dataBytesLeftToSend);
  291. }
  292. buffer = new byte[currentDataLength];
  293. int dataBytesSent = responseData.Length - dataBytesLeftToSend;
  294. Array.Copy(responseData, dataBytesSent, buffer, 0, currentDataLength);
  295. additionalResponse.TotalParameterCount = (ushort)responseParameters.Length;
  296. additionalResponse.TotalDataCount = (ushort)responseData.Length;
  297. additionalResponse.TransData = buffer;
  298. additionalResponse.ParameterDisplacement = (ushort)response.TransParameters.Length;
  299. additionalResponse.DataDisplacement = (ushort)dataBytesSent;
  300. result.Add(additionalResponse);
  301. dataBytesLeftToSend -= currentDataLength;
  302. }
  303. }
  304. return result;
  305. }
  306. }
  307. }