Django如何實(shí)現(xiàn)防止XSS攻擊
一、什么是XSS攻擊
xss攻擊:----->web注入
xss跨站腳本攻擊(Cross site script,簡(jiǎn)稱xss)是一種“HTML注入”,由于攻擊的腳本多數(shù)時(shí)候是跨域的,所以稱之為“跨域腳本”。
我們常常聽(tīng)到“注入”(Injection),如SQL注入,那么到底“注入”是什么?注入本質(zhì)上就是把輸入的數(shù)據(jù)變成可執(zhí)行的程序語(yǔ)句。SQL注入是如此,XSS也如此,只不過(guò)XSS一般注入的是惡意的腳本代碼,這些腳本代碼可以用來(lái)獲取合法用戶的數(shù)據(jù),如Cookie信息。
PS: 把用戶輸入的數(shù)據(jù)以安全的形式顯示,那只能是在頁(yè)面上顯示字符串。
django框架中給數(shù)據(jù)標(biāo)記安全方式顯示(但這種操作是不安全的!):
- 模版頁(yè)面上對(duì)拿到的數(shù)據(jù)后寫上safe. ----> {{XXXX|safe}} - 在后臺(tái)導(dǎo)入模塊:from django.utils.safestring import mark_safe把要傳給頁(yè)面的字符串做安全處理 ----> s = mark_safe(s)
二、測(cè)試代碼
實(shí)施XSS攻擊需要具備兩個(gè)條件:
一、需要向web頁(yè)面注入惡意代碼;
二、這些惡意代碼能夠被瀏覽器成功的執(zhí)行。
解決辦法:
1、一種方法是在表單提交或者url參數(shù)傳遞前,對(duì)需要的參數(shù)進(jìn)行過(guò)濾。
2、在后臺(tái)對(duì)從數(shù)據(jù)庫(kù)獲取的字符串?dāng)?shù)據(jù)進(jìn)行過(guò)濾,判斷關(guān)鍵字。
3、設(shè)置安全機(jī)制。
django框架:內(nèi)部機(jī)制默認(rèn)阻止了。它會(huì)判定傳入的字符串是不安全的,就不會(huì)渲染而以字符串的形式顯示。如果手賤寫了safe,那就危險(xiǎn)了,若想使用safe,那就必須在后臺(tái)對(duì)要渲染的字符串做過(guò)濾了。所以在開(kāi)發(fā)的時(shí)候,一定要慎用安全機(jī)制。尤其是對(duì)用戶可以提交的并能渲染的內(nèi)容?。?!
這里是不存在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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. IntelliJ IDEA安裝插件的方法步驟3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. spring acegi security 1.0.0 發(fā)布6. MyBatis中的JdbcType映射使用詳解7. vue 使用localstorage實(shí)現(xiàn)面包屑的操作8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備