Applied Generative AI SQL Server database security audit using Claude as an audit aperture tool — validated against Microsoft Learn, tested empirically.
| File | Description |
|---|---|
README.md |
This file — project overview and walkthrough |
permissions_baseline.csv |
Full permission export from the HospitalDB simulation environment |
remediation_script.sql |
Validated GRANT/REVOKE T-SQL for all true findings |
Module7_Permission_Audit_Waqar_Javed.docx |
Complete assignment deliverable (audit transcript, findings, reflection) |
This project demonstrates using AI (Claude) to audit SQL Server database permissions for:
- Over-privileged grants — permissions that exceed a principal's role function
- Separation-of-duties violations — role compositions that create dangerous effective permissions
- Redundant grants — object-level permissions already covered by schema or role
- RBAC bypasses — direct grants that route around the intended role hierarchy
The core discipline: AI widens the audit aperture. It does not close the audit. Every finding was cross-checked against Microsoft Learn and tested empirically before any remediation was accepted.
Run these three queries in SSMS against your database:
-- Database-scoped permissions
SELECT
dp.name AS principal_name,
dp.type_desc AS principal_type,
perm.permission_name,
perm.state_desc AS permission_state,
COALESCE(obj.name,'') AS object_name,
COALESCE(obj.type_desc,'') AS object_type,
grantor.name AS grantor
FROM sys.database_permissions perm
JOIN sys.database_principals dp ON perm.grantee_principal_id = dp.principal_id
JOIN sys.database_principals grantor ON perm.grantor_principal_id = grantor.principal_id
LEFT JOIN sys.objects obj ON perm.major_id = obj.object_id
ORDER BY dp.name, perm.permission_name;
-- Server-scoped permissions
SELECT
sp.name AS principal_name,
sp.type_desc AS principal_type,
perm.permission_name,
perm.state_desc AS permission_state
FROM sys.server_permissions perm
JOIN sys.server_principals sp ON perm.grantee_principal_id = sp.principal_id
ORDER BY sp.name, perm.permission_name;
-- Role memberships
SELECT
member.name AS member_name,
role.name AS role_name
FROM sys.database_role_members drm
JOIN sys.database_principals role ON drm.role_principal_id = role.principal_id
JOIN sys.database_principals member ON drm.member_principal_id = member.principal_id;Export the results to permissions_baseline.csv (right-click results grid in SSMS → Save Results As).
Paste your CSV into Claude (or ChatGPT) with this prompt:
Below is the full permission export from my SQL Server environment.
Please audit it for:
1. Over-privileged grants (permissions that exceed role function)
2. Separation-of-duties violations
3. Redundant grants already covered by a role
4. Grants that bypass intended RBAC
For each finding provide:
(a) finding label
(b) affected principal
(c) specific permission
(d) risk rationale
(e) recommended GRANT/REVOKE remediation
Save the full AI response — this becomes your audit transcript.
For each AI finding, look up the exact permission in Microsoft Learn and classify it:
| Classification | Meaning |
|---|---|
| ✅ TRUE FINDING | Real risk confirmed by MS Learn permission semantics |
| ❌ FALSE POSITIVE | Intentional design or not actually a risk in context |
| AI misunderstood DENY precedence, role composition, or schema scope |
This step is mandatory — do not skip it. See the findings table in Module7_Permission_Audit_Waqar_Javed.docx for the full classification with MS Learn citations.
Ask the AI to generate the fix script, then review it manually before running anything:
-- Example: AI-generated remediation (always validate before running)
REVOKE ALTER ANY USER FROM AppServiceAccount;
REVOKE EXECUTE ON DATABASE::HospitalDB FROM AppServiceAccount;
ALTER ROLE BillingDept DROP MEMBER jr_nurse_chen;
-- ... see remediation_script.sql for full validated scriptRun each statement in a dev/test environment first. Verify the bad access is blocked using EXEC AS USER impersonation tests.
Look for at least one AI-proposed remediation that would break legitimate access. The classic failure mode:
AI proposed:
REVOKE SELECT ON dbo.PatientRecords FROM BackupOperatorto "clean up" a DENY entry it called redundant.Why it's wrong:
DENYandREVOKEhave completely different future-state behaviors.
REVOKEremoves the record — a later role grant will take effect silently.DENYpersists and wins over any futureGRANTfrom any source.Running the AI's REVOKE would have silently removed a defense-in-depth control.
MS Learn reference: DENY (Transact-SQL) — "DENY overrides a GRANT from any source."
Watch for these in any AI-assisted SQL Server audit:
- DENY vs REVOKE confusion — AI treats DENY as redundant when no GRANT exists; proposes REVOKE which removes future protection
- Role composition blindness — AI may miss that membership in two individually-reasonable roles creates a dangerous combined effective permission set
- Schema vs object scope — Schema-level
SELECTpropagates to all contained objects; AI sometimes flags the object grant as the problem when the schema grant is the root cause - Server vs database DENY precedence — AI sometimes believes a server-level GRANT overrides a database-level DENY (it does not)
- Invented permission types — AI may suggest syntax like
GRANT EXECUTE ON DATABASEwhich is not valid in all SQL Server versions
| Classification | Count | % |
|---|---|---|
| ✅ True Finding | 8 | 80% |
| ❌ False Positive | 0 | 0% |
| 2 | 20% | |
| Total | 10 | 100% |
Key findings included:
AppServiceAccounthadALTER ANY USERand database-wideEXECUTE— far beyond app DML needsETLProcesshadALTER ANY SCHEMAandTRUNCATEon ePHI tablesDevOpsLoginheld bothCONTROL SERVERandsysadmin— dual SA-equivalent grantsjr_nurse_chenwas member of bothNursingStaffandBillingDept— clinical + financial SoD breachbilling_mgr_kimcouldINSERTintoAuditLogvia role composition — could cover own transactions
- Permissions (Database Engine) — Microsoft Learn
- sys.database_permissions — Microsoft Learn
- DENY (Transact-SQL) — Microsoft Learn
- REVOKE (Transact-SQL) — Microsoft Learn
- Server-level roles — Microsoft Learn
- HIPAA Minimum Necessary Standard — HHS
Waqar Javed · Applied Generative AI
GitHub: @iamwaqarjaved