|
| 1 | +/* |
| 2 | +Author: buckley |
| 3 | +Original link: https://stackoverflow.com/a/7059579/2298061 |
| 4 | +
|
| 5 | +Security Audit Report |
| 6 | +1) List all access provisioned to a sql user or windows user/group directly |
| 7 | +2) List all access provisioned to a sql user or windows user/group through a database or application role |
| 8 | +3) List all access provisioned to the public role |
| 9 | +
|
| 10 | +Columns Returned: |
| 11 | +UserName : SQL or Windows/Active Directory user cccount. This could also be an Active Directory group. |
| 12 | +UserType : Value will be either 'SQL User' or 'Windows User'. This reflects the type of user defined for the |
| 13 | + SQL Server user account. |
| 14 | +DatabaseUserName: Name of the associated user as defined in the database user account. The database user may not be the |
| 15 | + same as the server user. |
| 16 | +Role : The role name. This will be null if the associated permissions to the object are defined at directly |
| 17 | + on the user account, otherwise this will be the name of the role that the user is a member of. |
| 18 | +PermissionType : Type of permissions the user/role has on an object. Examples could include CONNECT, EXECUTE, SELECT |
| 19 | + DELETE, INSERT, ALTER, CONTROL, TAKE OWNERSHIP, VIEW DEFINITION, etc. |
| 20 | + This value may not be populated for all roles. Some built in roles have implicit permission |
| 21 | + definitions. |
| 22 | +PermissionState : Reflects the state of the permission type, examples could include GRANT, DENY, etc. |
| 23 | + This value may not be populated for all roles. Some built in roles have implicit permission |
| 24 | + definitions. |
| 25 | +ObjectType : Type of object the user/role is assigned permissions on. Examples could include USER_TABLE, |
| 26 | + SQL_SCALAR_FUNCTION, SQL_INLINE_TABLE_VALUED_FUNCTION, SQL_STORED_PROCEDURE, VIEW, etc. |
| 27 | + This value may not be populated for all roles. Some built in roles have implicit permission |
| 28 | + definitions. |
| 29 | +ObjectName : Name of the object that the user/role is assigned permissions on. |
| 30 | + This value may not be populated for all roles. Some built in roles have implicit permission |
| 31 | + definitions. |
| 32 | +ColumnName : Name of the column of the object that the user/role is assigned permissions on. This value |
| 33 | + is only populated if the object is a table, view or a table value function. |
| 34 | +*/ |
| 35 | + |
| 36 | +--List all access provisioned to a sql user or windows user/group directly |
| 37 | +SELECT |
| 38 | + [UserName] = CASE princ.[type] |
| 39 | + WHEN 'S' THEN princ.[name] |
| 40 | + WHEN 'U' THEN ulogin.[name] COLLATE Latin1_General_CI_AI |
| 41 | + END, |
| 42 | + [UserType] = CASE princ.[type] |
| 43 | + WHEN 'S' THEN 'SQL User' |
| 44 | + WHEN 'U' THEN 'Windows User' |
| 45 | + END, |
| 46 | + [DatabaseUserName] = princ.[name], |
| 47 | + [Role] = null, |
| 48 | + [PermissionType] = perm.[permission_name], |
| 49 | + [PermissionState] = perm.[state_desc], |
| 50 | + [ObjectType] = obj.type_desc,--perm.[class_desc], |
| 51 | + [ObjectName] = OBJECT_NAME(perm.major_id), |
| 52 | + [ColumnName] = col.[name] |
| 53 | +FROM |
| 54 | + --database user |
| 55 | + sys.database_principals AS princ |
| 56 | +LEFT JOIN |
| 57 | + --Login accounts |
| 58 | + sys.login_token AS ulogin on princ.[sid] = ulogin.[sid] |
| 59 | +LEFT JOIN |
| 60 | + --Permissions |
| 61 | + sys.database_permissions AS perm ON perm.[grantee_principal_id] = princ.[principal_id] |
| 62 | +LEFT JOIN |
| 63 | + --Table columns |
| 64 | + sys.columns col ON col.[object_id] = perm.major_id |
| 65 | + AND col.[column_id] = perm.[minor_id] |
| 66 | +LEFT JOIN |
| 67 | + sys.objects obj ON perm.[major_id] = obj.[object_id] |
| 68 | +WHERE |
| 69 | + princ.[type] in ('S','U') |
| 70 | +UNION |
| 71 | +--List all access provisioned to a sql user or windows user/group through a database or application role |
| 72 | +SELECT |
| 73 | + [UserName] = CASE memberprinc.[type] |
| 74 | + WHEN 'S' THEN memberprinc.[name] |
| 75 | + WHEN 'U' THEN ulogin.[name] COLLATE Latin1_General_CI_AI |
| 76 | + END, |
| 77 | + [UserType] = CASE memberprinc.[type] |
| 78 | + WHEN 'S' THEN 'SQL User' |
| 79 | + WHEN 'U' THEN 'Windows User' |
| 80 | + END, |
| 81 | + [DatabaseUserName] = memberprinc.[name], |
| 82 | + [Role] = roleprinc.[name], |
| 83 | + [PermissionType] = perm.[permission_name], |
| 84 | + [PermissionState] = perm.[state_desc], |
| 85 | + [ObjectType] = obj.type_desc,--perm.[class_desc], |
| 86 | + [ObjectName] = OBJECT_NAME(perm.major_id), |
| 87 | + [ColumnName] = col.[name] |
| 88 | +FROM |
| 89 | + --Role/member associations |
| 90 | + sys.database_role_members members |
| 91 | +JOIN |
| 92 | + --Roles |
| 93 | + sys.database_principals roleprinc ON roleprinc.[principal_id] = members.[role_principal_id] |
| 94 | +JOIN |
| 95 | + --Role members (database users) |
| 96 | + sys.database_principals memberprinc ON memberprinc.[principal_id] = members.[member_principal_id] |
| 97 | +LEFT JOIN |
| 98 | + --Login accounts |
| 99 | + sys.login_token ulogin on memberprinc.[sid] = ulogin.[sid] |
| 100 | +LEFT JOIN |
| 101 | + --Permissions |
| 102 | + sys.database_permissions perm ON perm.[grantee_principal_id] = roleprinc.[principal_id] |
| 103 | +LEFT JOIN |
| 104 | + --Table columns |
| 105 | + sys.columns col on col.[object_id] = perm.major_id |
| 106 | + AND col.[column_id] = perm.[minor_id] |
| 107 | +LEFT JOIN |
| 108 | + sys.objects obj ON perm.[major_id] = obj.[object_id] |
| 109 | +UNION |
| 110 | +--List all access provisioned to the public role, which everyone gets by default |
| 111 | +SELECT |
| 112 | + [UserName] = '{All Users}', |
| 113 | + [UserType] = '{All Users}', |
| 114 | + [DatabaseUserName] = '{All Users}', |
| 115 | + [Role] = roleprinc.[name], |
| 116 | + [PermissionType] = perm.[permission_name], |
| 117 | + [PermissionState] = perm.[state_desc], |
| 118 | + [ObjectType] = obj.type_desc,--perm.[class_desc], |
| 119 | + [ObjectName] = OBJECT_NAME(perm.major_id), |
| 120 | + [ColumnName] = col.[name] |
| 121 | +FROM |
| 122 | + --Roles |
| 123 | + sys.database_principals roleprinc |
| 124 | +LEFT JOIN |
| 125 | + --Role permissions |
| 126 | + sys.database_permissions perm ON perm.[grantee_principal_id] = roleprinc.[principal_id] |
| 127 | +LEFT JOIN |
| 128 | + --Table columns |
| 129 | + sys.columns col on col.[object_id] = perm.major_id |
| 130 | + AND col.[column_id] = perm.[minor_id] |
| 131 | +JOIN |
| 132 | + --All objects |
| 133 | + sys.objects obj ON obj.[object_id] = perm.[major_id] |
| 134 | +WHERE |
| 135 | + --Only roles |
| 136 | + roleprinc.[type] = 'R' AND |
| 137 | + --Only public role |
| 138 | + roleprinc.[name] = 'public' AND |
| 139 | + --Only objects of ours, not the MS objects |
| 140 | + obj.is_ms_shipped = 0 |
| 141 | +ORDER BY |
| 142 | + princ.[name], |
| 143 | + OBJECT_NAME(perm.major_id), |
| 144 | + col.[name], |
| 145 | + perm.[permission_name], |
| 146 | + perm.[state_desc], |
| 147 | + obj.type_desc; |
0 commit comments