第十章 sqlplus的安全性

to_be_Dba發表於2013-01-29


第十章  sqlplus的安全性

一.product_user_profile表
簡稱PUP表,在system使用者下,用於生產級的安全保護,可以管理使用者級的安全命令如grant、revoke以及使用者角色。
可以通過該表禁用某些命令,比如grant、revoke、set role,當普通使用者訪問時無法執行,system、sys以及sysdba或sysoper許可權的使用者登入時不會檢查該表,也就不會受到許可權上的影響。
PUP表只對本地使用者有作用,使用dblink時不受影響。

建立PUP表
system使用者下執行指令碼PUPBLD:


[oracle@localhost admin]$ pwd
/u01/oracle/sqlplus/admin
[oracle@localhost admin]$ ls
glogin.sql  help  libsqlplus.def  plustrce.sql  pupbld.sql
[oracle@localhost admin]$ exit
exit

SQL> show user 
USER is "SYSTEM"

SQL> @/u01/oracle/sqlplus/admin/pupbld.sql

Synonym dropped.

CREATE TABLE SQLPLUS_PRODUCT_PROFILE AS
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


DROP TABLE PRODUCT_USER_PROFILE
           *
ERROR at line 1:
ORA-00942: table or view does not exist


ALTER TABLE SQLPLUS_PRODUCT_PROFILE ADD (LONG_VALUE LONG)
                                         *
ERROR at line 1:
ORA-01430: column being added already exists in table


CREATE TABLE SQLPLUS_PRODUCT_PROFILE
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


DROP TABLE PRODUCT_PROFILE
           *
ERROR at line 1:
ORA-00942: table or view does not exist

 

View dropped.


View created.


Grant succeeded.


Synonym dropped.


Synonym created.

DROP SYNONYM PRODUCT_USER_PROFILE
             *
ERROR at line 1:
ORA-01434: private synonym to be dropped does not exist

 

Synonym created.


Synonym dropped.


Synonym created.


SQL> desc product_user_profile
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 PRODUCT                                   NOT NULL VARCHAR2(30)
 USERID                                             VARCHAR2(30)
 ATTRIBUTE                                          VARCHAR2(240)
 SCOPE                                              VARCHAR2(240)
 NUMERIC_VALUE                                      NUMBER(15,2)
 CHAR_VALUE                                         VARCHAR2(240)
 DATE_VALUE                                         DATE
 LONG_VALUE                                         LONG

以下是各個欄位的填充規則:
 PRODUCT        要限制的產品名稱,“SQL*Plus”,不能是萬用字元或空
 USERID         要禁用部分命令的使用者名稱,可以使用萬用字元,%表示所有
 ATTRIBUTE      要限制的命令的大寫形式,不能用萬用字元
 SCOPE          推薦置空
 NUMERIC_VALUE  推薦置空
 CHAR_VALUE     “DISABLED”表示禁用,若禁用角色,需要填寫角色名
 DATE_VALUE     推薦置空
 LONG_VALUE     推薦置空

system使用者擁有所有對該表的操作,其他使用者只能select。

可禁用的sqlplus命令:
ACCEPT DEFINE PASSWORD SHUTDOWN
APPEND DEL PAUSE SPOOL
ARCHIVE LOG DESCRIBE PRINT START(@, @@)
ATTRIBUTE DISCONNECT PROMPT STARTUP
BREAK EDIT RECOVER STORE
BTITLE EXECUTE REMARK TIMING
CHANGE EXIT/QUIT REPFOOTER TTITLE
CLEAR GET REPHEADER UNDEFINE
COLUMN HELP (?) RUN VARIABLE
COMPUTE HOST SAVE WHENEVEROSERROR
CONNECT INPUT SET WHENEVER
SQLERROR
COPY LIST (;) SHOW

sql命令:
ALTER DELETE MERGE SET CONSTRAINTS
ANALYZE DISASSOCIATE NOAUDIT SET ROLE
ASSOCIATE DROP PURGE SET TRANSACTION
AUDIT EXPLAIN RENAME TRUNCATE
CALL FLASHBACK REVOKE UPDATE
COMMENT GRANT ROLLBACK VALIDATE
COMMIT INSERT SAVEPOINT
CREATE LOCK SELECT

pl/sql命令:
BEGIN DECLARE

例如:
禁止使用者terry使用set、grant、select命令
INSERT INTO PRODUCT_USER_PROFILE
VALUES ('SQL*Plus', 'TERRY', 'GRANT', NULL, NULL, 'DISABLED', NULL, NULL);

INSERT INTO PRODUCT_USER_PROFILE
VALUES ('SQL*Plus', 'TERRY', 'SET', NULL, NULL, 'DISABLED', NULL, NULL);

INSERT INTO PRODUCT_USER_PROFILE
VALUES ('SQL*Plus', 'TERRY', 'SELECT', NULL, NULL, 'DISABLED', NULL, NULL);

commit;

校驗一下:
SQL> conn terry/terry
Connected.
SQL> set autot on
SP2-0544: Command "set" disabled in Product User Profile
SQL> select * from user_Tables;
SP2-0544: Command "select" disabled in Product User Profile
SQL> desc emp;      
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                                              NUMBER(4)
 ENAME                                              VARCHAR2(10)
 JOB                                                VARCHAR2(9)
 MGR                                                NUMBER(4)
 HIREDATE                                           DATE
 SAL                                                NUMBER(7,2)
 COMM                                               NUMBER(7,2)
 DEPTNO                                             NUMBER(2)

SQL> grant select on emp to scott;
SP2-0544: Command "grant" disabled in Product User Profile

根據提示可以看到,限制成功。

