duanhan5230 2018-04-28 13:31
浏览 96

PHP检测SQL注入尝试

My code is already safe, using parameters in SQL queries, but, I would like to detect if anyone attempts to inject something into a submit form.

I found Snort, but I would need something that would be at PHP script level, not the whole network.

This is for a site that contains personal information for students and thus, we will warn (or even take action against) anyone that even tries an attack.

  • 写回答

2条回答 默认 最新

  • duannian7116 2018-12-17 17:00
    关注

    I have created a very basic and simple PHP class for checking / detecting SQL injection attempts.

    <?php
    /**
     * simpleSQLinjectionDetect Class
     * @link      https://github.com/bs4creations/simpleSQLinjectionDetect 
     * @version   1.1
     */
    
    class simpleSQLinjectionDetect
    {   
        protected $_method  = array();
        protected $_suspect = null; 
    
        public $_options = array(
                                'log'    => true,
                                'unset'  => true,
                                'exit'   => true,
                                'errMsg' => 'Not allowed',
                            );
    
        public function detect()
        {
            self::setMethod();
    
            if(!empty($this->_method))
            {
                $result = self::parseQuery();
    
                if ($result)
                {
                    if ($this->_options['log']) {
                        self::logQuery();
                    }
    
                    if ($this->_options['unset']){
                        unset($_GET, $_POST);
                    }
    
                    if ($this->_options['exit']){
                        exit($this->_options['errMsg']);
                    }
                }
            }
        }
    
        private function setMethod()
        {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') {
                $this->_method = $_GET;
            }
    
            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                $this->_method = $_POST;
            }
        }
    
        private function parseQuery()
        {
            $operators = array(
                'select * ',
                'select ',
                'union all ',
                'union ',
                ' all ',
                ' where ',
                ' and 1 ',
                ' and ',
                ' or ',
                ' 1=1 ',
                ' 2=2 ',
                ' -- ',
            );
    
            foreach($this->_method as $key => $val)
            {
                $k = urldecode(strtolower($key));
                $v = urldecode(strtolower($val));
    
                foreach($operators as $operator)
                {
                    if (preg_match("/".$operator."/i", $k)) {
                        $this->_suspect = "operator: '".$operator."', key: '".$k."'";
                        return true;
                    }
                    if (preg_match("/".$operator."/i", $v)) {
                        $this->_suspect = "operator: '".$operator."', val: '".$v."'";
                        return true;
                    }
                }
            }
        }
    
        private function logQuery()
        {
            $data  = date('d-m-Y H:i:s') . ' - ';
            $data .= $_SERVER['REMOTE_ADDR'] . ' - ';
            $data .= 'Suspect: ['.$this->_suspect.'] ';
            $data .= json_encode($_SERVER);
            @file_put_contents('./logs/sql.injection.txt', $data . PHP_EOL, FILE_APPEND);
        }
    }
    
    /* then call it in your app...
    *********************************************/
    $inj = new simpleSQLinjectionDetect();
    $inj->detect();
    

    You can check it on github also

    This is a very simple and basic class. Any suggestions for improvements / updates are welcome :)

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示