使用DBV的命令規則和規則集強化資料庫安全

hooca發表於2015-01-09
幾個場景示例:
1. 指定時間維護資料庫
2. 指定不能直接使用DML命令,而必須呼叫指定儲存過程
3. 指定必須使用密碼認證方式(而不是OS認證)連線資料庫
4. 指定必須從指定客戶端IP連線資料庫
5. 將做指定動作的嘗試記錄下來
6. 結合業務資料制定訪問規則

示例1:只能在週五17-23點做DROP TABLE命令

點選(此處)摺疊或開啟

  1. --建立規則
  2. BEGIN
     dbms_macadm.create_rule(
       rule_name => 'Is Maintenance Timeframe'
     , rule_expr => 'TRIM(TO_CHAR(SYSDATE,''DAY'')) = ''FRIDAY'' AND
    TO_CHAR(SYSDATE,''HH24'') BETWEEN 17 AND 23'
     );
     END;
    /
  3. --建立規則集
  4. BEGIN
        dbms_macadm.create_rule_set(
            rule_set_name => 'Is System Maintenance Allowed',
            description =>
    'Checks to determine if the system maintenance is allowed',
            enabled =>dbms_macutl.g_yes,
            eval_options =>dbms_macutl.g_ruleset_eval_all,
            audit_options =>dbms_macutl.g_ruleset_audit_fail,
            fail_options =>dbms_macutl.g_ruleset_fail_show,
            fail_message =>NULL,
            fail_code =>NULL,
            handler_options =>dbms_macutl.g_ruleset_handler_off,
            handler =>NULL);
    END;
    /
  5. --將規則加入規則集,可以將多個規則加入規則集
  6. BEGIN
       dbms_macadm.add_rule_to_rule_set (
         rule_set_name => 'Is System Maintenance Allowed'
       , rule_name     => 'Is Maintenance Timeframe'
       );
    END;
    /
  7. --建立命令規則,其中指定了規則集
  8. BEGIN
      dbms_macadm.create_command_rule (
        command       =>  'DROP TABLE'
        ,rule_set_name => 'Is System Maintenance Allowed'
        ,object_owner  => 'SOE'
        ,object_name   => '%'
        ,enabled       => 'Y'
    );
    END;
    /
驗證:

點選(此處)摺疊或開啟

  1. drop table soe.customers3;

drop table soe.customers3;
drop table soe.customers3
*
ERROR at line 1:
ORA-47400: Command Rule violation for DROP TABLE on SOE.CUSTOMERS3


示例2. 只能呼叫儲存過程來更新表資料
建立儲存過程

點選(此處)摺疊或開啟

  1. CREATE OR REPLACE PACKAGE soe.cards_transaction is
  2.     PROCEDURE disable_card(card_no in PLS_INTEGER);
  3. END cards_transaction; --沒有schema名soe
  4. /

  5. CREATE OR REPLACE PACKAGE BODY soe.cards_transaction AS
  6.     PROCEDURE disable_card(card_no in PLS_INTEGER) IS
  7.     BEGIN
  8.         -- show the output of the DBMS_UTILITY function
  9.         DBMS_OUTPUT.PUT_LINE ( DBMS_UTILITY.FORMAT_CALL_STACK );
  10.         -- perform the SQL UPDATE
  11.         UPDATE soe.card_details SET is_valid = 'N'
  12.             WHERE card_number = card_no;
  13.     END;
  14. END cards_transaction; --沒有schema名soe
  15. /
驗證儲存過程

點選(此處)摺疊或開啟

  1. set serverout on
  2. begin
  3.   cards_transaction.disable_card(
  4.     card_no => 1374047923
  5.   );
  6. end;
  7. /
建立規則,驗證是否使用了儲存過程

點選(此處)摺疊或開啟

  1. --建立規則
  2. BEGIN
             dbms_macadm.create_rule(
             rule_name => 'Called From Cards Transaction Package'
             , rule_expr => 'INSTR(UPPER(DBMS_UTILITY.FORMAT_CALL_STACK),
    ''PACKAGE BODY SOE.CARDS_TRANSACTION'') > 0'
        );
    END;
    /
  3. --建立規則集
    BEGIN
            dbms_macadm.create_rule_set(
                rule_set_name   => 'Using Cards Application',
                description     => 'Checks to verify commands
    are executed from trusted cards packages',
                enabled         => dbms_macutl.g_yes,
                eval_options    => dbms_macutl.g_ruleset_eval_all,
                audit_options   => dbms_macutl.g_ruleset_audit_fail,
                fail_options    => dbms_macutl.g_ruleset_fail_show,
                fail_message    => NULL,
                fail_code       => NULL,
                handler_options => dbms_macutl.g_ruleset_handler_off,
                handler         => NULL
            );
    END;
    /

  4. --將規則加入
  5. BEGIN
       dbms_macadm.add_rule_to_rule_set (
         rule_set_name => 'Using Cards Application'
       , rule_name     => 'Called From Cards Transaction Package'
       );
    END;
    /
  6. --建立命令規則
  7. BEGIN
          dbms_macadm.create_command_rule (
            command        => 'UPDATE'
            ,rule_set_name => 'Using Cards Application'
            ,object_owner  => 'SOE'
            ,object_name   => 'CARD_DETAILS'
            ,enabled       => 'Y'
        );
    END;
    /
