SecurityDescriptor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Utilities;
  10. namespace SMBLibrary
  11. {
  12. /// <summary>
  13. /// [MS-DTYP] SECURITY_DESCRIPTOR
  14. /// </summary>
  15. public class SecurityDescriptor
  16. {
  17. public const int FixedLength = 20;
  18. public byte Revision;
  19. public byte Sbz1;
  20. public SecurityDescriptorControl Control;
  21. // uint OffsetOwner;
  22. // uint OffsetGroup;
  23. // uint OffsetSacl;
  24. // uint OffsetDacl;
  25. public SID OwnerSid;
  26. public SID GroupSid;
  27. public ACL Sacl;
  28. public ACL Dacl;
  29. public SecurityDescriptor()
  30. {
  31. Revision = 0x01;
  32. }
  33. public SecurityDescriptor(byte[] buffer, int offset)
  34. {
  35. Revision = ByteReader.ReadByte(buffer, ref offset);
  36. Sbz1 = ByteReader.ReadByte(buffer, ref offset);
  37. Control = (SecurityDescriptorControl)LittleEndianReader.ReadUInt16(buffer, ref offset);
  38. uint offsetOwner = LittleEndianReader.ReadUInt32(buffer, ref offset);
  39. uint offsetGroup = LittleEndianReader.ReadUInt32(buffer, ref offset);
  40. uint offsetSacl = LittleEndianReader.ReadUInt32(buffer, ref offset);
  41. uint offsetDacl = LittleEndianReader.ReadUInt32(buffer, ref offset);
  42. if (offsetOwner != 0)
  43. {
  44. OwnerSid = new SID(buffer, (int)offsetOwner);
  45. }
  46. if (offsetGroup != 0)
  47. {
  48. GroupSid = new SID(buffer, (int)offsetGroup);
  49. }
  50. if (offsetSacl != 0)
  51. {
  52. Sacl = new ACL(buffer, (int)offsetSacl);
  53. }
  54. if (offsetDacl != 0)
  55. {
  56. Dacl = new ACL(buffer, (int)offsetDacl);
  57. }
  58. }
  59. public byte[] GetBytes()
  60. {
  61. byte[] buffer = new byte[Length];
  62. uint offsetOwner = 0;
  63. uint offsetGroup = 0;
  64. uint offsetSacl = 0;
  65. uint offsetDacl = 0;
  66. int offset = FixedLength;
  67. if (OwnerSid != null)
  68. {
  69. offsetOwner = (uint)offset;
  70. offset += OwnerSid.Length;
  71. }
  72. if (GroupSid != null)
  73. {
  74. offsetGroup = (uint)offset;
  75. offset += GroupSid.Length;
  76. }
  77. if (Sacl != null)
  78. {
  79. offsetSacl = (uint)offset;
  80. offset += Sacl.Length;
  81. }
  82. if (Dacl != null)
  83. {
  84. offsetDacl = (uint)offset;
  85. offset += Dacl.Length;
  86. }
  87. offset = 0;
  88. ByteWriter.WriteByte(buffer, ref offset, Revision);
  89. ByteWriter.WriteByte(buffer, ref offset, Sbz1);
  90. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)Control);
  91. LittleEndianWriter.WriteUInt32(buffer, ref offset, offsetOwner);
  92. LittleEndianWriter.WriteUInt32(buffer, ref offset, offsetGroup);
  93. LittleEndianWriter.WriteUInt32(buffer, ref offset, offsetSacl);
  94. LittleEndianWriter.WriteUInt32(buffer, ref offset, offsetDacl);
  95. if (OwnerSid != null)
  96. {
  97. OwnerSid.WriteBytes(buffer, ref offset);
  98. }
  99. if (GroupSid != null)
  100. {
  101. GroupSid.WriteBytes(buffer, ref offset);
  102. }
  103. if (Sacl != null)
  104. {
  105. Sacl.WriteBytes(buffer, ref offset);
  106. }
  107. if (Dacl != null)
  108. {
  109. Dacl.WriteBytes(buffer, ref offset);
  110. }
  111. return buffer;
  112. }
  113. public int Length
  114. {
  115. get
  116. {
  117. int length = FixedLength;
  118. if (OwnerSid != null)
  119. {
  120. length += OwnerSid.Length;
  121. }
  122. if (GroupSid != null)
  123. {
  124. length += GroupSid.Length;
  125. }
  126. if (Sacl != null)
  127. {
  128. length += Sacl.Length;
  129. }
  130. if (Dacl != null)
  131. {
  132. length += Dacl.Length;
  133. }
  134. return length;
  135. }
  136. }
  137. }
  138. }