1+ import pytest
2+ import os
3+
14import windows
5+ import windows .security
26import windows .generated_def as gdef
37
8+ @pytest .fixture
9+ def curtok ():
10+ return windows .current_process .token
11+
12+ @pytest .fixture
13+ def newtok ():
14+ return windows .current_process .token .duplicate ()
15+
16+ def test_token_info (curtok ):
17+ assert isinstance (curtok .computername , basestring )
18+ assert isinstance (curtok .username , basestring )
19+ assert isinstance (curtok .integrity , (int , long ))
20+ assert isinstance (curtok .is_elevated , (bool ))
21+
22+ def test_lower_integrity (newtok ):
23+ assert newtok .integrity != 123
24+ # Change token integrity
25+ newtok .integrity = 123
26+ # newtok.integrity retrieve the integrity at each call so this in enough
27+ assert newtok .integrity == 123
28+
29+ def test_token_user (curtok ):
30+ user_sid = curtok .user
31+ assert user_sid
32+ computername , username = windows .security .lookup_sid (user_sid )
33+ assert computername == windows .system .computer_name
34+ assert username == os .environ ["USERNAME" ]
35+
36+ def test_token_id (curtok ):
37+ ntok = curtok .duplicate ()
38+ assert ntok .id != curtok .id
39+ mid = ntok .modified_id
40+ aid = ntok .authentication_id
41+ ntok .enable_privilege ("SeShutDownPrivilege" )
42+ mid2 = ntok .modified_id
43+ aid2 = ntok .authentication_id
44+ ntok .integrity -= 1
45+ assert ntok .modified_id != mid2 != mid
46+ assert ntok .authentication_id == aid2 == aid
47+
48+
49+ def test_enable_privilege (newtok ):
50+ PRIVILEGE_NAME = "SeShutdownPrivilege"
51+ assert not newtok .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
52+ newtok .enable_privilege (PRIVILEGE_NAME )
53+ assert newtok .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
54+
55+
56+ def test_adjust_privilege (newtok ):
57+ PRIVILEGE_NAME = "SeShutdownPrivilege"
58+ PRIVILEGE2_NAME = "SeTimeZonePrivilege"
59+ tok_dup = newtok .duplicate ()
60+ assert not tok_dup .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
61+ assert not tok_dup .privileges [PRIVILEGE2_NAME ] & gdef .SE_PRIVILEGE_ENABLED
62+ # Enable privilege in another token.
63+ privs = newtok .privileges
64+ assert not privs [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
65+ assert not privs [PRIVILEGE2_NAME ] & gdef .SE_PRIVILEGE_ENABLED
66+
67+ privs [PRIVILEGE_NAME ] = gdef .SE_PRIVILEGE_ENABLED
68+ privs [PRIVILEGE2_NAME ] = gdef .SE_PRIVILEGE_ENABLED
69+ tok_dup .adjust_privileges (privs )
70+
71+ assert tok_dup .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
72+ assert tok_dup .privileges [PRIVILEGE2_NAME ] & gdef .SE_PRIVILEGE_ENABLED
73+
74+ assert not newtok .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
75+ newtok .enable_privilege (PRIVILEGE_NAME )
76+ assert newtok .privileges [PRIVILEGE_NAME ] & gdef .SE_PRIVILEGE_ENABLED
77+
78+
79+ def test_token_groups (curtok ):
80+ groups = curtok .groups
81+ groups_size = groups .GroupCount
82+ assert groups_size > 0
83+ assert len (groups .sids ) == groups_size
84+ assert len (groups .sids_and_attributes ) == groups_size
85+
86+ def test_token_duplicate (newtok ):
87+ x = newtok .duplicate ()
88+ assert x .type == newtok .type
89+
90+ primtok = newtok .duplicate (type = gdef .TokenPrimary )
91+ assert primtok .type == gdef .TokenPrimary
92+ with pytest .raises (WindowsError ):
93+ assert x .impersonation_level
94+
95+ with pytest .raises (ValueError ):
96+ # duplicate TokenPrimary -> TokenImpersonation require explicit impersonation_level
97+ primtok .duplicate (type = gdef .TokenImpersonation )
498
99+ for i in range (gdef .SecurityAnonymous , gdef .SecurityDelegation + 1 ):
100+ x = newtok .duplicate (type = gdef .TokenImpersonation , impersonation_level = i )
101+ assert x .type == gdef .TokenImpersonation
102+ assert x .impersonation_level == i
5103
6- def test_token_info ():
7- token = windows .current_process .token
8- assert isinstance (token .computername , basestring )
9- assert isinstance (token .username , basestring )
10- assert isinstance (token .integrity , (int , long ))
11- assert isinstance (token .is_elevated , (bool ))
12104
13- def test_lower_integrity (proc32 ):
14- # Lowering the integrity in remote process
15- # Because we don't want to mess with the token of our testing process
16105
17- proc32 .execute_python ("import windows" )
18- # We stock the handle becase lowering the integrity
19- # will mess with token retrieval
20- proc32 .execute_python ("token = windows.current_process.token" )
21- proc32 .execute_python ("token.integrity = 123" )
22- # execute_python will raise this in our own process :)
23- proc32 .execute_python ("assert token.integrity == 123" )
24106
25107
26- def test_token_elevation ():
27- tok = windows .current_process .token
28- assert tok .TokenElevation
108+ # def test_token_groups
0 commit comments