SMBServer.SMB1.cs 15 KB

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