SMBServer.SMB1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 SMBLibrary.NetBios;
  10. using SMBLibrary.Server.SMB1;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public partial class SMBServer
  16. {
  17. private void ProcessSMB1Message(SMB1Message message, ref ConnectionState state)
  18. {
  19. SMB1Header header = new SMB1Header();
  20. PrepareResponseHeader(header, message.Header);
  21. List<SMB1Command> sendQueue = new List<SMB1Command>();
  22. bool isBatchedRequest = (message.Commands.Count > 1);
  23. foreach (SMB1Command command in message.Commands)
  24. {
  25. List<SMB1Command> responses = ProcessSMB1Command(header, command, ref state);
  26. sendQueue.AddRange(responses);
  27. if (header.Status != NTStatus.STATUS_SUCCESS)
  28. {
  29. break;
  30. }
  31. }
  32. if (isBatchedRequest)
  33. {
  34. if (sendQueue.Count > 0)
  35. {
  36. // The server MUST batch the response into an AndX Response chain.
  37. SMB1Message reply = new SMB1Message();
  38. reply.Header = header;
  39. for (int index = 0; index < sendQueue.Count; index++)
  40. {
  41. if (sendQueue[index] is SMBAndXCommand || index == sendQueue.Count - 1)
  42. {
  43. reply.Commands.Add(sendQueue[index]);
  44. sendQueue.RemoveAt(index);
  45. index--;
  46. }
  47. }
  48. EnqueueMessage(state, reply);
  49. }
  50. }
  51. foreach (SMB1Command response in sendQueue)
  52. {
  53. SMB1Message reply = new SMB1Message();
  54. reply.Header = header;
  55. reply.Commands.Add(response);
  56. EnqueueMessage(state, reply);
  57. }
  58. }
  59. /// <summary>
  60. /// May return an empty list
  61. /// </summary>
  62. private List<SMB1Command> ProcessSMB1Command(SMB1Header header, SMB1Command command, ref ConnectionState state)
  63. {
  64. if (state.Dialect == SMBDialect.NotSet)
  65. {
  66. if (command is NegotiateRequest)
  67. {
  68. NegotiateRequest request = (NegotiateRequest)command;
  69. if (request.Dialects.Contains(SMBServer.NTLanManagerDialect))
  70. {
  71. state = new SMB1ConnectionState(state);
  72. state.Dialect = SMBDialect.NTLM012;
  73. m_connectionManager.AddConnection(state);
  74. if (EnableExtendedSecurity && header.ExtendedSecurityFlag)
  75. {
  76. return NegotiateHelper.GetNegotiateResponseExtended(request, m_serverGuid);
  77. }
  78. else
  79. {
  80. return NegotiateHelper.GetNegotiateResponse(header, request, m_securityProvider, state);
  81. }
  82. }
  83. else
  84. {
  85. return new NegotiateResponseNotSupported();
  86. }
  87. }
  88. else
  89. {
  90. // [MS-CIFS] An SMB_COM_NEGOTIATE exchange MUST be completed before any other SMB messages are sent to the server
  91. header.Status = NTStatus.STATUS_INVALID_SMB;
  92. return new ErrorResponse(command.CommandName);
  93. }
  94. }
  95. else if (command is NegotiateRequest)
  96. {
  97. // There MUST be only one SMB_COM_NEGOTIATE exchange per SMB connection.
  98. // Subsequent SMB_COM_NEGOTIATE requests received by the server MUST be rejected with error responses.
  99. header.Status = NTStatus.STATUS_INVALID_SMB;
  100. return new ErrorResponse(command.CommandName);
  101. }
  102. else
  103. {
  104. return ProcessSMB1Command(header, command, (SMB1ConnectionState)state);
  105. }
  106. }
  107. private List<SMB1Command> ProcessSMB1Command(SMB1Header header, SMB1Command command, SMB1ConnectionState state)
  108. {
  109. if (command is SessionSetupAndXRequest)
  110. {
  111. SessionSetupAndXRequest request = (SessionSetupAndXRequest)command;
  112. state.MaxBufferSize = request.MaxBufferSize;
  113. return SessionSetupHelper.GetSessionSetupResponse(header, request, m_securityProvider, state);
  114. }
  115. else if (command is SessionSetupAndXRequestExtended)
  116. {
  117. SessionSetupAndXRequestExtended request = (SessionSetupAndXRequestExtended)command;
  118. state.MaxBufferSize = request.MaxBufferSize;
  119. return SessionSetupHelper.GetSessionSetupResponseExtended(header, request, m_securityProvider, state);
  120. }
  121. else if (command is EchoRequest)
  122. {
  123. return ServerResponseHelper.GetEchoResponse((EchoRequest)command);
  124. }
  125. else
  126. {
  127. SMB1Session session = state.GetSession(header.UID);
  128. if (session == null)
  129. {
  130. header.Status = NTStatus.STATUS_USER_SESSION_DELETED;
  131. return new ErrorResponse(command.CommandName);
  132. }
  133. if (command is TreeConnectAndXRequest)
  134. {
  135. TreeConnectAndXRequest request = (TreeConnectAndXRequest)command;
  136. return TreeConnectHelper.GetTreeConnectResponse(header, request, state, m_services, m_shares);
  137. }
  138. else if (command is LogoffAndXRequest)
  139. {
  140. m_securityProvider.DeleteSecurityContext(ref session.SecurityContext.AuthenticationContext);
  141. state.RemoveSession(header.UID);
  142. return new LogoffAndXResponse();
  143. }
  144. else
  145. {
  146. ISMBShare share = session.GetConnectedTree(header.TID);
  147. if (share == null)
  148. {
  149. header.Status = NTStatus.STATUS_SMB_BAD_TID;
  150. return new ErrorResponse(command.CommandName);
  151. }
  152. if (command is CreateDirectoryRequest)
  153. {
  154. CreateDirectoryRequest request = (CreateDirectoryRequest)command;
  155. return FileStoreResponseHelper.GetCreateDirectoryResponse(header, request, share, state);
  156. }
  157. else if (command is DeleteDirectoryRequest)
  158. {
  159. DeleteDirectoryRequest request = (DeleteDirectoryRequest)command;
  160. return FileStoreResponseHelper.GetDeleteDirectoryResponse(header, request, share, state);
  161. }
  162. else if (command is CloseRequest)
  163. {
  164. CloseRequest request = (CloseRequest)command;
  165. return ServerResponseHelper.GetCloseResponse(header, request, share, state);
  166. }
  167. else if (command is FlushRequest)
  168. {
  169. return new FlushResponse();
  170. }
  171. else if (command is DeleteRequest)
  172. {
  173. DeleteRequest request = (DeleteRequest)command;
  174. return FileStoreResponseHelper.GetDeleteResponse(header, request, share, state);
  175. }
  176. else if (command is RenameRequest)
  177. {
  178. RenameRequest request = (RenameRequest)command;
  179. return FileStoreResponseHelper.GetRenameResponse(header, request, share, state);
  180. }
  181. else if (command is QueryInformationRequest)
  182. {
  183. QueryInformationRequest request = (QueryInformationRequest)command;
  184. return FileStoreResponseHelper.GetQueryInformationResponse(header, request, share, state);
  185. }
  186. else if (command is SetInformationRequest)
  187. {
  188. SetInformationRequest request = (SetInformationRequest)command;
  189. return FileStoreResponseHelper.GetSetInformationResponse(header, request, share, state);
  190. }
  191. else if (command is ReadRequest)
  192. {
  193. ReadRequest request = (ReadRequest)command;
  194. return ReadWriteResponseHelper.GetReadResponse(header, request, share, state);
  195. }
  196. else if (command is WriteRequest)
  197. {
  198. WriteRequest request = (WriteRequest)command;
  199. return ReadWriteResponseHelper.GetWriteResponse(header, request, share, state);
  200. }
  201. else if (command is CheckDirectoryRequest)
  202. {
  203. CheckDirectoryRequest request = (CheckDirectoryRequest)command;
  204. return FileStoreResponseHelper.GetCheckDirectoryResponse(header, request, share, state);
  205. }
  206. else if (command is WriteRawRequest)
  207. {
  208. // [MS-CIFS] 3.3.5.26 - Receiving an SMB_COM_WRITE_RAW Request:
  209. // the server MUST verify that the Server.Capabilities include CAP_RAW_MODE,
  210. // If an error is detected [..] the Write Raw operation MUST fail and
  211. // the server MUST return a Final Server Response [..] with the Count field set to zero.
  212. return new WriteRawFinalResponse();
  213. }
  214. else if (command is SetInformation2Request)
  215. {
  216. SetInformation2Request request = (SetInformation2Request)command;
  217. return FileStoreResponseHelper.GetSetInformation2Response(header, request, share, state);
  218. }
  219. else if (command is LockingAndXRequest)
  220. {
  221. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  222. return new ErrorResponse(command.CommandName);
  223. }
  224. else if (command is OpenAndXRequest)
  225. {
  226. OpenAndXRequest request = (OpenAndXRequest)command;
  227. return OpenAndXHelper.GetOpenAndXResponse(header, request, share, state);
  228. }
  229. else if (command is ReadAndXRequest)
  230. {
  231. ReadAndXRequest request = (ReadAndXRequest)command;
  232. return ReadWriteResponseHelper.GetReadResponse(header, request, share, state);
  233. }
  234. else if (command is WriteAndXRequest)
  235. {
  236. WriteAndXRequest request = (WriteAndXRequest)command;
  237. return ReadWriteResponseHelper.GetWriteResponse(header, request, share, state);
  238. }
  239. else if (command is FindClose2Request)
  240. {
  241. return ServerResponseHelper.GetFindClose2Request(header, (FindClose2Request)command, state);
  242. }
  243. else if (command is TreeDisconnectRequest)
  244. {
  245. TreeDisconnectRequest request = (TreeDisconnectRequest)command;
  246. return TreeConnectHelper.GetTreeDisconnectResponse(header, request, share, state);
  247. }
  248. else if (command is TransactionRequest) // Both TransactionRequest and Transaction2Request
  249. {
  250. TransactionRequest request = (TransactionRequest)command;
  251. try
  252. {
  253. return TransactionHelper.GetTransactionResponse(header, request, share, state);
  254. }
  255. catch (UnsupportedInformationLevelException)
  256. {
  257. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  258. return new ErrorResponse(command.CommandName);
  259. }
  260. }
  261. else if (command is TransactionSecondaryRequest) // Both TransactionSecondaryRequest and Transaction2SecondaryRequest
  262. {
  263. TransactionSecondaryRequest request = (TransactionSecondaryRequest)command;
  264. try
  265. {
  266. return TransactionHelper.GetTransactionResponse(header, request, share, state);
  267. }
  268. catch (UnsupportedInformationLevelException)
  269. {
  270. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  271. return new ErrorResponse(command.CommandName);
  272. }
  273. }
  274. else if (command is NTTransactRequest)
  275. {
  276. NTTransactRequest request = (NTTransactRequest)command;
  277. return NTTransactHelper.GetNTTransactResponse(header, request, share, state);
  278. }
  279. else if (command is NTTransactSecondaryRequest)
  280. {
  281. NTTransactSecondaryRequest request = (NTTransactSecondaryRequest)command;
  282. return NTTransactHelper.GetNTTransactResponse(header, request, share, state);
  283. }
  284. else if (command is NTCreateAndXRequest)
  285. {
  286. NTCreateAndXRequest request = (NTCreateAndXRequest)command;
  287. return NTCreateHelper.GetNTCreateResponse(header, request, share, state);
  288. }
  289. }
  290. }
  291. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  292. return new ErrorResponse(command.CommandName);
  293. }
  294. private static void EnqueueMessage(ConnectionState state, SMB1Message response)
  295. {
  296. SessionMessagePacket packet = new SessionMessagePacket();
  297. packet.Trailer = response.GetBytes();
  298. state.SendQueue.Enqueue(packet);
  299. state.LogToServer(Severity.Verbose, "SMB1 message queued: {0} responses, First response: {1}, Packet length: {2}", response.Commands.Count, response.Commands[0].CommandName.ToString(), packet.Length);
  300. }
  301. private static void PrepareResponseHeader(SMB1Header responseHeader, SMB1Header requestHeader)
  302. {
  303. responseHeader.Status = NTStatus.STATUS_SUCCESS;
  304. responseHeader.Flags = HeaderFlags.CaseInsensitive | HeaderFlags.CanonicalizedPaths | HeaderFlags.Reply;
  305. responseHeader.Flags2 = HeaderFlags2.NTStatusCode;
  306. if ((requestHeader.Flags2 & HeaderFlags2.LongNamesAllowed) > 0)
  307. {
  308. responseHeader.Flags2 |= HeaderFlags2.LongNamesAllowed | HeaderFlags2.LongNameUsed;
  309. }
  310. if ((requestHeader.Flags2 & HeaderFlags2.ExtendedAttributes) > 0)
  311. {
  312. responseHeader.Flags2 |= HeaderFlags2.ExtendedAttributes;
  313. }
  314. if ((requestHeader.Flags2 & HeaderFlags2.ExtendedSecurity) > 0)
  315. {
  316. responseHeader.Flags2 |= HeaderFlags2.ExtendedSecurity;
  317. }
  318. if ((requestHeader.Flags2 & HeaderFlags2.Unicode) > 0)
  319. {
  320. responseHeader.Flags2 |= HeaderFlags2.Unicode;
  321. }
  322. responseHeader.MID = requestHeader.MID;
  323. responseHeader.PID = requestHeader.PID;
  324. responseHeader.UID = requestHeader.UID;
  325. responseHeader.TID = requestHeader.TID;
  326. }
  327. }
  328. }