There are two distinct type of auditing
Example Trigger
Auditing:
Creating Auditing:
Creating
(handler):
Removing auditing:
Standard: Auditing based on statement, Privileges and object level.
Fine-grained: Auditing on a finer granule which is based on content i.e
value > 10,000
The standard auditing having 3 levels of auditing:
Statement: Audit all action at any type of objects.
Privilege: audit action on system level privileges
Object_level: Specific audit action lie select, update, insert or
delete.
For all the 3 level of auditing you can choose to audit by access
(audit every time you access) or by session (audit only once per access during
the session), you can also audit on if the access was successful (whenever
successful) or not (whenever not successful)
DB:
Enables database auditing and directs all audit records
to the database audit trail, except for records that are always written to the
operating system audit trail.DB, extended:
As per the DB value but also populate sqlbind
and sqltext clob columnsOS:
Enables database auditing and directs all
audit records to an operating system fileNONE:
Disables auditing (This value is the default.)Setting Audit Trail:
SQL>alter system set audit_trail = db
scope=spfile;
SQL>alter
system set audit_file_dest = ‘c:\oracle\auditing’;
SQL>alter system set audit_trail = os
scope=spfile;
Note: Note: if the audit_file_dest is not set then
the default location is $oracle_home/rdbms/audit/
SQL>alter system set audit_sys_operations = true
scope = spfile;
Note: this will audit all sys operations regardless
ifaudit_ trail is set.
SQL>alter system set audit_trail = none
scope=spfile;
- Set "audit_trail = true" in the init.ora file.
- Run the $ORACLE_HOME/rdbms/admin/cataudit.sql script while connected as SYS
Audit Options
BY SESSION
causes Oracle to
write a single record for all SQL statements of the same type issued in the
same session. BY ACCESS
causes Oracle to write one record for each access.WHENEVER SUCCESSFUL
chooses auditing only
for statements that succeed. WHENEVER NOT SUCCESSFUL
chooses auditing only
for statements that fail or result in errors.
Auditing Example:
Audit Session By Scott, Lori;
Audit Delete Any Table By Access Whenever Not Successful;
Audit Delete Any Table;
Audit Select Table, Insert Table, Delete Table, Execute Procedure By Access Whenever Not Successful;
Audit Delete On Scott.Emp;
Audit Select, Insert, Delete On Jward.Dept By Access Whenever Successful;
Audit Select On Default Whenever Not Successful;
Audit Select Table By Appserve On Behalf Of Jackson;
Audit Alter, Index, Rename On Default By Session;
Audit Alter User;
Audit Lock Table By Access Whenever Successful;
Audit Delete On Scott.Emp By Access Whenever Successful;
Audit Delete Table, Update Table By Hr By Access;
Audit All By Hr By Access;
Audit Execute Procedure By Hr By Access;
Disabling Audit
Noaudit Table;
Noaudit All Privileges;
Turn Off All Auditing
Noaudit All;
Noaudit All Privileges;
Noaudit All On Default;
Purge Auditing
Delete From Sys.Aud$;
Truncate From Sys.Aud$
Delete From Sys.Aud$;
Delete From Sys.Aud$ Where Obj$Name='Emp';
View Audit Trail
The audit trail is stored in the SYS.AUD$
table. It's contents can be viewed directly or via the following views:
Dba_Audit_Exists, Dba_Audit_Object,
Dba_Audit_Session, Dba_Audit_Statement, Dba_Audit_Trail, Dba_Obj_Audit_Opts,
Dba_Priv_Audit_Opts, Dba_Stmt_Audit_Opts
Auditing Via Trigger
It is possible to audit the system by using triggers, there are a number of system-level triggers that can be fired such as database startup, logon, logoff, ddl, server error
Example Trigger
Auditing:
create or replace trigger audit_insert after
insert on vallep.employees for each row
insert into employees_table_audit
values (user, sysdate);
create or replace trigger logon_audit_trig
after logon on database
begin
insert into logon_audit values (user,
sys_context('userenv', 'sessionid'), sysdate, null,
sys_context('userenv', 'host'));end;
Fine-Grain Auditing
Fine-grain auditing (FGA) allows you to audit users accessing data of a certain criteria. As per standard auditing you can audit select, insert, update and delete operations. You use the package dbms_fga to add, remove, enable and disable FGA auditing policies, it is virtually impossible to bypass these policies, one nice feature is that you can attach handlers (like a trigger) to the policies which can execute procedures which could email or page you. There are many options that can be applied to the dbms_fga package, so best to look up the oracle man pages but here are some simple example
Privilege: grant execute on dbms_fga to vallep;
Creating Auditing:
dbms_fga.add_policy (object_schema => 'vallep',
object_name => 'employees',policy_name
=> 'compensation_aud',
audit_columns => 'salary,commission_pct',
enable => false, statement_types =>
'select');
Creating
(handler):
dbms_fga.add_policy (object_schema => 'vallep',
object_name => 'employees',policy_name
=> 'compensation_aud',
audit_columns => 'salary,commission_pct',
enable => false,statement_types
=> 'select'
handler_schema => 'vallep'
handler_module => 'log_id');
create procedure vallep.log_id (schema1 varchar2, table1
varchar2, policy1
varchar2) as
beginutil_alert_pager(schema1,
table1, policy1);/* send an alert via a pager */ end;
Removing auditing:
dbms_fga.drop_policy (object_schema
=> 'vallep',
object_name => 'employees', policy_name
=> 'compensation_aud');
Enabling auditing:
dbms_fga.enable_policy (object_schema
=> 'vallep',
object_name => 'employees',
policy_name => 'compensation_aud');
Disabling auditing:
dbms_fga.edisable_policy ( object_schema => 'vallep',object_name => 'employees', policy_name => 'compensation_aud');
Usful Tables:
DBA_AUDIT_POLICIES, DBA_FGA_AUDIT_TRAIL, DBA_COMMON_AUDIT_TRAIL
Maintenance
The audit trail must be deleted/archived on a regular basis to
prevent the SYS.AUD$ table growing to an unacceptable size
Security
Only DBAs should have maintenance access to the audit trail. If
SELECT access is required by any applications this can be granted to any users,
or alternatively a specific user may be created for this. Auditing modifications of the data in the audit trail itself can
be achieved as follows:
AUDIT
INSERT, UPDATE, DELETE ON sys.aud$ BY ACCESS;
init parameters
Until Oracle 10g, auditing is disabled by default, but can be enabled by setting the AUDIT_TRAIL static parameter in the init.ora file.
From Oracle 11g, auditing is enabled for some system level privileges.
SQL> show parameter audit
NAME | TYPE | VALUE |
---|---|---|
---------------------- | ------------ | ------------- |
audit_file_dest | string | ?/rdbms/audit |
audit_sys_operations | boolean | FALSE |
audit_trail | string | NONE |
transaction_auditing | boolean | TRUE |
AUDIT_TRAIL can have the following values.
AUDIT_TRAIL={NONE or FALSE| OS| DB or TRUE| DB_EXTENDED| XML |XML_EXTENDED}
The following list provides a description of each value:
- NONE or FALSE -> Auditing is disabled. Default until Oracle 10g.
- DB or TRUE -> Auditing is enabled, with all audit records stored in the database audit trial (AUD$). Default from Oracle 11g.
- DB_EXTENDED –> Same as DB, but the SQL_BIND and SQL_TEXT columns are also populated.
- XML-> Auditing is enabled, with all audit records stored as XML format OS files.
- XML_EXTENDED –> Same as XML, but the SQL_BIND and SQL_TEXT columns are also populated.
- OS -> Auditing is enabled, with all audit records directed to the operating system's file specified by AUDIT_FILE_DEST.
The AUDIT_FILE_DEST parameter specifies the OS directory used for the audit trail when the OS, XML and XML_EXTENDED options are used. It is also the location for all mandatory auditing specified by the AUDIT_SYS_OPERATIONS parameter.
The AUDIT_SYS_OPERATIONS static parameter enables or disables the auditing of operations issued by users connecting with SYSDBA or SYSOPER privileges, including the SYS user. All audit records are written to the OS audit trail.
Run the $ORACLE_HOME/rdbms/admin/cataudit.sql script while connected as SYS (no need to run this, if you ran catalog.sql at the time of database creation).
Start Auditing
Syntax of audit command:
audit {statement_option|privilege_option} [by user] [by
{session|access}] [whenever {successful|not successful}]
Only the statement_option or privilege_option part is mandatory. The other clauses are optional and enabling them allows audit be more specific.
There are three levels that can be audited:
Statement level
Auditing will be done at statement level.
Statements that can be audited are found in STMT_AUDIT_OPTION_MAP.
SQL> audit table by scott;
Audit records can be found in DBA_STMT_AUDIT_OPTS.
SQL> select * from DBA_STMT_AUDIT_OPTS;
Object level
Auditing will be done at object level.
These objects can be audited: tables, views, sequences, packages, stored procedures and stored functions.
SQL> audit insert, update, delete on scott.emp by hr;
Audit records can be found in DBA_OBJ_AUDIT_OPTS.
SQL> select * from DBA_OBJ_AUDIT_OPTS;
Privilege level
Auditing will be done at privilege level.
All system privileges that are found in SYSTEM_PRIVILEGE_MAP can be audited.
SQL> audit create tablespace, alter tablespace by all;
Specify ALL PRIVILEGES to audit all system privileges.
Audit records can be found in DBA_PRIV_AUDIT_OPTS.
SQL> select * from DBA_PRIV_AUDIT_OPTS;
Audit options
BY SESSION
Specify BY SESSION if you want Oracle to write a single record for all SQL statements of the same type issued and operations of the same type executed on the same schema objects in the same session.
Oracle database can write to an operating system audit file but cannot read it to detect whether an entry has already been written for a particular operation. Therefore, if you are using an operating system file for the audit trail (that is, the AUDIT_TRAIL initialization parameter is set to OS), then the database may write multiple records to the audit trail file even if you specify BY SESSION.
SQL> audit create, alter, drop on currency by xe by session;
SQL> audit alter materialized view by session;
BY ACCESS
Specify BY ACCESS if you want Oracle database to write one record for each audited statement and operation.
If you specify statement options or system privileges that audit data definition language (DDL) statements, then the database automatically audits by access regardless of whether you specify the BY SESSION clause or BY ACCESS clause.
For statement options and system privileges that audit SQL statements other than DDL, you can specify either BY SESSION or BY ACCESS. BY SESSION is the default.
SQL> audit update on health by access;
SQL> audit alter sequence by tester by access;
WHENEVER [NOT] SUCCESSFUL
Specify WHENEVER SUCCESSFUL to audit only SQL statements and operations that succeed.
Specify WHENEVER NOT SUCCESSFUL to audit only SQL statements and operations that fail or result in errors.
If you omit this clause, then Oracle Database performs the audit regardless of success or failure.
SQL> audit insert, update, delete on hr.emp by hr by session whenever not successful;
SQL> audit materialized view by pingme by access whenever successful;
Examples
Auditing for every SQL statement related to roles (create, alter, drop or set a role).
SQL> AUDIT ROLE;
Auditing for every statement that reads files from database directory
SQL> AUDIT READ ON DIRECTORY ext_dir;
Auditing for every statement that performs any operation on the sequence
SQL> AUDIT ALL ON hr.emp_seq;
View Audit Trail
The audit trail is stored in the base table SYS.AUD$.
It's contents can be viewed in the following views:
· DBA_AUDIT_TRAIL
· DBA_OBJ_AUDIT_OPTS
· DBA_PRIV_AUDIT_OPTS
· DBA_STMT_AUDIT_OPTS
· DBA_AUDIT_EXISTS
· DBA_AUDIT_OBJECT
· DBA_AUDIT_SESSION
· DBA_AUDIT_STATEMENT
· AUDIT_ACTIONS
· DBA_AUDIT_POLICIES
· DBA_AUDIT_POLICY_COLUMNS
· DBA_COMMON_AUDIT_TRAIL
· DBA_FGA_AUDIT_TRAIL (FGA_LOG$)
· DBA_REPAUDIT_ATTRIBUTE
· DBA_REPAUDIT_COLUMN
The audit trail contains lots of data, but the following are most likely to be of interest:
Username - Oracle Username.
Terminal - Machine that the user performed the action from.
Timestamp - When the action occurred.
Object Owner - The owner of the object that was interacted with.
Object Name - name of the object that was interacted with.
Action Name - The action that occurred against the object (INSERT, UPDATE, DELETE, SELECT, EXECUTE)
Fine Grained Auditing (FGA), introduced in Oracle9i,
allowed recording of row-level changes along with SCN numbers to
reconstruct the old data, but they work for select statements only, not
for DML such as update, insert, and delete.
From Oracle 10g, FGA supports DML statements in addition to selects.
Several fields have been added to both the standard and fine-grained audit trails:
- EXTENDED_TIMESTAMP - A more precise value than the existing TIMESTAMP column.
- PROXY_SESSIONID - Proxy session serial number when an enterprise user is logging in via the proxy method.
- GLOBAL_UID - Global Universal Identifier for an enterprise user.
- INSTANCE_NUMBER - The INSTANCE_NUMBER value from the actioning instance.
- OS_PROCESS - Operating system process id for the oracle process.
- TRANSACTIONID - Transaction identifier for the audited transaction. This column can be used to join to the XID column on the FLASHBACK_TRANSACTION_QUERY view.
- SCN - System change number of the query. This column can be used in flashback queries.
- SQL_BIND - The values of any bind variables if any.
- SQL_TEXT - The SQL statement that initiated the audit action.
The SQL_BIND and SQL_TEXT columns are only populated when the AUDIT_TRAIL=DB_EXTENDED or AUDIT_TRAIL=XML_EXTENDED initialization parameter is set.
The audit trail must be deleted/archived on a regular basis to prevent the SYS.AUD$ table growing to an unacceptable size.
Only users who have been granted specific access to SYS.AUD$ can access the table to select, alter or delete from it. This is usually just the user SYS or any user who has had permissions. There are two specific roles that allow access to SYS.AUD$ for select and delete, these are DELETE_CATALOG_ROLE and SELECT_CATALOG_ROLE. These roles should not be granted to general users.
Auditing modifications of the data in the audit trail itself can be achieved as follows
SQL> AUDIT INSERT, UPDATE, DELETE ON sys.aud$ BY ACCESS;
To delete all audit records from the audit trail:
SQL> DELETE FROM sys.aud$;
From Oracle 11g R2, we can change audit table's (SYS.AUD$ and SYS.FGA_LOG$) tablespace and we can periodically delete the audit trail records using DBMS_AUDIT_MGMT package.
Disabling Auditing
The NOAUDIT statement turns off the various audit options of Oracle. Use it to reset statement, privilege and object audit options. A NOAUDIT statement that sets statement and privilege audit options can include the BY USER option to specify a list of users to limit the scope of the statement and privilege audit options.
SQL> NOAUDIT;
SQL> NOAUDIT session;
SQL> NOAUDIT session BY scott, hr;
SQL> NOAUDIT DELETE ON emp;
SQL> NOAUDIT SELECT TABLE, INSERT TABLE, DELETE TABLE, EXECUTE PROCEDURE;
SQL> NOAUDIT ALL;
SQL> NOAUDIT ALL PRIVILEGES;
SQL> NOAUDIT ALL ON DEFAULT;
Prudent Chartered Accountants is a leading provider of accounting, auditing and management consultancy in the Dubai, UAE. We provide accountancy and auditing services through Middle East. Our professional and qualified team has a wealth of knowledge and experience in areas such as taxation, corporate finance, litigation support, audit assurance and accounting.
ReplyDeleteits similar like staya blog
ReplyDelete