Skip to content

Commit 4f62434

Browse files
committed
Moved lookup_sid to windows.utils + add Token to window.security + update doc about official Token path
1 parent fb567c6 commit 4f62434

8 files changed

Lines changed: 43 additions & 18 deletions

File tree

docs/source/security.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ This module give access to :class:`SecurityDescriptor` and related structures (`
1818

1919
See sample :ref:`sample_security`
2020

21+
Token
22+
"""""
23+
24+
The :mod:`windows.security` module is the official module where to retrieve the :class:`~windows.winobject.token.Token` class if ever needed.
25+
26+
Indeed ``SecurityDescriptor`` & ``Token`` are deeply related and I may move ``token.py`` to a ``security/`` directory in the futur.
27+
28+
29+
>>> windows.security.Token
30+
<class 'windows.winobject.token.Token'>
31+
2132

2233
SecurityDescriptor
2334
""""""""""""""""""

docs/source/token.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ This module expose the :class:`Token` object that can be primarily retrieved th
99
* :data:`windows.winobject.process.WinThread.token`
1010
* :data:`windows.current_process.token <windows.winobject.process.CurrentProcess.token>`
1111
* :data:`windows.current_thread.token <windows.winobject.process.CurrentThread.token>`
12+
* :class:`windows.security.Token`
13+
14+
.. note::
15+
16+
If you need to directly access the :class:`Token` class, please use :class:`windows.security.Token` as the
17+
path of ``token.py`` may change.
18+
19+
Indeed ``SecurityDescriptor`` & ``Token`` are deeply related and I may move ``token.py`` to a
20+
``security/`` directory in the futur.
1221

1322
.. note::
1423

samples/security/security_descriptor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
print("Security descriptor is: {0}".format(sd))
77

88
print("Owner: {0}".format(sd.owner))
9-
print(" - lookup: {0}".format(windows.security.lookup_sid(sd.owner)))
9+
print(" - lookup: {0}".format(windows.utils.lookup_sid(sd.owner)))
1010
print("Group: {0}".format(sd.group))
11-
print(" - lookup: {0}".format(windows.security.lookup_sid(sd.group)))
11+
print(" - lookup: {0}".format(windows.utils.lookup_sid(sd.group)))
1212

1313
dacl = sd.dacl
1414
print("Dacl: {0}".format(dacl))

samples/token/token_demo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import windows
2-
import windows.security
32
import windows.generated_def as gdef
43

54
tok = windows.current_process.token
65
print("Our process token is {0}".format(tok))
76
print("Retrieving some infos")
87
print("Username: <{0}>".format(tok.username))
98
print("User: {0!r}".format(tok.user))
10-
print(" - lookup : {0}".format(windows.security.lookup_sid(tok.user)))
9+
print(" - lookup : {0}".format(windows.utils.lookup_sid(tok.user)))
1110
print("Primary group: {0!r}".format(tok.primary_group))
12-
print(" - lookup : {0}".format(windows.security.lookup_sid(tok.primary_group)))
11+
print(" - lookup : {0}".format(windows.utils.lookup_sid(tok.primary_group)))
1312

1413
print("")
1514
groups = tok.groups

tests/test_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_lower_integrity(newtok):
2929
def test_token_user(curtok):
3030
user_sid = curtok.user
3131
assert user_sid
32-
computername, username = windows.security.lookup_sid(user_sid)
32+
computername, username = windows.utils.lookup_sid(user_sid)
3333
assert computername == windows.system.computer_name
3434
assert username == os.environ["USERNAME"]
3535

windows/security.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@
55
import windows.generated_def as gdef
66
from windows import winproxy
77

8-
# Temporary ? real API ?
9-
# Mov to utils ?
10-
def lookup_sid(psid):
11-
usernamesize = gdef.DWORD(0x1000)
12-
computernamesize = gdef.DWORD(0x1000)
13-
username = ctypes.create_unicode_buffer(usernamesize.value)
14-
computername = ctypes.create_unicode_buffer(computernamesize.value)
15-
peUse = gdef.SID_NAME_USE()
16-
winproxy.LookupAccountSidW(None, psid, username, usernamesize, computername, computernamesize, peUse)
17-
return computername[:computernamesize.value], username[:usernamesize.value]
8+
from windows.winobject.token import Token
9+
1810

1911
# Specific access right
2012

windows/utils/winutils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ def lookup_privilege_name(privilege_value):
110110
winproxy.LookupPrivilegeNameA(None, privilege_value, buff, size)
111111
return buff[:size.value]
112112

113+
114+
def lookup_sid(psid):
115+
"""Retrieves the name of the Computer/Domain and the name of the Account for a given SID
116+
117+
:returns: (:class:`unicode`, :class:`unicode`) - A tuple of two unicode strings
118+
"""
119+
usernamesize = gdef.DWORD(0x1000)
120+
computernamesize = gdef.DWORD(0x1000)
121+
username = ctypes.create_unicode_buffer(usernamesize.value)
122+
computername = ctypes.create_unicode_buffer(computernamesize.value)
123+
peUse = gdef.SID_NAME_USE()
124+
winproxy.LookupAccountSidW(None, psid, username, usernamesize, computername, computernamesize, peUse)
125+
return computername[:computernamesize.value], username[:usernamesize.value]
126+
113127
def enable_privilege(lpszPrivilege, bEnablePrivilege):
114128
"""
115129
Enable or disable a privilege::

windows/winobject/token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from windows import utils
77
from windows import winproxy
88
import windows.generated_def as gdef
9-
import windows.security
109

1110

1211
KNOW_INTEGRITY_LEVEL = gdef.FlagMapper(
@@ -282,7 +281,7 @@ def computername(self):
282281
return self._user_and_computer_name()[0]
283282

284283
def _user_and_computer_name(self):
285-
return windows.security.lookup_sid(self.user)
284+
return windows.utils.lookup_sid(self.user)
286285

287286

288287
groups = TokenGroups #: Alias for TokenGroups (type may change in the future for improved struct)
@@ -309,6 +308,7 @@ def default_dacl(self):
309308
310309
:type: :class:`windows.security.Acl`
311310
"""
311+
import window.security # Beuk move token.py & in a security/ directory ?
312312
return self.get_token_infomations(gdef.TokenDefaultDacl, windows.security.PAcl)[0]
313313

314314
# def source(self): (tok.TokenSource) ??

0 commit comments

Comments
 (0)