Django如何實(shí)現(xiàn)防止XSS攻擊
一、什么是XSS攻擊
xss攻擊:----->web注入
xss跨站腳本攻擊(Cross site script,簡稱xss)是一種“HTML注入”,由于攻擊的腳本多數(shù)時(shí)候是跨域的,所以稱之為“跨域腳本”。
我們常常聽到“注入”(Injection),如SQL注入,那么到底“注入”是什么?注入本質(zhì)上就是把輸入的數(shù)據(jù)變成可執(zhí)行的程序語句。SQL注入是如此,XSS也如此,只不過XSS一般注入的是惡意的腳本代碼,這些腳本代碼可以用來獲取合法用戶的數(shù)據(jù),如Cookie信息。
PS: 把用戶輸入的數(shù)據(jù)以安全的形式顯示,那只能是在頁面上顯示字符串。
django框架中給數(shù)據(jù)標(biāo)記安全方式顯示(但這種操作是不安全的!):
- 模版頁面上對(duì)拿到的數(shù)據(jù)后寫上safe. ----> {{XXXX|safe}} - 在后臺(tái)導(dǎo)入模塊:from django.utils.safestring import mark_safe把要傳給頁面的字符串做安全處理 ----> s = mark_safe(s)
二、測(cè)試代碼
實(shí)施XSS攻擊需要具備兩個(gè)條件:
一、需要向web頁面注入惡意代碼;
二、這些惡意代碼能夠被瀏覽器成功的執(zhí)行。
解決辦法:
1、一種方法是在表單提交或者url參數(shù)傳遞前,對(duì)需要的參數(shù)進(jìn)行過濾。
2、在后臺(tái)對(duì)從數(shù)據(jù)庫獲取的字符串?dāng)?shù)據(jù)進(jìn)行過濾,判斷關(guān)鍵字。
3、設(shè)置安全機(jī)制。
django框架:內(nèi)部機(jī)制默認(rèn)阻止了。它會(huì)判定傳入的字符串是不安全的,就不會(huì)渲染而以字符串的形式顯示。如果手賤寫了safe,那就危險(xiǎn)了,若想使用safe,那就必須在后臺(tái)對(duì)要渲染的字符串做過濾了。所以在開發(fā)的時(shí)候,一定要慎用安全機(jī)制。尤其是對(duì)用戶可以提交的并能渲染的內(nèi)容?。。?/p>
這里是不存在xss漏洞的寫法,因?yàn)閐jango已經(jīng)做了防攻擊措施
index.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>評(píng)論</h1>{% for item in msg %}{# <div>{{ item|safe }}</div>#} #這里被注釋的,是因?yàn)?,|safe 加了這個(gè)就認(rèn)為是安全的了,寫入 <script> alert(123)</script> 就會(huì)惡意加載 <div>{{ item}}</div>{% endfor %}</body></html>
conment.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/comment/' method='POST'> <input type='text' name='content'> <input type='submit' value='提交'></form></body></html>
views.py
from django.shortcuts import render,HttpResponse# Create your views here.msg = []def comment(request): if request.method == 'GET': return render(request,'comment.html') else: v = request.POST.get('content') msg.append(v) return render(request,'comment.html')def index(request): return render(request,'index.html',{'msg':msg})########################################################def test(request): from django.utils.safestring import mark_safe temp = '<a href=’http://www.baidu.com’>百度</a>' newtemp = mark_safe(temp) #這里相當(dāng)于加了 |safe ,把字符串認(rèn)為是安全的,執(zhí)行代碼,如果不加 test.html里面 {{ temp }} 就只會(huì)顯示出字符串,而不是 a 標(biāo)簽 return render(request,’test.html’,{’temp’:newtemp})
urls.py
from app01 import viewsurlpatterns = [ url(r’^admin/’, admin.site.urls), url(r’^index/’, views.index), url(r’^comment/’,views.comment),]
------------------------------------######################_-------------------------------
以下是做了用戶輸入判斷,檢測(cè)是否有特殊字符
views.py
from django.shortcuts import render,HttpResponse# Create your views here.msg = []def comment(request): if request.method == 'GET': return render(request,'comment.html') else: v = request.POST.get('content') if 'script' in v: return render(request, 'comment.html',{’error’:’小比崽子’}) else: msg.append(v) return render(request,’comment.html’)def index(request): return render(request,'index.html',{'msg':msg})
index.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>評(píng)論</h1>{% for item in msg %} <div>{{ item|safe }}</div>{# <div>{{ item}}</div>#}{% endfor %}</body></html>
comment.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/comment/' method='POST'> <input type='text' name='content'> <input type='submit' value='提交'>{{ error }}</form></body></html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效2. 告別AJAX實(shí)現(xiàn)無刷新提交表單3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. XML入門精解之結(jié)構(gòu)與語法5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. uni-app低成本封裝一個(gè)取色器組件的簡單方法7. 解析原生JS getComputedStyle8. CSS3中Transition屬性詳解以及示例分享9. UDDI FAQs10. 使用css實(shí)現(xiàn)全兼容tooltip提示框
