<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 第二節:XSS攻擊 # XSS攻擊 XSS(Cross Site Script)攻擊又叫做跨站腳本攻擊。他的原理是用戶在使用具有`XSS`漏洞的網站的時候,向這個網站提交一些惡意的代碼,當用戶在訪問這個網站的某個頁面的時候,這個惡意的代碼就會被執行,從而來破壞網頁的結構,獲取用戶的隱私信息等。 ## XSS攻擊場景: 比如`A網站`有一個發布帖子的入口,如果用戶在提交數據的時候,提交了一段`js`代碼比如:`<script>alert("hello world");</script>`,然后`A網站`在渲染這個帖子的時候,直接把這個代碼渲染了,那么這個代碼就會執行,會在瀏覽器的窗口中彈出一個模態對話框來顯示`hello world`!如果攻擊者能成功的運行以上這么一段`js`代碼,那他能做的事情就有很多很多了! ## XSS攻擊防御: 1. 如果不需要顯示一些富文本,那么在渲染用戶提交的數據的時候,直接進行轉義就可以了。在`Django`的模板中默認就是轉義的。也可以把數據在存儲到數據庫之前,就轉義再存儲進去,這樣以后在渲染的時候,即使不轉義也不會有安全問題,示例代碼如下: ``` <pre class="calibre12">``` <span class="hljs-keyword">from</span> django.template.defaultfilters <span class="hljs-keyword">import</span> escape <span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Comment <span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">comment</span><span class="hljs-params">(request)</span>:</span> content = request.POST.get(<span class="hljs-string">"content"</span>) escaped_content = escape(content) Comment.objects.create(content=escaped_content) <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">'success'</span>) ``` ``` 2. 如果對于用戶提交上來的數據包含了一些富文本(比如:給字體換色,字體加粗等),那么這時候我們在渲染的時候也要以富文本的形式進行渲染,也即需要使用`safe`過濾器將其標記為安全的,這樣才能顯示出富文本樣式。但是這樣又會存在一個問題,如果用戶提交上來的數據存在攻擊的代碼呢,那將其標記為安全的肯定是有問題的。示例代碼如下: ``` <pre class="calibre12">``` <span class="hljs-title"># views.py</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span> message = <span class="hljs-string">"<span style='color:red;'>紅色字體</span><script>alert('hello world');</script>"</span>; <span class="hljs-keyword">return</span> render_template(request,<span class="hljs-string">'index.html'</span>,context={<span class="hljs-string">"message"</span>:message}) ``` ``` ``` <pre class="calibre12">``` # index.html ``` ``` 那么這時候該怎么辦呢?這時候我們可以指定某些標簽我們是需要的(比如:span標簽),而某些標簽我們是不需要的(比如:script)那么我們在服務器處理數據的時候,就可以將這些需要的標簽保留下來,把那些不需要的標簽進行轉義,或者干脆移除掉,這樣就可以解決我們的問題了。這個方法是可行的,包括很多線上網站也是這樣做的,在`Python`中,有一個庫可以專門用來處理這個事情,那就是`sanitizer`。接下來講下這個庫的使用。 ## `bleach`庫: `bleach`庫是用來清理包含`html`格式字符串的庫。他可以指定哪些標簽需要保留,哪些標簽是需要過濾掉的。也可以指定標簽上哪些屬性是可以保留,哪些屬性是不需要的。想要使用這個庫,可以通過以下命令進行安裝: ``` <pre class="calibre12">``` pip install bleach ``` ``` 這個庫最重要的一個方法是`bleach.clean`方法,`bleach.clean`示例代碼如下: ``` <pre class="calibre12">``` <span class="hljs-keyword">import</span> bleach <span class="hljs-keyword">from</span> bleach.sanitizer <span class="hljs-keyword">import</span> ALLOWED_TAGS,ALLOWED_ATTRIBUTES <span class="hljs-class">@require_http_methods(['POST'])</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">message</span><span class="hljs-params">(request)</span>:</span> <span class="hljs-title"># 從客戶端中獲取提交的數據</span> content = request.POST.get(<span class="hljs-string">'content'</span>) <span class="hljs-title"># 在默認的允許標簽中添加img標簽</span> tags = ALLOWED_TAGS + [<span class="hljs-string">'img'</span>] <span class="hljs-title"># 在默認的允許屬性中添加src屬性</span> attributes = {**ALLOWED_ATTRIBUTES,<span class="hljs-string">'img'</span>:[<span class="hljs-string">'src'</span>]} <span class="hljs-title"># 對提交的數據進行過濾</span> cleaned_content=bleach.clean(content,tags=tags,attributes=attributes) <span class="hljs-title"># 保存到數據庫中</span> Message.objects.create(content=cleaned_content) <span class="hljs-keyword">return</span> redirect(reverse(<span class="hljs-string">'index'</span>)) ``` ``` 相關介紹如下: 1. `tags`:表示允許哪些標簽。 2. `attributes`:表示標簽中允許哪些屬性。 3. `ALLOWED_TAGS`:這個變量是`bleach`默認定義的一些標簽。如果不符合要求,可以對其進行增加或者刪除。 4. `ALLOWED_ATTRIBUTES`:這個變量是`bleach`默認定義的一些屬性。如果不符合要求,可以對其進行增加或者刪除。 ### bleach更多資料: 1. github地址: <https://github.com/mozilla/bleach> 2. 文檔地址: <https://bleach.readthedocs.io/>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看