win32-security 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,121 @@
1
+ module Windows
2
+ module Security
3
+ module Constants
4
+ TOKEN_QUERY = 8
5
+ ERROR_NO_TOKEN = 1008
6
+
7
+ # ACL Revisions
8
+
9
+ ACL_REVISION1 = 1
10
+ ACL_REVISION = 2
11
+ ACL_REVISION2 = 2
12
+ ACL_REVISION3 = 3
13
+ ACL_REVISION4 = 4
14
+
15
+ # ACL Information Classes
16
+
17
+ AclRevisionInformation = 1
18
+ AclSizeInformation = 2
19
+
20
+ # Identifier Authorities
21
+
22
+ SECURITY_NULL_SID_AUTHORITY = 0
23
+ SECURITY_WORLD_SID_AUTHORITY = 1
24
+ SECURITY_LOCAL_SID_AUTHORITY = 2
25
+ SECURITY_CREATOR_SID_AUTHORITY = 3
26
+ SECURITY_NON_UNIQUE_AUTHORITY = 4
27
+ SECURITY_NT_AUTHORITY = 5
28
+ SECURITY_RESOURCE_MANAGER_AUTHORITY = 9
29
+
30
+ # Subauthorities
31
+
32
+ SECURITY_NULL_RID = 0x00000000
33
+ SECURITY_WORLD_RID = 0x00000000
34
+ SECURITY_LOCAL_RID = 0x00000000
35
+ SECURITY_CREATOR_OWNER_RID = 0x00000000
36
+ SECURITY_CREATOR_GROUP_RID = 0x00000001
37
+ SECURITY_CREATOR_OWNER_SERVER_RID = 0x00000002
38
+ SECURITY_CREATOR_GROUP_SERVER_RID = 0x00000003
39
+ SECURITY_DIALUP_RID = 0x00000001
40
+ SECURITY_NETWORK_RID = 0x00000002
41
+ SECURITY_BATCH_RID = 0x00000003
42
+ SECURITY_INTERACTIVE_RID = 0x00000004
43
+ SECURITY_LOGON_IDS_RID = 0x00000005
44
+ SECURITY_LOGON_IDS_RID_COUNT = 3
45
+ SECURITY_SERVICE_RID = 0x00000006
46
+ SECURITY_ANONYMOUS_LOGON_RID = 0x00000007
47
+ SECURITY_PROXY_RID = 0x00000008
48
+
49
+ SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x00000009
50
+ SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
51
+ SECURITY_PRINCIPAL_SELF_RID = 0x0000000A
52
+ SECURITY_AUTHENTICATED_USER_RID = 0x0000000B
53
+ SECURITY_RESTRICTED_CODE_RID = 0x0000000C
54
+ SECURITY_TERMINAL_SERVER_RID = 0x0000000D
55
+ SECURITY_REMOTE_LOGON_RID = 0x0000000E
56
+ SECURITY_THIS_ORGANIZATION_RID = 0x0000000F
57
+ SECURITY_LOCAL_SYSTEM_RID = 0x00000012
58
+ SECURITY_LOCAL_SERVICE_RID = 0x00000013
59
+ SECURITY_NETWORK_SERVICE_RID = 0x00000014
60
+ SECURITY_NT_NON_UNIQUE = 0x00000015
61
+ SECURITY_NT_NON_UNIQUE_SUB_AUTH_COUNT = 3
62
+
63
+ SECURITY_BUILTIN_DOMAIN_RID = 0x00000020
64
+ SECURITY_PACKAGE_BASE_RID = 0x00000040
65
+ SECURITY_PACKAGE_RID_COUNT = 2
66
+ SECURITY_PACKAGE_NTLM_RID = 0x0000000A
67
+ SECURITY_PACKAGE_SCHANNEL_RID = 0x0000000E
68
+ SECURITY_PACKAGE_DIGEST_RID = 0x00000015
69
+ SECURITY_MAX_ALWAYS_FILTERED = 0x000003E7
70
+ SECURITY_MIN_NEVER_FILTERED = 0x000003E8
71
+
72
+ SECURITY_OTHER_ORGANIZATION_RID = 0x000003E8
73
+ FOREST_USER_RID_MAX = 0x000001F3
74
+ DOMAIN_USER_RID_ADMIN = 0x000001F4
75
+ DOMAIN_USER_RID_GUEST = 0x000001F5
76
+ DOMAIN_USER_RID_KRBTGT = 0x000001F6
77
+ DOMAIN_USER_RID_MAX = 0x000003E7
78
+ DOMAIN_GROUP_RID_ADMINS = 0x00000200
79
+ DOMAIN_GROUP_RID_USERS = 0x00000201
80
+ DOMAIN_GROUP_RID_GUESTS = 0x00000202
81
+ DOMAIN_GROUP_RID_COMPUTERS = 0x00000203
82
+ DOMAIN_GROUP_RID_CONTROLLERS = 0x00000204
83
+ DOMAIN_GROUP_RID_CERT_ADMINS = 0x00000205
84
+ DOMAIN_GROUP_RID_SCHEMA_ADMINS = 0x00000206
85
+ DOMAIN_GROUP_RID_ENTERPRISE_ADMINS = 0x00000207
86
+ DOMAIN_GROUP_RID_POLICY_ADMINS = 0x00000208
87
+ DOMAIN_ALIAS_RID_ADMINS = 0x00000220
88
+ DOMAIN_ALIAS_RID_USERS = 0x00000221
89
+ DOMAIN_ALIAS_RID_GUESTS = 0x00000222
90
+ DOMAIN_ALIAS_RID_POWER_USERS = 0x00000223
91
+ DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x00000224
92
+ DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x00000225
93
+ DOMAIN_ALIAS_RID_PRINT_OPS = 0x00000226
94
+ DOMAIN_ALIAS_RID_BACKUP_OPS = 0x00000227
95
+ DOMAIN_ALIAS_RID_REPLICATOR = 0x00000228
96
+ DOMAIN_ALIAS_RID_RAS_SERVERS = 0x00000229
97
+
98
+ DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x0000022A
99
+ DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x0000022B
100
+ DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x0000022C
101
+ DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x0000022D
102
+ DOMAIN_ALIAS_RID_MONITORING_USERS = 0x0000022E
103
+ DOMAIN_ALIAS_RID_LOGGING_USERS = 0x0000022F
104
+ DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x00000230
105
+ DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x00000231
106
+ DOMAIN_ALIAS_RID_DCOM_USERS = 0x00000232
107
+
108
+ # SID types
109
+
110
+ SidTypeUser = 1
111
+ SidTypeGroup = 2
112
+ SidTypeDomain = 3
113
+ SidTypeAlias = 4
114
+ SidTypeWellKnownGroup = 5
115
+ SidTypeDeletedAccount = 6
116
+ SidTypeInvalid = 7
117
+ SidTypeUnknown = 8
118
+ SidTypeComputer = 9
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,97 @@
1
+ require 'ffi'
2
+
3
+ module Windows
4
+ module Security
5
+ module Functions
6
+ extend FFI::Library
7
+
8
+ module FFI::Library
9
+ # Wrapper method for attach_function + private
10
+ def attach_pfunc(*args)
11
+ attach_function(*args)
12
+ private args[0]
13
+ end
14
+ end
15
+
16
+ ffi_lib :kernel32
17
+
18
+ enum :token_info_class, [
19
+ :TokenUser, 1,
20
+ :TokenGroups,
21
+ :TokenPrivileges,
22
+ :TokenOwner,
23
+ :TokenPrimaryGroup,
24
+ :TokenDefaultDacl,
25
+ :TokenSource,
26
+ :TokenType,
27
+ :TokenImpersonationLevel,
28
+ :TokenStatistics,
29
+ :TokenRestrictedSids,
30
+ :TokenSessionId,
31
+ :TokenGroupsAndPrivileges,
32
+ :TokenSessionReference,
33
+ :TokenSandBoxInert,
34
+ :TokenAuditPolicy,
35
+ :TokenOrigin,
36
+ :TokenElevationType,
37
+ :TokenLinkedToken,
38
+ :TokenElevation,
39
+ :TokenHasRestrictions,
40
+ :TokenAccessInformation,
41
+ :TokenVirtualizationAllowed,
42
+ :TokenVirtualizationEnabled,
43
+ :TokenIntegrityLevel,
44
+ :TokenUIAccess,
45
+ :TokenMandatoryPolicy,
46
+ :TokenLogonSid,
47
+ :TokenIsAppContainer,
48
+ :TokenCapabilities,
49
+ :TokenAppContainerSid,
50
+ :TokenAppContainerNumber,
51
+ :TokenUserClaimAttributes,
52
+ :TokenDeviceClaimAttributes,
53
+ :TokenRestrictedUserClaimAttributes,
54
+ :TokenRestrictedDeviceClaimAttributes,
55
+ :TokenDeviceGroups,
56
+ :TokenRestrictedDeviceGroups,
57
+ :TokenSecurityAttributes,
58
+ :TokenIsRestricted,
59
+ :MaxTokenInfoClass
60
+ ]
61
+
62
+ attach_pfunc :GetCurrentProcess, [], :ulong
63
+ attach_pfunc :GetCurrentThread, [], :ulong
64
+ attach_pfunc :GetVersionExA, [:pointer], :bool
65
+ attach_pfunc :GetLastError, [], :ulong
66
+ attach_pfunc :CloseHandle, [:ulong], :bool
67
+
68
+ ffi_lib :advapi32
69
+
70
+ attach_pfunc :AddAccessAllowedAce, [:pointer, :ulong, :ulong, :pointer], :bool
71
+ attach_pfunc :AllocateAndInitializeSid,
72
+ [:pointer, :int, :ulong, :ulong, :ulong, :ulong, :ulong, :ulong, :ulong, :ulong, :pointer], :bool
73
+ attach_pfunc :CheckTokenMembership, [:ulong, :pointer, :pointer], :bool
74
+ attach_pfunc :ConvertSidToStringSid, :ConvertSidToStringSidA, [:pointer, :pointer], :bool
75
+ attach_pfunc :ConvertStringSidToSid, :ConvertStringSidToSidA, [:string, :pointer], :bool
76
+ attach_pfunc :EqualSid, [:pointer, :pointer], :bool
77
+ attach_pfunc :FindFirstFreeAce, [:pointer, :pointer], :bool
78
+ attach_pfunc :GetAclInformation, [:pointer, :pointer, :ulong, :int], :bool
79
+ attach_pfunc :GetLengthSid, [:pointer], :ulong
80
+ attach_pfunc :GetSidLengthRequired, [:uint], :ulong
81
+ attach_pfunc :GetSidSubAuthority, [:pointer, :ulong], :pointer
82
+ attach_pfunc :GetTokenInformation, [:ulong, :token_info_class, :pointer, :ulong, :pointer], :bool
83
+ attach_pfunc :InitializeAcl, [:pointer, :ulong, :ulong], :bool
84
+ attach_pfunc :InitializeSid, [:pointer, :pointer, :uint], :bool
85
+ attach_pfunc :IsValidAcl, [:pointer], :bool
86
+ attach_pfunc :IsValidSid, [:pointer], :bool
87
+ attach_pfunc :IsWellKnownSid, [:pointer, :int], :bool
88
+ attach_pfunc :LookupAccountName, :LookupAccountNameA,
89
+ [:string, :string, :pointer, :pointer, :pointer, :pointer, :pointer], :bool
90
+ attach_pfunc :LookupAccountSid, :LookupAccountSidA,
91
+ [:string, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer], :bool
92
+ attach_pfunc :OpenProcessToken, [:ulong, :ulong, :pointer], :bool
93
+ attach_pfunc :OpenThreadToken, [:ulong, :ulong, :bool, :pointer], :bool
94
+ attach_pfunc :SetAclInformation, [:pointer, :pointer, :ulong, :int], :bool
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,67 @@
1
+ require 'ffi'
2
+
3
+ module Windows
4
+ module Security
5
+ module Structs
6
+ extend FFI::Library
7
+
8
+ class SID_IDENTIFIER_AUTHORITY < FFI::Struct
9
+ layout(:Value, [:char, 6])
10
+ end
11
+
12
+ class OSVERSIONINFO < FFI::Struct
13
+ layout(
14
+ :dwOSVersionInfoSize, :ulong,
15
+ :dwMajorVersion, :ulong,
16
+ :dwMinorVersion, :ulong,
17
+ :dwBuildNumber, :ulong,
18
+ :dwPlatformId, :ulong,
19
+ :szCSDVersion, [:char, 128]
20
+ )
21
+ end
22
+
23
+ class ACE_HEADER < FFI::Struct
24
+ layout(
25
+ :AceType, :uchar,
26
+ :AceFlags, :uchar,
27
+ :AceSize, :ushort
28
+ )
29
+ end
30
+
31
+ class ACCESS_ALLOWED_ACE < FFI::Struct
32
+ layout(
33
+ :Header, ACE_HEADER,
34
+ :Mask, :ulong,
35
+ :SidStart, :ulong
36
+ )
37
+ end
38
+
39
+ class ACCESS_ALLOWED_ACE2 < FFI::Struct
40
+ layout(
41
+ :Header, ACE_HEADER,
42
+ :Mask, :ulong,
43
+ :SidStart, :ulong,
44
+ :dummy, [:uchar, 40]
45
+ )
46
+ end
47
+
48
+ class ACL_STRUCT < FFI::Struct
49
+ layout(
50
+ :AclRevision, :uchar,
51
+ :Sbz1, :uchar,
52
+ :AclSize, :ushort,
53
+ :AceCount, :ushort,
54
+ :Sbz2, :ushort
55
+ )
56
+ end
57
+
58
+ class ACL_SIZE_INFORMATION < FFI::Struct
59
+ layout(
60
+ :AceCount, :ulong,
61
+ :AclBytesInUse, :ulong,
62
+ :AceBytesFree, :ulong
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
data/test/test_acl.rb CHANGED
@@ -4,64 +4,89 @@
4
4
  # Test suite for the Win32::Security::ACL class. You should run these
5
5
  # tests via the 'rake test' task.
6
6
  ########################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
-
7
+ require 'test-unit'
10
8
  require 'win32/security'
11
- require 'test/unit'
9
+ require 'win32/security/sid'
10
+ require 'win32/security/acl'
12
11
 
13
12
  class TC_Win32_Security_Acl < Test::Unit::TestCase
14
- def setup
15
- @acl = Security::ACL.new
16
- end
17
-
18
- def test_version
19
- assert_equal('0.1.0', Security::ACL::VERSION)
20
- end
21
-
22
- def test_ace_count
23
- assert_respond_to(@acl, :ace_count)
24
- assert_kind_of(Fixnum, @acl.ace_count)
25
- assert_equal(0, @acl.ace_count)
26
- end
27
-
28
- def test_acl
29
- assert_respond_to(@acl, :acl)
30
- assert_kind_of(String, @acl.acl)
31
- end
32
-
33
- def test_add_access_allowed_ace
34
- assert_respond_to(@acl, :add_access_allowed_ace)
35
- end
36
-
37
- def test_add_access_denied_ace
38
- assert_respond_to(@acl, :add_access_denied_ace)
39
- end
40
-
41
- def test_add_ace
42
- assert_respond_to(@acl, :add_ace)
43
- end
44
-
45
- def test_delete_ace
46
- assert_respond_to(@acl, :delete_ace)
47
- end
48
-
49
- def test_find_ace
50
- assert_respond_to(@acl, :find_ace)
51
- assert_kind_of(Fixnum, @acl.find_ace)
52
- end
53
-
54
- def test_revision
55
- assert_respond_to(@acl, :revision)
56
- assert_kind_of(Fixnum, @acl.revision)
57
- end
58
-
59
- def test_is_valid
60
- assert_respond_to(@acl, :valid?)
61
- assert_equal(true, @acl.valid?)
62
- end
63
-
64
- def teardown
65
- @acl = nil
66
- end
13
+ def setup
14
+ @acl = Win32::Security::ACL.new
15
+ end
16
+
17
+ test "ACL version is set to the expected value" do
18
+ assert_equal('0.2.0', Win32::Security::ACL::VERSION)
19
+ end
20
+
21
+ test "ace_count basic functionality" do
22
+ assert_respond_to(@acl, :ace_count)
23
+ assert_kind_of(Fixnum, @acl.ace_count)
24
+ end
25
+
26
+ test "ace_count returns the expected value" do
27
+ assert_equal(0, @acl.ace_count)
28
+ end
29
+
30
+ test "ace_count does not accept any arguments" do
31
+ assert_raise(ArgumentError){ @acl.ace_count(0) }
32
+ end
33
+
34
+ test "acl method basic functionality" do
35
+ assert_respond_to(@acl, :acl)
36
+ assert_nothing_raised{ @acl.acl }
37
+ end
38
+
39
+ test "add_access_allowed_ace basic functionality" do
40
+ assert_respond_to(@acl, :add_access_allowed_ace)
41
+ end
42
+
43
+ test "add_access_denied_ace basic functionality" do
44
+ assert_respond_to(@acl, :add_access_denied_ace)
45
+ end
46
+
47
+ test "add_ace basic functionality" do
48
+ assert_respond_to(@acl, :add_ace)
49
+ end
50
+
51
+ test "delete_ace basic functionality" do
52
+ assert_respond_to(@acl, :delete_ace)
53
+ end
54
+
55
+ test "find_ace basic functionality" do
56
+ assert_respond_to(@acl, :find_ace)
57
+ assert_kind_of(Fixnum, @acl.find_ace)
58
+ end
59
+
60
+ test "find_ace returns a sane value" do
61
+ assert_true(@acl.find_ace > 1000)
62
+ end
63
+
64
+ test "revision getter basic functionality" do
65
+ assert_respond_to(@acl, :revision)
66
+ assert_kind_of(Fixnum, @acl.revision)
67
+ end
68
+
69
+ test "revision setter basic functionality" do
70
+ assert_respond_to(@acl, :revision=)
71
+ assert_nothing_raised{ @acl.revision = 3 }
72
+ assert_kind_of(Fixnum, @acl.revision = 3)
73
+ end
74
+
75
+ test "revision setter sets and returns the new value" do
76
+ assert_equal(3, @acl.revision = 3)
77
+ assert_equal(3, @acl.revision)
78
+ end
79
+
80
+ test "valid? basic functionality" do
81
+ assert_respond_to(@acl, :valid?)
82
+ assert_boolean(@acl.valid?)
83
+ end
84
+
85
+ test "valid? returns the expected value" do
86
+ assert_true(@acl.valid?)
87
+ end
88
+
89
+ def teardown
90
+ @acl = nil
91
+ end
67
92
  end
@@ -2,33 +2,18 @@
2
2
  # test_security.rb
3
3
  #
4
4
  # Test suite for the Win32::Security base class. You should run these
5
- # tests via the 'rake test' task.
5
+ # tests via the rake test tasks.
6
6
  ########################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
-
10
- require 'test/unit'
7
+ require 'test-unit'
11
8
  require 'win32/security'
12
- require 'windows/system_info'
13
9
 
14
10
  class TC_Win32_Security < Test::Unit::TestCase
15
- extend Windows::SystemInfo
16
-
17
- def self.startup
18
- @@version = windows_version
19
- end
20
-
21
- def test_version
22
- assert_equal('0.1.4', Win32::Security::VERSION)
23
- end
24
-
25
- def test_elevated_security
26
- omit_if(@@version < 6.0, 'Skipped on Windows 2000 and Windows XP')
27
- assert_respond_to(Win32::Security, :elevated_security?)
28
- assert_boolean(Win32::Security.elevated_security?)
29
- end
11
+ test "version constant is set to expected value" do
12
+ assert_equal('0.2.0', Win32::Security::VERSION)
13
+ end
30
14
 
31
- def self.shutdown
32
- @@version= nil
33
- end
15
+ test "elevated security basic functionality" do
16
+ assert_respond_to(Win32::Security, :elevated_security?)
17
+ assert_boolean(Win32::Security.elevated_security?)
18
+ end
34
19
  end