需要注意,此時通過pl/sql developer等其他工具是可以進行以上操作的,通過system使用者下的product_user_profile只是對sqlplus環境的限制,
因為在登入前會檢查是否有這些約束,而在其他環境中是沒有校驗的。

二、建立和控制角色
使用create命令可以建立角色,密碼可以不設定。
create role role_name identified by xxx;
drop role xxx;
grant select on emp to role_name;

set role tian identified by tian;在以前沒有密碼的情況下可以設定密碼,如果已經有密碼了,不能使用
set role none;將所有角色從當前會話收回
set role all except role_name;將當前使用者的所有角色的許可權授予當前會話


如果在sqlplus中禁止HR使用者對role1、role2兩個角色的使用,嘗試插入:
INSERT INTO PRODUCT_USER_PROFILE
VALUES ('SQL*Plus', 'HR', 'ROLES', NULL, NULL, 'ROLE%', NULL, NULL);
然後登陸:
SQL> conn terry/terry
ERROR:
ORA-00911: invalid character


SP2-0557: Error in disabling roles in product user profile.
Connected.
SQL>
這是因為角色中萬用字元是無效的。必須指定特定的角色。

三、Disabling Commands with SQLPLUS -RESTRICT
在用sqlplus命令連線到SQLPlus環境時,指定-RESTRICT n將限制與作業系統之間的互動。
主要影響的命令及對應級別為:
Command Level 1  Level 2  Level 3  備註
EDIT disabled  disabled  disabled edit是對檔案進行編輯操作
GET      disabled 
HOST  disabled  disabled  disabled 暫時切換到作業系統環境下,使用exit返回sqlplus
SAVE    disabled  disabled 儲存內容到作業系統檔案下
SPOOL    disabled  disabled 儲存內容到作業系統檔案下
START      disabled 將作業系統中的檔案內容作為命令執行 @、@@也受限
STORE    disabled  disabled 儲存內容到作業系統檔案下

例:

[oracle@localhost ~]$ sqlplus -restrict 1

SQL*Plus: Release 11.2.0.1.0 Production on Wed Dec 26 03:03:17 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

Enter user-name: scott
Enter password:

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> host
SP2-0738: Restricted command "host" not available

 

 

[oracle@localhost ~]$ sqlplus -r 2 scott/scott

SQL*Plus: Release 11.2.0.1.0 Production on Wed Dec 26 03:11:00 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> spool /u01/test.sql
SP2-0738: Restricted command "spool" not available

如果將這些限制放到登入檔案中,那麼就可以控制使用者的操作行為,實現安全性的目的。
該部分的命令相對來說都是對作業系統的保護,防止資料庫使用者在作業系統中亂放檔案等。

四、Program Argument Security
為了避免資料庫使用者和密碼在作業系統中被看到,以下幾點可以注意:

1.登入的時候總是在sqlplus環境中輸入使用者名稱、密碼
比如:
[oracle@localhost ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on Wed Dec 26 03:20:59 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL> conn scott/scott
Connected.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@localhost ~]$ sqlplus scott/scott

SQL*Plus: Release 11.2.0.1.0 Production on Wed Dec 26 03:21:27 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

通過history命令檢視一下剛才都執行了什麼命令:
[oracle@localhost ~]$ history 5
  205  sqlplus scott/scott -R 2
  206  sqlplus -r 2 scott/scott
  207  history
  208  sqlplus /nolog
  209  sqlplus scott/scott

可以看到,我們用sqlplus /nolog命令後再輸入密碼,是無法通過作業系統命令看到的,而直接sqlplus scott/scott則能看到,因此推薦第一種做法

2.在unix系統中通過sqlplus指令碼進行互動時,可以先設定變數MYUSERNAME、MYPASSWORD,再執行如下命令進行連線:
sqlplus /nolog <connect $MYUSERNAME/$MYPASSWORD
select ...
EOF

例項:
#!/bin/sh
user="system"
pass="manager"
sqlplus -S $user/$pass <SELECT * FROM tableName WHERE username=$var;
 exit;
EOF

3.

作業系統中包含了使用者名稱和密碼的檔案不應該被非授權的使用者得到。


五、iSQL*Plus的安全
主要包括:
瀏覽器和應用伺服器間的http協議連線
應用伺服器與資料庫之間的net connection

需要注意,iSQL*Plus環境中不能使用與作業系統互動的HOST、EDIT、SPOOL等命令

其他 略


以下摘抄自http://www.orafaq.com/wiki/SQL*Plus_FAQ

Here is a list of some of the most frequently used SQL*Plus commands:

ACCEPT - Get input from the user
DEFINE - Declare a variable (short: DEF)
DESCRIBE - Lists the attributes of tables and other objects (short: DESC)
EDIT - Places you in an editor so you can edit a SQL command (short: ED)
EXIT or QUIT - Disconnect from the database and terminate SQL*Plus
GET - Retrieves a SQL file and places it into the SQL buffer
HOST - Issue an operating system command (short: !)
LIST - Displays the last command executed/ command in the SQL buffer (short: L)
PROMPT - Display a text string on the screen. Eg prompt Hello World!!!
RUN - List and Run the command stored in the SQL buffer (short: /)
SAVE - Saves command in the SQL buffer to a file. Eg "save x" will create a script. file called x.sql
SET - Modify the SQL*Plus environment eg. SET PAGESIZE 23
SHOW - Show environment settings (short: SHO). Eg SHOW ALL, SHO PAGESIZE etc.
SPOOL - Send output to a file. Eg "spool x" will save STDOUT to a file called x.lst
START - Run a SQL script. file (short: @)

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

相關文章