這篇文章的內(nèi)容主要圍繞Django開發(fā)與攻防測試是怎樣的進(jìn)行講述,文章內(nèi)容清晰易懂,條理清晰,非常適合新手學(xué)習(xí),值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過這篇文章有所收獲!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的太白網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
以前搭博客用的是1.8.2,還在機(jī)子上裝著沒卸,順便拿過來用,當(dāng)然新版本會修復(fù)很多bug,盡可能還是要去學(xué)習(xí)新一些的版本,此篇權(quán)當(dāng)入門篇。
# 下載django
pip install django==1.8.2 -i https://pypi.mirrors.ustc.edu.cn/simple/
# 創(chuàng)建文件夾并啟動虛擬環(huán)境
virtualenv django_demo
cd django_demo
source bin/activate
# 創(chuàng)建存放django文件的文件夾
mkdir learn_django
cd learn_django
# 創(chuàng)建項(xiàng)目
python django-admin.py startproject django_web
# 創(chuàng)建應(yīng)用
python manage.py startapp django_app
# 編輯django_web中的settings.py文件,將django_app加入到apps中
這樣就完成了最基礎(chǔ)的搭建
運(yùn)行服務(wù)看一下
MVC是眾所周知的模式:model(模型)、view(視圖)、controller(控制器)
用戶在頁面輸入url,轉(zhuǎn)交給url控制器,然后根據(jù)url匹配相應(yīng)的視圖函數(shù),viwe會去到models取數(shù)據(jù),然后models在數(shù)據(jù)庫中取得數(shù)據(jù)后返回給視圖,視圖把要展示的數(shù)據(jù)返回給模版,然后就輸出到頁面上。
Django也是一個MVC框架,但是在Django中,控制器接受用戶輸入的部分由框架自行處理,所以django更加關(guān)注的是 模型(model)、view(視圖)、templates(模版),也就是MTV模型。
請求一個url后,匹配相應(yīng)的view區(qū),view去models(一個托管數(shù)據(jù)的層級)查找我們要的數(shù)據(jù),然后將數(shù)據(jù)裝載到templates層,然后呈獻(xiàn)給我們。
兩者很像,可以說MTV基于MVC。
創(chuàng)建模版層
當(dāng)然,要是只想讓簡單的數(shù)據(jù)顯示在Web頁面中,不需要創(chuàng)建模版,直接在views函數(shù)中相應(yīng)回去就可以了,但是還是正規(guī)化一點(diǎn)。
在learn_django中創(chuàng)建templates文件夾(如果是IDE創(chuàng)建的django項(xiàng)目會自動創(chuàng)建),這就是我們的模版文件夾,來添加一個可視化的模版index.html
<!DOCTYPE html>
<html>
<head>
<title>Django Learning</title>
</head>
<body>
<h2>Hellow,Django!</h2>>
</body>
</html>
創(chuàng)建視圖層
視圖層通常來說是一個視圖函數(shù),與url進(jìn)行匹配返回傳入對應(yīng)的Web頁面
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'index.html')
創(chuàng)建url層
創(chuàng)建url層,根據(jù)傳入的url來找到我們的視圖函數(shù),從而將渲染的模版返回
from django.conf.urls import include, url
from django.contrib import admin
from django_app.views import index
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
# 用正則去對url進(jìn)行匹配,然后將視圖函數(shù)匹配給相應(yīng)的url
url(r'^index/',index),
運(yùn)行服務(wù),默認(rèn)在8000端口
python manage.py runserver
到這一步有一部分同學(xué)會有一些小問題,那就是并不能返回模版,可能Windows和linux情況各不相同,linux需要把templates目錄放在app目錄下才可以找到。
原因就在于settings.py中模版路徑設(shè)置問題,如果templates目錄是放在項(xiàng)目根目錄,在settings中將templates路徑加入就可以了。
前邊是說靜態(tài)頁面,如果需要實(shí)現(xiàn)動態(tài),那就不得不說與數(shù)據(jù)庫存儲的交互問題,需要對models進(jìn)行對應(yīng)的編寫來取得數(shù)據(jù)。
MySQL + django
安裝對應(yīng)的數(shù)據(jù)庫接口驅(qū)動,這里大致有三種:mysqldb、pymysql、mysqlclient。
默認(rèn)使用web根目錄下的sqlite3數(shù)據(jù)庫
將其修改為相應(yīng)的mysql信息
創(chuàng)建mysql數(shù)據(jù)庫,指定字符集為UTF-8
models層
創(chuàng)建模型的對象和數(shù)據(jù)庫字段的對應(yīng)關(guān)系
字段定義中的特殊屬性
from django.db import models
# Create your models here.
class DjangoTest(models.Model):
text = models.CharField(max_length=20)
生成遷移文件并執(zhí)行遷移
python manage.py makemigrations
python manage.py migrate
查看創(chuàng)建的信息
建立一些測試數(shù)據(jù)
在去創(chuàng)建views層之前,我們先對models層進(jìn)行測試,看是否提取出了數(shù)據(jù)
可以用兩種方法:
1、直接在models中填寫提取數(shù)據(jù)
也可以寫在view層,這個無所謂
運(yùn)行后可能會出現(xiàn),因?yàn)樵陧?xiàng)目中單獨(dú)運(yùn)行python文件,需要搜索環(huán)境變量,而并沒有指定,所以需要進(jìn)行設(shè)置
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS.....
pycharm解決方法:https://blog.csdn.net/u011013781/article/details/52155761
2、使用django shell
# 打開django shell
python manage.py shell
import django
django.setup()
from django_app.models imoort DjangoTest as dj
# 添加數(shù)據(jù),結(jié)果見下方圖片
a = dj(text="django shell test")
a.save()
# 查詢數(shù)據(jù),get查詢單個數(shù)據(jù),filter查詢多個模型,all查詢
dj.objects.all()
django admin可以幫我們快速管理后臺數(shù)據(jù)
# 創(chuàng)建管理員
python manage.py createsuperuser
```

將我們的模型注冊到admin中,打開admin.py
將我們的模型注冊到admin中,打開admin.py
from django.contrib import admin
# Register your models here.
from models import DjangoTest
admin.site.register(DjangoTest)
這樣就可以在管理員界面管理模型
view與url層
當(dāng)用戶請求django站點(diǎn)上的某個頁面時(shí),django會使用路由解析模塊來解析路由,默認(rèn)是app目錄下的urls.py。
django加載該路由解析模塊,并尋找可用的urlpatterns,這是一個python列表,然后django依次匹配列表中每個url模式,在遇到第一個與請求相匹配的模式時(shí)停下來,然后調(diào)用對應(yīng)的視圖,視圖是一個python函數(shù)(或者是一個基于類的視圖)。
一個簡單的路由選擇模塊示例:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
與之對應(yīng)的請求的例子:
對/articles/2005/03/ 的請求將匹配列表中的第三個模式。Django 將調(diào)用函數(shù)views.month_archive(request, '2005', '03')。
/articles/2005/3/ 不匹配任何URL 模式,因?yàn)榱斜碇械牡谌齻€模式要求月份應(yīng)該是兩個數(shù)字。
/articles/2003/ 將匹配列表中的第一個模式不是第二個,因?yàn)槟J桨错樞蚱ヅ洌谝粋€會首先測試是否匹配。請像這樣自由插入一些特殊的情況來探測匹配的次序。
/articles/2003 不匹配任何一個模式,因?yàn)槊總€模式要求URL 以一個斜線結(jié)尾。
/articles/2003/03/03/ 將匹配最后一個模式。Django 將調(diào)用函數(shù)views.article_detail(request, '2003', '03', '03')。
根據(jù)url后邊的數(shù)值,賦予相應(yīng)的參數(shù):id
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django_app.views import index
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^index/(?P<id>[0-9])/$', index),
]
將對于的id參數(shù)代入查詢獲取數(shù)據(jù)列表中指定索引的值(忽略這的一點(diǎn)小細(xì)節(jié)錯誤)
views.py
from django.shortcuts import render
from django_app.models import DjangoTest
# Create your views here.
def index(request,id):
text = DjangoTest.objects.all()
iden = int(id)
context = {'text':text[iden]}
return render(request,'index.html',context)
模版層
模版層語法參考
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django test</title>
</head>
<body>
<h2>ID: {{ text.id }}</h2>
<h2>Hello:{{ text.text }}
</body>
</html>
實(shí)際運(yùn)行效果
修復(fù)前
from django.shortcuts import render
from django.contrib.auth import authenticate, login
import django
django.setup()
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request,'index.html')
else:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username,password=password)
if user is not None:
if user.is_active:
login(request, user)
template = 'Hello {user}! , You can set your email is:' + request.POST.get('email')
return render(request, 'index.html', {"value": template.format(user=request.user)})
else:
info = "The password is valid, but the account has been disabled!"
return render(request, 'index.html', {"value": info})
else:
info = "The username and password were incorrect."
return render(request, 'index.html', {"value": info})
在原來的代碼上變動了一下,如果不增加認(rèn)證選項(xiàng),返回的用戶永遠(yuǎn)是匿名用戶。
在前邊我已經(jīng)創(chuàng)建了一個超級用戶(admin admin),所以直接用這個用戶來進(jìn)行認(rèn)證。
認(rèn)證之后返回登錄用戶的用戶名,我們可以自己通過post方法傳入一個郵箱地址上去作為臨時(shí)地址,如果用戶名信息出現(xiàn)任何錯誤,返回相應(yīng)的錯誤信息。
使用django認(rèn)證系統(tǒng)
User對象是認(rèn)證系統(tǒng)的核心,默認(rèn)user的基本屬性有username、password、email.....
代碼中郵箱信息直接通過format拼接在了字符串中,然后展示在頁面里,我們可以通過以下payload來獲取敏感數(shù)據(jù),將user變量中的password屬性 作為變量信息進(jìn)行拼接,從而進(jìn)行獲取
payload: {user.password}
修復(fù)后
index.html
將其他的文本信息直接存放在模版中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Django test</title> </head> <body> <h2>Hello, Django!</h2> <h3>hello {{ user }}, You can set your email is:{{ value }}</h3> </body> </html> views.py ..... email = request.POST.get('email') return render(request, 'index.html', {"value": email}) ...
測試與修復(fù)前
只是簡單的接收post參數(shù)值,然后讓其顯示在頁面上
views.py def index(request): if request.method == 'GET': return render(request,'index.html') else: info = request.POST.get('info') return render(request,'index.html',{"value":info}) index.html <h2>Hello, Django!</h2> <h3>{{ value }}</h3>
當(dāng)鍵入payload時(shí),并沒有預(yù)想的彈窗,因?yàn)閐jango自動為開發(fā)者提供了escape功能,讓html代碼在render之前先進(jìn)行轉(zhuǎn)義,然后再顯示出來。
除了自動開啟的escape,還有safe、autoescape、make_Safe等
autoescape測試
{% autoescape off %}
<h3>{{ value }}</h3>
{% endautoescape %}
當(dāng)其值為off時(shí),即存在xss漏洞
safe測試
<h2>Hello, Django!</h2> <h3>{{ value | safe }}</h3>
通過safe關(guān)閉了模版的安全機(jī)制,出現(xiàn)XSS漏洞
還有幾種情況也可能存在XSS:
1、var mystr = "\{ \{ value | escapejs \} \}"
2、safe、make_safe、autoescape
3、DOM型XSS
4、HttpResponse返回動態(tài)內(nèi)容
修復(fù)后
import cgi
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request,'index.html')
else:
info = request.POST.get('info')
info = cgi.escape(info)
return render(request,'index.html',{"value":info})
使用cgi模塊需要注意:
設(shè)為True,讓其轉(zhuǎn)義盡可能多的導(dǎo)致逃逸的字符。
Django QuerySet
查看django queryset執(zhí)行的SQL
from django_app.models import DjangoTest as dj
print dj.objects.all().query
得到
SELECT `django_app_djangotest`.`id`, `django_app_djangotest`.`text` FROM `django_app_djangotest`
簡化之后就是
SELECT id,text FROM django_app_djangotest;
extra實(shí)現(xiàn)別名、條件、排序等
以select為例:
tag = dj.objects.all().extra(select={"tag_id":'id'})
以where為例:
extra里的允許當(dāng)前的where參數(shù)可以使用原聲的sql語句進(jìn)行查詢。
條件為id=1,結(jié)果即查詢出了一條數(shù)據(jù)
raw方法實(shí)現(xiàn)原生的SQL語句查詢
a = dj.objects.raw('SELECT id,text FROM django_app_djangotest ')
raw()方法支持索引訪問(a[0])
也可以打印當(dāng)前賦予的這個變量a都有哪些方法
直接利用API來查詢數(shù)據(jù)
django.connection
MySQL API
諸如mysqldb、pymysql、mysqlclient,在views層寫好sql語句,根據(jù)傳入的參數(shù)值來查詢,得出結(jié)果后返回給模版就可以了
修復(fù)前
views.py
from django.shortcuts import render
from django_app.models import DjangoTest as dj
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request,'index.html')
else:
id = request.POST.get('id')
tag = dj.objects.extra(where={'id={}'.format(id)})[0].text
return render(request,'index.html',{"value":tag})
接下來就可以進(jìn)行愉快的測試了
測試篇
a = dj.objects.extra(where={'id=1'})
SELECT `django_app_djangotest`.`id`, `django_app_djangotest`.`text` FROM `django_app_djangotest` WHERE (id=1asdasdad)
先輸入payload查看django傳遞回?cái)?shù)據(jù)庫的sql語句是什么
更改后的payload
后邊又構(gòu)造測試了幾個,SQL語句是正確,但是django傳入SQL語句時(shí)會提示里邊的語法問題,并且就算語法正確,也返回不了數(shù)據(jù)。(這其實(shí)有點(diǎn)問題,后邊做完一想,沒拿到mysql shell里邊去測,終端里邊測對了,再拿過來,這里有點(diǎn)懶沒再弄)
又因?yàn)閕d的值,從而在頁面中顯示不出來,所以這時(shí)候想到了延時(shí)注入
在這里調(diào)用a的時(shí)候會延時(shí)3秒
我們在Web頁面中進(jìn)行測試
關(guān)于這里的秒數(shù)是成倍關(guān)系,以前看到過一篇帖子,說是當(dāng)時(shí)間滿足出現(xiàn)成倍的關(guān)系時(shí),應(yīng)該是查詢出了多條數(shù)據(jù),每一個row執(zhí)行一次延時(shí)。
接下來就好辦了
x = dj.objects.extra(where={"id=1 and if(substr((select user()),1,1)='r',sleep(3),1)"})
后邊的步驟跟著盲注的流程走就OJBK了。
有時(shí)候不要直接在django shell中執(zhí)行,先去mysql命令行把命令敲對了,也確實(shí)可以執(zhí)行payload時(shí)候再回來測試,確保第一步先正確。
剛才說了的只是extra中的where子句,其他類型的數(shù)據(jù)提取方法在前邊也有說過了,具體案例具體分析
修復(fù)后
views.py
from django.shortcuts import render
from django_app.models import DjangoTest as dj
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request,'index.html')
else:
id = request.POST.get('id')
tag = dj.objects.extra(where=['id=%s'],params=id)
info = tag[0].text
return render(request,'index.html',{"value":info})
用戶發(fā)送的請求再匹配到視圖函數(shù)進(jìn)行處理時(shí),視圖函數(shù)的相關(guān)機(jī)制會對敏感信息進(jìn)行處理,導(dǎo)致一些惡意語句被過濾
現(xiàn)在測試就不會了
感謝你的閱讀,相信你對“Django開發(fā)與攻防測試是怎樣的”這一問題有一定的了解,快去動手實(shí)踐吧,如果想了解更多相關(guān)知識點(diǎn),可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站!小編會繼續(xù)為大家?guī)砀玫奈恼拢?/p>
文章名稱:Django開發(fā)與攻防測試是怎樣的
分享URL:http://chinadenli.net/article38/pgocsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站建設(shè)、建站公司、面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)