您的位置:首页 > 编程语言 > PHP开发

【安全牛学习笔记】基于PHP的SQL注入漏洞原理及解决办法

2017-10-23 22:30 1201 查看
基于PHP的SQL注入漏洞原理及解决办法

----------------------------------------------------------------------

[mysqlDriver]

<?php

//this file is the entry

error_erporting(E_ALL);

//include files

include 'conf.php';

include 'functions.php';

include 'actions.php';

include 'models.php';

STRACE_LOG = array();

if($action = $_REQUEST['action'] and funciton_exists($action."Action")){

       call_user_func($action.'Action');

}else{

       error('action not exists');

}

-----------------------------------------------------------------------

root@w:~# service apache2 status

Apache2 is runing (pid 7970).

root@w:~# service mysql status

[info] /usr/bin/mysqladmin  ver 8.42 Distib 5.5.40, for debian-linux-gnu on i686

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All right reserved.

Oracle is a registere trademark of Oracle Corportion and/or its

affiliates.Other name may be trademarks of their respective owers.

Server version         5.5.40-0+wherezy-log

Protocol version       10

Connection             Localhost via UNIX socket

UNIX socket            /var/run/mysql/mysqld.sock

Uptime:                3 hours 47 min 40 sec

Threads: 1  Qestions: 1271  Slow queries: 0  Opens: 457  Flush tables: 1 Open

tables: 50  Qestions per second avg: 0.093

root@w:~# php -v

PHP 5.4.36-0+deb7ul (cli) (built: Dec 31 2014 08:33:05)

Copyright (c) 1997-2014 The PHP Group

Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

www-data@w:~$ vim conf.php

<?php

//here is configure file for framework

define('DSN','mysql:host=localhost;dbname=secruity' );

define('DBHOST','127.0.0.1');

define('DBUSER','root');

www-data@w:~$ vim mysqlDriver.php

<?php

//this file is for mysql connection;

class mysql{

    public $conn = null;

    public function musql($table){

        $this->conn = mysql_connect(DBHOST,DBUSER,DBPASS);

        mysql_select_db($table);

    }

}

class mysqlPDO{

    public $conn;

    public function mysqlPDO(){

        try{

            $pdo = new PDO(DSN,DBUSER,DBPASS);

            $this->conn = $pdo;

        }catch(PDOException $e){

            error('error:'.$e);

        }

    }

}

-----------------------------------------------------------------------

sql注入原理及修复方法

       [php语言环境]

1.常见数据库操作方法

2.漏洞原理(sql注入)

3.漏洞危害

4.一些Tips

5.如何避免及修复漏洞

-----------------------------------------------------------------------

[actions.php]

<?php

    // indexAction

    function indexAction(){

        if($id = request('id')){

            $id = request('id');

            $indexModel = new indexModel();

            $result = $ indexModel->getDataById($id);

            include 'tpl/index.tpl';

        }else{

            error('id not exists');

        }

    }

//someAction

function someAction(){

    error("here is someAction");

}

//orderAction

function orderAction(){

    if($order = request('order')){

        $orderModel = new orderModel();

        $result = $orderModel->orderData($order);

        trace($order);

        include 'tpl/index.php';

    }else{

        error('order keywords not exists');

    }

}

//indexPDOAction

function indexPDOAction(){

    if($id = request('id')){

        $indexModel_pdo = new indexModelPDO();

        $result = $indexModel_pdo->getDataByPDO($id);

        include 'tpl/indexPDO.tpl';

    }else{

        error('id not exists');

    }

}

-----------------------------------------------------------------------

<?php

include 'head.tpl';

?>

<div class="panel panel-default">

    <!-- Default panel contents -->

    <div class="panel-heading">UserList</div>

    <!-- Table -->

    <table class="table">

<thead>

<tr>

    <th>id</th>

    <th>username</th>

    <th>password</th>

</tr?

</thead?

<?php

