首页 > 学院 > 开发设计 > 正文

Oracle数据库SQL语句绑定变量二----安全问题

2019-11-08 20:50:37
字体:
来源:转载
供稿:网友

如果没有使用绑定变量,就会有“SQL注入”的危险,下面通过一个例子来说明,这个例子摘自THomas的《Oracle编程艺术深入理解数据库体系结构》。

首先创建过程inj:

create or replace PRocedure inj(p_date in date)asl_username all_users.username%TYPE;c sys_refcursor;l_query varchar2(4000);begin  l_query := '     select username from all_users where created='''||p_date||''''; dbms_output.put_line(l_query); open c for l_query ; for i in 1 ..5 loop  fetch c into l_username;  exit when c%NOTFOUND;  dbms_output.put_line(l_username||'.....');   end loop;   close c;end;

创建绑定变量的过程inj1,我做了一点修改:

create or replace procedure inj1(p_date in date)asl_username all_users.username%TYPE;c sys_refcursor;l_query varchar2(4000);begin  l_query := '     select username from all_users where created= :date_'; dbms_output.put_line(l_query); open c for l_query using p_date; for i in 1 ..5 loop  fetch c into l_username;  exit when c%NOTFOUND;  dbms_output.put_line(l_username||'.....');   end loop;   close c;end;

创建一个表user_pw,默认为这个表是非常重要的。

create table user_pw(uname varchar2(30) primary key,

                                       pw varchar2(30));

insert into user_pw values(''est','test');

commit;

下面分别执行两个过程,通过结果来区分:

通过两个过程执行结果来看,inj过程打印出了参数sysdtae的值,而绑定变量的过程inj1却没有,那么接下来看SQL注入:

修改session级参数nls_date_format

alter session set nls_date_format='"''union select tname from tab--"';

此时执行inj的话,结果是:

看结果却返回了不应该被暴露的表的信息,这就是SQL注入的危险。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表