转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> ORACLE >> 正文
SQL Injection and Oracle, Part Two         ★★★★

SQL Injection and Oracle, Part Two

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2887 更新时间:2009/4/22 22:03:02
ements in the SGA with the worst performance times. It can be easily modified to remove the execution time restraints and bring back all SQL in the SGA. A script such as this can be scheduled on a regular basis and then the SQL that is returned can be used to guess if any SQL injection has been attempted. I say “guess” because it is virtually impossible to know all legal pieces of SQL an application generates; therefore, the same applies to spotting illegal ones. A good first step would be to identify all statements with “union” included or or statements with ‘x’=’x’ type lines. There could be performance issues with extracting all of the SQL from the SGA regularly!

The best cure of course is prevention!

Protecting against SQL Injection

On the surface, protection against SQL injection appears to be easy to implement but, in fact, it is not as easy as it looks. The solutions fall into two distinct areas:

  • Do not allow dynamic SQL that uses concatenation, or at least filter the input values and check for special characters such as quote symbols.
  • Use the principle of least privilege and ensure that the users created for the applications have the privileges needed and all extra privileges (such as PUBLIC ones) are not available.

This section cannot go into great detail; such a discussion would constitute an entire article in itself. However, certain basic measures can be taken. These actions fall into two sections:

  • Review the application source code. The code can be written in many different languages, such as PHP, JSP, java, PL/SQL VB, etc., so the solutions vary. However, they all follow a similar pattern. Review the source code for dynamic SQL where concatenation is used. Find the call that parses the SQL or executes it. Check back to where values are entered. Ensure that input values are validated and that quotes are matched and metacharacters are checked. Reviewing source code is a task that is specific to the language used.
  • Secure the database and ensure that all excess privileges are revoked.

