[鹤城杯 2021]EasyP

源码

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
27
<?php
include 'utils.php';

if (isset($_POST['guess'])) {
$guess = (string) $_POST['guess'];
if ($guess === $secret) {
$message = 'Congratulations! The flag is: ' . $flag;
} else {
$message = 'Wrong. Try Again';
}
}

if (preg_match('/utils\.php\/*$/i', $_SERVER['PHP_SELF'])) {
exit("hacker :)");
}

if (preg_match('/show_source/', $_SERVER['REQUEST_URI'])){
exit("hacker :)");
}

if (isset($_GET['show_source'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}else{
show_source(__FILE__);
}
?>

分析

此题主要是正则表达式的绕过,还有几个$_SERVER

1
2
3
4
5
$_SERVER['PHP_SELF']:正在执行脚本的文件名
$_SERVER['REQUEST_URI']:与 _SERVER[‘PHP_SELF’]的区别是会加上参数
例子:127.0.0.1/pikachu/index.php?file=1.php
$_SERVER['PHP_SELF']=/pikachu/index.php
$_SERVER['REQUEST_URI']=/pikachu/index.php?file=1.php

basename()函数有一个漏洞在使用默认语言环境设置时,basename() 会删除文件名开头的非 ASCII 字符和中文

payload

1
index.php/utils.php/你?show.source=1

这个正则:

1
2
3
if (preg_match('/utils\.php\/*$/i', $_SERVER['PHP_SELF'])) {
exit("hacker :)");
}

正则表达式的意思是url的末尾不能存在utils.php,我们的payload末尾是 你?show.source=1 绕过成功

1
2
3
4
if (preg_match('/show_source/', $_SERVER['REQUEST_URI'])){
exit("hacker :)");
}
//这里面过滤了show_source,可以用 [ . 来代替_

接下来是basename()

1
2
3
4
if (isset($_GET['show_source'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}

由于我们构造的payload的最后一个下划线/后是中文,无acill码,因此后面的/会被删除,只保留出utils.php

知识点:

1
2
3
4
5
6
7
8
9
basename() 函数是 PHP 的一个文件系统函数,它可以从一个包含有指向一个文件的全路径的字符串中返回基本的文件名。 也就是说,它可以忽略路径中的目录部分,只保留最后一个斜杠(/)或反斜杠(\)后面的内容。
例如,如果你有一个路径是 “/testweb/home.php”,那么 basename($path) 就会返回 “home.php”,这就是文件名部分。
basename() 函数还可以接受一个可选的参数 suffix,它是一个字符串,表示文件的扩展名。如果你指定了这个参数,那么 basename() 函数会在返回文件名之前,先把这个扩展名去掉。
例如,如果你有一个路径是 “/testweb/home.php”,那么 basename($path, “.php”) 就会返回 “home”,这就是去掉了扩展名的文件名。
$_SERVER['PHP_SELF']:正在执行脚本的文件名
$_SERVER['REQUEST_URI']:与 _SERVER[‘PHP_SELF’]的区别是会加上参数
例子:127.0.0.1/pikachu/index.php?file=1.php
$_SERVER['PHP_SELF']=/pikachu/index.php
$_SERVER['REQUEST_URI']=/pikachu/index.php?file=1.php
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信