(9)SQL的注入(致命的漏洞)

2022-08-14 00:36:15 字數 1789 閱讀 4812

使用者登陸**的時候進行賬戶驗證輸入特殊的格式和字元會觸發乙個漏洞,不需要密碼直接登入成功

import pymysql

username = input('請輸入賬號: ')

password = input('請輸入密碼: ')

conn = pymysql.connect(host='localhost',user='root',password='',database='company',port=3306,charset='utf8',cursorclass=pymysql.cursors.dictcursor)

cursor = conn.cursor()

cursor.execute("select * from userinfo where name='%s'and password='%s'"%(username,password))

res = cursor.fetchall()

if res:

print(res)

else:

print('賬號密碼錯誤')

使用者在輸入賬號的使用使用這種格式: 

1、使用者名稱+空格+ ' + 空格 + #, #知道使用者名稱的情況下以這種格式進行登陸驗證,密碼隨意填寫,就會觸發bug,使用者的驗證通過,並不需要密碼

2、任意字母 + 空格 + ' + or + 空格 + 1=1 + 空格 + #  #這種格式不需要賬戶和密碼,能夠直接通過資料庫驗證

ps:注入的原理就是在使用者名稱的後面加 #,這樣值在傳入指令的時候後面跟了乙個 #,#後面所有的**都被注釋掉,這樣值模仿判定**就會生效

解決注入漏洞的辦法

import pymysql

username = input('請輸入賬號: ')

password = input('請輸入密碼: ')

conn = pymysql.connect(host='localhost',user='root',password='',database='company',port=3306,charset='utf8',cursorclass=pymysql.cursors.dictcursor)

cursor = conn.cursor()

'''避免注入,將指令放入變數中'''

sql = "select * from userinfo where name=%sand password=%s"

'''將執行sql語句的時候將值以元組的形式傳入,python就會自動轉義,去掉#號'''

cursor.execute(sql,(username,password))

res = cursor.fetchall()

if res:

print(res)

else:

print('賬號密碼錯誤')

不單單是登陸驗證,在插入資料的時候也不要用這種格式

"select * from userinfo where name='%s'and password='%s'"%(username,password)

登陸和插入的時候都要以這種格式(安全格式)

sql = "select * from userinfo where name=%sand password=%s"

cursor.execute(sql,(username,password))

9 sql 中對日期的處理

1 查詢當天的使用量 select count count from t user where date create time select curdate 2查詢本週 select from t user where yearweek date format create time,y m d ...

SQL注入漏洞的防案

1 輸入過濾,對於整數,判斷變數是否符合 0 9 的值 其他限定值,也可以進行合法性校驗 對於字串,對sql語句特殊字元進行轉義 單引號轉成兩個單引號,雙引號轉成兩個雙引號 mysql也有類似的轉義函式mysql escape string和mysql real escape string。asp的...

判斷sql注入漏洞的型別

通常 sql 注入漏洞分為 2 種型別 其實所有的型別都是根據資料庫本身表的型別所產生的,在我們建立表的時候會發現其後總有個資料型別的限制,而不同的資料庫又有不同的資料型別,但是無論怎麼分常用的查詢資料型別總是以數字與字元來區分的,所以就會產生注入點為何種型別。5.2.1 數字型判斷 當輸入的參 x...