1- ''' The lib contains exceptions, constants and the sid class'''
1+ """ The lib contains exceptions, constants and the sid class"""
22
33import base64
44import struct
1111
1212
1313class sidException (Exception ):
14- '''Base exception derived from Exception class'''
14+ """Base exception derived from Exception class"""
15+
1516 pass
1617
1718
1819class sidExceptionNoSuchType (sidException ):
19- '''No such type exception. Used when class is not initialized properly.'''
20+ """No such type exception. Used when class is not initialized properly."""
21+
2022 pass
2123
2224
2325class sid (object ):
24- ''' Class to manage Windows SIDs'''
26+ """ Class to manage Windows SIDs"""
2527
2628 def __init__ (self , data , sidtype = SID_STRING ):
27- '''
29+ """
2830 Initialize class with either a string, binary or base64 sid.
2931 For example, Anonymous user is string 'S-1-5-7'
30- '''
32+ """
3133 if sidtype == SID_STRING :
3234 self ._sid = data
3335 return
@@ -39,120 +41,120 @@ def __init__(self, data, sidtype=SID_STRING):
3941 return
4042 else :
4143 raise sidExceptionNoSuchType ()
42-
44+
4345 def ldap (self ):
44- ''' Return ldap filter version of sid'''
46+ """ Return ldap filter version of sid"""
4547 return self .byteldap (self ._sid )
46-
48+
4749 def binary (self ):
48- ''' Return binary version of sid'''
50+ """ Return binary version of sid"""
4951 return self .byte (self ._sid )
5052
5153 def base64 (self ):
52- ''' Return base64 encoded version of binary sid'''
54+ """ Return base64 encoded version of binary sid"""
5355 return self .byteB64 (self ._sid )
54-
56+
5557 def str (self ):
56- ''' Return sid as a string'''
58+ """ Return sid as a string"""
5759 return str (self )
58-
60+
5961 def __str__ (self ):
60- ''' sid class can be used as a string'''
62+ """ sid class can be used as a string"""
6163 return self ._sid
62-
64+
6365 def __repr__ (self ):
64- ''' Return representation of sid'''
65- return repr ( self ._sid )
66+ """ Return representation of sid"""
67+ return repr (self ._sid )
6668
6769 @classmethod
6870 def longToByte (cls , integer , little_endian = True , size = 4 ):
69- '''
71+ """
7072 Convert a Python integer into bytes
7173 integer - integer to convert
7274 little_endian - True (default) or False for little or big endian
7375 size - size to be returned, default is 4 (thats 32bit)
74- '''
76+ """
7577 if little_endian :
76- return struct .pack ('<q' , integer )[0 :size ]
78+ return struct .pack ("<q" , integer )[0 :size ]
7779 else :
78- return struct .pack ('>q' , integer )[8 - size :]
79-
80+ return struct .pack (">q" , integer )[8 - size :]
81+
8082 @classmethod
8183 def byteToLong (cls , byte , little_endian = True ):
82- '''
84+ """
8385 Convert bytes into a Python integer
8486 byte - bytes to convert
8587 little_endian - True (default) or False for little or big endian
86- '''
88+ """
8789 if len (byte ) > 8 :
88- raise Exception (' Bytes too long. Needs to be <= 8 or 64bit' )
90+ raise Exception (" Bytes too long. Needs to be <= 8 or 64bit" )
8991 else :
9092 if little_endian :
91- a = byte .ljust (8 , b' \x00 ' )
92- return struct .unpack ('<q' , a )[0 ]
93+ a = byte .ljust (8 , b" \x00 " )
94+ return struct .unpack ("<q" , a )[0 ]
9395 else :
94- a = byte .rjust (8 , b' \x00 ' )
95- return struct .unpack ('>q' , a )[0 ]
96-
96+ a = byte .rjust (8 , b" \x00 " )
97+ return struct .unpack (">q" , a )[0 ]
98+
9799 @classmethod
98100 def strsid (cls , byte ):
99- '''
101+ """
100102 Convert bytes into a string SID
101103 byte - bytes to convert
102- '''
103- ret = 'S'
104+ """
105+ ret = "S"
104106 sid = []
105107 sid .append (cls .byteToLong (byte [0 :1 ]))
106- sid .append (cls .byteToLong (byte [2 : 2 + 6 ], False ))
108+ sid .append (cls .byteToLong (byte [2 : 2 + 6 ], False ))
107109 for i in range (8 , len (byte ), 4 ):
108- sid .append (cls .byteToLong (byte [i : i + 4 ]))
110+ sid .append (cls .byteToLong (byte [i : i + 4 ]))
109111 for i in sid :
110- ret += '-' + str (i )
112+ ret += "-" + str (i )
111113 return ret
112-
114+
113115 @classmethod
114116 def byte (cls , strsid ):
115- '''
117+ """
116118 Convert a SID into bytes
117119 strdsid - SID to convert into bytes
118- '''
119- sid = str .split (strsid , '-' )
120+ """
121+ sid = str .split (strsid , "-" )
120122 ret = bytearray ()
121- sid .remove ('S' )
123+ sid .remove ("S" )
122124 for i in range (len (sid )):
123125 sid [i ] = int (sid [i ])
124- sid .insert (1 , len (sid )- 2 )
126+ sid .insert (1 , len (sid ) - 2 )
125127 ret += cls .longToByte (sid [0 ], size = 1 )
126128 ret += cls .longToByte (sid [1 ], size = 1 )
127129 ret += cls .longToByte (sid [2 ], False , 6 )
128130 for i in range (3 , len (sid )):
129131 ret += cls .longToByte (sid [i ])
130132 return ret
131-
133+
132134 @classmethod
133135 def byteldap (cls , strsid ):
134- '''
136+ """
135137 Encode a sid into AD ldap search form
136138 strsid - SID to encode
137- '''
138- ret = ''
139- a = binascii .hexlify (cls .byte (strsid )).decode (' utf-8' )
139+ """
140+ ret = ""
141+ a = binascii .hexlify (cls .byte (strsid )).decode (" utf-8" )
140142 for i in range (0 , len (a ), 2 ):
141- ret += ' \\ ' + a [i : i + 2 ]
143+ ret += " \\ " + a [i : i + 2 ]
142144 return ret
143-
145+
144146 @classmethod
145147 def byteB64 (cls , strsid ):
146- '''
148+ """
147149 Encode a sid into base64
148150 strsid - SID to encode
149- '''
151+ """
150152 return base64 .b64encode (cls .byte (strsid ))
151-
153+
152154 @classmethod
153155 def b64Strsid (cls , data ):
154- '''
156+ """
155157 Decode a base64 SID into string
156158 data - base64 encoded sid
157- '''
159+ """
158160 return cls .strsid (base64 .b64decode (data ))
0 commit comments