Django如何實(shí)現(xiàn)防止XSS攻擊
一、什么是XSS攻擊
xss攻擊:----->web注入
xss跨站腳本攻擊(Cross site script,簡(jiǎn)稱(chēng)xss)是一種“HTML注入”,由于攻擊的腳本多數(shù)時(shí)候是跨域的,所以稱(chēng)之為“跨域腳本”。
我們常常聽(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ù)后寫(xiě)上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ì)渲染而以字符串的形式顯示。如果手賤寫(xiě)了safe,那就危險(xiǎn)了,若想使用safe,那就必須在后臺(tái)對(duì)要渲染的字符串做過(guò)濾了。所以在開(kāi)發(fā)的時(shí)候,一定要慎用安全機(jī)制。尤其是對(duì)用戶可以提交的并能渲染的內(nèi)容!!!
這里是不存在xss漏洞的寫(xiě)法,因?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)為是安全的了,寫(xiě)入 <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. JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁(yè)碼代碼實(shí)例2. 用Spring JMS使異步消息變得簡(jiǎn)單3. 關(guān)于IDEA 2020.3 多窗口視圖丟失的問(wèn)題4. PHP驗(yàn)證碼工具-Securimage5. js實(shí)現(xiàn)碰撞檢測(cè)6. 一文帶你徹底理解Java序列化和反序列化7. ASP基礎(chǔ)知識(shí)VBScript基本元素講解8. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例9. 通過(guò)實(shí)例解析Python文件操作實(shí)現(xiàn)步驟10. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條

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