Python sql注入 過濾字符串的非法字符實(shí)例
我就廢話不多說了,還是直接看代碼吧!
#coding:utf8 #在開發(fā)過程中,要對前端傳過來的數(shù)據(jù)進(jìn)行驗(yàn)證,防止sql注入攻擊,其中的一個(gè)方案就是過濾用戶傳過來的非法的字符 def sql_filter(sql, max_length=20): dirty_stuff = ["\"", "\\", "/", "*", "'", "=", "-", "#", ";", "<", ">", "+", "%", "$", "(", ")", "%", "@","!"] for stuff in dirty_stuff: sql = sql.replace(stuff, "") return sql[:max_length] username = "1234567890!@#!@#!@#$%======$%" username = sql_filter(username) # SQL注入 print username # 輸出結(jié)果是:1234567890
補(bǔ)充知識:python解決sql注入以及特殊字符
python往數(shù)據(jù)庫插入數(shù)據(jù),
基礎(chǔ)做法是:
cur=db.cursor() sql = "INSERT INTO test2(cid, author, content) VALUES (1, '1', 'aa')" cur.execute(sql,())
也可以這樣:
cur=db.cursor() sql = "INSERT INTO test2(cid, author, content) VALUES (%s, '%s', '%s')" sql=sql%('2','2','bb') cur.execute(sql,())
但是當(dāng)含有特殊一點(diǎn)的字符時(shí)就有問題了,比如單引號,%等,甚至?xí)籹ql注入。
和其他語言一樣,python也他的方法來解決sql注入。
cur=db.cursor() sql = "INSERT INTO test2(cid, author, content) VALUES (%s, %s, %s)" cur.execute(sql,('3','3','c%c'))
注意,后面2個(gè)%s的前后單引號去掉了。
結(jié)果如下:
以上這篇Python sql注入 過濾字符串的非法字符實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 使用raw socket進(jìn)行TCP SYN掃描實(shí)例
這篇文章主要介紹了python 使用raw socket進(jìn)行TCP SYN掃描實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

python實(shí)現(xiàn)的DES加密算法和3DES加密算法實(shí)例
![Python pandas 的索引方式 data.loc[],data[][]示例詳解](http://img.jbzj.com/images/xgimg/bcimg5.png)
Python pandas 的索引方式 data.loc[],data[][]示例詳解

Python實(shí)現(xiàn)自動(dòng)發(fā)送測試報(bào)告郵件的示例代碼