X00代碼審計之旅
剛接觸代碼審計,所以找了個入門級別的php源碼熊海cms進行審計。如果大佬發現有錯誤請告知哦~
熊海cms下載地址:http://js.down.chinaz.com/201503/xhcms_v1.0.rar
使用的是Seay審計工具,聽說誤報常用。但是作爲新手,也可以培養自己檢測誤報的能力
開始實驗吧~ (先自動審計一波)
X01文件包含
舉以下栗子 /index.php與/admin/index.php
<?php
//單一入口模式
error_reporting(0); //關閉錯誤顯示
$file=addslashes($_GET['r']); //接收文件名
$action=$file==''?'index':$file; //判斷爲空或者等於index
include('files/'.$action.'.php'); //載入相應文件
?>
問題:
這裏出現include()函數,存在一個非常明顯的文件包含漏洞,參數r只經過了addslashes()的過濾。
addslashes() 函數返回在預定義字符之前添加反斜槓的字符串。
預定義字符是:單引號(’)雙引號(")反斜槓(\)NULL
利用:
我們在根目錄下創建phpinfo.php文件,內容爲<?php phpinfo(); ?>,在url裏提交?r=…/phpinfo,程序添加.php後綴名後產生了文件包含。
X02注入
舉以下栗子 /admin/files/adset.php
<form class="form-horizontal" role="form" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label class="col-lg-4 control-label">廣告一</label>
<div class="col-lg-8">
<textarea name="ad1" class="form-control col-lg-12" placeholder="ad-1"><?php echo $ad['ad1']?></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">廣告二</label>
<div class="col-lg-8">
<textarea name="ad2" class="form-control col-lg-12" placeholder="ad-2"><?php echo $ad['ad2']?></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">廣告三</label>
<div class="col-lg-8">
<textarea name="ad3" class="form-control col-lg-12" placeholder="ad-2"><?php echo $ad['ad3']?></textarea>
</div>
</div>
#......
#......
$ad1=addslashes($_POST['ad1']);
$ad2=addslashes($_POST['ad2']);
$ad3=addslashes($_POST['ad3']);
if ($save==1){
$query = "UPDATE adword SET
ad1='$ad1',
ad2='$ad2',
ad3='$ad3',
date=now()";
問題:
delete函數中不存在參數及引號保護過濾,導致注入
利用:
1’ or updatexml(1,concat((select concat(0x7e,[可爆字段],0x7e) from [已知表])),0) #
X03文件任意讀取
舉以下栗子 /files/downloads.php
```php
$fileadd=$down['softadd'];
$sourceFile = $fileadd; //要下載的臨時文件名
$fp = fopen("$sourceFile", "rb");
//設置指針位置
fseek($fp, $range);
//虛幻輸出
while (!feof($fp)) {
//設置文件最長執行時間
set_time_limit(0);
print (fread($fp, 1024 * 8)); //輸出文件
flush(); //輸出緩衝
ob_flush();
}
fclose($fp);
問題:
沒進行參數對路徑的限制和過濾
利用:
如果猜解出敏感文件或已知文件,可導致信息泄露的安全問題
X04任意文件上傳
舉以下栗子 /inc/up.class.php
/////////////////////////////////////////////////////////////////////////////////////////////////
if ( move_uploaded_file( $this -> uploadFile[ 'tmp_name' ] , realpath( $this -> accessPath ) . "/" . $newFileName ) )
{
$this -> newFileName = $newFileName;
return true;
}else{
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
}
function CheckFileExist( $path = NULL)
{
return ($path == NULL) ? false : ((file_exists($path)) ? true : false);
}
function GetFileMIME()
{
return $this->GetFileTypeToString();
}
function CheckFileMIMEType()
{
$pass = false;
$defineTypeList = strtolower( $this ->defineTypeList);
$MIME = strtolower( $this -> GetFileMIME());
if (!empty ($defineTypeList))
{
if (!empty ($MIME))
{
foreach(explode("|",$defineTypeList) as $tmp)
{
if ($tmp == $MIME)
{
$pass = true;
}
}
}
else
{
return false;
}
}
else
{
return false;
}
return $pass;
}
function GetFileTypeToString()
{
if( ! empty( $this -> uploadFile[ 'name' ] ) )
{
return substr( strtolower( $this -> uploadFile[ 'name' ] ) , strlen( $this -> uploadFile[ 'name' ] ) - 3 , 3 );
}
}
}
?>
問題:
存在任意文件上傳漏洞。
explode()函數,使用一個字符串分割另一個字符串,並返回由字符串組成的數組。
substr()函數,返回字符串的一部分。
strtolower()函數,把字符串轉換爲小寫。並不能過濾阻止任意文件的上傳漏洞。
利用:
傳小馬,getshell
X05xss
舉以下栗子 /seacmseditor/php/controller.php
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode(array(
'state'=> 'callback參數不合法'
));
}
} else {
echo $result;
}
誤報: htmlspecialchars轉義成爲了html實體
X06越權訪問
舉以下栗子 inc/checklogin.php
<?php
$user=$_COOKIE['user'];
if ($user==""){
header("Location: ?r=login");
exit;
}
?>
利用:若猜解出管理員權限的user參數值,即可實現越權訪問
**************************************************************
以上是我暫有能力範圍內在此cms上找到找出常見的漏洞…