如果沒有使用綁定變量,就會有“SQL注入”的危險(xiǎn),下面通過一個(gè)例子來說明,這個(gè)例子摘自THomas的《Oracle編程藝術(shù)深入理解數(shù)據(jù)庫體系結(jié)構(gòu)》。
首先創(chuàng)建過程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;
創(chuàng)建綁定變量的過程inj1,我做了一點(diǎn)修改:
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;
創(chuàng)建一個(gè)表user_pw,默認(rèn)為這個(gè)表是非常重要的。
create table user_pw(uname varchar2(30) primary key,
pw varchar2(30));
insert into user_pw values(''est','test');
commit;
下面分別執(zhí)行兩個(gè)過程,通過結(jié)果來區(qū)分:
通過兩個(gè)過程執(zhí)行結(jié)果來看,inj過程打印出了參數(shù)sysdtae的值,而綁定變量的過程inj1卻沒有,那么接下來看SQL注入:
修改session級參數(shù)nls_date_format
alter session set nls_date_format='"''union select tname from tab--"';
此時(shí)執(zhí)行inj的話,結(jié)果是:
看結(jié)果卻返回了不應(yīng)該被暴露的表的信息,這就是SQL注入的危險(xiǎn)。
新聞熱點(diǎn)
疑難解答