11from impacket .ldap import ldapasn1 as ldapasn1_impacket
2+ from uuid import UUID
23
34
45def parse_result_attributes (ldap_response ):
@@ -11,16 +12,43 @@ def parse_result_attributes(ldap_response):
1112 for attribute in entry ["attributes" ]:
1213 val_list = []
1314 for val in attribute ["vals" ].components :
14- try :
15- encoding = val .encoding
16- val_decoded = str (val ).encode (encoding ).decode ("utf-8" )
17- except UnicodeDecodeError :
18- # If we can't decode the value, we'll just return the bytes
19- val_decoded = val .__bytes__ ()
15+ # Typical Byte objects we know how to decode
16+ if str (attribute ["type" ]) == "objectGUID" :
17+ val_decoded = UUID (bytes = val .__bytes__ ())
18+ elif str (attribute ["type" ]) == "objectSid" :
19+ val_decoded = sid_to_str (val .__bytes__ ())
20+ else :
21+ # For the rest we try to decode the value with its encoding
22+ try :
23+ encoding = val .encoding
24+ val_decoded = str (val ).encode (encoding ).decode ("utf-8" )
25+ except UnicodeDecodeError :
26+ # If we can't decode the value, we'll just return the bytes
27+ val_decoded = val .__bytes__ ()
2028 val_list .append (val_decoded )
2129 if len (val_list ) == 1 :
2230 attribute_map [str (attribute ["type" ])] = val_list [0 ]
2331 else :
2432 attribute_map [str (attribute ["type" ])] = val_list
2533 parsed_response .append (attribute_map )
2634 return parsed_response
35+
36+
37+ def sid_to_str (sid ):
38+ try :
39+ # revision
40+ revision = int (sid [0 ])
41+ # count of sub authorities
42+ sub_authorities = int (sid [1 ])
43+ # big endian
44+ identifier_authority = int .from_bytes (sid [2 :8 ], byteorder = "big" )
45+ # If true then it is represented in hex
46+ if identifier_authority >= 2 ** 32 :
47+ identifier_authority = hex (identifier_authority )
48+
49+ # loop over the count of small endians
50+ sub_authority = "-" + "-" .join ([str (int .from_bytes (sid [8 + (i * 4 ): 12 + (i * 4 )], byteorder = "little" )) for i in range (sub_authorities )])
51+ return "S-" + str (revision ) + "-" + str (identifier_authority ) + sub_authority
52+ except Exception :
53+ pass
54+ return sid
0 commit comments