数据库通过audit_trail参数来开启标准审计,我所服务的客户很少有开审计的,
这里写一下实验过程,方便有特殊需求的时候查看一下。当然,这里不涉及到细粒度审计功能。
audit_trail的参数有下面几种:
NONE:不开启审计
OS:说明审计信息放在系统汇总,如果是Linux那么由audit_file_dest 决定,如果是Windows 那么由事件查看器决定
DB 或 TRUE :表示审计信息存放在数据库里,也就是sys 用户的aud$ 表。
audit_sys_operations参数的含义:
false:不审计sys用户,默认不审计
true:审计sys用户
1、开启审计参数
SQL> alter system set audit_trail=db_extended scope=spfile sid='*';
2、重启数据库
静态参数,为了使参数生效,需要重启数据库
----------------------- RAC环境 su - oracle $ srvctl stop database -d orcl $ srvctl start database -d orcl ----------------------- 单实例环境 SQL> shutdown immediate; SQL> startup; ----------------------- 查看参数 SQL> show parameter audit NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ audit_file_dest string /u02/app/oracle/admin/testaud/ adump audit_sys_operations boolean FALSE audit_syslog_level string audit_trail string DB_EXTENDED
3、权限审计
数据库版本是11.2.0.4 SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production PL/SQL Release 11.2.0.4.0 - Production CORE 11.2.0.4.0 Production TNS for Linux: Version 11.2.0.4.0 - Production NLSRTL Version 11.2.0.4.0 - Production 查看默认审计哪些权限 SELECT PRIVILEGE, SUCCESS, FAILURE FROM DBA_PRIV_AUDIT_OPTS WHERE USER_NAME IS NULL AND PROXY_NAME IS NULL ORDER BY PRIVILEGE; PRIVILEGE SUCCESS FAILURE ---------------------------------------- ---------- ---------- DROP PROFILE BY ACCESS BY ACCESS ALTER PROFILE BY ACCESS BY ACCESS ALTER SYSTEM BY ACCESS BY ACCESS ALTER DATABASE BY ACCESS BY ACCESS DROP USER BY ACCESS BY ACCESS CREATE SESSION BY ACCESS BY ACCESS CREATE USER BY ACCESS BY ACCESS ALTER USER BY ACCESS BY ACCESS EXEMPT ACCESS POLICY BY ACCESS BY ACCESS CREATE PUBLIC DATABASE LINK BY ACCESS BY ACCESS CREATE ANY LIBRARY BY ACCESS BY ACCESS CREATE ANY JOB BY ACCESS BY ACCESS CREATE EXTERNAL JOB BY ACCESS BY ACCESS AUDIT SYSTEM BY ACCESS BY ACCESS GRANT ANY ROLE BY ACCESS BY ACCESS GRANT ANY OBJECT PRIVILEGE BY ACCESS BY ACCESS GRANT ANY PRIVILEGE BY ACCESS BY ACCESS ALTER ANY PROCEDURE BY ACCESS BY ACCESS DROP ANY PROCEDURE BY ACCESS BY ACCESS CREATE ANY PROCEDURE BY ACCESS BY ACCESS DROP ANY TABLE BY ACCESS BY ACCESS CREATE ANY TABLE BY ACCESS BY ACCESS ALTER ANY TABLE BY ACCESS BY ACCESS 23 rows selected. 如果user_name,proxy_name都为空,那么表示任何人使用到了上面提到的权限,都会进行一次审计。这是数据库默认给我们的参数。 success表示成功使用了该权限, failure表示没有成功使用该权限而进行审计记录。
3.1测试权限审计
由于我的audit_sys_operations 参数为false,那么无法使用sys用户测试审计效果,这里使用test用户做测试, 1.测试test用户的grant 操作能否被记录到dba_audit_trail 中。 2.测试scott用户的select any table 操作能否被记录到dba_audit_trail中。 创建test用户: SQL> show user; USER is "SYS" SQL> grant dba to test identified by test; Grant succeeded. SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; Session altered. SQL> set lines 200 SQL> col OS_USERNAME for a10 SQL> col USERNAME for a11 SQL> col USERHOST for a10 SQL> col TERMINAL for a10 SQL> col TIMESTAMP for a20 SQL> col obj_name for a10 SQL> col OWNER for a10 SQL> col ACTION_NAME for a11 SQL> col TRANSACTIONID for a16 SQL> col sql_text for a30 SQL> SELECT USERNAME, 2 USERHOST, 3 TIMESTAMP, 4 OWNER, 5 OBJ_NAME, 6 ACTION_NAME, 7 SQL_TEXT 8 FROM DBA_AUDIT_TRAIL 9 WHERE username='TEST' 10 ORDER BY TIMESTAMP; username 登录的用户 userhost 登录的主机 timestamp 时间戳 owner 操作表的所有者 obj_name 操作的表 action_name 做了什么动作 sql_text 具体的SQL -- 这里显示了登录时间。 USERNAME USERHOST TIMESTAMP OWNER OBJ_NAME ACTION_NAME SQL_TEXT ----------- ---------- -------------------- ---------- ---------- ----------- ------------------------------ TEST oracle 2015-10-28 23:38:02 LOGON SQL> save audtest.sql Created file audtest.sql ********************************************************* --test用户使用了GRANT ANY PRIVILEGE权限 SQL> grant alter user to scott ; Grant succeeded. SQL> @audtest.sql USERNAME USERHOST TIMESTAMP OWNER OBJ_NAME ACTION_NAME SQL_TEXT ----------- ---------- -------------------- ---------- ---------- ----------- ------------------------------ TEST oracle 2015-10-28 23:38:02 LOGON TEST oracle 2015-10-28 23:40:40 SYSTEM GRAN grant alter user to scott SQL> revoke alter user from scott; Revoke succeeded. SQL> @audtest.sql USERNAME USERHOST TIMESTAMP OWNER OBJ_NAME ACTION_NAME SQL_TEXT ----------- ---------- -------------------- ---------- ---------- ----------- ------------------------------ TEST oracle 2015-10-28 23:38:02 LOGON TEST oracle 2015-10-28 23:40:40 SYSTEM GRAN grant alter user to scott T TEST oracle 2015-10-28 23:42:04 SYSTEM REVO revoke alter user from scott KE T ********************************************************* --审计SCOTT的select any table 权限 SQL> conn test/test Connected. SQL> grant select any table to scott; Grant succeeded. --开启全库的select any table 审计 SQL> audit select any table by access; Audit succeeded. SQL> conn scott/tiger Connected. SQL> select count(*) from hr.employees; COUNT(*) ---------- 107 由于没有给他select any dictionary 权限,所以scott不能读取dba_objects。 SQL> select count(*) from dba_objects; select count(*) from dba_objects * ERROR at line 1: ORA-00942: table or view does not exist 使用test用户查看审计视图 COL OS_USERNAME FOR A10 COL USERNAME FOR A11 COL USERHOST FOR A10 COL TERMINAL FOR A10 COL TIMESTAMP FOR A20 COL OBJ_NAME FOR A10 COL OWNER FOR A10 COL ACTION_NAME FOR A15 COL TRANSACTIONID FOR A16 COL SQL_TEXT FOR A30 SELECT USERNAME, USERHOST, TIMESTAMP, OWNER, OBJ_NAME, ACTION_NAME, SQL_TEXT FROM DBA_AUDIT_TRAIL WHERE USERNAME='SCOTT' AND OWNER='HR' ORDER BY TIMESTAMP; USERNAME USERHOST TIMESTAMP OWNER OBJ_NAME ACTION_NAME SQL_TEXT ----------- ---------- -------------------- ---------- ---------- --------------- ------------------------------ SCOTT oracle 2015-10-29 00:14:37 HR EMPLOYEES SELECT select count(*) from hr.employ ees 这里要说明一下select any table 权限,如果使用scott查询scott.emp 那么不会使用到select any table ,如果去查询hr用户的employees表,就使用到了select any table 权限。做测试实验的时候要注意这一点。
3.2关掉默认数据库审计权限
关掉默认审计 noaudit DROP PROFILE; noaudit ALTER PROFILE ; noaudit ALTER SYSTEM; noaudit ALTER DATABASE; noaudit DROP USER ; noaudit CREATE SESSION; noaudit CREATE USER ; noaudit ALTER USER; noaudit EXEMPT ACCESS POLICY; noaudit CREATE PUBLIC DATABASE LINK ; noaudit CREATE ANY LIBRARY; noaudit CREATE ANY JOB; noaudit CREATE EXTERNAL JOB ; noaudit AUDIT SYSTEM; noaudit GRANT ANY ROLE; noaudit GRANT ANY OBJECT PRIVILEGE; noaudit GRANT ANY PRIVILEGE ; noaudit ALTER ANY PROCEDURE ; noaudit DROP ANY PROCEDURE; noaudit CREATE ANY PROCEDURE; noaudit DROP ANY TABLE; noaudit CREATE ANY TABLE; noaudit ALTER ANY TABLE; SQL> select * from dba_priv_audit_opts; no rows selected 开启默认审计 audit DROP PROFILE by access; audit ALTER PROFILE by access; audit ALTER SYSTEM by access; audit ALTER DATABASE by access; audit DROP USER by access; audit CREATE SESSION by access; audit CREATE USER by access; audit ALTER USER by access; audit EXEMPT ACCESS POLICY by access; audit CREATE PUBLIC DATABASE LINK by access; audit CREATE ANY LIBRARY by access; audit CREATE ANY JOB by access; audit CREATE EXTERNAL JOB by access; audit AUDIT SYSTEM by access; audit GRANT ANY ROLE by access; audit GRANT ANY OBJECT PRIVILEGE by access; audit GRANT ANY PRIVILEGE by access; audit ALTER ANY PROCEDURE by access; audit DROP ANY PROCEDURE by access; audit CREATE ANY PROCEDURE by access; audit DROP ANY TABLE by access; audit CREATE ANY TABLE by access; audit ALTER ANY TABLE;
4、设置对象审计
这样每次有用户对表进行操作,那么都会有相应的记录被添加到aud$中,而Oracle为了方便读取数据,创建了视图。
dba_audit_trail
测试对象审计
测试对象审计之前,先把权限审计都关掉,然后清空aud$,方便查看记录。 SQL> show user USER is "SYS" SQL> truncate table aud$; Table truncated. --没有对象审计 SQL> SELECT * FROM dba_obj_audit_opts; no rows selected --没有权限审计 SQL> select * from dba_priv_audit_opts; no rows selected --没有审计内容(truncate table aud$) SQL> select * from dba_audit_trail; no rows selected --SCOTT用户自己也可以设置对象审计权限。因为EMP表的owner就是SCOTT SQL> show user; USER is "SCOTT" SQL> audit select on scott.emp by access whenever successful; Audit succeeded. SQL> audit alter on scott.emp by session whenever successful; Audit succeeded. set lines 200 col object_name for a5 col object_type for a6 select * from user_obj_audit_opts; OBJEC OBJECT ALT AUD COM DEL GRA IND INS LOC REN SEL UPD REF EXE CRE REA WRI FBK ----- ------ ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- --- ----- ----- ----- ----- ----- EMP TABLE S/- -/- -/- -/- -/- -/- -/- -/- -/- A/- -/- -/- -/- -/- -/- -/- -/- 前面列中 A表示access,每次被审计的操作都会记录,比如开启了scott.emp的select审计,那么任何人select这张表都会触发一次审计,并且记录在aud$中。 S表示session,每个会话被审计的操作都记录一次。 SCOTT用户自己做一次查询, SQL> select count(*) from emp; COUNT(*) ---------- 14 TEST用户做一次查询 SQL> show user USER is "TEST" SQL> select count(*) from scott.emp; COUNT(*) ---------- 14
关于这个视图中的每个列解释我摘录官方文档中的一段列表
ALT Auditing ALTER WHENEVER SUCCESSFUL / UNSUCCESSFUL
AUD Auditing AUDIT WHENEVER SUCCESSFUL / UNSUCCESSFUL
COM Auditing COMMENT WHENEVER SUCCESSFUL / UNSUCCESSFUL
DEL Auditing DELETE WHENEVER SUCCESSFUL / UNSUCCESSFUL
GRA Auditing GRANT WHENEVER SUCCESSFUL / UNSUCCESSFUL
IND Auditing INDEX WHENEVER SUCCESSFUL / UNSUCCESSFUL
INS Auditing INSERT WHENEVER SUCCESSFUL / UNSUCCESSFUL
LOC Auditing LOCK WHENEVER SUCCESSFUL / UNSUCCESSFUL
REN Auditing RENAME WHENEVER SUCCESSFUL / UNSUCCESSFUL
SEL Auditing SELECT WHENEVER SUCCESSFUL / UNSUCCESSFUL
UPD Auditing UPDATE WHENEVER SUCCESSFUL / UNSUCCESSFUL
REF 淘汰列,无意义!
EXE Auditing EXECUTE WHENEVER SUCCESSFUL / UNSUCCESSFUL
CRE Auditing CREATE WHENEVER SUCCESSFUL / UNSUCCESSFUL
REA Auditing READ WHENEVER SUCCESSFUL / UNSUCCESSFUL
WRI Auditing WRITE WHENEVER SUCCESSFUL / UNSUCCESSFUL
FBK Auditing FLASHBACK WHENEVER SUCCESSFUL / UNSUCCESSFUL
其他:
1、清空aud$
这张系统表是可以使用TRUNCATE命令截断的。把它删掉之后那么视图中的记录也就相应消失了。
SQL> truncate table aud$; SQL> SELECT * FROM DBA_FGA_AUDIT_TRAIL; no rows selected SQL> select segment_name,bytes,tablespace_name from dba_segments where segment_name='AUD$'; SEGMENT_NA BYTES TABLESPACE_NAME ---------- ---------- ------------------------------ AUD$ 65536 SYSTEM SQL> alter table aud$ move tablespace users; Table altered. SQL> select segment_name,bytes,tablespace_name from dba_segments where segment_name='AUD$'; SEGMENT_NA BYTES TABLESPACE_NAME ---------- ---------- ------------------------------ AUD$ 65536 USERS
2、取消某张表的审计
SQL> noaudit all on scott.emp;
3、关闭审计
SQL> alter system set audit_trail=none;
4、查看当前库中开了哪些审计
对象审计
SQL> select * from dba_obj_audit_opts;
权限审计
SQL> select * from dba_priv_audit_opts;
下面列出一些简单的需求,以及如何打开审计的语句
1、对Scott用户权限进行审计,审计内容有update和delete
audit update any table by scott by access;
audit delete any table by scott by access;
SQL> col user_name for a10 SQL> col proxy_name for a10 SQL> col privilege for a25 SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- SCOTT DELETE ANY TABLE BY ACCESS BY ACCESS SCOTT UPDATE ANY TABLE BY ACCESS BY ACCESS
关闭当前审计
noaudit update any table by scott;
noaudit delete any table by scott;
2、对Scott用户权限进行审计,审计内容有update和delete,仅仅当用户执行命令成功才进行审计
audit update any table by scott by access whenever successful;
audit delete any table by scott by access whenever successful;
SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- SCOTT DELETE ANY TABLE BY ACCESS NOT SET SCOTT UPDATE ANY TABLE BY ACCESS NOT SET
关闭当前审计
noaudit update any table by scott whenever successful;
noaudit delete any table by scott whenever successful;
3、对数据库中非sys的用户进行登录审计
audit session;
SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- CREATE SESSION BY ACCESS BY ACCESS
关闭当前审计
noaudit session;
4、对Scott用户进行登录审计,不管成功与否都进行审计
audit session by scott;
SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- SCOTT CREATE SESSION BY ACCESS BY ACCESS
关闭当前审计
noaudit session by scott;
5、对Scott用户进行登录审计,仅仅当登录成功时才审计
audit session by scott whenever successful;
SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- SCOTT CREATE SESSION BY ACCESS NOT SET
关闭当前审计
noaudit session by scott whenever successful;
6、对Scott用户进行登录审计,仅仅当登录失败时才审计
audit session by scott whenever not successful;
SQL> select * from dba_priv_audit_opts ; USER_NAME PROXY_NAME PRIVILEGE SUCCESS FAILURE ---------- ---------- ------------------------- ---------- ---------- SCOTT CREATE SESSION NOT SET BY ACCESS
关闭当前审计
noaudit session by scott whenever not successful;
取消所有语句审计
NOAUDIT ALL;
取消所有权限审计
NOAUDIT ALL PRIVILEGES;
取消所有对象审计
NOAUDIT ALL ON DEFAULT;