<s> from . . import BinaryParser <EOL> from . . BinaryParser import Block <EOL> from . . BinaryParser import Nestable <EOL> class NULL_OBJECT ( object ) : <EOL> def __init__ ( self ) : <EOL> super ( NULL_OBJECT , self ) . __init__ ( ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return <NUM_LIT:0> <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:0> <EOL> null_object = NULL_OBJECT ( ) <EOL> class SECURITY_DESCRIPTOR_CONTROL : <EOL> SE_OWNER_DEFAULTED = <NUM_LIT:1> << <NUM_LIT:0> <EOL> SE_GROUP_DEFAULTED = <NUM_LIT:1> << <NUM_LIT:1> <EOL> SE_DACL_PRESENT = <NUM_LIT:1> << <NUM_LIT:2> <EOL> SE_DACL_DEFAULTED = <NUM_LIT:1> << <NUM_LIT:3> <EOL> SE_SACL_PRESENT = <NUM_LIT:1> << <NUM_LIT:4> <EOL> SE_SACL_DEFAULTED = <NUM_LIT:1> << <NUM_LIT:5> <EOL> SE_SACL_UNUSED0 = <NUM_LIT:1> << <NUM_LIT:6> <EOL> SE_SACL_UNUSED1 = <NUM_LIT:1> << <NUM_LIT:7> <EOL> SE_DACL_AUTO_INHERIT_REQ = <NUM_LIT:1> << <NUM_LIT:8> <EOL> SE_SACL_AUTO_INHERIT_REQ = <NUM_LIT:1> << <NUM_LIT:9> <EOL> SE_DACL_AUTO_INHERITED = <NUM_LIT:1> << <NUM_LIT:10> <EOL> SE_SACL_AUTO_INHERITED = <NUM_LIT:1> << <NUM_LIT:11> <EOL> SE_DACL_PROTECTED = <NUM_LIT:1> << <NUM_LIT:12> <EOL> SE_SACL_PROTECTED = <NUM_LIT:1> << <NUM_LIT> <EOL> SE_RM_CONTROL_VALID = <NUM_LIT:1> << <NUM_LIT> <EOL> SE_SELF_RELATIVE = <NUM_LIT:1> << <NUM_LIT:15> <EOL> class SID_IDENTIFIER_AUTHORITY ( Block , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SID_IDENTIFIER_AUTHORITY , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return <NUM_LIT:6> <EOL> def __len__ ( self ) : <EOL> return SID_IDENTIFIER_AUTHORITY . structure_size ( self . _buf , self . absolute_offset ( <NUM_LIT> ) , None ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:%s>" % ( self . high_part ( ) << <NUM_LIT:32> + self . low_part ( ) ) <EOL> class SID ( Block , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SID , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( SID_IDENTIFIER_AUTHORITY , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , count = self . sub_authority_count ( ) ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> sub_auth_count = BinaryParser . read_byte ( buf , offset + <NUM_LIT:1> ) <EOL> auth_size = SID_IDENTIFIER_AUTHORITY . structure_size ( buf , offset + <NUM_LIT:2> , parent ) <EOL> return <NUM_LIT:2> + auth_size + ( sub_auth_count * <NUM_LIT:4> ) <EOL> def __len__ ( self ) : <EOL> return self . _off_sub_authorities + ( self . sub_authority_count ( ) * <NUM_LIT:4> ) <EOL> def string ( self ) : <EOL> ret = "<STR_LIT>" % ( self . revision ( ) , self . identifier_authority ( ) ) <EOL> for sub_auth in self . sub_authorities ( ) : <EOL> ret += "<STR_LIT>" % ( str ( sub_auth ) ) <EOL> return ret <EOL> class ACE_TYPES : <EOL> """<STR_LIT>""" <EOL> ACCESS_MIN_MS_ACE_TYPE = <NUM_LIT:0> <EOL> ACCESS_ALLOWED_ACE_TYPE = <NUM_LIT:0> <EOL> ACCESS_DENIED_ACE_TYPE = <NUM_LIT:1> <EOL> SYSTEM_AUDIT_ACE_TYPE = <NUM_LIT:2> <EOL> SYSTEM_ALARM_ACE_TYPE = <NUM_LIT:3> <EOL> ACCESS_MAX_MS_V2_ACE_TYPE = <NUM_LIT:3> <EOL> ACCESS_ALLOWED_COMPOUND_ACE_TYPE = <NUM_LIT:4> <EOL> ACCESS_MAX_MS_V3_ACE_TYPE = <NUM_LIT:4> <EOL> ACCESS_MIN_MS_OBJECT_ACE_TYPE = <NUM_LIT:5> <EOL> ACCESS_ALLOWED_OBJECT_ACE_TYPE = <NUM_LIT:5> <EOL> ACCESS_DENIED_OBJECT_ACE_TYPE = <NUM_LIT:6> <EOL> SYSTEM_AUDIT_OBJECT_ACE_TYPE = <NUM_LIT:7> <EOL> SYSTEM_ALARM_OBJECT_ACE_TYPE = <NUM_LIT:8> <EOL> ACCESS_MAX_MS_OBJECT_ACE_TYPE = <NUM_LIT:8> <EOL> ACCESS_MAX_MS_V4_ACE_TYPE = <NUM_LIT:8> <EOL> ACCESS_MAX_MS_ACE_TYPE = <NUM_LIT:8> <EOL> class ACE_FLAGS : <EOL> """<STR_LIT>""" <EOL> OBJECT_INHERIT_ACE = <NUM_LIT> <EOL> CONTAINER_INHERIT_ACE = <NUM_LIT> <EOL> NO_PROPAGATE_INHERIT_ACE = <NUM_LIT> <EOL> INHERIT_ONLY_ACE = <NUM_LIT> <EOL> INHERITED_ACE = <NUM_LIT> <EOL> VALID_INHERIT_FLAGS = <NUM_LIT> <EOL> SUCCESSFUL_ACCESS_ACE_FLAG = <NUM_LIT> <EOL> FAILED_ACCESS_ACE_FLAG = <NUM_LIT> <EOL> class ACCESS_MASK : <EOL> """<STR_LIT>""" <EOL> FILE_READ_DATA = <NUM_LIT> <EOL> FILE_LIST_DIRECTORY = <NUM_LIT> <EOL> FILE_WRITE_DATA = <NUM_LIT> <EOL> FILE_ADD_FILE = <NUM_LIT> <EOL> FILE_APPEND_DATA = <NUM_LIT> <EOL> FILE_ADD_SUBDIRECTORY = <NUM_LIT> <EOL> FILE_READ_EA = <NUM_LIT> <EOL> FILE_WRITE_EA = <NUM_LIT> <EOL> FILE_EXECUTE = <NUM_LIT> <EOL> FILE_TRAVERSE = <NUM_LIT> <EOL> FILE_DELETE_CHILD = <NUM_LIT> <EOL> FILE_READ_ATTRIBUTES = <NUM_LIT> <EOL> FILE_WRITE_ATTRIBUTES = <NUM_LIT> <EOL> DELETE = <NUM_LIT> <EOL> READ_CONTROL = <NUM_LIT> <EOL> WRITE_DAC = <NUM_LIT> <EOL> WRITE_OWNER = <NUM_LIT> <EOL> SYNCHRONIZE = <NUM_LIT> <EOL> STANDARD_RIGHTS_READ = <NUM_LIT> <EOL> STANDARD_RIGHTS_WRITE = <NUM_LIT> <EOL> STANDARD_RIGHTS_EXECUTE = <NUM_LIT> <EOL> STANDARD_RIGHTS_REQUIRED = <NUM_LIT> <EOL> STANDARD_RIGHTS_ALL = <NUM_LIT> <EOL> ACCESS_SYSTEM_SECURITY = <NUM_LIT> <EOL> MAXIMUM_ALLOWED = <NUM_LIT> <EOL> GENERIC_ALL = <NUM_LIT> <EOL> GENERIC_EXECUTE = <NUM_LIT> <EOL> GENERIC_WRITE = <NUM_LIT> <EOL> GENERIC_READ = <NUM_LIT> <EOL> class ACE ( Block ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACE , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def get_ace ( buf , offset , parent ) : <EOL> header = ACE ( buf , offset , parent ) <EOL> if header . ace_type ( ) == ACE_TYPES . ACCESS_ALLOWED_ACE_TYPE : <EOL> return ACCESS_ALLOWED_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . ACCESS_DENIED_ACE_TYPE : <EOL> return ACCESS_DENIED_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . SYSTEM_AUDIT_ACE_TYPE : <EOL> return SYSTEM_AUDIT_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . SYSTEM_ALARM_ACE_TYPE : <EOL> return SYSTEM_ALARM_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . ACCESS_ALLOWED_OBJECT_ACE_TYPE : <EOL> return ACCESS_ALLOWED_OBJECT_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . ACCESS_DENIED_OBJECT_ACE_TYPE : <EOL> return ACCESS_DENIED_OBJECT_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . SYSTEM_AUDIT_OBJECT_ACE_TYPE : <EOL> return SYSTEM_AUDIT_OBJECT_ACE ( buf , offset , parent ) <EOL> elif header . ace_type ( ) == ACE_TYPES . SYSTEM_ALARM_OBJECT_ACE_TYPE : <EOL> return SYSTEM_ALARM_OBJECT_ACE ( buf , offset , parent ) <EOL> else : <EOL> raise BinaryParser . ParseException ( "<STR_LIT>" ) <EOL> class StandardACE ( ACE , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( StandardACE , self ) . __init__ ( buf , offset , parent ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT:size>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( SID , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return BinaryParser . read_word ( buf , offset + <NUM_LIT> ) <EOL> def __len__ ( self ) : <EOL> return self . size ( ) <EOL> class ACCESS_ALLOWED_ACE ( StandardACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACCESS_ALLOWED_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class ACCESS_DENIED_ACE ( StandardACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACCESS_DENIED_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class SYSTEM_AUDIT_ACE ( StandardACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SYSTEM_AUDIT_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class SYSTEM_ALARM_ACE ( StandardACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SYSTEM_ALARM_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class OBJECT_ACE_FLAGS : <EOL> """<STR_LIT>""" <EOL> ACE_OBJECT_TYPE_PRESENT = <NUM_LIT:1> <EOL> ACE_INHERITED_OBJECT_TYPE_PRESENT = <NUM_LIT:2> <EOL> class ObjectACE ( ACE , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ObjectACE , self ) . __init__ ( buf , offset , parent ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT:size>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return BinaryParser . read_word ( buf , offset + <NUM_LIT> ) <EOL> def __len__ ( self ) : <EOL> return self . size ( ) <EOL> class ACCESS_ALLOWED_OBJECT_ACE ( ObjectACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACCESS_ALLOWED_OBJECT_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class ACCESS_DENIED_OBJECT_ACE ( ObjectACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACCESS_DENIED_OBJECT_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class SYSTEM_AUDIT_OBJECT_ACE ( ObjectACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SYSTEM_AUDIT_OBJECT_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class SYSTEM_ALARM_OBJECT_ACE ( ObjectACE ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SYSTEM_ALARM_OBJECT_ACE , self ) . __init__ ( buf , offset , parent ) <EOL> class ACL ( Block , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( ACL , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT:size>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _off_ACEs = self . current_field_offset ( ) <EOL> self . add_explicit_field ( self . _off_ACEs , ACE , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return BinaryParser . read_word ( buf , offset + <NUM_LIT> ) <EOL> def __len__ ( self ) : <EOL> return self . size ( ) <EOL> def ACEs ( self ) : <EOL> ofs = self . _off_ACEs <EOL> for _ in range ( self . ace_count ( ) ) : <EOL> a = ACE . get_ace ( self . _buf , self . offset ( ) + ofs , self ) <EOL> yield a <EOL> ofs += a . size ( ) <EOL> ofs = BinaryParser . align ( ofs , <NUM_LIT:4> ) <EOL> class NULL_ACL ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( NULL_ACL , self ) . __init__ ( ) <EOL> def revision ( self ) : <EOL> return <NUM_LIT:1> <EOL> def alignment1 ( self ) : <EOL> return <NUM_LIT:0> <EOL> def size ( self ) : <EOL> return <NUM_LIT:0> <EOL> def ace_count ( self ) : <EOL> return <NUM_LIT:0> <EOL> def ACEs ( self ) : <EOL> return <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return <NUM_LIT:0> <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:0> <EOL> class SECURITY_DESCRIPTOR_RELATIVE ( Block , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SECURITY_DESCRIPTOR_RELATIVE , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . add_explicit_field ( self . owner_offset ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . add_explicit_field ( self . group_offset ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . control ( ) & SECURITY_DESCRIPTOR_CONTROL . SE_SACL_PRESENT : <EOL> self . add_explicit_field ( self . sacl_offset ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . control ( ) & SECURITY_DESCRIPTOR_CONTROL . SE_DACL_PRESENT : <EOL> self . add_explicit_field ( self . dacl_offset ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return len ( SECURITY_DESCRIPTOR_RELATIVE ( buf , offset , parent ) ) <EOL> def __len__ ( self ) : <EOL> ret = <NUM_LIT:20> <EOL> ret += len ( ( self . owner ( ) or null_object ) ) <EOL> ret += len ( ( self . group ( ) or null_object ) ) <EOL> ret += len ( ( self . sacl ( ) or null_object ) ) <EOL> ret += len ( ( self . dacl ( ) or null_object ) ) <EOL> return ret <EOL> def owner ( self ) : <EOL> if self . owner_offset ( ) != <NUM_LIT:0> : <EOL> return SID ( self . _buf , self . absolute_offset ( self . owner_offset ( ) ) , self ) <EOL> else : <EOL> return None <EOL> def group ( self ) : <EOL> if self . group_offset ( ) != <NUM_LIT:0> : <EOL> return SID ( self . _buf , self . absolute_offset ( self . group_offset ( ) ) , self ) <EOL> else : <EOL> return None <EOL> def sacl ( self ) : <EOL> if self . control ( ) & SECURITY_DESCRIPTOR_CONTROL . SE_SACL_PRESENT : <EOL> if self . sacl_offset ( ) > <NUM_LIT:0> : <EOL> return ACL ( self . _buf , self . absolute_offset ( self . sacl_offset ( ) ) , self ) <EOL> else : <EOL> return NULL_ACL ( ) <EOL> else : <EOL> return None <EOL> def dacl ( self ) : <EOL> if self . control ( ) & SECURITY_DESCRIPTOR_CONTROL . SE_DACL_PRESENT : <EOL> if self . dacl_offset ( ) > <NUM_LIT:0> : <EOL> return ACL ( self . _buf , self . absolute_offset ( self . dacl_offset ( ) ) , self ) <EOL> else : <EOL> return NULL_ACL ( ) <EOL> else : <EOL> return None <EOL> class SDS_ENTRY ( Block , Nestable ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SDS_ENTRY , self ) . __init__ ( buf , offset ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . declare_field ( SECURITY_DESCRIPTOR_RELATIVE , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def structure_size ( buf , offset , parent ) : <EOL> return BinaryParser . read_dword ( buf , offset + <NUM_LIT> ) <EOL> def __len__ ( self ) : <EOL> return self . length ( ) <EOL> class SDS ( Block ) : <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> super ( SDS , self ) . __init__ ( buf , offset ) <EOL> self . add_explicit_field ( <NUM_LIT:0> , SDS , "<STR_LIT>" ) <EOL> def sds_entries ( self ) : <EOL> ofs = <NUM_LIT:0> <EOL> while len ( self . _buf ) > self . offset ( ) + ofs + <NUM_LIT> : <EOL> s = SDS_ENTRY ( self . _buf , self . offset ( ) + ofs , self ) <EOL> if len ( s ) != <NUM_LIT:0> : <EOL> yield s <EOL> ofs += len ( s ) <EOL> ofs = BinaryParser . align ( ofs , <NUM_LIT> ) <EOL> else : <EOL> if ofs % <NUM_LIT> == <NUM_LIT:0> : <EOL> return <EOL> else : <EOL> ofs = BinaryParser . align ( ofs , <NUM_LIT> ) <EOL> def main ( ) : <EOL> import sys <EOL> import mmap <EOL> import contextlib <EOL> with open ( sys . argv [ <NUM_LIT:1> ] , '<STR_LIT:r>' ) as f : <EOL> with contextlib . closing ( mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> , <EOL> access = mmap . ACCESS_READ ) ) as buf : <EOL> s = SDS ( buf , <NUM_LIT:0> , None ) <EOL> print "<STR_LIT>" <EOL> for e in s . sds_entries ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( e . get_all_string ( indent = <NUM_LIT:2> ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
