SMBServer.SMB1.cs 19 KB

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