while ($row = mysql_fetch_array($result,MYSQL_NUM)){

    echo '<tbody><tr>';

    echo '<th>',$row[0].'</th>';

    echo '<th>',$row[1].'</th>';

    echo '<th>',$row[2].'</th>';

    echo '</tr></tbody';

}

?>

-----------------------------------------------------------------------

漏洞原理:

    没有对用户提交的数据做安全处理,直接接拼接到sql语句中,传递给数据库引擎执行。

    本质是冯.诺依曼体系结构,没有讲书籍和指令严格的区分开。

-----------------------------------------------------------------------

[models.php]

<?php

include 'mysqlDriver.php';

class indexModel{

    // some vars for model

    public $conn = null;

    //construct function for model

    public function indexModel(){

        $mysql = new mysql('security');

        if($mysql->conn){

            trace('log:mysql connected');

            $this->conn = $mysql-conn;

        }else{

            error('error: mysql connecting error');

        }

    }

    // model functions

    public function getDataById($id){

        $sql = 'select * from users where id='.$id.'';

        $result = mysql_query($sql,$this->conn);

        echo mysql_erron().':'.mysql_error()';

        return $result;

    }

}

-----------------------------------------------------------------------

root@w:~# tail -f /var/log/mysql/mysql.log

//监视filename文件的尾部内容(默认10行,相当于增加参数 -n 10),刷新显示在屏幕上。

                  222 Query     select * from users where id=3  '

                  222 Quit

150128  9:50:07   223 Connect   root@localhost on

                  223 Init DB   security

                  223 Query     select * from users where id=3 union select 1,2,user()

                  223 Quit

150128  9:51:49   224 Conncet   root@localhost on

                  224 Init DB   security

                  224 Query     select * from users where id=1

                  224 Quit

150128  9:55:53   255 Connect   debian-sys-maint@localhost on

                  225 Quit

                  225 Connect   debian-sys-maint@localhost on

                  226 Quit

150128  10:00:44  227 Connect   root@localhost on

                  227 Init DB   security

                  227 Query     select * from users where id=3

                  227 Quit

150128  10:02:31  228 Connect   root@localhost on

                  228 Init DB   security

                  228 Query     select * from users where id=3 and 1=1

                  228 Quit

root@w:~# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g

Your MySQL connection id is 230

Server version: 5.5.40-0+wheeyl-log (Debian)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliate. Other names my be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user()

    -> ;

+-----------------+

| user            |

+-----------------+

| root@localhost  |

+-----------------+

1 row in set (0.00 sec)
http://sql.com/index.php?action=index.php&id=3 union select 1,2,version()
http://sql.com/index.php?action=index.php&id=3 union select 1,2,load_file("/etc/passwd")

漏洞危害

    任意操作数据库数据(读取、修改等),利用数据库引擎读取系统文件、执行系统命令udf,也就是拖库、提权等。

危害等级:高/严重

一些Tipe

都有哪些地方会存在sql注入?

select * from table where id=xx order by xx lim it xx update table set username=xx where id =xxx insert into table values(id ,username,password)

如何避免&修复漏洞:

1.对数据做安全处理(转义)

2.使用PDO(数据与指令隔离)

addslashes \'"

-----------------------------------------------------------------------

models.php

<?php

include 'mysqlDriver.php';

class indexMode{

    // some vars for model

    pubilc $conn = null;

    //construct function for model

    public function indexModel(){

        $mysql = new mysql('security');

        if ($mysql->conn){

            trace('log:mysql connected');

            $this->conn = $mysql->conn;

        }else{

            error('error: mysql connecting error');

        }

    }

    // model function

    public function getDataById($id){

        $id = addslashes($id);                             

        // $id = intval($id);

        $sql = 'select * from users where id='.$id.'"';    

        // $sql = 'select * from users where id='.$id.''; 

        $result = mysql_query($sql,$this->conn);

        echo mysql_errno().':'.mysql_error()'

    retun $result;

    }

            error('error:mysql connecting error');

        }

    }

    publicd function orderData($order){

        $sql = 'select * from users order by '.$order;

        $result = mysql_query($sql,$this->conn);

        return $result;

    }

}

