æè¿å°ã調ã¹ã¦ããã®ããPHPã®ä»»æã³ã¼ãå®è¡ç³»ã®èå¼±æ§ã§ããä¸ã§ããpreg_replaceé¢æ°ï¼Perläºæã®æ£è¦è¡¨ç¾ã«ããç½®æãè¡ãªãããã®é¢æ°ï¼ãä¸é©åãªæ¹æ³ã§ä½¿ã£ãå ´åã«çºçããèå¼±æ§ã«ã¤ãã¦èª¿ã¹ã¦ãã¾ããã
ãã£ãããªã®ã§ãæ¥è¨ã«ã¾ã¨ãã¦ã¿ã¾ãã
3種é¡ã®èå¼±æ§
preg_replaceé¢æ°ã使ã£ãPHPã³ã¼ãå®è¡ç³»ã®èå¼±æ§ã«ã¯ã大ããåãã¦3ã¤ã®ç¨®é¡ãããã¾ãã
- 第ä¸å¼æ°ã¸ã®æ¿å ¥ã許ã
- e修飾åä»ãã»ç¬¬äºå¼æ°ã¸ã®æ¿å ¥ã許ã
- e修飾åä»ãã»ç¬¬ä¸å¼æ°ã¸ã®æ¿å ¥ã許ã
以ä¸ã§ããããã«ã¤ãã¦è¦ã¦ããã¾ãã
ã¿ã¤ãï¼ï¼ç¬¬ä¸å¼æ°ã¸ã®æ¿å ¥
以ä¸ã®ã³ã¼ãã«ãä»»æã®PHPã³ã¼ããå®è¡å¯è½ãªèå¼±æ§ãããã¾ãã
$m = preg_replace("/([^<]*)$kw([^>]*)/i", "\\1<font color=red>$kw</font>\\2", $m);
$kwã¨$mã¯å¤é¨ããå¤ãå¶å¾¡å¯è½ã§ãã
ãã®ã³ã¼ãã¯ãæåå$mã®$kwã«åè´ããé¨åã赤æåã§è¡¨ç¤ºããããã®ãã®ã§ãï¼ãããããã¼ã¯ã¼ãã®ãã¤ã©ã¤ããè¡ãªãï¼ã
ãã®ã³ã¼ãã®å ´åãæ»æè ã¯$kwã«ã/e[NULL]ãã®ãããªå¤ãä¸ãã¾ããpreg_replaceé¢æ°ã®ç¬¬ä¸å¼æ°ã¯NULLã»ã¼ãã§ãªãããããã¿ã¼ã³é¨åã®NULLæå以éãç¡è¦ãããä¸è¨ã¯e修飾åä»ãã®æ£è¦è¡¨ç¾ã§ããã¨è§£éããã¦ãã¾ãã¾ãã
ãã¨ã¯ã$mã®å é ã«å®è¡ããããPHPã³ã¼ããåãè¾¼ãã¨ããããã\1ãã«ãªã£ã¦å®è¡ããã¾ãããªãã第äºå¼æ°ã®å é é¨åãæ»æè ãèªç±ã«ã§ããªããããªã±ã¼ã¹ã§ã¯ãä»®ã«ç¬¬ä¸å¼æ°ã«ã/e[NULL]ããæ¿å ¥ã§ãã¦ããä»»æã³ã¼ãã®å®è¡ã«ã¯è³ãã¾ããã
対çã«ã¤ãã¦ãå°ãæ¸ãã¾ããããããã®è©±ã¨ãã¦ãä¸è¨ã®ãããªå¦çã§ããã°preg_replaceé¢æ°ã®ä»£ããã«str_replaceé¢æ°ã使ãã°ååãªã®ã§ããããã®è¾ºã¯æ¬é¡ã§ã¯ç¡ãã®ã§ç½®ãã¦ããã¾ãã
é常ã¯ã第ä¸å¼æ°ã®$kwã«å«ã¾ãããã¿ã¼ã³å ã«ãããç¹æ®è¨å·ï¼ã.ãã(ãã[ãã\ããªã©ï¼ãpreg_quoteé¢æ°ã§ã¨ã¹ã±ã¼ãããã°ããã§ãããããæ£è¦è¡¨ç¾ã§ã®ç½®æï¼ä¾ãã°emacsã®replace-regexpã®ãããªãã¨ï¼ããããããªãã°ãNULLæåãã/ãã®ã¨ã¹ã±ã¼ããå¿ è¦ã§ãã
çºè¦ãããèå¼±æ§ã®ä¾ï¼
Zeroboard Preg_replace Remote Command Execution Vulnerability
Invision Power Board Search.PHP Script Injection Vulnerability
ã¿ã¤ãï¼ï¼e修飾åä»ãã»ç¬¬äºå¼æ°ã¸ã®æ¿å ¥
2ã¤ç®ã¯ãe修飾åä»ãã®preg_replaceé¢æ°ã®ç¬¬äºå¼æ°ã«æ¿å ¥å¯è½ãªã±ã¼ã¹ã§ãã
$m = preg_replace('#^foo (.*)#iem', "'$u'.str_replace('<br>', '', '\\1')", $m);
$uã¨$mã¯å¤é¨ããå¤ãå¶å¾¡å¯è½ã§ãã
ä¸è¨ã³ã¼ãã§ã¯ã$mã®ãfooãã§å§ã¾ãè¡ã«å¯¾ãã¦ã$uããã£ã¤ããå¦çãBRã¿ã°ã®é¤å»å¦çããã¦ãã¾ãã
æ»ææ¹æ³ã¯é常ã«ããããããã§ãã$uã«ã'.phpinfo().'ãã®ãããªå¤ãä¸ããã¨ãe修飾åã«ããevalå®è¡ãããPHPã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
''.phpinfo().''.str_replace('<br>'...ï¼çç¥ï¼
ãã®çµæãphpinfoé¢æ°ãå®è¡ããã¾ãï¼å½ç¶ãªãããphpinfoé¢æ°ã®ä»£ããã«systemé¢æ°ãããè¾¼ãã¨ãshellä¸ã§ã³ãã³ãå®è¡ããã¾ãï¼ã
対çã¨ãã¦ã¯ã$uã第äºå¼æ°ã«å ¥ããéã«ã¨ã¹ã±ã¼ãããæ¹æ³ãã¾ãæãã¤ãã¾ããããããã¨ã¹ã±ã¼ãæ¹æ³ã¯ããç¨åç´ã§ã¯ããã¾ããï¼addslashesã§ã¯ãã¡ã§ãï¼ã
ããããããã®ã¯ä»¥ä¸ã®ãããªã³ã¼ãã¸ã®æ¸ãæãã§ãã
$m = preg_replace('#^foo (.*)#iem', "\$u.str_replace('<br>', '', '\\1')", $m);
ãã®å ´åãevalå®è¡ãããPHPã³ã¼ãã¯ä»¥ä¸ã«ãªãã¾ãã
$u.str_replace('<br>'...ï¼çç¥ï¼
å ã ã®èå¼±ãªã³ã¼ãã§ã¯ã$uã®å¤ãå±éãããå¾ã«evalå®è¡ããã¾ãããä¸ã®ã³ã¼ãã§ã¯evalå®è¡ã«ãã$uãå±éããã¾ãããã®ããã$uå ã®PHPã³ã¼ããå®è¡ããããã¨ã¯ããã¾ããã
ããããæåãªã®ã¯e修飾åã使ç¨ããªãããã°ã©ã ã«ãããã¨ã§ããç¡çã«1ã¤ã®æã«ãããå¦çãè¤æ°ã®ã¹ãããã«åãã¦ããã°ãe修飾åã使ç¨ããã«åçã®å¦çãå®ç¾ã§ãã¾ãã
çºè¦ãããèå¼±æ§ã®ä¾ï¼
MyBB DomeCode Remote PHP Script Code Injection Vulnerability
phpBB 'viewtopic.php' Remote Code Execution Vulnerabilityï¼Santy Wormã«å©ç¨ãããèå¼±æ§ï¼
ã¿ã¤ãï¼ï¼e修飾åä»ãã»ç¬¬ä¸å¼æ°ã¸ã®æ¿å ¥
3ã¤ç®ã¯ãã¡ãã£ã¨ãããã«ããèå¼±æ§ã§ãã
$s = preg_replace('/\[\[(.*?)\]\]/e', 'addlink("\\1")', $s);
wikiè¨æ³ã«ããããªã³ã¯ãå¦çããããã°ã©ã ã§ãã
å¤é¨ããå¶å¾¡ã§ããã®ã¯ã第ä¸å¼æ°ã®$sã§ããã¿ã¤ãï¼ã»ï¼ã¨ã¯ç°ãªãã第ä¸å¼æ°ã¨ç¬¬äºå¼æ°ã¯åºå®ã§ãã
ã±ã£ã¨è¦ã§ã第äºå¼æ°ã®ã\\1ãã®é¨åã«ã"ããå ¥ããããª$sãä¸ãã¦ããã°ãevalå®è¡ãããã³ã¼ãã®æ§é ãå£ããã¨ãã§ãããã«è¦ãã¾ãããããã¯ããã¾ãããpreg_replaceé¢æ°ã«e修飾åãä»ãå ´åãå¾æ¹åç §ããå¤æ°ã«å«ã¾ããã"ããªã©ã®æåã¯ãPHPã«ããèªåã§addslashesãããããã§ã*1ã
ä¸è¨ã®ããã°ã©ã ã§PHPã³ã¼ããåããããã«ã¯ã$sã«ä»¥ä¸ã®ãããªå¤ãä¸ãã¾ãã
[[{${phpinfo()}}]]
ãã®å ´åãevalå®è¡ãããPHPã³ã¼ãã¯ä»¥ä¸ã«ãªãã¾ãã
addlink("{${phpinfo()}}")
ããã«ãããphpinfoé¢æ°ãå®è¡ãããã®ã§ãããããã¯å°ã å¤ãã¥ããã¨ããã ã¨æãã¾ãã
PHPãPerlã§ã¯ãããã«ã¯ã©ã¼ãã§æ¬ã£ãæååãªãã©ã«å ã«æ¸ãããã$myvarãã®ãããªå¤æ°ãå±éãããã®ã¯ããç¥ããã¦ãã¾ããå®ã¯ãæ¸ãæ¹ã«ãã£ã¦ã¯å¤æ°ã®å±éã ãã§ã¯ãªããé¢æ°ãèµ·åãããã¨ãå¯è½ã§ãã
æååå ã§ã®é¢æ°ã®å®è¡ã¯ãPHPã§ã¯Ver5以ä¸ã®ã¿ã§å¯è½ã§ããä¾ãã°ãã"ãã§æ¬ã£ãæååãªãã©ã«å ã§ã{${myfunc()}}ãã®ããã«æ¸ãã¨ãPHP5ã§ã¯myfuncé¢æ°ãå®è¡ããã¾ãã
æååãã·ã³ã°ã«ã¯ã©ã¼ãã§æ¬ã£ããªãã°ãå¤æ°ã®å±éãé¢æ°ã®å®è¡ãããã¾ãããé¢æ°ãå®è¡ãããããå¤æ°ãå±éããããããã®ã¯ãããã«ã¯ã©ã¼ãã§æ¬ãããæååå ã ãã§ãã
å ã»ã©ã®èå¼±ãªããã°ã©ã ã«è©±ãæ»ãã¾ããå ã»ã©ã®ããã°ã©ã ã¯ãæ¬æ¥ã¯ä»¥ä¸ã®ããã«æ¸ããªããã°ãªãã¾ããã
$s = preg_replace('/\[\[(.*?)\]\]/e',
"addlink('\\1')", $s);
第äºå¼æ°ã®å ¨ä½ã®ã¯ã©ã¼ãæåã¯ã©ã¡ãã§ãããã§ãããå¾æ¹åç §ããå¤æ°ã¯ã·ã³ã°ã«ã¯ã©ã¼ãã'ãã§æ¬ã£ã¦ããå¿ è¦ãããã¾ãã
ãããã¯preg_replaceé¢æ°ã®ãããã«ãpreg_replace_callbacké¢æ°ã使ããã¨ã§ããæå³ããªãã³ã¼ãå®è¡ãé¿ãããã¾ãï¼callbackã®æ¹ã¯å°ã 使ãã¥ããã¨ãããããã¾ããï¼ã
çºè¦ãããèå¼±æ§ã®ä¾ï¼
DokuWiki Remote PHP Script Code Injection Vulnerability
Strawberry 'html.php' Remote Code Execution Vulnerability
ã¾ã¨ã
ã¾ã¨ãã®æåã«æ¸ãã¦ããã¾ãããããããæ®éã®ä½¿ãæ¹ï¼e修飾åç¡ãã§ããã¿ã¼ã³é¨åãåºå®ã«ããï¼ããã¦ããéããã³ã¼ãå®è¡ã®èå¼±æ§ãçãããã¨ã¯ããã¾ãããã¤ã¾ããä¸è¨ã§ç´¹ä»ãããããªèå¼±æ§ã¯ãããªãçããé¨é¡ã®ãã®ã¨ããã¾ããå®éã«CVEã§ãã®æã®èå¼±æ§ãæ¤ç´¢ãã¦ãããããããã®ã¯10件ããªãã¨æãã¾ãã
ããã«ããã¨ãä»®ã«èå¼±æ§ãåå¨ããã¨ãã¦ãããã©ãã¯ããã¯ã¹ï¼ã½ã¼ã¹ã³ã¼ããç¡ãç¶æ ï¼ã§ã®çºè¦ã¯ããªãé£ããã§ã*2ãï¼ã¤åãä¸ããã¿ã¤ãã§è¥å¹²ç°ãªãã¾ãããã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ãããªãç¶æ ã§ããã°ãããã«é£æ度ã¯ä¸ããã¾ãã
ã¨ããããã§ãç¡è¦ãããã¡ãªèå¼±æ§ãªã®ããããã¾ããããä¸ãä¸èå¼±æ§ãåå¨ãã¦ãããçºè¦ã»æªç¨ãããå ´åã®è¢«å®³ã¯ç大ã§ããPHPã®ã³ã¼ããæ¸ããããä»äººã®ã³ã¼ãããã§ãã¯ãããããéã«ã¯ãè¦æ³¨æãªç®æã¨ãããã§ãããã
æ¢ã«èª¬æããããã«ãèå¼±æ§ãåå¨ããå¯è½æ§ãããã®ã¯ããã¿ã¼ã³ãå¤é¨ããå¶å¾¡å¯è½ã§ãã£ãããe修飾åã使ç¨ãã¦ããç®æã§ããå®éã«CVEãªã©ã«ç»é²ããã¦ããèå¼±æ§ãè¦ãã¨ãHTMLã解æã»æä½ãããããªæ¯è¼çé¢åãªããã¹ãå¦çãè¡ãªãå¦çã«åé¡ãæ½ãã§ããã±ã¼ã¹ãè¦ããã¾ããã
æå¾ã«ãpreg_replaceé¢æ°ãã®ãã®ã«ã¤ãã¦å°ãæ¸ãã¾ããä¸è¨ã§è¨ãã¨ã使ãä¸ã§ã®è½ã¨ãç©´ãå¤ãã§ããé¢æ°ã®è¨è¨ããã¾ãã¡ã ã¨æãã¨ãããããã¾ããPerlã®ç½®ææ¼ç®åã¨æ¯ã¹ãã¨*3ã使ãã«ãããªã¼ã¨æã£ã¦ãã¾ãã¾ããã
*1:ãã®ã¨ã¹ã±ã¼ãããæåã¯ãã³ã¼ãå®è¡ã®é²æ¢ã«ã¯å½¹ç«ã¡ã¾ãããããã¨ã¯å¥ã®æ°æã¡ã®æªãåé¡ãããããã¾ããããã¯PHPããã¥ã¢ã«ã®User Contributed Notesã«æ¸ããã¦ãã¾ãã®ã§ãèå³ãããæ¹ã¯è¦ã¦ãã ããã
åæã«ãã®ã¨ã¹ã±ã¼ããããä»æ§ã¯ãexploitã³ã¼ãã®å®è¡ã«ãå½±é¿ãã¾ããååçã«exploitã³ã¼ãã¯ãã'ãã"ãã\ããå«ã¾ãªãããã«ä½æããå¿
è¦ãããã¾ãã
*2:éã«è¨ãã¨ããã¯ã¤ãããã¯ã¹ã§ã¯æ¢ããããèå¼±æ§ã§ãã
*3:PHPã§ã®pregç³»é¢æ°ã¯å¾ä»ãããæ©è½ãªã®ã§ãPerlã¨æ¯è¼ãããã¨èªä½ãé©åã§ã¯ãªãããããã¾ããããã©ããã¦ãæ¯è¼ãã¦ãã¾ãã¾ãã