时间2022‎年‎5‎月‎13‎日,‏‎19:36:29

源码分析:

<?php

show_source(__FILE__);

$Step1=False;
$Step2=False;

$info=(array)json_decode(@$_GET['Information']);//将传入的json格式的字符串转化为数组
if(is_array($info)){
var_dump($info);
is_numeric(@$info["year"])?die("Sorry~"):NULL;//year键的值不能是数字
if(@$info["year"]){
($info["year"]=2022)?$Step1=True:NULL;//year键的值得是2022
}
if(is_array(@$info["items"])){
if(!is_array($info["items"][1])OR count($info["items"])!==3 ) die("Sorry~");//items键的第一个索引得是数组,items的键数得是3个——不知道是不是叫键数。
$status = array_search("skiing", $info["items"]);//查找items键数组对应的值中有没有skiing,有就返回他的键名
$status===false?die("Sorry~"):NULL;//没有就是sorry
foreach($info["items"] as $key=>$val){//对items键对应的值——数组中的键值来遍历
$val==="skiing"?die("Sorry~"):NULL;//值不能有skiing
}
$Step2=True;
}
}
if($Step1 && $Step2){
include "2022flag.php";echo $flag;
}
?>

array(0) { }

json_decode

json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON格式的字符串进行编码.那么这个函数该如何使用呢?

json_decode的语法规则:  json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

json_decode接受一个JSON格式的字符串并且把它转换为PHP变量 ,当该参数$assoc为TRUE时,将返回array,否则返回object。

JSON 格式的字符串

$json = ‘{“a”:”php”,”b”:”mysql”,”c”:3}’;

其中a为键,php为a的键值。

实例:

<?php   
$json = '{"a":"php","b":"mysql","c":3}';
$json_Class=json_decode($json);
$json_Array=json_decode($json, true);
print_r($json_Class);
print_r($json_Array);
?>

程序输出:

stdClass Object (
[a] => php
[b] => mysql
[c] => 3 )
Array (
[a] => php
[b] => mysql
[c] => 3 )

在上面代码的前提下访问对象类型$json_Class的a的值

echo $json_Class->{'a'};

程序输出:php

访问数组类型$json_Array的a的值

echo $json_Array['a'];

程序输出:php

试着传一个试试看

image-20220513194640406

实例

在数组中搜索键值 “red”,并返回它的键名:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

返回 a

定义和用法

array_search() 函数在数组中搜索某个键值,并返回对应的键名。


语法

array_search(value,array,strict)

参数 描述
value 必需。规定在数组中搜索的键值。
array 必需。规定被搜索的数组。
strict 可选。如果该参数被设置为 TRUE,则函数在数组中搜索数据类型和值都一致的元素。可能的值:truefalse - 默认如果设置为 true,则在数组中检查给定值的类型,数字 5 和字符串 5 是不同的(参见实例 2)

image-20220513202015298

PHP count() 函数

返回数组中元素的数目:

<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>

image-20220513210435652

image-20220513220635204

本地搭建环境自己试一试绕过

第一个参数用2022a绕过

第二个参数得用数组里面套数组

image-20220513224324556

image-20220513224759527

第二关可以,但是第三关就矛盾了。

image-20220513225305411

绕过第二关与第三关——用0

image-20220513225334743

$info=array("year"=>"2022a","items"=> array("0",array("skiing"=>"123"),"123"));

不行

image-20220513225858083

image-20220513225928404

如果最里面的array有东西,那么用json_decode会出来一个对象,那么就不能是数组了,第一个条件就过不来了。

主要是对象和数组

比如,现在有一个索引数组

$arr = Array(‘one’,’two’, ‘three’);

echo json_encode($arr);

输出

[“one”,”two”,”three”]

如果将它改为关联数组:

$arr = Array(‘1’=>’one’,’2’=>’two’,’3’=>’three’);

echo json_encode($arr);

输出变为

{“1”:”one”,”2”:”two”,”3”:”three”}

注意,数据格式从”[]”(数组)变成了”{}”(对象)

所以主要是得[]而非{}

{“year”:”2022a”,”items”:[0,{},”123”]}——不行

{“year”:”2022a”,”items”:[0,[],”123”]}——行

(48条消息) CTF之PHP代码审计1_bmth666的博客-CSDN博客_ctf php代码审计