驗證
直接用update命令:

點選(此處)摺疊或開啟

  1. update card_details set is_valid='N' where card_number='1374047923';

       *
ERROR at line 1:
ORA-01031: insufficient privileges
用儲存過程

點選(此處)摺疊或開啟

  1. begin
  2.   cards_transaction.disable_card(
  3.     card_no => 1374047923
  4.   );
  5. end;
  6. /

PL/SQL procedure successfully completed.


示例3. 指定必須使用密碼認證方式(而不是OS認證)連線資料庫


點選(此處)摺疊或開啟

  1. --使用密碼認證連線資料庫,其他方式還有OS, SSL
  2. BEGIN
         dbms_macadm.create_rule(
             rule_name => 'Is Secure Authentication Method'
           , rule_expr => 'SYS_CONTEXT(''USERENV'',
               ''AUTHENTICATION_METHOD'') IN (''PASSWORD'')'
         );
    END;
    /

  3. --客戶端IP,如果是OS登入,IP為IS NULL
  4. BEGIN
         dbms_macadm.create_rule(
             rule_name => 'Is Console Client'
           , rule_expr =>
    'SYS_CONTEXT(''USERENV'', ''IP_ADDRESS'') = ''192.168.226.31'''
         );
    END;
    /

  5. --使用預設建立的規則集Allow Sessions,因此只要update
  6. BEGIN
        dbms_macadm.update_rule_set(
            rule_set_name   => 'Allow Sessions',
            description     => 'Rule set that controls the ability to create a
    session in the database.',
            enabled         => dbms_macutl.g_yes,
            eval_options    => dbms_macutl.g_ruleset_eval_all,
            audit_options   => dbms_macutl.g_ruleset_audit_fail,
            fail_options    => dbms_macutl.g_ruleset_fail_show,
            fail_message    => NULL,
            fail_code       => NULL,
            handler_options => dbms_macutl.g_ruleset_handler_off,
            handler         => NULL);
    END;
    /
  7. --將規則加入規則集
  8. BEGIN
       dbms_macadm.add_rule_to_rule_set (
         rule_set_name => 'Allow Sessions'
       , rule_name     => 'Is Secure Authentication Method'
       );
    END;
    /
    BEGIN
       dbms_macadm.add_rule_to_rule_set (
         rule_set_name => 'Allow Sessions'
       , rule_name     => 'Is Console Client'
       );
    END;
    /
  9. --加入CONNECT命令規則
  10. BEGIN
      dbms_macadm.create_command_rule (
        command       =>  'CONNECT'
        ,rule_set_name => 'Allow Sessions'
        ,object_owner  => '%'
        ,object_name   => '%'
        ,enabled       => 'Y'
    );
    END;
    /


驗證:使用oracle的系統使用者以OS方式連線資料庫,此時應該無法連線上

點選(此處)摺疊或開啟

  1. [oracle@vault ~]$ sqlplus / as sysdba

  2. SQL*Plus: Release 11.2.0.4.0 Production on Mon Jan 12 15:47:37 2015

  3. Copyright (c) 1982, 2013, Oracle. All rights reserved.

  4. Connected.
  5. SQL> show user
  6. USER is "SYS"
  7. SQL> desc v$instance;
  8. ERROR:
  9. ORA-01012: not logged on


  10. SQL> connect / as sysdba
  11. Connected to an idle instance.
  12. SQL> exit
  13. Disconnected
可以在規則集的選項
eval_options    => dbms_macutl.g_ruleset_eval_all,
指定多個規則要ALL TRUE或ANY TRUE

示例5. 使用自定義過程處理程式

以下摘抄原書:


