NTCreateAndXRequest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_NT_CREATE_ANDX Request
  15. /// </summary>
  16. public class NTCreateAndXRequest : SMBAndXCommand
  17. {
  18. public const int ParametersLength = 48;
  19. // Parameters:
  20. //CommandName AndXCommand;
  21. //byte AndXReserved;
  22. //ushort AndXOffset;
  23. public byte Reserved;
  24. private ushort NameLength; // in bytes
  25. public NTCreateFlags Flags;
  26. public uint RootDirectoryFID;
  27. public FileAccessMask DesiredAccess;
  28. public long AllocationSize;
  29. public ExtendedFileAttributes ExtFileAttributes;
  30. public ShareAccess ShareAccess;
  31. public CreateDisposition CreateDisposition;
  32. public CreateOptions CreateOptions;
  33. public ImpersonationLevel ImpersonationLevel;
  34. public SecurityFlags SecurityFlags;
  35. // Data:
  36. public string FileName; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  37. public NTCreateAndXRequest() : base()
  38. {
  39. }
  40. public NTCreateAndXRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  41. {
  42. Reserved = ByteReader.ReadByte(this.SMBParameters, 4);
  43. NameLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 5);
  44. Flags = (NTCreateFlags)LittleEndianConverter.ToUInt32(this.SMBParameters, 7);
  45. RootDirectoryFID = LittleEndianConverter.ToUInt32(this.SMBParameters, 11);
  46. DesiredAccess = (FileAccessMask)LittleEndianConverter.ToUInt32(this.SMBParameters, 15);
  47. AllocationSize = LittleEndianConverter.ToInt64(this.SMBParameters, 19);
  48. ExtFileAttributes = (ExtendedFileAttributes)LittleEndianConverter.ToUInt32(this.SMBParameters, 27);
  49. ShareAccess = (ShareAccess)LittleEndianConverter.ToUInt32(this.SMBParameters, 31);
  50. CreateDisposition = (CreateDisposition)LittleEndianConverter.ToUInt32(this.SMBParameters, 35);
  51. CreateOptions = (CreateOptions)LittleEndianConverter.ToUInt32(this.SMBParameters, 39);
  52. ImpersonationLevel = (ImpersonationLevel)LittleEndianConverter.ToUInt32(this.SMBParameters, 43);
  53. SecurityFlags = (SecurityFlags)ByteReader.ReadByte(this.SMBParameters, 47);
  54. int dataOffset = 0;
  55. if (isUnicode)
  56. {
  57. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  58. // Note: SMBData starts at an odd offset.
  59. dataOffset = 1;
  60. }
  61. FileName = SMB1Helper.ReadSMBString(this.SMBData, dataOffset, isUnicode);
  62. }
  63. public override byte[] GetBytes(bool isUnicode)
  64. {
  65. NameLength = (ushort)FileName.Length;
  66. this.SMBParameters = new byte[ParametersLength];
  67. ByteWriter.WriteByte(this.SMBParameters, 0, (byte)AndXCommand);
  68. ByteWriter.WriteByte(this.SMBParameters, 1, AndXReserved);
  69. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, AndXOffset);
  70. ByteWriter.WriteByte(this.SMBParameters, 4, Reserved);
  71. LittleEndianWriter.WriteUInt16(this.SMBParameters, 5, NameLength);
  72. LittleEndianWriter.WriteUInt32(this.SMBParameters, 7, (uint)Flags);
  73. LittleEndianWriter.WriteUInt32(this.SMBParameters, 11, RootDirectoryFID);
  74. LittleEndianWriter.WriteUInt32(this.SMBParameters, 15, (uint)DesiredAccess);
  75. LittleEndianWriter.WriteInt64(this.SMBParameters, 19, AllocationSize);
  76. LittleEndianWriter.WriteUInt32(this.SMBParameters, 27, (uint)ExtFileAttributes);
  77. LittleEndianWriter.WriteUInt32(this.SMBParameters, 31, (uint)ShareAccess);
  78. LittleEndianWriter.WriteUInt32(this.SMBParameters, 35, (uint)CreateDisposition);
  79. LittleEndianWriter.WriteUInt32(this.SMBParameters, 39, (uint)CreateOptions);
  80. LittleEndianWriter.WriteUInt32(this.SMBParameters, 43, (uint)ImpersonationLevel);
  81. ByteWriter.WriteByte(this.SMBParameters, 47, (byte)SecurityFlags);
  82. int padding = 0;
  83. if (isUnicode)
  84. {
  85. padding = 1;
  86. this.SMBData = new byte[padding + FileName.Length * 2 + 2];
  87. }
  88. else
  89. {
  90. this.SMBData = new byte[FileName.Length + 1];
  91. }
  92. SMB1Helper.WriteSMBString(this.SMBData, padding, isUnicode, FileName);
  93. return base.GetBytes(isUnicode);
  94. }
  95. public override CommandName CommandName
  96. {
  97. get
  98. {
  99. return CommandName.SMB_COM_NT_CREATE_ANDX;
  100. }
  101. }
  102. }
  103. }