NTCreateAndXRequest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // ushort NameLength; // in bytes
  25. public NTCreateFlags Flags;
  26. public uint RootDirectoryFID;
  27. public AccessMask 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. ushort nameLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 5);
  44. Flags = (NTCreateFlags)LittleEndianConverter.ToUInt32(this.SMBParameters, 7);
  45. RootDirectoryFID = LittleEndianConverter.ToUInt32(this.SMBParameters, 11);
  46. DesiredAccess = (AccessMask)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. ushort nameLength = (ushort)FileName.Length;
  66. if (isUnicode)
  67. {
  68. nameLength *= 2;
  69. }
  70. this.SMBParameters = new byte[ParametersLength];
  71. ByteWriter.WriteByte(this.SMBParameters, 0, (byte)AndXCommand);
  72. ByteWriter.WriteByte(this.SMBParameters, 1, AndXReserved);
  73. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, AndXOffset);
  74. ByteWriter.WriteByte(this.SMBParameters, 4, Reserved);
  75. LittleEndianWriter.WriteUInt16(this.SMBParameters, 5, nameLength);
  76. LittleEndianWriter.WriteUInt32(this.SMBParameters, 7, (uint)Flags);
  77. LittleEndianWriter.WriteUInt32(this.SMBParameters, 11, RootDirectoryFID);
  78. LittleEndianWriter.WriteUInt32(this.SMBParameters, 15, (uint)DesiredAccess);
  79. LittleEndianWriter.WriteInt64(this.SMBParameters, 19, AllocationSize);
  80. LittleEndianWriter.WriteUInt32(this.SMBParameters, 27, (uint)ExtFileAttributes);
  81. LittleEndianWriter.WriteUInt32(this.SMBParameters, 31, (uint)ShareAccess);
  82. LittleEndianWriter.WriteUInt32(this.SMBParameters, 35, (uint)CreateDisposition);
  83. LittleEndianWriter.WriteUInt32(this.SMBParameters, 39, (uint)CreateOptions);
  84. LittleEndianWriter.WriteUInt32(this.SMBParameters, 43, (uint)ImpersonationLevel);
  85. ByteWriter.WriteByte(this.SMBParameters, 47, (byte)SecurityFlags);
  86. int padding = 0;
  87. if (isUnicode)
  88. {
  89. padding = 1;
  90. this.SMBData = new byte[padding + FileName.Length * 2 + 2];
  91. }
  92. else
  93. {
  94. this.SMBData = new byte[FileName.Length + 1];
  95. }
  96. SMB1Helper.WriteSMBString(this.SMBData, padding, isUnicode, FileName);
  97. return base.GetBytes(isUnicode);
  98. }
  99. public override CommandName CommandName
  100. {
  101. get
  102. {
  103. return CommandName.SMB_COM_NT_CREATE_ANDX;
  104. }
  105. }
  106. }
  107. }