Some other simple tips to follow include:

  • If possible, do not use dynamic PL/SQL. The potential for damage is much greater than for dynamic SQL, as then there is scope to execute any SQL, DDL, PL/SQL etc.
  • If dynamic PL/SQL is necessary then use bind variables.
  • If PL/SQL is used use AUTHID CURRENT_USER so that the PL/SQL runs as the logged in user and not the creator of the procedure, function or package.
  • If concatenation is necessary then use numeric values for the concatenation part. This way strings cannot be passed in to add SQL.
  • If concatenation is necessary then check the input for malicious code, i.e. check for union in the string passed in or metacharacters such as quotes.
  • For dynamic SQL if it is necessary use bind variables. An example is shown below:
  • We first need to alter our simple procedure to allow the dynamic part passed in to use a bind variable. This is shown here:

    create or replace procedure get_cust_bind (lv_surname in varchar2)
    is
            type cv_typ is ref cursor;
            cv cv_typ;
            lv_phone        customers.customer_phone%type;
            lv_stmt         varchar2(32767):=''''select customer_phone ''''||
                                    ''''from customers ''''||
                                    ''''where customer_surname=:surname'''';
    begin
            dbms_output.put_line(''''debug:''''||lv_stmt);
            open cv for lv_stmt using lv_surname;
            loop
                    fetch cv into lv_phone;
                    exit when cv%notfound;
                    dbms_output.put_line(''''::''''||lv_phone);
            end loop;
            close cv;
    exception
            when others then
                    dbms_output.put_line(sqlcode||sqlerrm);
    end get_cust_bind;
    /       
    

    First we execute with a genuine value, in this case “Clark”, to show that the correct records are returned. We then we try to SQL inject this procedure and find it doesn’t work:

    SQL> exec get_cust_bind(''''Clark'''');
    debug:select customer_phone from customers where customer_surname=:surname
    ::999444888
    ::999777888
    
    PL/SQL procedure successfully completed.
    
    SQL> exec get_cust_bind(''''x'''''''' union select username from all_users where ''''''''x''''''''=''''''''
    x'''');
    debug:select customer_phone from customers where customer_surname=:surname
    

    Some more pointers:

    • Encrypt sensitive data so that it cannot be viewed.
    • Revoke all PUBLIC privileges where possible from the database
    • Do not allow access to UTL_FILE, DBMS_LOB, DBMS_PIPE, DBMS_OUTPUT, UTL_HTTP,UTL_SMTP or any other standard or application packages that allow access to the O/S.
    • Change database default passwords.
    • Run the listener as a non-privileged user.
    • Ensure that minimum privileges are granted to application users.
    • Restrict PL/SQL packages that can be accessed from apache.
    • Remove all example scripts and programs from the Oracle install.

    Final thoughts

    I hope that this article has given an overview of some of the possibilities of SQL injecting Oracle and done so with simple examples that most readers can try. Again, SQL injection is a relatively simple technique and on the surface protecting against it should be fairly simple; however, auditing all of the source code and protecting dynamic input is not trivial, neither is reducing the permissions of all applications users in the database itself. Be vigilant, grant what is needed, and try and reduce dynamic SQL to the minimum.

    Pete Finnigan is a freelance consultant specialising in Oracle and security of Oracle. Pete is currently working in the UK financial sector and has recently completed the new Oracle security step-by-step guide for the SANS institute. Pete has many years of development and administration experience in many languages. Pete is regarded as one of the worlds leading experts on Oracle security.

    Watch for the forthcoming book The SANS Institute Oracle Security Step-by-step – A survival guide for Oracle security written by Pete Finnigan with consensus achieved by experts from over 53 organizations with over 230 years of Oracle and security experience. Due to be published in the next few weeks by the SANS Institute.

  • If PL/SQL is used use AUTHID CURRENT_USER so that the PL/SQL runs as the logged in user and not the creator of the procedure, function or package.
  • If concatenation is necessary then use numeric values for the concatenation part. This way strings cannot be passed in to add SQL.
  • If concatenation is necessary then check the input for malicious code, i.e. check for union in the string passed in or metacharacters such as quotes.
  • For dynamic SQL if it is necessary use bind variables. An example is shown below:

We first need to alter our simple procedure to allow the dynamic part passed in to use a bind variable. This is shown here:

create or replace procedure get_cust_bind (lv_surname in varchar2)
is
        type cv_typ is ref cursor;
        cv cv_typ;
        lv_phone        customers.customer_phone%type;
        lv_stmt         varchar2(32767):=''''select customer_phone ''''||
                                ''''from customers ''''||
                                ''''where customer_surname=:surname'''';
begin
        dbms_output.put_line(''''debug:''''||lv_stmt);
        open cv for lv_stmt using lv_surname;
        loop
                fetch cv into lv_phone;
                exit when cv%notfound;
                dbms_output.put_line(''''::''''||lv_phone);
        end loop;
        close cv;
exception
        when others then
                dbms_output.put_line(sqlcode||sqlerrm);
end get_cust_bind;
/       

First we execute with a genuine value, in this case “Clark”, to show that the correct records are returned. We then we try to SQL inject this procedure and find it doesn’t work:

SQL> exec get_cust_bind(''''Clark'''');
debug:select customer_phone from customers where customer_surname=:surname
::999444888
::999777888

PL/SQL procedure successfully completed.

SQL> exec get_cust_bind(''''x'''''''' union select username from all_users where ''''''''x''''''''=''''''''
x'''');
debug:select customer_phone from customers where customer_surname=:surname

Some more pointers:

  • Encrypt sensitive data so that it cannot be viewed.
  • Revoke all PUBLIC privileges where possible from the database
  • Do not allow access to UTL_FILE, DBMS_LOB, DBMS_PIPE, DBMS_OUTPUT, UTL_HTTP,UTL_SMTP or any other standard or application packages that allow access to the O/S.
  • Change database default passwords.
  • Run the listener as a non-privileged user.
  • Ensure that minimum privileges are granted to application users.
  • Restrict PL/SQL packages that can be accessed from apache.
  • Remove all example scripts and programs from the Oracle install.

Final thoughts

I hope that this article has given an overview of some of the possibilities of SQL injecting Oracle and done so with simple examples that most readers can try. Again, SQL injection is a relatively simple technique and on the surface protecting against it should be fairly simple; however, audit

上一页  [1] [2] [3]  下一页


[Access]sql随机抽取记录  [Access]ASP&SQL让select查询结果随机排序的实现方法
[系统软件]EXP-00008: ORACLE error 904 encountered的解决方…  [系统软件]Explanation of UFT-8 and Unicode
[系统软件]Using dllimport and dllexport in C++ Classes  [系统软件]SQL语句性能优化--LECCO SQL Expert
[常用软件]神奇 我家的照片会唱歌 照片会唱歌  [常用软件]PB7 连接 Oracle 的配置方法
[C语言系列]SQL Server到DB2连接服务器的实现  [C语言系列]SQL Server到SYBASE连接服务器的实现
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台