php内置函数intval()使用不当的安全漏洞分析_PHP

WBOY
Release: 2016-06-01 12:20:55
Original
886 people have browsed it
一、描述

intval函数有个特性:"直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时(\0)结束转换",在某些应用程序里由于对intval函数这个特性认识不够,错误的使用导致绕过一些安全判断导致安全漏洞.

二、分析

  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;

当intval函数接受到字符串型参数是调用convert_to_long_base()处理,接下来调用Z_LVAL_P(op) = strtol(strval, NULL, base);通过strtol函数来处理参数。

函数原型如下:

long int strtol(const char *nptr,char **endptr,int base);

这个函数会将参数nptr字符串根据参数base来转换成长整型数,参数base范围从2至36,或0.参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。

流程为:

strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时(\0)结束转换,并将结果返回。

那么当intval用在if等的判断里面,将会导致这个判断实去意义,从而导致安全漏洞.

三、测试代码

  //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;

四、实际应用

WordPress
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!