SMB2Command.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* Copyright (C) 2017-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 System.Security.Cryptography;
  11. using Utilities;
  12. namespace SMBLibrary.SMB2
  13. {
  14. public abstract class SMB2Command
  15. {
  16. public SMB2Header Header;
  17. public SMB2Command(SMB2CommandName commandName)
  18. {
  19. Header = new SMB2Header(commandName);
  20. }
  21. public SMB2Command(byte[] buffer, int offset)
  22. {
  23. Header = new SMB2Header(buffer, offset);
  24. }
  25. public void WriteBytes(byte[] buffer, int offset)
  26. {
  27. Header.WriteBytes(buffer, offset);
  28. WriteCommandBytes(buffer, offset + SMB2Header.Length);
  29. }
  30. public abstract void WriteCommandBytes(byte[] buffer, int offset);
  31. public byte[] GetBytes()
  32. {
  33. byte[] buffer = new byte[this.Length];
  34. WriteBytes(buffer, 0);
  35. return buffer;
  36. }
  37. public SMB2CommandName CommandName
  38. {
  39. get
  40. {
  41. return Header.Command;
  42. }
  43. }
  44. public int Length
  45. {
  46. get
  47. {
  48. return SMB2Header.Length + CommandLength;
  49. }
  50. }
  51. public abstract int CommandLength
  52. {
  53. get;
  54. }
  55. public static SMB2Command ReadRequest(byte[] buffer, int offset)
  56. {
  57. SMB2CommandName commandName = (SMB2CommandName)LittleEndianConverter.ToUInt16(buffer, offset + 12);
  58. switch (commandName)
  59. {
  60. case SMB2CommandName.Negotiate:
  61. return new NegotiateRequest(buffer, offset);
  62. case SMB2CommandName.SessionSetup:
  63. return new SessionSetupRequest(buffer, offset);
  64. case SMB2CommandName.Logoff:
  65. return new LogoffRequest(buffer, offset);
  66. case SMB2CommandName.TreeConnect:
  67. return new TreeConnectRequest(buffer, offset);
  68. case SMB2CommandName.TreeDisconnect:
  69. return new TreeDisconnectRequest(buffer, offset);
  70. case SMB2CommandName.Create:
  71. return new CreateRequest(buffer, offset);
  72. case SMB2CommandName.Close:
  73. return new CloseRequest(buffer, offset);
  74. case SMB2CommandName.Flush:
  75. return new FlushRequest(buffer, offset);
  76. case SMB2CommandName.Read:
  77. return new ReadRequest(buffer, offset);
  78. case SMB2CommandName.Write:
  79. return new WriteRequest(buffer, offset);
  80. case SMB2CommandName.Lock:
  81. return new LockRequest(buffer, offset);
  82. case SMB2CommandName.IOCtl:
  83. return new IOCtlRequest(buffer, offset);
  84. case SMB2CommandName.Cancel:
  85. return new CancelRequest(buffer, offset);
  86. case SMB2CommandName.Echo:
  87. return new EchoRequest(buffer, offset);
  88. case SMB2CommandName.QueryDirectory:
  89. return new QueryDirectoryRequest(buffer, offset);
  90. case SMB2CommandName.ChangeNotify:
  91. return new ChangeNotifyRequest(buffer, offset);
  92. case SMB2CommandName.QueryInfo:
  93. return new QueryInfoRequest(buffer, offset);
  94. case SMB2CommandName.SetInfo:
  95. return new SetInfoRequest(buffer, offset);
  96. default:
  97. throw new InvalidDataException("Invalid SMB2 command 0x" + ((ushort)commandName).ToString("X4"));
  98. }
  99. }
  100. public static List<SMB2Command> ReadRequestChain(byte[] buffer, int offset)
  101. {
  102. List<SMB2Command> result = new List<SMB2Command>();
  103. SMB2Command command;
  104. do
  105. {
  106. command = ReadRequest(buffer, offset);
  107. result.Add(command);
  108. offset += (int)command.Header.NextCommand;
  109. }
  110. while (command.Header.NextCommand != 0);
  111. return result;
  112. }
  113. public static byte[] GetCommandChainBytes(List<SMB2Command> commands)
  114. {
  115. return GetCommandChainBytes(commands, null);
  116. }
  117. /// <param name="sessionKey">
  118. /// command will be signed using this key if (not null and) SMB2_FLAGS_SIGNED is set.
  119. /// </param>
  120. public static byte[] GetCommandChainBytes(List<SMB2Command> commands, byte[] sessionKey)
  121. {
  122. int totalLength = 0;
  123. for (int index = 0; index < commands.Count; index++)
  124. {
  125. // Any subsequent SMB2 header MUST be 8-byte aligned
  126. int length = commands[index].Length;
  127. if (index < commands.Count - 1)
  128. {
  129. int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
  130. totalLength += paddedLength;
  131. }
  132. else
  133. {
  134. totalLength += length;
  135. }
  136. }
  137. byte[] buffer = new byte[totalLength];
  138. int offset = 0;
  139. for (int index = 0; index < commands.Count; index++)
  140. {
  141. SMB2Command command = commands[index];
  142. int commandLength = command.Length;
  143. int paddedLength;
  144. if (index < commands.Count - 1)
  145. {
  146. paddedLength = (int)Math.Ceiling((double)commandLength / 8) * 8;
  147. command.Header.NextCommand = (uint)paddedLength;
  148. }
  149. else
  150. {
  151. paddedLength = commandLength;
  152. }
  153. command.WriteBytes(buffer, offset);
  154. if (command.Header.IsSigned && sessionKey != null)
  155. {
  156. // [MS-SMB2] Any padding at the end of the message MUST be used in the hash computation.
  157. byte[] signature = new HMACSHA256(sessionKey).ComputeHash(buffer, offset, paddedLength);
  158. // [MS-SMB2] The first 16 bytes of the hash MUST be copied into the 16-byte signature field of the SMB2 Header.
  159. ByteWriter.WriteBytes(buffer, offset + SMB2Header.SignatureOffset, signature, 16);
  160. }
  161. offset += paddedLength;
  162. }
  163. return buffer;
  164. }
  165. public static SMB2Command ReadResponse(byte[] buffer, int offset)
  166. {
  167. SMB2CommandName commandName = (SMB2CommandName)LittleEndianConverter.ToUInt16(buffer, offset + 12);
  168. ushort structureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  169. switch (commandName)
  170. {
  171. case SMB2CommandName.Negotiate:
  172. {
  173. if (structureSize == NegotiateResponse.DeclaredSize)
  174. {
  175. return new NegotiateResponse(buffer, offset);
  176. }
  177. else if (structureSize == ErrorResponse.DeclaredSize)
  178. {
  179. return new ErrorResponse(buffer, offset);
  180. }
  181. else
  182. {
  183. throw new InvalidDataException();
  184. }
  185. }
  186. case SMB2CommandName.SessionSetup:
  187. {
  188. // SESSION_SETUP Response and ERROR Response have the same declared StructureSize of 9.
  189. if (structureSize == SessionSetupResponse.DeclaredSize)
  190. {
  191. NTStatus status = (NTStatus)LittleEndianConverter.ToUInt32(buffer, offset + 8);
  192. if (status == NTStatus.STATUS_SUCCESS || status == NTStatus.STATUS_MORE_PROCESSING_REQUIRED)
  193. {
  194. return new SessionSetupResponse(buffer, offset);
  195. }
  196. else
  197. {
  198. return new ErrorResponse(buffer, offset);
  199. }
  200. }
  201. else
  202. {
  203. throw new InvalidDataException();
  204. }
  205. }
  206. case SMB2CommandName.Logoff:
  207. {
  208. if (structureSize == LogoffResponse.DeclaredSize)
  209. {
  210. return new LogoffResponse(buffer, offset);
  211. }
  212. else if (structureSize == ErrorResponse.DeclaredSize)
  213. {
  214. return new ErrorResponse(buffer, offset);
  215. }
  216. else
  217. {
  218. throw new InvalidDataException();
  219. }
  220. }
  221. case SMB2CommandName.TreeConnect:
  222. {
  223. if (structureSize == TreeConnectResponse.DeclaredSize)
  224. {
  225. return new TreeConnectResponse(buffer, offset);
  226. }
  227. else if (structureSize == ErrorResponse.DeclaredSize)
  228. {
  229. return new ErrorResponse(buffer, offset);
  230. }
  231. else
  232. {
  233. throw new InvalidDataException();
  234. }
  235. }
  236. case SMB2CommandName.TreeDisconnect:
  237. {
  238. if (structureSize == TreeDisconnectResponse.DeclaredSize)
  239. {
  240. return new TreeDisconnectResponse(buffer, offset);
  241. }
  242. else if (structureSize == ErrorResponse.DeclaredSize)
  243. {
  244. return new ErrorResponse(buffer, offset);
  245. }
  246. else
  247. {
  248. throw new InvalidDataException();
  249. }
  250. }
  251. case SMB2CommandName.Create:
  252. {
  253. if (structureSize == CreateResponse.DeclaredSize)
  254. {
  255. return new CreateResponse(buffer, offset);
  256. }
  257. else if (structureSize == ErrorResponse.DeclaredSize)
  258. {
  259. return new ErrorResponse(buffer, offset);
  260. }
  261. else
  262. {
  263. throw new InvalidDataException();
  264. }
  265. }
  266. case SMB2CommandName.Close:
  267. {
  268. if (structureSize == CloseResponse.DeclaredSize)
  269. {
  270. return new CloseResponse(buffer, offset);
  271. }
  272. else if (structureSize == ErrorResponse.DeclaredSize)
  273. {
  274. return new ErrorResponse(buffer, offset);
  275. }
  276. else
  277. {
  278. throw new InvalidDataException();
  279. }
  280. }
  281. case SMB2CommandName.Flush:
  282. {
  283. if (structureSize == FlushResponse.DeclaredSize)
  284. {
  285. return new FlushResponse(buffer, offset);
  286. }
  287. else if (structureSize == ErrorResponse.DeclaredSize)
  288. {
  289. return new ErrorResponse(buffer, offset);
  290. }
  291. else
  292. {
  293. throw new InvalidDataException();
  294. }
  295. }
  296. case SMB2CommandName.Read:
  297. {
  298. if (structureSize == SMB2.ReadResponse.DeclaredSize)
  299. {
  300. return new ReadResponse(buffer, offset);
  301. }
  302. else if (structureSize == ErrorResponse.DeclaredSize)
  303. {
  304. return new ErrorResponse(buffer, offset);
  305. }
  306. else
  307. {
  308. throw new InvalidDataException();
  309. }
  310. }
  311. case SMB2CommandName.Write:
  312. {
  313. if (structureSize == WriteResponse.DeclaredSize)
  314. {
  315. return new WriteResponse(buffer, offset);
  316. }
  317. else if (structureSize == ErrorResponse.DeclaredSize)
  318. {
  319. return new ErrorResponse(buffer, offset);
  320. }
  321. else
  322. {
  323. throw new InvalidDataException();
  324. }
  325. }
  326. case SMB2CommandName.Lock:
  327. {
  328. if (structureSize == LockResponse.DeclaredSize)
  329. {
  330. return new LockResponse(buffer, offset);
  331. }
  332. else if (structureSize == ErrorResponse.DeclaredSize)
  333. {
  334. return new ErrorResponse(buffer, offset);
  335. }
  336. else
  337. {
  338. throw new InvalidDataException();
  339. }
  340. }
  341. case SMB2CommandName.IOCtl:
  342. {
  343. if (structureSize == IOCtlResponse.DeclaredSize)
  344. {
  345. return new IOCtlResponse(buffer, offset);
  346. }
  347. else if (structureSize == ErrorResponse.DeclaredSize)
  348. {
  349. return new ErrorResponse(buffer, offset);
  350. }
  351. else
  352. {
  353. throw new InvalidDataException();
  354. }
  355. }
  356. case SMB2CommandName.Cancel:
  357. {
  358. if (structureSize == ErrorResponse.DeclaredSize)
  359. {
  360. return new ErrorResponse(buffer, offset);
  361. }
  362. else
  363. {
  364. throw new InvalidDataException();
  365. }
  366. }
  367. case SMB2CommandName.Echo:
  368. {
  369. if (structureSize == EchoResponse.DeclaredSize)
  370. {
  371. return new EchoResponse(buffer, offset);
  372. }
  373. else if (structureSize == ErrorResponse.DeclaredSize)
  374. {
  375. return new ErrorResponse(buffer, offset);
  376. }
  377. else
  378. {
  379. throw new InvalidDataException();
  380. }
  381. }
  382. case SMB2CommandName.QueryDirectory:
  383. {
  384. // QUERY_DIRECTORY Response and ERROR Response have the same declared StructureSize of 9.
  385. if (structureSize == QueryDirectoryResponse.DeclaredSize)
  386. {
  387. NTStatus status = (NTStatus)LittleEndianConverter.ToUInt32(buffer, offset + 8);
  388. if (status == NTStatus.STATUS_SUCCESS)
  389. {
  390. return new QueryDirectoryResponse(buffer, offset);
  391. }
  392. else
  393. {
  394. return new ErrorResponse(buffer, offset);
  395. }
  396. }
  397. else
  398. {
  399. throw new InvalidDataException();
  400. }
  401. }
  402. case SMB2CommandName.ChangeNotify:
  403. {
  404. // CHANGE_NOTIFY Response and ERROR Response have the same declared StructureSize of 9.
  405. if (structureSize == ChangeNotifyResponse.DeclaredSize)
  406. {
  407. NTStatus status = (NTStatus)LittleEndianConverter.ToUInt32(buffer, offset + 8);
  408. if (status == NTStatus.STATUS_SUCCESS ||
  409. status == NTStatus.STATUS_NOTIFY_CLEANUP ||
  410. status == NTStatus.STATUS_NOTIFY_ENUM_DIR)
  411. {
  412. return new ChangeNotifyResponse(buffer, offset);
  413. }
  414. else
  415. {
  416. return new ErrorResponse(buffer, offset);
  417. }
  418. }
  419. else
  420. {
  421. throw new InvalidDataException();
  422. }
  423. }
  424. case SMB2CommandName.QueryInfo:
  425. {
  426. // QUERY_INFO Response and ERROR Response have the same declared StructureSize of 9.
  427. if (structureSize == QueryInfoResponse.DeclaredSize)
  428. {
  429. NTStatus status = (NTStatus)LittleEndianConverter.ToUInt32(buffer, offset + 8);
  430. if (status == NTStatus.STATUS_SUCCESS || status == NTStatus.STATUS_BUFFER_OVERFLOW)
  431. {
  432. return new QueryInfoResponse(buffer, offset);
  433. }
  434. else
  435. {
  436. return new ErrorResponse(buffer, offset);
  437. }
  438. }
  439. else
  440. {
  441. throw new InvalidDataException();
  442. }
  443. }
  444. case SMB2CommandName.SetInfo:
  445. {
  446. if (structureSize == SetInfoResponse.DeclaredSize)
  447. {
  448. return new SetInfoResponse(buffer, offset);
  449. }
  450. else if (structureSize == ErrorResponse.DeclaredSize)
  451. {
  452. return new ErrorResponse(buffer, offset);
  453. }
  454. else
  455. {
  456. throw new InvalidDataException();
  457. }
  458. }
  459. default:
  460. throw new InvalidDataException("Invalid SMB2 command 0x" + ((ushort)commandName).ToString("X4"));
  461. }
  462. }
  463. public static List<SMB2Command> ReadResponseChain(byte[] buffer, int offset)
  464. {
  465. List<SMB2Command> result = new List<SMB2Command>();
  466. SMB2Command command;
  467. do
  468. {
  469. command = ReadResponse(buffer, offset);
  470. result.Add(command);
  471. offset += (int)command.Header.NextCommand;
  472. }
  473. while (command.Header.NextCommand != 0);
  474. return result;
  475. }
  476. }
  477. }