SMB1FileStore.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 SetFileSystemInformation(FileSystemInformation information)
  248. {
  249. throw new NotImplementedException();
  250. }
  251. public NTStatus GetFileSystemInformation(out QueryFSInformation result, QueryFSInformationLevel informationLevel)
  252. {
  253. result = null;
  254. int maxOutputLength = 4096;
  255. Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest();
  256. subcommand.InformationLevel = informationLevel;
  257. Transaction2Request request = new Transaction2Request();
  258. request.Setup = subcommand.GetSetup();
  259. request.TransParameters = subcommand.GetParameters(true);
  260. request.TransData = subcommand.GetData(true);
  261. request.TotalDataCount = (ushort)request.TransData.Length;
  262. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  263. request.MaxParameterCount = Transaction2QueryFSInformationResponse.ParametersLength;
  264. request.MaxDataCount = (ushort)maxOutputLength;
  265. TrySendMessage(request);
  266. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  267. if (reply != null)
  268. {
  269. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  270. {
  271. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  272. Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(response.TransParameters, response.TransData, true);
  273. result = subcommandResponse.GetQueryFSInformation(informationLevel, true);
  274. }
  275. return reply.Header.Status;
  276. }
  277. return NTStatus.STATUS_INVALID_SMB;
  278. }
  279. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  280. {
  281. result = null;
  282. return NTStatus.STATUS_NOT_SUPPORTED;
  283. }
  284. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  285. {
  286. return NTStatus.STATUS_NOT_SUPPORTED;
  287. }
  288. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  289. {
  290. throw new NotImplementedException();
  291. }
  292. public NTStatus Cancel(object ioRequest)
  293. {
  294. throw new NotImplementedException();
  295. }
  296. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  297. {
  298. if ((IoControlCode)ctlCode == IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  299. {
  300. return FsCtlPipeTranscieve(handle, input, out output, maxOutputLength);
  301. }
  302. output = null;
  303. NTTransactIOCTLRequest subcommand = new NTTransactIOCTLRequest();
  304. subcommand.FID = (ushort)handle;
  305. subcommand.FunctionCode = ctlCode;
  306. subcommand.IsFsctl = true;
  307. subcommand.Data = input;
  308. NTTransactRequest request = new NTTransactRequest();
  309. request.Function = subcommand.SubcommandName;
  310. request.Setup = subcommand.GetSetup();
  311. request.TransParameters = subcommand.GetParameters(true);
  312. request.TransData = subcommand.GetData();
  313. request.TotalDataCount = (uint)request.TransData.Length;
  314. request.TotalParameterCount = (uint)request.TransParameters.Length;
  315. request.MaxParameterCount = 0;
  316. request.MaxDataCount = (uint)maxOutputLength;
  317. TrySendMessage(request);
  318. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
  319. if (reply != null)
  320. {
  321. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
  322. {
  323. NTTransactResponse response = (NTTransactResponse)reply.Commands[0];
  324. NTTransactIOCTLResponse subcommandResponse = new NTTransactIOCTLResponse(response.Setup, response.TransData);
  325. output = subcommandResponse.Data;
  326. }
  327. return reply.Header.Status;
  328. }
  329. return NTStatus.STATUS_INVALID_SMB;
  330. }
  331. public NTStatus FsCtlPipeTranscieve(object handle, byte[] input, out byte[] output, int maxOutputLength)
  332. {
  333. output = null;
  334. TransactionTransactNamedPipeRequest subcommand = new TransactionTransactNamedPipeRequest();
  335. subcommand.FID = (ushort)handle;
  336. subcommand.WriteData = input;
  337. TransactionRequest request = new TransactionRequest();
  338. request.Setup = subcommand.GetSetup();
  339. request.TransParameters = subcommand.GetParameters(true);
  340. request.TransData = subcommand.GetData();
  341. request.TotalDataCount = (ushort)request.TransData.Length;
  342. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  343. request.MaxParameterCount = 0;
  344. request.MaxDataCount = (ushort)maxOutputLength;
  345. request.Name = @"\PIPE\";
  346. TrySendMessage(request);
  347. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION);
  348. if (reply != null)
  349. {
  350. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is TransactionResponse)
  351. {
  352. TransactionResponse response = (TransactionResponse)reply.Commands[0];
  353. TransactionTransactNamedPipeResponse subcommandResponse = new TransactionTransactNamedPipeResponse(response.TransData);
  354. output = subcommandResponse.ReadData;
  355. }
  356. return reply.Header.Status;
  357. }
  358. return NTStatus.STATUS_INVALID_SMB;
  359. }
  360. public NTStatus Disconnect()
  361. {
  362. TreeDisconnectRequest request = new TreeDisconnectRequest();
  363. TrySendMessage(request);
  364. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TREE_DISCONNECT);
  365. if (reply != null)
  366. {
  367. return reply.Header.Status;
  368. }
  369. return NTStatus.STATUS_INVALID_SMB;
  370. }
  371. private static ExtendedFileAttributes ToExtendedFileAttributes(FileAttributes fileAttributes)
  372. {
  373. // We only return flags that can be used with NtCreateFile
  374. ExtendedFileAttributes extendedFileAttributes = ExtendedFileAttributes.ReadOnly |
  375. ExtendedFileAttributes.Hidden |
  376. ExtendedFileAttributes.System |
  377. ExtendedFileAttributes.Archive |
  378. ExtendedFileAttributes.Normal |
  379. ExtendedFileAttributes.Temporary |
  380. ExtendedFileAttributes.Offline |
  381. ExtendedFileAttributes.Encrypted;
  382. return (extendedFileAttributes & (ExtendedFileAttributes)fileAttributes);
  383. }
  384. private void TrySendMessage(SMB1Command request)
  385. {
  386. m_client.TrySendMessage(request, m_treeID);
  387. }
  388. private static FileStatus ToFileStatus(CreateDisposition createDisposition)
  389. {
  390. switch (createDisposition)
  391. {
  392. case CreateDisposition.FILE_SUPERSEDE:
  393. return FileStatus.FILE_SUPERSEDED;
  394. case CreateDisposition.FILE_OPEN:
  395. return FileStatus.FILE_OPENED;
  396. case CreateDisposition.FILE_CREATE:
  397. return FileStatus.FILE_CREATED;
  398. case CreateDisposition.FILE_OPEN_IF:
  399. return FileStatus.FILE_OVERWRITTEN;
  400. case CreateDisposition.FILE_OVERWRITE:
  401. return FileStatus.FILE_EXISTS;
  402. case CreateDisposition.FILE_OVERWRITE_IF:
  403. return FileStatus.FILE_DOES_NOT_EXIST;
  404. default:
  405. return FileStatus.FILE_OPENED;
  406. }
  407. }
  408. }
  409. }