Skip to content

Commit 785dd2a

Browse files
committed
Update README.md - Fix Base46 input example
1 parent e52d9db commit 785dd2a

File tree

5 files changed

+98
-92
lines changed

5 files changed

+98
-92
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
Python library to convert Windows [SIDs](https://en.wikipedia.org/wiki/Security_Identifier)
44

5+
## Install
6+
7+
E.g:
8+
`python3 setup.py install`
9+
510
## Example
611

712
String input
@@ -22,7 +27,7 @@ Base46 input
2227
import sid
2328

2429
mysid = sid.sid('AQUAAAAAAAUVAAAAoGXPfnhLm1/nfIdwCRwBAA==', sid.SID_BASE64)
25-
print mysid
30+
print(mysid)
2631
```
2732

2833
Output

setup.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
"""
22
The MIT License (MIT)
33
44
Copyright (c) 2015 Sascha Spreitzer, Red Hat
@@ -20,23 +20,23 @@
2020
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
23-
'''
23+
"""
2424

2525

2626
from setuptools import setup
2727

2828

2929
setup(
30-
name = "sid",
31-
version = "0.2",
32-
author = "Sascha Spreitzer",
33-
author_email = "[email protected]",
34-
description = ("Python library to convert Windows SIDs"),
35-
license = "MIT",
36-
keywords = "windows sid",
37-
url = "https://github.com/sspreitzer/python-sid",
38-
package_dir = {'': 'src'},
39-
packages = ['sid'],
30+
name="sid",
31+
version="0.2.1",
32+
author="Sascha Spreitzer",
33+
author_email="[email protected]",
34+
description=("Python library to convert Windows SIDs"),
35+
license="MIT",
36+
keywords="windows sid",
37+
url="https://github.com/sspreitzer/python-sid",
38+
package_dir={"": "src"},
39+
packages=["sid"],
4040
classifiers=[
4141
"Development Status :: 4 - Beta",
4242
"License :: OSI Approved :: MIT License",

src/sid/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'''
1+
"""
22
A Python module to convert Windows SIDs
33
44
55
import sid
66
77
s = sid.sid('S-1-5-21-2127521184-1604012920-1887927527-72713')
88
print s.base64()
9-
'''
9+
"""
1010

1111

1212
from .lib import *

src/sid/lib.py

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''The lib contains exceptions, constants and the sid class'''
1+
"""The lib contains exceptions, constants and the sid class"""
22

33
import base64
44
import struct
@@ -11,23 +11,25 @@
1111

1212

1313
class sidException(Exception):
14-
'''Base exception derived from Exception class'''
14+
"""Base exception derived from Exception class"""
15+
1516
pass
1617

1718

1819
class 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

2325
class 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))

src/sid/test_lib.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
class Testsid(unittest.TestCase):
77
"""Class for testing sid"""
8-
sid_null = 'S-1-0-0'
9-
sid_sample = 'S-1-5-21-2127521184-1604012920-1887927527-72713'
10-
sid_null_bin = bytearray(
11-
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
8+
9+
sid_null = "S-1-0-0"
10+
sid_sample = "S-1-5-21-2127521184-1604012920-1887927527-72713"
11+
sid_null_bin = bytearray(b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1212
sid_sample_bin = bytearray(
13-
b'\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xa0e\xcf~xK\x9b_\xe7|\x87p\t\x1c\x01\x00')
14-
sid_null_b64 = b'AQEAAAAAAAAAAAAA'
15-
sid_sample_b64 = b'AQUAAAAAAAUVAAAAoGXPfnhLm1/nfIdwCRwBAA=='
13+
b"\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xa0e\xcf~xK\x9b_\xe7|\x87p\t\x1c\x01\x00"
14+
)
15+
sid_null_b64 = b"AQEAAAAAAAAAAAAA"
16+
sid_sample_b64 = b"AQUAAAAAAAUVAAAAoGXPfnhLm1/nfIdwCRwBAA=="
1617

1718
def test_init_string(self):
1819
"""Test creating sids from strings"""
@@ -21,37 +22,35 @@ def test_init_string(self):
2122

2223
def test_init_base64(self):
2324
"""Test creating sids from base64"""
24-
self.assertEqual(self.sid_null, str(
25-
sid.sid(self.sid_null_b64, sid.SID_BASE64)))
26-
self.assertEqual(self.sid_sample, str(
27-
sid.sid(self.sid_sample_b64, sid.SID_BASE64)))
25+
self.assertEqual(self.sid_null, str(sid.sid(self.sid_null_b64, sid.SID_BASE64)))
26+
self.assertEqual(
27+
self.sid_sample, str(sid.sid(self.sid_sample_b64, sid.SID_BASE64))
28+
)
2829

2930
def test_init_binary(self):
3031
"""Test creating sids from binary"""
31-
self.assertEqual(self.sid_null, str(
32-
sid.sid(self.sid_null_bin, sid.SID_BINARY)))
33-
self.assertEqual(self.sid_sample, str(
34-
sid.sid(self.sid_sample_bin, sid.SID_BINARY)))
32+
self.assertEqual(self.sid_null, str(sid.sid(self.sid_null_bin, sid.SID_BINARY)))
33+
self.assertEqual(
34+
self.sid_sample, str(sid.sid(self.sid_sample_bin, sid.SID_BINARY))
35+
)
3536

3637
def test_ldap(self):
3738
"""Test ldap filter form of sid"""
38-
sid_null_ldap = '\\01\\01\\00\\00\\00\\00\\00\\00\\00\\00\\00\\00'
39-
sid_sample_ldap = '\\01\\05\\00\\00\\00\\00\\00\\05\\15\\00\\00\\00\\a0\\65\\cf\\7e\\78\\4b\\9b\\5f\\e7\\7c\\87\\70\\09\\1c\\01\\00'
39+
sid_null_ldap = "\\01\\01\\00\\00\\00\\00\\00\\00\\00\\00\\00\\00"
40+
sid_sample_ldap = "\\01\\05\\00\\00\\00\\00\\00\\05\\15\\00\\00\\00\\a0\\65\\cf\\7e\\78\\4b\\9b\\5f\\e7\\7c\\87\\70\\09\\1c\\01\\00"
4041
self.assertEqual(sid_null_ldap, sid.sid(self.sid_null).ldap())
4142
self.assertEqual(sid_sample_ldap, sid.sid(self.sid_sample).ldap())
4243

4344
def test_binary(self):
4445
"""Test binary form of sid"""
4546
self.assertEqual(self.sid_null_bin, sid.sid(self.sid_null).binary())
46-
self.assertEqual(self.sid_sample_bin,
47-
sid.sid(self.sid_sample).binary())
47+
self.assertEqual(self.sid_sample_bin, sid.sid(self.sid_sample).binary())
4848

4949
def test_base64(self):
5050
"""Test base64 form of sid"""
5151
self.assertEqual(self.sid_null_b64, sid.sid(self.sid_null).base64())
52-
self.assertEqual(self.sid_sample_b64,
53-
sid.sid(self.sid_sample).base64())
52+
self.assertEqual(self.sid_sample_b64, sid.sid(self.sid_sample).base64())
5453

5554

56-
if __name__ == '__main__':
55+
if __name__ == "__main__":
5756
unittest.main()

0 commit comments

Comments
 (0)