SID.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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] 2.4.2.2 - SID (Packet Representation)
  14. /// </summary>
  15. public class SID
  16. {
  17. public static readonly byte[] WORLD_SID_AUTHORITY = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  18. public static readonly byte[] LOCAL_SID_AUTHORITY = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 };
  19. public static readonly byte[] CREATOR_SID_AUTHORITY = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 };
  20. public static readonly byte[] SECURITY_NT_AUTHORITY = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };
  21. public const int FixedLength = 8;
  22. public byte Revision;
  23. // byte SubAuthorityCount;
  24. public byte[] IdentifierAuthority; // 6 bytes
  25. public List<uint> SubAuthority = new List<uint>();
  26. public SID()
  27. {
  28. Revision = 0x01;
  29. }
  30. public SID(byte[] buffer, int offset)
  31. {
  32. Revision = ByteReader.ReadByte(buffer, ref offset);
  33. byte subAuthorityCount = ByteReader.ReadByte(buffer, ref offset);
  34. IdentifierAuthority = ByteReader.ReadBytes(buffer, ref offset, 6);
  35. for (int index = 0; index < subAuthorityCount; index++)
  36. {
  37. uint entry = LittleEndianReader.ReadUInt32(buffer, ref offset);
  38. SubAuthority.Add(entry);
  39. }
  40. }
  41. public void WriteBytes(byte[] buffer, ref int offset)
  42. {
  43. byte subAuthorityCount = (byte)SubAuthority.Count;
  44. ByteWriter.WriteByte(buffer, ref offset, Revision);
  45. ByteWriter.WriteByte(buffer, ref offset, subAuthorityCount);
  46. ByteWriter.WriteBytes(buffer, ref offset, IdentifierAuthority, 6);
  47. for (int index = 0; index < SubAuthority.Count; index++)
  48. {
  49. LittleEndianWriter.WriteUInt32(buffer, ref offset, SubAuthority[index]);
  50. }
  51. }
  52. public int Length
  53. {
  54. get
  55. {
  56. return FixedLength + SubAuthority.Count * 4;
  57. }
  58. }
  59. public static SID Everyone
  60. {
  61. get
  62. {
  63. SID sid = new SID();
  64. sid.IdentifierAuthority = WORLD_SID_AUTHORITY;
  65. sid.SubAuthority.Add(0);
  66. return sid;
  67. }
  68. }
  69. public static SID LocalSystem
  70. {
  71. get
  72. {
  73. SID sid = new SID();
  74. sid.IdentifierAuthority = SECURITY_NT_AUTHORITY;
  75. sid.SubAuthority.Add(18);
  76. return sid;
  77. }
  78. }
  79. }
  80. }