Django中避免sql注入的方法:1、對使用者的輸入進行校驗;2、不要使用動態拼裝sql;3、不要把機密資訊直接存放;4、應用的異常資訊應該給盡可能少的提示;5、利用Dajngo的ORM來有效避免sql注入。
什麼是SQL注入?
所謂SQL注入,就是透過把SQL指令插入到Web表單提交或輸入網域或頁面請求的查詢字串,最後達到欺騙伺服器執行惡意的SQL指令。具體來說,它是利用現有應用程序,將(惡意的)SQL命令注入到後台資料庫引擎執行的能力,它可以透過在Web表單中輸入(惡意)SQL語句得到一個存在安全漏洞的網站上的資料庫,而不是依照設計者意圖去執行SQL語句。例如先前的許多影視網站外洩VIP會員密碼大多就是透過WEB表單遞交查詢字元暴出的,這類表單特別容易受到SQL注入式攻擊。
例如現在資料庫中有一個front_user表,表結構如下:
class User(models.Model): telephone = models.CharField(max_length=11) username = models.CharField(max_length=100) password = models.CharField(max_length=100)
#然後我們使用原生sql語句實作以下需求:
1.實作一個根據使用者id獲取使用者詳情的視圖。範例程式碼如下:
def index(request): user_id = request.GET.get('user_id') cursor = connection.cursor() cursor.execute('select id,username from front_user where id=%s' % user_id) rows = cursor.fetchall() for row in rows: print(row) return HttpResponse('success')
這樣表面上看起來沒有問題。但如果使用者傳的user_id是等於1 or 1=1,那麼以上拼接後的sql語句為:
select id,username from front_user where id=1 or 1=1
以上sql語句的條件是id=1 or 1=1,只要id=1或是1=1兩個有一個成立,那麼整個條件就成立。毫無疑問1=1
是肯定成立的。因此執行完以上sql語句後,會將front_user表中所有的資料都提取出來。
2. 實作一個根據使用者的username來提取使用者的檢視。範例程式碼如下:
def index(request): username = request.GET.get('username') cursor = connection.cursor() cursor.execute('select id,username from front_user where username='%s'' % username) rows = cursor.fetchall() for row in rows: print(row) return HttpResponse('success')
這樣表面上看起來也沒有問題。但如果使用者傳的username是zhiliao' or '1=1,那麼以上拼接後的sql語句為:
select id,username from front_user where username='zhiliao' or '1=1'
以上sql語句的條件是username='zhiliao'或是一個字串,毫無疑問,字串的判斷是肯定成立的。因此會將front_user表中所有的資料都提取出來。
sql注入防禦,歸類起來主要有以下幾點:
以上便是sql注入的原理。他透過傳遞一些惡意的參數來破壞原有的sql語句以便達到自己的目的。當然sql注入遠遠沒有這麼簡單,我們現在講到的只是冰山一角。那麼如何防禦sql注入呢?
1. 永遠不要信任使用者的輸入。對使用者的輸入進行校驗,可以透過正規表示式,或限制長度;對單引號和 雙'-'進行轉換等。
2. 永遠不要使用動態拼裝sql,可以使用參數化的sql或直接使用預存程序進行資料查詢存取。例如:
def index(request): user_id = '1 or 1=1' cursor = connection.cursor() cursor.execute('select id,username from front_user where id=%s',(user_id,)) rows = cursor.fetchall() for row in rows: print(row) return HttpResponse('success')
3. 永遠不要使用管理員權限的資料庫連接,為每個應用程式使用單獨的權限有限的資料庫連接。
4. 不要把機密資訊直接存放,加密或hash掉密碼和敏感的資訊。
5. 應用程式的異常訊息應該給出盡可能少的提示,最好使用自訂的錯誤訊息對原始錯誤訊息進行包裝。
總結:
1. 在網頁中利用sql語句進行注入攻擊,網頁取得使用者輸入參數,但有些惡意使用者利用特殊sql語句上傳參數,後端獲取參數若不對其正確性合法性進行判斷,則有可能對資料庫造成危害
2. get和post上傳資料的時候,做好對參數的檢查
#3.利用Dajngo的ORM可有效避免sql注入,因為Django已經對特殊字元進行轉義
以上是django如何避免sql注入的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!