We learn
1. PHP intval Function Description
The intval function has a feature: "conversion starts only when a number or positive or negative sign is encountered, and the conversion ends when a non-number or string ends ", in some applications, due to insufficient understanding of the intval function, improper use may cause security vulnerabilities caused by bypassing some security judgments.
Ii. PHP intval Function Analysis
- PHP_FUNCTION (intval)
- {
- Zval ** num, ** arg_base;
- Int base;
- Switch (ZEND_NUM_ARGS ()){
- Case 1:
- If (zend_get_parameters_ex (1, & num) = FAILURE ){
- WRONG_PARAM_COUNT;
- }
- Base = 10;
- Break;
When the intval function receives a string parameter, it calls convert_to_long_base () for processing, and then calls Z_LVAL_P (op) = strtol (strval, NULL, base). The strtol function is used to process the parameter.
The PHP intval function is prototype as follows:
Long int strtol (const char * nptr, char ** endptr, int base );
This function converts the nptr parameter to a growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. the base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal.
The process is as follows:
Strtol () scans the nptr parameter string, skips the leading space character, and ends the conversion only when a number or positive or negative sign is encountered, and return the result.
When intval is used in the if and so on, it will cause the judgment to be de-meaningful, leading to security vulnerabilities.
Iii. PHP intval function test code
- //intval.php
- $var="20070601";
- if (intval($var))
- echo "it's safe";
- echo '$var='.$var;
- echo "
- ";
- $var1="1 union select 1,1,1 from admin";
- if (intval($var1))
- echo "it's safe too";
- echo '$var1='.$var1;
- ?>
Iv. Practical Application of PHP intval Functions
WordPress <= 2.0.6 wp-trackback.php Zend_Hash_Del_Key_Or_Index/SQL injection exploit