點選(此處)摺疊或開啟

  1. mary@aos> -- First create a table to hold the alerts
    mary@aos> create table sh.alerts ( msg varchar2(4000)
    , msgdate date default sysdate);
    Table created.
    mary@aos> -- next create a package to process the alerts
    mary@aos> CREATE OR REPLACE package sh.sales_alerts as
    PROCEDURE sales_update_alert(ruleset_name IN VARCHAR2,
    ruleset_result IN VARCHAR2);
    end;
    /
    Package created.
    mary@aos> CREATE OR REPLACE PACKAGE BODY sh.sales_alerts AS
              PROCEDURE sales_update_alert(ruleset_name IN VARCHAR2,
    ruleset_result IN VARCHAR2) is
            PRAGMA AUTONOMOUS_TRANSACTION;
        BEGIN
                INSERT into sh.alerts (msg )
    VALUES ('Alert for Rule Set:'
    || ruleset_name || ', result is ' || ruleset_result);
                COMMOT;
        END;
    END;
    /
    Package created.
    mary@aos> -- GRANT EXECUTE on the handler package to DVSYS
    mary@aos> GRANT EXECUTE ON sh.sales_alerts TO dvsys;
    Grant succeeded.
    mary@aos> -- Update the rule set to use the handler package
    mary@aos> -- on rule set failure (failed access attempt)
    dbvowner@aos> BEGIN
        dbms_macadm.update_rule_set(
            rule_set_name =>'Using Financials Application',
            description =>'Checks to verify commands came are
    executed from trusted financials packages',
            enabled =>dbms_macutl.g_yes,
            eval_options =>dbms_macutl.g_ruleset_eval_all,
            audit_options =>dbms_macutl.g_ruleset_audit_fail,
            fail_options =>dbms_macutl.g_ruleset_fail_show,
            fail_message =>NULL,
            fail_code =>NULL,
            handler_options =>dbms_macutl.g_ruleset_handler_fail,
            handler =>'sh.sales_alerts.sales_update_alert'
    );
    END;
    /
    PL/SQL procedure successfully completed.
    mary@aos> -- Attempt to update the table outside the policy
    mary@aos> UPDATE sh.sales
    SET amount_sold = 200
    WHERE cust_id = 305;
    UPDATE sh.sales
              *
    ERROR at line 1:
    ORA-01031: insufficient privileges
    mary@aos> -- View our alerts
    mary@aos> SELECT * from sh.alerts;
    MSG
    ----------------------------------------------------------------
    MSGDATE
    ---------
    Alert for Rule Set:Using Financials Application, result is FALSE
    12-JUL-08


示例6. 使用驗證函式確認是否允許執行命令
摘抄原書:

點選(此處)摺疊或開啟

  1. --以下函式表示只有在週六、週日的8-16點才能驗證通過。實際業務環境中,可以使用業務資料、函式來自定義
  2. mary@aos> CREATE OR REPLACE FUNCTION sh.can_perform_sales_summary RETURN NUMBER AS
     l_day  VARCHAR2(10) := TRIM(TO_CHAR(SYSDATE,'DAY'));
     l_hour VARCHAR2(2)  := TO_CHAR(SYSDATE,'HH24');
    BEGIN
     -- allow sales summary logic to run on weekends or week nights
     -- weekend
     IF l_day IN ( 'SATURDAY', 'SUNDAY' ) THEN
        RETURN 1;
     -- weekday - business hours
     ELSIF l_hour BETWEEN '08' AND '16' THEN
        RETURN 0;
     -- week night
     ELSE
        RETURN 1;
     END IF;
    END;
    /
    Function created.
    mary@aos> GRANT EXECUTE ON sh.can_perform_sales_summary TO dvsys;
    Grant succeeded.
  3. --
  4. dbvowner@aos> -- Create the DBV Rule
    BEGIN
         dbms_macadm.create_rule(
            rule_name => 'Is Sales Summary Allowed'
           , rule_expr => 'sh.can_perform_sales_summary = 1'
         );
    END;
    /
    PL/SQL procedure successfully completed.
    dbvowner@aos> -- Create the DBV Rule Set
    dbvowner@aos> BEGIN
        dbms_macadm.create_rule_set(
            rule_set_name =>'Can Execute Financials Summary',
            description =>
    'Checks to see if summary job for financials can be run.',
            enabled =>dbms_macutl.g_yes,
            eval_options =>dbms_macutl.g_ruleset_eval_all,
            audit_options =>dbms_macutl.g_ruleset_audit_fail,
            fail_options =>dbms_macutl.g_ruleset_fail_show,
            fail_message =>NULL,
            fail_code =>NULL,
            handler_options =>dbms_macutl.g_ruleset_handler_off,
            handler =>NULL);
    END;
    /
    PL/SQL procedure successfully completed.
    dbvowner@aos> -- Associate the DBV Rule to the DBV Rule Set
    BEGIN
       dbms_macadm.add_rule_to_rule_set (
         rule_set_name => 'Can Execute Financials Summary'
       , rule_name     => 'Is Sales Summary Allowed'
       );
    END;
    /
    PL/SQL procedure successfully completed.
    dbvowner@aos> BEGIN
      dbms_macadm.create_command_rule (
        command       =>  'EXECUTE'
        ,rule_set_name => 'Can Execute Financials Summary'
        ,object_owner  => 'SH'
        ,object_name   => 'SALES_SUMMARY.RUN_SUMMARY'
        ,enabled       => 'Y'
    );
    END;
    /
    PL/SQL procedure successfully completed.

查詢規則集

點選(此處)摺疊或開啟

  1. dbvowner@aos>SELECT name,rule_expr
  2. from dvsys.dv$rule
  3. WHERE id# = 5045;

NAME                      RULE_EXPR
------------------------- --------------------------------
Is Sales Summary Allowed  sh.can_perform_sales_summary = 1
1 row selected.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22621861/viewspace-1396261/,如需轉載,請註明出處,否則將追究法律責任。

相關文章