加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 18:49:19 所属栏目:百科 来源:网络整理
导读:如果没有使用绑定变量,就会有“SQL注入”的危险,下面通过一个例子来说明,这个例子摘自THomas的《Oracle编程艺术深入理解数据库体系结构》。 首先创建过程inj: create or replace procedure inj(p_date in date) as l_username all_users.username%TYPE;

如果没有使用绑定变量,就会有“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注入的危险。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读