MySQL paging negative SQL attack

Source: Internet
Author: User

1 Attack analysis

Clevercode's OPS colleague told me that when he looked at MySQL's error log, he found a lot of errors, so someone was attacking an address. A large number of this error occurs because MySQL does not support negative numbers at limit. Usually when we split the page, the URL is generally written as http://xxx.com?page=1&pageSize=20, that is, to get the first page of data. 20 rows per page. But if http://xxx.com?page=-1&pageSize=20 is passed in. The following error will occur.



2 Problematic PHP code

function Getuserloginlog ($page, $pageSize) {        //parameter Check    if (!is_int ($page) | |!is_int ($pageSize)) {        return;    }        $start = ($page-1) * $pageSize;     $SQLSTR = "SELECT * from User_login_log the ORDER by id desc limit $start, $pageSize";    Execute SQL statement    //...            .. If page=-1,pagesize=20, the above statement becomes    //$sqlStr = "SELECT * from User_login_log ORDER BY id desc limit-40,20";       


3 PHP code to prevent attacks

You only need to judge $page, $pageSize is a positive integer.

function Getuserloginlog ($page, $pageSize) {        //parameter Check    if (!is_int ($page) | |!is_int ($pageSize)) {        return;    }        //Positive integer check    if ($page < 1 | | $pageSize < 1) {        return;    }        $start = ($page-1) * $pageSize;     $SQLSTR = "SELECT * from User_login_log the ORDER by id desc limit $start, $pageSize";        Execute SQL statement    //...              }


Copyright Notice:

1) original works, from "Clevercode's blog" , please be sure to mention the following original address when reproduced , otherwise hold the copyright legal responsibility.

2) Original address : http://blog.csdn.net/clevercode/article/details/45935593 ( reprint must indicate this address ).

3) Category address: http://blog.csdn.net/clevercode/article/category/3262205 ( Blog continues to increase, concern please collection )

4) welcome everyone to pay attention to my blog more wonderful content: Http://blog.csdn.net/CleverCode.



MySQL paging negative SQL attack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.