Author: xy7 # 80sec.com
From: http://www.80vul.com/pch/
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.
2. 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;
Case 2:
If (zend_get_parameters_ex (2, & num, & arg_base) = FAILURE ){
WRONG_PARAM_COUNT;
}
Convert_to_long_ex (arg_base );
Base = Z_LVAL_PP (arg_base );
Break;
Default:
WRONG_PARAM_COUNT;
}
RETVAL_ZVAL (* num, 1, 0 );
Convert_to_long_base (return_value, base );
}
Zend/zend_operators.c-> convert_to_long_base ()
......
Case IS_STRING:
Strval = Z_STRVAL_P (op );
Z_LVAL_P (op) = strtol (strval, NULL, base );
STR_FREE (strval );
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); it uses the strtol function to process the parameter.
The function prototype is 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.
Test code
<?
// Intval. php
$ Var = "20070601 ";
If (intval ($ var ))
Echo "its safe ";
Echo $ var =. $ var;
Echo "<br> ";
$ Var1 = "1 union select 1, 1 from admin ";
If (intval ($ var1 ))
Echo "its safe too ";
Echo $ var1 =. $ var1;
?> 4. Practical Application
WordPress <= 2.0.6 wp-trackback.php Zend_Hash_Del_Key_Or_Index/SQL injection exploit
[Http://superhei.blogbus.com/logs/4255503.html]