Implementing Audit Policies in Oracle Database
Creating a Privilege Audit Policy
Steps:
- Create user xyz1 and grant user resource, connect, create, and drop any table privileges.
- Grant the user xyz1 the privilege to select any table:
GRANT SELECT ANY TABLE TO xyz1;
- Create policy:
CREATE AUDIT POLICY aud100
PRIVILEGES select any table, create library
WHEN 'SYS_CONTEXT (''USERENV'', ''SESSION_USER'') = ''xyz1'' '
EVALUATE PER STATEMENT;
- Enable Policy:
AUDIT POLICY aud100;
- View the audit policy options:
SELECT POLICY_NAME, AUDIT_OPTION, CONDITION_EVAL_OPT
FROM AUDIT_UNIFIED_POLICIES
WHERE POLICY_NAME ='aud100';
- Verify that the audit policy is enabled:
SELECT POLICY_NAME, ENABLED_OPT, USER_NAME, SUCCESS, FAILURE
FROM AUDIT_UNIFIED_ENABLED_POLICIES
WHERE POLICY_NAME ='aud100';
- Test the policy by performing an audit operation such as selecting a table:
CONNECT xyz1/password
SELECT * FROM <schema>.<table_name>;
- View the auditing data using a Unified_Auditing_Trail query:
SELECT DBUSERNAME, ACTION_NAME, SYSTEM_PRIVILEGE_USED from unified_audit_trail where DBUSERNAME in ('xyz1');
Creating an Action Audit Policy
Create an audit policy that will audit any user performing any select or update operation on any object using an object or system privilege, or deleting rows from the XYZ1.CUSTOMER table.
CREATE AUDIT POLICY audit_actions_policy1
ACTIONS select, update, delete on xyz1.customer;
- Enable the audit policy for all users:
AUDIT POLICY audit_actions_policy1;
- View the audit policy options:
SELECT POLICY_NAME, AUDIT_OPTION, OBJECT_NAME
FROM AUDIT_UNIFIED_POLICIES
WHERE POLICY_NAME ='audit_actions_policy1';
- Verify that the audit policy is enabled:
SELECT POLICY_NAME, ENABLED_OPT, USER_NAME, SUCCESS, FAILURE
FROM AUDIT_UNIFIED_ENABLED_POLICIES
WHERE POLICY_NAME ='audit_actions_policy1';
- Perform an audited operation. First, create a new user xyz2 and grant appropriate privileges to xyz2 to execute operations:
CREATE USER xyz2 IDENTIFIED BY abc123;
GRANT create session TO xyz2;
CONNECT xyz1/xxxxxxxx
GRANT delete ON xyz1.customer TO xyz2;
CONNECT xyz2/abc123;
SELECT * FROM xyz1.customer;
DELETE xyz1.customer;
- View the resulting audit data:
CONNECT system/xxxxxxxxx
SELECT UNIFIED_AUDIT_POLICIES, DBUSERNAME, ACTION_NAME,
SYSTEM_PRIVILEGE_USED, to_char(EVENT_TIMESTAMP,'DD-MON-YY HH:MI') "DATE"
FROM unified_audit_trail
WHERE DBUSERNAME in ('xyz2')
AND ACTION_NAME not in ('LOGON', 'LOGOFF')
ORDER BY 4;
If the audited data is still in memory, you cannot see it. We do not want to wait until the background process flushes the data to disk.
EXEC SYS.DBMS_AUDIT_MGMT.FLUSH_UNIFIED_AUDIT_TRAIL
Now you can view the resulting audit data.
SELECT UNIFIED_AUDIT_POLICIES, DBUSERNAME, ACTION_NAME,
SYSTEM_PRIVILEGE_USED, to_char(EVENT_TIMESTAMP,'DD-MON-YY HH:MI') "DATE"
FROM unified_audit_trail
WHERE DBUSERNAME in ('xyz2')
AND ACTION_NAME not in ('LOGON', 'LOGOFF')
ORDER BY 4;
Creating a Role Audit Policy
Create an audit policy that will audit all users while using the manager role.
CREATE USER xyz3 IDENTIFIED BY abc123;
CREATE ROLE MANAGER;
GRANT create tablespace TO MANAGER;
GRANT MANAGER, create session TO xyz3;
CREATE AUDIT POLICY audit_roles_policy1
ROLES MANAGER;
Create an audit policy that will audit all users as soon as these users use the DBA role.
Create a DBA_JUNIOR user granted the DBA role.
CREATE USER DBA_JUNIOR IDENTIFIED BY aaabbcc123;
GRANT DBA TO DBA_JUNIOR;
CREATE AUDIT POLICY audit_dba_policy1
ROLES dba;
- Enable the audit policies whenever the execution is completed successfully only:
AUDIT POLICY audit_roles_policy1 WHENEVER SUCCESSFUL;
AUDIT POLICY audit_dba_policy1 WHENEVER SUCCESSFUL;
- View the audit policy options.