NTCreateAndXRequest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 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. ushort 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. dataOffset = 1; // 1 byte padding for 2 byte alignment
  58. }
  59. FileName = SMB1Helper.ReadSMBString(this.SMBData, dataOffset, isUnicode);
  60. }
  61. public override byte[] GetBytes(bool isUnicode)
  62. {
  63. ushort nameLength = (ushort)FileName.Length;
  64. this.SMBParameters = new byte[ParametersLength];
  65. ByteWriter.WriteByte(this.SMBParameters, 0, (byte)AndXCommand);
  66. ByteWriter.WriteByte(this.SMBParameters, 1, AndXReserved);
  67. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, AndXOffset);
  68. ByteWriter.WriteByte(this.SMBParameters, 4, Reserved);
  69. LittleEndianWriter.WriteUInt16(this.SMBParameters, 5, nameLength);
  70. LittleEndianWriter.WriteUInt32(this.SMBParameters, 7, (uint)Flags);
  71. LittleEndianWriter.WriteUInt32(this.SMBParameters, 11, RootDirectoryFID);
  72. LittleEndianWriter.WriteUInt32(this.SMBParameters, 15, (uint)DesiredAccess);
  73. LittleEndianWriter.WriteInt64(this.SMBParameters, 19, AllocationSize);
  74. LittleEndianWriter.WriteUInt32(this.SMBParameters, 27, (uint)ExtFileAttributes);
  75. LittleEndianWriter.WriteUInt32(this.SMBParameters, 31, (uint)ShareAccess);
  76. LittleEndianWriter.WriteUInt32(this.SMBParameters, 35, (uint)CreateDisposition);
  77. LittleEndianWriter.WriteUInt32(this.SMBParameters, 39, (uint)CreateOptions);
  78. LittleEndianWriter.WriteUInt32(this.SMBParameters, 43, (uint)ImpersonationLevel);
  79. ByteWriter.WriteByte(this.SMBParameters, 47, (byte)SecurityFlags);
  80. if (isUnicode)
  81. {
  82. int padding = 1;
  83. this.SMBData = new byte[padding + FileName.Length * 2 + 2];
  84. int offset = padding;
  85. ByteWriter.WriteNullTerminatedUTF16String(this.SMBData, offset, FileName);
  86. }
  87. else
  88. {
  89. this.SMBData = new byte[FileName.Length + 1];
  90. ByteWriter.WriteNullTerminatedUTF16String(this.SMBData, 0, FileName);
  91. }
  92. return base.GetBytes(isUnicode);
  93. }
  94. public override CommandName CommandName
  95. {
  96. get
  97. {
  98. return CommandName.SMB_COM_NT_CREATE_ANDX;
  99. }
  100. }
  101. }
  102. }