# Copyright: (c) 2019, Jordan Borean (@jborean93) # MIT License (see LICENSE or https://opensource.org/licenses/MIT) import struct from collections import OrderedDict from smbprotocol.structure import ( BytesField, EnumField, FlagField, IntField, ListField, Structure, StructureField, ) class AccessMask: """ [MS-DTYP] 2.4.3 ACCESS_MASK 32-bit set of flags that are used to encode the user rights to an object. This is just a generic setup of access mask flags to set an can vary from the object being set. When setting the AccessMask on an ACE packet, any 32-bit value can be used and this is just as a guideline. """ GENERIC_READ = 0x80000000 GENERIC_WRITE = 0x40000000 GENERIC_EXECUTE = 0x20000000 GENERIC_ALL = 0x10000000 MAXIMUM_ALLOWED = 0x02000000 ACCESS_SYSTEM_SECURITY = 0x01000000 SYNCHRONIZE = 0x00100000 WRITE_OWNER = 0x00080000 WRITE_DACL = 0x00040000 READ_CONTROL = 0x00020000 DELETE = 0x00010000 class AceType: """ [MS-DTYP] 2.4.4.1 ACE_HEADER AceType The type of ACE in the ACE packet. """ # Current only have structures for the first 3 ACCESS_ALLOWED_ACE_TYPE = 0x00 ACCESS_DENIED_ACE_TYPE = 0x01 SYSTEM_AUDIT_ACE_TYPE = 0x02 # No structures are defined for the below SYSTEM_ALARM_ACE_TYPE = 0x03 ACCESS_ALLOWED_COMPOUND_ACE_TYPE = 0x04 ACCESS_ALLOWED_OBJECT_ACE_TYPE = 0x05 ACCESS_DENIED_OBJECT_ACE_TYPE = 0x06 SYSTEM_AUDIT_OBJECT_ACE_TYPE = 0x07 SYSTEM_ALARM_OBJECT_ACE_TYPE = 0x08 ACCESS_ALLOWED_CALLBACK_ACE_TYPE = 0x09 ACCESS_DENIED_CALLBACK_ACE_TYPE = 0x0A ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE = 0x0B ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE = 0x0C SYSTEM_AUDIT_CALLBACK_ACE_TYPE = 0x0D SYSTEM_ALARM_CALLBACK_ACE_TYPE = 0x0E SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE = 0x0F SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE = 0x10 SYSTEM_MANDATORY_LABEL_ACE_TYPE = 0x11 SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE = 0x12 SYSTEM_SCOPED_POLICY_ID_ACE_TYPE = 0x13 class AceFlags: """ [MS-DTYP] 2.4.4.1 ACE_HEADER AceFlags Controls the ACE specified in the ACE packet. """ CONTAINER_INHERIT_ACE = 0x02 FAILED_ACCESS_ACE_FLAG = 0x80 INHERIT_ONLY_ACE = 0x08 INHERITED_ACE = 0x10 NO_PROPAGATE_INHERITY_ACE = 0x04 OBJECT_INHERIT_ACE = 0x01 SUCCESSFUL_ACCESS_ACE_FLAG = 0x40 class AclRevision: """ [MS-DTYP] 2.4.5 ACL AclRevision ACL_REVISION - AceType 0, 1, 2, 3, 11, 12, 13 are valid ACL_REVISION_DS - AceType 5, 6, 7, 8, 11 are valid (Directory Service) """ ACL_REVISION = 0x02 ACL_REVISION_DS = 0x04 # not natively supported yet class SDControl: """ [MS-DTYP] 2.4.6 SECURITY_DESCRIPTOR Control Specifies control access bit flags. """ SELF_RELATIVE = 0x8000 RM_CONTROL_VALID = 0x4000 SACL_PROTECTED = 0x2000 DACL_PROTECTED = 0x1000 SACL_AUTO_INHERITED = 0x0800 DACL_AUTO_INHERITED = 0x0400 SACL_COMPUTED_INHERITANCE_REQUIRED = 0x0200 DACL_COMPUTED_INHERITANCE_REQUIRED = 0x0100 SERVER_SECURITY = 0x0080 DACL_TRUSTED = 0x0040 SACL_DEFAULTED = 0x0020 SACL_PRESENT = 0x0010 DACL_DEFAULTED = 0x0008 DACL_PRESENT = 0x0004 GROUP_DEFAULTED = 0x0002 OWNER_DEFAULTED = 0x0001 NONE = 0x0000 class SIDPacket(Structure): """ [MS-DTYP] 2.4.2.2 SID--Packet Representation The packet representation of the SID type for use by block protocols. While the values can be set explicitly, it may be easier to use the from_string function store the byte structure from the string format. """ def __init__(self): self.fields = OrderedDict( [ ("revision", IntField(size=1, default=1)), ("sub_authority_count", IntField(size=1, default=lambda s: len(s["sub_authorities"].get_value()))), ("reserved", IntField(size=2)), ("identifier_authority", IntField(size=4, little_endian=False)), ( "sub_authorities", ListField(list_type=IntField(size=4), list_count=lambda s: s["sub_authority_count"].get_value()), ), ] ) super().__init__() def __str__(self): revision = self["revision"].get_value() id_authority = self["identifier_authority"].get_value() sub_authorities = self["sub_authorities"].get_value() authorities_string = "-".join(str(x) for x in sub_authorities) sid_string = f"S-{revision}-{id_authority}-{authorities_string}" return sid_string def from_string(self, sid_string): """ Used to set the structure parameters based on the input string :param sid_string: String of the sid in S-x-x-x-x form """ if not sid_string.startswith("S-"): raise ValueError("A SID string must start with S-") sid_entries = sid_string.split("-") if len(sid_entries) < 3: raise ValueError( "A SID string must start with S and contain a revision and identifier authority, e.g. S-1-0" ) revision = int(sid_entries[1]) id_authority = int(sid_entries[2]) sub_authorities = [int(i) for i in sid_entries[3:]] self["revision"].set_value(revision) self["identifier_authority"].set_value(id_authority) self["sub_authorities"] = sub_authorities class AccessAllowedAce(Structure): """ [MS-DTYP] 2.4.4.3 ACCESS_ALLOWED_ACE Used for the DACL that controls access to an object. """ def __init__(self): self.fields = OrderedDict( [ ("ace_type", EnumField(size=1, default=AceType.ACCESS_ALLOWED_ACE_TYPE, enum_type=AceType)), ("ace_flags", FlagField(size=1, flag_type=AceFlags)), ("ace_size", IntField(size=2, default=lambda s: 8 + len(s["sid"]))), ("mask", FlagField(size=4, flag_type=AccessMask, flag_strict=False)), ("sid", StructureField(structure_type=SIDPacket)), ] ) super().__init__() class AccessDeniedAce(Structure): """ [MS-DTYP] 2.4.4.4 ACCESS_DENIED_ACE Used for the DACL that controls denies to an object. """ def __init__(self): self.fields = OrderedDict( [ ("ace_type", EnumField(size=1, default=AceType.ACCESS_DENIED_ACE_TYPE, enum_type=AceType)), ("ace_flags", FlagField(size=1, flag_type=AceFlags)), ("ace_size", IntField(size=2, default=lambda s: 8 + len(s["sid"]))), ("mask", FlagField(size=4, flag_type=AccessMask, flag_strict=False)), ("sid", StructureField(structure_type=SIDPacket)), ] ) super().__init__() class SystemAuditAce(Structure): """ [MS-DTYP] 2.4.4.10 SYSTEM_AUDIT_ACE Used for the SACL that specifies what types of access cause system-level notifications. """ def __init__(self): self.fields = OrderedDict( [ ("ace_type", EnumField(size=1, default=AceType.SYSTEM_AUDIT_ACE_TYPE, enum_type=AceType)), ("ace_flags", FlagField(size=1, flag_type=AceFlags)), ("ace_size", IntField(size=2, default=lambda s: 8 + len(s["sid"]))), ("mask", FlagField(size=4, flag_type=AccessMask, flag_strict=False)), ("sid", StructureField(structure_type=SIDPacket)), ] ) super().__init__() class AclPacket(Structure): """ [MS-DTYP] 2.4.5 ACL Access Control List packet is used to specify a list of individual ACEs. An ACL is said to be in canonical form if: All explicit ACEs are placed before inherited ACEs Within the explicit ACEs, deny ACEs come before grant ACEs Deny ACEs on the object come before deny ACEs on a child or property Grant ACEs on the object come before grant ACEs on a child or property Inherited ACEs are placed in the order in which they were inherited """ def __init__(self): self.fields = OrderedDict( [ ("acl_revision", EnumField(size=1, default=AclRevision.ACL_REVISION, enum_type=AclRevision)), ("sbz1", IntField(size=1)), ("acl_size", IntField(size=2, default=lambda s: 8 + len(s["aces"]))), ("ace_count", IntField(size=2, default=lambda s: len(s["aces"].get_value()))), ("sbz2", IntField(size=2)), ( "aces", ListField( list_count=lambda s: s["ace_count"].get_value(), unpack_func=AclPacket._unpack_aces, ), ), ] ) super().__init__() @staticmethod def _unpack_aces(structure, data): aces = [] while data != b"" and len(aces) < structure["ace_count"].value: ace_type = struct.unpack("