class indexModelPDO{

    public $db;

    public $conn;

    public $db_prepare;

    public function indexModelPDO(){

        $this-db = new mysqlPDO();

        $this-conn = $this->db->conn;

    }

    public function getDataByPDO($id){

        $this->db_prepare = $this->conn->prepare('select * from users where id>?');

        $this->db_prepare->execute(array($id));

        return $this->db_prepare->fetchAll();

    }

}

-----------------------------------------------------------------------

1,

第一种情况:

int-->intval($var)

第二种情况:

string -> ((adddslashes($var),"$var")        //注意的第2点
http://sql.com/index.php?action=indexPDO&id=6 union select 1,2,user()

select * from users where id>'6 union select 1,2,user()'
http://sql.com/index.php?action=indexPDO&id=6'
select * from users where id>'6 \''

2,使用预编译,php->PDO

select * from users where id> 6 union select 1,2,user()       //注意的第1点

object

该笔记为安全牛课堂学员笔记,想看此课程或者信息安全类干货可以移步到安全牛课堂

Security+认证为什么是互联网+时代最火爆的认证?

      牛妹先给大家介绍一下Security+

        Security+ 认证是一种中立第三方认证,其发证机构为美国计算机行业协会CompTIA ;是和CISSP、ITIL 等共同包含在内的国际 IT 业 10 大热门认证之一,和CISSP偏重信息安全管理相比,Security+ 认证更偏重信息安全技术和操作。

       通过该认证证明了您具备网络安全,合规性和操作安全,威胁和漏洞,应用程序、数据和主机安全,访问控制和身份管理以及加密技术等方面的能力。因其考试难度不易,含金量较高,目前已被全球企业和安全专业人士所普遍采纳。

Security+认证如此火爆的原因?  

       原因一:在所有信息安全认证当中,偏重信息安全技术的认证是空白的, Security+认证正好可以弥补信息安全技术领域的空白 。

      目前行业内受认可的信息安全认证主要有CISP和CISSP,但是无论CISP还是CISSP都是偏重信息安全管理的,技术知识讲的宽泛且浅显,考试都是一带而过。而且CISSP要求持证人员的信息安全工作经验都要5年以上,CISP也要求大专学历4年以上工作经验,这些要求无疑把有能力且上进的年轻人的持证之路堵住。在现实社会中,无论是找工作还是升职加薪,或是投标时候报人员,认证都是必不可少的,这给年轻人带来了很多不公平。而Security+的出现可以扫清这些年轻人职业发展中的障碍,由于Security+偏重信息安全技术,所以对工作经验没有特别的要求。只要你有IT相关背景,追求进步就可以学习和考试。

       原因二: IT运维人员工作与翻身的利器。

       在银行、证券、保险、信息通讯等行业,IT运维人员非常多,IT运维涉及的工作面也非常广。是一个集网络、系统、安全、应用架构、存储为一体的综合性技术岗。虽然没有程序猿们“生当做光棍,死亦写代码”的悲壮,但也有着“锄禾日当午,不如运维苦“的感慨。天天对着电脑和机器,时间长了难免有对于职业发展的迷茫和困惑。Security+国际认证的出现可以让有追求的IT运维人员学习网络安全知识,掌握网络安全实践。职业发展朝着网络安全的方向发展,解决国内信息安全人才的匮乏问题。另外,即使不转型,要做好运维工作,学习安全知识取得安全认证也是必不可少的。

        原因三:接地气、国际范儿、考试方便、费用适中!

CompTIA作为全球ICT领域最具影响力的全球领先机构,在信息安全人才认证方面是专业、公平、公正的。Security+认证偏重操作且和一线工程师的日常工作息息相关。适合银行、证券、保险、互联网公司等IT相关人员学习。作为国际认证在全球147个国家受到广泛的认可。

        在目前的信息安全大潮之下,人才是信息安全发展的关键。而目前国内的信息安全人才是非常匮乏的,相信Security+认证一定会成为最火爆的信息安全认证。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息