網站弱點掃描、原始碼掃描經驗談
2022/07/16
XSS相關Issue
主要處理方式是避免在使用JavaScript的appendChild或innerHtml功能時
濾掉所有script tag避免執行JavaScript程式碼
除了tinymce本身在前端濾掉script tag的功能
後端也有另外過濾處理
處理方式
後端用preg_replace
當掃描到內容有script tag的時候
整串移除避免任何XSS風險
<?php
if (!function_exists('replaceStr')) {
function replaceStr($data) {
try {
$data = preg_replace("/<(script.*?)>(.*?)<(.*?script.*?)>/si","", $data);
$data = preg_replace("/alert(\(.*?)\)/si","", $data);
$data = str_replace( 'this.' , '' , $data);
$bad = array(
'document.cookie' => '',
'document.write' => '',
'window.location' => '',
"javascript\s*:" => '',
"Redirect\s+302" => '',
);
foreach ($bad as $key => $val){
$data = preg_replace("#" . $key . "#i", $val, $data);
}
} catch (\Exception $exception) {
return null;
}
return $data;
}
}
File Disclosure、Path Traversal
當php使用file_get_contents method時
若此method帶入的參數沒特別處理或過濾
將會處先此issue
原因是攻擊者可能會透過此method隨意帶入任何路徑取得server內的任何檔案
例如
<?php
file_get_contents($path);
解決方式
在將$path參數傳入file_get_contents時必須強制驗證不可亂帶或是不要使用變數
若是使用file_get_contents來做curl get功能
必須要驗證帶入的參數一定是的url格式
Missing HSTS header
HSTS: HTTP Strict Transport Security
必須在網站的Response加入header Strict-Transport-Security', 'max-age=31536000; includeSubdomains
Use Of Hardcoded Password
當弱掃工具掃描到password字眼
並且發現程式碼是直接將字串assign給password
而不是使用變數的方式賦予
將會出現此風險
範例
<?php
$password = 'foobar';
解決方式
透過環境變數或其他變數方式assign給password即可
<?php
$password = env('DEFAULT_PASSWORD');
Unsafe use of target blank
當弱掃工具偵測到連結的href參數使用變數
將會產生此錯誤
<a :href="url" target="_blank"></a>
此風險屬於低風險
若無必要可忽略
Client dom open redirect
同Unsafe use of target blank風險