PHP 检测 SQL 注入(inject)尝试

标签 php mysql sql sql-injection

我的代码已经很安全,在 SQL 查询中使用参数,但是,我想检测是否有人尝试将某些内容注入(inject)提交表单。

我找到了 Snort,但我需要的是 PHP 脚本级别的东西,而不是整个网络。

这是一个包含学生个人信息的网站,因此,我们将警告(甚至采取行动)任何试图攻击的人。

最佳答案

我已经创建了一个非常基本和简单的 PHP 类来检查/检测 SQL 注入(inject)尝试。

<?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();

你可以在github上查看还有

这是一个非常简单的基础类。欢迎提出任何改进/更新建议:)

关于PHP 检测 SQL 注入(inject)尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077086/

相关文章:

php - 使用php在数据库中添加多行

php - 如何验证富文本编辑器源

php - Laravel闭包,什么是$query,$query是如何传递的?

php - 这个查询有什么问题?

sql - 获取当月的当前天数(节假日除外)

php - 基于 2 个参数从 MySQL 数据库中选择

sql - 标签模式提供类似于 Stack Overflow 的标签同义词的功能

sql - MySQL:按字段大小/长度排序

sql - 创建Oracle物化 View ,每5分钟刷新一次使用物化 View 日志

mysql - 选择最新行