前言
整理并汇总了在文件上传攻击利用环节中 frequently encountered 的几个关键PHP函数。
透彻掌握这些函数的内部机制与边界情况,对于顺利实施或防御文件上传漏洞至关重要。
1. deldot
deldot是upload-lab靶场中出现的一个自定义函数,其代码定义在common.php文件中,具体实现如下所示:
function deldot($s){
for($i = strlen($s)-1;$i>0;$i--){
$c = substr($s,$i,1);
if($i == strlen($s)-1 and $c != '.'){
return $s;
}
if($c != '.'){
return substr($s,0,$i+1);
}
}
}
该函数的逻辑是从字符串末尾向前遍历,逐个删除字符'.',直到遇到非'.'字符或到达字符串起始位置为止。
针对以下输入进行测试:
echo deldot("hello world")."\n";
echo deldot("hello world.")."\n";
echo deldot("hello world....")."\n";
echo deldot("hello.world.")."\n";
程序输出结果为:
hello world hello world hello world hello.world
2. in_array
in_array(mixed $needle, array $haystack, bool $strict = false): bool
第一个参数$needle代表要搜索的目标值,$haystack是被搜索的数组,第三个参数$strict控制是否进行严格的类型检查。
默认情况下,第三个参数为false,意味着不进行类型比对,仅比较值是否相等。
示例代码如下:
if(in_array("AAA",$arr,false)) echo 1;
if(in_array("aaa",$arr,false)) echo 2;
if(in_array("AAA",$arr,true)) echo 3;
if(in_array("aaa",$arr,true)) echo 4;
执行后的输出为:
13
3. intval
intval(mixed $value, int $base = 10): int
intval函数主要用于将变量转换为整数类型。
$value参数指定待转换的变量,支持字符串、数字或数组。$base参数指定转换时的进制基数,仅在$value为字符串时生效。
若$base设为0,函数会根据$value的前缀自动推断进制。
- 当存在前置的0x或0X时,使用16进制。
- 若字符串以0开头(且非0x),则按八进制解析。
- 若无特殊前缀,则默认按十进制解析。
intval返回int类型的结果。若转换失败,则返回0。
需特别注意返回值存在上限。当待转换数值超出PHP整型最大值时,函数将返回整型的最大允许值。
echo intval("111");
echo "\n";
echo intval("111a");
echo "\n";
echo intval("0x333");
echo "\n";
echo intval("888",8);
echo "\n";
echo intval("122",3);
echo "\n";
echo intval("11111111111111111111111111111111111");
echo "\n";
echo intval("2222222222222222222222222222222");
测试输出如下:
111 111 0 0 17 9223372036854775807 9223372036854775807
4. strrchr
strrchr(string $haystack, mixed $needle): string
strrchr在$haystack中查找最后一次出现的$needle,并返回从该位置到字符串末尾的子串。若未找到,则返回false。
注意事项:
- 若$needle不是单字符,函数仅使用其第一个字符进行匹配。
- 若$needle是数值,函数会将其视为对应的ASCII码值进行匹配。
$S = "hhhahahaha2333";
echo strrchr($S,'h')."\n";
echo strrchr($S,'hwweraer')."\n";
echo strrchr($S,104)."\n";
if(strrchr($S,'k') == false) echo "false";
ha2333 ha2333 ha2333 false
5. strtolower
strtolower(string $string): string
将字符串$string中的所有英文字母转换为小写并返回。
$S = "HaHaHaHa,Hello!!";
echo strtolower($S);
hahahaha,hello!!
6. strrpos
strrpos(string $haystack, string $needle, int $offset = 0): int
返回子串$needle在字符串中最后一次出现的位置索引。
在PHP4版本中,$needle必须为单字符;若传入多字符,仅取首字符匹配。
与strrchr类似,若$needle为数值,则按其ASCII码对应字符匹配。
自PHP5起,$needle支持多字符匹配。
PHP5新增第四个参数$offset,用于指定开始匹配的起始位置。
返回匹配位置的索引,未找到时返回false。
注意:
- 由于成功匹配可能返回索引0,判断是否为false时必须使用全等运算符===。
- 该函数区分大小写。与该函数相似的函数有:
- stripos:查找首次出现位置,不区分大小写。
- strpos:查找首次出现位置,区分大小写。
- strripos:查找最后出现位置,不区分大小写。
- 命名规则中,“i”表示忽略大小写,“rr”表示查找最后出现位置。
$s = "Phpphphpphpp";
echo strrpos($s,"php");
echo strrpos($s,"h");
echo strrpos($s,"P");
if(strrpos($s,"PHP") === false) echo "No exist";
输出结果:
890No exist
注:测试环境PHP版本为5.3.3。
在PHP4环境下,上述行为可能有所不同。
7. str_ireplace
str_ireplace(
mixed $search,
mixed $replace,
mixed $subject,
int &$count = ?
): mixed
str_ireplace用于对字符串或数组中的子串进行不区分大小写的替换。
$search指定被替换的内容(子串或数组),$replace指定替换后的内容,$subject为目标字符串。
- 若$search和$replace均为字符串,则将$subject中所有匹配$subsearch的子串替换为$replace。
- 若两者均为数组,则建立映射关系替换。若$replace元素少于$search,多余项将被空字符串替代。
- 若$search为数组而$replace为字符串,则$search中所有元素均被替换为$replace。
$count参数可用于记录替换发生的次数。
注意:
- 替换过程从左向右进行。
- 该函数执行时不区分大小写(区别于str_replace)。
<?php
echo str_ireplace("php","","hello.php")."\n";
echo str_ireplace("pHP","","hello.Php")."\n";
echo str_ireplace("php","","hello.phPHpp")."\n";
echo str_ireplace("php","p","hello.phphp")."\n";
echo str_ireplace("php",""."phpphpphpphpphp.php",3)."\n";
echo str_ireplace(array("php","html"),"","hello.php.html")."\n";
echo str_ireplace(array("php","html"),array("1","2"),"hello.php.html")."\n";
echo str_ireplace(array("php","html","css"),array("1","2"),"hello.php.css.html")."\n";
echo str_ireplace(array("php","html","css"),"1","hello.php.css.html")."\n";
foreach (str_ireplace(array("php","html","css"),array("1","2","3"),array("hello.html","hello.css","hello.html")) as $it){
echo "$it ";
}
?>
hello. hello. hello.Hpp hello.php 3 hello… hello.1.2 hello.1…2 hello.1.1.1 hello.2 hello.3 hello.2
8. strstr
strstr(string $haystack, mixed $needle, bool $before_needle = false): string
查找$needle在$haystack中首次出现的位置,并返回从该位置至末尾的子串。
PHP5新增第三个参数$before_needle。若设为true,则返回$needle之前的部分。
$s = "123phpphp.php";
echo strstr($s,"php")."\n";
echo strstr($s,"php",true)."\n";
phpphp.php 123
9. substr
substr(string $string, int $offset, ?int $length = null): string
截取并返回字符串$string中的子串。
$offset指定子串起始字符在$string中的索引,$length指定截取长度。
$length参数的取值影响如下:
- 当$length为null(默认)时,截取从$offset到字符串末尾的所有字符。
- 当$length为正数时,从$offset开始截取最多$length个字符。
- 当$length为0时,返回空字符串。
- 当$length为负数时,截取从$offset开始,直到距离字符串末尾倒数第$length个字符之前的内容。
$s = "123456789";
echo substr($s,1,3)."\n";
echo substr($s,1,-1)."\n";
echo substr($s,1)."\n";
echo substr($s,1,0)."\n";
234 2345678 23456789
10. trim
trim(string $str, string $character_mask = " \t\n\r\0\x0B"): string
移除字符串$str首尾指定的字符,默认为空白字符。
当第二个参数省略时,默认去除以下字符:
- " " 空格
- “\t” 制表符
- “\n” 换行符
- “\r” 回车符
- “\0” 空字节符
- “\x0B” 垂直制表符
$s = "\n 1 23456789\t\n123456789\r";
echo trim($s);
1 23456789 123456789
附PHP 文件上传处理
- $_FILES["file"]["name"] 客户端提交文件的原始名称
- $_FILES["file"]["type"] 浏览器提供的MIME类型,如image/gif。PHP端通常不校验此值,不可依赖。
- $_FILES["file"]["size"] 上传文件大小,单位为字节
- $_FILES["file"]["tmp_name"] 文件在服务端的临时存储路径
- $_FILES["file"]["error"] 上传错误代码,0表示无错误,即上传成功
参考资料
PHP官方文档
总结
至此,关于PHP文件上传利用中常见函数的分析介绍完毕。如需了解更多相关内容,请查阅本站历史文章或继续关注后续更新。感谢支持!