Security Implementation Analysis of ThinkPHP framework (1)

Source: Internet
Author: User
Tags sql injection prevention

Security Implementation Analysis of ThinkPHP framework (1)

The ThinkPHP framework is one of the popular PHP frameworks in China. Although it cannot be compared with those frameworks outside China, it has the advantage that the Chinese manual is comprehensive. I recently studied SQL injection. I used to use the TP framework because the underlying layer provides security functions, so I didn't think about security issues during the development process. I want to know how TP implements SQL Injection prevention, so I read some source code. Combined with the vulnerabilities of phith0n Daniel on wooyun, I analyzed and sorted out some ideas ~~

I. I have to say I Function

The TP system provides the I function for filtering input variables. The significance of the entire function body is to obtain data in various formats, such as I ('get. '), I ('Post. and then use the htmlspecialchars function (by default) for processing. If you need to use other methods for security filtering, you can set it in/ThinkPHP/Conf/convention. php:

'Default _ filter' => 'strip _ tags', // You can also set multiple filtering methods 'default _ filter' => 'strip _ tags, stripslashes ',

You can find the I function from/ThinkPHP/Common/functions. php. The source code is as follows:

 
 
  1. /**
  2. * Filtering and default values are supported for retrieving input parameters.
  3. * Usage:
  4. * <Code>
  5. * I ('id', 0); get the id parameter to automatically judge get or post
  6. * I ('Post. name', '', 'htmlspecialchars'); get $ _ post ['name']
  7. * I ('get. '); get $ _ GET
  8. * </Code>
  9. * @ Param string $ name the variable name supports the specified type
  10. * @ Param mixed $ default value when the default value does not exist
  11. * @ Param mixed $ filter parameter Filtering Method
  12. * @ Param mixed $ datas the additional data source to be obtained
  13. * @ Return mixed
  14. */
  15. Function I ($ name, $ default = '', $ filter = null, $ datas = null ){
  16. Static $ _ PUT = null;
  17. If (strpos ($ name, '/') {// specifies the Modifier
  18. List ($ name, $ type) = explode ('/', $ name, 2 );
  19. } Elseif (C ('var _ AUTO_STRING ') {// forcibly converted to a string by default
  20. $ Type ='s ';
  21. }
  22.  
  23. /* Get data in the format of $ name: first judge the source of the parameter, and then obtain data in various formats */
  24. If (strpos ($ name, '.') {list ($ method, $ name) = explode ('.', $ name, 2);} // specify the parameter Source
  25. Else {$ method = 'param';} // set to automatically get
  26. Switch (strtolower ($ method )){
  27. Case 'get': $ input = & $ _ get; break;
  28. Case 'post': $ input = & $ _ post; break;
  29. Case 'put':/* omitted here */
  30. Case 'param':/* omitted here */
  31. Case 'path':/* omitted here */
  32. }
  33.  
  34. /* Filter the obtained data */
  35. If (''// get all variables
  36. $ Data = $ input;
  37. $ Filters = isset ($ filter )? $ Filter: C ('default _ filter ');
  38. If ($ filters ){
  39. If (is_string ($ filters) {$ filters = explode (',', $ filters);} // supports multiple filtering methods
  40. Foreach ($ filters as $ filter ){
  41. $ Data = array_map_recursive ($ filter, $ data); // loop Filtering
  42. }
  43. }
  44. } Elseif (isset ($ input [$ name]) {// value operation
  45. $ Data = $ input [$ name];
  46. $ Filters = isset ($ filter )? $ Filter: C ('default _ filter ');
  47. If ($ filters) {/* filters parameters and supports regular expression verification */
  48. /* Omitted here */
  49. }
  50. If (! Emptyempty ($ type) {// If the forced conversion type is set
  51. Switch (strtolower ($ type )){
  52. Case 'A': $ data = (array) $ data; break; // array
  53. Case 'D': $ data = (int) $ data; break; // number
  54. Case 'F': $ data = (float) $ data; break; // floating point
  55. Case 'B': $ data = (boolean) $ data; break; // boolean
  56. Case's ': // string
  57. Default: $ data = (string) $ data;
  58. }
  59. }
  60. } Else {// default variable value
  61. $ Data = isset ($ default )? $ Default: null;
  62. }
  63.  
  64. Is_array ($ data) & array_walk_recursive ($ data, 'Think _ filter'); // If $ data is an array, use think_filter to filter the Array
  65. Return $ data;
  66. }

Well, the function is basically divided into three parts: the first one to obtain data in various formats. The second part is the cyclic encoding of the obtained data, whether it is a two-dimensional array or a three-dimensional array. The third block, that is, the second to the last line, calls think_filter to perform the last step of mysterious processing on the data.

Let's first trace the think_filter function:

 
 
  1. // Added the latest version 3.2.3 of row 1536
  2. Function think_filter (& $ value) {// filter and query special characters
  3. If (preg_match ('/^ (EXP | NEQ | GT | EGT | LT | ELT | OR | XOR | LIKE | NOTLIKE | not between | NOTBETWEEN | BETWEEN | NOTIN | not in | IN) $/I ', $ value )){
  4. $ Value. = '';
  5. }
  6. }

This function is very simple. It can be seen at a glance that a space is added after some specific keywords. But this is called the think_filter function. It only adds a space. What filtering function does it play?

We all know important logic verification, such as verifying whether a user has logged on, whether the user can buy a product, etc. It must be verified on the server side. If it is verified on the front end, it is easy to bypass. In the same way, in/exp logic structures in a program should also be controlled by the server.

When the data transmitted to the server is like this: id [0] = in & id [1] =, 3, if the think_filter function is not available, it will be parsed into 1 in the following table, it will be regarded as the server-side logic parsing. However, if it is changed to the following table 2, a space is added, and the parsing cannot be matched, the vulnerability is avoided.

 
 
  1. 1. $ data ['id'] = array ('in' => '1, 2, 3 ')
  2.  
  3. // After being filtered by think_filter, it will look like:
  4. 2. $ data ['id'] = array ('in' => '1, 2, 3 ')


Related Article

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.