前言:實現的功能主要是,oracle登錄成功記錄登錄用戶ip地址,登錄失敗記錄登錄失敗ip地址
大家都知道在v$session 中記錄著客戶端的機器名稱,但是沒有IP , 如果記錄clinet ip 呢?先運行DBMS_SESSION 過程包注冊,然後執行存儲過程on_logon_trigger,這樣當客戶端登陸後,在v$session的client_info列會記錄其相應的IP信息。
利用 DBMS_SESSION 過程包,先執行
BEGIN DBMS_SESSION.set_identifier(SYS_CONTEXT('USERENV', 'IP_ADDRESS')); END;
再執行觸發器trigger
createorreplacetrigger on_logon_trigger after logon ondatabase begin dbms_application_info.set_client_info(sys_context( 'userenv', 'ip_address' ) ); end;
執行這些過程包觸發器需要dba權限。
執行查詢SQL:
select username,program,machine,client_info,sys_context('userenv','ip_address') as ipadd from v$session s where username isnotnull orderby username,program,machine;
信息如下所示:
寫一個觸發器,觸發器的信息記錄在alert日志裡面,通過查看alert日志來獲取登錄失敗的用戶信息。
觸發器如下:
CREATE OR REPLACE TRIGGERlogon_denied_to_alert AFTER servererror ON DATABASE DECLARE message VARCHAR2(168); ip VARCHAR2(15); v_os_user VARCHAR2(80); v_module VARCHAR2(50); v_action VARCHAR2(50); v_pid VARCHAR2(10); v_sid NUMBER; v_program VARCHAR2(48); BEGIN IF(ora_is_servererror(1017)) THEN -- get ip FOR remote connections : IF upper(sys_context('userenv', 'network_protocol')) = 'TCP' THEN ip := sys_context('userenv', 'ip_address'); END IF; SELECT sid INTO v_sid FROM sys.v_$mystat WHERE rownum < 2; SELECT p.spid, v.program INTO v_pid, v_program FROM v$process p, v$session v WHERE p.addr = v.paddr AND v.sid = v_sid; v_os_user := sys_context('userenv', 'os_user'); dbms_application_info.read_module(v_module, v_action); message := to_char(SYSDATE, 'YYYYMMDD HH24MISS') || ' logon denied from ' || nvl(ip,'localhost') || ' ' || v_pid || ' ' || v_os_user || 'with ' || v_program || ' – ' || v_module || ' ' || v_action; sys.dbms_system.ksdwrt(2, message); ENDIF; END; /
執行報錯:
Compilation errors for TRIGGERPOWERDESK.LOGON_DENIED_TO_ALERT Error: PLS-00201: identifier'SYS.DBMS_SYSTEM' must be declared Line: 35 Text: sys.dbms_system.ksdwrt(2, message); Error: PL/SQL: Statement ignored Line: 35 Text: sys.dbms_system.ksdwrt(2, message);
需要賦予權限
grant execute on sys.dbms_system topowerdesk;
之後執行成功了。
Pslql登錄,如下圖所示:
再去後台查看alert日志,就會看到失敗信息記錄:
Fri May 15 19:11:09 2015
20150515 191109 logon denied from192.168.120.169 20934 Administrator with plsqldev.exe ? plsqldev.exe
20150515 191109 logon denied from192.168.120.169 20934 Administrator with plsqldev.exe ? plsqldev.exe
Fri May 15 19:11:18 2015
20150515 191118 logon denied from192.168.120.169 20958 Administrator with plsqldev.exe ? plsqldev.exe
20150515 191118 logon denied from 192.168.120.16920958 Administrator with plsqldev.exe ? plsqldev.exe