How to Configure Nginx to prevent SQL injection attacks

Source: Internet
Author: User
Tags mysql in php and php and mysql sql injection

Defense principle:

1. Filter the injection keywords in the basic url through the above configuration;
2. Of course, the user password in the database must be encrypted and stored;
3. The php program performs secondary filtering to filter keywords in GET and POST variables;
4. Disable the error message of PHP and MySQL in the production environment.

SQL injection attacks are generally the request parameters following the question mark, which are represented by $ query_string in nginx.

For example, if 'in the parameter is matched with single quotes and then directed to the error page,/plus/list. php? Tid = 19 & mid = 1124'


The code is as follows:

Rewrite ^. * ([; '<>]). */error.html break;



Directly writing such an rewrite will certainly not match correctly, because the rewrite parameter will only match the requested uri, that is, the/plus/list. php part.
You need to use $ query_string to determine with if. if the query string contains special characters, 404 is returned.


The code is as follows:

If ($ query_string ~ * ". * [; '<>]. *") {Return 404 ;}



Set some concurrencies


The code is as follows:

If ($ request_uri ~ * "(Cost () | (concat ()"){
Return 404;
        }
If ($ request_uri ~ * "[+ | (% 20)] union [+ | (% 20)]") {
Return 404;
        }
If ($ request_uri ~ * "[+ | (% 20)] and [+ | (% 20)]") {
Return 404;
        }
If ($ request_uri ~ * "[+ | (% 20)] select [+ | (% 20)]") {
Return 404;
        }

If ($ query_string ~ * ". * [; '<>]. *") {
Return 404;
}



The following describes how to disable file injection.


The code is as follows:

# Disable file injection
 
Set $ block_file_injections 0;
 
If ($ query_string ~ "[A-zA-Z0-9 _] = http ://"){
 
Set $ block_file_injections 1;
 
}
 
If ($ query_string ~ "[A-zA-Z0-9 _] = (..//?) + "){
 
Set $ block_file_injections 1;
 
}
 
If ($ query_string ~ "[A-zA-Z0-9 _] =/([a-z0-9 _.] //?) + "){
 
Set $ block_file_injections 1;
 
}
 
If ($ block_file_injections = 1 ){
 
Return 444;
 
}



Many injection prevention methods are not described here. For more information, see.

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.