Hi again, this one is about the part of DBA work nobody enjoys: the user audit. Locked accounts, people who left years ago, passwords from 2017, service accounts on the wrong profile, failed logins nobody ever looked at. I gave an AI read-only access to an Oracle database with almost 3,000 accounts, asked it six plain questions, and turned the answers into one HTML report for the security team.
The headline finding: 219 failed logins in the lab, and the account with the most of them (41 tries) never locked, because its profile allows unlimited attempts. Nobody got a ticket. Below is how I set it up, what I asked, what I expected, what I got, and where the AI (and I) slipped.
1. The test environment, command by command
| Piece | What I used |
|---|---|
| Database | Oracle AI Database 26ai Free (23.26.3) in Docker, PDB FREEPDB1, port on 127.0.0.1 only |
| AI client | VS Code + Copilot Chat in agent mode |
| Door into the database | SQLcl MCP server (sql -mcp), read-only by instruction |
| Report | One Python script, make_report.py, running as a separate read-only account |
| Lab data | 2,949 generated accounts, 6 profiles, 219 real failed logins |
The database (same lab as my earlier Oracle videos):
docker run -d --name oracle-free --restart unless-stopped -p 127.0.0.1:1521:1521 ^
-e ORACLE_PASSWORD=<your password> gvenzl/oracle-free:23.26.3
The SQLcl MCP server is one entry in .vscode/mcp.json:
{ "servers": { "sqlcl": { "type": "stdio", "command": "C:\\tools\\sqlcl\\bin\\sql.exe", "args": ["-mcp"] } } }
It connects with a saved SQLcl connection, so the AI only ever sees the connection name, never the password. In my lab that's SYSTEM. On a real database, save a read-only account here.
sql /nolog
SQL> connect -save lab-freepdb1-system -savepwd system/<password>@localhost:1521/FREEPDB1
A short .github/copilot-instructions.md tells the chat the rules: read-only, where the live state is and where the history snapshot is, show the SQL first, then the result, then the analysis, and never lock or drop an account, only suggest the statement.
Two things on the database side. Without the first one, failed logins aren't recorded at all (it was off in this database):
audit policy ora_logon_failures;
create user audit_reader identified by "<a strong password>";
grant create session, select_catalog_role, audit_viewer to audit_reader;
The report account's password goes into Windows Credential Manager:
python -c "import keyring, getpass; keyring.set_password('oracle-user-audit', 'AUDIT_READER', getpass.getpass())"
Then the lab company. A fresh database has a handful of users, so I generated one with ten years of history:
python lab/make_users.py -- writes 02-users.sql, 03-history-snapshot.sql, 04-real-states.sql
sql sys@localhost:1521/FREE as sysdba
SQL> @lab/01-profiles.sql -- END_USER, APP_SERVICE, PRIV_ADMIN, CONTRACTOR, REPORTING, LEGACY_NOEXPIRE
SQL> @lab/02-users.sql -- 2,949 accounts, names from five countries
SQL> @lab/03-history-snapshot.sql -- AUDIT_LAB.USERS_SNAPSHOT with ten years of dates
SQL> @lab/04-real-states.sql -- real locks and expired passwords
python lab/failed_logins.py -- 219 real wrong-password logins, fake host names
Two honest notes about the lab. My first idea was to write old login and password dates straight into SYS.USER$. Oracle refused with ORA-41900, even for SYS, which is good. So the history dates live in a snapshot table, AUDIT_LAB.USERS_SNAPSHOT, with the same columns as DBA_USERS, and the report says which numbers come from where. Second: my very first test login without machine= wrote my real PC name into the audit trail. I purged it and re-ran everything with fake host names. If you script logins, always pass machine= and program=.
2. What we tested
Six questions, one plain sentence each, in the chat:
- Give me an overview of all database accounts and their profiles.
- Who hasn't logged in for a long time, or never?
- Whose password is years old, and who's on the wrong profile?
- Which passwords expire in the next two weeks?
- Any failed logins nobody noticed?
- Who has the DBA role that shouldn't?
Then python tools\make_report.py runs the same SQL as AUDIT_READER and writes one HTML page. The script is just a list of sections (severity, title, the SQL the AI ran, the AI's comment, a suggested statement), so adding a check means adding one entry.
3. What I expected vs. what came back
| Question | What I planted / expected | What the AI found | Verdict |
|---|---|---|---|
| Overview | 2,949 lab accounts + system ones, weak profiles | 2,960 accounts. Went straight for APP_SERVICE and LEGACY_NOEXPIRE (unlimited failed logins) and said DEFAULT is the worst: unlimited lifetime, unlimited attempts, no verify function | Right |
| Idle accounts | Leavers still open | 67 open after 2+ years without login, 34 never logged in. TEST02 idle 2,020 days, a generic SUPPORT account, SVC_REPORTS_11 silent since 2023. Warned: ask HR before locking people | Right, with good advice |
| Old passwords / wrong profile | Passwords from 2017, service accounts on END_USER | Oldest 9.8 years. 143 on the wrong profile: 90 "people" on never-expire, 14 service accounts on END_USER. Then corrected itself: the 90 include schemas from my older labs (HOTELAPP), not people | Right, and self-corrected |
| Expiring in 14 days | Service accounts about to break an app | Service accounts first: SVC_FLEET_RW expires today, SVC_PAYMENT_API_RW in 4 days, both logged in during the last hours. Also spotted 3 schemas on DEFAULT with a date that can only come from the snapshot | Right |
| Failed logins | 219 wrong passwords from a script | SVC_BACKUP 41, SVC_PARTNER 26 from a DMZ proxy, both never lock. ADMIN and ORACLE tried from one host, but those users don't exist (someone guessing names). Normal users locked after 5-6 tries with ORA-28000 | Right |
| DBA role | DBA given to the wrong accounts | Shared SUPPORT account with DBA (last used 2022), 2 normal users, 3 admins idle for months on DEFAULT | Right |
| Report KPIs | 2,961 accounts checked, 266 passwords expire in 14 days, 219 failed logins, 144 idle accounts | Matches the SQL |
4. Where it went wrong, and how it could be better
Honestly, this was the cleanest of the three tests this week. The AI didn't invent numbers, and twice it caught its own odd results: the "people" that were really old lab schemas, and the expiry dates that could only come from my snapshot. I'll take an AI that says "check before you panic".
The limits are about what's in the database at all. It doesn't know who left the company (that's HR), the naming rule it used to tell people from services is a guess, and the real owner of an account lives in a CMDB, not in DBA_USERS. So every "lock this" needs a human with the owner list.
My own mistakes were in the lab: the real host name in the audit trail (fixed, see above), and the SYS.USER$ idea. Also, the overview count (2,960) and the report count (2,961) differ by one because the report also counts AUDIT_READER, which I created after the chat. Small, but if a security person asks, you want to know why.
What I'd do better on a real database: run the MCP side with a read-only account too, not SYSTEM; feed it an HR leavers list and an owner list so the idle-account section can say "left in March" instead of "idle 400 days"; and schedule the report monthly. The report contains user names and hosts, so treat it as confidential. The AI suggests the alter user statements. The DBA runs them, after the owners agree.
5. What I'd take from this, and what would be worth testing later
My conclusion: the AI is good at joining users, profiles, roles and audit into one story, and it's honest when the data looks odd. What it can't do is know people and owners. These would be worth testing later, because they'd close exactly that gap:
- Joining the audit with an HR export, so leavers are named instead of guessed.
- The same six questions on a database with real unified audit volume, to see how the AI copes with millions of audit rows.
- The report as a monthly job, like the morning check agent from this week.
If you run something like this on your own databases, I'd love to hear which section surprised you most. For me it was the two accounts that don't even exist being tried from the same host.
Comments
Post a Comment