Security Audits: Finding What You Missed

Security audits are the thing every DBA knows they should do more often and rarely has time for. You check the obvious — sysadmin members, sa account status — and move on to the next fire. The gaps you don’t check are the ones that show up in a penetration test report or, worse, a breach notification.

An AI agent doesn’t make security audits exciting. But it makes them fast enough that you might actually do them regularly.

Orphaned Users

Every environment has them. Employees leave, Active Directory groups change, databases get restored from backups — and user accounts accumulate like sediment.

The agent generates a script that uses sys.database_principals, sys.server_principals, and sys.database_role_members to cross-reference across every database. The important detail: it flags orphaned users who still have elevated permissions — those are your highest-priority remediation targets.

What this can’t detect from SQL Server alone: disabled Active Directory accounts (you need AD/Entra ID queries for that), and dormant contained database users (SQL Server doesn’t track last authentication time without explicit auditing or Extended Events). Both are worth checking, but require tooling outside T-SQL.

Try This Yourself: Run the orphaned user query the agent generates. Most DBAs find at least a few surprises, especially on instances that have been running for years.

Permission Sprawl: Who Has Sysadmin and Why?

This is the question every auditor asks, and too many DBAs answer by querying sys.server_role_members and calling it done. The real answer is more complex.

The agent can’t query Active Directory for group membership or classify accounts as “person” vs “service account” — that requires AD/Entra ID data. But it generates the SQL Server side: login-to-role mappings, CONTROL SERVER grants, and the audit structure that covers the paths auditors care about. You’ll still need to expand Windows group membership and classify identities using AD tooling.

For a reusable, version-controlled approach to this kind of audit, I built ai-security-audit — a set of AI-ready prompt templates for common security assessments.

Connection Encryption

With TLS requirements tightening — especially for compliance frameworks like PCI-DSS and SOX — knowing which instances accept unencrypted connections matters.

The agent generates what T-SQL can tell you — current session encryption status — and correctly flags what it can’t: Force Encryption configuration, TLS protocol version negotiation (the DMVs show TDS protocol, not TLS version), and certificate binding details. For those, you need server-level inspection or external tooling.

For a fleet-wide TLS audit that covers the full picture — certificate details, SPN diagnostics, protocol configuration, and health checks across all your instances — sql-cert-inspector handles the parts T-SQL can’t reach. We covered automating it with PowerShell in an earlier post.

Dynamic SQL Injection Surface

This is the audit most DBAs never do — and it’s the one that matters most from a security perspective. How many of your stored procedures build dynamic SQL by concatenating user input?

The agent parses procedure definitions and flags the risky patterns. This is a triage scan, not a definitive security audit — it’s heuristic pattern matching on source text with significant blind spots: encrypted modules are invisible, CLR procedures bypass T-SQL entirely, SQL Agent job steps contain their own dynamic SQL, and complex variable flow through multiple assignments can hide concatenation from simple text analysis. And note that QUOTENAME only protects against identifier injection (table/column names), not arbitrary value injection — those still need parameterized sp_executesql.

But as a first pass to find the procedures worth deeper investigation, it beats manually reading through hundreds of module definitions. In my experience, legacy codebases almost always have a few procedures that concatenate parameters directly into EXEC() calls with no sanitization.

This is exactly the kind of audit that becomes a satellite post with a deeper dive into remediation patterns.

Compliance Report Generation

Auditors want evidence. They want it formatted their way. And they want it now.

The agent generates queries for what SQL Server metadata can actually evidence — login configurations, role memberships, audit settings — and flags what it can’t prove from T-SQL alone. And that’s a long list: “no default passwords” requires password policy verification outside the engine, “unnecessary services disabled” is an OS/configuration concern, and “audit trail for cardholder data access” depends entirely on your scope definition and whether you’ve already configured SQL Server Audit or Extended Events to capture those events.

Use the generated scripts as a starting point for evidence collection, not as a finished compliance package. The agent’s knowledge of compliance frameworks comes from training data, not from a certified QSA. Your auditor has the final word on what constitutes sufficient evidence — and they will have opinions about your scope, your compensating controls, and your evidence format that no AI can anticipate.

Reviewing Your Own Security Scripts

One of the most useful patterns: point the agent at your existing security scripts and ask it to find the gaps.

The agent reviews your audit scripts with the same rigor you’d apply to production code. It often catches edge cases: contained database users that bypass server-level audits, Windows group nesting that hides effective permissions, cross-database ownership chains that grant unintended access.

The Audit Areas You Probably Aren’t Checking

Beyond the sections above, here are security surface areas that a thorough audit should cover — and that the agent can help you script:

  • guest user enabled in user databases (it shouldn’t be, except in msdb)
  • public role grants beyond defaults — one of the most common permission sprawl vectors
  • TRUSTWORTHY database property enabled (escalation path to sysadmin)
  • Cross-database ownership chaining enabled at instance level
  • Database owners set to sa or high-privilege accounts when they shouldn’t be
  • Linked servers with saved credentials or overly permissive security contexts
  • SQL Agent job ownership and proxy/credential configuration
  • xp_cmdshell, OLE Automation, Ad Hoc Distributed Queries, CLR — surface area configuration
  • Server-level impersonation grants that create privilege escalation paths
  • SQL Server Audit — is it actually configured and running, or just planned?

Each of these is a prompt away from a diagnostic script. The agent won’t know your environment’s risk tolerance — that’s your judgment call — but it can enumerate the current state faster than you can type the DMV names.

Deep Dives

Want to go deeper? These companion posts walk through specific scenarios in detail:


Next up: Migration Planning: Compatibility Checks and Deprecated Features — using AI to plan and de-risk SQL Server version upgrades.


Part of the ALTER DBA ADD AGENT series — Previous: Incident Response