SMB1FileStore.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.SMB1;
  10. using Utilities;
  11. namespace SMBLibrary.Client
  12. {
  13. public class SMB1FileStore : ISMBFileStore
  14. {
  15. private SMB1Client m_client;
  16. private ushort m_treeID;
  17. public SMB1FileStore(SMB1Client client, ushort treeID)
  18. {
  19. m_client = client;
  20. m_treeID = treeID;
  21. }
  22. public NTStatus CreateFile(out object handle, out FileStatus fileStatus, string path, AccessMask desiredAccess, FileAttributes fileAttributes, ShareAccess shareAccess, CreateDisposition createDisposition, CreateOptions createOptions, SecurityContext securityContext)
  23. {
  24. handle = null;
  25. fileStatus = FileStatus.FILE_DOES_NOT_EXIST;
  26. NTCreateAndXRequest request = new NTCreateAndXRequest();
  27. request.FileName = path;
  28. request.DesiredAccess = desiredAccess;
  29. request.ExtFileAttributes = ToExtendedFileAttributes(fileAttributes);
  30. request.ShareAccess = shareAccess;
  31. request.CreateDisposition = createDisposition;
  32. request.CreateOptions = createOptions;
  33. request.ImpersonationLevel = ImpersonationLevel.SEC_IMPERSONATE;
  34. TrySendMessage(request);
  35. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_CREATE_ANDX);
  36. if (reply != null)
  37. {
  38. if (reply.Commands[0] is NTCreateAndXResponse)
  39. {
  40. NTCreateAndXResponse response = reply.Commands[0] as NTCreateAndXResponse;
  41. handle = response.FID;
  42. fileStatus = ToFileStatus(response.CreateDisposition);
  43. return reply.Header.Status;
  44. }
  45. else if (reply.Commands[0] is ErrorResponse)
  46. {
  47. return reply.Header.Status;
  48. }
  49. }
  50. return NTStatus.STATUS_INVALID_SMB;
  51. }
  52. public NTStatus CloseFile(object handle)
  53. {
  54. CloseRequest request = new CloseRequest();
  55. request.FID = (ushort)handle;
  56. TrySendMessage(request);
  57. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_CLOSE);
  58. if (reply != null)
  59. {
  60. return reply.Header.Status;
  61. }
  62. return NTStatus.STATUS_INVALID_SMB;
  63. }
  64. public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
  65. {
  66. data = null;
  67. ReadAndXRequest request = new ReadAndXRequest();
  68. request.FID = (ushort)handle;
  69. request.Offset = (ulong)offset;
  70. request.MaxCountLarge = (uint)maxCount;
  71. TrySendMessage(request);
  72. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_READ_ANDX);
  73. if (reply != null)
  74. {
  75. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is ReadAndXResponse)
  76. {
  77. data = ((ReadAndXResponse)reply.Commands[0]).Data;
  78. }
  79. return reply.Header.Status;
  80. }
  81. return NTStatus.STATUS_INVALID_SMB;
  82. }
  83. public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
  84. {
  85. numberOfBytesWritten = 0;
  86. WriteAndXRequest request = new WriteAndXRequest();
  87. request.FID = (ushort)handle;
  88. request.Offset = (ulong)offset;
  89. request.Data = data;
  90. TrySendMessage(request);
  91. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_WRITE_ANDX);
  92. if (reply != null)
  93. {
  94. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is WriteAndXResponse)
  95. {
  96. numberOfBytesWritten = (int)((WriteAndXResponse)reply.Commands[0]).Count;
  97. }
  98. return reply.Header.Status;
  99. }
  100. return NTStatus.STATUS_INVALID_SMB;
  101. }
  102. public NTStatus FlushFileBuffers(object handle)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public NTStatus LockFile(object handle, long byteOffset, long length, bool exclusiveLock)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public NTStatus UnlockFile(object handle, long byteOffset, long length)
  111. {
  112. throw new NotImplementedException();
  113. }
  114. public NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, object handle, string fileName, FileInformationClass informationClass)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public NTStatus QueryDirectory(out List<FindInformation> result, string fileName, FindInformationLevel informationLevel)
  119. {
  120. result = null;
  121. int maxOutputLength = 4096;
  122. Transaction2FindFirst2Request subcommand = new Transaction2FindFirst2Request();
  123. subcommand.SearchAttributes = SMBFileAttributes.Hidden | SMBFileAttributes.System | SMBFileAttributes.Directory;
  124. subcommand.SearchCount = UInt16.MaxValue;
  125. subcommand.Flags = FindFlags.SMB_FIND_CLOSE_AT_EOS;
  126. subcommand.InformationLevel = informationLevel;
  127. subcommand.FileName = fileName;
  128. Transaction2Request request = new Transaction2Request();
  129. request.Setup = subcommand.GetSetup();
  130. request.TransParameters = subcommand.GetParameters(true);
  131. request.TransData = subcommand.GetData(true);
  132. request.TotalDataCount = (ushort)request.TransData.Length;
  133. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  134. request.MaxParameterCount = Transaction2FindFirst2Response.ParametersLength;
  135. request.MaxDataCount = (ushort)maxOutputLength;
  136. TrySendMessage(request);
  137. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  138. if (reply != null)
  139. {
  140. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  141. {
  142. result = new List<FindInformation>();
  143. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  144. Transaction2FindFirst2Response subcommandResponse = new Transaction2FindFirst2Response(response.TransParameters, response.TransData, true);
  145. FindInformationList findInformationList = subcommandResponse.GetFindInformationList(subcommand.InformationLevel, true);
  146. result.AddRange(findInformationList);
  147. bool endOfSearch = subcommandResponse.EndOfSearch;
  148. while (!endOfSearch)
  149. {
  150. Transaction2FindNext2Request nextSubcommand = new Transaction2FindNext2Request();
  151. nextSubcommand.SID = subcommandResponse.SID;
  152. nextSubcommand.SearchCount = UInt16.MaxValue;
  153. nextSubcommand.Flags = FindFlags.SMB_FIND_CLOSE_AT_EOS | FindFlags.SMB_FIND_CONTINUE_FROM_LAST;
  154. nextSubcommand.InformationLevel = informationLevel;
  155. nextSubcommand.FileName = fileName;
  156. request = new Transaction2Request();
  157. request.Setup = nextSubcommand.GetSetup();
  158. request.TransParameters = nextSubcommand.GetParameters(true);
  159. request.TransData = nextSubcommand.GetData(true);
  160. request.TotalDataCount = (ushort)request.TransData.Length;
  161. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  162. request.MaxParameterCount = Transaction2FindNext2Response.ParametersLength;
  163. request.MaxDataCount = (ushort)maxOutputLength;
  164. TrySendMessage(request);
  165. reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  166. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  167. {
  168. response = (Transaction2Response)reply.Commands[0];
  169. Transaction2FindNext2Response nextSubcommandResponse = new Transaction2FindNext2Response(response.TransParameters, response.TransData, true);
  170. findInformationList = nextSubcommandResponse.GetFindInformationList(subcommand.InformationLevel, true);
  171. result.AddRange(findInformationList);
  172. endOfSearch = nextSubcommandResponse.EndOfSearch;
  173. }
  174. else
  175. {
  176. endOfSearch = true;
  177. }
  178. }
  179. }
  180. return reply.Header.Status;
  181. }
  182. return NTStatus.STATUS_INVALID_SMB;
  183. }
  184. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  185. {
  186. throw new NotImplementedException();
  187. }
  188. public NTStatus GetFileInformation(out QueryInformation result, object handle, QueryInformationLevel informationLevel)
  189. {
  190. result = null;
  191. int maxOutputLength = 4096;
  192. Transaction2QueryFileInformationRequest subcommand = new Transaction2QueryFileInformationRequest();
  193. subcommand.FID = (ushort)handle;
  194. subcommand.InformationLevel = informationLevel;
  195. Transaction2Request request = new Transaction2Request();
  196. request.Setup = subcommand.GetSetup();
  197. request.TransParameters = subcommand.GetParameters(true);
  198. request.TransData = subcommand.GetData(true);
  199. request.TotalDataCount = (ushort)request.TransData.Length;
  200. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  201. request.MaxParameterCount = Transaction2QueryFileInformationResponse.ParametersLength;
  202. request.MaxDataCount = (ushort)maxOutputLength;
  203. TrySendMessage(request);
  204. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  205. if (reply != null)
  206. {
  207. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  208. {
  209. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  210. Transaction2QueryFileInformationResponse subcommandResponse = new Transaction2QueryFileInformationResponse(response.TransParameters, response.TransData, true);
  211. result = subcommandResponse.GetQueryInformation(informationLevel);
  212. }
  213. return reply.Header.Status;
  214. }
  215. return NTStatus.STATUS_INVALID_SMB;
  216. }
  217. public NTStatus SetFileInformation(object handle, FileInformation information)
  218. {
  219. throw new NotImplementedException();
  220. }
  221. public NTStatus SetFileInformation(object handle, SetInformation information)
  222. {
  223. int maxOutputLength = 4096;
  224. Transaction2SetFileInformationRequest subcommand = new Transaction2SetFileInformationRequest();
  225. subcommand.FID = (ushort)handle;
  226. subcommand.SetInformation(information);
  227. Transaction2Request request = new Transaction2Request();
  228. request.Setup = subcommand.GetSetup();
  229. request.TransParameters = subcommand.GetParameters(true);
  230. request.TransData = subcommand.GetData(true);
  231. request.TotalDataCount = (ushort)request.TransData.Length;
  232. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  233. request.MaxParameterCount = Transaction2SetFileInformationResponse.ParametersLength;
  234. request.MaxDataCount = (ushort)maxOutputLength;
  235. TrySendMessage(request);
  236. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  237. if (reply != null)
  238. {
  239. return reply.Header.Status;
  240. }
  241. return NTStatus.STATUS_INVALID_SMB;
  242. }
  243. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  244. {
  245. throw new NotImplementedException();
  246. }
  247. public NTStatus GetFileSystemInformation(out QueryFSInformation result, QueryFSInformationLevel informationLevel)
  248. {
  249. result = null;
  250. int maxOutputLength = 4096;
  251. Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest();
  252. subcommand.InformationLevel = informationLevel;
  253. Transaction2Request request = new Transaction2Request();
  254. request.Setup = subcommand.GetSetup();
  255. request.TransParameters = subcommand.GetParameters(true);
  256. request.TransData = subcommand.GetData(true);
  257. request.TotalDataCount = (ushort)request.TransData.Length;
  258. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  259. request.MaxParameterCount = Transaction2QueryFSInformationResponse.ParametersLength;
  260. request.MaxDataCount = (ushort)maxOutputLength;
  261. TrySendMessage(request);
  262. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  263. if (reply != null)
  264. {
  265. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  266. {
  267. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  268. Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(response.TransParameters, response.TransData, true);
  269. result = subcommandResponse.GetQueryFSInformation(informationLevel, true);
  270. }
  271. return reply.Header.Status;
  272. }
  273. return NTStatus.STATUS_INVALID_SMB;
  274. }
  275. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  276. {
  277. result = null;
  278. return NTStatus.STATUS_NOT_SUPPORTED;
  279. }
  280. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  281. {
  282. return NTStatus.STATUS_NOT_SUPPORTED;
  283. }
  284. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  285. {
  286. throw new NotImplementedException();
  287. }
  288. public NTStatus Cancel(object ioRequest)
  289. {
  290. throw new NotImplementedException();
  291. }
  292. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  293. {
  294. if ((IoControlCode)ctlCode == IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  295. {
  296. return FsCtlPipeTranscieve(handle, input, out output, maxOutputLength);
  297. }
  298. output = null;
  299. NTTransactIOCTLRequest subcommand = new NTTransactIOCTLRequest();
  300. subcommand.FID = (ushort)handle;
  301. subcommand.FunctionCode = ctlCode;
  302. subcommand.IsFsctl = true;
  303. subcommand.Data = input;
  304. NTTransactRequest request = new NTTransactRequest();
  305. request.Function = subcommand.SubcommandName;
  306. request.Setup = subcommand.GetSetup();
  307. request.TransParameters = subcommand.GetParameters(true);
  308. request.TransData = subcommand.GetData();
  309. request.TotalDataCount = (uint)request.TransData.Length;
  310. request.TotalParameterCount = (uint)request.TransParameters.Length;
  311. request.MaxParameterCount = 0;
  312. request.MaxDataCount = (uint)maxOutputLength;
  313. TrySendMessage(request);
  314. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
  315. if (reply != null)
  316. {
  317. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
  318. {
  319. NTTransactResponse response = (NTTransactResponse)reply.Commands[0];
  320. NTTransactIOCTLResponse subcommandResponse = new NTTransactIOCTLResponse(response.Setup, response.TransData);
  321. output = subcommandResponse.Data;
  322. }
  323. return reply.Header.Status;
  324. }
  325. return NTStatus.STATUS_INVALID_SMB;
  326. }
  327. public NTStatus FsCtlPipeTranscieve(object handle, byte[] input, out byte[] output, int maxOutputLength)
  328. {
  329. output = null;
  330. TransactionTransactNamedPipeRequest subcommand = new TransactionTransactNamedPipeRequest();
  331. subcommand.FID = (ushort)handle;
  332. subcommand.WriteData = input;
  333. TransactionRequest request = new TransactionRequest();
  334. request.Setup = subcommand.GetSetup();
  335. request.TransParameters = subcommand.GetParameters(true);
  336. request.TransData = subcommand.GetData();
  337. request.TotalDataCount = (ushort)request.TransData.Length;
  338. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  339. request.MaxParameterCount = 0;
  340. request.MaxDataCount = (ushort)maxOutputLength;
  341. request.Name = @"\PIPE\";
  342. TrySendMessage(request);
  343. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION);
  344. if (reply != null)
  345. {
  346. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is TransactionResponse)
  347. {
  348. TransactionResponse response = (TransactionResponse)reply.Commands[0];
  349. TransactionTransactNamedPipeResponse subcommandResponse = new TransactionTransactNamedPipeResponse(response.TransData);
  350. output = subcommandResponse.ReadData;
  351. }
  352. return reply.Header.Status;
  353. }
  354. return NTStatus.STATUS_INVALID_SMB;
  355. }
  356. public NTStatus Disconnect()
  357. {
  358. TreeDisconnectRequest request = new TreeDisconnectRequest();
  359. TrySendMessage(request);
  360. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TREE_DISCONNECT);
  361. if (reply != null)
  362. {
  363. return reply.Header.Status;
  364. }
  365. return NTStatus.STATUS_INVALID_SMB;
  366. }
  367. private static ExtendedFileAttributes ToExtendedFileAttributes(FileAttributes fileAttributes)
  368. {
  369. // We only return flags that can be used with NtCreateFile
  370. ExtendedFileAttributes extendedFileAttributes = ExtendedFileAttributes.ReadOnly |
  371. ExtendedFileAttributes.Hidden |
  372. ExtendedFileAttributes.System |
  373. ExtendedFileAttributes.Archive |
  374. ExtendedFileAttributes.Normal |
  375. ExtendedFileAttributes.Temporary |
  376. ExtendedFileAttributes.Offline |
  377. ExtendedFileAttributes.Encrypted;
  378. return (extendedFileAttributes & (ExtendedFileAttributes)fileAttributes);
  379. }
  380. private void TrySendMessage(SMB1Command request)
  381. {
  382. m_client.TrySendMessage(request, m_treeID);
  383. }
  384. private static FileStatus ToFileStatus(CreateDisposition createDisposition)
  385. {
  386. switch (createDisposition)
  387. {
  388. case CreateDisposition.FILE_SUPERSEDE:
  389. return FileStatus.FILE_SUPERSEDED;
  390. case CreateDisposition.FILE_OPEN:
  391. return FileStatus.FILE_OPENED;
  392. case CreateDisposition.FILE_CREATE:
  393. return FileStatus.FILE_CREATED;
  394. case CreateDisposition.FILE_OPEN_IF:
  395. return FileStatus.FILE_OVERWRITTEN;
  396. case CreateDisposition.FILE_OVERWRITE:
  397. return FileStatus.FILE_EXISTS;
  398. case CreateDisposition.FILE_OVERWRITE_IF:
  399. return FileStatus.FILE_DOES_NOT_EXIST;
  400. default:
  401. return FileStatus.FILE_OPENED;
  402. }
  403. }
  404. }
  405. }