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

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

首先创建过程inj:

create or replace procedure inj(p_date in date)
as
l_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)
as
l_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注入的危险。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值