我無法理解以下JavaScript的作用。 (或者至少試過,因爲它不像其他學生一樣和我一起工作,它一直要求我登錄並且從未顯示歡迎消息)。該實驗室是關於XSS攻擊的,教師告訴我們這個腳本可能被利用,我不知道怎麼做。下面的JavaScript有什麼作用?
不介意評論;我添加它們來幫助我瞭解代碼跟蹤
<html>
<head>
<title>Mobile code:: XSS cookie example </title>
<link rel="stylesheet" type="text/css" href="SecureM.css"/>
</head>
<body>
<script language="Javascript">
function checkCookie(){
//Here is the initialization of variables to be used for this function
var lf = "\n"; //character for next line (lf = line feed)
var CookieString = document.cookie; //cookies are accessible through the document.cookie property
var CookieSet = CookieString.split(';'); //This splits document.cookie on semicolons. CookieSet becomes an array containing all cookies that are set for this domain and path.
var SetSize = CookieSet.length;
var CookiePieces;
var ReturnValue=""; //set default empty return if no cookie found
var x = 0;
for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++){
CookiePieces = CookieSet[x].split('=');
if (CookiePieces[0].substring(0,1) == ' '){
CookiePieces[0] =
CookiePieces[0].substring(1, CookiePieces[0].length);
} //end if statement
if (CookiePieces[0] == "superSecretPassword"){
ReturnValue = CookiePieces[1];
} //end if statement
} //end for loop
return ReturnValue;
} //end checkCookie() function
if (checkCookie() == ""){ // no cookie found, relocate to login.php
window.location.href="login.php";
}
</script>
<?
if (isset($_GET["username"])){
$cur_username = $_GET["username"];
}
else if (isset($_POST["username"])){
$cur_username = $_POST["username"];
}
else if (isset($_COOKIE["username"])){
$cur_username = $_COOKIE["username"];
}
else{
$cur_username = "No Name";
}
echo "<strong>LOGIN SUCCESSFULL!</strong><br/><br/>";
echo "<strong>Thank you, ".$cur_username." your information have been updated</strong>";
//
?>
</body>
</html>
無論如何,有助於解釋這是非常感謝。最後,我希望我的英語不是那麼糟..
編輯:嗯,哇..爲什麼這會被投票下來..?尋求幫助/解釋是如此糟糕?
它似乎主要是脆弱的,因爲超級密碼直接存儲在客戶端可用的cookie中,所以任何用戶都可以訪問它或偷走它。 – adeneo
在PHP部分顯示'$ cur_username',非轉義對XSS易受攻擊。如果一個惡意鏈接被共享爲'yourscript?username ='那麼''將被執行。你需要使用'htmlspecialchars($ cur_username)'來緩解它。 –
我猜這些評論描述了整個代碼。你真的想明白什麼? – 2013-12-15 18:16:38