TransactionHelper.cs 15 KB

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